blob: fbccdd0fa091ee24d4e4f0ec7c7a7d286cc3e116 [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 Li49109b52019-01-22 00:17:28 +090083 private static final K8sNode MINION_1 = createNode(
84 MINION_1_HOSTNAME,
85 MINION,
86 MINION_1_INTG_DEVICE,
Jian Libf562c22019-04-15 18:07:14 +090087 MINION_1_EXT_DEVICE,
Jian Li49109b52019-01-22 00:17:28 +090088 IpAddress.valueOf("10.100.0.1"),
89 INIT
90 );
91 private static final K8sNode MINION_2 = createNode(
92 MINION_2_HOSTNAME,
93 MINION,
94 MINION_2_INTG_DEVICE,
Jian Libf562c22019-04-15 18:07:14 +090095 MINION_2_EXT_DEVICE,
Jian Li49109b52019-01-22 00:17:28 +090096 IpAddress.valueOf("10.100.0.2"),
97 INIT
98 );
99 private static final K8sNode MINION_3 = createNode(
100 MINION_3_HOSTNAME,
101 MINION,
102 MINION_3_INTG_DEVICE,
Jian Libf562c22019-04-15 18:07:14 +0900103 MINION_3_EXT_DEVICE,
Jian Li49109b52019-01-22 00:17:28 +0900104 IpAddress.valueOf("10.100.0.3"),
105 COMPLETE
106 );
107
108 private final TestK8sNodeListener testListener = new TestK8sNodeListener();
109
110 private K8sNodeManager target;
111 private DistributedK8sNodeStore nodeStore;
112
113 /**
114 * Initial setup for this unit test.
115 */
116 @Before
117 public void setUp() {
118 nodeStore = new DistributedK8sNodeStore();
119 TestUtils.setField(nodeStore, "coreService", new TestCoreService());
120 TestUtils.setField(nodeStore, "storageService", new TestStorageService());
121 TestUtils.setField(nodeStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
122 nodeStore.activate();
123
124 nodeStore.createNode(MINION_2);
125 nodeStore.createNode(MINION_3);
126
127 target = new K8sNodeManager();
128 target.storageService = new TestStorageService();
129 target.coreService = new TestCoreService();
130 target.clusterService = new TestClusterService();
131 target.leadershipService = new TestLeadershipService();
132 target.nodeStore = nodeStore;
133 target.addListener(testListener);
134 target.activate();
135 testListener.events.clear();
136 }
137
138 /**
139 * Clean up unit test.
140 */
141 @After
142 public void tearDown() {
143 target.removeListener(testListener);
144 target.deactivate();
145 nodeStore.deactivate();
146 nodeStore = null;
147 target = null;
148 }
149
150 /**
151 * Checks if creating and removing a node work well with proper events.
152 */
153 @Test
154 public void testCreateAndRemoveNode() {
155 target.createNode(MINION_1);
156 assertEquals(ERR_SIZE, 3, target.nodes().size());
157 assertNotNull(target.node(MINION_1_HOSTNAME));
158
159 target.removeNode(MINION_1_HOSTNAME);
160 assertEquals(ERR_SIZE, 2, target.nodes().size());
161 assertNull(target.node(MINION_1_HOSTNAME));
162
163 validateEvents(K8S_NODE_CREATED, K8S_NODE_REMOVED);
164 }
165
166 /**
167 * Checks if creating null node fails with proper exception.
168 */
169 @Test(expected = NullPointerException.class)
170 public void testCreateNullNode() {
171 target.createNode(null);
172 }
173
174 /**
175 * Checks if creating a duplicated node fails with proper exception.
176 */
177 @Test(expected = IllegalArgumentException.class)
178 public void testCreateDuplicateNode() {
179 target.createNode(MINION_1);
180 target.createNode(MINION_1);
181 }
182
183 /**
184 * Checks if removing null node fails with proper exception.
185 */
186 @Test(expected = IllegalArgumentException.class)
187 public void testRemoveNullNode() {
188 target.removeNode(null);
189 }
190
191 /**
192 * Checks if updating a node works well with proper event.
193 */
194 @Test
195 public void testUpdateNode() {
196 K8sNode updated = DefaultK8sNode.from(MINION_2)
197 .dataIp(IpAddress.valueOf("10.200.0.100"))
198 .build();
199 target.updateNode(updated);
200 assertEquals(ERR_NOT_MATCH, updated, target.node(MINION_2_INTG_DEVICE.id()));
201 validateEvents(K8S_NODE_UPDATED);
202 }
203
204 /**
205 * Checks if updating a node state to complete generates proper events.
206 */
207 @Test
208 public void testUpdateNodeStateComplete() {
209 K8sNode updated = DefaultK8sNode.from(MINION_2)
210 .state(COMPLETE)
211 .build();
212 target.updateNode(updated);
213 assertEquals(ERR_NOT_MATCH, updated, target.node(MINION_2_HOSTNAME));
214 validateEvents(K8S_NODE_UPDATED, K8S_NODE_COMPLETE);
215 }
216
217 /**
218 * Checks if updating a node state to incomplete generates proper events.
219 */
220 @Test
221 public void testUpdateNodeStateIncomplete() {
222 K8sNode updated = DefaultK8sNode.from(MINION_3)
223 .state(INCOMPLETE)
224 .build();
225 target.updateNode(updated);
226 assertEquals(ERR_NOT_MATCH, updated, target.node(MINION_3_HOSTNAME));
227 validateEvents(K8S_NODE_UPDATED, K8S_NODE_INCOMPLETE);
228 }
229
230 /**
231 * Checks if updating a null node fails with proper exception.
232 */
233 @Test(expected = NullPointerException.class)
234 public void testUpdateNullNode() {
235 target.updateNode(null);
236 }
237
238 /**
239 * Checks if updating not existing node fails with proper exception.
240 */
Jian Li1cee9882019-02-13 11:25:25 +0900241 @Test(expected = NullPointerException.class)
Jian Li49109b52019-01-22 00:17:28 +0900242 public void testUpdateNotExistingNode() {
243 target.updateNode(MINION_1);
244 }
245
246 /**
247 * Checks if getting all nodes method returns correct set of nodes.
248 */
249 @Test
250 public void testGetAllNodes() {
251 assertEquals(ERR_SIZE, 2, target.nodes().size());
252 assertTrue(ERR_NOT_FOUND, target.nodes().contains(MINION_2));
253 assertTrue(ERR_NOT_FOUND, target.nodes().contains(MINION_3));
254 }
255
256 /**
257 * Checks if getting complete nodes method returns correct set of nodes.
258 */
259 @Test
260 public void testGetCompleteNodes() {
261 assertEquals(ERR_SIZE, 1, target.completeNodes().size());
262 assertTrue(ERR_NOT_FOUND, target.completeNodes().contains(MINION_3));
263 }
264
265 /**
266 * Checks if getting nodes by type method returns correct set of nodes.
267 */
268 @Test
269 public void testGetNodesByType() {
270 assertEquals(ERR_SIZE, 2, target.nodes(MINION).size());
271 assertTrue(ERR_NOT_FOUND, target.nodes(MINION).contains(MINION_2));
272 assertTrue(ERR_NOT_FOUND, target.nodes(MINION).contains(MINION_3));
273 }
274
275 /**
276 * Checks if getting a node by hostname returns correct node.
277 */
278 @Test
279 public void testGetNodeByHostname() {
280 assertEquals(ERR_NOT_FOUND, target.node(MINION_2_HOSTNAME), MINION_2);
281 assertEquals(ERR_NOT_FOUND, target.node(MINION_3_HOSTNAME), MINION_3);
282 }
283
284 private void validateEvents(Enum... types) {
285 int i = 0;
286 assertEquals("Number of events did not match", types.length, testListener.events.size());
287 for (Event event : testListener.events) {
288 assertEquals("Incorrect event received", types[i], event.type());
289 i++;
290 }
291 testListener.events.clear();
292 }
293
294 private static Device createDevice(long devIdNum) {
295 return new DefaultDevice(new ProviderId("of", "foo"),
296 DeviceId.deviceId(String.format("of:%016d", devIdNum)),
297 SWITCH,
298 "manufacturer",
299 "hwVersion",
300 "swVersion",
301 "serialNumber",
302 new ChassisId(1));
303 }
304
305 private static class TestK8sNodeListener implements K8sNodeListener {
306 private List<K8sNodeEvent> events = Lists.newArrayList();
307
308 @Override
309 public void event(K8sNodeEvent event) {
310 events.add(event);
311 }
312 }
313
314 private static class TestCoreService extends CoreServiceAdapter {
315 @Override
316 public ApplicationId registerApplication(String name) {
317 return TEST_APP_ID;
318 }
319 }
320
321 private class TestClusterService extends ClusterServiceAdapter {
322
323 }
324
325 private static class TestLeadershipService extends LeadershipServiceAdapter {
326
327 }
328
329 private static K8sNode createNode(String hostname, K8sNode.Type type,
Jian Libf562c22019-04-15 18:07:14 +0900330 Device intgBridge, Device extBridge,
331 IpAddress ipAddr, K8sNodeState state) {
Jian Li49109b52019-01-22 00:17:28 +0900332 return DefaultK8sNode.builder()
333 .hostname(hostname)
334 .type(type)
335 .intgBridge(intgBridge.id())
Jian Libf562c22019-04-15 18:07:14 +0900336 .extBridge(extBridge.id())
Jian Li49109b52019-01-22 00:17:28 +0900337 .managementIp(ipAddr)
338 .dataIp(ipAddr)
339 .state(state)
340 .build();
341 }
342}