blob: 68f0aed38dca62beb27ff7f82d93557a08bca6d8 [file] [log] [blame]
Hyunsun Moon090d77d2017-07-05 17:48:37 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Hyunsun Moon090d77d2017-07-05 17:48:37 +09003 *
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.openstacknode.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.IpAddress;
25import org.onosproject.cluster.ClusterServiceAdapter;
26import org.onosproject.cluster.LeadershipServiceAdapter;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreServiceAdapter;
29import org.onosproject.core.DefaultApplicationId;
30import org.onosproject.event.Event;
31import org.onosproject.net.Device;
Jian Li5a38ab62018-07-02 22:34:11 +090032import org.onosproject.openstacknode.api.DefaultOpenstackNode;
Hyunsun Moon090d77d2017-07-05 17:48:37 +090033import org.onosproject.openstacknode.api.NodeState;
34import org.onosproject.openstacknode.api.OpenstackNode;
35import org.onosproject.openstacknode.api.OpenstackNodeEvent;
36import org.onosproject.openstacknode.api.OpenstackNodeListener;
Jian Li5a38ab62018-07-02 22:34:11 +090037import org.onosproject.openstacknode.api.OpenstackNodeTest;
Hyunsun Moon090d77d2017-07-05 17:48:37 +090038import org.onosproject.store.service.TestStorageService;
39
40import java.util.List;
41import java.util.Objects;
42
43import static org.junit.Assert.assertEquals;
44import static org.junit.Assert.assertTrue;
45import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE;
46import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY;
sanghof8164112017-07-14 14:33:16 +090047import static org.onosproject.openstacknode.api.OpenstackNodeEvent.Type.OPENSTACK_NODE_COMPLETE;
48import static org.onosproject.openstacknode.api.OpenstackNodeEvent.Type.OPENSTACK_NODE_CREATED;
49import static org.onosproject.openstacknode.api.OpenstackNodeEvent.Type.OPENSTACK_NODE_INCOMPLETE;
50import static org.onosproject.openstacknode.api.OpenstackNodeEvent.Type.OPENSTACK_NODE_REMOVED;
51import static org.onosproject.openstacknode.api.OpenstackNodeEvent.Type.OPENSTACK_NODE_UPDATED;
Hyunsun Moon090d77d2017-07-05 17:48:37 +090052
53/**
54 * Unit tests for OpenStack node manager.
55 */
56public class OpenstackNodeManagerTest extends OpenstackNodeTest {
57
58 private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
59
60 private static final String ERR_SIZE = "Number of nodes did not match";
61 private static final String ERR_NOT_MATCH = "Node did not match";
62 private static final String ERR_NOT_FOUND = "Node did not exist";
63
64 private static final String COMPUTE_1_HOSTNAME = "compute_1";
65 private static final String COMPUTE_2_HOSTNAME = "compute_2";
66 private static final String COMPUTE_3_HOSTNAME = "compute_3";
67 private static final String GATEWAY_1_HOSTNAME = "gateway_1";
Jian Lie3141542018-08-13 18:05:43 +090068 private static final String COMPUTE_1_DUP_INT_HOSTNAME = "compute_1_dup_int";
Hyunsun Moon090d77d2017-07-05 17:48:37 +090069
daniel parkb18424c2018-02-05 15:43:43 +090070 private static final String GATEWAY_1_UPLINKPORT = "eth0";
71
Hyunsun Moon090d77d2017-07-05 17:48:37 +090072 private static final Device COMPUTE_1_INTG_DEVICE = createDevice(1);
73 private static final Device COMPUTE_2_INTG_DEVICE = createDevice(2);
74 private static final Device COMPUTE_3_INTG_DEVICE = createDevice(3);
75 private static final Device GATEWAY_1_INTG_DEVICE = createDevice(4);
Hyunsun Moon090d77d2017-07-05 17:48:37 +090076
77 private static final OpenstackNode COMPUTE_1 = createNode(
78 COMPUTE_1_HOSTNAME,
79 COMPUTE,
80 COMPUTE_1_INTG_DEVICE,
81 IpAddress.valueOf("10.100.0.1"),
82 NodeState.INIT
83 );
84 private static final OpenstackNode COMPUTE_2 = createNode(
85 COMPUTE_2_HOSTNAME,
86 COMPUTE,
87 COMPUTE_2_INTG_DEVICE,
88 IpAddress.valueOf("10.100.0.2"),
89 NodeState.INIT
90 );
91 private static final OpenstackNode COMPUTE_3 = createNode(
92 COMPUTE_3_HOSTNAME,
93 COMPUTE,
94 COMPUTE_3_INTG_DEVICE,
95 IpAddress.valueOf("10.100.0.3"),
96 NodeState.COMPLETE
97 );
98 private static final OpenstackNode GATEWAY_1 = createNode(
99 GATEWAY_1_HOSTNAME,
100 OpenstackNode.NodeType.GATEWAY,
101 GATEWAY_1_INTG_DEVICE,
Hyunsun Moon090d77d2017-07-05 17:48:37 +0900102 IpAddress.valueOf("10.100.0.4"),
daniel parkb18424c2018-02-05 15:43:43 +0900103 GATEWAY_1_UPLINKPORT,
Hyunsun Moon090d77d2017-07-05 17:48:37 +0900104 NodeState.COMPLETE
105 );
Jian Lie3141542018-08-13 18:05:43 +0900106 private static final OpenstackNode COMPUTE_1_DUP_INT = createNode(
107 COMPUTE_1_DUP_INT_HOSTNAME,
108 COMPUTE,
109 COMPUTE_1_INTG_DEVICE,
110 IpAddress.valueOf("10.100.0.1"),
111 NodeState.INIT
112 );
113 private static final OpenstackNode COMPUTE_2_DUP_INT = createNode(
114 COMPUTE_2_HOSTNAME,
115 COMPUTE,
116 COMPUTE_3_INTG_DEVICE,
117 IpAddress.valueOf("10.100.0.2"),
118 NodeState.INIT
119 );
Hyunsun Moon090d77d2017-07-05 17:48:37 +0900120
121 private final TestOpenstackNodeListener testListener = new TestOpenstackNodeListener();
122
123 private OpenstackNodeManager target;
124 private DistributedOpenstackNodeStore osNodeStore;
125
126 @Before
127 public void setUp() {
128 osNodeStore = new DistributedOpenstackNodeStore();
129 TestUtils.setField(osNodeStore, "coreService", new TestCoreService());
130 TestUtils.setField(osNodeStore, "storageService", new TestStorageService());
131 TestUtils.setField(osNodeStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
132 osNodeStore.activate();
133
134 osNodeStore.createNode(COMPUTE_2);
135 osNodeStore.createNode(COMPUTE_3);
136 osNodeStore.createNode(GATEWAY_1);
137
sanghof8164112017-07-14 14:33:16 +0900138 target = new org.onosproject.openstacknode.impl.OpenstackNodeManager();
Daniel Parkc4d06402018-05-28 15:57:37 +0900139 target.storageService = new TestStorageService();
Hyunsun Moon090d77d2017-07-05 17:48:37 +0900140 target.coreService = new TestCoreService();
141 target.clusterService = new TestClusterService();
142 target.leadershipService = new TestLeadershipService();
Hyunsun Moon090d77d2017-07-05 17:48:37 +0900143 target.osNodeStore = osNodeStore;
144 target.addListener(testListener);
145 target.activate();
146 testListener.events.clear();
147 }
148
149 @After
150 public void tearDown() {
151 target.removeListener(testListener);
152 target.deactivate();
153 osNodeStore.deactivate();
154 osNodeStore = 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(COMPUTE_1);
164 assertEquals(ERR_SIZE, 4, target.nodes().size());
165 assertTrue(target.node(COMPUTE_1_HOSTNAME) != null);
166
167 target.removeNode(COMPUTE_1_HOSTNAME);
168 assertEquals(ERR_SIZE, 3, target.nodes().size());
169 assertTrue(target.node(COMPUTE_1_HOSTNAME) == null);
170
171 validateEvents(OPENSTACK_NODE_CREATED, OPENSTACK_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(COMPUTE_1);
188 target.createNode(COMPUTE_1);
189 }
190
191 /**
Jian Lie3141542018-08-13 18:05:43 +0900192 * Checks if creating a node with duplicated integration bridge.
193 */
194 @Test(expected = IllegalArgumentException.class)
195 public void testCreateNodeWithDuplicateIntgBridge() {
196 target.createNode(COMPUTE_1);
197 target.createNode(COMPUTE_1_DUP_INT);
198 }
199
200 /**
Hyunsun Moon090d77d2017-07-05 17:48:37 +0900201 * Checks if removing null node fails with proper exception.
202 */
203 @Test(expected = IllegalArgumentException.class)
204 public void testRemoveNullNode() {
205 target.removeNode(null);
206 }
207
208 /**
209 * Checks if updating a node works well with proper event.
210 */
211 @Test
212 public void testUpdateNode() {
213 OpenstackNode updated = DefaultOpenstackNode.from(COMPUTE_2)
214 .dataIp(IpAddress.valueOf("10.200.0.100"))
215 .build();
216 target.updateNode(updated);
217 assertEquals(ERR_NOT_MATCH, updated, target.node(COMPUTE_2_INTG_DEVICE.id()));
218 validateEvents(OPENSTACK_NODE_UPDATED);
219 }
220
221 /**
Jian Lie3141542018-08-13 18:05:43 +0900222 * Checks if updating a node with duplicated integration bridge.
223 */
224 @Test(expected = IllegalArgumentException.class)
225 public void testUpdateNodeWithDuplicateIntgBridge() {
226 target.updateNode(COMPUTE_2_DUP_INT);
227 }
228
229 /**
Hyunsun Moon090d77d2017-07-05 17:48:37 +0900230 * Checks if updating a node state to complete generates proper events.
231 */
232 @Test
233 public void testUpdateNodeStateComplete() {
234 OpenstackNode updated = DefaultOpenstackNode.from(COMPUTE_2)
235 .state(NodeState.COMPLETE)
236 .build();
237 target.updateNode(updated);
238 assertEquals(ERR_NOT_MATCH, updated, target.node(COMPUTE_2_HOSTNAME));
239 validateEvents(OPENSTACK_NODE_UPDATED, OPENSTACK_NODE_COMPLETE);
240 }
241
242 /**
243 * Checks if updating a node state to incomplete generates proper events.
244 */
245 @Test
246 public void testUpdateNodeStateIncomplete() {
247 OpenstackNode updated = DefaultOpenstackNode.from(COMPUTE_3)
248 .state(NodeState.INCOMPLETE)
249 .build();
250 target.updateNode(updated);
251 assertEquals(ERR_NOT_MATCH, updated, target.node(COMPUTE_3_HOSTNAME));
252 validateEvents(OPENSTACK_NODE_UPDATED, OPENSTACK_NODE_INCOMPLETE);
253 }
254
255 /**
256 * Checks if updating a null node fails with proper exception.
257 */
258 @Test(expected = NullPointerException.class)
259 public void testUpdateNullNode() {
260 target.updateNode(null);
261 }
262
263 /**
264 * Checks if updating not existing node fails with proper exception.
265 */
Jian Lie3141542018-08-13 18:05:43 +0900266 @Test(expected = NullPointerException.class)
Hyunsun Moon090d77d2017-07-05 17:48:37 +0900267 public void testUpdateNotExistingNode() {
268 target.updateNode(COMPUTE_1);
269 }
270
271 /**
272 * Checks if getting all nodes method returns correct set of nodes.
273 */
274 @Test
275 public void testGetAllNodes() {
276 assertEquals(ERR_SIZE, 3, target.nodes().size());
277 assertTrue(ERR_NOT_FOUND, target.nodes().contains(COMPUTE_2));
278 assertTrue(ERR_NOT_FOUND, target.nodes().contains(COMPUTE_3));
279 assertTrue(ERR_NOT_FOUND, target.nodes().contains(GATEWAY_1));
280 }
281
282 /**
283 * Checks if getting complete nodes method returns correct set of nodes.
284 */
285 @Test
286 public void testGetCompleteNodes() {
287 assertEquals(ERR_SIZE, 2, target.completeNodes().size());
288 assertTrue(ERR_NOT_FOUND, target.completeNodes().contains(COMPUTE_3));
289 assertTrue(ERR_NOT_FOUND, target.completeNodes().contains(GATEWAY_1));
290 }
291
292 /**
293 * Checks if getting nodes by type method returns correct set of nodes.
294 */
295 @Test
296 public void testGetNodesByType() {
297 assertEquals(ERR_SIZE, 2, target.nodes(COMPUTE).size());
298 assertTrue(ERR_NOT_FOUND, target.nodes(COMPUTE).contains(COMPUTE_2));
299 assertTrue(ERR_NOT_FOUND, target.nodes(COMPUTE).contains(COMPUTE_3));
300
301 assertEquals(ERR_SIZE, 1, target.nodes(GATEWAY).size());
302 assertTrue(ERR_NOT_FOUND, target.nodes(GATEWAY).contains(GATEWAY_1));
303 }
304
305 /**
306 * Checks if getting a node by hostname returns correct node.
307 */
308 @Test
309 public void testGetNodeByHostname() {
310 assertTrue(ERR_NOT_FOUND, Objects.equals(
311 target.node(COMPUTE_2_HOSTNAME), COMPUTE_2));
312 assertTrue(ERR_NOT_FOUND, Objects.equals(
313 target.node(COMPUTE_3_HOSTNAME), COMPUTE_3));
314 assertTrue(ERR_NOT_FOUND, Objects.equals(
315 target.node(GATEWAY_1_HOSTNAME), GATEWAY_1));
316 }
317
318 /**
319 * Checks if getting a node by device ID returns correct node.
320 */
321 @Test
322 public void testGetNodeByDeviceId() {
323 assertTrue(ERR_NOT_FOUND, Objects.equals(
324 target.node(GATEWAY_1_INTG_DEVICE.id()), GATEWAY_1));
325 assertTrue(ERR_NOT_FOUND, Objects.equals(
326 target.node(GATEWAY_1.ovsdb()), GATEWAY_1));
Hyunsun Moon090d77d2017-07-05 17:48:37 +0900327 }
328
329 private void validateEvents(Enum... types) {
330 int i = 0;
331 assertEquals("Number of events did not match", types.length, testListener.events.size());
332 for (Event event : testListener.events) {
333 assertEquals("Incorrect event received", types[i], event.type());
334 i++;
335 }
336 testListener.events.clear();
337 }
338
339 private static class TestOpenstackNodeListener implements OpenstackNodeListener {
340 private List<OpenstackNodeEvent> events = Lists.newArrayList();
341
342 @Override
343 public void event(OpenstackNodeEvent event) {
344 events.add(event);
345 }
346 }
347
348 private static class TestCoreService extends CoreServiceAdapter {
349
350 @Override
351 public ApplicationId registerApplication(String name) {
352 return TEST_APP_ID;
353 }
354 }
355
Hyunsun Moon090d77d2017-07-05 17:48:37 +0900356 private class TestClusterService extends ClusterServiceAdapter {
357
358 }
359
360 private static class TestLeadershipService extends LeadershipServiceAdapter {
361
362 }
363}