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