Jian Li | 3e1b887 | 2019-02-19 21:45:04 +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.k8snetworking.impl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.util.concurrent.MoreExecutors; |
| 20 | import io.fabric8.kubernetes.api.model.Endpoints; |
| 21 | import io.fabric8.kubernetes.api.model.ObjectMeta; |
| 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.junit.TestUtils; |
| 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.k8snetworking.api.K8sEndpointsEvent; |
| 31 | import org.onosproject.k8snetworking.api.K8sEndpointsListener; |
| 32 | import org.onosproject.store.service.TestStorageService; |
| 33 | |
| 34 | import java.util.List; |
| 35 | |
| 36 | import static org.junit.Assert.assertEquals; |
| 37 | import static org.junit.Assert.assertNotNull; |
| 38 | import static org.junit.Assert.assertNull; |
| 39 | import static org.onosproject.k8snetworking.api.K8sEndpointsEvent.Type.K8S_ENDPOINTS_CREATED; |
| 40 | import static org.onosproject.k8snetworking.api.K8sEndpointsEvent.Type.K8S_ENDPOINTS_REMOVED; |
| 41 | import static org.onosproject.k8snetworking.api.K8sEndpointsEvent.Type.K8S_ENDPOINTS_UPDATED; |
| 42 | |
| 43 | /** |
| 44 | * Unit tests for kubernetes endpoints manager. |
| 45 | */ |
| 46 | public class K8sEndpointsManagerTest { |
| 47 | |
| 48 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 49 | |
| 50 | private static final String UNKNOWN_UID = "unknown_uid"; |
| 51 | private static final String UPDATED_UID = "updated_uid"; |
| 52 | private static final String UPDATED_NAME = "updated_name"; |
| 53 | |
| 54 | private static final String ENDPOINTS_UID = "endpoints_uid"; |
| 55 | private static final String ENDPOINTS_NAME = "endpoints_name"; |
| 56 | |
| 57 | private static final Endpoints ENDPOINTS = createK8sEndpoints(ENDPOINTS_UID, ENDPOINTS_NAME); |
| 58 | private static final Endpoints ENDPOINTS_UPDATED = |
| 59 | createK8sEndpoints(ENDPOINTS_UID, UPDATED_NAME); |
| 60 | |
| 61 | private final TestK8sEndpointsListener testListener = new TestK8sEndpointsListener(); |
| 62 | |
| 63 | private K8sEndpointsManager target; |
| 64 | private DistributedK8sEndpointsStore k8sEndpointsStore; |
| 65 | |
| 66 | @Before |
| 67 | public void setUp() throws Exception { |
| 68 | k8sEndpointsStore = new DistributedK8sEndpointsStore(); |
| 69 | TestUtils.setField(k8sEndpointsStore, "coreService", new TestCoreService()); |
| 70 | TestUtils.setField(k8sEndpointsStore, "storageService", new TestStorageService()); |
| 71 | TestUtils.setField(k8sEndpointsStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
| 72 | k8sEndpointsStore.activate(); |
| 73 | |
| 74 | target = new K8sEndpointsManager(); |
| 75 | TestUtils.setField(target, "coreService", new TestCoreService()); |
| 76 | target.k8sEndpointsStore = k8sEndpointsStore; |
| 77 | target.addListener(testListener); |
| 78 | target.activate(); |
| 79 | } |
| 80 | |
| 81 | @After |
| 82 | public void tearDown() { |
| 83 | target.removeListener(testListener); |
| 84 | k8sEndpointsStore.deactivate(); |
| 85 | target.deactivate(); |
| 86 | k8sEndpointsStore = null; |
| 87 | target = null; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Tests if getting all endpoints return correct set of endpoints. |
| 92 | */ |
| 93 | @Test |
| 94 | public void testGetEndpoints() { |
| 95 | createBasicEndpoints(); |
| 96 | assertEquals("Number of endpoints did not match", 1, target.endpointses().size()); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests if getting a endpoints with UID returns the correct endpoints. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testGetEndpointsByUid() { |
| 104 | createBasicEndpoints(); |
| 105 | assertNotNull("Endpoints did not match", target.endpoints(ENDPOINTS_UID)); |
| 106 | assertNull("Endpoints did not match", target.endpoints(UNKNOWN_UID)); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Tests creating and removing a endpoints, and checks if it triggers proper events. |
| 111 | */ |
| 112 | @Test |
| 113 | public void testCreateAndRemoveEndpoints() { |
| 114 | target.createEndpoints(ENDPOINTS); |
| 115 | assertEquals("Number of endpoints did not match", 1, target.endpointses().size()); |
| 116 | assertNotNull("Endpoint was not created", target.endpoints(ENDPOINTS_UID)); |
| 117 | |
| 118 | target.removeEndpoints(ENDPOINTS_UID); |
| 119 | assertEquals("Number of endpoints did not match", 0, target.endpointses().size()); |
| 120 | assertNull("Endpoint was not removed", target.endpoints(ENDPOINTS_UID)); |
| 121 | |
| 122 | validateEvents(K8S_ENDPOINTS_CREATED, K8S_ENDPOINTS_REMOVED); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Tests updating a endpoints, and checks if it triggers proper events. |
| 127 | */ |
| 128 | @Test |
| 129 | public void testCreateAndUpdateEndpoints() { |
| 130 | target.createEndpoints(ENDPOINTS); |
| 131 | assertEquals("Number of endpoints did not match", 1, target.endpointses().size()); |
| 132 | assertEquals("Endpoint did not match", ENDPOINTS_NAME, |
| 133 | target.endpoints(ENDPOINTS_UID).getMetadata().getName()); |
| 134 | |
| 135 | target.updateEndpoints(ENDPOINTS_UPDATED); |
| 136 | |
| 137 | assertEquals("Number of endpoints did not match", 1, target.endpointses().size()); |
| 138 | assertEquals("Endpoints did not match", UPDATED_NAME, |
| 139 | target.endpoints(ENDPOINTS_UID).getMetadata().getName()); |
| 140 | validateEvents(K8S_ENDPOINTS_CREATED, K8S_ENDPOINTS_UPDATED); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Tests if creating a null endpoints fails with an exception. |
| 145 | */ |
| 146 | @Test(expected = NullPointerException.class) |
| 147 | public void testCreateNullEndpoints() { |
| 148 | target.createEndpoints(null); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Tests if creating a duplicate endpoints fails with an exception. |
| 153 | */ |
| 154 | @Test(expected = IllegalArgumentException.class) |
| 155 | public void testCreateDuplicateEndpoints() { |
| 156 | target.createEndpoints(ENDPOINTS); |
| 157 | target.createEndpoints(ENDPOINTS); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Tests if removing endpoints with null ID fails with an exception. |
| 162 | */ |
| 163 | @Test(expected = IllegalArgumentException.class) |
| 164 | public void testRemoveEndpointsWithNull() { |
| 165 | target.removeEndpoints(null); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Tests if updating an unregistered endpoints fails with an exception. |
| 170 | */ |
| 171 | @Test(expected = IllegalArgumentException.class) |
| 172 | public void testUpdateUnregisteredEndpoints() { |
| 173 | target.updateEndpoints(ENDPOINTS); |
| 174 | } |
| 175 | |
| 176 | private void createBasicEndpoints() { |
| 177 | target.createEndpoints(ENDPOINTS); |
| 178 | } |
| 179 | |
| 180 | private static Endpoints createK8sEndpoints(String uid, String name) { |
| 181 | ObjectMeta meta = new ObjectMeta(); |
| 182 | meta.setUid(uid); |
| 183 | meta.setName(name); |
| 184 | |
| 185 | Endpoints endpoints = new Endpoints(); |
| 186 | endpoints.setApiVersion("v1"); |
| 187 | endpoints.setKind("endpoints"); |
| 188 | endpoints.setMetadata(meta); |
| 189 | |
| 190 | return endpoints; |
| 191 | } |
| 192 | |
| 193 | private static class TestCoreService extends CoreServiceAdapter { |
| 194 | |
| 195 | @Override |
| 196 | public ApplicationId registerApplication(String name) { |
| 197 | return TEST_APP_ID; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | private static class TestK8sEndpointsListener implements K8sEndpointsListener { |
| 202 | private List<K8sEndpointsEvent> events = Lists.newArrayList(); |
| 203 | |
| 204 | @Override |
| 205 | public void event(K8sEndpointsEvent event) { |
| 206 | events.add(event); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | private void validateEvents(Enum... types) { |
| 211 | int i = 0; |
| 212 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 213 | for (Event event : testListener.events) { |
| 214 | assertEquals("Incorrect event received", types[i], event.type()); |
| 215 | i++; |
| 216 | } |
| 217 | testListener.events.clear(); |
| 218 | } |
| 219 | } |