Jian Li | 8fe714d | 2021-01-11 02:35:06 +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.ImmutableSet; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.util.concurrent.MoreExecutors; |
| 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.junit.TestUtils; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreServiceAdapter; |
| 28 | import org.onosproject.core.DefaultApplicationId; |
| 29 | import org.onosproject.event.Event; |
| 30 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtNetwork; |
| 31 | import org.onosproject.kubevirtnetworking.api.KubevirtIpPool; |
| 32 | import org.onosproject.kubevirtnetworking.api.KubevirtNetwork; |
| 33 | import org.onosproject.kubevirtnetworking.api.KubevirtNetworkEvent; |
| 34 | import org.onosproject.kubevirtnetworking.api.KubevirtNetworkListener; |
| 35 | import org.onosproject.store.service.TestStorageService; |
| 36 | |
| 37 | import java.util.List; |
| 38 | |
| 39 | import static org.junit.Assert.assertEquals; |
| 40 | import static org.junit.Assert.assertNotNull; |
| 41 | import static org.junit.Assert.assertNull; |
| 42 | import static org.onosproject.kubevirtnetworking.api.KubevirtNetwork.Type.VXLAN; |
| 43 | import static org.onosproject.kubevirtnetworking.api.KubevirtNetworkEvent.Type.KUBEVIRT_NETWORK_CREATED; |
| 44 | import static org.onosproject.kubevirtnetworking.api.KubevirtNetworkEvent.Type.KUBEVIRT_NETWORK_REMOVED; |
| 45 | import static org.onosproject.kubevirtnetworking.api.KubevirtNetworkEvent.Type.KUBEVIRT_NETWORK_UPDATED; |
| 46 | |
| 47 | /** |
| 48 | * Unit tests for kubevirt network manager. |
| 49 | */ |
| 50 | public class KubevirtNetworkManagerTest { |
| 51 | |
| 52 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 53 | |
| 54 | private static final String UNKNOWN_ID = "unknown_id"; |
| 55 | private static final String UPDATED_NAME = "updated_name"; |
| 56 | |
| 57 | private static final String NETWORK_ID = "network_id"; |
| 58 | private static final String NETWORK_NAME = "network_name"; |
| 59 | |
| 60 | private static final KubevirtNetwork.Type TYPE = VXLAN; |
| 61 | private static final Integer MTU = 1500; |
| 62 | private static final String SEGMENT_ID = "1"; |
| 63 | private static final IpAddress GATEWAY_IP = IpAddress.valueOf("10.10.10.1"); |
| 64 | private static final String CIDR = "10.10.10.0/24"; |
| 65 | private static final IpAddress IP_POOL_START = IpAddress.valueOf("10.10.10.100"); |
| 66 | private static final IpAddress IP_POOL_END = IpAddress.valueOf("10.10.10.200"); |
| 67 | |
| 68 | private static final KubevirtNetwork NETWORK = DefaultKubevirtNetwork.builder() |
| 69 | .networkId(NETWORK_ID) |
| 70 | .name(NETWORK_NAME) |
| 71 | .type(TYPE) |
| 72 | .mtu(MTU) |
| 73 | .segmentId(SEGMENT_ID) |
| 74 | .gatewayIp(GATEWAY_IP) |
| 75 | .cidr(CIDR) |
| 76 | .ipPool(new KubevirtIpPool(IP_POOL_START, IP_POOL_END)) |
| 77 | .hostRoutes(ImmutableSet.of()) |
| 78 | .build(); |
| 79 | |
| 80 | private static final KubevirtNetwork NETWORK_UPDATED = DefaultKubevirtNetwork.builder() |
| 81 | .networkId(NETWORK_ID) |
| 82 | .name(UPDATED_NAME) |
| 83 | .type(TYPE) |
| 84 | .mtu(MTU) |
| 85 | .segmentId(SEGMENT_ID) |
| 86 | .gatewayIp(GATEWAY_IP) |
| 87 | .cidr(CIDR) |
| 88 | .ipPool(new KubevirtIpPool(IP_POOL_START, IP_POOL_END)) |
| 89 | .hostRoutes(ImmutableSet.of()) |
| 90 | .build(); |
| 91 | |
| 92 | private final TestKubevirtnetworkListener testListener = new TestKubevirtnetworkListener(); |
| 93 | |
| 94 | private KubevirtNetworkManager target; |
| 95 | private DistributedKubevirtNetworkStore networkStore; |
| 96 | |
| 97 | @Before |
| 98 | public void setUp() throws Exception { |
| 99 | networkStore = new DistributedKubevirtNetworkStore(); |
| 100 | TestUtils.setField(networkStore, "coreService", new TestCoreService()); |
| 101 | TestUtils.setField(networkStore, "storageService", new TestStorageService()); |
| 102 | TestUtils.setField(networkStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
| 103 | networkStore.activate(); |
| 104 | |
| 105 | target = new KubevirtNetworkManager(); |
| 106 | TestUtils.setField(target, "coreService", new TestCoreService()); |
| 107 | target.networkStore = networkStore; |
| 108 | target.addListener(testListener); |
| 109 | target.activate(); |
| 110 | } |
| 111 | |
| 112 | @After |
| 113 | public void tearDown() { |
| 114 | target.removeListener(testListener); |
| 115 | networkStore.deactivate(); |
| 116 | target.deactivate(); |
| 117 | networkStore = null; |
| 118 | target = null; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Tests if getting all networks returns the correct set of networks. |
| 123 | */ |
| 124 | @Test |
| 125 | public void testGetNetworks() { |
| 126 | createBasicNetworks(); |
| 127 | assertEquals("Number of network did not match", 1, target.networks().size()); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Tests if getting a network with ID returns the correct network. |
| 132 | */ |
| 133 | @Test |
| 134 | public void testGetNetworkById() { |
| 135 | createBasicNetworks(); |
| 136 | assertNotNull("Network did not match", target.network(NETWORK_ID)); |
| 137 | assertNull("Network did not match", target.network(UNKNOWN_ID)); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Tests creating and removing a network, and checks if it triggers proper events. |
| 142 | */ |
| 143 | @Test |
| 144 | public void testCreateAndRemoveNetwork() { |
| 145 | target.createNetwork(NETWORK); |
| 146 | assertEquals("Number of networks did not match", 1, target.networks().size()); |
| 147 | assertNotNull("Network was not created", target.network(NETWORK_ID)); |
| 148 | |
| 149 | target.removeNetwork(NETWORK_ID); |
| 150 | assertEquals("Number of networks did not match", 0, target.networks().size()); |
| 151 | assertNull("Network was not removed", target.network(NETWORK_ID)); |
| 152 | |
| 153 | validateEvents(KUBEVIRT_NETWORK_CREATED, KUBEVIRT_NETWORK_REMOVED); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Tests updating a network, and checks if it triggers proper events. |
| 158 | */ |
| 159 | @Test |
| 160 | public void testCreateAndUpdateNetwork() { |
| 161 | target.createNetwork(NETWORK); |
| 162 | assertEquals("Number of networks did not match", 1, target.networks().size()); |
| 163 | assertEquals("Network did not match", NETWORK_NAME, target.network(NETWORK_ID).name()); |
| 164 | |
| 165 | target.updateNetwork(NETWORK_UPDATED); |
| 166 | |
| 167 | assertEquals("Number of networks did not match", 1, target.networks().size()); |
| 168 | assertEquals("Network did not match", UPDATED_NAME, target.network(NETWORK_ID).name()); |
| 169 | validateEvents(KUBEVIRT_NETWORK_CREATED, KUBEVIRT_NETWORK_UPDATED); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Tests if creating a null network fails with an exception. |
| 174 | */ |
| 175 | @Test(expected = NullPointerException.class) |
| 176 | public void testCreateNullNetwork() { |
| 177 | target.createNetwork(null); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Tests if creating a duplicate network fails with an exception. |
| 182 | */ |
| 183 | @Test(expected = IllegalArgumentException.class) |
| 184 | public void testCreateDuplicateNetwork() { |
| 185 | target.createNetwork(NETWORK); |
| 186 | target.createNetwork(NETWORK); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Tests if removing network with null ID fails with an exception. |
| 191 | */ |
| 192 | @Test(expected = IllegalArgumentException.class) |
| 193 | public void testRemoveNetworkWithNull() { |
| 194 | target.removeNetwork(null); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Tests if updating an unregistered network fails with an exception. |
| 199 | */ |
| 200 | @Test(expected = IllegalArgumentException.class) |
| 201 | public void testUpdateUnregisteredNetwork() { |
| 202 | target.updateNetwork(NETWORK); |
| 203 | } |
| 204 | |
| 205 | private void createBasicNetworks() { |
| 206 | target.createNetwork(NETWORK); |
| 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 TestKubevirtnetworkListener implements KubevirtNetworkListener { |
| 218 | private List<KubevirtNetworkEvent> events = Lists.newArrayList(); |
| 219 | |
| 220 | @Override |
| 221 | public void event(KubevirtNetworkEvent event) { |
| 222 | events.add(event); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | private void validateEvents(Enum... types) { |
| 227 | int i = 0; |
| 228 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 229 | for (Event event : testListener.events) { |
| 230 | assertEquals("Incorrect event received", types[i], event.type()); |
| 231 | i++; |
| 232 | } |
| 233 | testListener.events.clear(); |
| 234 | } |
| 235 | } |