Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
NSX : Unit tests
  • Loading branch information
Pearl1594 committed Dec 19, 2023
commit 80d42d4707b62012d4f28b3905565e8565e6c9bb
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,36 @@

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

import com.cloud.configuration.ConfigurationService;
import com.cloud.host.Host;
import com.cloud.offering.NetworkOffering;
import junit.framework.TestCase;
import org.apache.cloudstack.api.ResponseGenerator;
import org.apache.cloudstack.api.command.admin.network.CreateNetworkOfferingCmd;
import org.apache.cloudstack.api.response.HostResponse;
import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.api.response.NetworkOfferingResponse;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.test.util.ReflectionTestUtils;

public class CreateNetworkOfferingCmdTest {
import java.util.Arrays;
import java.util.List;


public class CreateNetworkOfferingCmdTest extends TestCase {

@InjectMocks
private CreateNetworkOfferingCmd createNetworkOfferingCmd = new CreateNetworkOfferingCmd();

String netName = "network";

@Test
public void createVpcNtwkOffWithEmptyDisplayText() {
String netName = "network";
ReflectionTestUtils.setField(createNetworkOfferingCmd, "networkOfferingName", netName);
Assert.assertEquals(createNetworkOfferingCmd.getDisplayText(), netName);
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/network-elements/nsx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
<version>${cs.jacoco-plugin.version}</version>
<configuration>
<excludes>
<exclude>**/org/apache/cloudstack/agent/api/**</exclude>
<exclude>**/org/apache/cloudstack/api/response/**</exclude>
<exclude>**/org/apache/cloudstack/agent/api/*</exclude>
<exclude>**/org/apache/cloudstack/api/response/*</exclude>
</excludes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,7 @@ public void testAllocateForUserVM() throws InsufficientVirtualNetworkCapacityExc
when(network.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
when(vmProfile.getVirtualMachine()).thenReturn(virtualMachine);
when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.User);
// when(network.getId()).thenReturn(2L);
// when(offering.getId()).thenReturn(11L);
when(networkModel.getNetworkIp4Dns(any(Network.class), nullable(DataCenter.class))).thenReturn(dns);
// when(networkModel.getNextAvailableMacAddressInNetwork(anyLong())).thenReturn(macAddress);
when(nicProfile.getMacAddress()).thenReturn(macAddress);
when(networkOfferingDao.isIpv6Supported(anyLong())).thenReturn(false);

Expand All @@ -274,15 +271,11 @@ public void testAllocateForDomainRouter() throws InsufficientVirtualNetworkCapac
when(vmProfile.getVirtualMachine()).thenReturn(virtualMachine);
when(virtualMachine.getType()).thenReturn(VirtualMachine.Type.DomainRouter);
when(network.getId()).thenReturn(2L);
// when(offering.getId()).thenReturn(11L);
// when(networkModel.getNetworkIp4Dns(any(Network.class), nullable(DataCenter.class))).thenReturn(dns);
// when(networkModel.getNextAvailableMacAddressInNetwork(anyLong())).thenReturn(macAddress);
when(nicProfile.getMacAddress()).thenReturn(macAddress);
when(networkOfferingDao.isIpv6Supported(anyLong())).thenReturn(false);
when(network.getDataCenterId()).thenReturn(1L);
when(network.getAccountId()).thenReturn(5L);
when(network.getVpcId()).thenReturn(51L);
// when(account.getDomainId()).thenReturn(2L);
when(dcDao.findById(anyLong())).thenReturn(Mockito.mock(DataCenterVO.class));
when(accountDao.findById(anyLong())).thenReturn(Mockito.mock(AccountVO.class));
when(vpcDao.findById(anyLong())).thenReturn(Mockito.mock(VpcVO.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,46 @@
// under the License.
package com.cloud.configuration;

import com.cloud.capacity.dao.CapacityDao;
import com.cloud.dc.DataCenterIpAddressVO;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.HostPodVO;
import com.cloud.dc.VlanVO;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.dc.dao.DataCenterIpAddressDao;
import com.cloud.dc.dao.DedicatedResourceDao;
import com.cloud.dc.dao.HostPodDao;
import com.cloud.dc.dao.VlanDao;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.network.Network;
import com.cloud.network.NetworkModel;
import com.cloud.network.NetworkService;
import com.cloud.network.Networks;
import com.cloud.network.dao.IPAddressDao;
import com.cloud.network.dao.NsxProviderDao;
import com.cloud.network.dao.PhysicalNetworkDao;
import com.cloud.network.dao.PhysicalNetworkVO;
import com.cloud.network.element.NsxProviderVO;
import com.cloud.offering.NetworkOffering;
import com.cloud.offerings.NetworkOfferingVO;
import com.cloud.offerings.dao.NetworkOfferingDao;
import com.cloud.storage.StorageManager;
import com.cloud.storage.VolumeVO;
import com.cloud.storage.dao.VMTemplateZoneDao;
import com.cloud.storage.dao.VolumeDao;
import com.cloud.utils.net.NetUtils;
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.dao.VMInstanceDao;
import org.apache.cloudstack.annotation.dao.AnnotationDao;
import org.apache.cloudstack.api.command.admin.network.CreateNetworkOfferingCmd;
import org.apache.cloudstack.api.command.admin.zone.DeleteZoneCmd;
import org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope;
import org.apache.cloudstack.framework.config.ConfigDepot;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.cloudstack.storage.datastore.db.ImageStoreDao;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -29,19 +64,88 @@
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.Collections;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.*;


@RunWith(MockitoJUnitRunner.class)
public class ConfigurationManagerImplTest {
@Mock
ConfigDepot configDepot;
@Mock
NsxProviderDao nsxProviderDao;
@Mock
DataCenterDao zoneDao;
@Mock
HostDao hostDao;
@Mock
HostPodDao podDao;
@Mock
DataCenterIpAddressDao ipAddressDao;
@Mock
IPAddressDao publicIpAddressDao;
@Mock
VMInstanceDao vmInstanceDao;
@Mock
VolumeDao volumeDao;
@Mock
PhysicalNetworkDao physicalNetworkDao;
@Mock
ImageStoreDao imageStoreDao;
@Mock
VlanDao vlanDao;
@Mock
VMTemplateZoneDao vmTemplateZoneDao;
@Mock
CapacityDao capacityDao;
@Mock
DedicatedResourceDao dedicatedResourceDao;
@Mock
AnnotationDao annotationDao;
@Mock
ConfigurationDao configDao;
@Mock
NetworkOfferingDao networkOfferingDao;
@Mock
NetworkService networkService;
@Mock
NetworkModel networkModel;

ConfigurationManagerImpl configurationManagerImplSpy = Mockito.spy(new ConfigurationManagerImpl());
DeleteZoneCmd deleteZoneCmd;
CreateNetworkOfferingCmd createNetworkOfferingCmd;

@Before
public void setUp() throws Exception {
configurationManagerImplSpy._configDepot = configDepot;
configurationManagerImplSpy.nsxProviderDao = nsxProviderDao;
configurationManagerImplSpy._zoneDao = zoneDao;
configurationManagerImplSpy._hostDao = hostDao;
configurationManagerImplSpy._podDao = podDao;
configurationManagerImplSpy._privateIpAddressDao = ipAddressDao;
configurationManagerImplSpy._publicIpAddressDao = publicIpAddressDao;
configurationManagerImplSpy._vmInstanceDao = vmInstanceDao;
configurationManagerImplSpy._volumeDao = volumeDao;
configurationManagerImplSpy._physicalNetworkDao = physicalNetworkDao;
configurationManagerImplSpy._imageStoreDao = imageStoreDao;
configurationManagerImplSpy._vlanDao = vlanDao;
configurationManagerImplSpy._capacityDao = capacityDao;
configurationManagerImplSpy._dedicatedDao = dedicatedResourceDao;
configurationManagerImplSpy._configDao = configDao;
configurationManagerImplSpy._networkOfferingDao = networkOfferingDao;
configurationManagerImplSpy._networkSvc = networkService;
configurationManagerImplSpy._networkModel = networkModel;
ReflectionTestUtils.setField(configurationManagerImplSpy, "templateZoneDao", vmTemplateZoneDao);
ReflectionTestUtils.setField(configurationManagerImplSpy, "annotationDao", annotationDao);

deleteZoneCmd = Mockito.mock(DeleteZoneCmd.class);
createNetworkOfferingCmd = Mockito.mock(CreateNetworkOfferingCmd.class);
}

@Test
Expand Down Expand Up @@ -250,4 +354,55 @@ public void testValidateIpAddressRelatedConfigValuesValidIpRange() {
Mockito.doReturn(key).when(configurationManagerImplSpy._configDepot).get("config.iprange");
configurationManagerImplSpy.validateIpAddressRelatedConfigValues("config.iprange", "192.168.1.1-192.168.1.100");
}

@Test
public void testDeleteZoneInvokesDeleteNsxProviderWhenNSXIsEnabled() {
NsxProviderVO nsxProviderVO = Mockito.mock(NsxProviderVO.class);
DataCenterVO dataCenterVO = Mockito.mock(DataCenterVO.class);

when(nsxProviderDao.findByZoneId(anyLong())).thenReturn(nsxProviderVO);
when(zoneDao.findById(anyLong())).thenReturn(dataCenterVO);
when(hostDao.findByDataCenterId(anyLong())).thenReturn(Collections.emptyList());
when(podDao.listByDataCenterId(anyLong())).thenReturn(Collections.emptyList());
when(ipAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
when(publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
when(vmInstanceDao.listByZoneId(anyLong())).thenReturn(Collections.emptyList());
when(volumeDao.findByDc(anyLong())).thenReturn(Collections.emptyList());
when(physicalNetworkDao.listByZone(anyLong())).thenReturn(Collections.emptyList());
when(imageStoreDao.findByZone(any(ZoneScope.class), nullable(Boolean.class))).thenReturn(Collections.emptyList());
when(vlanDao.listByZone(anyLong())).thenReturn(List.of(Mockito.mock(VlanVO.class)));
when(nsxProviderVO.getId()).thenReturn(1L);
when(zoneDao.remove(anyLong())).thenReturn(true);
when(capacityDao.removeBy(nullable(Short.class), anyLong(), nullable(Long.class), nullable(Long.class), nullable(Long.class))).thenReturn(true);
when(dedicatedResourceDao.findByZoneId(anyLong())).thenReturn(null);
when(annotationDao.removeByEntityType(anyString(), anyString())).thenReturn(true);

configurationManagerImplSpy.deleteZone(deleteZoneCmd);

verify(nsxProviderDao, times(1)).remove(anyLong());
}

@Test
public void testCreateNetworkOfferingForNsx() {
NetworkOfferingVO offeringVO = Mockito.mock(NetworkOfferingVO.class);

when(createNetworkOfferingCmd.isForNsx()).thenReturn(true);
when(createNetworkOfferingCmd.getNsxMode()).thenReturn(NetworkOffering.NsxMode.NATTED.name());
when(createNetworkOfferingCmd.getTraffictype()).thenReturn(Networks.TrafficType.Guest.name());
when(createNetworkOfferingCmd.getGuestIpType()).thenReturn(Network.GuestType.Isolated.name());
when(createNetworkOfferingCmd.getAvailability()).thenReturn(NetworkOffering.Availability.Optional.name());
when(configurationManagerImplSpy.createNetworkOffering(anyString(), anyString(), any(Networks.TrafficType.class), anyString(),
anyBoolean(), any(NetworkOffering.Availability.class), anyInt(), anyMap(), anyBoolean(), any(Network.GuestType.class),
anyBoolean(), anyLong(), anyBoolean(), anyMap(), anyBoolean(), anyBoolean(), anyMap(), anyBoolean(), anyInt(),
anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean(), anyString(), anyList(), anyList(), anyBoolean(), any(NetUtils.InternetProtocol.class)))
.thenReturn(offeringVO);
when(configDao.getValue(anyString())).thenReturn("1000");
when(networkOfferingDao.persist(any(NetworkOfferingVO.class), anyMap())).thenReturn(offeringVO);
doNothing().when(networkService).validateIfServiceOfferingIsActiveAndSystemVmTypeIsDomainRouter(anyLong());
doNothing().when(networkModel).canProviderSupportServices(anyMap());

NetworkOffering offering = configurationManagerImplSpy.createNetworkOffering(createNetworkOfferingCmd);

Assert.assertNotNull(offering);
}
}