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