blob: 50d769f94c785eb23c61fbd617639e25d9784982 [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;
26import org.onosproject.cluster.ClusterServiceAdapter;
27import org.onosproject.cluster.LeadershipServiceAdapter;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreServiceAdapter;
30import org.onosproject.core.DefaultApplicationId;
31import org.onosproject.event.Event;
32import org.onosproject.k8snode.api.DefaultK8sNode;
33import org.onosproject.k8snode.api.K8sNode;
34import org.onosproject.k8snode.api.K8sNodeEvent;
35import org.onosproject.k8snode.api.K8sNodeListener;
36import org.onosproject.k8snode.api.K8sNodeState;
37import org.onosproject.net.DefaultDevice;
38import org.onosproject.net.Device;
39import org.onosproject.net.DeviceId;
40import org.onosproject.net.provider.ProviderId;
41import org.onosproject.store.service.TestStorageService;
42
43import java.util.List;
44
45import static org.junit.Assert.assertEquals;
46import static org.junit.Assert.assertNotNull;
47import static org.junit.Assert.assertNull;
48import static org.junit.Assert.assertTrue;
49import static org.onosproject.k8snode.api.K8sNode.Type.MINION;
50import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_COMPLETE;
51import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_CREATED;
52import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_INCOMPLETE;
53import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_REMOVED;
54import static org.onosproject.k8snode.api.K8sNodeEvent.Type.K8S_NODE_UPDATED;
55import static org.onosproject.k8snode.api.K8sNodeState.COMPLETE;
56import static org.onosproject.k8snode.api.K8sNodeState.INCOMPLETE;
57import static org.onosproject.k8snode.api.K8sNodeState.INIT;
58import static org.onosproject.net.Device.Type.SWITCH;
59
60/**
61 * Unit tests for Kubernetes node manager.
62 */
63public 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
Jian Libf562c22019-04-15 18:07:14 +090079 private static final Device MINION_1_EXT_DEVICE = createDevice(4);
80 private static final Device MINION_2_EXT_DEVICE = createDevice(5);
81 private static final Device MINION_3_EXT_DEVICE = createDevice(6);
82
Jian Li1a2eb5d2019-08-27 02:07:05 +090083 private static final Device MINION_1_LOCAL_DEVICE = createDevice(7);
84 private static final Device MINION_2_LOCAL_DEVICE = createDevice(8);
85 private static final Device MINION_3_LOCAL_DEVICE = createDevice(9);
86
87
Jian Li49109b52019-01-22 00:17:28 +090088 private static final K8sNode MINION_1 = createNode(
89 MINION_1_HOSTNAME,
90 MINION,
91 MINION_1_INTG_DEVICE,
Jian Libf562c22019-04-15 18:07:14 +090092 MINION_1_EXT_DEVICE,
Jian Li1a2eb5d2019-08-27 02:07:05 +090093 MINION_1_LOCAL_DEVICE,
Jian Li49109b52019-01-22 00:17:28 +090094 IpAddress.valueOf("10.100.0.1"),
95 INIT
96 );
97 private static final K8sNode MINION_2 = createNode(
98 MINION_2_HOSTNAME,
99 MINION,
100 MINION_2_INTG_DEVICE,
Jian Libf562c22019-04-15 18:07:14 +0900101 MINION_2_EXT_DEVICE,
Jian Li1a2eb5d2019-08-27 02:07:05 +0900102 MINION_2_LOCAL_DEVICE,
Jian Li49109b52019-01-22 00:17:28 +0900103 IpAddress.valueOf("10.100.0.2"),
104 INIT
105 );
106 private static final K8sNode MINION_3 = createNode(
107 MINION_3_HOSTNAME,
108 MINION,
109 MINION_3_INTG_DEVICE,
Jian Libf562c22019-04-15 18:07:14 +0900110 MINION_3_EXT_DEVICE,
Jian Li1a2eb5d2019-08-27 02:07:05 +0900111 MINION_3_LOCAL_DEVICE,
Jian Li49109b52019-01-22 00:17:28 +0900112 IpAddress.valueOf("10.100.0.3"),
113 COMPLETE
114 );
115
116 private final TestK8sNodeListener testListener = new TestK8sNodeListener();
117
118 private K8sNodeManager target;
119 private DistributedK8sNodeStore nodeStore;
120
121 /**
122 * Initial setup for this unit test.
123 */
124 @Before
125 public void setUp() {
126 nodeStore = new DistributedK8sNodeStore();
127 TestUtils.setField(nodeStore, "coreService", new TestCoreService());
128 TestUtils.setField(nodeStore, "storageService", new TestStorageService());
129 TestUtils.setField(nodeStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
130 nodeStore.activate();
131
132 nodeStore.createNode(MINION_2);
133 nodeStore.createNode(MINION_3);
134
135 target = new K8sNodeManager();
136 target.storageService = new TestStorageService();
137 target.coreService = new TestCoreService();
138 target.clusterService = new TestClusterService();
139 target.leadershipService = new TestLeadershipService();
140 target.nodeStore = nodeStore;
141 target.addListener(testListener);
142 target.activate();
143 testListener.events.clear();
144 }
145
146 /**
147 * Clean up unit test.
148 */
149 @After
150 public void tearDown() {
151 target.removeListener(testListener);
152 target.deactivate();
153 nodeStore.deactivate();
154 nodeStore = null;
155 target = null;
156 }
157
158 /**
159 * Checks if creating and removing a node work well with proper events.
160 */
161 @Test
162 public void testCreateAndRemoveNode() {
163 target.createNode(MINION_1);
164 assertEquals(ERR_SIZE, 3, target.nodes().size());
165 assertNotNull(target.node(MINION_1_HOSTNAME));
166
167 target.removeNode(MINION_1_HOSTNAME);
168 assertEquals(ERR_SIZE, 2, target.nodes().size());
169 assertNull(target.node(MINION_1_HOSTNAME));
170
171 validateEvents(K8S_NODE_CREATED, K8S_NODE_REMOVED);
172 }
173
174 /**
175 * Checks if creating null node fails with proper exception.
176 */
177 @Test(expected = NullPointerException.class)
178 public void testCreateNullNode() {
179 target.createNode(null);
180 }
181
182 /**
183 * Checks if creating a duplicated node fails with proper exception.
184 */
185 @Test(expected = IllegalArgumentException.class)
186 public void testCreateDuplicateNode() {
187 target.createNode(MINION_1);
188 target.createNode(MINION_1);
189 }
190
191 /**
192 * Checks if removing null node fails with proper exception.
193 */
194 @Test(expected = IllegalArgumentException.class)
195 public void testRemoveNullNode() {
196 target.removeNode(null);
197 }
198
199 /**
200 * Checks if updating a node works well with proper event.
201 */
202 @Test
203 public void testUpdateNode() {
204 K8sNode updated = DefaultK8sNode.from(MINION_2)
205 .dataIp(IpAddress.valueOf("10.200.0.100"))
206 .build();
207 target.updateNode(updated);
208 assertEquals(ERR_NOT_MATCH, updated, target.node(MINION_2_INTG_DEVICE.id()));
209 validateEvents(K8S_NODE_UPDATED);
210 }
211
212 /**
213 * Checks if updating a node state to complete generates proper events.
214 */
215 @Test
216 public void testUpdateNodeStateComplete() {
217 K8sNode updated = DefaultK8sNode.from(MINION_2)
218 .state(COMPLETE)
219 .build();
220 target.updateNode(updated);
221 assertEquals(ERR_NOT_MATCH, updated, target.node(MINION_2_HOSTNAME));
222 validateEvents(K8S_NODE_UPDATED, K8S_NODE_COMPLETE);
223 }
224
225 /**
226 * Checks if updating a node state to incomplete generates proper events.
227 */
228 @Test
229 public void testUpdateNodeStateIncomplete() {
230 K8sNode updated = DefaultK8sNode.from(MINION_3)
231 .state(INCOMPLETE)
232 .build();
233 target.updateNode(updated);
234 assertEquals(ERR_NOT_MATCH, updated, target.node(MINION_3_HOSTNAME));
235 validateEvents(K8S_NODE_UPDATED, K8S_NODE_INCOMPLETE);
236 }
237
238 /**
239 * Checks if updating a null node fails with proper exception.
240 */
241 @Test(expected = NullPointerException.class)
242 public void testUpdateNullNode() {
243 target.updateNode(null);
244 }
245
246 /**
247 * Checks if updating not existing node fails with proper exception.
248 */
Jian Li1cee9882019-02-13 11:25:25 +0900249 @Test(expected = NullPointerException.class)
Jian Li49109b52019-01-22 00:17:28 +0900250 public void testUpdateNotExistingNode() {
251 target.updateNode(MINION_1);
252 }
253
254 /**
255 * Checks if getting all nodes method returns correct set of nodes.
256 */
257 @Test
258 public void testGetAllNodes() {
259 assertEquals(ERR_SIZE, 2, target.nodes().size());
260 assertTrue(ERR_NOT_FOUND, target.nodes().contains(MINION_2));
261 assertTrue(ERR_NOT_FOUND, target.nodes().contains(MINION_3));
262 }
263
264 /**
265 * Checks if getting complete nodes method returns correct set of nodes.
266 */
267 @Test
268 public void testGetCompleteNodes() {
269 assertEquals(ERR_SIZE, 1, target.completeNodes().size());
270 assertTrue(ERR_NOT_FOUND, target.completeNodes().contains(MINION_3));
271 }
272
273 /**
274 * Checks if getting nodes by type method returns correct set of nodes.
275 */
276 @Test
277 public void testGetNodesByType() {
278 assertEquals(ERR_SIZE, 2, target.nodes(MINION).size());
279 assertTrue(ERR_NOT_FOUND, target.nodes(MINION).contains(MINION_2));
280 assertTrue(ERR_NOT_FOUND, target.nodes(MINION).contains(MINION_3));
281 }
282
283 /**
284 * Checks if getting a node by hostname returns correct node.
285 */
286 @Test
287 public void testGetNodeByHostname() {
288 assertEquals(ERR_NOT_FOUND, target.node(MINION_2_HOSTNAME), MINION_2);
289 assertEquals(ERR_NOT_FOUND, target.node(MINION_3_HOSTNAME), MINION_3);
290 }
291
292 private void validateEvents(Enum... types) {
293 int i = 0;
294 assertEquals("Number of events did not match", types.length, testListener.events.size());
295 for (Event event : testListener.events) {
296 assertEquals("Incorrect event received", types[i], event.type());
297 i++;
298 }
299 testListener.events.clear();
300 }
301
302 private static Device createDevice(long devIdNum) {
303 return new DefaultDevice(new ProviderId("of", "foo"),
304 DeviceId.deviceId(String.format("of:%016d", devIdNum)),
305 SWITCH,
306 "manufacturer",
307 "hwVersion",
308 "swVersion",
309 "serialNumber",
310 new ChassisId(1));
311 }
312
313 private static class TestK8sNodeListener implements K8sNodeListener {
314 private List<K8sNodeEvent> events = Lists.newArrayList();
315
316 @Override
317 public void event(K8sNodeEvent event) {
318 events.add(event);
319 }
320 }
321
322 private static class TestCoreService extends CoreServiceAdapter {
323 @Override
324 public ApplicationId registerApplication(String name) {
325 return TEST_APP_ID;
326 }
327 }
328
329 private class TestClusterService extends ClusterServiceAdapter {
330
331 }
332
333 private static class TestLeadershipService extends LeadershipServiceAdapter {
334
335 }
336
337 private static K8sNode createNode(String hostname, K8sNode.Type type,
Jian Libf562c22019-04-15 18:07:14 +0900338 Device intgBridge, Device extBridge,
Jian Li1a2eb5d2019-08-27 02:07:05 +0900339 Device localBridge, IpAddress ipAddr,
340 K8sNodeState state) {
Jian Li49109b52019-01-22 00:17:28 +0900341 return DefaultK8sNode.builder()
342 .hostname(hostname)
343 .type(type)
344 .intgBridge(intgBridge.id())
Jian Libf562c22019-04-15 18:07:14 +0900345 .extBridge(extBridge.id())
Jian Li1a2eb5d2019-08-27 02:07:05 +0900346 .localBridge(localBridge.id())
Jian Li49109b52019-01-22 00:17:28 +0900347 .managementIp(ipAddr)
348 .dataIp(ipAddr)
349 .state(state)
350 .build();
351 }
352}