Skip to content

Commit 80d42d4

Browse files
committed
NSX : Unit tests
1 parent 2b896a3 commit 80d42d4

File tree

4 files changed

+175
-11
lines changed

4 files changed

+175
-11
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,36 @@
1717

1818
package org.apache.cloudstack.api.command.admin.offering;
1919

20+
import com.cloud.configuration.ConfigurationService;
21+
import com.cloud.host.Host;
22+
import com.cloud.offering.NetworkOffering;
23+
import junit.framework.TestCase;
24+
import org.apache.cloudstack.api.ResponseGenerator;
2025
import org.apache.cloudstack.api.command.admin.network.CreateNetworkOfferingCmd;
26+
import org.apache.cloudstack.api.response.HostResponse;
27+
import org.apache.cloudstack.api.response.ListResponse;
28+
import org.apache.cloudstack.api.response.NetworkOfferingResponse;
2129
import org.junit.Assert;
30+
import org.junit.Before;
2231
import org.junit.Test;
2332
import org.mockito.InjectMocks;
33+
import org.mockito.Mock;
34+
import org.mockito.Mockito;
2435
import org.springframework.test.util.ReflectionTestUtils;
2536

26-
public class CreateNetworkOfferingCmdTest {
37+
import java.util.Arrays;
38+
import java.util.List;
39+
40+
41+
public class CreateNetworkOfferingCmdTest extends TestCase {
2742

2843
@InjectMocks
2944
private CreateNetworkOfferingCmd createNetworkOfferingCmd = new CreateNetworkOfferingCmd();
3045

46+
String netName = "network";
47+
3148
@Test
3249
public void createVpcNtwkOffWithEmptyDisplayText() {
33-
String netName = "network";
3450
ReflectionTestUtils.setField(createNetworkOfferingCmd, "networkOfferingName", netName);
3551
Assert.assertEquals(createNetworkOfferingCmd.getDisplayText(), netName);
3652
}

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: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,46 @@
1616
// under the License.
1717
package com.cloud.configuration;
1818

19+
import com.cloud.capacity.dao.CapacityDao;
20+
import com.cloud.dc.DataCenterIpAddressVO;
21+
import com.cloud.dc.DataCenterVO;
22+
import com.cloud.dc.HostPodVO;
23+
import com.cloud.dc.VlanVO;
24+
import com.cloud.dc.dao.DataCenterDao;
25+
import com.cloud.dc.dao.DataCenterIpAddressDao;
26+
import com.cloud.dc.dao.DedicatedResourceDao;
27+
import com.cloud.dc.dao.HostPodDao;
28+
import com.cloud.dc.dao.VlanDao;
1929
import com.cloud.exception.InvalidParameterValueException;
30+
import com.cloud.host.HostVO;
31+
import com.cloud.host.dao.HostDao;
32+
import com.cloud.network.Network;
33+
import com.cloud.network.NetworkModel;
34+
import com.cloud.network.NetworkService;
35+
import com.cloud.network.Networks;
36+
import com.cloud.network.dao.IPAddressDao;
37+
import com.cloud.network.dao.NsxProviderDao;
38+
import com.cloud.network.dao.PhysicalNetworkDao;
39+
import com.cloud.network.dao.PhysicalNetworkVO;
40+
import com.cloud.network.element.NsxProviderVO;
41+
import com.cloud.offering.NetworkOffering;
42+
import com.cloud.offerings.NetworkOfferingVO;
43+
import com.cloud.offerings.dao.NetworkOfferingDao;
2044
import com.cloud.storage.StorageManager;
45+
import com.cloud.storage.VolumeVO;
46+
import com.cloud.storage.dao.VMTemplateZoneDao;
47+
import com.cloud.storage.dao.VolumeDao;
2148
import com.cloud.utils.net.NetUtils;
49+
import com.cloud.vm.VMInstanceVO;
50+
import com.cloud.vm.dao.VMInstanceDao;
51+
import org.apache.cloudstack.annotation.dao.AnnotationDao;
52+
import org.apache.cloudstack.api.command.admin.network.CreateNetworkOfferingCmd;
53+
import org.apache.cloudstack.api.command.admin.zone.DeleteZoneCmd;
54+
import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
2255
import org.apache.cloudstack.framework.config.ConfigDepot;
2356
import org.apache.cloudstack.framework.config.ConfigKey;
57+
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
58+
import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
2459
import org.junit.Assert;
2560
import org.junit.Before;
2661
import org.junit.Test;
@@ -29,19 +64,88 @@
2964
import org.mockito.MockedStatic;
3065
import org.mockito.Mockito;
3166
import org.mockito.junit.MockitoJUnitRunner;
67+
import org.springframework.test.util.ReflectionTestUtils;
3268

69+
import java.util.Collections;
3370
import java.util.List;
3471

72+
import static org.mockito.ArgumentMatchers.any;
73+
import static org.mockito.ArgumentMatchers.anyLong;
74+
import static org.mockito.Mockito.*;
75+
3576

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

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

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

0 commit comments

Comments
 (0)