blob: b3854ba360b3a6a8c2e4fe503b9694be322eb136 [file] [log] [blame]
Jian Lif4523d82019-07-07 01:06:09 +09001/*
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 */
16package org.onosproject.k8snetworking.impl;
17
18import com.google.common.collect.Lists;
19import com.google.common.util.concurrent.MoreExecutors;
20import io.fabric8.kubernetes.api.model.ObjectMeta;
Jian Li00418d12021-01-15 14:53:22 +090021import io.fabric8.kubernetes.api.model.networking.v1.NetworkPolicy;
Jian Lif4523d82019-07-07 01:06:09 +090022import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.junit.TestUtils;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreServiceAdapter;
28import org.onosproject.core.DefaultApplicationId;
29import org.onosproject.event.Event;
30import org.onosproject.k8snetworking.api.K8sNetworkPolicyEvent;
31import org.onosproject.k8snetworking.api.K8sNetworkPolicyListener;
32import org.onosproject.store.service.TestStorageService;
33
34import java.util.List;
35
36import static org.junit.Assert.assertEquals;
37import static org.junit.Assert.assertNotNull;
38import static org.junit.Assert.assertNull;
39import static org.onosproject.k8snetworking.api.K8sNetworkPolicyEvent.Type.K8S_NETWORK_POLICY_CREATED;
40import static org.onosproject.k8snetworking.api.K8sNetworkPolicyEvent.Type.K8S_NETWORK_POLICY_REMOVED;
41import static org.onosproject.k8snetworking.api.K8sNetworkPolicyEvent.Type.K8S_NETWORK_POLICY_UPDATED;
42
43/**
44 * Unit tests for kubernetes network policy manager.
45 */
46public class K8sNetworkPolicyManagerTest {
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 NETWORK_POLICY_UID = "network_policy_uid";
55 private static final String NETWORK_POLICY_NAME = "network_policy_name";
56
57 private static final NetworkPolicy NETWORK_POLICY =
58 createK8sNetworkPolicy(NETWORK_POLICY_UID, NETWORK_POLICY_NAME);
59 private static final NetworkPolicy NETWORK_POLICY_UPDATED =
60 createK8sNetworkPolicy(NETWORK_POLICY_UID, UPDATED_NAME);
61
62 private final TestK8sNetworkPolicyListener testListener = new TestK8sNetworkPolicyListener();
63
64 private K8sNetworkPolicyManager target;
65 private DistributedK8sNetworkPolicyStore k8sNetworkPolicyStore;
66
67 @Before
68 public void setUp() throws Exception {
69 k8sNetworkPolicyStore = new DistributedK8sNetworkPolicyStore();
70 TestUtils.setField(k8sNetworkPolicyStore, "coreService", new TestCoreService());
71 TestUtils.setField(k8sNetworkPolicyStore, "storageService", new TestStorageService());
72 TestUtils.setField(k8sNetworkPolicyStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
73 k8sNetworkPolicyStore.activate();
74
75 target = new K8sNetworkPolicyManager();
76 TestUtils.setField(target, "coreService", new TestCoreService());
77 target.k8sNetworkPolicyStore = k8sNetworkPolicyStore;
78 target.addListener(testListener);
79 target.activate();
80 }
81
82 @After
83 public void tearDown() {
84 target.removeListener(testListener);
85 k8sNetworkPolicyStore.deactivate();
86 target.deactivate();
87 k8sNetworkPolicyStore = null;
88 target = null;
89 }
90
91 /**
92 * Tests if getting all network policies return correct set of network policies.
93 */
94 @Test
95 public void testGetNetworkPolicies() {
96 createBasicNetworkPolicies();
97 assertEquals("Number of network policies did not match", 1, target.networkPolicies().size());
98 }
99
100 /**
101 * Tests if getting a network policy with UID returns the correct network policy.
102 */
103 @Test
104 public void testGetNetworkPolicyByUid() {
105 createBasicNetworkPolicies();
106 assertNotNull("Network policy did not match", target.networkPolicy(NETWORK_POLICY_UID));
107 assertNull("Network policy did not match", target.networkPolicy(UNKNOWN_UID));
108 }
109
110 /**
111 * Tests creating and removing a network policy, and checks if it triggers proper events.
112 */
113 @Test
114 public void testCreateAndRemoveNetworkPolicy() {
115 target.createNetworkPolicy(NETWORK_POLICY);
116 assertEquals("Number of network policies did not match", 1, target.networkPolicies().size());
117 assertNotNull("Network policy was not created", target.networkPolicy(NETWORK_POLICY_UID));
118
119 target.removeNetworkPolicy(NETWORK_POLICY_UID);
120 assertEquals("Number of network policies did not match", 0, target.networkPolicies().size());
121 assertNull("Network policy was not removed", target.networkPolicy(NETWORK_POLICY_UID));
122 validateEvents(K8S_NETWORK_POLICY_CREATED, K8S_NETWORK_POLICY_REMOVED);
123 }
124
125 /**
126 * Tests updating a network policy, and checks if it triggers proper events.
127 */
128 @Test
129 public void testCreateAndUpdateNetworkPolicy() {
130 target.createNetworkPolicy(NETWORK_POLICY);
131 assertEquals("Number of network policies did not match", 1, target.networkPolicies().size());
132 assertEquals("Network policy did not match", NETWORK_POLICY_NAME,
133 target.networkPolicy(NETWORK_POLICY_UID).getMetadata().getName());
134
135 target.updateNetworkPolicy(NETWORK_POLICY_UPDATED);
136 assertEquals("Number of network policies did not match", 1, target.networkPolicies().size());
137 assertEquals("Network policy did not match", UPDATED_NAME,
138 target.networkPolicy(NETWORK_POLICY_UID).getMetadata().getName());
139 validateEvents(K8S_NETWORK_POLICY_CREATED, K8S_NETWORK_POLICY_UPDATED);
140 }
141
142 /**
143 * Tests if creating a null network policy fails with an exception.
144 */
145 @Test(expected = NullPointerException.class)
146 public void testCreateNullNetworkPolicy() {
147 target.createNetworkPolicy(null);
148 }
149
150 /**
151 * Tests if creating a duplicate network policies fails with an exception.
152 */
153 @Test(expected = IllegalArgumentException.class)
154 public void testCreateDuplicatedNetworkPolicy() {
155 target.createNetworkPolicy(NETWORK_POLICY);
156 target.createNetworkPolicy(NETWORK_POLICY);
157 }
158
159 /**
160 * Tests if removing network policy with null ID fails with an exception.
161 */
162 @Test(expected = IllegalArgumentException.class)
163 public void testRemoveNetworkPolicyWithNull() {
164 target.removeNetworkPolicy(null);
165 }
166
167 /**
168 * Tests if updating an unregistered network policy fails with an exception.
169 */
170 @Test(expected = IllegalArgumentException.class)
171 public void testUpdateUnregisteredNetworkPolicy() {
172 target.updateNetworkPolicy(NETWORK_POLICY);
173 }
174
175 private void createBasicNetworkPolicies() {
176 target.createNetworkPolicy(NETWORK_POLICY);
177 }
178
179 private static NetworkPolicy createK8sNetworkPolicy(String uid, String name) {
180 ObjectMeta meta = new ObjectMeta();
181 meta.setUid(uid);
182 meta.setName(name);
183
184 NetworkPolicy networkPolicy = new NetworkPolicy();
185 networkPolicy.setApiVersion("v1");
186 networkPolicy.setKind("NetworkPolicy");
187 networkPolicy.setMetadata(meta);
188
189 return networkPolicy;
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 TestK8sNetworkPolicyListener implements K8sNetworkPolicyListener {
201 private List<K8sNetworkPolicyEvent> events = Lists.newArrayList();
202
203 @Override
204 public void event(K8sNetworkPolicyEvent 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}