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() |
| 75 | .scheme(K8sApiConfig.Scheme.HTTP) |
| 76 | .ipAddress(IpAddress.valueOf("10.10.10.2")) |
| 77 | .port(6443) |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 78 | .state(DISCONNECTED) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 79 | .build(); |
| 80 | apiConfig2 = DefaultK8sApiConfig.builder() |
| 81 | .scheme(K8sApiConfig.Scheme.HTTPS) |
| 82 | .ipAddress(IpAddress.valueOf("10.10.10.3")) |
| 83 | .port(6443) |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 84 | .state(DISCONNECTED) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 85 | .token("token") |
| 86 | .caCertData("caCertData") |
| 87 | .clientCertData("clientCertData") |
| 88 | .clientKeyData("clientKeyData") |
| 89 | .build(); |
| 90 | apiConfig3 = DefaultK8sApiConfig.builder() |
| 91 | .scheme(K8sApiConfig.Scheme.HTTP) |
| 92 | .ipAddress(IpAddress.valueOf("10.10.10.4")) |
| 93 | .port(8080) |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 94 | .state(DISCONNECTED) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 95 | .build(); |
| 96 | |
| 97 | configStore = new DistributedK8sApiConfigStore(); |
| 98 | TestUtils.setField(configStore, "coreService", new TestCoreService()); |
| 99 | TestUtils.setField(configStore, "storageService", new TestStorageService()); |
| 100 | TestUtils.setField(configStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
| 101 | configStore.activate(); |
| 102 | |
| 103 | configStore.createApiConfig(apiConfig2); |
| 104 | configStore.createApiConfig(apiConfig3); |
| 105 | |
| 106 | target = new K8sApiConfigManager(); |
| 107 | target.storageService = new TestStorageService(); |
| 108 | target.coreService = new TestCoreService(); |
| 109 | target.clusterService = new TestClusterService(); |
| 110 | target.leadershipService = new TestLeadershipService(); |
| 111 | target.configStore = configStore; |
| 112 | target.addListener(testListener); |
| 113 | target.activate(); |
| 114 | testListener.events.clear(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Clean up unit test. |
| 119 | */ |
| 120 | @After |
| 121 | public void tearDown() { |
| 122 | target.removeListener(testListener); |
| 123 | target.deactivate(); |
| 124 | configStore.deactivate(); |
| 125 | configStore = null; |
| 126 | target = null; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Checks if creating and removing a config work well with proper events. |
| 131 | */ |
| 132 | @Test |
| 133 | public void testCreateAndRemoveConfig() { |
| 134 | target.createApiConfig(apiConfig1); |
| 135 | assertEquals(ERR_SIZE, 3, target.apiConfigs().size()); |
| 136 | assertNotNull(target.apiConfig(endpoint(apiConfig1))); |
| 137 | |
| 138 | target.removeApiConfig(endpoint(apiConfig1)); |
| 139 | assertEquals(ERR_SIZE, 2, target.apiConfigs().size()); |
| 140 | assertNull(target.apiConfig(endpoint(apiConfig1))); |
| 141 | |
| 142 | validateEvents(K8S_API_CONFIG_CREATED, K8S_API_CONFIG_REMOVED); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Checks if creating null config fails with proper exception. |
| 147 | */ |
| 148 | @Test(expected = NullPointerException.class) |
| 149 | public void testCreateNullConfig() { |
| 150 | target.createApiConfig(null); |
| 151 | } |
| 152 | |
| 153 | private static class TestK8sApiConfigListener implements K8sApiConfigListener { |
| 154 | private List<K8sApiConfigEvent> events = Lists.newArrayList(); |
| 155 | |
| 156 | @Override |
| 157 | public void event(K8sApiConfigEvent event) { |
| 158 | events.add(event); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Checks if creating a duplicated config fails with proper exception. |
| 164 | */ |
| 165 | @Test(expected = IllegalArgumentException.class) |
| 166 | public void testCreateDuplicateConfig() { |
| 167 | target.createApiConfig(apiConfig1); |
| 168 | target.createApiConfig(apiConfig1); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Checks if removing null config fails with proper exception. |
| 173 | */ |
| 174 | @Test(expected = IllegalArgumentException.class) |
| 175 | public void testRemoveNullConfig() { |
| 176 | target.removeApiConfig(null); |
| 177 | } |
| 178 | |
| 179 | private void validateEvents(Enum... types) { |
| 180 | int i = 0; |
| 181 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 182 | for (Event event : testListener.events) { |
| 183 | assertEquals("Incorrect event received", types[i], event.type()); |
| 184 | i++; |
| 185 | } |
| 186 | testListener.events.clear(); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Checks if updating a null config fails with proper exception. |
| 191 | */ |
| 192 | @Test(expected = NullPointerException.class) |
| 193 | public void testUpdateNullConfig() { |
| 194 | target.updateApiConfig(null); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Checks if updating not existing config fails with proper exception. |
| 199 | */ |
| 200 | @Test(expected = IllegalArgumentException.class) |
| 201 | public void testUpdateNotExistingConfig() { |
| 202 | target.updateApiConfig(apiConfig1); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Checks if getting all nodes method returns correct set of nodes. |
| 207 | */ |
| 208 | @Test |
| 209 | public void testGetAllNodes() { |
| 210 | assertEquals(ERR_SIZE, 2, target.apiConfigs().size()); |
| 211 | assertTrue(ERR_NOT_FOUND, target.apiConfigs().contains(apiConfig2)); |
| 212 | assertTrue(ERR_NOT_FOUND, target.apiConfigs().contains(apiConfig3)); |
| 213 | } |
| 214 | |
| 215 | private static class TestCoreService extends CoreServiceAdapter { |
| 216 | @Override |
| 217 | public ApplicationId registerApplication(String name) { |
| 218 | return TEST_APP_ID; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | private class TestClusterService extends ClusterServiceAdapter { |
| 223 | |
| 224 | } |
| 225 | |
| 226 | private static class TestLeadershipService extends LeadershipServiceAdapter { |
| 227 | |
| 228 | } |
| 229 | } |