Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.k8snode.impl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.util.concurrent.MoreExecutors; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.junit.TestUtils; |
| 24 | import org.onlab.packet.ChassisId; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onosproject.cluster.ClusterServiceAdapter; |
| 27 | import org.onosproject.cluster.LeadershipServiceAdapter; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreServiceAdapter; |
| 30 | import org.onosproject.core.DefaultApplicationId; |
| 31 | import org.onosproject.event.Event; |
| 32 | import org.onosproject.k8snode.api.DefaultK8sNode; |
| 33 | import org.onosproject.k8snode.api.K8sNode; |
| 34 | import org.onosproject.k8snode.api.K8sNodeEvent; |
| 35 | import org.onosproject.k8snode.api.K8sNodeListener; |
| 36 | import org.onosproject.k8snode.api.K8sNodeState; |
| 37 | import org.onosproject.net.DefaultDevice; |
| 38 | import org.onosproject.net.Device; |
| 39 | import org.onosproject.net.DeviceId; |
| 40 | import org.onosproject.net.provider.ProviderId; |
| 41 | import org.onosproject.store.service.TestStorageService; |
| 42 | |
| 43 | import java.util.List; |
| 44 | |
| 45 | import static org.junit.Assert.assertEquals; |
| 46 | import static org.junit.Assert.assertNotNull; |
| 47 | import static org.junit.Assert.assertNull; |
| 48 | import static org.junit.Assert.assertTrue; |
| 49 | import static org.onosproject.k8snode.api.K8sNode.Type.MINION; |
| 50 | import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_COMPLETE; |
| 51 | import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_CREATED; |
| 52 | import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_INCOMPLETE; |
| 53 | import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_REMOVED; |
| 54 | import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_UPDATED; |
| 55 | import static org.onosproject.k8snode.api.K8sNodeState.COMPLETE; |
| 56 | import static org.onosproject.k8snode.api.K8sNodeState.INCOMPLETE; |
| 57 | import static org.onosproject.k8snode.api.K8sNodeState.INIT; |
| 58 | import static org.onosproject.net.Device.Type.SWITCH; |
| 59 | |
| 60 | /** |
| 61 | * Unit tests for Kubernetes node manager. |
| 62 | */ |
| 63 | public class K8sNodeManagerTest { |
| 64 | |
| 65 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 66 | |
| 67 | private static final String ERR_SIZE = "Number of nodes did not match"; |
| 68 | private static final String ERR_NOT_MATCH = "Node did not match"; |
| 69 | private static final String ERR_NOT_FOUND = "Node did not exist"; |
| 70 | |
| 71 | private static final String MINION_1_HOSTNAME = "minion_1"; |
| 72 | private static final String MINION_2_HOSTNAME = "minion_2"; |
| 73 | private static final String MINION_3_HOSTNAME = "minion_3"; |
| 74 | |
| 75 | private static final Device MINION_1_INTG_DEVICE = createDevice(1); |
| 76 | private static final Device MINION_2_INTG_DEVICE = createDevice(2); |
| 77 | private static final Device MINION_3_INTG_DEVICE = createDevice(3); |
| 78 | |
| 79 | private static final K8sNode MINION_1 = createNode( |
| 80 | MINION_1_HOSTNAME, |
| 81 | MINION, |
| 82 | MINION_1_INTG_DEVICE, |
| 83 | IpAddress.valueOf("10.100.0.1"), |
| 84 | INIT |
| 85 | ); |
| 86 | private static final K8sNode MINION_2 = createNode( |
| 87 | MINION_2_HOSTNAME, |
| 88 | MINION, |
| 89 | MINION_2_INTG_DEVICE, |
| 90 | IpAddress.valueOf("10.100.0.2"), |
| 91 | INIT |
| 92 | ); |
| 93 | private static final K8sNode MINION_3 = createNode( |
| 94 | MINION_3_HOSTNAME, |
| 95 | MINION, |
| 96 | MINION_3_INTG_DEVICE, |
| 97 | IpAddress.valueOf("10.100.0.3"), |
| 98 | COMPLETE |
| 99 | ); |
| 100 | |
| 101 | private final TestK8sNodeListener testListener = new TestK8sNodeListener(); |
| 102 | |
| 103 | private K8sNodeManager target; |
| 104 | private DistributedK8sNodeStore nodeStore; |
| 105 | |
| 106 | /** |
| 107 | * Initial setup for this unit test. |
| 108 | */ |
| 109 | @Before |
| 110 | public void setUp() { |
| 111 | nodeStore = new DistributedK8sNodeStore(); |
| 112 | TestUtils.setField(nodeStore, "coreService", new TestCoreService()); |
| 113 | TestUtils.setField(nodeStore, "storageService", new TestStorageService()); |
| 114 | TestUtils.setField(nodeStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
| 115 | nodeStore.activate(); |
| 116 | |
| 117 | nodeStore.createNode(MINION_2); |
| 118 | nodeStore.createNode(MINION_3); |
| 119 | |
| 120 | target = new K8sNodeManager(); |
| 121 | target.storageService = new TestStorageService(); |
| 122 | target.coreService = new TestCoreService(); |
| 123 | target.clusterService = new TestClusterService(); |
| 124 | target.leadershipService = new TestLeadershipService(); |
| 125 | target.nodeStore = nodeStore; |
| 126 | target.addListener(testListener); |
| 127 | target.activate(); |
| 128 | testListener.events.clear(); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Clean up unit test. |
| 133 | */ |
| 134 | @After |
| 135 | public void tearDown() { |
| 136 | target.removeListener(testListener); |
| 137 | target.deactivate(); |
| 138 | nodeStore.deactivate(); |
| 139 | nodeStore = null; |
| 140 | target = null; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Checks if creating and removing a node work well with proper events. |
| 145 | */ |
| 146 | @Test |
| 147 | public void testCreateAndRemoveNode() { |
| 148 | target.createNode(MINION_1); |
| 149 | assertEquals(ERR_SIZE, 3, target.nodes().size()); |
| 150 | assertNotNull(target.node(MINION_1_HOSTNAME)); |
| 151 | |
| 152 | target.removeNode(MINION_1_HOSTNAME); |
| 153 | assertEquals(ERR_SIZE, 2, target.nodes().size()); |
| 154 | assertNull(target.node(MINION_1_HOSTNAME)); |
| 155 | |
| 156 | validateEvents(K8S_NODE_CREATED, K8S_NODE_REMOVED); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Checks if creating null node fails with proper exception. |
| 161 | */ |
| 162 | @Test(expected = NullPointerException.class) |
| 163 | public void testCreateNullNode() { |
| 164 | target.createNode(null); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Checks if creating a duplicated node fails with proper exception. |
| 169 | */ |
| 170 | @Test(expected = IllegalArgumentException.class) |
| 171 | public void testCreateDuplicateNode() { |
| 172 | target.createNode(MINION_1); |
| 173 | target.createNode(MINION_1); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Checks if removing null node fails with proper exception. |
| 178 | */ |
| 179 | @Test(expected = IllegalArgumentException.class) |
| 180 | public void testRemoveNullNode() { |
| 181 | target.removeNode(null); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Checks if updating a node works well with proper event. |
| 186 | */ |
| 187 | @Test |
| 188 | public void testUpdateNode() { |
| 189 | K8sNode updated = DefaultK8sNode.from(MINION_2) |
| 190 | .dataIp(IpAddress.valueOf("10.200.0.100")) |
| 191 | .build(); |
| 192 | target.updateNode(updated); |
| 193 | assertEquals(ERR_NOT_MATCH, updated, target.node(MINION_2_INTG_DEVICE.id())); |
| 194 | validateEvents(K8S_NODE_UPDATED); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Checks if updating a node state to complete generates proper events. |
| 199 | */ |
| 200 | @Test |
| 201 | public void testUpdateNodeStateComplete() { |
| 202 | K8sNode updated = DefaultK8sNode.from(MINION_2) |
| 203 | .state(COMPLETE) |
| 204 | .build(); |
| 205 | target.updateNode(updated); |
| 206 | assertEquals(ERR_NOT_MATCH, updated, target.node(MINION_2_HOSTNAME)); |
| 207 | validateEvents(K8S_NODE_UPDATED, K8S_NODE_COMPLETE); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Checks if updating a node state to incomplete generates proper events. |
| 212 | */ |
| 213 | @Test |
| 214 | public void testUpdateNodeStateIncomplete() { |
| 215 | K8sNode updated = DefaultK8sNode.from(MINION_3) |
| 216 | .state(INCOMPLETE) |
| 217 | .build(); |
| 218 | target.updateNode(updated); |
| 219 | assertEquals(ERR_NOT_MATCH, updated, target.node(MINION_3_HOSTNAME)); |
| 220 | validateEvents(K8S_NODE_UPDATED, K8S_NODE_INCOMPLETE); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Checks if updating a null node fails with proper exception. |
| 225 | */ |
| 226 | @Test(expected = NullPointerException.class) |
| 227 | public void testUpdateNullNode() { |
| 228 | target.updateNode(null); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Checks if updating not existing node fails with proper exception. |
| 233 | */ |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 234 | @Test(expected = NullPointerException.class) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 235 | public void testUpdateNotExistingNode() { |
| 236 | target.updateNode(MINION_1); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Checks if getting all nodes method returns correct set of nodes. |
| 241 | */ |
| 242 | @Test |
| 243 | public void testGetAllNodes() { |
| 244 | assertEquals(ERR_SIZE, 2, target.nodes().size()); |
| 245 | assertTrue(ERR_NOT_FOUND, target.nodes().contains(MINION_2)); |
| 246 | assertTrue(ERR_NOT_FOUND, target.nodes().contains(MINION_3)); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Checks if getting complete nodes method returns correct set of nodes. |
| 251 | */ |
| 252 | @Test |
| 253 | public void testGetCompleteNodes() { |
| 254 | assertEquals(ERR_SIZE, 1, target.completeNodes().size()); |
| 255 | assertTrue(ERR_NOT_FOUND, target.completeNodes().contains(MINION_3)); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Checks if getting nodes by type method returns correct set of nodes. |
| 260 | */ |
| 261 | @Test |
| 262 | public void testGetNodesByType() { |
| 263 | assertEquals(ERR_SIZE, 2, target.nodes(MINION).size()); |
| 264 | assertTrue(ERR_NOT_FOUND, target.nodes(MINION).contains(MINION_2)); |
| 265 | assertTrue(ERR_NOT_FOUND, target.nodes(MINION).contains(MINION_3)); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Checks if getting a node by hostname returns correct node. |
| 270 | */ |
| 271 | @Test |
| 272 | public void testGetNodeByHostname() { |
| 273 | assertEquals(ERR_NOT_FOUND, target.node(MINION_2_HOSTNAME), MINION_2); |
| 274 | assertEquals(ERR_NOT_FOUND, target.node(MINION_3_HOSTNAME), MINION_3); |
| 275 | } |
| 276 | |
| 277 | private void validateEvents(Enum... types) { |
| 278 | int i = 0; |
| 279 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 280 | for (Event event : testListener.events) { |
| 281 | assertEquals("Incorrect event received", types[i], event.type()); |
| 282 | i++; |
| 283 | } |
| 284 | testListener.events.clear(); |
| 285 | } |
| 286 | |
| 287 | private static Device createDevice(long devIdNum) { |
| 288 | return new DefaultDevice(new ProviderId("of", "foo"), |
| 289 | DeviceId.deviceId(String.format("of:%016d", devIdNum)), |
| 290 | SWITCH, |
| 291 | "manufacturer", |
| 292 | "hwVersion", |
| 293 | "swVersion", |
| 294 | "serialNumber", |
| 295 | new ChassisId(1)); |
| 296 | } |
| 297 | |
| 298 | private static class TestK8sNodeListener implements K8sNodeListener { |
| 299 | private List<K8sNodeEvent> events = Lists.newArrayList(); |
| 300 | |
| 301 | @Override |
| 302 | public void event(K8sNodeEvent event) { |
| 303 | events.add(event); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | private static class TestCoreService extends CoreServiceAdapter { |
| 308 | @Override |
| 309 | public ApplicationId registerApplication(String name) { |
| 310 | return TEST_APP_ID; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | private class TestClusterService extends ClusterServiceAdapter { |
| 315 | |
| 316 | } |
| 317 | |
| 318 | private static class TestLeadershipService extends LeadershipServiceAdapter { |
| 319 | |
| 320 | } |
| 321 | |
| 322 | private static K8sNode createNode(String hostname, K8sNode.Type type, |
| 323 | Device intgBridge, IpAddress ipAddr, |
| 324 | K8sNodeState state) { |
| 325 | return DefaultK8sNode.builder() |
| 326 | .hostname(hostname) |
| 327 | .type(type) |
| 328 | .intgBridge(intgBridge.id()) |
| 329 | .managementIp(ipAddr) |
| 330 | .dataIp(ipAddr) |
| 331 | .state(state) |
| 332 | .build(); |
| 333 | } |
| 334 | } |