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; |
| 26 | import org.onosproject.k8snode.api.DefaultK8sNode; |
| 27 | import org.onosproject.k8snode.api.K8sNode; |
| 28 | import org.onosproject.k8snode.api.K8sNodeAdminService; |
| 29 | import org.onosproject.k8snode.api.K8sNodeState; |
| 30 | import org.onosproject.k8snode.codec.K8sNodeCodec; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.rest.resources.ResourceTest; |
| 33 | |
| 34 | import javax.ws.rs.client.Entity; |
| 35 | import javax.ws.rs.client.WebTarget; |
| 36 | import javax.ws.rs.core.MediaType; |
| 37 | import javax.ws.rs.core.Response; |
| 38 | import java.io.InputStream; |
| 39 | |
| 40 | import static org.easymock.EasyMock.anyObject; |
| 41 | import static org.easymock.EasyMock.anyString; |
| 42 | import static org.easymock.EasyMock.createMock; |
| 43 | import static org.easymock.EasyMock.expect; |
| 44 | import static org.easymock.EasyMock.replay; |
| 45 | import static org.easymock.EasyMock.verify; |
| 46 | import static org.hamcrest.Matchers.is; |
| 47 | import static org.junit.Assert.assertThat; |
| 48 | |
| 49 | /** |
| 50 | * Unit test for Kubernetes node REST API. |
| 51 | */ |
| 52 | public class K8sNodeWebResourceTest extends ResourceTest { |
| 53 | |
| 54 | final K8sNodeAdminService mockK8sNodeAdminService = createMock(K8sNodeAdminService.class); |
| 55 | private static final String PATH = "configure"; |
| 56 | |
| 57 | private K8sNode k8sNode; |
| 58 | |
| 59 | /** |
| 60 | * Constructs a kubernetes node resource test instance. |
| 61 | */ |
| 62 | public K8sNodeWebResourceTest() { |
| 63 | super(ResourceConfig.forApplicationClass(K8sNodeWebApplication.class)); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Sets up the global values for all the tests. |
| 68 | */ |
| 69 | @Before |
| 70 | public void setUpTest() { |
| 71 | final CodecManager codecService = new CodecManager(); |
| 72 | codecService.activate(); |
| 73 | codecService.registerCodec(K8sNode.class, new K8sNodeCodec()); |
| 74 | ServiceDirectory testDirectory = |
| 75 | new TestServiceDirectory() |
| 76 | .add(K8sNodeAdminService.class, mockK8sNodeAdminService) |
| 77 | .add(CodecService.class, codecService); |
| 78 | setServiceDirectory(testDirectory); |
| 79 | |
| 80 | k8sNode = DefaultK8sNode.builder() |
| 81 | .hostname("minion-node") |
| 82 | .type(K8sNode.Type.MINION) |
| 83 | .dataIp(IpAddress.valueOf("10.134.34.222")) |
| 84 | .managementIp(IpAddress.valueOf("10.134.231.30")) |
| 85 | .intgBridge(DeviceId.deviceId("of:00000000000000a1")) |
| 86 | .state(K8sNodeState.INIT) |
| 87 | .build(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Tests the results of the REST API POST method with creating new nodes operation. |
| 92 | */ |
| 93 | @Test |
| 94 | public void testCreateNodesWithCreateOperation() { |
| 95 | expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once(); |
| 96 | mockK8sNodeAdminService.createNode(anyObject()); |
| 97 | replay(mockK8sNodeAdminService); |
| 98 | |
| 99 | final WebTarget wt = target(); |
| 100 | InputStream jsonStream = K8sNodeWebResourceTest.class |
| 101 | .getResourceAsStream("k8s-node-minion-config.json"); |
| 102 | Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 103 | .post(Entity.json(jsonStream)); |
| 104 | final int status = response.getStatus(); |
| 105 | |
| 106 | assertThat(status, is(201)); |
| 107 | |
| 108 | verify(mockK8sNodeAdminService); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Tests the results of the REST API POST method without creating new nodes operation. |
| 113 | */ |
| 114 | @Test |
| 115 | public void testCreateNodesWithoutCreateOperation() { |
| 116 | expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once(); |
| 117 | replay(mockK8sNodeAdminService); |
| 118 | |
| 119 | final WebTarget wt = target(); |
| 120 | InputStream jsonStream = K8sNodeWebResourceTest.class |
| 121 | .getResourceAsStream("k8s-node-minion-config.json"); |
| 122 | Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 123 | .post(Entity.json(jsonStream)); |
| 124 | final int status = response.getStatus(); |
| 125 | |
| 126 | assertThat(status, is(201)); |
| 127 | |
| 128 | verify(mockK8sNodeAdminService); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Tests the results of the REST API PUT method with modifying the nodes. |
| 133 | */ |
| 134 | @Test |
Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 135 | public void testUpdateNodesWithModifyOperation() { |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 136 | expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once(); |
| 137 | mockK8sNodeAdminService.updateNode(anyObject()); |
| 138 | replay(mockK8sNodeAdminService); |
| 139 | |
| 140 | final WebTarget wt = target(); |
| 141 | InputStream jsonStream = K8sNodeWebResourceTest.class |
| 142 | .getResourceAsStream("k8s-node-minion-config.json"); |
| 143 | Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 144 | .put(Entity.json(jsonStream)); |
| 145 | final int status = response.getStatus(); |
| 146 | |
| 147 | assertThat(status, is(200)); |
| 148 | |
| 149 | verify(mockK8sNodeAdminService); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Tests the results of the REST API PUT method without modifying the nodes. |
| 154 | */ |
| 155 | @Test |
Jian Li | a80b158 | 2019-01-25 12:47:42 +0900 | [diff] [blame] | 156 | public void testUpdateNodesWithoutModifyOperation() { |
Jian Li | 49109b5 | 2019-01-22 00:17:28 +0900 | [diff] [blame] | 157 | expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once(); |
| 158 | replay(mockK8sNodeAdminService); |
| 159 | |
| 160 | final WebTarget wt = target(); |
| 161 | InputStream jsonStream = K8sNodeWebResourceTest.class |
| 162 | .getResourceAsStream("k8s-node-minion-config.json"); |
| 163 | Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 164 | .put(Entity.json(jsonStream)); |
| 165 | final int status = response.getStatus(); |
| 166 | |
| 167 | assertThat(status, is(304)); |
| 168 | |
| 169 | verify(mockK8sNodeAdminService); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Tests the results of the REST API DELETE method with deleting the nodes. |
| 174 | */ |
| 175 | @Test |
| 176 | public void testDeleteNodesWithDeletionOperation() { |
| 177 | expect(mockK8sNodeAdminService.node(anyString())).andReturn(k8sNode).once(); |
| 178 | expect(mockK8sNodeAdminService.removeNode(anyString())).andReturn(k8sNode).once(); |
| 179 | replay(mockK8sNodeAdminService); |
| 180 | |
| 181 | String location = PATH + "/minion-node"; |
| 182 | |
| 183 | final WebTarget wt = target(); |
| 184 | Response response = wt.path(location).request( |
| 185 | MediaType.APPLICATION_JSON_TYPE).delete(); |
| 186 | |
| 187 | final int status = response.getStatus(); |
| 188 | |
| 189 | assertThat(status, is(204)); |
| 190 | |
| 191 | verify(mockK8sNodeAdminService); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Tests the results of the REST API DELETE method without deleting the nodes. |
| 196 | */ |
| 197 | @Test |
| 198 | public void testDeleteNodesWithoutDeletionOperation() { |
| 199 | expect(mockK8sNodeAdminService.node(anyString())).andReturn(null).once(); |
| 200 | replay(mockK8sNodeAdminService); |
| 201 | |
| 202 | String location = PATH + "/minion-node"; |
| 203 | |
| 204 | final WebTarget wt = target(); |
| 205 | Response response = wt.path(location).request( |
| 206 | MediaType.APPLICATION_JSON_TYPE).delete(); |
| 207 | |
| 208 | final int status = response.getStatus(); |
| 209 | |
| 210 | assertThat(status, is(304)); |
| 211 | |
| 212 | verify(mockK8sNodeAdminService); |
| 213 | } |
| 214 | } |