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