Jian Li | 7eb2078 | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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 | */ |
| 16 | package org.onosproject.kubevirtnetworking.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.Lists; |
| 21 | import com.google.common.util.concurrent.MoreExecutors; |
| 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.junit.TestUtils; |
| 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreServiceAdapter; |
| 30 | import org.onosproject.core.DefaultApplicationId; |
| 31 | import org.onosproject.event.Event; |
Jian Li | e48a617 | 2021-02-28 03:50:15 +0900 | [diff] [blame^] | 32 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtFloatingIp; |
Jian Li | 7eb2078 | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 33 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtRouter; |
Jian Li | e48a617 | 2021-02-28 03:50:15 +0900 | [diff] [blame^] | 34 | import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp; |
Jian Li | 7eb2078 | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 35 | import org.onosproject.kubevirtnetworking.api.KubevirtPeerRouter; |
| 36 | import org.onosproject.kubevirtnetworking.api.KubevirtRouter; |
| 37 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent; |
| 38 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterListener; |
| 39 | import org.onosproject.store.service.TestStorageService; |
| 40 | |
| 41 | import java.util.List; |
| 42 | |
| 43 | import static org.junit.Assert.assertEquals; |
| 44 | import static org.junit.Assert.assertNotNull; |
| 45 | import static org.junit.Assert.assertNull; |
Jian Li | e48a617 | 2021-02-28 03:50:15 +0900 | [diff] [blame^] | 46 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_ASSOCIATED; |
| 47 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_CREATED; |
| 48 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_DISASSOCIATED; |
| 49 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_REMOVED; |
| 50 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_UPDATED; |
Jian Li | 7eb2078 | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 51 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_CREATED; |
| 52 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_REMOVED; |
| 53 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_UPDATED; |
| 54 | |
| 55 | /** |
| 56 | * Unit tests for kubernetes router manager. |
| 57 | */ |
| 58 | public class KubevirtRouterManagerTest { |
| 59 | |
| 60 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 61 | |
| 62 | private static final String ROUTER_NAME = "router-1"; |
Jian Li | e48a617 | 2021-02-28 03:50:15 +0900 | [diff] [blame^] | 63 | private static final String POD_NAME = "pod-1"; |
Jian Li | 7eb2078 | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 64 | private static final String UPDATED_DESCRIPTION = "router-updated"; |
Jian Li | 7eb2078 | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 65 | private static final MacAddress UPDATED_MAC = MacAddress.valueOf("FF:FF:FF:FF:FF:FF"); |
| 66 | |
Jian Li | e48a617 | 2021-02-28 03:50:15 +0900 | [diff] [blame^] | 67 | private static final String FLOATING_IP_ID = "fip-1"; |
| 68 | private static final String UNKNOWN_ID = "unknown"; |
| 69 | |
Jian Li | 7eb2078 | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 70 | private static final KubevirtRouter ROUTER = DefaultKubevirtRouter.builder() |
| 71 | .name(ROUTER_NAME) |
| 72 | .description(ROUTER_NAME) |
| 73 | .internal(ImmutableSet.of("vxlan-1", "vxlan-2")) |
| 74 | .external(ImmutableMap.of("10.10.10.10", "flat")) |
| 75 | .enableSnat(true) |
| 76 | .peerRouter(new KubevirtPeerRouter(IpAddress.valueOf("20.20.20.20"), |
| 77 | MacAddress.valueOf("11:22:33:44:55:66"))) |
| 78 | .build(); |
| 79 | |
| 80 | private static final KubevirtRouter ROUTER_UPDATED = DefaultKubevirtRouter.builder() |
| 81 | .name(ROUTER_NAME) |
| 82 | .description(UPDATED_DESCRIPTION) |
| 83 | .internal(ImmutableSet.of("vxlan-1", "vxlan-2")) |
| 84 | .external(ImmutableMap.of("10.10.10.10", "flat")) |
| 85 | .enableSnat(true) |
| 86 | .peerRouter(new KubevirtPeerRouter(IpAddress.valueOf("20.20.20.20"), |
| 87 | MacAddress.valueOf("11:22:33:44:55:66"))) |
| 88 | .build(); |
| 89 | |
Jian Li | e48a617 | 2021-02-28 03:50:15 +0900 | [diff] [blame^] | 90 | private static final KubevirtFloatingIp FLOATING_IP_DISASSOCIATED = DefaultKubevirtFloatingIp.builder() |
| 91 | .id(FLOATING_IP_ID) |
| 92 | .routerName(ROUTER_NAME) |
| 93 | .floatingIp(IpAddress.valueOf("10.10.10.10")) |
| 94 | .build(); |
| 95 | |
| 96 | private static final KubevirtFloatingIp FLOATING_IP_ASSOCIATED = DefaultKubevirtFloatingIp.builder() |
| 97 | .id(FLOATING_IP_ID) |
| 98 | .routerName(ROUTER_NAME) |
| 99 | .floatingIp(IpAddress.valueOf("10.10.10.10")) |
| 100 | .fixedIp(IpAddress.valueOf("20.20.20.20")) |
| 101 | .podName(POD_NAME) |
| 102 | .build(); |
| 103 | |
Jian Li | 7eb2078 | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 104 | private final TestKubevirtRouterListener testListener = new TestKubevirtRouterListener(); |
| 105 | |
| 106 | private KubevirtRouterManager target; |
| 107 | private DistributedKubevirtRouterStore kubevirtRouterStore; |
| 108 | |
| 109 | @Before |
| 110 | public void setUp() throws Exception { |
| 111 | kubevirtRouterStore = new DistributedKubevirtRouterStore(); |
| 112 | TestUtils.setField(kubevirtRouterStore, "coreService", new TestCoreService()); |
| 113 | TestUtils.setField(kubevirtRouterStore, "storageService", new TestStorageService()); |
| 114 | TestUtils.setField(kubevirtRouterStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
| 115 | kubevirtRouterStore.activate(); |
| 116 | |
| 117 | target = new KubevirtRouterManager(); |
| 118 | TestUtils.setField(target, "coreService", new TestCoreService()); |
| 119 | target.kubevirtRouterStore = kubevirtRouterStore; |
| 120 | target.addListener(testListener); |
| 121 | target.activate(); |
| 122 | } |
| 123 | |
| 124 | @After |
| 125 | public void tearDown() { |
| 126 | target.removeListener(testListener); |
| 127 | kubevirtRouterStore.deactivate(); |
| 128 | target.deactivate(); |
| 129 | kubevirtRouterStore = null; |
| 130 | target = null; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Tests if getting all routers returns correct set of values. |
| 135 | */ |
| 136 | @Test |
| 137 | public void testGetRouters() { |
| 138 | createBasicRouters(); |
| 139 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Tests if getting a router with name returns correct value. |
| 144 | */ |
| 145 | @Test |
| 146 | public void testGetRouterByName() { |
| 147 | createBasicRouters(); |
| 148 | assertNotNull("Router did not match", target.router(ROUTER_NAME)); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Tests creating and removing a router, and checks if proper event is triggered. |
| 153 | */ |
| 154 | @Test |
| 155 | public void testCreateAndRemoveRouter() { |
| 156 | target.createRouter(ROUTER); |
| 157 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 158 | assertNotNull("Router was not created", target.router(ROUTER_NAME)); |
| 159 | |
| 160 | target.removeRouter(ROUTER_NAME); |
| 161 | assertEquals("Number of router did not match", 0, target.routers().size()); |
| 162 | assertNull("Router was not created", target.router(ROUTER_NAME)); |
| 163 | |
| 164 | validateEvents(KUBEVIRT_ROUTER_CREATED, KUBEVIRT_ROUTER_REMOVED); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Tests creating and updating a port, and checks if proper event is triggered. |
| 169 | */ |
| 170 | @Test |
| 171 | public void testCreateAndUpdateRouter() { |
| 172 | target.createRouter(ROUTER); |
| 173 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 174 | |
| 175 | target.updateRouter(ROUTER_UPDATED); |
| 176 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 177 | assertEquals("Router did not match", UPDATED_DESCRIPTION, target.router(ROUTER_NAME).description()); |
| 178 | |
| 179 | validateEvents(KUBEVIRT_ROUTER_CREATED, KUBEVIRT_ROUTER_UPDATED); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Tests updating peer router MAC address. |
| 184 | */ |
| 185 | @Test |
| 186 | public void testPeerRouterMacUpdate() { |
| 187 | target.createRouter(ROUTER); |
| 188 | |
| 189 | target.updatePeerRouterMac(ROUTER_NAME, UPDATED_MAC); |
| 190 | assertEquals("MAC address was not updated", UPDATED_MAC, |
| 191 | target.router(ROUTER_NAME).peerRouter().macAddress()); |
| 192 | |
| 193 | validateEvents(KUBEVIRT_ROUTER_CREATED, KUBEVIRT_ROUTER_UPDATED); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Tests if creating a null router fails with an exception. |
| 198 | */ |
| 199 | @Test(expected = NullPointerException.class) |
| 200 | public void testCreateNullRouter() { |
| 201 | target.createRouter(null); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Tests if creating a duplicate router fails with an exception. |
| 206 | */ |
| 207 | @Test(expected = IllegalArgumentException.class) |
| 208 | public void createDuplicateRouter() { |
| 209 | target.createRouter(ROUTER); |
| 210 | target.createRouter(ROUTER); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Tests if updating an unregistered router fails with an exception. |
| 215 | */ |
| 216 | @Test(expected = IllegalArgumentException.class) |
| 217 | public void testUpdateUnregisteredRouter() { |
| 218 | target.updateRouter(ROUTER); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Tests if updating a null router fails with an exception. |
| 223 | */ |
| 224 | @Test(expected = NullPointerException.class) |
| 225 | public void testUpdateNullRouter() { |
| 226 | target.updateRouter(null); |
| 227 | } |
| 228 | |
Jian Li | e48a617 | 2021-02-28 03:50:15 +0900 | [diff] [blame^] | 229 | /** |
| 230 | * Tests if getting all floating IPs returns the correct set of floating IPs. |
| 231 | */ |
| 232 | @Test |
| 233 | public void testGetFloatingIps() { |
| 234 | createBasicFloatingIpDisassociated(); |
| 235 | assertEquals("Number of floating IPs did not match", 1, target.floatingIps().size()); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Tests if getting a floating IP with ID returns the correct floating IP. |
| 240 | */ |
| 241 | @Test |
| 242 | public void testGetFloatingIpById() { |
| 243 | createBasicFloatingIpDisassociated(); |
| 244 | assertNotNull("Floating IP did not match", target.floatingIp(FLOATING_IP_ID)); |
| 245 | assertNull("Floating IP did not match", target.floatingIp(UNKNOWN_ID)); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Tests if getting a floating IP with POD name returns the correct floating IP. |
| 250 | */ |
| 251 | @Test |
| 252 | public void testGetFloatingIpByPodName() { |
| 253 | createBasicFloatingIpAssociated(); |
| 254 | assertNotNull("Floating IP did not match", target.floatingIpByPodName(POD_NAME)); |
| 255 | assertNull("Floating IP did not match", target.floatingIpByPodName(UNKNOWN_ID)); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Tests if getting floating IPs with router name returns the correct floating IPs. |
| 260 | */ |
| 261 | @Test |
| 262 | public void testGetFloatingIpsByRouterName() { |
| 263 | createBasicFloatingIpDisassociated(); |
| 264 | assertEquals("Number of floating IPs did not match", 1, |
| 265 | target.floatingIpsByRouter(ROUTER_NAME).size()); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Tests creating and removing a floating IP, and checks if it triggers proper events. |
| 270 | */ |
| 271 | @Test |
| 272 | public void testCreateAndRemoveFloatingIp() { |
| 273 | target.createFloatingIp(FLOATING_IP_DISASSOCIATED); |
| 274 | assertEquals("Number of floating IP did not match", 1, target.floatingIps().size()); |
| 275 | assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID)); |
| 276 | |
| 277 | target.removeFloatingIp(FLOATING_IP_ID); |
| 278 | assertEquals("Number of floating IP did not match", 0, target.floatingIps().size()); |
| 279 | assertNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID)); |
| 280 | |
| 281 | validateEvents(KUBEVIRT_FLOATING_IP_CREATED, KUBEVIRT_FLOATING_IP_REMOVED); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Tests associating a floating IP, and checks if it triggers proper events. |
| 286 | */ |
| 287 | @Test |
| 288 | public void testAssociateFloatingIp() { |
| 289 | target.createFloatingIp(FLOATING_IP_DISASSOCIATED); |
| 290 | assertEquals("Number of floating IP did not match", 1, target.floatingIps().size()); |
| 291 | assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID)); |
| 292 | |
| 293 | target.updateFloatingIp(FLOATING_IP_ASSOCIATED); |
| 294 | assertEquals("Number of floating IP did not match", 1, target.floatingIps().size()); |
| 295 | assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID)); |
| 296 | |
| 297 | validateEvents(KUBEVIRT_FLOATING_IP_CREATED, KUBEVIRT_FLOATING_IP_UPDATED, |
| 298 | KUBEVIRT_FLOATING_IP_ASSOCIATED); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Tests disassociating a floating IP, and checks if it triggers proper events. |
| 303 | */ |
| 304 | @Test |
| 305 | public void testDisassociateFloatingIp() { |
| 306 | target.createFloatingIp(FLOATING_IP_ASSOCIATED); |
| 307 | assertEquals("Number of floating IP did not match", 1, target.floatingIps().size()); |
| 308 | assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID)); |
| 309 | |
| 310 | target.updateFloatingIp(FLOATING_IP_DISASSOCIATED); |
| 311 | assertEquals("Number of floating IP did not match", 1, target.floatingIps().size()); |
| 312 | assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID)); |
| 313 | |
| 314 | validateEvents(KUBEVIRT_FLOATING_IP_CREATED, KUBEVIRT_FLOATING_IP_UPDATED, |
| 315 | KUBEVIRT_FLOATING_IP_DISASSOCIATED); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Tests if creating a null floating IP fails with an exception. |
| 320 | */ |
| 321 | @Test(expected = NullPointerException.class) |
| 322 | public void testCreateNullFloatingIp() { |
| 323 | target.createFloatingIp(null); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Tests if creating a duplicate floating IP fails with an exception. |
| 328 | */ |
| 329 | @Test(expected = IllegalArgumentException.class) |
| 330 | public void testCreateDuplicateFloatingIp() { |
| 331 | target.createFloatingIp(FLOATING_IP_ASSOCIATED); |
| 332 | target.createFloatingIp(FLOATING_IP_DISASSOCIATED); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Tests if removing floating IP with null ID fails with an exception. |
| 337 | */ |
| 338 | @Test(expected = IllegalArgumentException.class) |
| 339 | public void testRemoveFloatingIpWithNull() { |
| 340 | target.removeFloatingIp(null); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Tests if updating an unregistered floating IP fails with an exception. |
| 345 | */ |
| 346 | @Test(expected = IllegalArgumentException.class) |
| 347 | public void testUpdateUnregisteredFloatingIp() { |
| 348 | target.updateFloatingIp(FLOATING_IP_ASSOCIATED); |
| 349 | } |
| 350 | |
Jian Li | 7eb2078 | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 351 | private void createBasicRouters() { |
| 352 | target.createRouter(ROUTER); |
| 353 | } |
| 354 | |
Jian Li | e48a617 | 2021-02-28 03:50:15 +0900 | [diff] [blame^] | 355 | private void createBasicFloatingIpDisassociated() { |
| 356 | target.createFloatingIp(FLOATING_IP_DISASSOCIATED); |
| 357 | } |
| 358 | |
| 359 | private void createBasicFloatingIpAssociated() { |
| 360 | target.createFloatingIp(FLOATING_IP_ASSOCIATED); |
| 361 | } |
| 362 | |
Jian Li | 7eb2078 | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 363 | private static class TestCoreService extends CoreServiceAdapter { |
| 364 | |
| 365 | @Override |
| 366 | public ApplicationId registerApplication(String name) { |
| 367 | return TEST_APP_ID; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | private static class TestKubevirtRouterListener implements KubevirtRouterListener { |
| 372 | |
| 373 | private List<KubevirtRouterEvent> events = Lists.newArrayList(); |
| 374 | |
| 375 | @Override |
| 376 | public void event(KubevirtRouterEvent event) { |
| 377 | events.add(event); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | private void validateEvents(Enum... types) { |
| 382 | int i = 0; |
| 383 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 384 | for (Event event : testListener.events) { |
| 385 | assertEquals("Incorrect event received", types[i], event.type()); |
| 386 | i++; |
| 387 | } |
| 388 | testListener.events.clear(); |
| 389 | } |
| 390 | } |