Jian Li | 324d6dc | 2019-07-10 10:55:15 +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.Namespace; |
| 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.K8sNamespaceEvent; |
| 31 | import org.onosproject.k8snetworking.api.K8sNamespaceListener; |
| 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.K8sNamespaceEvent.Type.K8S_NAMESPACE_CREATED; |
| 40 | import static org.onosproject.k8snetworking.api.K8sNamespaceEvent.Type.K8S_NAMESPACE_REMOVED; |
| 41 | import static org.onosproject.k8snetworking.api.K8sNamespaceEvent.Type.K8S_NAMESPACE_UPDATED; |
| 42 | |
| 43 | /** |
| 44 | * Unit tests for kubernetes network policy manager. |
| 45 | */ |
| 46 | public class K8sNamespaceManagerTest { |
| 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 NAMESPACE_UID = "namespace_uid"; |
| 55 | private static final String NAMESPACE_NAME = "namespace_name"; |
| 56 | |
| 57 | private static final Namespace NAMESPACE = |
| 58 | createK8sNamespace(NAMESPACE_UID, NAMESPACE_NAME); |
| 59 | private static final Namespace NAMESPACE_UPDATED = |
| 60 | createK8sNamespace(NAMESPACE_UID, UPDATED_NAME); |
| 61 | |
| 62 | private final TestK8sNamespaceListener testListener = new TestK8sNamespaceListener(); |
| 63 | |
| 64 | private K8sNamespaceManager target; |
| 65 | private DistributedK8sNamespaceStore k8sNamespaceStore; |
| 66 | |
| 67 | @Before |
| 68 | public void setUp() throws Exception { |
| 69 | k8sNamespaceStore = new DistributedK8sNamespaceStore(); |
| 70 | TestUtils.setField(k8sNamespaceStore, "coreService", new TestCoreService()); |
| 71 | TestUtils.setField(k8sNamespaceStore, "storageService", new TestStorageService()); |
| 72 | TestUtils.setField(k8sNamespaceStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
| 73 | k8sNamespaceStore.activate(); |
| 74 | |
| 75 | target = new K8sNamespaceManager(); |
| 76 | TestUtils.setField(target, "coreService", new TestCoreService()); |
| 77 | target.k8sNamespaceStore = k8sNamespaceStore; |
| 78 | target.addListener(testListener); |
| 79 | target.activate(); |
| 80 | } |
| 81 | |
| 82 | @After |
| 83 | public void tearDown() { |
| 84 | target.removeListener(testListener); |
| 85 | k8sNamespaceStore.deactivate(); |
| 86 | target.deactivate(); |
| 87 | k8sNamespaceStore = null; |
| 88 | target = null; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Tests if getting all namespaces return correct set of namespaces. |
| 93 | */ |
| 94 | @Test |
| 95 | public void testGetNamespaces() { |
| 96 | createBasicNamespaces(); |
| 97 | assertEquals("Number of namespaces did not match", 1, target.namespaces().size()); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Tests if getting a namespace with UID returns the correct namespace. |
| 102 | */ |
| 103 | @Test |
| 104 | public void testGetNamespaceByUid() { |
| 105 | createBasicNamespaces(); |
| 106 | assertNotNull("Namespace did not match", target.namespace(NAMESPACE_UID)); |
| 107 | assertNull("Namespace did not match", target.namespace(UNKNOWN_UID)); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Tests creating and removing a namespace, and checks if it triggers proper events. |
| 112 | */ |
| 113 | @Test |
| 114 | public void testCreateAndRemoveNetworkPolicy() { |
| 115 | target.createNamespace(NAMESPACE); |
| 116 | assertEquals("Number of namespaces did not match", 1, target.namespaces().size()); |
| 117 | assertNotNull("Namespace was not created", target.namespace(NAMESPACE_UID)); |
| 118 | |
| 119 | target.removeNamespace(NAMESPACE_UID); |
| 120 | assertEquals("Number of namespaces did not match", 0, target.namespaces().size()); |
| 121 | assertNull("Namespace was not removed", target.namespace(NAMESPACE_UID)); |
| 122 | validateEvents(K8S_NAMESPACE_CREATED, K8S_NAMESPACE_REMOVED); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Tests updating a namespace, and checks if it triggers proper events. |
| 127 | */ |
| 128 | @Test |
| 129 | public void testCreateAndUpdateNamespace() { |
| 130 | target.createNamespace(NAMESPACE); |
| 131 | assertEquals("Number of namespaces did not match", 1, target.namespaces().size()); |
| 132 | assertEquals("Namespace did not match", NAMESPACE_NAME, |
| 133 | target.namespace(NAMESPACE_UID).getMetadata().getName()); |
| 134 | |
| 135 | target.updateNamespace(NAMESPACE_UPDATED); |
| 136 | assertEquals("Number of namespaces did not match", 1, target.namespaces().size()); |
| 137 | assertEquals("Namespace did not match", UPDATED_NAME, |
| 138 | target.namespace(NAMESPACE_UID).getMetadata().getName()); |
| 139 | validateEvents(K8S_NAMESPACE_CREATED, K8S_NAMESPACE_UPDATED); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Tests if creating a null namespace fails with an exception. |
| 144 | */ |
| 145 | @Test(expected = NullPointerException.class) |
| 146 | public void testCreateNullNamespace() { |
| 147 | target.createNamespace(null); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Tests if creating a duplicate namespaces fails with an exception. |
| 152 | */ |
| 153 | @Test(expected = IllegalArgumentException.class) |
| 154 | public void testCreateDuplicatedNamespace() { |
| 155 | target.createNamespace(NAMESPACE); |
| 156 | target.createNamespace(NAMESPACE); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Tests if removing namespace with null ID fails with an exception. |
| 161 | */ |
| 162 | @Test(expected = IllegalArgumentException.class) |
| 163 | public void testRemoveNamespaceWithNull() { |
| 164 | target.removeNamespace(null); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Tests if updating an unregistered namespace fails with an exception. |
| 169 | */ |
| 170 | @Test(expected = IllegalArgumentException.class) |
| 171 | public void testUpdateUnregisteredNamespace() { |
| 172 | target.updateNamespace(NAMESPACE); |
| 173 | } |
| 174 | |
| 175 | private void createBasicNamespaces() { |
| 176 | target.createNamespace(NAMESPACE); |
| 177 | } |
| 178 | |
| 179 | private static Namespace createK8sNamespace(String uid, String name) { |
| 180 | ObjectMeta meta = new ObjectMeta(); |
| 181 | meta.setUid(uid); |
| 182 | meta.setName(name); |
| 183 | |
| 184 | Namespace namespace = new Namespace(); |
| 185 | namespace.setApiVersion("v1"); |
| 186 | namespace.setKind("Namespace"); |
| 187 | namespace.setMetadata(meta); |
| 188 | |
| 189 | return namespace; |
| 190 | } |
| 191 | |
| 192 | private static class TestCoreService extends CoreServiceAdapter { |
| 193 | |
| 194 | @Override |
| 195 | public ApplicationId registerApplication(String name) { |
| 196 | return TEST_APP_ID; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | private static class TestK8sNamespaceListener implements K8sNamespaceListener { |
| 201 | private List<K8sNamespaceEvent> events = Lists.newArrayList(); |
| 202 | |
| 203 | @Override |
| 204 | public void event(K8sNamespaceEvent event) { |
| 205 | events.add(event); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | private void validateEvents(Enum... types) { |
| 210 | int i = 0; |
| 211 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 212 | for (Event event : testListener.events) { |
| 213 | assertEquals("Incorrect event received", types[i], event.type()); |
| 214 | i++; |
| 215 | } |
| 216 | testListener.events.clear(); |
| 217 | } |
| 218 | } |