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