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; |
| 32 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtRouter; |
| 33 | import org.onosproject.kubevirtnetworking.api.KubevirtPeerRouter; |
| 34 | import org.onosproject.kubevirtnetworking.api.KubevirtRouter; |
| 35 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent; |
| 36 | import org.onosproject.kubevirtnetworking.api.KubevirtRouterListener; |
| 37 | import org.onosproject.store.service.TestStorageService; |
| 38 | |
| 39 | import java.util.List; |
| 40 | |
| 41 | import static org.junit.Assert.assertEquals; |
| 42 | import static org.junit.Assert.assertNotNull; |
| 43 | import static org.junit.Assert.assertNull; |
| 44 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_CREATED; |
| 45 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_REMOVED; |
| 46 | import static org.onosproject.kubevirtnetworking.api.KubevirtRouterEvent.Type.KUBEVIRT_ROUTER_UPDATED; |
| 47 | |
| 48 | /** |
| 49 | * Unit tests for kubernetes router manager. |
| 50 | */ |
| 51 | public class KubevirtRouterManagerTest { |
| 52 | |
| 53 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 54 | |
| 55 | private static final String ROUTER_NAME = "router-1"; |
| 56 | private static final String UPDATED_DESCRIPTION = "router-updated"; |
| 57 | |
| 58 | private static final MacAddress UPDATED_MAC = MacAddress.valueOf("FF:FF:FF:FF:FF:FF"); |
| 59 | |
| 60 | private static final KubevirtRouter ROUTER = DefaultKubevirtRouter.builder() |
| 61 | .name(ROUTER_NAME) |
| 62 | .description(ROUTER_NAME) |
| 63 | .internal(ImmutableSet.of("vxlan-1", "vxlan-2")) |
| 64 | .external(ImmutableMap.of("10.10.10.10", "flat")) |
| 65 | .enableSnat(true) |
| 66 | .peerRouter(new KubevirtPeerRouter(IpAddress.valueOf("20.20.20.20"), |
| 67 | MacAddress.valueOf("11:22:33:44:55:66"))) |
| 68 | .build(); |
| 69 | |
| 70 | private static final KubevirtRouter ROUTER_UPDATED = DefaultKubevirtRouter.builder() |
| 71 | .name(ROUTER_NAME) |
| 72 | .description(UPDATED_DESCRIPTION) |
| 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 final TestKubevirtRouterListener testListener = new TestKubevirtRouterListener(); |
| 81 | |
| 82 | private KubevirtRouterManager target; |
| 83 | private DistributedKubevirtRouterStore kubevirtRouterStore; |
| 84 | |
| 85 | @Before |
| 86 | public void setUp() throws Exception { |
| 87 | kubevirtRouterStore = new DistributedKubevirtRouterStore(); |
| 88 | TestUtils.setField(kubevirtRouterStore, "coreService", new TestCoreService()); |
| 89 | TestUtils.setField(kubevirtRouterStore, "storageService", new TestStorageService()); |
| 90 | TestUtils.setField(kubevirtRouterStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
| 91 | kubevirtRouterStore.activate(); |
| 92 | |
| 93 | target = new KubevirtRouterManager(); |
| 94 | TestUtils.setField(target, "coreService", new TestCoreService()); |
| 95 | target.kubevirtRouterStore = kubevirtRouterStore; |
| 96 | target.addListener(testListener); |
| 97 | target.activate(); |
| 98 | } |
| 99 | |
| 100 | @After |
| 101 | public void tearDown() { |
| 102 | target.removeListener(testListener); |
| 103 | kubevirtRouterStore.deactivate(); |
| 104 | target.deactivate(); |
| 105 | kubevirtRouterStore = null; |
| 106 | target = null; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Tests if getting all routers returns correct set of values. |
| 111 | */ |
| 112 | @Test |
| 113 | public void testGetRouters() { |
| 114 | createBasicRouters(); |
| 115 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Tests if getting a router with name returns correct value. |
| 120 | */ |
| 121 | @Test |
| 122 | public void testGetRouterByName() { |
| 123 | createBasicRouters(); |
| 124 | assertNotNull("Router did not match", target.router(ROUTER_NAME)); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Tests creating and removing a router, and checks if proper event is triggered. |
| 129 | */ |
| 130 | @Test |
| 131 | public void testCreateAndRemoveRouter() { |
| 132 | target.createRouter(ROUTER); |
| 133 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 134 | assertNotNull("Router was not created", target.router(ROUTER_NAME)); |
| 135 | |
| 136 | target.removeRouter(ROUTER_NAME); |
| 137 | assertEquals("Number of router did not match", 0, target.routers().size()); |
| 138 | assertNull("Router was not created", target.router(ROUTER_NAME)); |
| 139 | |
| 140 | validateEvents(KUBEVIRT_ROUTER_CREATED, KUBEVIRT_ROUTER_REMOVED); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Tests creating and updating a port, and checks if proper event is triggered. |
| 145 | */ |
| 146 | @Test |
| 147 | public void testCreateAndUpdateRouter() { |
| 148 | target.createRouter(ROUTER); |
| 149 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 150 | |
| 151 | target.updateRouter(ROUTER_UPDATED); |
| 152 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 153 | assertEquals("Router did not match", UPDATED_DESCRIPTION, target.router(ROUTER_NAME).description()); |
| 154 | |
| 155 | validateEvents(KUBEVIRT_ROUTER_CREATED, KUBEVIRT_ROUTER_UPDATED); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Tests updating peer router MAC address. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testPeerRouterMacUpdate() { |
| 163 | target.createRouter(ROUTER); |
| 164 | |
| 165 | target.updatePeerRouterMac(ROUTER_NAME, UPDATED_MAC); |
| 166 | assertEquals("MAC address was not updated", UPDATED_MAC, |
| 167 | target.router(ROUTER_NAME).peerRouter().macAddress()); |
| 168 | |
| 169 | validateEvents(KUBEVIRT_ROUTER_CREATED, KUBEVIRT_ROUTER_UPDATED); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Tests if creating a null router fails with an exception. |
| 174 | */ |
| 175 | @Test(expected = NullPointerException.class) |
| 176 | public void testCreateNullRouter() { |
| 177 | target.createRouter(null); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Tests if creating a duplicate router fails with an exception. |
| 182 | */ |
| 183 | @Test(expected = IllegalArgumentException.class) |
| 184 | public void createDuplicateRouter() { |
| 185 | target.createRouter(ROUTER); |
| 186 | target.createRouter(ROUTER); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Tests if updating an unregistered router fails with an exception. |
| 191 | */ |
| 192 | @Test(expected = IllegalArgumentException.class) |
| 193 | public void testUpdateUnregisteredRouter() { |
| 194 | target.updateRouter(ROUTER); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Tests if updating a null router fails with an exception. |
| 199 | */ |
| 200 | @Test(expected = NullPointerException.class) |
| 201 | public void testUpdateNullRouter() { |
| 202 | target.updateRouter(null); |
| 203 | } |
| 204 | |
| 205 | private void createBasicRouters() { |
| 206 | target.createRouter(ROUTER); |
| 207 | } |
| 208 | |
| 209 | private static class TestCoreService extends CoreServiceAdapter { |
| 210 | |
| 211 | @Override |
| 212 | public ApplicationId registerApplication(String name) { |
| 213 | return TEST_APP_ID; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | private static class TestKubevirtRouterListener implements KubevirtRouterListener { |
| 218 | |
| 219 | private List<KubevirtRouterEvent> events = Lists.newArrayList(); |
| 220 | |
| 221 | @Override |
| 222 | public void event(KubevirtRouterEvent event) { |
| 223 | events.add(event); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | private void validateEvents(Enum... types) { |
| 228 | int i = 0; |
| 229 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 230 | for (Event event : testListener.events) { |
| 231 | assertEquals("Incorrect event received", types[i], event.type()); |
| 232 | i++; |
| 233 | } |
| 234 | testListener.events.clear(); |
| 235 | } |
| 236 | } |