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