blob: a1df1b6a61ff0d34596890a8759e18ba00b9f782 [file] [log] [blame]
Jian Li49109b52019-01-22 00:17:28 +09001/*
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 */
16package org.onosproject.k8snode.impl;
17
18import com.google.common.collect.Lists;
19import com.google.common.util.concurrent.MoreExecutors;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.junit.TestUtils;
24import org.onlab.packet.ChassisId;
25import org.onlab.packet.IpAddress;
Jian Li6d2ffbf2020-11-04 15:58:18 +090026import org.onlab.packet.MacAddress;
Jian Li49109b52019-01-22 00:17:28 +090027import org.onosproject.cluster.ClusterServiceAdapter;
28import org.onosproject.cluster.LeadershipServiceAdapter;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreServiceAdapter;
31import org.onosproject.core.DefaultApplicationId;
32import org.onosproject.event.Event;
33import org.onosproject.k8snode.api.DefaultK8sNode;
34import org.onosproject.k8snode.api.K8sNode;
35import org.onosproject.k8snode.api.K8sNodeEvent;
Jian Li6d2ffbf2020-11-04 15:58:18 +090036import org.onosproject.k8snode.api.K8sNodeInfo;
Jian Li49109b52019-01-22 00:17:28 +090037import org.onosproject.k8snode.api.K8sNodeListener;
38import org.onosproject.k8snode.api.K8sNodeState;
39import org.onosproject.net.DefaultDevice;
40import org.onosproject.net.Device;
41import org.onosproject.net.DeviceId;
42import org.onosproject.net.provider.ProviderId;
43import org.onosproject.store.service.TestStorageService;
44
45import java.util.List;
46
47import static org.junit.Assert.assertEquals;
48import static org.junit.Assert.assertNotNull;
49import static org.junit.Assert.assertNull;
50import static org.junit.Assert.assertTrue;
51import static org.onosproject.k8snode.api.K8sNode.Type.MINION;
52import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_COMPLETE;
53import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_CREATED;
54import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_INCOMPLETE;
55import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_REMOVED;
56import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_UPDATED;
57import static org.onosproject.k8snode.api.K8sNodeState.COMPLETE;
58import static org.onosproject.k8snode.api.K8sNodeState.INCOMPLETE;
59import static org.onosproject.k8snode.api.K8sNodeState.INIT;
60import static org.onosproject.net.Device.Type.SWITCH;
61
62/**
63 * Unit tests for Kubernetes node manager.
64 */
65public class K8sNodeManagerTest {
66
67 private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
68
69 private static final String ERR_SIZE = "Number of nodes did not match";
70 private static final String ERR_NOT_MATCH = "Node did not match";
71 private static final String ERR_NOT_FOUND = "Node did not exist";
72
Jian Lie2a04ce2020-07-01 19:07:02 +090073 private static final String CLUSTER_NAME = "kubernetes";
74
Jian Li49109b52019-01-22 00:17:28 +090075 private static final String MINION_1_HOSTNAME = "minion_1";
76 private static final String MINION_2_HOSTNAME = "minion_2";
77 private static final String MINION_3_HOSTNAME = "minion_3";
78
Jian Li6d2ffbf2020-11-04 15:58:18 +090079 private static final IpAddress NODE_IP = IpAddress.valueOf("30.30.30.30");
80 private static final MacAddress NODE_MAC = MacAddress.valueOf("fa:00:00:00:00:08");
81 private static final K8sNodeInfo NODE_INFO = new K8sNodeInfo(NODE_IP, NODE_MAC);
82
Jian Li49109b52019-01-22 00:17:28 +090083 private static final Device MINION_1_INTG_DEVICE = createDevice(1);
84 private static final Device MINION_2_INTG_DEVICE = createDevice(2);
85 private static final Device MINION_3_INTG_DEVICE = createDevice(3);
86
Jian Libf562c22019-04-15 18:07:14 +090087 private static final Device MINION_1_EXT_DEVICE = createDevice(4);
88 private static final Device MINION_2_EXT_DEVICE = createDevice(5);
89 private static final Device MINION_3_EXT_DEVICE = createDevice(6);
90
Jian Li1a2eb5d2019-08-27 02:07:05 +090091 private static final Device MINION_1_LOCAL_DEVICE = createDevice(7);
92 private static final Device MINION_2_LOCAL_DEVICE = createDevice(8);
93 private static final Device MINION_3_LOCAL_DEVICE = createDevice(9);
94
Jian Lie2a04ce2020-07-01 19:07:02 +090095 private static final Device MINION_1_TUN_DEVICE = createDevice(10);
96 private static final Device MINION_2_TUN_DEVICE = createDevice(11);
97 private static final Device MINION_3_TUN_DEVICE = createDevice(12);
98
Jian Li1a2eb5d2019-08-27 02:07:05 +090099
Jian Li49109b52019-01-22 00:17:28 +0900100 private static final K8sNode MINION_1 = createNode(
Jian Lie2a04ce2020-07-01 19:07:02 +0900101 CLUSTER_NAME,
Jian Li49109b52019-01-22 00:17:28 +0900102 MINION_1_HOSTNAME,
103 MINION,
104 MINION_1_INTG_DEVICE,
Jian Libf562c22019-04-15 18:07:14 +0900105 MINION_1_EXT_DEVICE,
Jian Li1a2eb5d2019-08-27 02:07:05 +0900106 MINION_1_LOCAL_DEVICE,
Jian Lie2a04ce2020-07-01 19:07:02 +0900107 MINION_1_TUN_DEVICE,
Jian Li49109b52019-01-22 00:17:28 +0900108 IpAddress.valueOf("10.100.0.1"),
Jian Li6d2ffbf2020-11-04 15:58:18 +0900109 NODE_INFO,
Jian Li49109b52019-01-22 00:17:28 +0900110 INIT
111 );
112 private static final K8sNode MINION_2 = createNode(
Jian Lie2a04ce2020-07-01 19:07:02 +0900113 CLUSTER_NAME,
Jian Li49109b52019-01-22 00:17:28 +0900114 MINION_2_HOSTNAME,
115 MINION,
116 MINION_2_INTG_DEVICE,
Jian Libf562c22019-04-15 18:07:14 +0900117 MINION_2_EXT_DEVICE,
Jian Li1a2eb5d2019-08-27 02:07:05 +0900118 MINION_2_LOCAL_DEVICE,
Jian Lie2a04ce2020-07-01 19:07:02 +0900119 MINION_2_TUN_DEVICE,
Jian Li49109b52019-01-22 00:17:28 +0900120 IpAddress.valueOf("10.100.0.2"),
Jian Li6d2ffbf2020-11-04 15:58:18 +0900121 NODE_INFO,
Jian Li49109b52019-01-22 00:17:28 +0900122 INIT
123 );
124 private static final K8sNode MINION_3 = createNode(
Jian Lie2a04ce2020-07-01 19:07:02 +0900125 CLUSTER_NAME,
Jian Li49109b52019-01-22 00:17:28 +0900126 MINION_3_HOSTNAME,
127 MINION,
128 MINION_3_INTG_DEVICE,
Jian Libf562c22019-04-15 18:07:14 +0900129 MINION_3_EXT_DEVICE,
Jian Li1a2eb5d2019-08-27 02:07:05 +0900130 MINION_3_LOCAL_DEVICE,
Jian Lie2a04ce2020-07-01 19:07:02 +0900131 MINION_3_TUN_DEVICE,
Jian Li49109b52019-01-22 00:17:28 +0900132 IpAddress.valueOf("10.100.0.3"),
Jian Li6d2ffbf2020-11-04 15:58:18 +0900133 NODE_INFO,
Jian Li49109b52019-01-22 00:17:28 +0900134 COMPLETE
135 );
136
137 private final TestK8sNodeListener testListener = new TestK8sNodeListener();
138
139 private K8sNodeManager target;
140 private DistributedK8sNodeStore nodeStore;
141
142 /**
143 * Initial setup for this unit test.
144 */
145 @Before
146 public void setUp() {
147 nodeStore = new DistributedK8sNodeStore();
148 TestUtils.setField(nodeStore, "coreService", new TestCoreService());
149 TestUtils.setField(nodeStore, "storageService", new TestStorageService());
150 TestUtils.setField(nodeStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
151 nodeStore.activate();
152
153 nodeStore.createNode(MINION_2);
154 nodeStore.createNode(MINION_3);
155
156 target = new K8sNodeManager();
157 target.storageService = new TestStorageService();
158 target.coreService = new TestCoreService();
159 target.clusterService = new TestClusterService();
160 target.leadershipService = new TestLeadershipService();
161 target.nodeStore = nodeStore;
162 target.addListener(testListener);
163 target.activate();
164 testListener.events.clear();
165 }
166
167 /**
168 * Clean up unit test.
169 */
170 @After
171 public void tearDown() {
172 target.removeListener(testListener);
173 target.deactivate();
174 nodeStore.deactivate();
175 nodeStore = null;
176 target = null;
177 }
178
179 /**
180 * Checks if creating and removing a node work well with proper events.
181 */
182 @Test
183 public void testCreateAndRemoveNode() {
184 target.createNode(MINION_1);
185 assertEquals(ERR_SIZE, 3, target.nodes().size());
186 assertNotNull(target.node(MINION_1_HOSTNAME));
187
188 target.removeNode(MINION_1_HOSTNAME);
189 assertEquals(ERR_SIZE, 2, target.nodes().size());
190 assertNull(target.node(MINION_1_HOSTNAME));
191
192 validateEvents(K8S_NODE_CREATED, K8S_NODE_REMOVED);
193 }
194
195 /**
196 * Checks if creating null node fails with proper exception.
197 */
198 @Test(expected = NullPointerException.class)
199 public void testCreateNullNode() {
200 target.createNode(null);
201 }
202
203 /**
204 * Checks if creating a duplicated node fails with proper exception.
205 */
206 @Test(expected = IllegalArgumentException.class)
207 public void testCreateDuplicateNode() {
208 target.createNode(MINION_1);
209 target.createNode(MINION_1);
210 }
211
212 /**
213 * Checks if removing null node fails with proper exception.
214 */
215 @Test(expected = IllegalArgumentException.class)
216 public void testRemoveNullNode() {
217 target.removeNode(null);
218 }
219
220 /**
221 * Checks if updating a node works well with proper event.
222 */
223 @Test
224 public void testUpdateNode() {
225 K8sNode updated = DefaultK8sNode.from(MINION_2)
226 .dataIp(IpAddress.valueOf("10.200.0.100"))
227 .build();
228 target.updateNode(updated);
229 assertEquals(ERR_NOT_MATCH, updated, target.node(MINION_2_INTG_DEVICE.id()));
230 validateEvents(K8S_NODE_UPDATED);
231 }
232
233 /**
234 * Checks if updating a node state to complete generates proper events.
235 */
236 @Test
237 public void testUpdateNodeStateComplete() {
238 K8sNode updated = DefaultK8sNode.from(MINION_2)
239 .state(COMPLETE)
240 .build();
241 target.updateNode(updated);
242 assertEquals(ERR_NOT_MATCH, updated, target.node(MINION_2_HOSTNAME));
243 validateEvents(K8S_NODE_UPDATED, K8S_NODE_COMPLETE);
244 }
245
246 /**
247 * Checks if updating a node state to incomplete generates proper events.
248 */
249 @Test
250 public void testUpdateNodeStateIncomplete() {
251 K8sNode updated = DefaultK8sNode.from(MINION_3)
252 .state(INCOMPLETE)
253 .build();
254 target.updateNode(updated);
255 assertEquals(ERR_NOT_MATCH, updated, target.node(MINION_3_HOSTNAME));
256 validateEvents(K8S_NODE_UPDATED, K8S_NODE_INCOMPLETE);
257 }
258
259 /**
260 * Checks if updating a null node fails with proper exception.
261 */
262 @Test(expected = NullPointerException.class)
263 public void testUpdateNullNode() {
264 target.updateNode(null);
265 }
266
267 /**
268 * Checks if updating not existing node fails with proper exception.
269 */
Jian Li1cee9882019-02-13 11:25:25 +0900270 @Test(expected = NullPointerException.class)
Jian Li49109b52019-01-22 00:17:28 +0900271 public void testUpdateNotExistingNode() {
272 target.updateNode(MINION_1);
273 }
274
275 /**
276 * Checks if getting all nodes method returns correct set of nodes.
277 */
278 @Test
279 public void testGetAllNodes() {
280 assertEquals(ERR_SIZE, 2, target.nodes().size());
281 assertTrue(ERR_NOT_FOUND, target.nodes().contains(MINION_2));
282 assertTrue(ERR_NOT_FOUND, target.nodes().contains(MINION_3));
283 }
284
285 /**
286 * Checks if getting complete nodes method returns correct set of nodes.
287 */
288 @Test
289 public void testGetCompleteNodes() {
290 assertEquals(ERR_SIZE, 1, target.completeNodes().size());
291 assertTrue(ERR_NOT_FOUND, target.completeNodes().contains(MINION_3));
292 }
293
294 /**
295 * Checks if getting nodes by type method returns correct set of nodes.
296 */
297 @Test
298 public void testGetNodesByType() {
299 assertEquals(ERR_SIZE, 2, target.nodes(MINION).size());
300 assertTrue(ERR_NOT_FOUND, target.nodes(MINION).contains(MINION_2));
301 assertTrue(ERR_NOT_FOUND, target.nodes(MINION).contains(MINION_3));
302 }
303
304 /**
305 * Checks if getting a node by hostname returns correct node.
306 */
307 @Test
308 public void testGetNodeByHostname() {
309 assertEquals(ERR_NOT_FOUND, target.node(MINION_2_HOSTNAME), MINION_2);
310 assertEquals(ERR_NOT_FOUND, target.node(MINION_3_HOSTNAME), MINION_3);
311 }
312
313 private void validateEvents(Enum... types) {
314 int i = 0;
315 assertEquals("Number of events did not match", types.length, testListener.events.size());
316 for (Event event : testListener.events) {
317 assertEquals("Incorrect event received", types[i], event.type());
318 i++;
319 }
320 testListener.events.clear();
321 }
322
323 private static Device createDevice(long devIdNum) {
324 return new DefaultDevice(new ProviderId("of", "foo"),
325 DeviceId.deviceId(String.format("of:%016d", devIdNum)),
326 SWITCH,
327 "manufacturer",
328 "hwVersion",
329 "swVersion",
330 "serialNumber",
331 new ChassisId(1));
332 }
333
334 private static class TestK8sNodeListener implements K8sNodeListener {
335 private List<K8sNodeEvent> events = Lists.newArrayList();
336
337 @Override
338 public void event(K8sNodeEvent event) {
339 events.add(event);
340 }
341 }
342
343 private static class TestCoreService extends CoreServiceAdapter {
344 @Override
345 public ApplicationId registerApplication(String name) {
346 return TEST_APP_ID;
347 }
348 }
349
350 private class TestClusterService extends ClusterServiceAdapter {
351
352 }
353
354 private static class TestLeadershipService extends LeadershipServiceAdapter {
355
356 }
357
Jian Lie2a04ce2020-07-01 19:07:02 +0900358 private static K8sNode createNode(String clusterName, String hostname, K8sNode.Type type,
Jian Libf562c22019-04-15 18:07:14 +0900359 Device intgBridge, Device extBridge,
Jian Lie2a04ce2020-07-01 19:07:02 +0900360 Device localBridge, Device tunBridge,
Jian Li6d2ffbf2020-11-04 15:58:18 +0900361 IpAddress ipAddr, K8sNodeInfo nodeInfo,
362 K8sNodeState state) {
Jian Li49109b52019-01-22 00:17:28 +0900363 return DefaultK8sNode.builder()
364 .hostname(hostname)
Jian Lie2a04ce2020-07-01 19:07:02 +0900365 .clusterName(clusterName)
Jian Li49109b52019-01-22 00:17:28 +0900366 .type(type)
367 .intgBridge(intgBridge.id())
Jian Libf562c22019-04-15 18:07:14 +0900368 .extBridge(extBridge.id())
Jian Li1a2eb5d2019-08-27 02:07:05 +0900369 .localBridge(localBridge.id())
Jian Lie2a04ce2020-07-01 19:07:02 +0900370 .tunBridge(tunBridge.id())
Jian Li49109b52019-01-22 00:17:28 +0900371 .managementIp(ipAddr)
372 .dataIp(ipAddr)
Jian Li6d2ffbf2020-11-04 15:58:18 +0900373 .nodeInfo(nodeInfo)
Jian Li49109b52019-01-22 00:17:28 +0900374 .state(state)
375 .build();
376 }
377}