|
16 | 16 | // under the License. |
17 | 17 | package org.apache.cloudstack.service; |
18 | 18 |
|
| 19 | +import com.cloud.api.ApiDBUtils; |
19 | 20 | import com.cloud.dc.DataCenter; |
20 | 21 | import com.cloud.dc.DataCenterVO; |
21 | 22 | import com.cloud.dc.dao.DataCenterDao; |
22 | 23 | import com.cloud.deploy.DeployDestination; |
23 | 24 | import com.cloud.deploy.DeploymentPlan; |
24 | 25 | import com.cloud.domain.DomainVO; |
25 | 26 | import com.cloud.domain.dao.DomainDao; |
| 27 | +import com.cloud.exception.InsufficientAddressCapacityException; |
| 28 | +import com.cloud.exception.InsufficientVirtualNetworkCapacityException; |
| 29 | +import com.cloud.network.IpAddressManager; |
26 | 30 | import com.cloud.network.Network; |
27 | 31 | import com.cloud.network.NetworkModel; |
28 | 32 | import com.cloud.network.Networks; |
| 33 | +import com.cloud.network.PhysicalNetwork; |
29 | 34 | import com.cloud.network.dao.NetworkDao; |
30 | 35 | import com.cloud.network.dao.NetworkVO; |
31 | 36 | import com.cloud.network.dao.PhysicalNetworkDao; |
32 | 37 | import com.cloud.network.dao.PhysicalNetworkVO; |
| 38 | +import com.cloud.network.guru.GuestNetworkGuru; |
| 39 | +import com.cloud.network.vpc.NetworkACLItem; |
33 | 40 | import com.cloud.network.vpc.VpcVO; |
34 | 41 | import com.cloud.network.vpc.dao.VpcDao; |
35 | 42 | import com.cloud.offering.NetworkOffering; |
| 43 | +import com.cloud.offerings.dao.NetworkOfferingDao; |
36 | 44 | import com.cloud.offerings.dao.NetworkOfferingServiceMapDao; |
37 | 45 | import com.cloud.user.Account; |
38 | 46 | import com.cloud.user.AccountVO; |
39 | 47 | import com.cloud.user.dao.AccountDao; |
| 48 | +import com.cloud.utils.Pair; |
| 49 | +import com.cloud.vm.Nic; |
| 50 | +import com.cloud.vm.NicProfile; |
40 | 51 | import com.cloud.vm.ReservationContext; |
| 52 | +import com.cloud.vm.VirtualMachine; |
| 53 | +import com.cloud.vm.VirtualMachineProfile; |
41 | 54 | import org.apache.cloudstack.NsxAnswer; |
| 55 | +import org.apache.cloudstack.agent.api.CreateNsxDhcpRelayConfigCommand; |
42 | 56 | import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand; |
| 57 | +import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand; |
43 | 58 | import org.apache.cloudstack.agent.api.NsxCommand; |
44 | 59 | import org.apache.cloudstack.utils.NsxControllerUtils; |
45 | 60 | import org.junit.After; |
|
53 | 68 | import org.mockito.junit.MockitoJUnitRunner; |
54 | 69 | import org.springframework.test.util.ReflectionTestUtils; |
55 | 70 |
|
| 71 | +import java.lang.reflect.Field; |
| 72 | +import java.lang.reflect.InvocationTargetException; |
| 73 | +import java.lang.reflect.Method; |
56 | 74 | import java.util.List; |
57 | 75 |
|
58 | 76 | import static org.junit.Assert.assertTrue; |
59 | 77 | import static org.junit.Assert.assertEquals; |
60 | 78 | import static org.junit.Assert.assertSame; |
61 | 79 | import static org.junit.Assert.assertFalse; |
62 | 80 | import static org.junit.Assert.assertNotNull; |
63 | | -import static org.mockito.ArgumentMatchers.any; |
64 | | -import static org.mockito.ArgumentMatchers.anyLong; |
| 81 | +import static org.mockito.ArgumentMatchers.*; |
65 | 82 | import static org.mockito.Mockito.when; |
66 | 83 | import static org.mockito.Mockito.verify; |
67 | 84 | import static org.mockito.Mockito.mock; |
@@ -103,19 +120,27 @@ public class NsxGuestNetworkGuruTest { |
103 | 120 | DomainDao domainDao; |
104 | 121 | @Mock |
105 | 122 | NetworkDao networkDao; |
| 123 | + @Mock |
| 124 | + IpAddressManager ipAddressManager; |
| 125 | + @Mock |
| 126 | + NetworkOfferingDao networkOfferingDao; |
106 | 127 |
|
107 | 128 | NsxGuestNetworkGuru guru; |
| 129 | + GuestNetworkGuru guestNetworkGuru; |
108 | 130 | AutoCloseable closeable; |
109 | 131 |
|
110 | 132 | @Before |
111 | | - public void setUp() { |
| 133 | + public void setUp() throws IllegalAccessException, NoSuchFieldException { |
112 | 134 | closeable = MockitoAnnotations.openMocks(this); |
113 | 135 | guru = new NsxGuestNetworkGuru(); |
114 | | - ReflectionTestUtils.setField(guru, "_physicalNetworkDao", physicalNetworkDao); |
| 136 | + |
115 | 137 | ReflectionTestUtils.setField(guru, "_dcDao", dcDao); |
116 | 138 | ReflectionTestUtils.setField(guru, "_networkDao", networkDao); |
117 | 139 | ReflectionTestUtils.setField(guru, "_networkModel", networkModel); |
118 | 140 | ReflectionTestUtils.setField(guru, "_vpcDao", vpcDao); |
| 141 | + ReflectionTestUtils.setField((GuestNetworkGuru) guru, "_ipAddrMgr", ipAddressManager); |
| 142 | + ReflectionTestUtils.setField((GuestNetworkGuru) guru, "_networkModel", networkModel); |
| 143 | + ReflectionTestUtils.setField((GuestNetworkGuru) guru, "networkOfferingDao", networkOfferingDao); |
119 | 144 |
|
120 | 145 | guru.networkOfferingServiceMapDao = networkOfferingServiceMapDao; |
121 | 146 | guru.nsxControllerUtils = nsxControllerUtils; |
@@ -215,4 +240,97 @@ public void testNsxNetworkImplementation() { |
215 | 240 | assertEquals(4L, implemented.getVpcId().longValue()); |
216 | 241 | assertFalse(implemented.isRedundant()); |
217 | 242 | } |
| 243 | + |
| 244 | + @Test |
| 245 | + public void testAllocateForUserVM() throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException { |
| 246 | + Network network = Mockito.mock(Network.class); |
| 247 | + NicProfile nicProfile = Mockito.mock(NicProfile.class); |
| 248 | + VirtualMachineProfile vmProfile = Mockito.mock(VirtualMachineProfile.class); |
| 249 | + VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class); |
| 250 | + Pair<String, String> dns = new Pair<>("10.1.5.1", "8.8.8.8"); |
| 251 | + String macAddress = "00:00:00:11:1D:1E:CD"; |
| 252 | + |
| 253 | + when(network.getTrafficType()).thenReturn(Networks.TrafficType.Guest); |
| 254 | + when(vmProfile.getVirtualMachine()).thenReturn(virtualMachine); |
| 255 | + when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.User); |
| 256 | + when(network.getId()).thenReturn(2L); |
| 257 | + when(offering.getId()).thenReturn(11L); |
| 258 | + when(networkModel.getNetworkIp4Dns(any(Network.class), nullable(DataCenter.class))).thenReturn(dns); |
| 259 | + when(networkModel.getNextAvailableMacAddressInNetwork(anyLong())).thenReturn(macAddress); |
| 260 | + when(nicProfile.getMacAddress()).thenReturn(macAddress); |
| 261 | + when(networkOfferingDao.isIpv6Supported(anyLong())).thenReturn(false); |
| 262 | + |
| 263 | + NicProfile profile = guru.allocate(network, nicProfile, vmProfile); |
| 264 | + assertNotNull(profile); |
| 265 | + } |
| 266 | + |
| 267 | + @Test |
| 268 | + public void testAllocateForDomainRouter() throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException { |
| 269 | + Network network = Mockito.mock(Network.class); |
| 270 | + NicProfile nicProfile = Mockito.mock(NicProfile.class); |
| 271 | + VirtualMachineProfile vmProfile = Mockito.mock(VirtualMachineProfile.class); |
| 272 | + VirtualMachine virtualMachine = Mockito.mock(VirtualMachine.class); |
| 273 | + Pair<String, String> dns = new Pair<>("10.1.5.1", "8.8.8.8"); |
| 274 | + String macAddress = "00:00:00:11:1D:1E:CD"; |
| 275 | + |
| 276 | + when(network.getTrafficType()).thenReturn(Networks.TrafficType.Guest); |
| 277 | + when(vmProfile.getType()).thenReturn(VirtualMachine.Type.DomainRouter); |
| 278 | + when(vmProfile.getVirtualMachine()).thenReturn(virtualMachine); |
| 279 | + when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.DomainRouter); |
| 280 | + when(network.getId()).thenReturn(2L); |
| 281 | + when(offering.getId()).thenReturn(11L); |
| 282 | + when(networkModel.getNetworkIp4Dns(any(Network.class), nullable(DataCenter.class))).thenReturn(dns); |
| 283 | + when(networkModel.getNextAvailableMacAddressInNetwork(anyLong())).thenReturn(macAddress); |
| 284 | + when(nicProfile.getMacAddress()).thenReturn(macAddress); |
| 285 | + when(networkOfferingDao.isIpv6Supported(anyLong())).thenReturn(false); |
| 286 | + when(network.getDataCenterId()).thenReturn(1L); |
| 287 | + when(network.getAccountId()).thenReturn(5L); |
| 288 | + when(network.getVpcId()).thenReturn(51L); |
| 289 | + when(account.getDomainId()).thenReturn(2L); |
| 290 | + when(dcDao.findById(anyLong())).thenReturn(Mockito.mock(DataCenterVO.class)); |
| 291 | + when(accountDao.findById(anyLong())).thenReturn(Mockito.mock(AccountVO.class)); |
| 292 | + when(vpcDao.findById(anyLong())).thenReturn(Mockito.mock(VpcVO.class)); |
| 293 | + when(domainDao.findById(anyLong())).thenReturn(Mockito.mock(DomainVO.class)); |
| 294 | + when(nicProfile.getIPv4Address()).thenReturn("10.1.13.10"); |
| 295 | + when(nsxControllerUtils.sendNsxCommand(any(CreateNsxDhcpRelayConfigCommand.class), |
| 296 | + anyLong())).thenReturn(new NsxAnswer(new NsxCommand(), true, "")); |
| 297 | + |
| 298 | + NicProfile profile = guru.allocate(network, nicProfile, vmProfile); |
| 299 | + |
| 300 | + assertNotNull(profile); |
| 301 | + verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateNsxDhcpRelayConfigCommand.class), |
| 302 | + anyLong()); |
| 303 | + } |
| 304 | + |
| 305 | + @Test |
| 306 | + public void testCreateNsxSegmentForVpc() { |
| 307 | + NetworkVO networkVO = Mockito.mock(NetworkVO.class); |
| 308 | + DataCenter dataCenter = Mockito.mock(DataCenter.class); |
| 309 | + |
| 310 | + when(networkVO.getAccountId()).thenReturn(1L); |
| 311 | + when(nsxControllerUtils.sendNsxCommand(any(CreateNsxSegmentCommand.class), |
| 312 | + anyLong())).thenReturn(new NsxAnswer(new NsxCommand(), true, "")); |
| 313 | + guru.createNsxSegment(networkVO, dataCenter); |
| 314 | + verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateNsxSegmentCommand.class), |
| 315 | + anyLong()); |
| 316 | + } |
| 317 | + |
| 318 | + |
| 319 | + @Test |
| 320 | + public void testCreateNsxSegmentForIsolatedNetwork() { |
| 321 | + NetworkVO networkVO = Mockito.mock(NetworkVO.class); |
| 322 | + DataCenter dataCenter = Mockito.mock(DataCenter.class); |
| 323 | + |
| 324 | + when(networkVO.getAccountId()).thenReturn(1L); |
| 325 | + when(networkVO.getVpcId()).thenReturn(null); |
| 326 | + when(nsxControllerUtils.sendNsxCommand(any(CreateNsxTier1GatewayCommand.class), |
| 327 | + anyLong())).thenReturn(new NsxAnswer(new NsxCommand(), true, "")); |
| 328 | + when(nsxControllerUtils.sendNsxCommand(any(CreateNsxSegmentCommand.class), |
| 329 | + anyLong())).thenReturn(new NsxAnswer(new NsxCommand(), true, "")); |
| 330 | + guru.createNsxSegment(networkVO, dataCenter); |
| 331 | + verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateNsxTier1GatewayCommand.class), |
| 332 | + anyLong()); |
| 333 | + verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateNsxSegmentCommand.class), |
| 334 | + anyLong()); |
| 335 | + } |
218 | 336 | } |
0 commit comments