/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/impl/K8sNamespaceManagerTest.java

https://github.com/opennetworkinglab/onos · Java · 218 lines · 144 code · 32 blank · 42 comment · 1 complexity · 514163de2870c41f0d5a48b85a3558e0 MD5 · raw file

  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. import com.google.common.collect.Lists;
  18. import com.google.common.util.concurrent.MoreExecutors;
  19. import io.fabric8.kubernetes.api.model.Namespace;
  20. import io.fabric8.kubernetes.api.model.ObjectMeta;
  21. import org.junit.After;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.onlab.junit.TestUtils;
  25. import org.onosproject.core.ApplicationId;
  26. import org.onosproject.core.CoreServiceAdapter;
  27. import org.onosproject.core.DefaultApplicationId;
  28. import org.onosproject.event.Event;
  29. import org.onosproject.k8snetworking.api.K8sNamespaceEvent;
  30. import org.onosproject.k8snetworking.api.K8sNamespaceListener;
  31. import org.onosproject.store.service.TestStorageService;
  32. import java.util.List;
  33. import static org.junit.Assert.assertEquals;
  34. import static org.junit.Assert.assertNotNull;
  35. import static org.junit.Assert.assertNull;
  36. import static org.onosproject.k8snetworking.api.K8sNamespaceEvent.Type.K8S_NAMESPACE_CREATED;
  37. import static org.onosproject.k8snetworking.api.K8sNamespaceEvent.Type.K8S_NAMESPACE_REMOVED;
  38. import static org.onosproject.k8snetworking.api.K8sNamespaceEvent.Type.K8S_NAMESPACE_UPDATED;
  39. /**
  40. * Unit tests for kubernetes network policy manager.
  41. */
  42. public class K8sNamespaceManagerTest {
  43. private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
  44. private static final String UNKNOWN_UID = "unknown_uid";
  45. private static final String UPDATED_UID = "updated_uid";
  46. private static final String UPDATED_NAME = "updated_name";
  47. private static final String NAMESPACE_UID = "namespace_uid";
  48. private static final String NAMESPACE_NAME = "namespace_name";
  49. private static final Namespace NAMESPACE =
  50. createK8sNamespace(NAMESPACE_UID, NAMESPACE_NAME);
  51. private static final Namespace NAMESPACE_UPDATED =
  52. createK8sNamespace(NAMESPACE_UID, UPDATED_NAME);
  53. private final TestK8sNamespaceListener testListener = new TestK8sNamespaceListener();
  54. private K8sNamespaceManager target;
  55. private DistributedK8sNamespaceStore k8sNamespaceStore;
  56. @Before
  57. public void setUp() throws Exception {
  58. k8sNamespaceStore = new DistributedK8sNamespaceStore();
  59. TestUtils.setField(k8sNamespaceStore, "coreService", new TestCoreService());
  60. TestUtils.setField(k8sNamespaceStore, "storageService", new TestStorageService());
  61. TestUtils.setField(k8sNamespaceStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
  62. k8sNamespaceStore.activate();
  63. target = new K8sNamespaceManager();
  64. TestUtils.setField(target, "coreService", new TestCoreService());
  65. target.k8sNamespaceStore = k8sNamespaceStore;
  66. target.addListener(testListener);
  67. target.activate();
  68. }
  69. @After
  70. public void tearDown() {
  71. target.removeListener(testListener);
  72. k8sNamespaceStore.deactivate();
  73. target.deactivate();
  74. k8sNamespaceStore = null;
  75. target = null;
  76. }
  77. /**
  78. * Tests if getting all namespaces return correct set of namespaces.
  79. */
  80. @Test
  81. public void testGetNamespaces() {
  82. createBasicNamespaces();
  83. assertEquals("Number of namespaces did not match", 1, target.namespaces().size());
  84. }
  85. /**
  86. * Tests if getting a namespace with UID returns the correct namespace.
  87. */
  88. @Test
  89. public void testGetNamespaceByUid() {
  90. createBasicNamespaces();
  91. assertNotNull("Namespace did not match", target.namespace(NAMESPACE_UID));
  92. assertNull("Namespace did not match", target.namespace(UNKNOWN_UID));
  93. }
  94. /**
  95. * Tests creating and removing a namespace, and checks if it triggers proper events.
  96. */
  97. @Test
  98. public void testCreateAndRemoveNetworkPolicy() {
  99. target.createNamespace(NAMESPACE);
  100. assertEquals("Number of namespaces did not match", 1, target.namespaces().size());
  101. assertNotNull("Namespace was not created", target.namespace(NAMESPACE_UID));
  102. target.removeNamespace(NAMESPACE_UID);
  103. assertEquals("Number of namespaces did not match", 0, target.namespaces().size());
  104. assertNull("Namespace was not removed", target.namespace(NAMESPACE_UID));
  105. validateEvents(K8S_NAMESPACE_CREATED, K8S_NAMESPACE_REMOVED);
  106. }
  107. /**
  108. * Tests updating a namespace, and checks if it triggers proper events.
  109. */
  110. @Test
  111. public void testCreateAndUpdateNamespace() {
  112. target.createNamespace(NAMESPACE);
  113. assertEquals("Number of namespaces did not match", 1, target.namespaces().size());
  114. assertEquals("Namespace did not match", NAMESPACE_NAME,
  115. target.namespace(NAMESPACE_UID).getMetadata().getName());
  116. target.updateNamespace(NAMESPACE_UPDATED);
  117. assertEquals("Number of namespaces did not match", 1, target.namespaces().size());
  118. assertEquals("Namespace did not match", UPDATED_NAME,
  119. target.namespace(NAMESPACE_UID).getMetadata().getName());
  120. validateEvents(K8S_NAMESPACE_CREATED, K8S_NAMESPACE_UPDATED);
  121. }
  122. /**
  123. * Tests if creating a null namespace fails with an exception.
  124. */
  125. @Test(expected = NullPointerException.class)
  126. public void testCreateNullNamespace() {
  127. target.createNamespace(null);
  128. }
  129. /**
  130. * Tests if creating a duplicate namespaces fails with an exception.
  131. */
  132. @Test(expected = IllegalArgumentException.class)
  133. public void testCreateDuplicatedNamespace() {
  134. target.createNamespace(NAMESPACE);
  135. target.createNamespace(NAMESPACE);
  136. }
  137. /**
  138. * Tests if removing namespace with null ID fails with an exception.
  139. */
  140. @Test(expected = IllegalArgumentException.class)
  141. public void testRemoveNamespaceWithNull() {
  142. target.removeNamespace(null);
  143. }
  144. /**
  145. * Tests if updating an unregistered namespace fails with an exception.
  146. */
  147. @Test(expected = IllegalArgumentException.class)
  148. public void testUpdateUnregisteredNamespace() {
  149. target.updateNamespace(NAMESPACE);
  150. }
  151. private void createBasicNamespaces() {
  152. target.createNamespace(NAMESPACE);
  153. }
  154. private static Namespace createK8sNamespace(String uid, String name) {
  155. ObjectMeta meta = new ObjectMeta();
  156. meta.setUid(uid);
  157. meta.setName(name);
  158. Namespace namespace = new Namespace();
  159. namespace.setApiVersion("v1");
  160. namespace.setKind("Namespace");
  161. namespace.setMetadata(meta);
  162. return namespace;
  163. }
  164. private static class TestCoreService extends CoreServiceAdapter {
  165. @Override
  166. public ApplicationId registerApplication(String name) {
  167. return TEST_APP_ID;
  168. }
  169. }
  170. private static class TestK8sNamespaceListener implements K8sNamespaceListener {
  171. private List<K8sNamespaceEvent> events = Lists.newArrayList();
  172. @Override
  173. public void event(K8sNamespaceEvent event) {
  174. events.add(event);
  175. }
  176. }
  177. private void validateEvents(Enum... types) {
  178. int i = 0;
  179. assertEquals("Number of events did not match", types.length, testListener.events.size());
  180. for (Event event : testListener.events) {
  181. assertEquals("Incorrect event received", types[i], event.type());
  182. i++;
  183. }
  184. testListener.events.clear();
  185. }
  186. }