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