Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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.Lists; |
| 19 | import com.google.common.util.concurrent.MoreExecutors; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.junit.TestUtils; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.cluster.ClusterServiceAdapter; |
| 26 | import org.onosproject.cluster.LeadershipServiceAdapter; |
| 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreServiceAdapter; |
| 29 | import org.onosproject.core.DefaultApplicationId; |
| 30 | import org.onosproject.event.Event; |
| 31 | import org.onosproject.k8snode.api.DefaultK8sApiConfig; |
| 32 | import org.onosproject.k8snode.api.K8sApiConfig; |
| 33 | import org.onosproject.k8snode.api.K8sApiConfigEvent; |
| 34 | import org.onosproject.k8snode.api.K8sApiConfigListener; |
| 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.junit.Assert.assertTrue; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 43 | import static org.onosproject.k8snode.api.K8sApiConfig.State.DISCONNECTED; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 44 | import static org.onosproject.k8snode.api.K8sApiConfigEvent.Type.K8S_API_CONFIG_CREATED; |
| 45 | import static org.onosproject.k8snode.api.K8sApiConfigEvent.Type.K8S_API_CONFIG_REMOVED; |
| 46 | import static org.onosproject.k8snode.util.K8sNodeUtil.endpoint; |
| 47 | |
| 48 | /** |
| 49 | * Unit tests for kubernetes API config manager. |
| 50 | */ |
| 51 | public class K8sApiConfigManagerTest { |
| 52 | |
| 53 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 54 | |
| 55 | private static final String ERR_SIZE = "Number of configs did not match"; |
| 56 | private static final String ERR_NOT_MATCH = "Config did not match"; |
| 57 | private static final String ERR_NOT_FOUND = "Config did not exist"; |
| 58 | |
| 59 | private K8sApiConfig apiConfig1; |
| 60 | private K8sApiConfig apiConfig2; |
| 61 | private K8sApiConfig apiConfig3; |
| 62 | |
| 63 | private final TestK8sApiConfigListener testListener = new TestK8sApiConfigListener(); |
| 64 | |
| 65 | private K8sApiConfigManager target; |
| 66 | private DistributedK8sApiConfigStore configStore; |
| 67 | |
| 68 | /** |
| 69 | * Initial setup for this unit test. |
| 70 | */ |
| 71 | @Before |
| 72 | public void setUp() { |
| 73 | |
| 74 | apiConfig1 = DefaultK8sApiConfig.builder() |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 75 | .clusterName("kubernetes1") |
| 76 | .segmentId(1) |
| 77 | .mode(K8sApiConfig.Mode.NORMAL) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 78 | .scheme(K8sApiConfig.Scheme.HTTP) |
| 79 | .ipAddress(IpAddress.valueOf("10.10.10.2")) |
| 80 | .port(6443) |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 81 | .state(DISCONNECTED) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 82 | .build(); |
| 83 | apiConfig2 = DefaultK8sApiConfig.builder() |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 84 | .clusterName("kubernetes2") |
| 85 | .segmentId(2) |
| 86 | .mode(K8sApiConfig.Mode.NORMAL) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 87 | .scheme(K8sApiConfig.Scheme.HTTPS) |
| 88 | .ipAddress(IpAddress.valueOf("10.10.10.3")) |
| 89 | .port(6443) |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 90 | .state(DISCONNECTED) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 91 | .token("token") |
| 92 | .caCertData("caCertData") |
| 93 | .clientCertData("clientCertData") |
| 94 | .clientKeyData("clientKeyData") |
| 95 | .build(); |
| 96 | apiConfig3 = DefaultK8sApiConfig.builder() |
Jian Li | 58b3398 | 2020-07-01 19:07:02 +0900 | [diff] [blame^] | 97 | .clusterName("kubernetes3") |
| 98 | .segmentId(3) |
| 99 | .mode(K8sApiConfig.Mode.PASSTHROUGH) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 100 | .scheme(K8sApiConfig.Scheme.HTTP) |
| 101 | .ipAddress(IpAddress.valueOf("10.10.10.4")) |
| 102 | .port(8080) |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 103 | .state(DISCONNECTED) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 104 | .build(); |
| 105 | |
| 106 | configStore = new DistributedK8sApiConfigStore(); |
| 107 | TestUtils.setField(configStore, "coreService", new TestCoreService()); |
| 108 | TestUtils.setField(configStore, "storageService", new TestStorageService()); |
| 109 | TestUtils.setField(configStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
| 110 | configStore.activate(); |
| 111 | |
| 112 | configStore.createApiConfig(apiConfig2); |
| 113 | configStore.createApiConfig(apiConfig3); |
| 114 | |
| 115 | target = new K8sApiConfigManager(); |
| 116 | target.storageService = new TestStorageService(); |
| 117 | target.coreService = new TestCoreService(); |
| 118 | target.clusterService = new TestClusterService(); |
| 119 | target.leadershipService = new TestLeadershipService(); |
| 120 | target.configStore = configStore; |
| 121 | target.addListener(testListener); |
| 122 | target.activate(); |
| 123 | testListener.events.clear(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Clean up unit test. |
| 128 | */ |
| 129 | @After |
| 130 | public void tearDown() { |
| 131 | target.removeListener(testListener); |
| 132 | target.deactivate(); |
| 133 | configStore.deactivate(); |
| 134 | configStore = null; |
| 135 | target = null; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Checks if creating and removing a config work well with proper events. |
| 140 | */ |
| 141 | @Test |
| 142 | public void testCreateAndRemoveConfig() { |
| 143 | target.createApiConfig(apiConfig1); |
| 144 | assertEquals(ERR_SIZE, 3, target.apiConfigs().size()); |
| 145 | assertNotNull(target.apiConfig(endpoint(apiConfig1))); |
| 146 | |
| 147 | target.removeApiConfig(endpoint(apiConfig1)); |
| 148 | assertEquals(ERR_SIZE, 2, target.apiConfigs().size()); |
| 149 | assertNull(target.apiConfig(endpoint(apiConfig1))); |
| 150 | |
| 151 | validateEvents(K8S_API_CONFIG_CREATED, K8S_API_CONFIG_REMOVED); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Checks if creating null config fails with proper exception. |
| 156 | */ |
| 157 | @Test(expected = NullPointerException.class) |
| 158 | public void testCreateNullConfig() { |
| 159 | target.createApiConfig(null); |
| 160 | } |
| 161 | |
| 162 | private static class TestK8sApiConfigListener implements K8sApiConfigListener { |
| 163 | private List<K8sApiConfigEvent> events = Lists.newArrayList(); |
| 164 | |
| 165 | @Override |
| 166 | public void event(K8sApiConfigEvent event) { |
| 167 | events.add(event); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Checks if creating a duplicated config fails with proper exception. |
| 173 | */ |
| 174 | @Test(expected = IllegalArgumentException.class) |
| 175 | public void testCreateDuplicateConfig() { |
| 176 | target.createApiConfig(apiConfig1); |
| 177 | target.createApiConfig(apiConfig1); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Checks if removing null config fails with proper exception. |
| 182 | */ |
| 183 | @Test(expected = IllegalArgumentException.class) |
| 184 | public void testRemoveNullConfig() { |
| 185 | target.removeApiConfig(null); |
| 186 | } |
| 187 | |
| 188 | private void validateEvents(Enum... types) { |
| 189 | int i = 0; |
| 190 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 191 | for (Event event : testListener.events) { |
| 192 | assertEquals("Incorrect event received", types[i], event.type()); |
| 193 | i++; |
| 194 | } |
| 195 | testListener.events.clear(); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Checks if updating a null config fails with proper exception. |
| 200 | */ |
| 201 | @Test(expected = NullPointerException.class) |
| 202 | public void testUpdateNullConfig() { |
| 203 | target.updateApiConfig(null); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Checks if updating not existing config fails with proper exception. |
| 208 | */ |
| 209 | @Test(expected = IllegalArgumentException.class) |
| 210 | public void testUpdateNotExistingConfig() { |
| 211 | target.updateApiConfig(apiConfig1); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Checks if getting all nodes method returns correct set of nodes. |
| 216 | */ |
| 217 | @Test |
| 218 | public void testGetAllNodes() { |
| 219 | assertEquals(ERR_SIZE, 2, target.apiConfigs().size()); |
| 220 | assertTrue(ERR_NOT_FOUND, target.apiConfigs().contains(apiConfig2)); |
| 221 | assertTrue(ERR_NOT_FOUND, target.apiConfigs().contains(apiConfig3)); |
| 222 | } |
| 223 | |
| 224 | private static class TestCoreService extends CoreServiceAdapter { |
| 225 | @Override |
| 226 | public ApplicationId registerApplication(String name) { |
| 227 | return TEST_APP_ID; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | private class TestClusterService extends ClusterServiceAdapter { |
| 232 | |
| 233 | } |
| 234 | |
| 235 | private static class TestLeadershipService extends LeadershipServiceAdapter { |
| 236 | |
| 237 | } |
| 238 | } |