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