tom | ea961ff | 2014-10-01 12:45:15 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.trivial.impl; |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 2 | |
| 3 | import java.util.Set; |
| 4 | import java.util.concurrent.atomic.AtomicInteger; |
| 5 | |
| 6 | import org.junit.After; |
| 7 | import org.junit.Before; |
| 8 | import org.junit.Test; |
| 9 | import org.onlab.onos.cluster.MastershipTerm; |
| 10 | import org.onlab.onos.cluster.NodeId; |
| 11 | import org.onlab.onos.net.DeviceId; |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 12 | |
| 13 | import com.google.common.collect.Sets; |
| 14 | |
| 15 | import static org.junit.Assert.assertEquals; |
| 16 | import static org.junit.Assert.assertNull; |
| 17 | import static org.junit.Assert.assertTrue; |
| 18 | import static org.onlab.onos.net.MastershipRole.*; |
| 19 | import static org.onlab.onos.cluster.MastershipEvent.Type.*; |
| 20 | |
| 21 | /** |
| 22 | * Test for the simple MastershipStore implementation. |
| 23 | */ |
| 24 | public class SimpleMastershipStoreTest { |
| 25 | |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 26 | private static final DeviceId DID1 = DeviceId.deviceId("of:01"); |
| 27 | private static final DeviceId DID2 = DeviceId.deviceId("of:02"); |
| 28 | private static final DeviceId DID3 = DeviceId.deviceId("of:03"); |
| 29 | private static final DeviceId DID4 = DeviceId.deviceId("of:04"); |
| 30 | |
| 31 | private static final NodeId N1 = new NodeId("local"); |
| 32 | private static final NodeId N2 = new NodeId("other"); |
| 33 | |
| 34 | private SimpleMastershipStore sms; |
| 35 | |
| 36 | @Before |
| 37 | public void setUp() throws Exception { |
| 38 | sms = new SimpleMastershipStore(); |
| 39 | sms.activate(); |
| 40 | } |
| 41 | |
| 42 | @After |
| 43 | public void tearDown() throws Exception { |
| 44 | sms.deactivate(); |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void getRole() { |
| 49 | //special case, no backup or master |
| 50 | put(DID1, N1, false, false); |
| 51 | assertEquals("wrong role", NONE, sms.getRole(N1, DID1)); |
| 52 | |
| 53 | //backup exists but we aren't mapped |
| 54 | put(DID2, N1, false, true); |
| 55 | assertEquals("wrong role", STANDBY, sms.getRole(N1, DID2)); |
| 56 | |
| 57 | //N2 is master |
| 58 | put(DID3, N2, true, true); |
| 59 | assertEquals("wrong role", MASTER, sms.getRole(N2, DID3)); |
| 60 | |
| 61 | //N2 is master but N1 is only in backups set |
| 62 | put(DID4, N2, true, false); |
| 63 | assertEquals("wrong role", STANDBY, sms.getRole(N1, DID4)); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void getMaster() { |
| 68 | put(DID3, N2, true, true); |
| 69 | assertEquals("wrong role", MASTER, sms.getRole(N2, DID3)); |
| 70 | assertEquals("wrong device", N2, sms.getMaster(DID3)); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void setMaster() { |
| 75 | put(DID1, N1, false, false); |
| 76 | assertEquals("wrong event", MASTER_CHANGED, sms.setMaster(N1, DID1).type()); |
| 77 | assertEquals("wrong role", MASTER, sms.getRole(N1, DID1)); |
| 78 | //set node that's already master - should be ignored |
| 79 | assertNull("wrong event", sms.setMaster(N1, DID1)); |
| 80 | |
| 81 | //set STANDBY to MASTER |
| 82 | put(DID2, N1, false, true); |
| 83 | assertEquals("wrong role", STANDBY, sms.getRole(N1, DID2)); |
| 84 | assertEquals("wrong event", MASTER_CHANGED, sms.setMaster(N1, DID2).type()); |
| 85 | assertEquals("wrong role", MASTER, sms.getRole(N1, DID2)); |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void getDevices() { |
| 90 | Set<DeviceId> d = Sets.newHashSet(DID1, DID2); |
| 91 | |
| 92 | put(DID1, N2, true, true); |
| 93 | put(DID2, N2, true, true); |
| 94 | put(DID3, N1, true, true); |
| 95 | assertTrue("wrong devices", d.equals(sms.getDevices(N2))); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void getTermFor() { |
| 100 | put(DID1, N1, true, true); |
| 101 | assertEquals("wrong term", MastershipTerm.of(N1, 0), sms.getTermFor(DID1)); |
| 102 | |
| 103 | //switch to N2 and back - 2 term switches |
| 104 | sms.setMaster(N2, DID1); |
| 105 | sms.setMaster(N1, DID1); |
| 106 | assertEquals("wrong term", MastershipTerm.of(N1, 2), sms.getTermFor(DID1)); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void requestRole() { |
| 111 | //NONE - become MASTER |
| 112 | put(DID1, N1, false, false); |
| 113 | assertEquals("wrong role", MASTER, sms.requestRole(DID1)); |
| 114 | |
| 115 | //STANDBY without backup - become MASTER |
| 116 | put(DID2, N1, false, true); |
| 117 | assertEquals("wrong role", MASTER, sms.requestRole(DID2)); |
| 118 | |
| 119 | //STANDBY with backup - stay STANDBY |
| 120 | put(DID3, N2, false, true); |
| 121 | assertEquals("wrong role", STANDBY, sms.requestRole(DID3)); |
| 122 | |
| 123 | //local (N1) is MASTER - stay MASTER |
| 124 | put(DID4, N1, true, true); |
| 125 | assertEquals("wrong role", MASTER, sms.requestRole(DID4)); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void unsetMaster() { |
| 130 | //NONE - record backup but take no other action |
| 131 | put(DID1, N1, false, false); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame^] | 132 | sms.setStandby(N1, DID1); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 133 | assertTrue("not backed up", sms.backups.contains(N1)); |
| 134 | sms.termMap.clear(); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame^] | 135 | sms.setStandby(N1, DID1); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 136 | assertTrue("term not set", sms.termMap.containsKey(DID1)); |
| 137 | |
| 138 | //no backup, MASTER |
| 139 | put(DID1, N1, true, true); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame^] | 140 | assertNull("wrong event", sms.setStandby(N1, DID1)); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 141 | assertNull("wrong node", sms.masterMap.get(DID1)); |
| 142 | |
| 143 | //backup, switch |
| 144 | sms.masterMap.clear(); |
| 145 | put(DID1, N1, true, true); |
| 146 | put(DID2, N2, true, true); |
Ayaka Koshibe | c404770 | 2014-10-07 14:43:52 -0700 | [diff] [blame^] | 147 | assertEquals("wrong event", MASTER_CHANGED, sms.setStandby(N1, DID1).type()); |
Ayaka Koshibe | 1743ddb | 2014-09-30 15:24:48 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | //helper to populate master/backup structures |
| 151 | private void put(DeviceId dev, NodeId node, boolean store, boolean backup) { |
| 152 | if (store) { |
| 153 | sms.masterMap.put(dev, node); |
| 154 | } |
| 155 | if (backup) { |
| 156 | sms.backups.add(node); |
| 157 | } |
| 158 | sms.termMap.put(dev, new AtomicInteger()); |
| 159 | } |
| 160 | } |