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")) |
Jian Li | 4294af7 | 2020-10-07 02:12:33 +0900 | [diff] [blame] | 100 | .nodeIp(IpAddress.valueOf("30.30.30.3")) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 101 | .intgBridge(DeviceId.deviceId("of:00000000000000a1")) |
Jian Li | bf562c2 | 2019-04-15 18:07:14 +0900 | [diff] [blame] | 102 | .extBridge(DeviceId.deviceId("of:00000000000000b1")) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 103 | .state(K8sNodeState.INIT) |
| 104 | .build(); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 105 | |
| 106 | k8sApiConfig = DefaultK8sApiConfig.builder() |
Jian Li | e2a04ce | 2020-07-01 19:07:02 +0900 | [diff] [blame] | 107 | .clusterName("kubernetes") |
| 108 | .segmentId(1) |
| 109 | .mode(K8sApiConfig.Mode.NORMAL) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 110 | .scheme(K8sApiConfig.Scheme.HTTPS) |
| 111 | .ipAddress(IpAddress.valueOf("10.134.34.223")) |
| 112 | .port(6443) |
Jian Li | 1cee988 | 2019-02-13 11:25:25 +0900 | [diff] [blame] | 113 | .state(DISCONNECTED) |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 114 | .token("tokenMod") |
| 115 | .caCertData("caCertData") |
| 116 | .clientCertData("clientCertData") |
| 117 | .clientKeyData("clientKeyData") |
| 118 | .build(); |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Tests the results of the REST API POST method with creating new nodes operation. |
| 123 | */ |
| 124 | @Test |
| 125 | public void testCreateNodesWithCreateOperation() { |
| 126 | expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once(); |
| 127 | mockK8sNodeAdminService.createNode(anyObject()); |
| 128 | replay(mockK8sNodeAdminService); |
| 129 | |
| 130 | final WebTarget wt = target(); |
| 131 | InputStream jsonStream = K8sNodeWebResourceTest.class |
| 132 | .getResourceAsStream("k8s-node-minion-config.json"); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 133 | Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 134 | .post(Entity.json(jsonStream)); |
| 135 | final int status = response.getStatus(); |
| 136 | |
| 137 | assertThat(status, is(201)); |
| 138 | |
| 139 | verify(mockK8sNodeAdminService); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Tests the results of the REST API POST method without creating new nodes operation. |
| 144 | */ |
| 145 | @Test |
| 146 | public void testCreateNodesWithoutCreateOperation() { |
| 147 | expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once(); |
| 148 | replay(mockK8sNodeAdminService); |
| 149 | |
| 150 | final WebTarget wt = target(); |
| 151 | InputStream jsonStream = K8sNodeWebResourceTest.class |
| 152 | .getResourceAsStream("k8s-node-minion-config.json"); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 153 | Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 154 | .post(Entity.json(jsonStream)); |
| 155 | final int status = response.getStatus(); |
| 156 | |
| 157 | assertThat(status, is(201)); |
| 158 | |
| 159 | verify(mockK8sNodeAdminService); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Tests the results of the REST API PUT method with modifying the nodes. |
| 164 | */ |
| 165 | @Test |
Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 166 | public void testUpdateNodesWithModifyOperation() { |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 167 | expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once(); |
| 168 | mockK8sNodeAdminService.updateNode(anyObject()); |
| 169 | replay(mockK8sNodeAdminService); |
| 170 | |
| 171 | final WebTarget wt = target(); |
| 172 | InputStream jsonStream = K8sNodeWebResourceTest.class |
| 173 | .getResourceAsStream("k8s-node-minion-config.json"); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 174 | Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 175 | .put(Entity.json(jsonStream)); |
| 176 | final int status = response.getStatus(); |
| 177 | |
| 178 | assertThat(status, is(200)); |
| 179 | |
| 180 | verify(mockK8sNodeAdminService); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Tests the results of the REST API PUT method without modifying the nodes. |
| 185 | */ |
| 186 | @Test |
Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 187 | public void testUpdateNodesWithoutModifyOperation() { |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 188 | expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once(); |
| 189 | replay(mockK8sNodeAdminService); |
| 190 | |
| 191 | final WebTarget wt = target(); |
| 192 | InputStream jsonStream = K8sNodeWebResourceTest.class |
| 193 | .getResourceAsStream("k8s-node-minion-config.json"); |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 194 | Response response = wt.path(NODE_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 195 | .put(Entity.json(jsonStream)); |
| 196 | final int status = response.getStatus(); |
| 197 | |
| 198 | assertThat(status, is(304)); |
| 199 | |
| 200 | verify(mockK8sNodeAdminService); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Tests the results of the REST API DELETE method with deleting the nodes. |
| 205 | */ |
| 206 | @Test |
| 207 | public void testDeleteNodesWithDeletionOperation() { |
| 208 | expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once(); |
| 209 | expect(mockK8sNodeAdminService.removeNode(anyString())).andReturn(k8sNode).once(); |
| 210 | replay(mockK8sNodeAdminService); |
| 211 | |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 212 | String location = NODE_PATH + "/minion-node"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 213 | |
| 214 | final WebTarget wt = target(); |
| 215 | Response response = wt.path(location).request( |
| 216 | MediaType.APPLICATION_JSON_TYPE).delete(); |
| 217 | |
| 218 | final int status = response.getStatus(); |
| 219 | |
| 220 | assertThat(status, is(204)); |
| 221 | |
| 222 | verify(mockK8sNodeAdminService); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Tests the results of the REST API DELETE method without deleting the nodes. |
| 227 | */ |
| 228 | @Test |
| 229 | public void testDeleteNodesWithoutDeletionOperation() { |
| 230 | expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once(); |
| 231 | replay(mockK8sNodeAdminService); |
| 232 | |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 233 | String location = NODE_PATH + "/minion-node"; |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 234 | |
| 235 | final WebTarget wt = target(); |
| 236 | Response response = wt.path(location).request( |
| 237 | MediaType.APPLICATION_JSON_TYPE).delete(); |
| 238 | |
| 239 | final int status = response.getStatus(); |
| 240 | |
| 241 | assertThat(status, is(304)); |
| 242 | |
| 243 | verify(mockK8sNodeAdminService); |
| 244 | } |
Jian Li | 3defa84 | 2019-02-12 00:31:35 +0900 | [diff] [blame] | 245 | |
| 246 | /** |
| 247 | * Tests the results of the REST API POST method with creating new configs operation. |
| 248 | */ |
| 249 | @Test |
| 250 | public void testCreateConfigsWithCreateOperation() { |
| 251 | expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once(); |
| 252 | mockK8sApiConfigAdminService.createApiConfig(anyObject()); |
| 253 | replay(mockK8sApiConfigAdminService); |
| 254 | |
| 255 | final WebTarget wt = target(); |
| 256 | InputStream jsonStream = K8sNodeWebResourceTest.class |
| 257 | .getResourceAsStream("k8s-api-config.json"); |
| 258 | Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 259 | .post(Entity.json(jsonStream)); |
| 260 | final int status = response.getStatus(); |
| 261 | |
| 262 | assertThat(status, is(201)); |
| 263 | |
| 264 | verify(mockK8sApiConfigAdminService); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Tests the results of the REST API POST method without creating new configs operation. |
| 269 | */ |
| 270 | @Test |
| 271 | public void testCreateConfigsWithoutCreateOperation() { |
| 272 | expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(k8sApiConfig).once(); |
| 273 | replay(mockK8sApiConfigAdminService); |
| 274 | |
| 275 | final WebTarget wt = target(); |
| 276 | InputStream jsonStream = K8sNodeWebResourceTest.class |
| 277 | .getResourceAsStream("k8s-api-config.json"); |
| 278 | Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 279 | .post(Entity.json(jsonStream)); |
| 280 | final int status = response.getStatus(); |
| 281 | |
| 282 | assertThat(status, is(201)); |
| 283 | |
| 284 | verify(mockK8sApiConfigAdminService); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Tests the results of the REST API PUT method with modifying the configs. |
| 289 | */ |
| 290 | @Test |
| 291 | public void testUpdateConfigsWithModifyOperation() { |
| 292 | expect(mockK8sApiConfigAdminService.apiConfig(anyString())) |
| 293 | .andReturn(k8sApiConfig).once(); |
| 294 | mockK8sApiConfigAdminService.updateApiConfig(anyObject()); |
| 295 | replay(mockK8sApiConfigAdminService); |
| 296 | |
| 297 | final WebTarget wt = target(); |
| 298 | InputStream jsonStream = K8sNodeWebResourceTest.class |
| 299 | .getResourceAsStream("k8s-api-config.json"); |
| 300 | Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 301 | .put(Entity.json(jsonStream)); |
| 302 | final int status = response.getStatus(); |
| 303 | |
| 304 | assertThat(status, is(200)); |
| 305 | |
| 306 | verify(mockK8sApiConfigAdminService); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Tests the results of the REST API PUT method without modifying the configs. |
| 311 | */ |
| 312 | @Test |
| 313 | public void testUpdateConfigsWithoutModifyOperation() { |
| 314 | expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once(); |
| 315 | replay(mockK8sApiConfigAdminService); |
| 316 | |
| 317 | final WebTarget wt = target(); |
| 318 | InputStream jsonStream = K8sNodeWebResourceTest.class |
| 319 | .getResourceAsStream("k8s-api-config.json"); |
| 320 | Response response = wt.path(API_PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 321 | .put(Entity.json(jsonStream)); |
| 322 | final int status = response.getStatus(); |
| 323 | |
| 324 | assertThat(status, is(304)); |
| 325 | |
| 326 | verify(mockK8sApiConfigAdminService); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Tests the results of the REST API DELETE method with deleting the configs. |
| 331 | */ |
| 332 | @Test |
| 333 | public void testDeleteConfigsWithDeletionOperation() { |
| 334 | expect(mockK8sApiConfigAdminService.apiConfig(anyString())) |
| 335 | .andReturn(k8sApiConfig).once(); |
| 336 | expect(mockK8sApiConfigAdminService.removeApiConfig(anyString())) |
| 337 | .andReturn(k8sApiConfig).once(); |
| 338 | replay(mockK8sApiConfigAdminService); |
| 339 | |
| 340 | String location = API_PATH + "/https://test:8663"; |
| 341 | |
| 342 | final WebTarget wt = target(); |
| 343 | Response response = wt.path(location).request( |
| 344 | MediaType.APPLICATION_JSON_TYPE).delete(); |
| 345 | |
| 346 | final int status = response.getStatus(); |
| 347 | |
| 348 | assertThat(status, is(204)); |
| 349 | |
| 350 | verify(mockK8sApiConfigAdminService); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Tests the results of the REST API DELETE method without deleting the configs. |
| 355 | */ |
| 356 | @Test |
| 357 | public void testDeleteConfigsWithoutDeletionOperation() { |
| 358 | expect(mockK8sApiConfigAdminService.apiConfig(anyString())).andReturn(null).once(); |
| 359 | replay(mockK8sApiConfigAdminService); |
| 360 | |
| 361 | String location = API_PATH + "/https://test:8663"; |
| 362 | |
| 363 | final WebTarget wt = target(); |
| 364 | Response response = wt.path(location).request( |
| 365 | MediaType.APPLICATION_JSON_TYPE).delete(); |
| 366 | |
| 367 | final int status = response.getStatus(); |
| 368 | |
| 369 | assertThat(status, is(304)); |
| 370 | |
| 371 | verify(mockK8sApiConfigAdminService); |
| 372 | } |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 373 | } |