Ayaka Koshibe | 406d010 | 2014-09-24 16:08:12 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.cluster.impl; |
| 2 | |
| 3 | import java.util.Set; |
| 4 | |
| 5 | import org.junit.After; |
| 6 | import org.junit.Before; |
| 7 | import org.junit.Test; |
| 8 | import org.onlab.onos.cluster.ClusterEventListener; |
| 9 | import org.onlab.onos.cluster.ClusterService; |
| 10 | import org.onlab.onos.cluster.ControllerNode; |
| 11 | import org.onlab.onos.cluster.ControllerNode.State; |
| 12 | import org.onlab.onos.cluster.DefaultControllerNode; |
| 13 | import org.onlab.onos.cluster.MastershipService; |
| 14 | import org.onlab.onos.cluster.NodeId; |
| 15 | import org.onlab.onos.event.impl.TestEventDispatcher; |
| 16 | import org.onlab.onos.net.DeviceId; |
| 17 | import org.onlab.onos.net.trivial.impl.SimpleMastershipStore; |
| 18 | import org.onlab.packet.IpPrefix; |
| 19 | |
| 20 | import static org.junit.Assert.assertEquals; |
| 21 | import static org.onlab.onos.net.MastershipRole.*; |
| 22 | |
| 23 | /** |
| 24 | * Test codifying the mastership service contracts. |
| 25 | */ |
| 26 | public class MastershipManagerTest { |
| 27 | |
| 28 | private static final NodeId NID_LOCAL = new NodeId("local"); |
| 29 | private static final NodeId NID_OTHER = new NodeId("foo"); |
| 30 | private static final IpPrefix LOCALHOST = IpPrefix.valueOf("127.0.0.1"); |
| 31 | private static final DeviceId DEV_MASTER = DeviceId.deviceId("of:1"); |
| 32 | private static final DeviceId DEV_OTHER = DeviceId.deviceId("of:2"); |
| 33 | |
| 34 | private MastershipManager mgr; |
| 35 | protected MastershipService service; |
| 36 | |
| 37 | @Before |
| 38 | public void setUp() { |
| 39 | mgr = new MastershipManager(); |
| 40 | service = mgr; |
| 41 | mgr.store = new SimpleMastershipStore(); |
| 42 | mgr.eventDispatcher = new TestEventDispatcher(); |
| 43 | mgr.clusterService = new TestClusterService(); |
| 44 | mgr.activate(); |
| 45 | } |
| 46 | |
| 47 | @After |
| 48 | public void tearDown() { |
| 49 | mgr.deactivate(); |
| 50 | mgr.clusterService = null; |
| 51 | mgr.eventDispatcher = null; |
| 52 | mgr.store = null; |
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void setRole() { |
| 57 | mgr.setRole(NID_OTHER, DEV_MASTER, MASTER); |
| 58 | assertEquals("wrong local role:", STANDBY, mgr.getLocalRole(DEV_MASTER)); |
| 59 | |
| 60 | //set to master |
| 61 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 62 | assertEquals("wrong local role:", MASTER, mgr.getLocalRole(DEV_MASTER)); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void relinquishMastership() { |
| 67 | //TODO |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void requestRoleFor() { |
| 72 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 73 | mgr.setRole(NID_OTHER, DEV_OTHER, MASTER); |
| 74 | |
| 75 | //local should be master for one but standby for other |
| 76 | assertEquals("wrong role:", MASTER, mgr.requestRoleFor(DEV_MASTER)); |
| 77 | assertEquals("wrong role:", STANDBY, mgr.requestRoleFor(DEV_OTHER)); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void getMasterFor() { |
| 82 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 83 | mgr.setRole(NID_OTHER, DEV_OTHER, MASTER); |
| 84 | assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_MASTER)); |
| 85 | assertEquals("wrong master:", NID_OTHER, mgr.getMasterFor(DEV_OTHER)); |
| 86 | |
| 87 | //have NID_OTHER hand over DEV_OTHER to NID_LOCAL |
| 88 | mgr.setRole(NID_LOCAL, DEV_OTHER, MASTER); |
| 89 | assertEquals("wrong master:", NID_LOCAL, mgr.getMasterFor(DEV_OTHER)); |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void getDevicesOf() { |
| 94 | mgr.setRole(NID_LOCAL, DEV_MASTER, MASTER); |
| 95 | mgr.setRole(NID_LOCAL, DEV_OTHER, STANDBY); |
| 96 | assertEquals("should be one device:", 1, mgr.getDevicesOf(NID_LOCAL).size()); |
| 97 | |
| 98 | //hand both devices to NID_LOCAL |
| 99 | mgr.setRole(NID_LOCAL, DEV_OTHER, MASTER); |
| 100 | assertEquals("should be two devices:", 2, mgr.getDevicesOf(NID_LOCAL).size()); |
| 101 | } |
| 102 | |
| 103 | private final class TestClusterService implements ClusterService { |
| 104 | |
| 105 | ControllerNode local = new DefaultControllerNode(NID_LOCAL, LOCALHOST); |
| 106 | |
| 107 | @Override |
| 108 | public ControllerNode getLocalNode() { |
| 109 | return local; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public Set<ControllerNode> getNodes() { |
| 114 | return null; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public ControllerNode getNode(NodeId nodeId) { |
| 119 | return null; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public State getState(NodeId nodeId) { |
| 124 | return null; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void addListener(ClusterEventListener listener) { |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void removeListener(ClusterEventListener listener) { |
| 133 | } |
| 134 | |
| 135 | } |
| 136 | } |