blob: b99c88155bb56e4067e1f19942703551b9c37179 [file] [log] [blame]
Jian Li49109b52019-01-22 00:17:28 +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.k8snode.web;
17
18import org.glassfish.jersey.server.ResourceConfig;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.osgi.ServiceDirectory;
22import org.onlab.osgi.TestServiceDirectory;
23import org.onlab.packet.IpAddress;
24import org.onosproject.codec.CodecService;
25import org.onosproject.codec.impl.CodecManager;
Jian Li3defa842019-02-12 00:31:35 +090026import org.onosproject.k8snode.api.DefaultK8sApiConfig;
Jian Li49109b52019-01-22 00:17:28 +090027import org.onosproject.k8snode.api.DefaultK8sNode;
Jian Lie2a04ce2020-07-01 19:07:02 +090028import org.onosproject.k8snode.api.HostNodesInfo;
Jian Li3defa842019-02-12 00:31:35 +090029import org.onosproject.k8snode.api.K8sApiConfig;
30import org.onosproject.k8snode.api.K8sApiConfigAdminService;
Jian Li49109b52019-01-22 00:17:28 +090031import org.onosproject.k8snode.api.K8sNode;
32import org.onosproject.k8snode.api.K8sNodeAdminService;
Jian Li6d2ffbf2020-11-04 15:58:18 +090033import org.onosproject.k8snode.api.K8sNodeInfo;
Jian Li49109b52019-01-22 00:17:28 +090034import org.onosproject.k8snode.api.K8sNodeState;
Jian Lie2a04ce2020-07-01 19:07:02 +090035import org.onosproject.k8snode.codec.HostNodesInfoCodec;
Jian Li3defa842019-02-12 00:31:35 +090036import org.onosproject.k8snode.codec.K8sApiConfigCodec;
Jian Li49109b52019-01-22 00:17:28 +090037import org.onosproject.k8snode.codec.K8sNodeCodec;
38import org.onosproject.net.DeviceId;
39import org.onosproject.rest.resources.ResourceTest;
40
41import javax.ws.rs.client.Entity;
42import javax.ws.rs.client.WebTarget;
43import javax.ws.rs.core.MediaType;
44import javax.ws.rs.core.Response;
45import java.io.InputStream;
46
47import static org.easymock.EasyMock.anyObject;
48import static org.easymock.EasyMock.anyString;
49import static org.easymock.EasyMock.createMock;
50import static org.easymock.EasyMock.expect;
51import static org.easymock.EasyMock.replay;
52import static org.easymock.EasyMock.verify;
53import static org.hamcrest.Matchers.is;
54import static org.junit.Assert.assertThat;
Jian Li1cee9882019-02-13 11:25:25 +090055import static org.onosproject.k8snode.api.K8sApiConfig.State.DISCONNECTED;
Jian Li49109b52019-01-22 00:17:28 +090056
57/**
58 * Unit test for Kubernetes node REST API.
59 */
60public class K8sNodeWebResourceTest extends ResourceTest {
61
62 final K8sNodeAdminService mockK8sNodeAdminService = createMock(K8sNodeAdminService.class);
Jian Li3defa842019-02-12 00:31:35 +090063 final K8sApiConfigAdminService mockK8sApiConfigAdminService =
64 createMock(K8sApiConfigAdminService.class);
65 private static final String NODE_PATH = "configure/node";
66 private static final String API_PATH = "configure/api";
Jian Li49109b52019-01-22 00:17:28 +090067
68 private K8sNode k8sNode;
Jian Li3defa842019-02-12 00:31:35 +090069 private K8sApiConfig k8sApiConfig;
Jian Li49109b52019-01-22 00:17:28 +090070
71 /**
72 * Constructs a kubernetes node resource test instance.
73 */
74 public K8sNodeWebResourceTest() {
75 super(ResourceConfig.forApplicationClass(K8sNodeWebApplication.class));
76 }
77
78 /**
79 * Sets up the global values for all the tests.
80 */
81 @Before
82 public void setUpTest() {
83 final CodecManager codecService = new CodecManager();
84 codecService.activate();
85 codecService.registerCodec(K8sNode.class, new K8sNodeCodec());
Jian Li3defa842019-02-12 00:31:35 +090086 codecService.registerCodec(K8sApiConfig.class, new K8sApiConfigCodec());
Jian Lie2a04ce2020-07-01 19:07:02 +090087 codecService.registerCodec(HostNodesInfo.class, new HostNodesInfoCodec());
Jian Li49109b52019-01-22 00:17:28 +090088 ServiceDirectory testDirectory =
89 new TestServiceDirectory()
90 .add(K8sNodeAdminService.class, mockK8sNodeAdminService)
Jian Li3defa842019-02-12 00:31:35 +090091 .add(K8sApiConfigAdminService.class, mockK8sApiConfigAdminService)
Jian Li49109b52019-01-22 00:17:28 +090092 .add(CodecService.class, codecService);
93 setServiceDirectory(testDirectory);
94
95 k8sNode = DefaultK8sNode.builder()
Jian Lie2a04ce2020-07-01 19:07:02 +090096 .clusterName("kubernetes")
Jian Li49109b52019-01-22 00:17:28 +090097 .hostname("minion-node")
98 .type(K8sNode.Type.MINION)
99 .dataIp(IpAddress.valueOf("10.134.34.222"))
100 .managementIp(IpAddress.valueOf("10.134.231.30"))
Jian Li6d2ffbf2020-11-04 15:58:18 +0900101 .nodeInfo(new K8sNodeInfo(IpAddress.valueOf("30.30.30.3"), null))
Jian Li49109b52019-01-22 00:17:28 +0900102 .intgBridge(DeviceId.deviceId("of:00000000000000a1"))
Jian Libf562c22019-04-15 18:07:14 +0900103 .extBridge(DeviceId.deviceId("of:00000000000000b1"))
Jian Li49109b52019-01-22 00:17:28 +0900104 .state(K8sNodeState.INIT)
105 .build();
Jian Li3defa842019-02-12 00:31:35 +0900106
107 k8sApiConfig = DefaultK8sApiConfig.builder()
Jian Lie2a04ce2020-07-01 19:07:02 +0900108 .clusterName("kubernetes")
109 .segmentId(1)
110 .mode(K8sApiConfig.Mode.NORMAL)
Jian Li3defa842019-02-12 00:31:35 +0900111 .scheme(K8sApiConfig.Scheme.HTTPS)
112 .ipAddress(IpAddress.valueOf("10.134.34.223"))
113 .port(6443)
Jian Li1cee9882019-02-13 11:25:25 +0900114 .state(DISCONNECTED)
Jian Li3defa842019-02-12 00:31:35 +0900115 .token("tokenMod")
116 .caCertData("caCertData")
117 .clientCertData("clientCertData")
118 .clientKeyData("clientKeyData")
119 .build();
Jian Li49109b52019-01-22 00:17:28 +0900120 }
121
122 /**
123 * Tests the results of the REST API POST method with creating new nodes operation.
124 */
125 @Test
126 public void testCreateNodesWithCreateOperation() {
127 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
128 mockK8sNodeAdminService.createNode(anyObject());
129 replay(mockK8sNodeAdminService);
130
131 final WebTarget wt = target();
132 InputStream jsonStream = K8sNodeWebResourceTest.class
133 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900134 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900135 .post(Entity.json(jsonStream));
136 final int status = response.getStatus();
137
138 assertThat(status, is(201));
139
140 verify(mockK8sNodeAdminService);
141 }
142
143 /**
144 * Tests the results of the REST API POST method without creating new nodes operation.
145 */
146 @Test
147 public void testCreateNodesWithoutCreateOperation() {
148 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
149 replay(mockK8sNodeAdminService);
150
151 final WebTarget wt = target();
152 InputStream jsonStream = K8sNodeWebResourceTest.class
153 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900154 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900155 .post(Entity.json(jsonStream));
156 final int status = response.getStatus();
157
158 assertThat(status, is(201));
159
160 verify(mockK8sNodeAdminService);
161 }
162
163 /**
164 * Tests the results of the REST API PUT method with modifying the nodes.
165 */
166 @Test
Jian Lia80b1582019-01-25 12:47:42 +0900167 public void testUpdateNodesWithModifyOperation() {
Jian Li49109b52019-01-22 00:17:28 +0900168 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
169 mockK8sNodeAdminService.updateNode(anyObject());
170 replay(mockK8sNodeAdminService);
171
172 final WebTarget wt = target();
173 InputStream jsonStream = K8sNodeWebResourceTest.class
174 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900175 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900176 .put(Entity.json(jsonStream));
177 final int status = response.getStatus();
178
179 assertThat(status, is(200));
180
181 verify(mockK8sNodeAdminService);
182 }
183
184 /**
185 * Tests the results of the REST API PUT method without modifying the nodes.
186 */
187 @Test
Jian Lia80b1582019-01-25 12:47:42 +0900188 public void testUpdateNodesWithoutModifyOperation() {
Jian Li49109b52019-01-22 00:17:28 +0900189 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
190 replay(mockK8sNodeAdminService);
191
192 final WebTarget wt = target();
193 InputStream jsonStream = K8sNodeWebResourceTest.class
194 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900195 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900196 .put(Entity.json(jsonStream));
197 final int status = response.getStatus();
198
199 assertThat(status, is(304));
200
201 verify(mockK8sNodeAdminService);
202 }
203
204 /**
205 * Tests the results of the REST API DELETE method with deleting the nodes.
206 */
207 @Test
208 public void testDeleteNodesWithDeletionOperation() {
209 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
210 expect(mockK8sNodeAdminService.removeNode(anyString())).andReturn(k8sNode).once();
211 replay(mockK8sNodeAdminService);
212
Jian Li3defa842019-02-12 00:31:35 +0900213 String location = NODE_PATH + "/minion-node";
Jian Li49109b52019-01-22 00:17:28 +0900214
215 final WebTarget wt = target();
216 Response response = wt.path(location).request(
217 MediaType.APPLICATION_JSON_TYPE).delete();
218
219 final int status = response.getStatus();
220
221 assertThat(status, is(204));
222
223 verify(mockK8sNodeAdminService);
224 }
225
226 /**
227 * Tests the results of the REST API DELETE method without deleting the nodes.
228 */
229 @Test
230 public void testDeleteNodesWithoutDeletionOperation() {
231 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
232 replay(mockK8sNodeAdminService);
233
Jian Li3defa842019-02-12 00:31:35 +0900234 String location = NODE_PATH + "/minion-node";
Jian Li49109b52019-01-22 00:17:28 +0900235
236 final WebTarget wt = target();
237 Response response = wt.path(location).request(
238 MediaType.APPLICATION_JSON_TYPE).delete();
239
240 final int status = response.getStatus();
241
242 assertThat(status, is(304));
243
244 verify(mockK8sNodeAdminService);
245 }
Jian Li3defa842019-02-12 00:31:35 +0900246
247 /**
248 * Tests the results of the REST API POST method with creating new configs operation.
249 */
250 @Test
251 public void testCreateConfigsWithCreateOperation() {
252 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
253 mockK8sApiConfigAdminService.createApiConfig(anyObject());
254 replay(mockK8sApiConfigAdminService);
255
256 final WebTarget wt = target();
257 InputStream jsonStream = K8sNodeWebResourceTest.class
258 .getResourceAsStream("k8s-api-config.json");
259 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
260 .post(Entity.json(jsonStream));
261 final int status = response.getStatus();
262
263 assertThat(status, is(201));
264
265 verify(mockK8sApiConfigAdminService);
266 }
267
268 /**
269 * Tests the results of the REST API POST method without creating new configs operation.
270 */
271 @Test
272 public void testCreateConfigsWithoutCreateOperation() {
273 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(k8sApiConfig).once();
274 replay(mockK8sApiConfigAdminService);
275
276 final WebTarget wt = target();
277 InputStream jsonStream = K8sNodeWebResourceTest.class
278 .getResourceAsStream("k8s-api-config.json");
279 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
280 .post(Entity.json(jsonStream));
281 final int status = response.getStatus();
282
283 assertThat(status, is(201));
284
285 verify(mockK8sApiConfigAdminService);
286 }
287
288 /**
289 * Tests the results of the REST API PUT method with modifying the configs.
290 */
291 @Test
292 public void testUpdateConfigsWithModifyOperation() {
293 expect(mockK8sApiConfigAdminService.apiConfig(anyString()))
294 .andReturn(k8sApiConfig).once();
295 mockK8sApiConfigAdminService.updateApiConfig(anyObject());
296 replay(mockK8sApiConfigAdminService);
297
298 final WebTarget wt = target();
299 InputStream jsonStream = K8sNodeWebResourceTest.class
300 .getResourceAsStream("k8s-api-config.json");
301 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
302 .put(Entity.json(jsonStream));
303 final int status = response.getStatus();
304
305 assertThat(status, is(200));
306
307 verify(mockK8sApiConfigAdminService);
308 }
309
310 /**
311 * Tests the results of the REST API PUT method without modifying the configs.
312 */
313 @Test
314 public void testUpdateConfigsWithoutModifyOperation() {
315 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
316 replay(mockK8sApiConfigAdminService);
317
318 final WebTarget wt = target();
319 InputStream jsonStream = K8sNodeWebResourceTest.class
320 .getResourceAsStream("k8s-api-config.json");
321 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
322 .put(Entity.json(jsonStream));
323 final int status = response.getStatus();
324
325 assertThat(status, is(304));
326
327 verify(mockK8sApiConfigAdminService);
328 }
329
330 /**
331 * Tests the results of the REST API DELETE method with deleting the configs.
332 */
333 @Test
334 public void testDeleteConfigsWithDeletionOperation() {
335 expect(mockK8sApiConfigAdminService.apiConfig(anyString()))
336 .andReturn(k8sApiConfig).once();
337 expect(mockK8sApiConfigAdminService.removeApiConfig(anyString()))
338 .andReturn(k8sApiConfig).once();
339 replay(mockK8sApiConfigAdminService);
340
341 String location = API_PATH + "/https://test:8663";
342
343 final WebTarget wt = target();
344 Response response = wt.path(location).request(
345 MediaType.APPLICATION_JSON_TYPE).delete();
346
347 final int status = response.getStatus();
348
349 assertThat(status, is(204));
350
351 verify(mockK8sApiConfigAdminService);
352 }
353
354 /**
355 * Tests the results of the REST API DELETE method without deleting the configs.
356 */
357 @Test
358 public void testDeleteConfigsWithoutDeletionOperation() {
359 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
360 replay(mockK8sApiConfigAdminService);
361
362 String location = API_PATH + "/https://test:8663";
363
364 final WebTarget wt = target();
365 Response response = wt.path(location).request(
366 MediaType.APPLICATION_JSON_TYPE).delete();
367
368 final int status = response.getStatus();
369
370 assertThat(status, is(304));
371
372 verify(mockK8sApiConfigAdminService);
373 }
Jian Li49109b52019-01-22 00:17:28 +0900374}