blob: 9908e997d0a012d3c4c9eb41e2ff244c2e4fcd71 [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"))
Jian Li4294af72020-10-07 02:12:33 +0900100 .nodeIp(IpAddress.valueOf("30.30.30.3"))
Jian Li49109b52019-01-22 00:17:28 +0900101 .intgBridge(DeviceId.deviceId("of:00000000000000a1"))
Jian Libf562c22019-04-15 18:07:14 +0900102 .extBridge(DeviceId.deviceId("of:00000000000000b1"))
Jian Li49109b52019-01-22 00:17:28 +0900103 .state(K8sNodeState.INIT)
104 .build();
Jian Li3defa842019-02-12 00:31:35 +0900105
106 k8sApiConfig = DefaultK8sApiConfig.builder()
Jian Lie2a04ce2020-07-01 19:07:02 +0900107 .clusterName("kubernetes")
108 .segmentId(1)
109 .mode(K8sApiConfig.Mode.NORMAL)
Jian Li3defa842019-02-12 00:31:35 +0900110 .scheme(K8sApiConfig.Scheme.HTTPS)
111 .ipAddress(IpAddress.valueOf("10.134.34.223"))
112 .port(6443)
Jian Li1cee9882019-02-13 11:25:25 +0900113 .state(DISCONNECTED)
Jian Li3defa842019-02-12 00:31:35 +0900114 .token("tokenMod")
115 .caCertData("caCertData")
116 .clientCertData("clientCertData")
117 .clientKeyData("clientKeyData")
118 .build();
Jian Li49109b52019-01-22 00:17:28 +0900119 }
120
121 /**
122 * Tests the results of the REST API POST method with creating new nodes operation.
123 */
124 @Test
125 public void testCreateNodesWithCreateOperation() {
126 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
127 mockK8sNodeAdminService.createNode(anyObject());
128 replay(mockK8sNodeAdminService);
129
130 final WebTarget wt = target();
131 InputStream jsonStream = K8sNodeWebResourceTest.class
132 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900133 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900134 .post(Entity.json(jsonStream));
135 final int status = response.getStatus();
136
137 assertThat(status, is(201));
138
139 verify(mockK8sNodeAdminService);
140 }
141
142 /**
143 * Tests the results of the REST API POST method without creating new nodes operation.
144 */
145 @Test
146 public void testCreateNodesWithoutCreateOperation() {
147 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
148 replay(mockK8sNodeAdminService);
149
150 final WebTarget wt = target();
151 InputStream jsonStream = K8sNodeWebResourceTest.class
152 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900153 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900154 .post(Entity.json(jsonStream));
155 final int status = response.getStatus();
156
157 assertThat(status, is(201));
158
159 verify(mockK8sNodeAdminService);
160 }
161
162 /**
163 * Tests the results of the REST API PUT method with modifying the nodes.
164 */
165 @Test
Jian Lia80b1582019-01-25 12:47:42 +0900166 public void testUpdateNodesWithModifyOperation() {
Jian Li49109b52019-01-22 00:17:28 +0900167 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
168 mockK8sNodeAdminService.updateNode(anyObject());
169 replay(mockK8sNodeAdminService);
170
171 final WebTarget wt = target();
172 InputStream jsonStream = K8sNodeWebResourceTest.class
173 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900174 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900175 .put(Entity.json(jsonStream));
176 final int status = response.getStatus();
177
178 assertThat(status, is(200));
179
180 verify(mockK8sNodeAdminService);
181 }
182
183 /**
184 * Tests the results of the REST API PUT method without modifying the nodes.
185 */
186 @Test
Jian Lia80b1582019-01-25 12:47:42 +0900187 public void testUpdateNodesWithoutModifyOperation() {
Jian Li49109b52019-01-22 00:17:28 +0900188 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
189 replay(mockK8sNodeAdminService);
190
191 final WebTarget wt = target();
192 InputStream jsonStream = K8sNodeWebResourceTest.class
193 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900194 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900195 .put(Entity.json(jsonStream));
196 final int status = response.getStatus();
197
198 assertThat(status, is(304));
199
200 verify(mockK8sNodeAdminService);
201 }
202
203 /**
204 * Tests the results of the REST API DELETE method with deleting the nodes.
205 */
206 @Test
207 public void testDeleteNodesWithDeletionOperation() {
208 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
209 expect(mockK8sNodeAdminService.removeNode(anyString())).andReturn(k8sNode).once();
210 replay(mockK8sNodeAdminService);
211
Jian Li3defa842019-02-12 00:31:35 +0900212 String location = NODE_PATH + "/minion-node";
Jian Li49109b52019-01-22 00:17:28 +0900213
214 final WebTarget wt = target();
215 Response response = wt.path(location).request(
216 MediaType.APPLICATION_JSON_TYPE).delete();
217
218 final int status = response.getStatus();
219
220 assertThat(status, is(204));
221
222 verify(mockK8sNodeAdminService);
223 }
224
225 /**
226 * Tests the results of the REST API DELETE method without deleting the nodes.
227 */
228 @Test
229 public void testDeleteNodesWithoutDeletionOperation() {
230 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
231 replay(mockK8sNodeAdminService);
232
Jian Li3defa842019-02-12 00:31:35 +0900233 String location = NODE_PATH + "/minion-node";
Jian Li49109b52019-01-22 00:17:28 +0900234
235 final WebTarget wt = target();
236 Response response = wt.path(location).request(
237 MediaType.APPLICATION_JSON_TYPE).delete();
238
239 final int status = response.getStatus();
240
241 assertThat(status, is(304));
242
243 verify(mockK8sNodeAdminService);
244 }
Jian Li3defa842019-02-12 00:31:35 +0900245
246 /**
247 * Tests the results of the REST API POST method with creating new configs operation.
248 */
249 @Test
250 public void testCreateConfigsWithCreateOperation() {
251 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
252 mockK8sApiConfigAdminService.createApiConfig(anyObject());
253 replay(mockK8sApiConfigAdminService);
254
255 final WebTarget wt = target();
256 InputStream jsonStream = K8sNodeWebResourceTest.class
257 .getResourceAsStream("k8s-api-config.json");
258 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
259 .post(Entity.json(jsonStream));
260 final int status = response.getStatus();
261
262 assertThat(status, is(201));
263
264 verify(mockK8sApiConfigAdminService);
265 }
266
267 /**
268 * Tests the results of the REST API POST method without creating new configs operation.
269 */
270 @Test
271 public void testCreateConfigsWithoutCreateOperation() {
272 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(k8sApiConfig).once();
273 replay(mockK8sApiConfigAdminService);
274
275 final WebTarget wt = target();
276 InputStream jsonStream = K8sNodeWebResourceTest.class
277 .getResourceAsStream("k8s-api-config.json");
278 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
279 .post(Entity.json(jsonStream));
280 final int status = response.getStatus();
281
282 assertThat(status, is(201));
283
284 verify(mockK8sApiConfigAdminService);
285 }
286
287 /**
288 * Tests the results of the REST API PUT method with modifying the configs.
289 */
290 @Test
291 public void testUpdateConfigsWithModifyOperation() {
292 expect(mockK8sApiConfigAdminService.apiConfig(anyString()))
293 .andReturn(k8sApiConfig).once();
294 mockK8sApiConfigAdminService.updateApiConfig(anyObject());
295 replay(mockK8sApiConfigAdminService);
296
297 final WebTarget wt = target();
298 InputStream jsonStream = K8sNodeWebResourceTest.class
299 .getResourceAsStream("k8s-api-config.json");
300 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
301 .put(Entity.json(jsonStream));
302 final int status = response.getStatus();
303
304 assertThat(status, is(200));
305
306 verify(mockK8sApiConfigAdminService);
307 }
308
309 /**
310 * Tests the results of the REST API PUT method without modifying the configs.
311 */
312 @Test
313 public void testUpdateConfigsWithoutModifyOperation() {
314 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
315 replay(mockK8sApiConfigAdminService);
316
317 final WebTarget wt = target();
318 InputStream jsonStream = K8sNodeWebResourceTest.class
319 .getResourceAsStream("k8s-api-config.json");
320 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
321 .put(Entity.json(jsonStream));
322 final int status = response.getStatus();
323
324 assertThat(status, is(304));
325
326 verify(mockK8sApiConfigAdminService);
327 }
328
329 /**
330 * Tests the results of the REST API DELETE method with deleting the configs.
331 */
332 @Test
333 public void testDeleteConfigsWithDeletionOperation() {
334 expect(mockK8sApiConfigAdminService.apiConfig(anyString()))
335 .andReturn(k8sApiConfig).once();
336 expect(mockK8sApiConfigAdminService.removeApiConfig(anyString()))
337 .andReturn(k8sApiConfig).once();
338 replay(mockK8sApiConfigAdminService);
339
340 String location = API_PATH + "/https://test:8663";
341
342 final WebTarget wt = target();
343 Response response = wt.path(location).request(
344 MediaType.APPLICATION_JSON_TYPE).delete();
345
346 final int status = response.getStatus();
347
348 assertThat(status, is(204));
349
350 verify(mockK8sApiConfigAdminService);
351 }
352
353 /**
354 * Tests the results of the REST API DELETE method without deleting the configs.
355 */
356 @Test
357 public void testDeleteConfigsWithoutDeletionOperation() {
358 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
359 replay(mockK8sApiConfigAdminService);
360
361 String location = API_PATH + "/https://test:8663";
362
363 final WebTarget wt = target();
364 Response response = wt.path(location).request(
365 MediaType.APPLICATION_JSON_TYPE).delete();
366
367 final int status = response.getStatus();
368
369 assertThat(status, is(304));
370
371 verify(mockK8sApiConfigAdminService);
372 }
Jian Li49109b52019-01-22 00:17:28 +0900373}