blob: 1005ab26b8778e45c45f8377f77f15ef85136b7c [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;
33import org.onosproject.k8snode.api.K8sNodeState;
Jian Lie2a04ce2020-07-01 19:07:02 +090034import org.onosproject.k8snode.codec.HostNodesInfoCodec;
Jian Li3defa842019-02-12 00:31:35 +090035import org.onosproject.k8snode.codec.K8sApiConfigCodec;
Jian Li49109b52019-01-22 00:17:28 +090036import org.onosproject.k8snode.codec.K8sNodeCodec;
37import org.onosproject.net.DeviceId;
38import org.onosproject.rest.resources.ResourceTest;
39
40import javax.ws.rs.client.Entity;
41import javax.ws.rs.client.WebTarget;
42import javax.ws.rs.core.MediaType;
43import javax.ws.rs.core.Response;
44import java.io.InputStream;
45
46import static org.easymock.EasyMock.anyObject;
47import static org.easymock.EasyMock.anyString;
48import static org.easymock.EasyMock.createMock;
49import static org.easymock.EasyMock.expect;
50import static org.easymock.EasyMock.replay;
51import static org.easymock.EasyMock.verify;
52import static org.hamcrest.Matchers.is;
53import static org.junit.Assert.assertThat;
Jian Li1cee9882019-02-13 11:25:25 +090054import static org.onosproject.k8snode.api.K8sApiConfig.State.DISCONNECTED;
Jian Li49109b52019-01-22 00:17:28 +090055
56/**
57 * Unit test for Kubernetes node REST API.
58 */
59public class K8sNodeWebResourceTest extends ResourceTest {
60
61 final K8sNodeAdminService mockK8sNodeAdminService = createMock(K8sNodeAdminService.class);
Jian Li3defa842019-02-12 00:31:35 +090062 final K8sApiConfigAdminService mockK8sApiConfigAdminService =
63 createMock(K8sApiConfigAdminService.class);
64 private static final String NODE_PATH = "configure/node";
65 private static final String API_PATH = "configure/api";
Jian Li49109b52019-01-22 00:17:28 +090066
67 private K8sNode k8sNode;
Jian Li3defa842019-02-12 00:31:35 +090068 private K8sApiConfig k8sApiConfig;
Jian Li49109b52019-01-22 00:17:28 +090069
70 /**
71 * Constructs a kubernetes node resource test instance.
72 */
73 public K8sNodeWebResourceTest() {
74 super(ResourceConfig.forApplicationClass(K8sNodeWebApplication.class));
75 }
76
77 /**
78 * Sets up the global values for all the tests.
79 */
80 @Before
81 public void setUpTest() {
82 final CodecManager codecService = new CodecManager();
83 codecService.activate();
84 codecService.registerCodec(K8sNode.class, new K8sNodeCodec());
Jian Li3defa842019-02-12 00:31:35 +090085 codecService.registerCodec(K8sApiConfig.class, new K8sApiConfigCodec());
Jian Lie2a04ce2020-07-01 19:07:02 +090086 codecService.registerCodec(HostNodesInfo.class, new HostNodesInfoCodec());
Jian Li49109b52019-01-22 00:17:28 +090087 ServiceDirectory testDirectory =
88 new TestServiceDirectory()
89 .add(K8sNodeAdminService.class, mockK8sNodeAdminService)
Jian Li3defa842019-02-12 00:31:35 +090090 .add(K8sApiConfigAdminService.class, mockK8sApiConfigAdminService)
Jian Li49109b52019-01-22 00:17:28 +090091 .add(CodecService.class, codecService);
92 setServiceDirectory(testDirectory);
93
94 k8sNode = DefaultK8sNode.builder()
Jian Lie2a04ce2020-07-01 19:07:02 +090095 .clusterName("kubernetes")
Jian Li49109b52019-01-22 00:17:28 +090096 .hostname("minion-node")
97 .type(K8sNode.Type.MINION)
98 .dataIp(IpAddress.valueOf("10.134.34.222"))
99 .managementIp(IpAddress.valueOf("10.134.231.30"))
100 .intgBridge(DeviceId.deviceId("of:00000000000000a1"))
Jian Libf562c22019-04-15 18:07:14 +0900101 .extBridge(DeviceId.deviceId("of:00000000000000b1"))
Jian Li49109b52019-01-22 00:17:28 +0900102 .state(K8sNodeState.INIT)
103 .build();
Jian Li3defa842019-02-12 00:31:35 +0900104
105 k8sApiConfig = DefaultK8sApiConfig.builder()
Jian Lie2a04ce2020-07-01 19:07:02 +0900106 .clusterName("kubernetes")
107 .segmentId(1)
108 .mode(K8sApiConfig.Mode.NORMAL)
Jian Li3defa842019-02-12 00:31:35 +0900109 .scheme(K8sApiConfig.Scheme.HTTPS)
110 .ipAddress(IpAddress.valueOf("10.134.34.223"))
111 .port(6443)
Jian Li1cee9882019-02-13 11:25:25 +0900112 .state(DISCONNECTED)
Jian Li3defa842019-02-12 00:31:35 +0900113 .token("tokenMod")
114 .caCertData("caCertData")
115 .clientCertData("clientCertData")
116 .clientKeyData("clientKeyData")
117 .build();
Jian Li49109b52019-01-22 00:17:28 +0900118 }
119
120 /**
121 * Tests the results of the REST API POST method with creating new nodes operation.
122 */
123 @Test
124 public void testCreateNodesWithCreateOperation() {
125 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
126 mockK8sNodeAdminService.createNode(anyObject());
127 replay(mockK8sNodeAdminService);
128
129 final WebTarget wt = target();
130 InputStream jsonStream = K8sNodeWebResourceTest.class
131 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900132 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900133 .post(Entity.json(jsonStream));
134 final int status = response.getStatus();
135
136 assertThat(status, is(201));
137
138 verify(mockK8sNodeAdminService);
139 }
140
141 /**
142 * Tests the results of the REST API POST method without creating new nodes operation.
143 */
144 @Test
145 public void testCreateNodesWithoutCreateOperation() {
146 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
147 replay(mockK8sNodeAdminService);
148
149 final WebTarget wt = target();
150 InputStream jsonStream = K8sNodeWebResourceTest.class
151 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900152 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900153 .post(Entity.json(jsonStream));
154 final int status = response.getStatus();
155
156 assertThat(status, is(201));
157
158 verify(mockK8sNodeAdminService);
159 }
160
161 /**
162 * Tests the results of the REST API PUT method with modifying the nodes.
163 */
164 @Test
Jian Lia80b1582019-01-25 12:47:42 +0900165 public void testUpdateNodesWithModifyOperation() {
Jian Li49109b52019-01-22 00:17:28 +0900166 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
167 mockK8sNodeAdminService.updateNode(anyObject());
168 replay(mockK8sNodeAdminService);
169
170 final WebTarget wt = target();
171 InputStream jsonStream = K8sNodeWebResourceTest.class
172 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900173 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900174 .put(Entity.json(jsonStream));
175 final int status = response.getStatus();
176
177 assertThat(status, is(200));
178
179 verify(mockK8sNodeAdminService);
180 }
181
182 /**
183 * Tests the results of the REST API PUT method without modifying the nodes.
184 */
185 @Test
Jian Lia80b1582019-01-25 12:47:42 +0900186 public void testUpdateNodesWithoutModifyOperation() {
Jian Li49109b52019-01-22 00:17:28 +0900187 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
188 replay(mockK8sNodeAdminService);
189
190 final WebTarget wt = target();
191 InputStream jsonStream = K8sNodeWebResourceTest.class
192 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900193 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900194 .put(Entity.json(jsonStream));
195 final int status = response.getStatus();
196
197 assertThat(status, is(304));
198
199 verify(mockK8sNodeAdminService);
200 }
201
202 /**
203 * Tests the results of the REST API DELETE method with deleting the nodes.
204 */
205 @Test
206 public void testDeleteNodesWithDeletionOperation() {
207 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
208 expect(mockK8sNodeAdminService.removeNode(anyString())).andReturn(k8sNode).once();
209 replay(mockK8sNodeAdminService);
210
Jian Li3defa842019-02-12 00:31:35 +0900211 String location = NODE_PATH + "/minion-node";
Jian Li49109b52019-01-22 00:17:28 +0900212
213 final WebTarget wt = target();
214 Response response = wt.path(location).request(
215 MediaType.APPLICATION_JSON_TYPE).delete();
216
217 final int status = response.getStatus();
218
219 assertThat(status, is(204));
220
221 verify(mockK8sNodeAdminService);
222 }
223
224 /**
225 * Tests the results of the REST API DELETE method without deleting the nodes.
226 */
227 @Test
228 public void testDeleteNodesWithoutDeletionOperation() {
229 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
230 replay(mockK8sNodeAdminService);
231
Jian Li3defa842019-02-12 00:31:35 +0900232 String location = NODE_PATH + "/minion-node";
Jian Li49109b52019-01-22 00:17:28 +0900233
234 final WebTarget wt = target();
235 Response response = wt.path(location).request(
236 MediaType.APPLICATION_JSON_TYPE).delete();
237
238 final int status = response.getStatus();
239
240 assertThat(status, is(304));
241
242 verify(mockK8sNodeAdminService);
243 }
Jian Li3defa842019-02-12 00:31:35 +0900244
245 /**
246 * Tests the results of the REST API POST method with creating new configs operation.
247 */
248 @Test
249 public void testCreateConfigsWithCreateOperation() {
250 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
251 mockK8sApiConfigAdminService.createApiConfig(anyObject());
252 replay(mockK8sApiConfigAdminService);
253
254 final WebTarget wt = target();
255 InputStream jsonStream = K8sNodeWebResourceTest.class
256 .getResourceAsStream("k8s-api-config.json");
257 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
258 .post(Entity.json(jsonStream));
259 final int status = response.getStatus();
260
261 assertThat(status, is(201));
262
263 verify(mockK8sApiConfigAdminService);
264 }
265
266 /**
267 * Tests the results of the REST API POST method without creating new configs operation.
268 */
269 @Test
270 public void testCreateConfigsWithoutCreateOperation() {
271 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(k8sApiConfig).once();
272 replay(mockK8sApiConfigAdminService);
273
274 final WebTarget wt = target();
275 InputStream jsonStream = K8sNodeWebResourceTest.class
276 .getResourceAsStream("k8s-api-config.json");
277 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
278 .post(Entity.json(jsonStream));
279 final int status = response.getStatus();
280
281 assertThat(status, is(201));
282
283 verify(mockK8sApiConfigAdminService);
284 }
285
286 /**
287 * Tests the results of the REST API PUT method with modifying the configs.
288 */
289 @Test
290 public void testUpdateConfigsWithModifyOperation() {
291 expect(mockK8sApiConfigAdminService.apiConfig(anyString()))
292 .andReturn(k8sApiConfig).once();
293 mockK8sApiConfigAdminService.updateApiConfig(anyObject());
294 replay(mockK8sApiConfigAdminService);
295
296 final WebTarget wt = target();
297 InputStream jsonStream = K8sNodeWebResourceTest.class
298 .getResourceAsStream("k8s-api-config.json");
299 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
300 .put(Entity.json(jsonStream));
301 final int status = response.getStatus();
302
303 assertThat(status, is(200));
304
305 verify(mockK8sApiConfigAdminService);
306 }
307
308 /**
309 * Tests the results of the REST API PUT method without modifying the configs.
310 */
311 @Test
312 public void testUpdateConfigsWithoutModifyOperation() {
313 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
314 replay(mockK8sApiConfigAdminService);
315
316 final WebTarget wt = target();
317 InputStream jsonStream = K8sNodeWebResourceTest.class
318 .getResourceAsStream("k8s-api-config.json");
319 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
320 .put(Entity.json(jsonStream));
321 final int status = response.getStatus();
322
323 assertThat(status, is(304));
324
325 verify(mockK8sApiConfigAdminService);
326 }
327
328 /**
329 * Tests the results of the REST API DELETE method with deleting the configs.
330 */
331 @Test
332 public void testDeleteConfigsWithDeletionOperation() {
333 expect(mockK8sApiConfigAdminService.apiConfig(anyString()))
334 .andReturn(k8sApiConfig).once();
335 expect(mockK8sApiConfigAdminService.removeApiConfig(anyString()))
336 .andReturn(k8sApiConfig).once();
337 replay(mockK8sApiConfigAdminService);
338
339 String location = API_PATH + "/https://test:8663";
340
341 final WebTarget wt = target();
342 Response response = wt.path(location).request(
343 MediaType.APPLICATION_JSON_TYPE).delete();
344
345 final int status = response.getStatus();
346
347 assertThat(status, is(204));
348
349 verify(mockK8sApiConfigAdminService);
350 }
351
352 /**
353 * Tests the results of the REST API DELETE method without deleting the configs.
354 */
355 @Test
356 public void testDeleteConfigsWithoutDeletionOperation() {
357 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
358 replay(mockK8sApiConfigAdminService);
359
360 String location = API_PATH + "/https://test:8663";
361
362 final WebTarget wt = target();
363 Response response = wt.path(location).request(
364 MediaType.APPLICATION_JSON_TYPE).delete();
365
366 final int status = response.getStatus();
367
368 assertThat(status, is(304));
369
370 verify(mockK8sApiConfigAdminService);
371 }
Jian Li49109b52019-01-22 00:17:28 +0900372}