blob: 207bd0ec609caa2c3f7fd3471f5d7bef2da561b6 [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 Li3defa842019-02-12 00:31:35 +090028import org.onosproject.k8snode.api.K8sApiConfig;
29import org.onosproject.k8snode.api.K8sApiConfigAdminService;
Jian Li49109b52019-01-22 00:17:28 +090030import org.onosproject.k8snode.api.K8sNode;
31import org.onosproject.k8snode.api.K8sNodeAdminService;
32import org.onosproject.k8snode.api.K8sNodeState;
Jian Li3defa842019-02-12 00:31:35 +090033import org.onosproject.k8snode.codec.K8sApiConfigCodec;
Jian Li49109b52019-01-22 00:17:28 +090034import org.onosproject.k8snode.codec.K8sNodeCodec;
35import org.onosproject.net.DeviceId;
36import org.onosproject.rest.resources.ResourceTest;
37
38import javax.ws.rs.client.Entity;
39import javax.ws.rs.client.WebTarget;
40import javax.ws.rs.core.MediaType;
41import javax.ws.rs.core.Response;
42import java.io.InputStream;
43
44import static org.easymock.EasyMock.anyObject;
45import static org.easymock.EasyMock.anyString;
46import static org.easymock.EasyMock.createMock;
47import static org.easymock.EasyMock.expect;
48import static org.easymock.EasyMock.replay;
49import static org.easymock.EasyMock.verify;
50import static org.hamcrest.Matchers.is;
51import static org.junit.Assert.assertThat;
Jian Li1cee9882019-02-13 11:25:25 +090052import static org.onosproject.k8snode.api.K8sApiConfig.State.DISCONNECTED;
Jian Li49109b52019-01-22 00:17:28 +090053
54/**
55 * Unit test for Kubernetes node REST API.
56 */
57public class K8sNodeWebResourceTest extends ResourceTest {
58
59 final K8sNodeAdminService mockK8sNodeAdminService = createMock(K8sNodeAdminService.class);
Jian Li3defa842019-02-12 00:31:35 +090060 final K8sApiConfigAdminService mockK8sApiConfigAdminService =
61 createMock(K8sApiConfigAdminService.class);
62 private static final String NODE_PATH = "configure/node";
63 private static final String API_PATH = "configure/api";
Jian Li49109b52019-01-22 00:17:28 +090064
65 private K8sNode k8sNode;
Jian Li3defa842019-02-12 00:31:35 +090066 private K8sApiConfig k8sApiConfig;
Jian Li49109b52019-01-22 00:17:28 +090067
68 /**
69 * Constructs a kubernetes node resource test instance.
70 */
71 public K8sNodeWebResourceTest() {
72 super(ResourceConfig.forApplicationClass(K8sNodeWebApplication.class));
73 }
74
75 /**
76 * Sets up the global values for all the tests.
77 */
78 @Before
79 public void setUpTest() {
80 final CodecManager codecService = new CodecManager();
81 codecService.activate();
82 codecService.registerCodec(K8sNode.class, new K8sNodeCodec());
Jian Li3defa842019-02-12 00:31:35 +090083 codecService.registerCodec(K8sApiConfig.class, new K8sApiConfigCodec());
Jian Li49109b52019-01-22 00:17:28 +090084 ServiceDirectory testDirectory =
85 new TestServiceDirectory()
86 .add(K8sNodeAdminService.class, mockK8sNodeAdminService)
Jian Li3defa842019-02-12 00:31:35 +090087 .add(K8sApiConfigAdminService.class, mockK8sApiConfigAdminService)
Jian Li49109b52019-01-22 00:17:28 +090088 .add(CodecService.class, codecService);
89 setServiceDirectory(testDirectory);
90
91 k8sNode = DefaultK8sNode.builder()
92 .hostname("minion-node")
93 .type(K8sNode.Type.MINION)
94 .dataIp(IpAddress.valueOf("10.134.34.222"))
95 .managementIp(IpAddress.valueOf("10.134.231.30"))
96 .intgBridge(DeviceId.deviceId("of:00000000000000a1"))
Jian Libf562c22019-04-15 18:07:14 +090097 .extBridge(DeviceId.deviceId("of:00000000000000b1"))
Jian Li49109b52019-01-22 00:17:28 +090098 .state(K8sNodeState.INIT)
99 .build();
Jian Li3defa842019-02-12 00:31:35 +0900100
101 k8sApiConfig = DefaultK8sApiConfig.builder()
102 .scheme(K8sApiConfig.Scheme.HTTPS)
103 .ipAddress(IpAddress.valueOf("10.134.34.223"))
104 .port(6443)
Jian Li1cee9882019-02-13 11:25:25 +0900105 .state(DISCONNECTED)
Jian Li3defa842019-02-12 00:31:35 +0900106 .token("tokenMod")
107 .caCertData("caCertData")
108 .clientCertData("clientCertData")
109 .clientKeyData("clientKeyData")
110 .build();
Jian Li49109b52019-01-22 00:17:28 +0900111 }
112
113 /**
114 * Tests the results of the REST API POST method with creating new nodes operation.
115 */
116 @Test
117 public void testCreateNodesWithCreateOperation() {
118 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
119 mockK8sNodeAdminService.createNode(anyObject());
120 replay(mockK8sNodeAdminService);
121
122 final WebTarget wt = target();
123 InputStream jsonStream = K8sNodeWebResourceTest.class
124 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900125 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900126 .post(Entity.json(jsonStream));
127 final int status = response.getStatus();
128
129 assertThat(status, is(201));
130
131 verify(mockK8sNodeAdminService);
132 }
133
134 /**
135 * Tests the results of the REST API POST method without creating new nodes operation.
136 */
137 @Test
138 public void testCreateNodesWithoutCreateOperation() {
139 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
140 replay(mockK8sNodeAdminService);
141
142 final WebTarget wt = target();
143 InputStream jsonStream = K8sNodeWebResourceTest.class
144 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900145 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900146 .post(Entity.json(jsonStream));
147 final int status = response.getStatus();
148
149 assertThat(status, is(201));
150
151 verify(mockK8sNodeAdminService);
152 }
153
154 /**
155 * Tests the results of the REST API PUT method with modifying the nodes.
156 */
157 @Test
Jian Lia80b1582019-01-25 12:47:42 +0900158 public void testUpdateNodesWithModifyOperation() {
Jian Li49109b52019-01-22 00:17:28 +0900159 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
160 mockK8sNodeAdminService.updateNode(anyObject());
161 replay(mockK8sNodeAdminService);
162
163 final WebTarget wt = target();
164 InputStream jsonStream = K8sNodeWebResourceTest.class
165 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900166 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900167 .put(Entity.json(jsonStream));
168 final int status = response.getStatus();
169
170 assertThat(status, is(200));
171
172 verify(mockK8sNodeAdminService);
173 }
174
175 /**
176 * Tests the results of the REST API PUT method without modifying the nodes.
177 */
178 @Test
Jian Lia80b1582019-01-25 12:47:42 +0900179 public void testUpdateNodesWithoutModifyOperation() {
Jian Li49109b52019-01-22 00:17:28 +0900180 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
181 replay(mockK8sNodeAdminService);
182
183 final WebTarget wt = target();
184 InputStream jsonStream = K8sNodeWebResourceTest.class
185 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900186 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900187 .put(Entity.json(jsonStream));
188 final int status = response.getStatus();
189
190 assertThat(status, is(304));
191
192 verify(mockK8sNodeAdminService);
193 }
194
195 /**
196 * Tests the results of the REST API DELETE method with deleting the nodes.
197 */
198 @Test
199 public void testDeleteNodesWithDeletionOperation() {
200 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
201 expect(mockK8sNodeAdminService.removeNode(anyString())).andReturn(k8sNode).once();
202 replay(mockK8sNodeAdminService);
203
Jian Li3defa842019-02-12 00:31:35 +0900204 String location = NODE_PATH + "/minion-node";
Jian Li49109b52019-01-22 00:17:28 +0900205
206 final WebTarget wt = target();
207 Response response = wt.path(location).request(
208 MediaType.APPLICATION_JSON_TYPE).delete();
209
210 final int status = response.getStatus();
211
212 assertThat(status, is(204));
213
214 verify(mockK8sNodeAdminService);
215 }
216
217 /**
218 * Tests the results of the REST API DELETE method without deleting the nodes.
219 */
220 @Test
221 public void testDeleteNodesWithoutDeletionOperation() {
222 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
223 replay(mockK8sNodeAdminService);
224
Jian Li3defa842019-02-12 00:31:35 +0900225 String location = NODE_PATH + "/minion-node";
Jian Li49109b52019-01-22 00:17:28 +0900226
227 final WebTarget wt = target();
228 Response response = wt.path(location).request(
229 MediaType.APPLICATION_JSON_TYPE).delete();
230
231 final int status = response.getStatus();
232
233 assertThat(status, is(304));
234
235 verify(mockK8sNodeAdminService);
236 }
Jian Li3defa842019-02-12 00:31:35 +0900237
238 /**
239 * Tests the results of the REST API POST method with creating new configs operation.
240 */
241 @Test
242 public void testCreateConfigsWithCreateOperation() {
243 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
244 mockK8sApiConfigAdminService.createApiConfig(anyObject());
245 replay(mockK8sApiConfigAdminService);
246
247 final WebTarget wt = target();
248 InputStream jsonStream = K8sNodeWebResourceTest.class
249 .getResourceAsStream("k8s-api-config.json");
250 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
251 .post(Entity.json(jsonStream));
252 final int status = response.getStatus();
253
254 assertThat(status, is(201));
255
256 verify(mockK8sApiConfigAdminService);
257 }
258
259 /**
260 * Tests the results of the REST API POST method without creating new configs operation.
261 */
262 @Test
263 public void testCreateConfigsWithoutCreateOperation() {
264 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(k8sApiConfig).once();
265 replay(mockK8sApiConfigAdminService);
266
267 final WebTarget wt = target();
268 InputStream jsonStream = K8sNodeWebResourceTest.class
269 .getResourceAsStream("k8s-api-config.json");
270 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
271 .post(Entity.json(jsonStream));
272 final int status = response.getStatus();
273
274 assertThat(status, is(201));
275
276 verify(mockK8sApiConfigAdminService);
277 }
278
279 /**
280 * Tests the results of the REST API PUT method with modifying the configs.
281 */
282 @Test
283 public void testUpdateConfigsWithModifyOperation() {
284 expect(mockK8sApiConfigAdminService.apiConfig(anyString()))
285 .andReturn(k8sApiConfig).once();
286 mockK8sApiConfigAdminService.updateApiConfig(anyObject());
287 replay(mockK8sApiConfigAdminService);
288
289 final WebTarget wt = target();
290 InputStream jsonStream = K8sNodeWebResourceTest.class
291 .getResourceAsStream("k8s-api-config.json");
292 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
293 .put(Entity.json(jsonStream));
294 final int status = response.getStatus();
295
296 assertThat(status, is(200));
297
298 verify(mockK8sApiConfigAdminService);
299 }
300
301 /**
302 * Tests the results of the REST API PUT method without modifying the configs.
303 */
304 @Test
305 public void testUpdateConfigsWithoutModifyOperation() {
306 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
307 replay(mockK8sApiConfigAdminService);
308
309 final WebTarget wt = target();
310 InputStream jsonStream = K8sNodeWebResourceTest.class
311 .getResourceAsStream("k8s-api-config.json");
312 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
313 .put(Entity.json(jsonStream));
314 final int status = response.getStatus();
315
316 assertThat(status, is(304));
317
318 verify(mockK8sApiConfigAdminService);
319 }
320
321 /**
322 * Tests the results of the REST API DELETE method with deleting the configs.
323 */
324 @Test
325 public void testDeleteConfigsWithDeletionOperation() {
326 expect(mockK8sApiConfigAdminService.apiConfig(anyString()))
327 .andReturn(k8sApiConfig).once();
328 expect(mockK8sApiConfigAdminService.removeApiConfig(anyString()))
329 .andReturn(k8sApiConfig).once();
330 replay(mockK8sApiConfigAdminService);
331
332 String location = API_PATH + "/https://test:8663";
333
334 final WebTarget wt = target();
335 Response response = wt.path(location).request(
336 MediaType.APPLICATION_JSON_TYPE).delete();
337
338 final int status = response.getStatus();
339
340 assertThat(status, is(204));
341
342 verify(mockK8sApiConfigAdminService);
343 }
344
345 /**
346 * Tests the results of the REST API DELETE method without deleting the configs.
347 */
348 @Test
349 public void testDeleteConfigsWithoutDeletionOperation() {
350 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
351 replay(mockK8sApiConfigAdminService);
352
353 String location = API_PATH + "/https://test:8663";
354
355 final WebTarget wt = target();
356 Response response = wt.path(location).request(
357 MediaType.APPLICATION_JSON_TYPE).delete();
358
359 final int status = response.getStatus();
360
361 assertThat(status, is(304));
362
363 verify(mockK8sApiConfigAdminService);
364 }
Jian Li49109b52019-01-22 00:17:28 +0900365}