Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-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.k8snode.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.cluster.ClusterServiceAdapter; |
| 27 | import org.onosproject.cluster.LeadershipServiceAdapter; |
| 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.k8snode.api.DefaultK8sHost; |
| 33 | import org.onosproject.k8snode.api.K8sHost; |
| 34 | import org.onosproject.k8snode.api.K8sHostEvent; |
| 35 | import org.onosproject.k8snode.api.K8sHostListener; |
| 36 | import org.onosproject.k8snode.api.K8sHostState; |
| 37 | import org.onosproject.store.service.TestStorageService; |
| 38 | |
| 39 | import java.util.List; |
| 40 | import java.util.Set; |
| 41 | |
| 42 | import static org.junit.Assert.assertEquals; |
| 43 | import static org.junit.Assert.assertNotNull; |
| 44 | import static org.junit.Assert.assertNull; |
| 45 | import static org.junit.Assert.assertTrue; |
| 46 | import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_HOST_COMPLETE; |
| 47 | import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_HOST_CREATED; |
| 48 | import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_HOST_REMOVED; |
| 49 | import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_HOST_UPDATED; |
| 50 | import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_NODES_ADDED; |
| 51 | import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_NODES_REMOVED; |
| 52 | import static org.onosproject.k8snode.api.K8sHostState.COMPLETE; |
| 53 | import static org.onosproject.k8snode.api.K8sHostState.INIT; |
| 54 | |
| 55 | /** |
| 56 | * Unit tests for kubernetes host manager. |
| 57 | */ |
| 58 | public class K8sHostManagerTest { |
| 59 | |
| 60 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 61 | |
| 62 | private static final String ERR_SIZE = "Number of hosts did not match"; |
| 63 | private static final String ERR_NOT_MATCH = "Host did not match"; |
| 64 | private static final String ERR_NOT_FOUND = "Host did not exist"; |
| 65 | |
| 66 | private static final IpAddress HOST_IP_1 = IpAddress.valueOf("192.168.100.2"); |
| 67 | private static final IpAddress HOST_IP_2 = IpAddress.valueOf("192.168.101.2"); |
| 68 | private static final IpAddress HOST_IP_3 = IpAddress.valueOf("192.168.102.2"); |
| 69 | |
| 70 | private static final K8sHost HOST_1 = createHost( |
| 71 | HOST_IP_1, |
| 72 | INIT, |
| 73 | ImmutableSet.of("1", "2") |
| 74 | ); |
| 75 | |
| 76 | private static final K8sHost HOST_2 = createHost( |
| 77 | HOST_IP_2, |
| 78 | INIT, |
| 79 | ImmutableSet.of("3", "4") |
| 80 | ); |
| 81 | |
| 82 | private static final K8sHost HOST_3 = createHost( |
| 83 | HOST_IP_3, |
| 84 | COMPLETE, |
| 85 | ImmutableSet.of("5", "6") |
| 86 | ); |
| 87 | |
| 88 | private final TestK8sHostListener testListener = new TestK8sHostListener(); |
| 89 | |
| 90 | private K8sHostManager target; |
| 91 | private DistributedK8sHostStore hostStore; |
| 92 | |
| 93 | /** |
| 94 | * Initial setup for this unit test. |
| 95 | */ |
| 96 | @Before |
| 97 | public void setUp() { |
| 98 | hostStore = new DistributedK8sHostStore(); |
| 99 | TestUtils.setField(hostStore, "coreService", new TestCoreService()); |
| 100 | TestUtils.setField(hostStore, "storageService", new TestStorageService()); |
| 101 | TestUtils.setField(hostStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
| 102 | hostStore.activate(); |
| 103 | |
| 104 | hostStore.createHost(HOST_2); |
| 105 | hostStore.createHost(HOST_3); |
| 106 | |
| 107 | target = new K8sHostManager(); |
| 108 | target.storageService = new TestStorageService(); |
| 109 | target.coreService = new TestCoreService(); |
| 110 | target.clusterService = new TestClusterService(); |
| 111 | target.leadershipService = new TestLeadershipService(); |
| 112 | target.hostStore = hostStore; |
| 113 | target.addListener(testListener); |
| 114 | target.activate(); |
| 115 | testListener.events.clear(); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Clean up unit test. |
| 120 | */ |
| 121 | @After |
| 122 | public void tearDown() { |
| 123 | target.removeListener(testListener); |
| 124 | target.deactivate(); |
| 125 | hostStore.deactivate(); |
| 126 | hostStore = null; |
| 127 | target = null; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Checks if creating and removing a host work well with proper events. |
| 132 | */ |
| 133 | @Test |
| 134 | public void testCreateAndRemoveHost() { |
| 135 | target.createHost(HOST_1); |
| 136 | assertEquals(ERR_SIZE, 3, target.hosts().size()); |
| 137 | assertNotNull(target.host(HOST_IP_1)); |
| 138 | |
| 139 | target.removeHost(HOST_IP_1); |
| 140 | assertEquals(ERR_SIZE, 2, target.hosts().size()); |
| 141 | assertNull(target.host(HOST_IP_1)); |
| 142 | |
| 143 | validateEvents(K8S_HOST_CREATED, K8S_HOST_REMOVED); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Checks if creating null host fails with proper exception. |
| 148 | */ |
| 149 | @Test(expected = NullPointerException.class) |
| 150 | public void testCreateNullHost() { |
| 151 | target.createHost(null); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Checks if creating a duplicated host fails with proper exception. |
| 156 | */ |
| 157 | @Test(expected = IllegalArgumentException.class) |
| 158 | public void testCreateDuplicateHost() { |
| 159 | target.createHost(HOST_1); |
| 160 | target.createHost(HOST_1); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Checks if removing null host fails with proper exception. |
| 165 | */ |
| 166 | @Test(expected = IllegalArgumentException.class) |
| 167 | public void testRemoveNullHost() { |
| 168 | target.removeHost(null); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Checks if updating a host works well with proper event. |
| 173 | */ |
| 174 | @Test |
| 175 | public void testUpdateHost() { |
| 176 | K8sHost updated = HOST_2.updateState(COMPLETE); |
| 177 | target.updateHost(updated); |
| 178 | validateEvents(K8S_HOST_UPDATED, K8S_HOST_COMPLETE); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Checks if updating a null host fails with proper exception. |
| 183 | */ |
| 184 | @Test(expected = NullPointerException.class) |
| 185 | public void testUpdateNullHost() { |
| 186 | target.updateHost(null); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Checks if updating not existing host fails with proper exception. |
| 191 | */ |
| 192 | @Test(expected = IllegalArgumentException.class) |
| 193 | public void testUpdateNotExistingHost() { |
| 194 | target.updateHost(HOST_1); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Checks if adding nodes into host works well with proper event. |
| 199 | */ |
| 200 | @Test |
| 201 | public void testAddNodesToHost() { |
| 202 | K8sHost updated = HOST_2.updateNodeNames(ImmutableSet.of("3", "4", "5")); |
| 203 | target.updateHost(updated); |
| 204 | validateEvents(K8S_HOST_UPDATED, K8S_NODES_ADDED); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Checks if removing nodes from host works well with proper event. |
| 209 | */ |
| 210 | @Test |
| 211 | public void testRemoveNodesFromHost() { |
| 212 | K8sHost updated = HOST_2.updateNodeNames(ImmutableSet.of("3")); |
| 213 | target.updateHost(updated); |
| 214 | validateEvents(K8S_HOST_UPDATED, K8S_NODES_REMOVED); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Checks if getting all hosts method returns correct set of nodes. |
| 219 | */ |
| 220 | @Test |
| 221 | public void testGetAllHosts() { |
| 222 | assertEquals(ERR_SIZE, 2, target.hosts().size()); |
| 223 | assertTrue(ERR_NOT_FOUND, target.hosts().contains(HOST_2)); |
| 224 | assertTrue(ERR_NOT_FOUND, target.hosts().contains(HOST_3)); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Checks if getting complete hosts method returns correct set of nodes. |
| 229 | */ |
| 230 | @Test |
| 231 | public void testGetCompleteHosts() { |
| 232 | assertEquals(ERR_SIZE, 1, target.completeHosts().size()); |
| 233 | assertTrue(ERR_NOT_FOUND, target.completeHosts().contains(HOST_3)); |
| 234 | } |
| 235 | |
| 236 | private void validateEvents(Enum... types) { |
| 237 | int i = 0; |
| 238 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 239 | for (Event event : testListener.events) { |
| 240 | assertEquals("Incorrect event received", types[i], event.type()); |
| 241 | i++; |
| 242 | } |
| 243 | testListener.events.clear(); |
| 244 | } |
| 245 | |
| 246 | private static class TestK8sHostListener implements K8sHostListener { |
| 247 | private List<K8sHostEvent> events = Lists.newArrayList(); |
| 248 | |
| 249 | @Override |
| 250 | public void event(K8sHostEvent event) { |
| 251 | events.add(event); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | private static class TestCoreService extends CoreServiceAdapter { |
| 256 | @Override |
| 257 | public ApplicationId registerApplication(String name) { |
| 258 | return TEST_APP_ID; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | private class TestClusterService extends ClusterServiceAdapter { |
| 263 | |
| 264 | } |
| 265 | |
| 266 | private static class TestLeadershipService extends LeadershipServiceAdapter { |
| 267 | |
| 268 | } |
| 269 | |
| 270 | private static K8sHost createHost(IpAddress hostIp, K8sHostState state, Set<String> nodeNames) { |
| 271 | return DefaultK8sHost.builder() |
| 272 | .hostIp(hostIp) |
| 273 | .nodeNames(nodeNames) |
| 274 | .state(state) |
| 275 | .build(); |
| 276 | } |
| 277 | } |