blob: 90f7d0d0d987b71f616f347683af080ea98e4b20 [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"))
97 .state(K8sNodeState.INIT)
98 .build();
Jian Li3defa842019-02-12 00:31:35 +090099
100 k8sApiConfig = DefaultK8sApiConfig.builder()
101 .scheme(K8sApiConfig.Scheme.HTTPS)
102 .ipAddress(IpAddress.valueOf("10.134.34.223"))
103 .port(6443)
Jian Li1cee9882019-02-13 11:25:25 +0900104 .state(DISCONNECTED)
Jian Li3defa842019-02-12 00:31:35 +0900105 .token("tokenMod")
106 .caCertData("caCertData")
107 .clientCertData("clientCertData")
108 .clientKeyData("clientKeyData")
109 .build();
Jian Li49109b52019-01-22 00:17:28 +0900110 }
111
112 /**
113 * Tests the results of the REST API POST method with creating new nodes operation.
114 */
115 @Test
116 public void testCreateNodesWithCreateOperation() {
117 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
118 mockK8sNodeAdminService.createNode(anyObject());
119 replay(mockK8sNodeAdminService);
120
121 final WebTarget wt = target();
122 InputStream jsonStream = K8sNodeWebResourceTest.class
123 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900124 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900125 .post(Entity.json(jsonStream));
126 final int status = response.getStatus();
127
128 assertThat(status, is(201));
129
130 verify(mockK8sNodeAdminService);
131 }
132
133 /**
134 * Tests the results of the REST API POST method without creating new nodes operation.
135 */
136 @Test
137 public void testCreateNodesWithoutCreateOperation() {
138 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
139 replay(mockK8sNodeAdminService);
140
141 final WebTarget wt = target();
142 InputStream jsonStream = K8sNodeWebResourceTest.class
143 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900144 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900145 .post(Entity.json(jsonStream));
146 final int status = response.getStatus();
147
148 assertThat(status, is(201));
149
150 verify(mockK8sNodeAdminService);
151 }
152
153 /**
154 * Tests the results of the REST API PUT method with modifying the nodes.
155 */
156 @Test
Jian Lia80b1582019-01-25 12:47:42 +0900157 public void testUpdateNodesWithModifyOperation() {
Jian Li49109b52019-01-22 00:17:28 +0900158 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
159 mockK8sNodeAdminService.updateNode(anyObject());
160 replay(mockK8sNodeAdminService);
161
162 final WebTarget wt = target();
163 InputStream jsonStream = K8sNodeWebResourceTest.class
164 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900165 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900166 .put(Entity.json(jsonStream));
167 final int status = response.getStatus();
168
169 assertThat(status, is(200));
170
171 verify(mockK8sNodeAdminService);
172 }
173
174 /**
175 * Tests the results of the REST API PUT method without modifying the nodes.
176 */
177 @Test
Jian Lia80b1582019-01-25 12:47:42 +0900178 public void testUpdateNodesWithoutModifyOperation() {
Jian Li49109b52019-01-22 00:17:28 +0900179 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
180 replay(mockK8sNodeAdminService);
181
182 final WebTarget wt = target();
183 InputStream jsonStream = K8sNodeWebResourceTest.class
184 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900185 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900186 .put(Entity.json(jsonStream));
187 final int status = response.getStatus();
188
189 assertThat(status, is(304));
190
191 verify(mockK8sNodeAdminService);
192 }
193
194 /**
195 * Tests the results of the REST API DELETE method with deleting the nodes.
196 */
197 @Test
198 public void testDeleteNodesWithDeletionOperation() {
199 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
200 expect(mockK8sNodeAdminService.removeNode(anyString())).andReturn(k8sNode).once();
201 replay(mockK8sNodeAdminService);
202
Jian Li3defa842019-02-12 00:31:35 +0900203 String location = NODE_PATH + "/minion-node";
Jian Li49109b52019-01-22 00:17:28 +0900204
205 final WebTarget wt = target();
206 Response response = wt.path(location).request(
207 MediaType.APPLICATION_JSON_TYPE).delete();
208
209 final int status = response.getStatus();
210
211 assertThat(status, is(204));
212
213 verify(mockK8sNodeAdminService);
214 }
215
216 /**
217 * Tests the results of the REST API DELETE method without deleting the nodes.
218 */
219 @Test
220 public void testDeleteNodesWithoutDeletionOperation() {
221 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
222 replay(mockK8sNodeAdminService);
223
Jian Li3defa842019-02-12 00:31:35 +0900224 String location = NODE_PATH + "/minion-node";
Jian Li49109b52019-01-22 00:17:28 +0900225
226 final WebTarget wt = target();
227 Response response = wt.path(location).request(
228 MediaType.APPLICATION_JSON_TYPE).delete();
229
230 final int status = response.getStatus();
231
232 assertThat(status, is(304));
233
234 verify(mockK8sNodeAdminService);
235 }
Jian Li3defa842019-02-12 00:31:35 +0900236
237 /**
238 * Tests the results of the REST API POST method with creating new configs operation.
239 */
240 @Test
241 public void testCreateConfigsWithCreateOperation() {
242 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
243 mockK8sApiConfigAdminService.createApiConfig(anyObject());
244 replay(mockK8sApiConfigAdminService);
245
246 final WebTarget wt = target();
247 InputStream jsonStream = K8sNodeWebResourceTest.class
248 .getResourceAsStream("k8s-api-config.json");
249 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
250 .post(Entity.json(jsonStream));
251 final int status = response.getStatus();
252
253 assertThat(status, is(201));
254
255 verify(mockK8sApiConfigAdminService);
256 }
257
258 /**
259 * Tests the results of the REST API POST method without creating new configs operation.
260 */
261 @Test
262 public void testCreateConfigsWithoutCreateOperation() {
263 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(k8sApiConfig).once();
264 replay(mockK8sApiConfigAdminService);
265
266 final WebTarget wt = target();
267 InputStream jsonStream = K8sNodeWebResourceTest.class
268 .getResourceAsStream("k8s-api-config.json");
269 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
270 .post(Entity.json(jsonStream));
271 final int status = response.getStatus();
272
273 assertThat(status, is(201));
274
275 verify(mockK8sApiConfigAdminService);
276 }
277
278 /**
279 * Tests the results of the REST API PUT method with modifying the configs.
280 */
281 @Test
282 public void testUpdateConfigsWithModifyOperation() {
283 expect(mockK8sApiConfigAdminService.apiConfig(anyString()))
284 .andReturn(k8sApiConfig).once();
285 mockK8sApiConfigAdminService.updateApiConfig(anyObject());
286 replay(mockK8sApiConfigAdminService);
287
288 final WebTarget wt = target();
289 InputStream jsonStream = K8sNodeWebResourceTest.class
290 .getResourceAsStream("k8s-api-config.json");
291 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
292 .put(Entity.json(jsonStream));
293 final int status = response.getStatus();
294
295 assertThat(status, is(200));
296
297 verify(mockK8sApiConfigAdminService);
298 }
299
300 /**
301 * Tests the results of the REST API PUT method without modifying the configs.
302 */
303 @Test
304 public void testUpdateConfigsWithoutModifyOperation() {
305 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
306 replay(mockK8sApiConfigAdminService);
307
308 final WebTarget wt = target();
309 InputStream jsonStream = K8sNodeWebResourceTest.class
310 .getResourceAsStream("k8s-api-config.json");
311 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
312 .put(Entity.json(jsonStream));
313 final int status = response.getStatus();
314
315 assertThat(status, is(304));
316
317 verify(mockK8sApiConfigAdminService);
318 }
319
320 /**
321 * Tests the results of the REST API DELETE method with deleting the configs.
322 */
323 @Test
324 public void testDeleteConfigsWithDeletionOperation() {
325 expect(mockK8sApiConfigAdminService.apiConfig(anyString()))
326 .andReturn(k8sApiConfig).once();
327 expect(mockK8sApiConfigAdminService.removeApiConfig(anyString()))
328 .andReturn(k8sApiConfig).once();
329 replay(mockK8sApiConfigAdminService);
330
331 String location = API_PATH + "/https://test:8663";
332
333 final WebTarget wt = target();
334 Response response = wt.path(location).request(
335 MediaType.APPLICATION_JSON_TYPE).delete();
336
337 final int status = response.getStatus();
338
339 assertThat(status, is(204));
340
341 verify(mockK8sApiConfigAdminService);
342 }
343
344 /**
345 * Tests the results of the REST API DELETE method without deleting the configs.
346 */
347 @Test
348 public void testDeleteConfigsWithoutDeletionOperation() {
349 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
350 replay(mockK8sApiConfigAdminService);
351
352 String location = API_PATH + "/https://test:8663";
353
354 final WebTarget wt = target();
355 Response response = wt.path(location).request(
356 MediaType.APPLICATION_JSON_TYPE).delete();
357
358 final int status = response.getStatus();
359
360 assertThat(status, is(304));
361
362 verify(mockK8sApiConfigAdminService);
363 }
Jian Li49109b52019-01-22 00:17:28 +0900364}