blob: 75d4433df568b63f13ddc2243acb2f225024ecfd [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;
52
53/**
54 * Unit test for Kubernetes node REST API.
55 */
56public class K8sNodeWebResourceTest extends ResourceTest {
57
58 final K8sNodeAdminService mockK8sNodeAdminService = createMock(K8sNodeAdminService.class);
Jian Li3defa842019-02-12 00:31:35 +090059 final K8sApiConfigAdminService mockK8sApiConfigAdminService =
60 createMock(K8sApiConfigAdminService.class);
61 private static final String NODE_PATH = "configure/node";
62 private static final String API_PATH = "configure/api";
Jian Li49109b52019-01-22 00:17:28 +090063
64 private K8sNode k8sNode;
Jian Li3defa842019-02-12 00:31:35 +090065 private K8sApiConfig k8sApiConfig;
Jian Li49109b52019-01-22 00:17:28 +090066
67 /**
68 * Constructs a kubernetes node resource test instance.
69 */
70 public K8sNodeWebResourceTest() {
71 super(ResourceConfig.forApplicationClass(K8sNodeWebApplication.class));
72 }
73
74 /**
75 * Sets up the global values for all the tests.
76 */
77 @Before
78 public void setUpTest() {
79 final CodecManager codecService = new CodecManager();
80 codecService.activate();
81 codecService.registerCodec(K8sNode.class, new K8sNodeCodec());
Jian Li3defa842019-02-12 00:31:35 +090082 codecService.registerCodec(K8sApiConfig.class, new K8sApiConfigCodec());
Jian Li49109b52019-01-22 00:17:28 +090083 ServiceDirectory testDirectory =
84 new TestServiceDirectory()
85 .add(K8sNodeAdminService.class, mockK8sNodeAdminService)
Jian Li3defa842019-02-12 00:31:35 +090086 .add(K8sApiConfigAdminService.class, mockK8sApiConfigAdminService)
Jian Li49109b52019-01-22 00:17:28 +090087 .add(CodecService.class, codecService);
88 setServiceDirectory(testDirectory);
89
90 k8sNode = DefaultK8sNode.builder()
91 .hostname("minion-node")
92 .type(K8sNode.Type.MINION)
93 .dataIp(IpAddress.valueOf("10.134.34.222"))
94 .managementIp(IpAddress.valueOf("10.134.231.30"))
95 .intgBridge(DeviceId.deviceId("of:00000000000000a1"))
96 .state(K8sNodeState.INIT)
97 .build();
Jian Li3defa842019-02-12 00:31:35 +090098
99 k8sApiConfig = DefaultK8sApiConfig.builder()
100 .scheme(K8sApiConfig.Scheme.HTTPS)
101 .ipAddress(IpAddress.valueOf("10.134.34.223"))
102 .port(6443)
103 .token("tokenMod")
104 .caCertData("caCertData")
105 .clientCertData("clientCertData")
106 .clientKeyData("clientKeyData")
107 .build();
Jian Li49109b52019-01-22 00:17:28 +0900108 }
109
110 /**
111 * Tests the results of the REST API POST method with creating new nodes operation.
112 */
113 @Test
114 public void testCreateNodesWithCreateOperation() {
115 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
116 mockK8sNodeAdminService.createNode(anyObject());
117 replay(mockK8sNodeAdminService);
118
119 final WebTarget wt = target();
120 InputStream jsonStream = K8sNodeWebResourceTest.class
121 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900122 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900123 .post(Entity.json(jsonStream));
124 final int status = response.getStatus();
125
126 assertThat(status, is(201));
127
128 verify(mockK8sNodeAdminService);
129 }
130
131 /**
132 * Tests the results of the REST API POST method without creating new nodes operation.
133 */
134 @Test
135 public void testCreateNodesWithoutCreateOperation() {
136 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
137 replay(mockK8sNodeAdminService);
138
139 final WebTarget wt = target();
140 InputStream jsonStream = K8sNodeWebResourceTest.class
141 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900142 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900143 .post(Entity.json(jsonStream));
144 final int status = response.getStatus();
145
146 assertThat(status, is(201));
147
148 verify(mockK8sNodeAdminService);
149 }
150
151 /**
152 * Tests the results of the REST API PUT method with modifying the nodes.
153 */
154 @Test
Jian Lia80b1582019-01-25 12:47:42 +0900155 public void testUpdateNodesWithModifyOperation() {
Jian Li49109b52019-01-22 00:17:28 +0900156 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
157 mockK8sNodeAdminService.updateNode(anyObject());
158 replay(mockK8sNodeAdminService);
159
160 final WebTarget wt = target();
161 InputStream jsonStream = K8sNodeWebResourceTest.class
162 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900163 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900164 .put(Entity.json(jsonStream));
165 final int status = response.getStatus();
166
167 assertThat(status, is(200));
168
169 verify(mockK8sNodeAdminService);
170 }
171
172 /**
173 * Tests the results of the REST API PUT method without modifying the nodes.
174 */
175 @Test
Jian Lia80b1582019-01-25 12:47:42 +0900176 public void testUpdateNodesWithoutModifyOperation() {
Jian Li49109b52019-01-22 00:17:28 +0900177 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
178 replay(mockK8sNodeAdminService);
179
180 final WebTarget wt = target();
181 InputStream jsonStream = K8sNodeWebResourceTest.class
182 .getResourceAsStream("k8s-node-minion-config.json");
Jian Li3defa842019-02-12 00:31:35 +0900183 Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE)
Jian Li49109b52019-01-22 00:17:28 +0900184 .put(Entity.json(jsonStream));
185 final int status = response.getStatus();
186
187 assertThat(status, is(304));
188
189 verify(mockK8sNodeAdminService);
190 }
191
192 /**
193 * Tests the results of the REST API DELETE method with deleting the nodes.
194 */
195 @Test
196 public void testDeleteNodesWithDeletionOperation() {
197 expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once();
198 expect(mockK8sNodeAdminService.removeNode(anyString())).andReturn(k8sNode).once();
199 replay(mockK8sNodeAdminService);
200
Jian Li3defa842019-02-12 00:31:35 +0900201 String location = NODE_PATH + "/minion-node";
Jian Li49109b52019-01-22 00:17:28 +0900202
203 final WebTarget wt = target();
204 Response response = wt.path(location).request(
205 MediaType.APPLICATION_JSON_TYPE).delete();
206
207 final int status = response.getStatus();
208
209 assertThat(status, is(204));
210
211 verify(mockK8sNodeAdminService);
212 }
213
214 /**
215 * Tests the results of the REST API DELETE method without deleting the nodes.
216 */
217 @Test
218 public void testDeleteNodesWithoutDeletionOperation() {
219 expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once();
220 replay(mockK8sNodeAdminService);
221
Jian Li3defa842019-02-12 00:31:35 +0900222 String location = NODE_PATH + "/minion-node";
Jian Li49109b52019-01-22 00:17:28 +0900223
224 final WebTarget wt = target();
225 Response response = wt.path(location).request(
226 MediaType.APPLICATION_JSON_TYPE).delete();
227
228 final int status = response.getStatus();
229
230 assertThat(status, is(304));
231
232 verify(mockK8sNodeAdminService);
233 }
Jian Li3defa842019-02-12 00:31:35 +0900234
235 /**
236 * Tests the results of the REST API POST method with creating new configs operation.
237 */
238 @Test
239 public void testCreateConfigsWithCreateOperation() {
240 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
241 mockK8sApiConfigAdminService.createApiConfig(anyObject());
242 replay(mockK8sApiConfigAdminService);
243
244 final WebTarget wt = target();
245 InputStream jsonStream = K8sNodeWebResourceTest.class
246 .getResourceAsStream("k8s-api-config.json");
247 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
248 .post(Entity.json(jsonStream));
249 final int status = response.getStatus();
250
251 assertThat(status, is(201));
252
253 verify(mockK8sApiConfigAdminService);
254 }
255
256 /**
257 * Tests the results of the REST API POST method without creating new configs operation.
258 */
259 @Test
260 public void testCreateConfigsWithoutCreateOperation() {
261 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(k8sApiConfig).once();
262 replay(mockK8sApiConfigAdminService);
263
264 final WebTarget wt = target();
265 InputStream jsonStream = K8sNodeWebResourceTest.class
266 .getResourceAsStream("k8s-api-config.json");
267 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
268 .post(Entity.json(jsonStream));
269 final int status = response.getStatus();
270
271 assertThat(status, is(201));
272
273 verify(mockK8sApiConfigAdminService);
274 }
275
276 /**
277 * Tests the results of the REST API PUT method with modifying the configs.
278 */
279 @Test
280 public void testUpdateConfigsWithModifyOperation() {
281 expect(mockK8sApiConfigAdminService.apiConfig(anyString()))
282 .andReturn(k8sApiConfig).once();
283 mockK8sApiConfigAdminService.updateApiConfig(anyObject());
284 replay(mockK8sApiConfigAdminService);
285
286 final WebTarget wt = target();
287 InputStream jsonStream = K8sNodeWebResourceTest.class
288 .getResourceAsStream("k8s-api-config.json");
289 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
290 .put(Entity.json(jsonStream));
291 final int status = response.getStatus();
292
293 assertThat(status, is(200));
294
295 verify(mockK8sApiConfigAdminService);
296 }
297
298 /**
299 * Tests the results of the REST API PUT method without modifying the configs.
300 */
301 @Test
302 public void testUpdateConfigsWithoutModifyOperation() {
303 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
304 replay(mockK8sApiConfigAdminService);
305
306 final WebTarget wt = target();
307 InputStream jsonStream = K8sNodeWebResourceTest.class
308 .getResourceAsStream("k8s-api-config.json");
309 Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE)
310 .put(Entity.json(jsonStream));
311 final int status = response.getStatus();
312
313 assertThat(status, is(304));
314
315 verify(mockK8sApiConfigAdminService);
316 }
317
318 /**
319 * Tests the results of the REST API DELETE method with deleting the configs.
320 */
321 @Test
322 public void testDeleteConfigsWithDeletionOperation() {
323 expect(mockK8sApiConfigAdminService.apiConfig(anyString()))
324 .andReturn(k8sApiConfig).once();
325 expect(mockK8sApiConfigAdminService.removeApiConfig(anyString()))
326 .andReturn(k8sApiConfig).once();
327 replay(mockK8sApiConfigAdminService);
328
329 String location = API_PATH + "/https://test:8663";
330
331 final WebTarget wt = target();
332 Response response = wt.path(location).request(
333 MediaType.APPLICATION_JSON_TYPE).delete();
334
335 final int status = response.getStatus();
336
337 assertThat(status, is(204));
338
339 verify(mockK8sApiConfigAdminService);
340 }
341
342 /**
343 * Tests the results of the REST API DELETE method without deleting the configs.
344 */
345 @Test
346 public void testDeleteConfigsWithoutDeletionOperation() {
347 expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once();
348 replay(mockK8sApiConfigAdminService);
349
350 String location = API_PATH + "/https://test:8663";
351
352 final WebTarget wt = target();
353 Response response = wt.path(location).request(
354 MediaType.APPLICATION_JSON_TYPE).delete();
355
356 final int status = response.getStatus();
357
358 assertThat(status, is(304));
359
360 verify(mockK8sApiConfigAdminService);
361 }
Jian Li49109b52019-01-22 00:17:28 +0900362}