blob: 6dd481d26ee1259e85b90981ba6046dd4b2cec57 [file] [log] [blame]
Hyunsun Moon090d77d2017-07-05 17:48:37 +09001/*
2 * Copyright 2017-present Open Networking Laboratory
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.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;
32import org.onosproject.net.config.NetworkConfigRegistryAdapter;
33import org.onosproject.openstacknode.api.NodeState;
34import org.onosproject.openstacknode.api.OpenstackNode;
35import org.onosproject.openstacknode.api.OpenstackNodeEvent;
36import org.onosproject.openstacknode.api.OpenstackNodeListener;
37import org.onosproject.store.service.TestStorageService;
38
39import java.util.List;
40import java.util.Objects;
41
42import static org.junit.Assert.assertEquals;
43import static org.junit.Assert.assertTrue;
44import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.COMPUTE;
45import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.GATEWAY;
46import static org.onosproject.openstacknode.api.OpenstackNodeEvent.Type.*;
47
48/**
49 * Unit tests for OpenStack node manager.
50 */
51public class OpenstackNodeManagerTest extends OpenstackNodeTest {
52
53 private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
54
55 private static final String ERR_SIZE = "Number of nodes did not match";
56 private static final String ERR_NOT_MATCH = "Node did not match";
57 private static final String ERR_NOT_FOUND = "Node did not exist";
58
59 private static final String COMPUTE_1_HOSTNAME = "compute_1";
60 private static final String COMPUTE_2_HOSTNAME = "compute_2";
61 private static final String COMPUTE_3_HOSTNAME = "compute_3";
62 private static final String GATEWAY_1_HOSTNAME = "gateway_1";
63
64 private static final Device COMPUTE_1_INTG_DEVICE = createDevice(1);
65 private static final Device COMPUTE_2_INTG_DEVICE = createDevice(2);
66 private static final Device COMPUTE_3_INTG_DEVICE = createDevice(3);
67 private static final Device GATEWAY_1_INTG_DEVICE = createDevice(4);
68 private static final Device GATEWAY_1_ROUT_DEVICE = createDevice(5);
69
70 private static final OpenstackNode COMPUTE_1 = createNode(
71 COMPUTE_1_HOSTNAME,
72 COMPUTE,
73 COMPUTE_1_INTG_DEVICE,
74 IpAddress.valueOf("10.100.0.1"),
75 NodeState.INIT
76 );
77 private static final OpenstackNode COMPUTE_2 = createNode(
78 COMPUTE_2_HOSTNAME,
79 COMPUTE,
80 COMPUTE_2_INTG_DEVICE,
81 IpAddress.valueOf("10.100.0.2"),
82 NodeState.INIT
83 );
84 private static final OpenstackNode COMPUTE_3 = createNode(
85 COMPUTE_3_HOSTNAME,
86 COMPUTE,
87 COMPUTE_3_INTG_DEVICE,
88 IpAddress.valueOf("10.100.0.3"),
89 NodeState.COMPLETE
90 );
91 private static final OpenstackNode GATEWAY_1 = createNode(
92 GATEWAY_1_HOSTNAME,
93 OpenstackNode.NodeType.GATEWAY,
94 GATEWAY_1_INTG_DEVICE,
95 GATEWAY_1_ROUT_DEVICE,
96 IpAddress.valueOf("10.100.0.4"),
97 NodeState.COMPLETE
98 );
99
100 private final TestOpenstackNodeListener testListener = new TestOpenstackNodeListener();
101
102 private OpenstackNodeManager target;
103 private DistributedOpenstackNodeStore osNodeStore;
104
105 @Before
106 public void setUp() {
107 osNodeStore = new DistributedOpenstackNodeStore();
108 TestUtils.setField(osNodeStore, "coreService", new TestCoreService());
109 TestUtils.setField(osNodeStore, "storageService", new TestStorageService());
110 TestUtils.setField(osNodeStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
111 osNodeStore.activate();
112
113 osNodeStore.createNode(COMPUTE_2);
114 osNodeStore.createNode(COMPUTE_3);
115 osNodeStore.createNode(GATEWAY_1);
116
117 target = new OpenstackNodeManager();
118 target.coreService = new TestCoreService();
119 target.clusterService = new TestClusterService();
120 target.leadershipService = new TestLeadershipService();
121 target.configRegistry = new TestConfigService();
122 target.osNodeStore = osNodeStore;
123 target.addListener(testListener);
124 target.activate();
125 testListener.events.clear();
126 }
127
128 @After
129 public void tearDown() {
130 target.removeListener(testListener);
131 target.deactivate();
132 osNodeStore.deactivate();
133 osNodeStore = null;
134 target = null;
135 }
136
137 /**
138 * Checks if creating and removing a node work well with proper events.
139 */
140 @Test
141 public void testCreateAndRemoveNode() {
142 target.createNode(COMPUTE_1);
143 assertEquals(ERR_SIZE, 4, target.nodes().size());
144 assertTrue(target.node(COMPUTE_1_HOSTNAME) != null);
145
146 target.removeNode(COMPUTE_1_HOSTNAME);
147 assertEquals(ERR_SIZE, 3, target.nodes().size());
148 assertTrue(target.node(COMPUTE_1_HOSTNAME) == null);
149
150 validateEvents(OPENSTACK_NODE_CREATED, OPENSTACK_NODE_REMOVED);
151 }
152
153 /**
154 * Checks if creating null node fails with proper exception.
155 */
156 @Test(expected = NullPointerException.class)
157 public void testCreateNullNode() {
158 target.createNode(null);
159 }
160
161 /**
162 * Checks if creating a duplicated node fails with proper exception.
163 */
164 @Test(expected = IllegalArgumentException.class)
165 public void testCreateDuplicateNode() {
166 target.createNode(COMPUTE_1);
167 target.createNode(COMPUTE_1);
168 }
169
170 /**
171 * Checks if removing null node fails with proper exception.
172 */
173 @Test(expected = IllegalArgumentException.class)
174 public void testRemoveNullNode() {
175 target.removeNode(null);
176 }
177
178 /**
179 * Checks if updating a node works well with proper event.
180 */
181 @Test
182 public void testUpdateNode() {
183 OpenstackNode updated = DefaultOpenstackNode.from(COMPUTE_2)
184 .dataIp(IpAddress.valueOf("10.200.0.100"))
185 .build();
186 target.updateNode(updated);
187 assertEquals(ERR_NOT_MATCH, updated, target.node(COMPUTE_2_INTG_DEVICE.id()));
188 validateEvents(OPENSTACK_NODE_UPDATED);
189 }
190
191 /**
192 * Checks if updating a node state to complete generates proper events.
193 */
194 @Test
195 public void testUpdateNodeStateComplete() {
196 OpenstackNode updated = DefaultOpenstackNode.from(COMPUTE_2)
197 .state(NodeState.COMPLETE)
198 .build();
199 target.updateNode(updated);
200 assertEquals(ERR_NOT_MATCH, updated, target.node(COMPUTE_2_HOSTNAME));
201 validateEvents(OPENSTACK_NODE_UPDATED, OPENSTACK_NODE_COMPLETE);
202 }
203
204 /**
205 * Checks if updating a node state to incomplete generates proper events.
206 */
207 @Test
208 public void testUpdateNodeStateIncomplete() {
209 OpenstackNode updated = DefaultOpenstackNode.from(COMPUTE_3)
210 .state(NodeState.INCOMPLETE)
211 .build();
212 target.updateNode(updated);
213 assertEquals(ERR_NOT_MATCH, updated, target.node(COMPUTE_3_HOSTNAME));
214 validateEvents(OPENSTACK_NODE_UPDATED, OPENSTACK_NODE_INCOMPLETE);
215 }
216
217 /**
218 * Checks if updating a null node fails with proper exception.
219 */
220 @Test(expected = NullPointerException.class)
221 public void testUpdateNullNode() {
222 target.updateNode(null);
223 }
224
225 /**
226 * Checks if updating not existing node fails with proper exception.
227 */
228 @Test(expected = IllegalArgumentException.class)
229 public void testUpdateNotExistingNode() {
230 target.updateNode(COMPUTE_1);
231 }
232
233 /**
234 * Checks if getting all nodes method returns correct set of nodes.
235 */
236 @Test
237 public void testGetAllNodes() {
238 assertEquals(ERR_SIZE, 3, target.nodes().size());
239 assertTrue(ERR_NOT_FOUND, target.nodes().contains(COMPUTE_2));
240 assertTrue(ERR_NOT_FOUND, target.nodes().contains(COMPUTE_3));
241 assertTrue(ERR_NOT_FOUND, target.nodes().contains(GATEWAY_1));
242 }
243
244 /**
245 * Checks if getting complete nodes method returns correct set of nodes.
246 */
247 @Test
248 public void testGetCompleteNodes() {
249 assertEquals(ERR_SIZE, 2, target.completeNodes().size());
250 assertTrue(ERR_NOT_FOUND, target.completeNodes().contains(COMPUTE_3));
251 assertTrue(ERR_NOT_FOUND, target.completeNodes().contains(GATEWAY_1));
252 }
253
254 /**
255 * Checks if getting nodes by type method returns correct set of nodes.
256 */
257 @Test
258 public void testGetNodesByType() {
259 assertEquals(ERR_SIZE, 2, target.nodes(COMPUTE).size());
260 assertTrue(ERR_NOT_FOUND, target.nodes(COMPUTE).contains(COMPUTE_2));
261 assertTrue(ERR_NOT_FOUND, target.nodes(COMPUTE).contains(COMPUTE_3));
262
263 assertEquals(ERR_SIZE, 1, target.nodes(GATEWAY).size());
264 assertTrue(ERR_NOT_FOUND, target.nodes(GATEWAY).contains(GATEWAY_1));
265 }
266
267 /**
268 * Checks if getting a node by hostname returns correct node.
269 */
270 @Test
271 public void testGetNodeByHostname() {
272 assertTrue(ERR_NOT_FOUND, Objects.equals(
273 target.node(COMPUTE_2_HOSTNAME), COMPUTE_2));
274 assertTrue(ERR_NOT_FOUND, Objects.equals(
275 target.node(COMPUTE_3_HOSTNAME), COMPUTE_3));
276 assertTrue(ERR_NOT_FOUND, Objects.equals(
277 target.node(GATEWAY_1_HOSTNAME), GATEWAY_1));
278 }
279
280 /**
281 * Checks if getting a node by device ID returns correct node.
282 */
283 @Test
284 public void testGetNodeByDeviceId() {
285 assertTrue(ERR_NOT_FOUND, Objects.equals(
286 target.node(GATEWAY_1_INTG_DEVICE.id()), GATEWAY_1));
287 assertTrue(ERR_NOT_FOUND, Objects.equals(
288 target.node(GATEWAY_1.ovsdb()), GATEWAY_1));
289 assertTrue(ERR_NOT_FOUND, Objects.equals(
290 target.node(GATEWAY_1.routerBridge()), GATEWAY_1));
291 }
292
293 private void validateEvents(Enum... types) {
294 int i = 0;
295 assertEquals("Number of events did not match", types.length, testListener.events.size());
296 for (Event event : testListener.events) {
297 assertEquals("Incorrect event received", types[i], event.type());
298 i++;
299 }
300 testListener.events.clear();
301 }
302
303 private static class TestOpenstackNodeListener implements OpenstackNodeListener {
304 private List<OpenstackNodeEvent> events = Lists.newArrayList();
305
306 @Override
307 public void event(OpenstackNodeEvent event) {
308 events.add(event);
309 }
310 }
311
312 private static class TestCoreService extends CoreServiceAdapter {
313
314 @Override
315 public ApplicationId registerApplication(String name) {
316 return TEST_APP_ID;
317 }
318 }
319
320 private class TestConfigService extends NetworkConfigRegistryAdapter {
321
322 }
323
324 private class TestClusterService extends ClusterServiceAdapter {
325
326 }
327
328 private static class TestLeadershipService extends LeadershipServiceAdapter {
329
330 }
331}