blob: 3ca7d03f8699d431327426b86661f75989ddb4a4 [file] [log] [blame]
Jian Liac31f652021-01-17 02:18:30 +09001/*
2 * Copyright 2021-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.kubevirtnetworking.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 io.fabric8.kubernetes.api.model.PodStatus;
23import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26import org.onlab.junit.TestUtils;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreServiceAdapter;
29import org.onosproject.core.DefaultApplicationId;
30import org.onosproject.event.Event;
31import org.onosproject.kubevirtnetworking.api.KubevirtPodEvent;
32import org.onosproject.kubevirtnetworking.api.KubevirtPodListener;
33import org.onosproject.store.service.TestStorageService;
34
35import java.util.List;
36
37import static org.junit.Assert.assertEquals;
38import static org.junit.Assert.assertNotNull;
39import static org.junit.Assert.assertNull;
40import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_CREATED;
41import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_REMOVED;
42import static org.onosproject.kubevirtnetworking.api.KubevirtPodEvent.Type.KUBEVIRT_POD_UPDATED;
43
44/**
45 * Unit tests for kubevirt pod manager.
46 */
47public class KubevirtPodManagerTest {
48
49 private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
50
51 private static final String UNKNOWN_UID = "unknown_uid";
52 private static final String UPDATED_UID = "updated_uid";
53 private static final String UPDATED_NAME = "updated_name";
54
55 private static final String POD_UID = "pod_uid";
56 private static final String POD_NAME = "pod_name";
57
58 private static final Pod POD = createKubevirtPod(POD_UID, POD_NAME);
59 private static final Pod POD_UPDATED = createKubevirtPod(POD_UID, UPDATED_NAME);
60
61 private final TestKubevirtPodListener testListener = new TestKubevirtPodListener();
62
63 private KubevirtPodManager target;
64 private DistributedKubevirtPodStore kubevirtPodStore;
65
66 @Before
67 public void setUp() throws Exception {
68 kubevirtPodStore = new DistributedKubevirtPodStore();
69 TestUtils.setField(kubevirtPodStore, "coreService", new TestCoreService());
70 TestUtils.setField(kubevirtPodStore, "storageService", new TestStorageService());
71 TestUtils.setField(kubevirtPodStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
72 kubevirtPodStore.activate();
73
74 target = new KubevirtPodManager();
75 TestUtils.setField(target, "coreService", new TestCoreService());
76 target.kubevirtPodStore = kubevirtPodStore;
77 target.addListener(testListener);
78 target.activate();
79 }
80
81 @After
82 public void tearDown() {
83 target.removeListener(testListener);
84 kubevirtPodStore.deactivate();
85 target.deactivate();
86 kubevirtPodStore = null;
87 target = null;
88 }
89
90 /**
91 * Tests if getting all pods return correct set of pods.
92 */
93 @Test
94 public void testGetPods() {
95 createBasicPods();
96 assertEquals("Number of pods did not match", 1, target.pods().size());
97 }
98
99 /**
100 * Tests if getting a pod with UID returns the correct pod.
101 */
102 @Test
103 public void testGetPodByUid() {
104 createBasicPods();
105 assertNotNull("Pod did not match", target.pod(POD_UID));
106 assertNull("Pod did not match", target.pod(UNKNOWN_UID));
107 }
108
109 /**
110 * Tests creating and removing a pod, and checks if it triggers proper events.
111 */
112 @Test
113 public void testCreateAndRemovePod() {
114 target.createPod(POD);
115 assertEquals("Number of pods did not match", 1, target.pods().size());
116 assertNotNull("Pod was not created", target.pod(POD_UID));
117
118 target.removePod(POD_UID);
119 assertEquals("Number of pods did not match", 0, target.pods().size());
120 assertNull("Pod was not removed", target.pod(POD_UID));
121
122 validateEvents(KUBEVIRT_POD_CREATED, KUBEVIRT_POD_REMOVED);
123 }
124
125 /**
126 * Tests updating a pod, and checks if it triggers proper events.
127 */
128 @Test
129 public void testCreateAndUpdatePod() {
130 target.createPod(POD);
131 assertEquals("Number of pods did not match", 1, target.pods().size());
132 assertEquals("Pod did not match", POD_NAME,
133 target.pod(POD_UID).getMetadata().getName());
134
135 target.updatePod(POD_UPDATED);
136
137 assertEquals("Number of pods did not match", 1, target.pods().size());
138 assertEquals("Pod did not match", UPDATED_NAME,
139 target.pod(POD_UID).getMetadata().getName());
140 validateEvents(KUBEVIRT_POD_CREATED, KUBEVIRT_POD_UPDATED);
141 }
142
143 /**
144 * Tests if creating a null pod fails with an exception.
145 */
146 @Test(expected = NullPointerException.class)
147 public void testCreateNullPod() {
148 target.createPod(null);
149 }
150
151 /**
152 * Tests if creating a duplicate pod fails with an exception.
153 */
154 @Test(expected = IllegalArgumentException.class)
155 public void testCreateDuplicatePod() {
156 target.createPod(POD);
157 target.createPod(POD);
158 }
159
160 /**
161 * Tests if removing pod with null ID fails with an exception.
162 */
163 @Test(expected = IllegalArgumentException.class)
164 public void testRemovePodWithNull() {
165 target.removePod(null);
166 }
167
168 /**
169 * Tests if updating an unregistered pod fails with an exception.
170 */
171 @Test(expected = IllegalArgumentException.class)
172 public void testUpdateUnregisteredPod() {
173 target.updatePod(POD);
174 }
175
176 private void createBasicPods() {
177 target.createPod(POD);
178 }
179
180 private static Pod createKubevirtPod(String uid, String name) {
181 ObjectMeta meta = new ObjectMeta();
182 meta.setUid(uid);
183 meta.setName(name);
184
185 PodStatus status = new PodStatus();
186 status.setPhase("Running");
187
188 Pod pod = new Pod();
189 pod.setApiVersion("v1");
190 pod.setKind("pod");
191 pod.setMetadata(meta);
192 pod.setStatus(status);
193
194 return pod;
195 }
196
197 private static class TestCoreService extends CoreServiceAdapter {
198
199 @Override
200 public ApplicationId registerApplication(String name) {
201 return TEST_APP_ID;
202 }
203 }
204
205 private static class TestKubevirtPodListener implements KubevirtPodListener {
206 private List<KubevirtPodEvent> events = Lists.newArrayList();
207
208 @Override
209 public void event(KubevirtPodEvent event) {
210 events.add(event);
211 }
212 }
213
214 private void validateEvents(Enum... types) {
215 int i = 0;
216 assertEquals("Number of events did not match", types.length, testListener.events.size());
217 for (Event event : testListener.events) {
218 assertEquals("Incorrect event received", types[i], event.type());
219 i++;
220 }
221 testListener.events.clear();
222 }
223}