blob: f401207ed8b0576febe50ede6d120533aedcf8db [file] [log] [blame]
Jian Li1cf51882019-02-26 14:41:20 +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.extensions.Ingress;
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.K8sIngressEvent;
31import org.onosproject.k8snetworking.api.K8sIngressListener;
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.K8sIngressEvent.Type.K8S_INGRESS_CREATED;
40import static org.onosproject.k8snetworking.api.K8sIngressEvent.Type.K8S_INGRESS_REMOVED;
41import static org.onosproject.k8snetworking.api.K8sIngressEvent.Type.K8S_INGRESS_UPDATED;
42
43/**
44 * Unit tests for kubernetes ingress manager.
45 */
46public class K8sIngressManagerTest {
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 INGRESS_UID = "ingress_uid";
55 private static final String INGRESS_NAME = "ingress_name";
56
57 private static final Ingress INGRESS = createK8sIngress(INGRESS_UID, INGRESS_NAME);
58 private static final Ingress INGRESS_UPDATED = createK8sIngress(INGRESS_UID, UPDATED_NAME);
59
60 private final TestK8sIngressListener testListener = new TestK8sIngressListener();
61
62 private K8sIngressManager target;
63 private DistributedK8sIngressStore k8sIngressStore;
64
65 @Before
66 public void setUp() throws Exception {
67 k8sIngressStore = new DistributedK8sIngressStore();
68 TestUtils.setField(k8sIngressStore, "coreService", new TestCoreService());
69 TestUtils.setField(k8sIngressStore, "storageService", new TestStorageService());
70 TestUtils.setField(k8sIngressStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
71 k8sIngressStore.activate();
72
73 target = new K8sIngressManager();
74 TestUtils.setField(target, "coreService", new TestCoreService());
75 target.k8sIngressStore = k8sIngressStore;
76 target.addListener(testListener);
77 target.activate();
78 }
79
80 @After
81 public void tearDown() {
82 target.removeListener(testListener);
83 k8sIngressStore.deactivate();
84 target.deactivate();
85 k8sIngressStore = null;
86 target = null;
87 }
88
89 /**
90 * Tests if getting all ingresses return correct set of ingresses.
91 */
92 @Test
93 public void testGetIngresses() {
94 createBasicIngresses();
95 assertEquals("Number of ingresses did not match", 1, target.ingresses().size());
96 }
97
98 /**
99 * Tests if getting an ingress with UID returns the correct ingress.
100 */
101 @Test
102 public void testGetIngressByUid() {
103 createBasicIngresses();
104 assertNotNull("Ingress did not match", target.ingress(INGRESS_UID));
105 assertNull("Ingress did not match", target.ingress(UNKNOWN_UID));
106 }
107
108 /**
109 * Tests creating and removing a ingress, and checks if it triggers proper events.
110 */
111 @Test
112 public void testCreateAndRemoveIngress() {
113 target.createIngress(INGRESS);
114 assertEquals("Number of ingresses did not match", 1, target.ingresses().size());
115 assertNotNull("Ingress was not created", target.ingress(INGRESS_UID));
116
117 target.removeIngress(INGRESS_UID);
118 assertEquals("Number of ingresses did not match", 0, target.ingresses().size());
119 assertNull("Ingress was not removed", target.ingress(INGRESS_UID));
120
121 validateEvents(K8S_INGRESS_CREATED, K8S_INGRESS_REMOVED);
122 }
123
124 /**
125 * Tests updating a ingress, and checks if it triggers proper events.
126 */
127 @Test
128 public void testCreateAndUpdateIngress() {
129 target.createIngress(INGRESS);
130 assertEquals("Number of ingresses did not match", 1, target.ingresses().size());
131 assertEquals("Ingress did not match", INGRESS_NAME,
132 target.ingress(INGRESS_UID).getMetadata().getName());
133
134 target.updateIngress(INGRESS_UPDATED);
135
136 assertEquals("Number of ingresses did not match", 1, target.ingresses().size());
137 assertEquals("Ingress did not match", UPDATED_NAME,
138 target.ingress(INGRESS_UID).getMetadata().getName());
139 validateEvents(K8S_INGRESS_CREATED, K8S_INGRESS_UPDATED);
140 }
141
142 /**
143 * Tests if creating a null ingress fails with an exception.
144 */
145 @Test(expected = NullPointerException.class)
146 public void testCreateNullIngress() {
147 target.createIngress(null);
148 }
149
150 /**
151 * Tests if creating a duplicate ingress fails with an exception.
152 */
153 @Test(expected = IllegalArgumentException.class)
154 public void testCreateDuplicateIngress() {
155 target.createIngress(INGRESS);
156 target.createIngress(INGRESS);
157 }
158
159 /**
160 * Tests if removing ingress with null ID fails with an exception.
161 */
162 @Test(expected = IllegalArgumentException.class)
163 public void testRemoveIngressWithNull() {
164 target.removeIngress(null);
165 }
166
167 /**
168 * Tests if updating an unregistered ingress fails with an exception.
169 */
170 @Test(expected = IllegalArgumentException.class)
171 public void testUpdateUnregisteredIngress() {
172 target.updateIngress(INGRESS);
173 }
174
175 private void createBasicIngresses() {
176 target.createIngress(INGRESS);
177 }
178
179 private static Ingress createK8sIngress(String uid, String name) {
180 ObjectMeta meta = new ObjectMeta();
181 meta.setUid(uid);
182 meta.setName(name);
183
184 Ingress ingress = new Ingress();
185 ingress.setApiVersion("v1");
186 ingress.setKind("Ingress");
187 ingress.setMetadata(meta);
188
189 return ingress;
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 TestK8sIngressListener implements K8sIngressListener {
201 private List<K8sIngressEvent> events = Lists.newArrayList();
202
203 @Override
204 public void event(K8sIngressEvent 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}