Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.k8snode.web; |
| 17 | |
| 18 | import org.glassfish.jersey.server.ResourceConfig; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.osgi.ServiceDirectory; |
| 22 | import org.onlab.osgi.TestServiceDirectory; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.codec.CodecService; |
| 25 | import org.onosproject.codec.impl.CodecManager; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 26 | import org.onosproject.k8snode.api.DefaultK8sApiConfig; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 27 | import org.onosproject.k8snode.api.DefaultK8sNode; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 28 | import org.onosproject.k8snode.api.K8sApiConfig; |
| 29 | import org.onosproject.k8snode.api.K8sApiConfigAdminService; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 30 | import org.onosproject.k8snode.api.K8sNode; |
| 31 | import org.onosproject.k8snode.api.K8sNodeAdminService; |
| 32 | import org.onosproject.k8snode.api.K8sNodeState; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 33 | import org.onosproject.k8snode.codec.K8sApiConfigCodec; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 34 | import org.onosproject.k8snode.codec.K8sNodeCodec; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.rest.resources.ResourceTest; |
| 37 | |
| 38 | import javax.ws.rs.client.Entity; |
| 39 | import javax.ws.rs.client.WebTarget; |
| 40 | import javax.ws.rs.core.MediaType; |
| 41 | import javax.ws.rs.core.Response; |
| 42 | import java.io.InputStream; |
| 43 | |
| 44 | import static org.easymock.EasyMock.anyObject; |
| 45 | import static org.easymock.EasyMock.anyString; |
| 46 | import static org.easymock.EasyMock.createMock; |
| 47 | import static org.easymock.EasyMock.expect; |
| 48 | import static org.easymock.EasyMock.replay; |
| 49 | import static org.easymock.EasyMock.verify; |
| 50 | import static org.hamcrest.Matchers.is; |
| 51 | import static org.junit.Assert.assertThat; |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame^] | 52 | import static org.onosproject.k8snode.api.K8sApiConfig.State.DISCONNECTED; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Unit test for Kubernetes node REST API. |
| 56 | */ |
| 57 | public class K8sNodeWebResourceTest extends ResourceTest { |
| 58 | |
| 59 | final K8sNodeAdminService mockK8sNodeAdminService = createMock(K8sNodeAdminService.class); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 60 | 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 Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 64 | |
| 65 | private K8sNode k8sNode; |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 66 | private K8sApiConfig k8sApiConfig; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 67 | |
| 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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 83 | codecService.registerCodec(K8sApiConfig.class, new K8sApiConfigCodec()); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 84 | ServiceDirectory testDirectory = |
| 85 | new TestServiceDirectory() |
| 86 | .add(K8sNodeAdminService.class, mockK8sNodeAdminService) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 87 | .add(K8sApiConfigAdminService.class, mockK8sApiConfigAdminService) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 88 | .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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 99 | |
| 100 | k8sApiConfig = DefaultK8sApiConfig.builder() |
| 101 | .scheme(K8sApiConfig.Scheme.HTTPS) |
| 102 | .ipAddress(IpAddress.valueOf("10.134.34.223")) |
| 103 | .port(6443) |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame^] | 104 | .state(DISCONNECTED) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 105 | .token("tokenMod") |
| 106 | .caCertData("caCertData") |
| 107 | .clientCertData("clientCertData") |
| 108 | .clientKeyData("clientKeyData") |
| 109 | .build(); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 110 | } |
| 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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 124 | Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 125 | .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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 144 | Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 145 | .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 Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 157 | public void testUpdateNodesWithModifyOperation() { |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 158 | 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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 165 | Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 166 | .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 Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 178 | public void testUpdateNodesWithoutModifyOperation() { |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 179 | 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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 185 | Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 186 | .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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 203 | String location = NODE_PATH + "/minion-node"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 204 | |
| 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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 224 | String location = NODE_PATH + "/minion-node"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 225 | |
| 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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 236 | |
| 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 Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 364 | } |