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")) |
Jian Li | bf562c2 | 2019-04-15 18:07:14 +0900 | [diff] [blame] | 97 | .extBridge(DeviceId.deviceId("of:00000000000000b1")) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 98 | .state(K8sNodeState.INIT) |
| 99 | .build(); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 100 | |
| 101 | k8sApiConfig = DefaultK8sApiConfig.builder() |
| 102 | .scheme(K8sApiConfig.Scheme.HTTPS) |
| 103 | .ipAddress(IpAddress.valueOf("10.134.34.223")) |
| 104 | .port(6443) |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 105 | .state(DISCONNECTED) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 106 | .token("tokenMod") |
| 107 | .caCertData("caCertData") |
| 108 | .clientCertData("clientCertData") |
| 109 | .clientKeyData("clientKeyData") |
| 110 | .build(); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 111 | } |
| 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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 125 | Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 126 | .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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 145 | Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 146 | .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 Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 158 | public void testUpdateNodesWithModifyOperation() { |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 159 | 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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 166 | Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 167 | .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 Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 179 | public void testUpdateNodesWithoutModifyOperation() { |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 180 | 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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 186 | Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 187 | .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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 204 | String location = NODE_PATH + "/minion-node"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 205 | |
| 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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 225 | String location = NODE_PATH + "/minion-node"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 226 | |
| 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 Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 237 | |
| 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 Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 365 | } |