Skip to content

Commit 7fa33a0

Browse files
authored
NSX: Add more unit tests (#8381)
* NSX : Unit tests * remove unused imports * remove unused import causing build failure * fix build failures due to unused imports * fix build failure * fix test assertion * remove unused imports * remove unused import
1 parent 2b896a3 commit 7fa33a0

File tree

7 files changed

+357
-11
lines changed

7 files changed

+357
-11
lines changed

api/src/test/java/org/apache/cloudstack/api/command/admin/offering/CreateNetworkOfferingCmdTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@
2323
import org.mockito.InjectMocks;
2424
import org.springframework.test.util.ReflectionTestUtils;
2525

26+
2627
public class CreateNetworkOfferingCmdTest {
2728

2829
@InjectMocks
2930
private CreateNetworkOfferingCmd createNetworkOfferingCmd = new CreateNetworkOfferingCmd();
3031

32+
String netName = "network";
33+
3134
@Test
3235
public void createVpcNtwkOffWithEmptyDisplayText() {
33-
String netName = "network";
3436
ReflectionTestUtils.setField(createNetworkOfferingCmd, "networkOfferingName", netName);
3537
Assert.assertEquals(createNetworkOfferingCmd.getDisplayText(), netName);
3638
}

plugins/network-elements/nsx/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
<version>${cs.jacoco-plugin.version}</version>
3838
<configuration>
3939
<excludes>
40-
<exclude>**/org/apache/cloudstack/agent/api/**</exclude>
41-
<exclude>**/org/apache/cloudstack/api/response/**</exclude>
40+
<exclude>**/org/apache/cloudstack/agent/api/*</exclude>
41+
<exclude>**/org/apache/cloudstack/api/response/*</exclude>
4242
</excludes>
4343
</configuration>
4444
</plugin>

plugins/network-elements/nsx/src/test/java/org/apache/cloudstack/service/NsxGuestNetworkGuruTest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,7 @@ public void testAllocateForUserVM() throws InsufficientVirtualNetworkCapacityExc
249249
when(network.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
250250
when(vmProfile.getVirtualMachine()).thenReturn(virtualMachine);
251251
when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.User);
252-
// when(network.getId()).thenReturn(2L);
253-
// when(offering.getId()).thenReturn(11L);
254252
when(networkModel.getNetworkIp4Dns(any(Network.class), nullable(DataCenter.class))).thenReturn(dns);
255-
// when(networkModel.getNextAvailableMacAddressInNetwork(anyLong())).thenReturn(macAddress);
256253
when(nicProfile.getMacAddress()).thenReturn(macAddress);
257254
when(networkOfferingDao.isIpv6Supported(anyLong())).thenReturn(false);
258255

@@ -274,15 +271,11 @@ public void testAllocateForDomainRouter() throws InsufficientVirtualNetworkCapac
274271
when(vmProfile.getVirtualMachine()).thenReturn(virtualMachine);
275272
when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.DomainRouter);
276273
when(network.getId()).thenReturn(2L);
277-
// when(offering.getId()).thenReturn(11L);
278-
// when(networkModel.getNetworkIp4Dns(any(Network.class), nullable(DataCenter.class))).thenReturn(dns);
279-
// when(networkModel.getNextAvailableMacAddressInNetwork(anyLong())).thenReturn(macAddress);
280274
when(nicProfile.getMacAddress()).thenReturn(macAddress);
281275
when(networkOfferingDao.isIpv6Supported(anyLong())).thenReturn(false);
282276
when(network.getDataCenterId()).thenReturn(1L);
283277
when(network.getAccountId()).thenReturn(5L);
284278
when(network.getVpcId()).thenReturn(51L);
285-
// when(account.getDomainId()).thenReturn(2L);
286279
when(dcDao.findById(anyLong())).thenReturn(Mockito.mock(DataCenterVO.class));
287280
when(accountDao.findById(anyLong())).thenReturn(Mockito.mock(AccountVO.class));
288281
when(vpcDao.findById(anyLong())).thenReturn(Mockito.mock(VpcVO.class));

server/src/test/java/com/cloud/configuration/ConfigurationManagerImplTest.java

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,40 @@
1616
// under the License.
1717
package com.cloud.configuration;
1818

19+
import com.cloud.capacity.dao.CapacityDao;
20+
import com.cloud.dc.DataCenterVO;
21+
import com.cloud.dc.VlanVO;
22+
import com.cloud.dc.dao.DataCenterDao;
23+
import com.cloud.dc.dao.DataCenterIpAddressDao;
24+
import com.cloud.dc.dao.DedicatedResourceDao;
25+
import com.cloud.dc.dao.HostPodDao;
26+
import com.cloud.dc.dao.VlanDao;
1927
import com.cloud.exception.InvalidParameterValueException;
28+
import com.cloud.host.dao.HostDao;
29+
import com.cloud.network.Network;
30+
import com.cloud.network.NetworkModel;
31+
import com.cloud.network.NetworkService;
32+
import com.cloud.network.Networks;
33+
import com.cloud.network.dao.IPAddressDao;
34+
import com.cloud.network.dao.NsxProviderDao;
35+
import com.cloud.network.dao.PhysicalNetworkDao;
36+
import com.cloud.network.element.NsxProviderVO;
37+
import com.cloud.offering.NetworkOffering;
38+
import com.cloud.offerings.NetworkOfferingVO;
39+
import com.cloud.offerings.dao.NetworkOfferingDao;
2040
import com.cloud.storage.StorageManager;
41+
import com.cloud.storage.dao.VMTemplateZoneDao;
42+
import com.cloud.storage.dao.VolumeDao;
2143
import com.cloud.utils.net.NetUtils;
44+
import com.cloud.vm.dao.VMInstanceDao;
45+
import org.apache.cloudstack.annotation.dao.AnnotationDao;
46+
import org.apache.cloudstack.api.command.admin.network.CreateNetworkOfferingCmd;
47+
import org.apache.cloudstack.api.command.admin.zone.DeleteZoneCmd;
48+
import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
2249
import org.apache.cloudstack.framework.config.ConfigDepot;
2350
import org.apache.cloudstack.framework.config.ConfigKey;
51+
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
52+
import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
2453
import org.junit.Assert;
2554
import org.junit.Before;
2655
import org.junit.Test;
@@ -29,19 +58,97 @@
2958
import org.mockito.MockedStatic;
3059
import org.mockito.Mockito;
3160
import org.mockito.junit.MockitoJUnitRunner;
61+
import org.springframework.test.util.ReflectionTestUtils;
3262

63+
import java.util.Collections;
3364
import java.util.List;
3465

66+
import static org.mockito.ArgumentMatchers.any;
67+
import static org.mockito.ArgumentMatchers.anyLong;
68+
import static org.mockito.Mockito.when;
69+
import static org.mockito.Mockito.lenient;
70+
import static org.mockito.Mockito.anyString;
71+
import static org.mockito.Mockito.anyBoolean;
72+
import static org.mockito.Mockito.doNothing;
73+
import static org.mockito.Mockito.nullable;
74+
import static org.mockito.Mockito.anyMap;
75+
import static org.mockito.Mockito.anyList;
76+
import static org.mockito.Mockito.anyInt;
77+
import static org.mockito.Mockito.verify;
78+
import static org.mockito.Mockito.times;
3579

3680
@RunWith(MockitoJUnitRunner.class)
3781
public class ConfigurationManagerImplTest {
3882
@Mock
3983
ConfigDepot configDepot;
84+
@Mock
85+
NsxProviderDao nsxProviderDao;
86+
@Mock
87+
DataCenterDao zoneDao;
88+
@Mock
89+
HostDao hostDao;
90+
@Mock
91+
HostPodDao podDao;
92+
@Mock
93+
DataCenterIpAddressDao ipAddressDao;
94+
@Mock
95+
IPAddressDao publicIpAddressDao;
96+
@Mock
97+
VMInstanceDao vmInstanceDao;
98+
@Mock
99+
VolumeDao volumeDao;
100+
@Mock
101+
PhysicalNetworkDao physicalNetworkDao;
102+
@Mock
103+
ImageStoreDao imageStoreDao;
104+
@Mock
105+
VlanDao vlanDao;
106+
@Mock
107+
VMTemplateZoneDao vmTemplateZoneDao;
108+
@Mock
109+
CapacityDao capacityDao;
110+
@Mock
111+
DedicatedResourceDao dedicatedResourceDao;
112+
@Mock
113+
AnnotationDao annotationDao;
114+
@Mock
115+
ConfigurationDao configDao;
116+
@Mock
117+
NetworkOfferingDao networkOfferingDao;
118+
@Mock
119+
NetworkService networkService;
120+
@Mock
121+
NetworkModel networkModel;
122+
40123
ConfigurationManagerImpl configurationManagerImplSpy = Mockito.spy(new ConfigurationManagerImpl());
124+
DeleteZoneCmd deleteZoneCmd;
125+
CreateNetworkOfferingCmd createNetworkOfferingCmd;
41126

42127
@Before
43128
public void setUp() throws Exception {
44129
configurationManagerImplSpy._configDepot = configDepot;
130+
configurationManagerImplSpy.nsxProviderDao = nsxProviderDao;
131+
configurationManagerImplSpy._zoneDao = zoneDao;
132+
configurationManagerImplSpy._hostDao = hostDao;
133+
configurationManagerImplSpy._podDao = podDao;
134+
configurationManagerImplSpy._privateIpAddressDao = ipAddressDao;
135+
configurationManagerImplSpy._publicIpAddressDao = publicIpAddressDao;
136+
configurationManagerImplSpy._vmInstanceDao = vmInstanceDao;
137+
configurationManagerImplSpy._volumeDao = volumeDao;
138+
configurationManagerImplSpy._physicalNetworkDao = physicalNetworkDao;
139+
configurationManagerImplSpy._imageStoreDao = imageStoreDao;
140+
configurationManagerImplSpy._vlanDao = vlanDao;
141+
configurationManagerImplSpy._capacityDao = capacityDao;
142+
configurationManagerImplSpy._dedicatedDao = dedicatedResourceDao;
143+
configurationManagerImplSpy._configDao = configDao;
144+
configurationManagerImplSpy._networkOfferingDao = networkOfferingDao;
145+
configurationManagerImplSpy._networkSvc = networkService;
146+
configurationManagerImplSpy._networkModel = networkModel;
147+
ReflectionTestUtils.setField(configurationManagerImplSpy, "templateZoneDao", vmTemplateZoneDao);
148+
ReflectionTestUtils.setField(configurationManagerImplSpy, "annotationDao", annotationDao);
149+
150+
deleteZoneCmd = Mockito.mock(DeleteZoneCmd.class);
151+
createNetworkOfferingCmd = Mockito.mock(CreateNetworkOfferingCmd.class);
45152
}
46153

47154
@Test
@@ -250,4 +357,55 @@ public void testValidateIpAddressRelatedConfigValuesValidIpRange() {
250357
Mockito.doReturn(key).when(configurationManagerImplSpy._configDepot).get("config.iprange");
251358
configurationManagerImplSpy.validateIpAddressRelatedConfigValues("config.iprange", "192.168.1.1-192.168.1.100");
252359
}
360+
361+
@Test
362+
public void testDeleteZoneInvokesDeleteNsxProviderWhenNSXIsEnabled() {
363+
NsxProviderVO nsxProviderVO = Mockito.mock(NsxProviderVO.class);
364+
DataCenterVO dataCenterVO = Mockito.mock(DataCenterVO.class);
365+
366+
when(nsxProviderDao.findByZoneId(anyLong())).thenReturn(nsxProviderVO);
367+
when(zoneDao.findById(anyLong())).thenReturn(dataCenterVO);
368+
lenient().when(hostDao.findByDataCenterId(anyLong())).thenReturn(Collections.emptyList());
369+
when(podDao.listByDataCenterId(anyLong())).thenReturn(Collections.emptyList());
370+
when(ipAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
371+
when(publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
372+
when(vmInstanceDao.listByZoneId(anyLong())).thenReturn(Collections.emptyList());
373+
when(volumeDao.findByDc(anyLong())).thenReturn(Collections.emptyList());
374+
when(physicalNetworkDao.listByZone(anyLong())).thenReturn(Collections.emptyList());
375+
when(imageStoreDao.findByZone(any(ZoneScope.class), nullable(Boolean.class))).thenReturn(Collections.emptyList());
376+
when(vlanDao.listByZone(anyLong())).thenReturn(List.of(Mockito.mock(VlanVO.class)));
377+
when(nsxProviderVO.getId()).thenReturn(1L);
378+
when(zoneDao.remove(anyLong())).thenReturn(true);
379+
when(capacityDao.removeBy(nullable(Short.class), anyLong(), nullable(Long.class), nullable(Long.class), nullable(Long.class))).thenReturn(true);
380+
when(dedicatedResourceDao.findByZoneId(anyLong())).thenReturn(null);
381+
lenient().when(annotationDao.removeByEntityType(anyString(), anyString())).thenReturn(true);
382+
383+
configurationManagerImplSpy.deleteZone(deleteZoneCmd);
384+
385+
verify(nsxProviderDao, times(1)).remove(anyLong());
386+
}
387+
388+
@Test
389+
public void testCreateNetworkOfferingForNsx() {
390+
NetworkOfferingVO offeringVO = Mockito.mock(NetworkOfferingVO.class);
391+
392+
when(createNetworkOfferingCmd.isForNsx()).thenReturn(true);
393+
when(createNetworkOfferingCmd.getNsxMode()).thenReturn(NetworkOffering.NsxMode.NATTED.name());
394+
when(createNetworkOfferingCmd.getTraffictype()).thenReturn(Networks.TrafficType.Guest.name());
395+
when(createNetworkOfferingCmd.getGuestIpType()).thenReturn(Network.GuestType.Isolated.name());
396+
when(createNetworkOfferingCmd.getAvailability()).thenReturn(NetworkOffering.Availability.Optional.name());
397+
lenient().when(configurationManagerImplSpy.createNetworkOffering(anyString(), anyString(), any(Networks.TrafficType.class), anyString(),
398+
anyBoolean(), any(NetworkOffering.Availability.class), anyInt(), anyMap(), anyBoolean(), any(Network.GuestType.class),
399+
anyBoolean(), anyLong(), anyBoolean(), anyMap(), anyBoolean(), anyBoolean(), anyMap(), anyBoolean(), anyInt(),
400+
anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean(), anyString(), anyList(), anyList(), anyBoolean(), any(NetUtils.InternetProtocol.class)))
401+
.thenReturn(offeringVO);
402+
when(configDao.getValue(anyString())).thenReturn("1000");
403+
lenient().when(networkOfferingDao.persist(any(NetworkOfferingVO.class), anyMap())).thenReturn(offeringVO);
404+
doNothing().when(networkService).validateIfServiceOfferingIsActiveAndSystemVmTypeIsDomainRouter(anyLong());
405+
doNothing().when(networkModel).canProviderSupportServices(anyMap());
406+
407+
NetworkOffering offering = configurationManagerImplSpy.createNetworkOffering(createNetworkOfferingCmd);
408+
409+
Assert.assertNotNull(offering);
410+
}
253411
}

server/src/test/java/com/cloud/network/NetworkModelImplTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,33 @@
1717
package com.cloud.network;
1818

1919
import com.cloud.dc.DataCenter;
20+
import com.cloud.dc.VlanVO;
2021
import com.cloud.exception.InvalidParameterValueException;
22+
import com.cloud.network.addr.PublicIp;
23+
import com.cloud.network.dao.IPAddressVO;
24+
import com.cloud.network.dao.NetworkServiceMapDao;
25+
import com.cloud.network.dao.NetworkServiceMapVO;
26+
import com.cloud.network.dao.NetworkVO;
27+
import com.cloud.network.element.NetworkElement;
28+
import com.cloud.network.element.VpcVirtualRouterElement;
29+
import com.cloud.offerings.NetworkOfferingVO;
30+
import com.cloud.offerings.dao.NetworkOfferingDao;
2131
import com.cloud.utils.Pair;
32+
import com.cloud.utils.net.Ip;
2233
import org.junit.Assert;
34+
import org.junit.Before;
2335
import org.junit.Test;
36+
import org.mockito.ArgumentMatchers;
2437
import org.mockito.InjectMocks;
2538
import org.mockito.Mockito;
39+
import org.springframework.test.util.ReflectionTestUtils;
40+
41+
import java.util.ArrayList;
42+
import java.util.HashMap;
43+
import java.util.HashSet;
44+
import java.util.List;
45+
import java.util.Map;
46+
import java.util.Set;
2647

2748
public class NetworkModelImplTest {
2849
final String[] ip4Dns1 = {"5.5.5.5", "6.6.6.6"};
@@ -33,6 +54,15 @@ public class NetworkModelImplTest {
3354
@InjectMocks
3455
private NetworkModelImpl networkModel = new NetworkModelImpl();
3556

57+
private NetworkOfferingDao networkOfferingDao;
58+
private NetworkServiceMapDao networkServiceMapDao;
59+
@Before
60+
public void setUp() {
61+
networkOfferingDao = Mockito.mock(NetworkOfferingDao.class);
62+
networkServiceMapDao = Mockito.mock(NetworkServiceMapDao.class);
63+
networkModel._networkOfferingDao = networkOfferingDao;
64+
networkModel._ntwkSrvcDao = networkServiceMapDao;
65+
}
3666
private void prepareMocks(boolean isIp6, Network network, DataCenter zone,
3767
String dns1, String dns2, String dns3, String dns4) {
3868
if (isIp6) {
@@ -140,4 +170,35 @@ public void testVerifyIp6DnsPairInvalidDns2Failure() {
140170
public void testVerifyIp6DnsPairValid() {
141171
networkModel.verifyIp6DnsPair(ip6Dns1[0], ip6Dns1[1]);
142172
}
173+
174+
@Test
175+
public void testGetProviderToIpList() {
176+
Set<Network.Service> services1 = new HashSet<>(List.of(Network.Service.Firewall));
177+
Set<Network.Service> services2 = new HashSet<>(List.of(Network.Service.SourceNat));
178+
Ip ip1 = new Ip("10.10.10.10");
179+
Ip ip2 = new Ip("10.10.10.10");
180+
IPAddressVO ipAddressVO1 = new IPAddressVO(ip1, 1L, 0x0ac00000L, 2L, true);
181+
IPAddressVO ipAddressVO2 = new IPAddressVO(ip2, 1L, 0x0ac00000L, 2L, true);
182+
VlanVO vlanVO = new VlanVO();
183+
vlanVO.setNetworkId(15L);
184+
PublicIpAddress publicIpAddress1 = new PublicIp(ipAddressVO1, vlanVO, 0x0ac00000L);
185+
PublicIpAddress publicIpAddress2 = new PublicIp(ipAddressVO2, vlanVO, 0x0ac00000L);
186+
NetworkOfferingVO networkOfferingVO = new NetworkOfferingVO();
187+
networkOfferingVO.setForVpc(true);
188+
networkOfferingVO.setForNsx(false);
189+
Network network = new NetworkVO();
190+
List<NetworkServiceMapVO> networkServiceMapVOs = new ArrayList<>();
191+
networkServiceMapVOs.add(new NetworkServiceMapVO(15L, Network.Service.Firewall, Network.Provider.VPCVirtualRouter));
192+
networkServiceMapVOs.add(new NetworkServiceMapVO(15L, Network.Service.SourceNat, Network.Provider.VPCVirtualRouter));
193+
NetworkElement element = new VpcVirtualRouterElement();
194+
195+
ReflectionTestUtils.setField(networkModel, "networkElements", List.of(element));
196+
Mockito.when(networkOfferingDao.findById(ArgumentMatchers.anyLong())).thenReturn(networkOfferingVO);
197+
Mockito.when(networkServiceMapDao.getServicesInNetwork(ArgumentMatchers.anyLong())).thenReturn(networkServiceMapVOs);
198+
Map<PublicIpAddress, Set<Network.Service>> ipToServices = new HashMap<>();
199+
ipToServices.put(publicIpAddress1, services1);
200+
ipToServices.put(publicIpAddress2, services2);
201+
Map<Network.Provider, ArrayList<PublicIpAddress>> result = networkModel.getProviderToIpList(network, ipToServices);
202+
Assert.assertNotNull(result);
203+
}
143204
}

0 commit comments

Comments
 (0)