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