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