blob: 33746721ae8939813621e14e2c238242381bd168 [file] [log] [blame]
Jian Lie2a04ce2020-07-01 19:07:02 +09001/*
2 * Copyright 2020-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.k8snode.impl;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Lists;
20import com.google.common.util.concurrent.MoreExecutors;
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.junit.TestUtils;
25import org.onlab.packet.IpAddress;
26import org.onosproject.cluster.ClusterServiceAdapter;
27import org.onosproject.cluster.LeadershipServiceAdapter;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreServiceAdapter;
30import org.onosproject.core.DefaultApplicationId;
31import org.onosproject.event.Event;
32import org.onosproject.k8snode.api.DefaultK8sHost;
33import org.onosproject.k8snode.api.K8sHost;
34import org.onosproject.k8snode.api.K8sHostEvent;
35import org.onosproject.k8snode.api.K8sHostListener;
36import org.onosproject.k8snode.api.K8sHostState;
37import org.onosproject.store.service.TestStorageService;
38
39import java.util.List;
40import java.util.Set;
41
42import static org.junit.Assert.assertEquals;
43import static org.junit.Assert.assertNotNull;
44import static org.junit.Assert.assertNull;
45import static org.junit.Assert.assertTrue;
46import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_HOST_COMPLETE;
47import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_HOST_CREATED;
48import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_HOST_REMOVED;
49import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_HOST_UPDATED;
50import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_NODES_ADDED;
51import static org.onosproject.k8snode.api.K8sHostEvent.Type.K8S_NODES_REMOVED;
52import static org.onosproject.k8snode.api.K8sHostState.COMPLETE;
53import static org.onosproject.k8snode.api.K8sHostState.INIT;
54
55/**
56 * Unit tests for kubernetes host manager.
57 */
58public class K8sHostManagerTest {
59
60 private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
61
62 private static final String ERR_SIZE = "Number of hosts did not match";
63 private static final String ERR_NOT_MATCH = "Host did not match";
64 private static final String ERR_NOT_FOUND = "Host did not exist";
65
66 private static final IpAddress HOST_IP_1 = IpAddress.valueOf("192.168.100.2");
67 private static final IpAddress HOST_IP_2 = IpAddress.valueOf("192.168.101.2");
68 private static final IpAddress HOST_IP_3 = IpAddress.valueOf("192.168.102.2");
69
70 private static final K8sHost HOST_1 = createHost(
71 HOST_IP_1,
72 INIT,
73 ImmutableSet.of("1", "2")
74 );
75
76 private static final K8sHost HOST_2 = createHost(
77 HOST_IP_2,
78 INIT,
79 ImmutableSet.of("3", "4")
80 );
81
82 private static final K8sHost HOST_3 = createHost(
83 HOST_IP_3,
84 COMPLETE,
85 ImmutableSet.of("5", "6")
86 );
87
88 private final TestK8sHostListener testListener = new TestK8sHostListener();
89
90 private K8sHostManager target;
91 private DistributedK8sHostStore hostStore;
92
93 /**
94 * Initial setup for this unit test.
95 */
96 @Before
97 public void setUp() {
98 hostStore = new DistributedK8sHostStore();
99 TestUtils.setField(hostStore, "coreService", new TestCoreService());
100 TestUtils.setField(hostStore, "storageService", new TestStorageService());
101 TestUtils.setField(hostStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
102 hostStore.activate();
103
104 hostStore.createHost(HOST_2);
105 hostStore.createHost(HOST_3);
106
107 target = new K8sHostManager();
108 target.storageService = new TestStorageService();
109 target.coreService = new TestCoreService();
110 target.clusterService = new TestClusterService();
111 target.leadershipService = new TestLeadershipService();
112 target.hostStore = hostStore;
113 target.addListener(testListener);
114 target.activate();
115 testListener.events.clear();
116 }
117
118 /**
119 * Clean up unit test.
120 */
121 @After
122 public void tearDown() {
123 target.removeListener(testListener);
124 target.deactivate();
125 hostStore.deactivate();
126 hostStore = null;
127 target = null;
128 }
129
130 /**
131 * Checks if creating and removing a host work well with proper events.
132 */
133 @Test
134 public void testCreateAndRemoveHost() {
135 target.createHost(HOST_1);
136 assertEquals(ERR_SIZE, 3, target.hosts().size());
137 assertNotNull(target.host(HOST_IP_1));
138
139 target.removeHost(HOST_IP_1);
140 assertEquals(ERR_SIZE, 2, target.hosts().size());
141 assertNull(target.host(HOST_IP_1));
142
143 validateEvents(K8S_HOST_CREATED, K8S_HOST_REMOVED);
144 }
145
146 /**
147 * Checks if creating null host fails with proper exception.
148 */
149 @Test(expected = NullPointerException.class)
150 public void testCreateNullHost() {
151 target.createHost(null);
152 }
153
154 /**
155 * Checks if creating a duplicated host fails with proper exception.
156 */
157 @Test(expected = IllegalArgumentException.class)
158 public void testCreateDuplicateHost() {
159 target.createHost(HOST_1);
160 target.createHost(HOST_1);
161 }
162
163 /**
164 * Checks if removing null host fails with proper exception.
165 */
166 @Test(expected = IllegalArgumentException.class)
167 public void testRemoveNullHost() {
168 target.removeHost(null);
169 }
170
171 /**
172 * Checks if updating a host works well with proper event.
173 */
174 @Test
175 public void testUpdateHost() {
176 K8sHost updated = HOST_2.updateState(COMPLETE);
177 target.updateHost(updated);
178 validateEvents(K8S_HOST_UPDATED, K8S_HOST_COMPLETE);
179 }
180
181 /**
182 * Checks if updating a null host fails with proper exception.
183 */
184 @Test(expected = NullPointerException.class)
185 public void testUpdateNullHost() {
186 target.updateHost(null);
187 }
188
189 /**
190 * Checks if updating not existing host fails with proper exception.
191 */
192 @Test(expected = IllegalArgumentException.class)
193 public void testUpdateNotExistingHost() {
194 target.updateHost(HOST_1);
195 }
196
197 /**
198 * Checks if adding nodes into host works well with proper event.
199 */
200 @Test
201 public void testAddNodesToHost() {
202 K8sHost updated = HOST_2.updateNodeNames(ImmutableSet.of("3", "4", "5"));
203 target.updateHost(updated);
204 validateEvents(K8S_HOST_UPDATED, K8S_NODES_ADDED);
205 }
206
207 /**
208 * Checks if removing nodes from host works well with proper event.
209 */
210 @Test
211 public void testRemoveNodesFromHost() {
212 K8sHost updated = HOST_2.updateNodeNames(ImmutableSet.of("3"));
213 target.updateHost(updated);
214 validateEvents(K8S_HOST_UPDATED, K8S_NODES_REMOVED);
215 }
216
217 /**
218 * Checks if getting all hosts method returns correct set of nodes.
219 */
220 @Test
221 public void testGetAllHosts() {
222 assertEquals(ERR_SIZE, 2, target.hosts().size());
223 assertTrue(ERR_NOT_FOUND, target.hosts().contains(HOST_2));
224 assertTrue(ERR_NOT_FOUND, target.hosts().contains(HOST_3));
225 }
226
227 /**
228 * Checks if getting complete hosts method returns correct set of nodes.
229 */
230 @Test
231 public void testGetCompleteHosts() {
232 assertEquals(ERR_SIZE, 1, target.completeHosts().size());
233 assertTrue(ERR_NOT_FOUND, target.completeHosts().contains(HOST_3));
234 }
235
236 private void validateEvents(Enum... types) {
237 int i = 0;
238 assertEquals("Number of events did not match", types.length, testListener.events.size());
239 for (Event event : testListener.events) {
240 assertEquals("Incorrect event received", types[i], event.type());
241 i++;
242 }
243 testListener.events.clear();
244 }
245
246 private static class TestK8sHostListener implements K8sHostListener {
247 private List<K8sHostEvent> events = Lists.newArrayList();
248
249 @Override
250 public void event(K8sHostEvent event) {
251 events.add(event);
252 }
253 }
254
255 private static class TestCoreService extends CoreServiceAdapter {
256 @Override
257 public ApplicationId registerApplication(String name) {
258 return TEST_APP_ID;
259 }
260 }
261
262 private class TestClusterService extends ClusterServiceAdapter {
263
264 }
265
266 private static class TestLeadershipService extends LeadershipServiceAdapter {
267
268 }
269
270 private static K8sHost createHost(IpAddress hostIp, K8sHostState state, Set<String> nodeNames) {
271 return DefaultK8sHost.builder()
272 .hostIp(hostIp)
273 .nodeNames(nodeNames)
274 .state(state)
275 .build();
276 }
277}