Jian Li | 810f58c | 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; |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame^] | 21 | import com.google.common.collect.Sets; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 22 | import com.google.common.util.concurrent.MoreExecutors; |
| 23 | import org.junit.After; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.junit.TestUtils; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.MacAddress; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreServiceAdapter; |
| 31 | import org.onosproject.core.DefaultApplicationId; |
| 32 | import org.onosproject.event.Event; |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 33 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtFloatingIp; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 34 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtRouter; |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 35 | import org.onosproject.kubevirtnetworking.api.KubevirtFloatingIp; |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame^] | 36 | import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancer; |
| 37 | import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerListener; |
| 38 | import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerService; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 39 | import org.onosproject.kubevirtnetworking.api.KubevirtPeerRouter; |
| 40 | import org.onosproject.kubevirtnetworking.api.KubevirtRouter; |
| 41 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent; |
| 42 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterListener; |
| 43 | import org.onosproject.store.service.TestStorageService; |
| 44 | |
Jian Li | da7ec15 | 2021-03-05 12:21:50 +0900 | [diff] [blame] | 45 | import java.util.HashSet; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 46 | import java.util.List; |
Jian Li | da7ec15 | 2021-03-05 12:21:50 +0900 | [diff] [blame] | 47 | import java.util.Set; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 48 | |
| 49 | import static org.junit.Assert.assertEquals; |
| 50 | import static org.junit.Assert.assertNotNull; |
| 51 | import static org.junit.Assert.assertNull; |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 52 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_ASSOCIATED; |
| 53 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_CREATED; |
| 54 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_DISASSOCIATED; |
| 55 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_REMOVED; |
| 56 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_FLOATING_IP_UPDATED; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 57 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_CREATED; |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 58 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_EXTERNAL_NETWORK_ATTACHED; |
| 59 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_EXTERNAL_NETWORK_DETACHED; |
| 60 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_INTERNAL_NETWORKS_ATTACHED; |
| 61 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_INTERNAL_NETWORKS_DETACHED; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 62 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_REMOVED; |
| 63 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_UPDATED; |
| 64 | |
| 65 | /** |
| 66 | * Unit tests for kubernetes router manager. |
| 67 | */ |
| 68 | public class KubevirtRouterManagerTest { |
| 69 | |
| 70 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 71 | |
| 72 | private static final String ROUTER_NAME = "router-1"; |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 73 | private static final String POD_NAME = "pod-1"; |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 74 | private static final String NETWORK_NAME = "flat-1"; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 75 | private static final String UPDATED_DESCRIPTION = "router-updated"; |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 76 | private static final MacAddress UPDATED_MAC = MacAddress.valueOf("FF:FF:FF:FF:FF:FF"); |
| 77 | |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 78 | private static final String FLOATING_IP_ID = "fip-1"; |
| 79 | private static final String UNKNOWN_ID = "unknown"; |
| 80 | |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 81 | private static final KubevirtRouter ROUTER = DefaultKubevirtRouter.builder() |
| 82 | .name(ROUTER_NAME) |
| 83 | .description(ROUTER_NAME) |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 84 | .internal(ImmutableSet.of()) |
| 85 | .external(ImmutableMap.of()) |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 86 | .enableSnat(true) |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 87 | .build(); |
| 88 | |
| 89 | private static final KubevirtRouter ROUTER_UPDATED = DefaultKubevirtRouter.builder() |
| 90 | .name(ROUTER_NAME) |
| 91 | .description(UPDATED_DESCRIPTION) |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 92 | .internal(ImmutableSet.of()) |
| 93 | .external(ImmutableMap.of()) |
| 94 | .enableSnat(true) |
| 95 | .build(); |
| 96 | |
| 97 | private static final KubevirtRouter ROUTER_WITH_INTERNAL = DefaultKubevirtRouter.builder() |
| 98 | .name(ROUTER_NAME) |
| 99 | .description(ROUTER_NAME) |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 100 | .internal(ImmutableSet.of("vxlan-1", "vxlan-2")) |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 101 | .external(ImmutableMap.of()) |
| 102 | .enableSnat(true) |
| 103 | .build(); |
| 104 | |
Jian Li | da7ec15 | 2021-03-05 12:21:50 +0900 | [diff] [blame] | 105 | private static final KubevirtRouter ROUTER_WITH_SINGLE_INTERNAL = DefaultKubevirtRouter.builder() |
| 106 | .name(ROUTER_NAME) |
| 107 | .description(ROUTER_NAME) |
| 108 | .internal(ImmutableSet.of("vxlan-1")) |
| 109 | .external(ImmutableMap.of()) |
| 110 | .enableSnat(true) |
| 111 | .build(); |
| 112 | |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 113 | private static final KubevirtRouter ROUTER_WITH_EXTERNAL = DefaultKubevirtRouter.builder() |
| 114 | .name(ROUTER_NAME) |
| 115 | .description(ROUTER_NAME) |
| 116 | .internal(ImmutableSet.of()) |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 117 | .external(ImmutableMap.of("10.10.10.10", "flat")) |
| 118 | .enableSnat(true) |
| 119 | .peerRouter(new KubevirtPeerRouter(IpAddress.valueOf("20.20.20.20"), |
| 120 | MacAddress.valueOf("11:22:33:44:55:66"))) |
| 121 | .build(); |
| 122 | |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 123 | private static final KubevirtFloatingIp FLOATING_IP_DISASSOCIATED = DefaultKubevirtFloatingIp.builder() |
| 124 | .id(FLOATING_IP_ID) |
| 125 | .routerName(ROUTER_NAME) |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 126 | .networkName(NETWORK_NAME) |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 127 | .floatingIp(IpAddress.valueOf("10.10.10.10")) |
| 128 | .build(); |
| 129 | |
| 130 | private static final KubevirtFloatingIp FLOATING_IP_ASSOCIATED = DefaultKubevirtFloatingIp.builder() |
| 131 | .id(FLOATING_IP_ID) |
| 132 | .routerName(ROUTER_NAME) |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 133 | .networkName(NETWORK_NAME) |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 134 | .floatingIp(IpAddress.valueOf("10.10.10.10")) |
| 135 | .fixedIp(IpAddress.valueOf("20.20.20.20")) |
| 136 | .podName(POD_NAME) |
| 137 | .build(); |
| 138 | |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 139 | private final TestKubevirtRouterListener testListener = new TestKubevirtRouterListener(); |
| 140 | |
| 141 | private KubevirtRouterManager target; |
| 142 | private DistributedKubevirtRouterStore kubevirtRouterStore; |
| 143 | |
| 144 | @Before |
| 145 | public void setUp() throws Exception { |
| 146 | kubevirtRouterStore = new DistributedKubevirtRouterStore(); |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame^] | 147 | |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 148 | TestUtils.setField(kubevirtRouterStore, "coreService", new TestCoreService()); |
| 149 | TestUtils.setField(kubevirtRouterStore, "storageService", new TestStorageService()); |
| 150 | TestUtils.setField(kubevirtRouterStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame^] | 151 | TestUtils.setField(kubevirtRouterStore, "loadBalancerService", new TestLoadBalancerService()); |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 152 | kubevirtRouterStore.activate(); |
| 153 | |
| 154 | target = new KubevirtRouterManager(); |
| 155 | TestUtils.setField(target, "coreService", new TestCoreService()); |
| 156 | target.kubevirtRouterStore = kubevirtRouterStore; |
| 157 | target.addListener(testListener); |
| 158 | target.activate(); |
| 159 | } |
| 160 | |
| 161 | @After |
| 162 | public void tearDown() { |
| 163 | target.removeListener(testListener); |
| 164 | kubevirtRouterStore.deactivate(); |
| 165 | target.deactivate(); |
| 166 | kubevirtRouterStore = null; |
| 167 | target = null; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Tests if getting all routers returns correct set of values. |
| 172 | */ |
| 173 | @Test |
| 174 | public void testGetRouters() { |
| 175 | createBasicRouters(); |
| 176 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Tests if getting a router with name returns correct value. |
| 181 | */ |
| 182 | @Test |
| 183 | public void testGetRouterByName() { |
| 184 | createBasicRouters(); |
| 185 | assertNotNull("Router did not match", target.router(ROUTER_NAME)); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Tests creating and removing a router, and checks if proper event is triggered. |
| 190 | */ |
| 191 | @Test |
| 192 | public void testCreateAndRemoveRouter() { |
| 193 | target.createRouter(ROUTER); |
| 194 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 195 | assertNotNull("Router was not created", target.router(ROUTER_NAME)); |
| 196 | |
| 197 | target.removeRouter(ROUTER_NAME); |
| 198 | assertEquals("Number of router did not match", 0, target.routers().size()); |
| 199 | assertNull("Router was not created", target.router(ROUTER_NAME)); |
| 200 | |
| 201 | validateEvents(KUBEVIRT_ROUTER_CREATED, KUBEVIRT_ROUTER_REMOVED); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Tests creating and updating a port, and checks if proper event is triggered. |
| 206 | */ |
| 207 | @Test |
| 208 | public void testCreateAndUpdateRouter() { |
| 209 | target.createRouter(ROUTER); |
| 210 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 211 | |
| 212 | target.updateRouter(ROUTER_UPDATED); |
| 213 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 214 | assertEquals("Router did not match", UPDATED_DESCRIPTION, target.router(ROUTER_NAME).description()); |
| 215 | |
| 216 | validateEvents(KUBEVIRT_ROUTER_CREATED, KUBEVIRT_ROUTER_UPDATED); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Tests updating peer router MAC address. |
| 221 | */ |
| 222 | @Test |
| 223 | public void testPeerRouterMacUpdate() { |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 224 | target.createRouter(ROUTER_WITH_EXTERNAL); |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 225 | |
| 226 | target.updatePeerRouterMac(ROUTER_NAME, UPDATED_MAC); |
| 227 | assertEquals("MAC address was not updated", UPDATED_MAC, |
| 228 | target.router(ROUTER_NAME).peerRouter().macAddress()); |
| 229 | |
| 230 | validateEvents(KUBEVIRT_ROUTER_CREATED, KUBEVIRT_ROUTER_UPDATED); |
| 231 | } |
| 232 | |
| 233 | /** |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 234 | * Tests router's internal networks attached and detached. |
| 235 | */ |
| 236 | @Test |
| 237 | public void testRouterInternalAttachedAndDetached() { |
| 238 | target.createRouter(ROUTER); |
| 239 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 240 | assertEquals("Router internal did not match", 0, target.router(ROUTER_NAME).internal().size()); |
| 241 | |
| 242 | target.updateRouter(ROUTER_WITH_INTERNAL); |
| 243 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 244 | assertEquals("Router internal did not match", 2, target.router(ROUTER_NAME).internal().size()); |
| 245 | |
| 246 | target.updateRouter(ROUTER); |
| 247 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 248 | assertEquals("Router internal did not match", 0, target.router(ROUTER_NAME).internal().size()); |
| 249 | |
| 250 | validateEvents(KUBEVIRT_ROUTER_CREATED, KUBEVIRT_ROUTER_UPDATED, |
| 251 | KUBEVIRT_ROUTER_INTERNAL_NETWORKS_ATTACHED, KUBEVIRT_ROUTER_UPDATED, |
| 252 | KUBEVIRT_ROUTER_INTERNAL_NETWORKS_DETACHED); |
| 253 | } |
| 254 | |
Jian Li | da7ec15 | 2021-03-05 12:21:50 +0900 | [diff] [blame] | 255 | @Test |
| 256 | public void testRouterInternalShrink() { |
| 257 | target.createRouter(ROUTER_WITH_INTERNAL); |
| 258 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 259 | assertEquals("Router internal did not match", 2, target.router(ROUTER_NAME).internal().size()); |
| 260 | |
| 261 | target.updateRouter(ROUTER_WITH_SINGLE_INTERNAL); |
| 262 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 263 | assertEquals("Router internal did not match", 1, target.router(ROUTER_NAME).internal().size()); |
| 264 | |
| 265 | validateEvents(KUBEVIRT_ROUTER_CREATED, KUBEVIRT_ROUTER_UPDATED, |
| 266 | KUBEVIRT_ROUTER_INTERNAL_NETWORKS_DETACHED); |
| 267 | |
| 268 | validateInternalRemoval(ImmutableSet.of("vxlan-2")); |
| 269 | } |
| 270 | |
Jian Li | b636f70 | 2021-03-03 14:46:50 +0900 | [diff] [blame] | 271 | /** |
| 272 | * Tests router's external networks attached and detached. |
| 273 | */ |
| 274 | @Test |
| 275 | public void testRouterExternalAttachedAndDetached() { |
| 276 | target.createRouter(ROUTER); |
| 277 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 278 | assertNull(target.router(ROUTER_NAME).peerRouter()); |
| 279 | assertEquals(0, target.router(ROUTER_NAME).external().size()); |
| 280 | |
| 281 | target.updateRouter(ROUTER_WITH_EXTERNAL); |
| 282 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 283 | assertNotNull(target.router(ROUTER_NAME).peerRouter()); |
| 284 | assertEquals(1, target.router(ROUTER_NAME).external().size()); |
| 285 | |
| 286 | target.updateRouter(ROUTER); |
| 287 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 288 | assertNull(target.router(ROUTER_NAME).peerRouter()); |
| 289 | assertEquals(0, target.router(ROUTER_NAME).external().size()); |
| 290 | |
| 291 | validateEvents(KUBEVIRT_ROUTER_CREATED, KUBEVIRT_ROUTER_UPDATED, |
| 292 | KUBEVIRT_ROUTER_EXTERNAL_NETWORK_ATTACHED, KUBEVIRT_ROUTER_UPDATED, |
| 293 | KUBEVIRT_ROUTER_EXTERNAL_NETWORK_DETACHED); |
| 294 | } |
| 295 | |
| 296 | /** |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 297 | * Tests if creating a null router fails with an exception. |
| 298 | */ |
| 299 | @Test(expected = NullPointerException.class) |
| 300 | public void testCreateNullRouter() { |
| 301 | target.createRouter(null); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Tests if creating a duplicate router fails with an exception. |
| 306 | */ |
| 307 | @Test(expected = IllegalArgumentException.class) |
| 308 | public void createDuplicateRouter() { |
| 309 | target.createRouter(ROUTER); |
| 310 | target.createRouter(ROUTER); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Tests if updating an unregistered router fails with an exception. |
| 315 | */ |
| 316 | @Test(expected = IllegalArgumentException.class) |
| 317 | public void testUpdateUnregisteredRouter() { |
| 318 | target.updateRouter(ROUTER); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Tests if updating a null router fails with an exception. |
| 323 | */ |
| 324 | @Test(expected = NullPointerException.class) |
| 325 | public void testUpdateNullRouter() { |
| 326 | target.updateRouter(null); |
| 327 | } |
| 328 | |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 329 | /** |
| 330 | * Tests if getting all floating IPs returns the correct set of floating IPs. |
| 331 | */ |
| 332 | @Test |
| 333 | public void testGetFloatingIps() { |
| 334 | createBasicFloatingIpDisassociated(); |
| 335 | assertEquals("Number of floating IPs did not match", 1, target.floatingIps().size()); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Tests if getting a floating IP with ID returns the correct floating IP. |
| 340 | */ |
| 341 | @Test |
| 342 | public void testGetFloatingIpById() { |
| 343 | createBasicFloatingIpDisassociated(); |
| 344 | assertNotNull("Floating IP did not match", target.floatingIp(FLOATING_IP_ID)); |
| 345 | assertNull("Floating IP did not match", target.floatingIp(UNKNOWN_ID)); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Tests if getting a floating IP with POD name returns the correct floating IP. |
| 350 | */ |
| 351 | @Test |
| 352 | public void testGetFloatingIpByPodName() { |
| 353 | createBasicFloatingIpAssociated(); |
| 354 | assertNotNull("Floating IP did not match", target.floatingIpByPodName(POD_NAME)); |
| 355 | assertNull("Floating IP did not match", target.floatingIpByPodName(UNKNOWN_ID)); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Tests if getting floating IPs with router name returns the correct floating IPs. |
| 360 | */ |
| 361 | @Test |
| 362 | public void testGetFloatingIpsByRouterName() { |
| 363 | createBasicFloatingIpDisassociated(); |
| 364 | assertEquals("Number of floating IPs did not match", 1, |
| 365 | target.floatingIpsByRouter(ROUTER_NAME).size()); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Tests creating and removing a floating IP, and checks if it triggers proper events. |
| 370 | */ |
| 371 | @Test |
| 372 | public void testCreateAndRemoveFloatingIp() { |
| 373 | target.createFloatingIp(FLOATING_IP_DISASSOCIATED); |
| 374 | assertEquals("Number of floating IP did not match", 1, target.floatingIps().size()); |
| 375 | assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID)); |
| 376 | |
| 377 | target.removeFloatingIp(FLOATING_IP_ID); |
| 378 | assertEquals("Number of floating IP did not match", 0, target.floatingIps().size()); |
| 379 | assertNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID)); |
| 380 | |
| 381 | validateEvents(KUBEVIRT_FLOATING_IP_CREATED, KUBEVIRT_FLOATING_IP_REMOVED); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Tests associating a floating IP, and checks if it triggers proper events. |
| 386 | */ |
| 387 | @Test |
| 388 | public void testAssociateFloatingIp() { |
| 389 | target.createFloatingIp(FLOATING_IP_DISASSOCIATED); |
| 390 | assertEquals("Number of floating IP did not match", 1, target.floatingIps().size()); |
| 391 | assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID)); |
| 392 | |
| 393 | target.updateFloatingIp(FLOATING_IP_ASSOCIATED); |
| 394 | assertEquals("Number of floating IP did not match", 1, target.floatingIps().size()); |
| 395 | assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID)); |
| 396 | |
| 397 | validateEvents(KUBEVIRT_FLOATING_IP_CREATED, KUBEVIRT_FLOATING_IP_UPDATED, |
| 398 | KUBEVIRT_FLOATING_IP_ASSOCIATED); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Tests disassociating a floating IP, and checks if it triggers proper events. |
| 403 | */ |
| 404 | @Test |
| 405 | public void testDisassociateFloatingIp() { |
| 406 | target.createFloatingIp(FLOATING_IP_ASSOCIATED); |
| 407 | assertEquals("Number of floating IP did not match", 1, target.floatingIps().size()); |
| 408 | assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID)); |
| 409 | |
| 410 | target.updateFloatingIp(FLOATING_IP_DISASSOCIATED); |
| 411 | assertEquals("Number of floating IP did not match", 1, target.floatingIps().size()); |
| 412 | assertNotNull("Floating IP was not created", target.floatingIp(FLOATING_IP_ID)); |
| 413 | |
| 414 | validateEvents(KUBEVIRT_FLOATING_IP_CREATED, KUBEVIRT_FLOATING_IP_UPDATED, |
| 415 | KUBEVIRT_FLOATING_IP_DISASSOCIATED); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Tests if creating a null floating IP fails with an exception. |
| 420 | */ |
| 421 | @Test(expected = NullPointerException.class) |
| 422 | public void testCreateNullFloatingIp() { |
| 423 | target.createFloatingIp(null); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Tests if creating a duplicate floating IP fails with an exception. |
| 428 | */ |
| 429 | @Test(expected = IllegalArgumentException.class) |
| 430 | public void testCreateDuplicateFloatingIp() { |
| 431 | target.createFloatingIp(FLOATING_IP_ASSOCIATED); |
| 432 | target.createFloatingIp(FLOATING_IP_DISASSOCIATED); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Tests if removing floating IP with null ID fails with an exception. |
| 437 | */ |
| 438 | @Test(expected = IllegalArgumentException.class) |
| 439 | public void testRemoveFloatingIpWithNull() { |
| 440 | target.removeFloatingIp(null); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Tests if updating an unregistered floating IP fails with an exception. |
| 445 | */ |
| 446 | @Test(expected = IllegalArgumentException.class) |
| 447 | public void testUpdateUnregisteredFloatingIp() { |
| 448 | target.updateFloatingIp(FLOATING_IP_ASSOCIATED); |
| 449 | } |
| 450 | |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 451 | private void createBasicRouters() { |
| 452 | target.createRouter(ROUTER); |
| 453 | } |
| 454 | |
Jian Li | 073f1ba | 2021-02-28 03:50:15 +0900 | [diff] [blame] | 455 | private void createBasicFloatingIpDisassociated() { |
| 456 | target.createFloatingIp(FLOATING_IP_DISASSOCIATED); |
| 457 | } |
| 458 | |
| 459 | private void createBasicFloatingIpAssociated() { |
| 460 | target.createFloatingIp(FLOATING_IP_ASSOCIATED); |
| 461 | } |
| 462 | |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 463 | private static class TestCoreService extends CoreServiceAdapter { |
| 464 | |
| 465 | @Override |
| 466 | public ApplicationId registerApplication(String name) { |
| 467 | return TEST_APP_ID; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | private static class TestKubevirtRouterListener implements KubevirtRouterListener { |
| 472 | |
| 473 | private List<KubevirtRouterEvent> events = Lists.newArrayList(); |
Jian Li | da7ec15 | 2021-03-05 12:21:50 +0900 | [diff] [blame] | 474 | private Set<String> internalAdded = new HashSet<>(); |
| 475 | private Set<String> internalRemoved = new HashSet<>(); |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 476 | |
| 477 | @Override |
| 478 | public void event(KubevirtRouterEvent event) { |
| 479 | events.add(event); |
Jian Li | da7ec15 | 2021-03-05 12:21:50 +0900 | [diff] [blame] | 480 | if (event.type() == KUBEVIRT_ROUTER_INTERNAL_NETWORKS_ATTACHED) { |
| 481 | internalAdded = event.internal(); |
| 482 | } |
| 483 | if (event.type() == KUBEVIRT_ROUTER_INTERNAL_NETWORKS_DETACHED) { |
| 484 | internalRemoved = event.internal(); |
| 485 | } |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
| 489 | private void validateEvents(Enum... types) { |
| 490 | int i = 0; |
| 491 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 492 | for (Event event : testListener.events) { |
| 493 | assertEquals("Incorrect event received", types[i], event.type()); |
| 494 | i++; |
| 495 | } |
| 496 | testListener.events.clear(); |
| 497 | } |
Jian Li | da7ec15 | 2021-03-05 12:21:50 +0900 | [diff] [blame] | 498 | |
| 499 | private void validateInternalAddition(Set<String> internal) { |
| 500 | assertEquals("internal addition entries", internal, testListener.internalAdded); |
| 501 | testListener.internalAdded.clear(); |
| 502 | } |
| 503 | |
| 504 | private void validateInternalRemoval(Set<String> internal) { |
| 505 | assertEquals("internal addition entries", internal, testListener.internalRemoved); |
| 506 | testListener.internalRemoved.clear(); |
| 507 | } |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame^] | 508 | |
| 509 | private class TestLoadBalancerService implements KubevirtLoadBalancerService { |
| 510 | |
| 511 | @Override |
| 512 | public KubevirtLoadBalancer loadBalancer(String name) { |
| 513 | return null; |
| 514 | } |
| 515 | |
| 516 | @Override |
| 517 | public Set<KubevirtLoadBalancer> loadBalancers() { |
| 518 | return Sets.newHashSet(); |
| 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public void addListener(KubevirtLoadBalancerListener listener) { |
| 523 | |
| 524 | } |
| 525 | |
| 526 | @Override |
| 527 | public void removeListener(KubevirtLoadBalancerListener listener) { |
| 528 | |
| 529 | } |
| 530 | } |
Jian Li | 810f58c | 2021-02-27 01:10:50 +0900 | [diff] [blame] | 531 | } |