Jian Li | 9b19916 | 2019-02-10 18:00:35 +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.k8snetworking.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.k8snetworking.api.DefaultK8sNetwork; |
| 27 | import org.onosproject.k8snetworking.api.K8sIpam; |
| 28 | import org.onosproject.k8snetworking.api.K8sIpamAdminService; |
| 29 | import org.onosproject.k8snetworking.api.K8sNetwork; |
| 30 | import org.onosproject.k8snetworking.api.K8sNetworkService; |
| 31 | import org.onosproject.k8snetworking.codec.K8sIpamCodec; |
| 32 | import org.onosproject.rest.resources.ResourceTest; |
| 33 | |
| 34 | import javax.ws.rs.client.WebTarget; |
| 35 | import javax.ws.rs.core.Response; |
| 36 | |
| 37 | import static junit.framework.TestCase.assertEquals; |
| 38 | import static org.easymock.EasyMock.anyObject; |
| 39 | import static org.easymock.EasyMock.createMock; |
| 40 | import static org.easymock.EasyMock.expect; |
| 41 | import static org.easymock.EasyMock.replay; |
| 42 | import static org.easymock.EasyMock.verify; |
| 43 | |
| 44 | /** |
| 45 | * Unit test for kubernetes IPAM REST API. |
| 46 | */ |
| 47 | public class K8sIpamWebResourceTest extends ResourceTest { |
| 48 | |
| 49 | final K8sNetworkService mockNetworkService = createMock(K8sNetworkService.class); |
| 50 | final K8sIpamAdminService mockIpamService = createMock(K8sIpamAdminService.class); |
| 51 | |
| 52 | private static final String IPAM = "ipam"; |
| 53 | |
| 54 | private K8sNetwork k8sNetwork; |
| 55 | |
| 56 | /** |
| 57 | * Constructs a kubernetes networking resource test instance. |
| 58 | */ |
| 59 | public K8sIpamWebResourceTest() { |
| 60 | super(ResourceConfig.forApplicationClass(K8sNetworkingWebApplication.class)); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Sets up the global values for all the tests. |
| 65 | */ |
| 66 | @Before |
| 67 | public void setUpTest() { |
| 68 | final CodecManager codecService = new CodecManager(); |
| 69 | codecService.activate(); |
| 70 | codecService.registerCodec(K8sIpam.class, new K8sIpamCodec()); |
| 71 | ServiceDirectory testDirectory = |
| 72 | new TestServiceDirectory() |
| 73 | .add(K8sNetworkService.class, mockNetworkService) |
| 74 | .add(K8sIpamAdminService.class, mockIpamService) |
| 75 | .add(CodecService.class, codecService); |
| 76 | setServiceDirectory(testDirectory); |
| 77 | |
| 78 | k8sNetwork = DefaultK8sNetwork.builder() |
| 79 | .networkId("sona-network") |
| 80 | .name("sona-network") |
| 81 | .segmentId("1") |
| 82 | .cidr("10.10.10.0/24") |
| 83 | .gatewayIp(IpAddress.valueOf("10.10.10.1")) |
| 84 | .type(K8sNetwork.Type.VXLAN) |
| 85 | .mtu(1500) |
| 86 | .build(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Tests the IP allocation with incorrect network ID. |
| 91 | */ |
| 92 | @Test |
| 93 | public void testAllocateIpWithIncorrectNetId() { |
| 94 | expect(mockNetworkService.network(anyObject())).andReturn(null); |
| 95 | |
| 96 | replay(mockNetworkService); |
| 97 | |
| 98 | final WebTarget wt = target(); |
| 99 | Response response = wt.path(IPAM + "/sona-network").request().get(); |
| 100 | final int status = response.getStatus(); |
| 101 | |
| 102 | assertEquals(404, status); |
| 103 | |
| 104 | verify(mockNetworkService); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Tests the IP allocation with null IP address returned. |
| 109 | */ |
| 110 | @Test |
| 111 | public void testAllocateIpWithNullIp() { |
| 112 | expect(mockNetworkService.network(anyObject())).andReturn(k8sNetwork); |
| 113 | expect(mockIpamService.allocateIp(anyObject())).andReturn(null); |
| 114 | |
| 115 | replay(mockNetworkService); |
| 116 | replay(mockIpamService); |
| 117 | |
| 118 | final WebTarget wt = target(); |
| 119 | Response response = wt.path(IPAM + "/sona-network").request().get(); |
| 120 | final int status = response.getStatus(); |
| 121 | |
| 122 | assertEquals(404, status); |
| 123 | |
| 124 | verify(mockNetworkService); |
| 125 | verify(mockIpamService); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Tests the IP allocation with correct IP address returned. |
| 130 | */ |
| 131 | @Test |
| 132 | public void testAllocateIp() { |
| 133 | expect(mockNetworkService.network(anyObject())).andReturn(k8sNetwork); |
| 134 | expect(mockIpamService.allocateIp(anyObject())) |
| 135 | .andReturn(IpAddress.valueOf("10.10.10.2")); |
| 136 | |
| 137 | replay(mockNetworkService); |
| 138 | replay(mockIpamService); |
| 139 | |
| 140 | final WebTarget wt = target(); |
| 141 | Response response = wt.path(IPAM + "/sona-network").request().get(); |
| 142 | final int status = response.getStatus(); |
| 143 | |
| 144 | assertEquals(200, status); |
| 145 | |
| 146 | verify(mockNetworkService); |
| 147 | verify(mockIpamService); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Tests the IP allocation with incorrect network ID. |
| 152 | */ |
| 153 | @Test |
| 154 | public void testReleaseIpWithIncorrectNetIdAndIp() { |
| 155 | expect(mockNetworkService.network(anyObject())).andReturn(null); |
| 156 | |
| 157 | replay(mockNetworkService); |
| 158 | |
| 159 | final WebTarget wt = target(); |
| 160 | Response response = wt.path(IPAM + "/sona-network/10.10.10.2").request().delete(); |
| 161 | final int status = response.getStatus(); |
| 162 | |
| 163 | assertEquals(404, status); |
| 164 | |
| 165 | verify(mockNetworkService); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Tests the IP allocation with correct network ID and IP address. |
| 170 | */ |
| 171 | @Test |
| 172 | public void testReleaseIpWithCorrectNetIdAndIp() { |
| 173 | expect(mockNetworkService.network(anyObject())).andReturn(k8sNetwork); |
| 174 | expect(mockIpamService.releaseIp(anyObject(), anyObject())).andReturn(true); |
| 175 | |
| 176 | replay(mockNetworkService); |
| 177 | replay(mockIpamService); |
| 178 | |
| 179 | final WebTarget wt = target(); |
| 180 | Response response = wt.path(IPAM + "/sona-network/10.10.10.2").request().delete(); |
| 181 | final int status = response.getStatus(); |
| 182 | |
| 183 | assertEquals(204, status); |
| 184 | |
| 185 | verify(mockNetworkService); |
| 186 | verify(mockIpamService); |
| 187 | } |
| 188 | } |