blob: 3894f9c617d78dc7d7d11c388e462255b6af46c2 [file] [log] [blame]
Jian Li747e1362019-02-19 22:59:46 +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;
21import io.fabric8.kubernetes.api.model.Pod;
22import 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.K8sPodEvent;
31import org.onosproject.k8snetworking.api.K8sPodListener;
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.K8sPodEvent.Type.K8S_POD_CREATED;
40import static org.onosproject.k8snetworking.api.K8sPodEvent.Type.K8S_POD_REMOVED;
41import static org.onosproject.k8snetworking.api.K8sPodEvent.Type.K8S_POD_UPDATED;
42
43/**
44 * Unit tests for kubernetes pod manager.
45 */
46public class K8sPodManagerTest {
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 POD_UID = "pod_uid";
55 private static final String POD_NAME = "pod_name";
56
57 private static final Pod POD = createK8sPod(POD_UID, POD_NAME);
58 private static final Pod POD_UPDATED = createK8sPod(POD_UID, UPDATED_NAME);
59
60 private final TestK8sPodListener testListener = new TestK8sPodListener();
61
62 private K8sPodManager target;
63 private DistributedK8sPodStore k8sPodStore;
64
65 @Before
66 public void setUp() throws Exception {
67 k8sPodStore = new DistributedK8sPodStore();
68 TestUtils.setField(k8sPodStore, "coreService", new TestCoreService());
69 TestUtils.setField(k8sPodStore, "storageService", new TestStorageService());
70 TestUtils.setField(k8sPodStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
71 k8sPodStore.activate();
72
73 target = new K8sPodManager();
74 TestUtils.setField(target, "coreService", new TestCoreService());
75 target.k8sPodStore = k8sPodStore;
76 target.addListener(testListener);
77 target.activate();
78 }
79
80 @After
81 public void tearDown() {
82 target.removeListener(testListener);
83 k8sPodStore.deactivate();
84 target.deactivate();
85 k8sPodStore = null;
86 target = null;
87 }
88
89 /**
90 * Tests if getting all pods return correct set of pods.
91 */
92 @Test
93 public void testGetPods() {
94 createBasicPods();
95 assertEquals("Number of pods did not match", 1, target.pods().size());
96 }
97
98 /**
99 * Tests if getting a pod with UID returns the correct pod.
100 */
101 @Test
102 public void testGetPodByUid() {
103 createBasicPods();
104 assertNotNull("Pod did not match", target.pod(POD_UID));
105 assertNull("Pod did not match", target.pod(UNKNOWN_UID));
106 }
107
108 /**
109 * Tests creating and removing a pod, and checks if it triggers proper events.
110 */
111 @Test
112 public void testCreateAndRemovePod() {
113 target.createPod(POD);
114 assertEquals("Number of pods did not match", 1, target.pods().size());
115 assertNotNull("Pod was not created", target.pod(POD_UID));
116
117 target.removePod(POD_UID);
118 assertEquals("Number of pods did not match", 0, target.pods().size());
119 assertNull("Pod was not removed", target.pod(POD_UID));
120
121 validateEvents(K8S_POD_CREATED, K8S_POD_REMOVED);
122 }
123
124 /**
125 * Tests updating a pod, and checks if it triggers proper events.
126 */
127 @Test
128 public void testCreateAndUpdatePod() {
129 target.createPod(POD);
130 assertEquals("Number of pods did not match", 1, target.pods().size());
131 assertEquals("Pod did not match", POD_NAME,
132 target.pod(POD_UID).getMetadata().getName());
133
134 target.updatePod(POD_UPDATED);
135
136 assertEquals("Number of pods did not match", 1, target.pods().size());
137 assertEquals("Pod did not match", UPDATED_NAME,
138 target.pod(POD_UID).getMetadata().getName());
139 validateEvents(K8S_POD_CREATED, K8S_POD_UPDATED);
140 }
141
142 /**
143 * Tests if creating a null pod fails with an exception.
144 */
145 @Test(expected = NullPointerException.class)
146 public void testCreateNullPod() {
147 target.createPod(null);
148 }
149
150 /**
151 * Tests if creating a duplicate pod fails with an exception.
152 */
153 @Test(expected = IllegalArgumentException.class)
154 public void testCreateDuplicatePod() {
155 target.createPod(POD);
156 target.createPod(POD);
157 }
158
159 /**
160 * Tests if removing pod with null ID fails with an exception.
161 */
162 @Test(expected = IllegalArgumentException.class)
163 public void testRemovePodWithNull() {
164 target.removePod(null);
165 }
166
167 /**
168 * Tests if updating an unregistered pod fails with an exception.
169 */
170 @Test(expected = IllegalArgumentException.class)
171 public void testUpdateUnregisteredPod() {
172 target.updatePod(POD);
173 }
174
175 private void createBasicPods() {
176 target.createPod(POD);
177 }
178
179 private static Pod createK8sPod(String uid, String name) {
180 ObjectMeta meta = new ObjectMeta();
181 meta.setUid(uid);
182 meta.setName(name);
183
184 Pod pod = new Pod();
185 pod.setApiVersion("v1");
186 pod.setKind("pod");
187 pod.setMetadata(meta);
188
189 return pod;
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 TestK8sPodListener implements K8sPodListener {
201 private List<K8sPodEvent> events = Lists.newArrayList();
202
203 @Override
204 public void event(K8sPodEvent 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}