mirror of
https://github.com/apache/cloudstack.git
synced 2025-11-02 11:52:28 +01:00
Nsx unit tests (#8090)
* Add tests * add test for NsxGuestNetworkGuru * add unit tests for NsxResource * add unti tests for NsxElement * cleanup * [NSX] Refactor API wrapper operations * update tests * update tests - add nsxProviderServiceImpl test * add unit test - NsxServiceImpl * add license * Big refactor * Address review comment * change network cidr to cidr to prevent NPE * add domain and zone names to the various networks - vpc & tier * fix tests --------- Co-authored-by: nvazquez <nicovazquez90@gmail.com>
This commit is contained in:
parent
3f8f9e294b
commit
8337486fdc
@ -1474,7 +1474,7 @@ public class VirtualMachineManagerImpl extends ManagerBase implements VirtualMac
|
||||
}
|
||||
}
|
||||
|
||||
private void setVmNetworkDetails(VMInstanceVO vm, VirtualMachineTO vmTO) {
|
||||
public void setVmNetworkDetails(VMInstanceVO vm, VirtualMachineTO vmTO) {
|
||||
if (VirtualMachine.Type.User.equals(vm.getType())) {
|
||||
List<UserVmJoinVO> userVmJoinVOs = userVmJoinDao.searchByIds(vm.getId());
|
||||
Map<Long, String> networkToNetworkNameMap = new HashMap<>();
|
||||
|
||||
@ -35,6 +35,18 @@ import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.cloud.agent.api.to.VirtualMachineTO;
|
||||
import com.cloud.api.query.vo.UserVmJoinVO;
|
||||
import com.cloud.dc.DataCenterVO;
|
||||
import com.cloud.dc.dao.DataCenterDao;
|
||||
import com.cloud.domain.DomainVO;
|
||||
import com.cloud.domain.dao.DomainDao;
|
||||
import com.cloud.network.dao.NetworkDao;
|
||||
import com.cloud.network.dao.NetworkVO;
|
||||
import com.cloud.network.vpc.VpcVO;
|
||||
import com.cloud.network.vpc.dao.VpcDao;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.user.dao.AccountDao;
|
||||
import org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator;
|
||||
import org.apache.cloudstack.framework.config.ConfigKey;
|
||||
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
|
||||
@ -155,6 +167,16 @@ public class VirtualMachineManagerImplTest {
|
||||
private UserVmDao userVmDaoMock;
|
||||
@Mock
|
||||
private UserVmVO userVmMock;
|
||||
@Mock
|
||||
private NetworkDao networkDao;
|
||||
@Mock
|
||||
private AccountDao accountDao;
|
||||
@Mock
|
||||
private DomainDao domainDao;
|
||||
@Mock
|
||||
private DataCenterDao dcDao;
|
||||
@Mock
|
||||
private VpcDao vpcDao;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
@ -895,4 +917,41 @@ public class VirtualMachineManagerImplTest {
|
||||
map.put(Mockito.mock(Volume.class), pool2);
|
||||
virtualMachineManagerImpl.checkAndAttemptMigrateVmAcrossCluster(vm, destinationClusterId, map);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkIfVmNetworkDetailsReturnedIsCorrect() {
|
||||
VMInstanceVO vm = new VMInstanceVO(1L, 1L, "VM1", "i-2-2-VM",
|
||||
VirtualMachine.Type.User, 1L, HypervisorType.KVM, 1L, 1L, 1L,
|
||||
1L, false, false);
|
||||
|
||||
VirtualMachineTO vmTO = new VirtualMachineTO() {};
|
||||
UserVmJoinVO userVm = new UserVmJoinVO();
|
||||
NetworkVO networkVO = new NetworkVO();
|
||||
AccountVO accountVO = new AccountVO();
|
||||
DomainVO domainVO = new DomainVO();
|
||||
domainVO.setName("testDomain");
|
||||
DataCenterVO dataCenterVO = mock(DataCenterVO.class);
|
||||
VpcVO vpcVO = new VpcVO();
|
||||
|
||||
networkVO.setAccountId(1L);
|
||||
networkVO.setName("testNet");
|
||||
networkVO.setVpcId(1L);
|
||||
|
||||
accountVO.setAccountName("testAcc");
|
||||
|
||||
vpcVO.setName("VPC1");
|
||||
|
||||
|
||||
List<UserVmJoinVO> userVms = List.of(userVm);
|
||||
Mockito.when(userVmJoinDaoMock.searchByIds(anyLong())).thenReturn(userVms);
|
||||
Mockito.when(networkDao.findById(anyLong())).thenReturn(networkVO);
|
||||
Mockito.when(accountDao.findById(anyLong())).thenReturn(accountVO);
|
||||
Mockito.when(domainDao.findById(anyLong())).thenReturn(domainVO);
|
||||
Mockito.when(dcDao.findById(anyLong())).thenReturn(dataCenterVO);
|
||||
Mockito.when(vpcDao.findById(anyLong())).thenReturn(vpcVO);
|
||||
Mockito.when(dataCenterVO.getName()).thenReturn("testZone");
|
||||
virtualMachineManagerImpl.setVmNetworkDetails(vm, vmTO);
|
||||
assertEquals(vmTO.getNetworkIdToNetworkNameMap().size(), 1);
|
||||
assertEquals(vmTO.getNetworkIdToNetworkNameMap().get(0L), "testDomain-testAcc-testZone-VPC1-testNet");
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,6 +25,9 @@ public class NsxCommand extends Command {
|
||||
private String accountName;
|
||||
private String domainName;
|
||||
|
||||
public NsxCommand() {
|
||||
}
|
||||
|
||||
public NsxCommand(String domainName, String accountName, String zoneName) {
|
||||
this.zoneName = zoneName;
|
||||
this.accountName = accountName;
|
||||
|
||||
@ -71,7 +71,6 @@ public class NsxResource implements ServerResource {
|
||||
public Host.Type getType() {
|
||||
return Host.Type.Routing;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StartupCommand[] initialize() {
|
||||
StartupNsxCommand sc = new StartupNsxCommand();
|
||||
|
||||
@ -100,7 +100,7 @@ public class NsxElement extends AdapterBase implements DhcpServiceProvider, DnsS
|
||||
@Inject
|
||||
NetworkModel networkModel;
|
||||
@Inject
|
||||
private DomainDao domainDao;
|
||||
DomainDao domainDao;
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(NsxElement.class);
|
||||
|
||||
@ -117,7 +117,6 @@ public class NsxElement extends AdapterBase implements DhcpServiceProvider, DnsS
|
||||
dnsCapabilities.put(Network.Capability.AllowDnsSuffixModification, "true");
|
||||
capabilities.put(Network.Service.Dns, dnsCapabilities);
|
||||
|
||||
// capabilities.put(Network.Service.Connectivity, null);
|
||||
capabilities.put(Network.Service.StaticNat, null);
|
||||
|
||||
Map<Network.Capability, String> sourceNatCapabilities = new HashMap<>();
|
||||
@ -234,7 +233,7 @@ public class NsxElement extends AdapterBase implements DhcpServiceProvider, DnsS
|
||||
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
|
||||
agentManager.registerForHostEvents(this, true, true, true);
|
||||
resourceManager.registerResourceStateAdapter(this.getClass().getSimpleName(), this);
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -20,7 +20,6 @@ import static java.util.Objects.isNull;
|
||||
import static java.util.Objects.nonNull;
|
||||
|
||||
import com.cloud.dc.DataCenter;
|
||||
import com.cloud.dc.dao.DataCenterDao;
|
||||
import com.cloud.deploy.DeployDestination;
|
||||
import com.cloud.deploy.DeploymentPlan;
|
||||
import com.cloud.domain.DomainVO;
|
||||
@ -66,11 +65,9 @@ public class NsxGuestNetworkGuru extends GuestNetworkGuru implements NetworkMigr
|
||||
@Inject
|
||||
NsxControllerUtils nsxControllerUtils;
|
||||
@Inject
|
||||
DataCenterDao zoneDao;
|
||||
@Inject
|
||||
AccountDao accountDao;
|
||||
@Inject
|
||||
private DomainDao domainDao;
|
||||
DomainDao domainDao;
|
||||
|
||||
public NsxGuestNetworkGuru() {
|
||||
super();
|
||||
@ -138,7 +135,7 @@ public class NsxGuestNetworkGuru extends GuestNetworkGuru implements NetworkMigr
|
||||
implemented.setBroadcastUri(Networks.BroadcastDomainType.NSX.toUri("nsx"));
|
||||
try {
|
||||
long zoneId = network.getDataCenterId();
|
||||
DataCenter zone = zoneDao.findById(zoneId);
|
||||
DataCenter zone = _dcDao.findById(zoneId);
|
||||
if (isNull(zone)) {
|
||||
throw new CloudRuntimeException(String.format("Failed to find zone with id: %s", zoneId));
|
||||
}
|
||||
@ -179,16 +176,6 @@ public class NsxGuestNetworkGuru extends GuestNetworkGuru implements NetworkMigr
|
||||
implemented.setName(network.getName());
|
||||
}
|
||||
implemented.setBroadcastUri(Networks.BroadcastDomainType.NSX.toUri("nsx"));
|
||||
// try {
|
||||
// long zoneId = network.getDataCenterId();
|
||||
// DataCenter zone = zoneDao.findById(zoneId);
|
||||
// if (isNull(zone)) {
|
||||
// throw new CloudRuntimeException(String.format("Failed to find zone with id: %s", zoneId));
|
||||
// }
|
||||
// createNsxSegment(implemented, zone);
|
||||
// } catch (Exception ex) {
|
||||
// throw new CloudRuntimeException("unable to create NSX network " + network.getUuid() + "due to: " + ex.getMessage());
|
||||
// }
|
||||
return implemented;
|
||||
}
|
||||
|
||||
|
||||
@ -53,9 +53,6 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
|
||||
|
||||
public class NsxProviderServiceImpl implements NsxProviderService {
|
||||
|
||||
@Inject
|
||||
|
||||
@ -30,9 +30,9 @@ import java.util.Objects;
|
||||
|
||||
public class NsxServiceImpl implements NsxService {
|
||||
@Inject
|
||||
private NsxControllerUtils nsxControllerUtils;
|
||||
NsxControllerUtils nsxControllerUtils;
|
||||
@Inject
|
||||
private VpcDao vpcDao;
|
||||
VpcDao vpcDao;
|
||||
|
||||
public boolean createVpcNetwork(Long zoneId, String zoneName, String accountName, String domainName, String vpcName) {
|
||||
CreateNsxTier1GatewayCommand createNsxTier1GatewayCommand =
|
||||
|
||||
@ -0,0 +1,163 @@
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
package org.apache.cloudstack.resource;
|
||||
|
||||
import com.cloud.network.dao.NetworkVO;
|
||||
import com.vmware.nsx.model.TransportZone;
|
||||
import com.vmware.nsx.model.TransportZoneListResult;
|
||||
import com.vmware.nsx_policy.model.EnforcementPoint;
|
||||
import com.vmware.nsx_policy.model.EnforcementPointListResult;
|
||||
import com.vmware.nsx_policy.model.Site;
|
||||
import com.vmware.nsx_policy.model.SiteListResult;
|
||||
import junit.framework.Assert;
|
||||
import org.apache.cloudstack.NsxAnswer;
|
||||
import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
|
||||
import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
|
||||
import org.apache.cloudstack.agent.api.DeleteNsxSegmentCommand;
|
||||
import org.apache.cloudstack.agent.api.DeleteNsxTier1GatewayCommand;
|
||||
import org.apache.cloudstack.agent.api.NsxCommand;
|
||||
import org.apache.cloudstack.service.NsxApiClient;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import javax.naming.ConfigurationException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class NsxResourceTest {
|
||||
|
||||
@Mock
|
||||
NsxApiClient nsxApi;
|
||||
|
||||
NsxResource nsxResource;
|
||||
AutoCloseable closeable;
|
||||
@Mock
|
||||
EnforcementPointListResult enforcementPointListResult;
|
||||
@Mock
|
||||
SiteListResult siteListResult;
|
||||
@Mock
|
||||
TransportZoneListResult transportZoneListResult;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
closeable = MockitoAnnotations.openMocks(this);
|
||||
nsxResource = new NsxResource();
|
||||
nsxResource.nsxApiClient = nsxApi;
|
||||
nsxResource.transportZone = "Overlay";
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
closeable.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigure() throws ConfigurationException {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("name", "nsxController");
|
||||
params.put("guid", "5944b356-644f-11ee-b8c2-f37bc1b564ff");
|
||||
params.put("zoneId", "1");
|
||||
params.put("hostname", "host1");
|
||||
params.put("username", "admin");
|
||||
params.put("password", "password");
|
||||
params.put("tier0Gateway", "Tier0-GW01");
|
||||
params.put("edgeCluster", "EdgeCluster");
|
||||
params.put("transportZone", "Overlay");
|
||||
params.put("port", "443");
|
||||
|
||||
Assert.assertTrue(nsxResource.configure("nsx", params));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConfigure_MissingParameter() throws ConfigurationException {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
|
||||
assertThrows(ConfigurationException.class, () -> nsxResource.configure("nsx", params));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNsxTier1Gateway() {
|
||||
NsxCommand command = new CreateNsxTier1GatewayCommand("testDomain", "testAcc",
|
||||
"ZoneNSX", "VPC01");
|
||||
|
||||
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(command);
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteTier1Gateway() {
|
||||
NsxCommand command = new DeleteNsxTier1GatewayCommand("testDomain", "testAcc",
|
||||
"ZoneNSX", "VPC01");
|
||||
|
||||
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(command);
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNsxSegment() {
|
||||
NetworkVO tierNetwork = new NetworkVO();
|
||||
tierNetwork.setName("tier1");
|
||||
tierNetwork.setCidr("10.0.0.0/8");
|
||||
tierNetwork.setGateway("10.0.0.1");
|
||||
Site site = mock(Site.class);
|
||||
List<Site> siteList = List.of(site);
|
||||
EnforcementPoint enforcementPoint = mock(EnforcementPoint.class);
|
||||
List<EnforcementPoint> enforcementPointList = List.of(enforcementPoint);
|
||||
List<TransportZone> transportZoneList = List.of(new TransportZone.Builder().setDisplayName("Overlay").build());
|
||||
|
||||
NsxCommand command = new CreateNsxSegmentCommand("testDomain", "testAcc",
|
||||
"ZoneNSX", "VPC01", "Web", "10.10.10.1", "10.10.10.0/24");
|
||||
|
||||
when(nsxApi.getSites()).thenReturn(siteListResult);
|
||||
when(siteListResult.getResults()).thenReturn(siteList);
|
||||
when(siteList.get(0).getId()).thenReturn("site1");
|
||||
|
||||
when(nsxApi.getEnforcementPoints(anyString())).thenReturn(enforcementPointListResult);
|
||||
when(enforcementPointListResult.getResults()).thenReturn(enforcementPointList);
|
||||
when(enforcementPointList.get(0).getPath()).thenReturn("enforcementPointPath");
|
||||
|
||||
when(nsxApi.getTransportZones()).thenReturn(transportZoneListResult);
|
||||
when(transportZoneListResult.getResults()).thenReturn(transportZoneList);
|
||||
|
||||
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(command);
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteNsxSegment() {
|
||||
NetworkVO tierNetwork = new NetworkVO();
|
||||
tierNetwork.setName("tier1");
|
||||
DeleteNsxSegmentCommand command = new DeleteNsxSegmentCommand("testDomain", "testAcc", "ZoneA", "VPC01", "Web");
|
||||
|
||||
NsxAnswer answer = (NsxAnswer) nsxResource.executeRequest(command);
|
||||
assertTrue(answer.getResult());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,135 @@
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
package org.apache.cloudstack.service;
|
||||
|
||||
import com.cloud.dc.DataCenterVO;
|
||||
import com.cloud.dc.dao.DataCenterDao;
|
||||
import com.cloud.deploy.DeployDestination;
|
||||
import com.cloud.domain.DomainVO;
|
||||
import com.cloud.domain.dao.DomainDao;
|
||||
import com.cloud.exception.InsufficientCapacityException;
|
||||
import com.cloud.exception.ResourceUnavailableException;
|
||||
import com.cloud.network.NetworkModel;
|
||||
import com.cloud.network.Networks;
|
||||
import com.cloud.network.dao.NetworkDao;
|
||||
import com.cloud.network.dao.PhysicalNetworkDao;
|
||||
import com.cloud.network.dao.PhysicalNetworkVO;
|
||||
import com.cloud.network.vpc.Vpc;
|
||||
import com.cloud.resource.ResourceManager;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.user.AccountManager;
|
||||
import com.cloud.vm.ReservationContext;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class NsxElementTest {
|
||||
|
||||
@Mock
|
||||
DataCenterDao dataCenterDao;
|
||||
@Mock
|
||||
NsxServiceImpl nsxService;
|
||||
@Mock
|
||||
AccountManager accountManager;
|
||||
@Mock
|
||||
NetworkDao networkDao;
|
||||
@Mock
|
||||
ResourceManager resourceManager;
|
||||
@Mock
|
||||
PhysicalNetworkDao physicalNetworkDao;
|
||||
@Mock
|
||||
NetworkModel networkModel;
|
||||
@Mock
|
||||
Vpc vpc;
|
||||
@Mock
|
||||
DataCenterVO zone;
|
||||
@Mock
|
||||
DataCenterVO dataCenterVO;
|
||||
@Mock
|
||||
Account account;
|
||||
@Mock
|
||||
DomainVO domain;
|
||||
|
||||
NsxElement nsxElement;
|
||||
ReservationContext reservationContext;
|
||||
DeployDestination deployDestination;
|
||||
@Mock
|
||||
DomainDao domainDao;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
nsxElement = new NsxElement();
|
||||
|
||||
nsxElement.dataCenterDao = dataCenterDao;
|
||||
nsxElement.nsxService = nsxService;
|
||||
nsxElement.accountMgr = accountManager;
|
||||
nsxElement.networkDao = networkDao;
|
||||
nsxElement.resourceManager = resourceManager;
|
||||
nsxElement.physicalNetworkDao = physicalNetworkDao;
|
||||
nsxElement.domainDao = domainDao;
|
||||
nsxElement.networkModel = networkModel;
|
||||
reservationContext = mock(ReservationContext.class);
|
||||
deployDestination = mock(DeployDestination.class);
|
||||
|
||||
when(vpc.getZoneId()).thenReturn(1L);
|
||||
when(vpc.getAccountId()).thenReturn(2L);
|
||||
when(dataCenterVO.getId()).thenReturn(1L);
|
||||
when(dataCenterVO.getName()).thenReturn("zoneNSX");
|
||||
when(account.getName()).thenReturn("testAcc");
|
||||
when(vpc.getName()).thenReturn("VPC01");
|
||||
when(accountManager.getAccount(2L)).thenReturn(account);
|
||||
when(dataCenterDao.findById(anyLong())).thenReturn(dataCenterVO);
|
||||
when(domainDao.findById(anyLong())).thenReturn(domain);
|
||||
when(vpc.getZoneId()).thenReturn(1L);
|
||||
when(domain.getName()).thenReturn("testDomain");
|
||||
when(vpc.getName()).thenReturn("testVPC");
|
||||
|
||||
PhysicalNetworkVO physicalNetworkVO = new PhysicalNetworkVO();
|
||||
physicalNetworkVO.setIsolationMethods(List.of("NSX"));
|
||||
List<PhysicalNetworkVO> physicalNetworkVOList = List.of(physicalNetworkVO);
|
||||
|
||||
when(physicalNetworkDao.listByZoneAndTrafficType(1L, Networks.TrafficType.Guest)).thenReturn(physicalNetworkVOList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImplementVpc() throws ResourceUnavailableException, InsufficientCapacityException {
|
||||
when(nsxService.createVpcNetwork(anyLong(), anyString(), anyString(), anyString(), anyString())).thenReturn(true);
|
||||
|
||||
assertTrue(nsxElement.implementVpc(vpc, deployDestination, reservationContext));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShutdownVpc() {
|
||||
when(nsxService.deleteVpcNetwork(anyLong(), anyString(), anyString(), anyString(), anyString())).thenReturn(true);
|
||||
|
||||
assertTrue(nsxElement.shutdownVpc(vpc, reservationContext));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,206 @@
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
package org.apache.cloudstack.service;
|
||||
|
||||
import com.cloud.dc.DataCenter;
|
||||
import com.cloud.dc.DataCenterVO;
|
||||
import com.cloud.dc.dao.DataCenterDao;
|
||||
import com.cloud.deploy.DeployDestination;
|
||||
import com.cloud.deploy.DeploymentPlan;
|
||||
import com.cloud.domain.DomainVO;
|
||||
import com.cloud.domain.dao.DomainDao;
|
||||
import com.cloud.network.Network;
|
||||
import com.cloud.network.NetworkModel;
|
||||
import com.cloud.network.Networks;
|
||||
import com.cloud.network.dao.PhysicalNetworkDao;
|
||||
import com.cloud.network.dao.PhysicalNetworkVO;
|
||||
import com.cloud.network.vpc.VpcVO;
|
||||
import com.cloud.network.vpc.dao.VpcDao;
|
||||
import com.cloud.offering.NetworkOffering;
|
||||
import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.user.AccountVO;
|
||||
import com.cloud.user.dao.AccountDao;
|
||||
import com.cloud.vm.ReservationContext;
|
||||
import org.apache.cloudstack.NsxAnswer;
|
||||
import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
|
||||
import org.apache.cloudstack.agent.api.NsxCommand;
|
||||
import org.apache.cloudstack.utils.NsxControllerUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentMatchers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class NsxGuestNetworkGuruTest {
|
||||
|
||||
@Mock
|
||||
PhysicalNetworkDao physicalNetworkDao;
|
||||
@Mock
|
||||
DataCenterDao dcDao;
|
||||
@Mock
|
||||
VpcDao vpcDao;
|
||||
@Mock
|
||||
NetworkOfferingServiceMapDao networkOfferingServiceMapDao;
|
||||
@Mock
|
||||
NsxControllerUtils nsxControllerUtils;
|
||||
@Mock
|
||||
DataCenterDao zoneDao;
|
||||
@Mock
|
||||
AccountDao accountDao;
|
||||
@Mock
|
||||
PhysicalNetworkVO physicalNetwork;
|
||||
@Mock
|
||||
DataCenterVO dataCenterVO;
|
||||
@Mock
|
||||
NetworkOffering offering;
|
||||
@Mock
|
||||
DeploymentPlan plan;
|
||||
@Mock
|
||||
Network network;
|
||||
@Mock
|
||||
Account account;
|
||||
@Mock
|
||||
VpcVO vpcVO;
|
||||
@Mock
|
||||
NetworkModel networkModel;
|
||||
@Mock
|
||||
DomainDao domainDao;
|
||||
|
||||
NsxGuestNetworkGuru guru;
|
||||
AutoCloseable closeable;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
closeable = MockitoAnnotations.openMocks(this);
|
||||
guru = new NsxGuestNetworkGuru();
|
||||
ReflectionTestUtils.setField(guru, "_physicalNetworkDao", physicalNetworkDao);
|
||||
ReflectionTestUtils.setField(guru, "_dcDao", dcDao);
|
||||
ReflectionTestUtils.setField(guru, "_networkModel", networkModel);
|
||||
ReflectionTestUtils.setField(guru, "_vpcDao", vpcDao);
|
||||
|
||||
guru.networkOfferingServiceMapDao = networkOfferingServiceMapDao;
|
||||
guru.nsxControllerUtils = nsxControllerUtils;
|
||||
guru.accountDao = accountDao;
|
||||
guru.domainDao = domainDao;
|
||||
|
||||
Mockito.when(dataCenterVO.getNetworkType()).thenReturn(DataCenter.NetworkType.Advanced);
|
||||
|
||||
when(physicalNetwork.getIsolationMethods()).thenReturn(List.of("NSX"));
|
||||
|
||||
when(offering.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
|
||||
when(offering.getGuestType()).thenReturn(Network.GuestType.Isolated);
|
||||
when(offering.getId()).thenReturn(1L);
|
||||
|
||||
when(plan.getDataCenterId()).thenReturn(1L);
|
||||
when(plan.getPhysicalNetworkId()).thenReturn(1L);
|
||||
|
||||
when(vpcDao.findById(anyLong())).thenReturn(vpcVO);
|
||||
|
||||
when(vpcVO.getName()).thenReturn("VPC01");
|
||||
|
||||
when(account.getAccountId()).thenReturn(1L);
|
||||
when(accountDao.findById(anyLong())).thenReturn(mock(AccountVO.class));
|
||||
when(domainDao.findById(anyLong())).thenReturn(mock(DomainVO.class));
|
||||
|
||||
Mockito.when(networkOfferingServiceMapDao.isProviderForNetworkOffering(offering.getId(), Network.Provider.Nsx)).thenReturn(
|
||||
true);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
closeable.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsMyIsolationMethod() {
|
||||
assertTrue(guru.isMyIsolationMethod(physicalNetwork));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCanHandle() {
|
||||
assertTrue(guru.canHandle(offering, dataCenterVO.getNetworkType(), physicalNetwork));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNsxNetworkDesign() {
|
||||
when(physicalNetworkDao.findById(ArgumentMatchers.anyLong())).thenReturn(physicalNetwork);
|
||||
when(dcDao.findById(ArgumentMatchers.anyLong())).thenReturn(dataCenterVO);
|
||||
when(nsxControllerUtils.sendNsxCommand(any(CreateNsxSegmentCommand.class), anyLong())).thenReturn(
|
||||
new NsxAnswer(new NsxCommand(), true, ""));
|
||||
|
||||
Network designedNetwork = guru.design(offering, plan, network, "", 1L, account);
|
||||
verify(nsxControllerUtils, times(1)).sendNsxCommand(any(CreateNsxSegmentCommand.class), anyLong());
|
||||
assertNotNull(designedNetwork);
|
||||
assertSame(Networks.BroadcastDomainType.NSX, designedNetwork.getBroadcastDomainType());
|
||||
assertSame(Network.State.Allocated, designedNetwork.getState());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNsxNetworkImplementation() {
|
||||
final DeployDestination deployDestination = mock(DeployDestination.class);
|
||||
final ReservationContext reservationContext = mock(ReservationContext.class);
|
||||
|
||||
when(network.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
|
||||
when(network.getMode()).thenReturn(Networks.Mode.Dhcp);
|
||||
when(network.getGateway()).thenReturn("192.168.1.1");
|
||||
when(network.getCidr()).thenReturn("192.168.1.0/24");
|
||||
when(network.getBroadcastDomainType()).thenReturn(Networks.BroadcastDomainType.NSX);
|
||||
when(network.getNetworkOfferingId()).thenReturn(1L);
|
||||
lenient().when(network.getState()).thenReturn(Network.State.Implementing);
|
||||
when(network.getDataCenterId()).thenReturn(2L);
|
||||
when(network.getPhysicalNetworkId()).thenReturn(3L);
|
||||
when(network.getVpcId()).thenReturn(4L);
|
||||
when(offering.isRedundantRouter()).thenReturn(false);
|
||||
lenient().when(offering.getGuestType()).thenReturn(Network.GuestType.Isolated);
|
||||
|
||||
|
||||
final Network implemented = guru.implement(network, offering, deployDestination, reservationContext);
|
||||
assertEquals(Networks.BroadcastDomainType.NSX.toUri("nsx"), implemented.getBroadcastUri());
|
||||
assertEquals("192.168.1.1", implemented.getGateway());
|
||||
assertEquals("192.168.1.0/24", implemented.getCidr());
|
||||
assertEquals(Networks.Mode.Dhcp, implemented.getMode());
|
||||
assertEquals(Networks.BroadcastDomainType.NSX, implemented.getBroadcastDomainType());
|
||||
assertEquals(1L, implemented.getNetworkOfferingId());
|
||||
assertEquals(Network.State.Implemented, implemented.getState());
|
||||
assertEquals(2L, implemented.getDataCenterId());
|
||||
assertEquals(3L, implemented.getPhysicalNetworkId().longValue());
|
||||
assertEquals(4L, implemented.getVpcId().longValue());
|
||||
assertFalse(implemented.isRedundant());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,158 @@
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
package org.apache.cloudstack.service;
|
||||
|
||||
import com.cloud.dc.DataCenterVO;
|
||||
import com.cloud.dc.dao.DataCenterDao;
|
||||
import com.cloud.host.Host;
|
||||
import com.cloud.host.dao.HostDetailsDao;
|
||||
import com.cloud.network.NsxProvider;
|
||||
import com.cloud.network.dao.NetworkDao;
|
||||
import com.cloud.network.dao.NetworkVO;
|
||||
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.resource.ResourceManager;
|
||||
import com.cloud.resource.ServerResource;
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
import org.apache.cloudstack.api.BaseResponse;
|
||||
import org.apache.cloudstack.api.command.AddNsxControllerCmd;
|
||||
import org.apache.cloudstack.api.response.NsxControllerResponse;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyMap;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class NsxProviderServiceImplTest {
|
||||
@Mock
|
||||
NsxProviderDao nsxProviderDao;
|
||||
@Mock
|
||||
DataCenterDao dataCenterDao;
|
||||
@Mock
|
||||
PhysicalNetworkDao physicalNetworkDao;
|
||||
@Mock
|
||||
NetworkDao networkDao;
|
||||
@Mock
|
||||
ResourceManager resourceManager;
|
||||
@Mock
|
||||
HostDetailsDao hostDetailsDao;
|
||||
|
||||
NsxProviderServiceImpl nsxProviderService;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
nsxProviderService = new NsxProviderServiceImpl();
|
||||
nsxProviderService.resourceManager = resourceManager;
|
||||
nsxProviderService.nsxProviderDao = nsxProviderDao;
|
||||
nsxProviderService.hostDetailsDao = hostDetailsDao;
|
||||
nsxProviderService.dataCenterDao = dataCenterDao;
|
||||
nsxProviderService.networkDao = networkDao;
|
||||
nsxProviderService.physicalNetworkDao = physicalNetworkDao;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddProvider() {
|
||||
AddNsxControllerCmd cmd = mock(AddNsxControllerCmd.class);
|
||||
when(cmd.getZoneId()).thenReturn(1L);
|
||||
when(cmd.getName()).thenReturn("NsxController");
|
||||
when(cmd.getHostname()).thenReturn("192.168.0.100");
|
||||
when(cmd.getPort()).thenReturn("443");
|
||||
when(cmd.getUsername()).thenReturn("admin");
|
||||
when(cmd.getPassword()).thenReturn("password");
|
||||
when(cmd.getEdgeCluster()).thenReturn("EdgeCluster");
|
||||
when(cmd.getTier0Gateway()).thenReturn("Tier0-GW01");
|
||||
when(cmd.getTransportZone()).thenReturn("Overlay");
|
||||
when(resourceManager.addHost(anyLong(), any(ServerResource.class), any(Host.Type.class), anyMap())).thenReturn(mock(Host.class));
|
||||
try {
|
||||
NsxProvider provider = nsxProviderService.addProvider(cmd);
|
||||
Assert.assertNotNull(provider);
|
||||
} catch (CloudRuntimeException e) {
|
||||
e.printStackTrace();
|
||||
fail("Failed to add NSX controller due to internal error.");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNsxControllerResponse() {
|
||||
NsxProvider nsxProvider = mock(NsxProvider.class);
|
||||
DataCenterVO zone = mock(DataCenterVO.class);
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
when(dataCenterDao.findById(anyLong())).thenReturn(zone);
|
||||
when(zone.getUuid()).thenReturn(UUID.randomUUID().toString());
|
||||
when(zone.getName()).thenReturn("ZoneNSX");
|
||||
when(nsxProvider.getProviderName()).thenReturn("NSXController");
|
||||
when(nsxProvider.getUuid()).thenReturn(uuid);
|
||||
when(nsxProvider.getHostname()).thenReturn("hostname");
|
||||
when(nsxProvider.getPort()).thenReturn("443");
|
||||
when(nsxProvider.getTier0Gateway()).thenReturn("Tier0Gw");
|
||||
when(nsxProvider.getEdgeCluster()).thenReturn("EdgeCluster");
|
||||
when(nsxProvider.getTransportZone()).thenReturn("Overlay");
|
||||
|
||||
NsxControllerResponse response = nsxProviderService.createNsxControllerResponse(nsxProvider);
|
||||
|
||||
assertEquals(response.getEdgeCluster(), "EdgeCluster");
|
||||
assertEquals(response.getTier0Gateway(), "Tier0Gw");
|
||||
assertEquals(response.getTransportZone(), "Overlay");
|
||||
assertEquals(response.getZoneName(), "ZoneNSX");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListNsxControllers() {
|
||||
NsxProviderVO nsxProviderVO = Mockito.mock(NsxProviderVO.class);
|
||||
|
||||
when(nsxProviderVO.getZoneId()).thenReturn(1L);
|
||||
when(dataCenterDao.findById(1L)).thenReturn(mock(DataCenterVO.class));
|
||||
when(nsxProviderDao.findByZoneId(anyLong())).thenReturn(nsxProviderVO);
|
||||
|
||||
List<BaseResponse> baseResponseList = nsxProviderService.listNsxProviders(1L);
|
||||
assertEquals(1, baseResponseList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteNsxController() {
|
||||
NsxProviderVO nsxProviderVO = Mockito.mock(NsxProviderVO.class);
|
||||
PhysicalNetworkVO physicalNetworkVO = mock(PhysicalNetworkVO.class);
|
||||
List<PhysicalNetworkVO> physicalNetworkVOList = List.of(physicalNetworkVO);
|
||||
NetworkVO networkVO = mock(NetworkVO.class);
|
||||
List<NetworkVO> networkVOList = List.of(networkVO);
|
||||
|
||||
when(nsxProviderVO.getZoneId()).thenReturn(1L);
|
||||
when(physicalNetworkVO.getId()).thenReturn(2L);
|
||||
when(physicalNetworkDao.listByZone(1L)).thenReturn(physicalNetworkVOList);
|
||||
when(nsxProviderDao.findById(anyLong())).thenReturn(nsxProviderVO);
|
||||
when(networkDao.listByPhysicalNetwork(anyLong())).thenReturn(networkVOList);
|
||||
|
||||
assertTrue(nsxProviderService.deleteNsxController(1L));
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,94 @@
|
||||
// Licensed to the Apache Software Foundation (ASF) under one
|
||||
// or more contributor license agreements. See the NOTICE file
|
||||
// distributed with this work for additional information
|
||||
// regarding copyright ownership. The ASF licenses this file
|
||||
// to you under the Apache License, Version 2.0 (the
|
||||
// "License"); you may not use this file except in compliance
|
||||
// with the License. You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing,
|
||||
// software distributed under the License is distributed on an
|
||||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
// KIND, either express or implied. See the License for the
|
||||
// specific language governing permissions and limitations
|
||||
// under the License.
|
||||
package org.apache.cloudstack.service;
|
||||
|
||||
import com.cloud.network.dao.NetworkVO;
|
||||
import com.cloud.network.vpc.VpcVO;
|
||||
import com.cloud.network.vpc.dao.VpcDao;
|
||||
import org.apache.cloudstack.NsxAnswer;
|
||||
import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
|
||||
import org.apache.cloudstack.agent.api.DeleteNsxSegmentCommand;
|
||||
import org.apache.cloudstack.agent.api.DeleteNsxTier1GatewayCommand;
|
||||
import org.apache.cloudstack.utils.NsxControllerUtils;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class NsxServiceImplTest {
|
||||
@Mock
|
||||
private NsxControllerUtils nsxControllerUtils;
|
||||
@Mock
|
||||
private VpcDao vpcDao;
|
||||
NsxServiceImpl nsxService;
|
||||
|
||||
AutoCloseable closeable;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
closeable = MockitoAnnotations.openMocks(this);
|
||||
nsxService = new NsxServiceImpl();
|
||||
nsxService.nsxControllerUtils = nsxControllerUtils;
|
||||
nsxService.vpcDao = vpcDao;
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() throws Exception {
|
||||
closeable.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateVpcNetwork() {
|
||||
NsxAnswer createNsxTier1GatewayAnswer = mock(NsxAnswer.class);
|
||||
when(nsxControllerUtils.sendNsxCommand(any(CreateNsxTier1GatewayCommand.class), anyLong())).thenReturn(createNsxTier1GatewayAnswer);
|
||||
when(createNsxTier1GatewayAnswer.getResult()).thenReturn(true);
|
||||
|
||||
assertTrue(nsxService.createVpcNetwork(1L, "ZoneNSX", "testAcc", "testAcc", "VPC01"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteVpcNetwork() {
|
||||
NsxAnswer deleteNsxTier1GatewayAnswer = mock(NsxAnswer.class);
|
||||
when(nsxControllerUtils.sendNsxCommand(any(DeleteNsxTier1GatewayCommand.class), anyLong())).thenReturn(deleteNsxTier1GatewayAnswer);
|
||||
when(deleteNsxTier1GatewayAnswer.getResult()).thenReturn(true);
|
||||
|
||||
assertTrue(nsxService.deleteVpcNetwork(1L, "ZoneNSX", "testAcc", "testAcc", "VPC01"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete() {
|
||||
NetworkVO network = new NetworkVO();
|
||||
network.setVpcId(1L);
|
||||
VpcVO vpc = mock(VpcVO.class);
|
||||
when(vpcDao.findById(1L)).thenReturn(mock(VpcVO.class));
|
||||
NsxAnswer deleteNsxSegmentAnswer = mock(NsxAnswer.class);
|
||||
when(nsxControllerUtils.sendNsxCommand(any(DeleteNsxSegmentCommand.class), anyLong())).thenReturn(deleteNsxSegmentAnswer);
|
||||
when(deleteNsxSegmentAnswer.getResult()).thenReturn(true);
|
||||
|
||||
assertTrue(nsxService.deleteNetwork("ZoneNSX", "testAcc", "testDomain", network));
|
||||
}
|
||||
}
|
||||
@ -21,14 +21,24 @@ import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.nullable;
|
||||
import static org.mockito.ArgumentMatchers.anyList;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.cloud.hypervisor.vmware.util.VmwareClient;
|
||||
import com.cloud.network.Networks;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.vmware.vim25.DynamicProperty;
|
||||
import com.vmware.vim25.ManagedObjectReference;
|
||||
import com.vmware.vim25.ObjectContent;
|
||||
import com.vmware.vim25.VimPortType;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
@ -61,6 +71,10 @@ public class HypervisorHostHelperTest {
|
||||
@Mock
|
||||
VmwareContext context;
|
||||
@Mock
|
||||
ManagedObjectReference mor;
|
||||
@Mock
|
||||
VmwareClient vmwareClient;
|
||||
@Mock
|
||||
DVPortgroupConfigInfo currentDvPortgroupInfo;
|
||||
@Mock
|
||||
DVPortgroupConfigSpec dvPortgroupConfigSpec;
|
||||
@ -78,6 +92,12 @@ public class HypervisorHostHelperTest {
|
||||
private ClusterConfigInfoEx clusterConfigInfo;
|
||||
@Mock
|
||||
private DatacenterConfigInfo datacenterConfigInfo;
|
||||
@Mock
|
||||
HostMO hostMO;
|
||||
@Mock
|
||||
VimPortType vimService;
|
||||
@Mock
|
||||
ObjectContent ocs;
|
||||
|
||||
String vSwitchName;
|
||||
Integer networkRateMbps;
|
||||
@ -90,6 +110,10 @@ public class HypervisorHostHelperTest {
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
closeable = MockitoAnnotations.openMocks(this);
|
||||
ObjectContent oc = new ObjectContent();
|
||||
when(hostMO.getContext()).thenReturn(context);
|
||||
when(context.getService()).thenReturn(vimService);
|
||||
when(context.getVimClient()).thenReturn(vmwareClient);
|
||||
when(context.getServiceContent()).thenReturn(serviceContent);
|
||||
when(serviceContent.getAbout()).thenReturn(aboutInfo);
|
||||
when(clusterMO.getClusterConfigInfo()).thenReturn(clusterConfigInfo);
|
||||
@ -947,4 +971,24 @@ public class HypervisorHostHelperTest {
|
||||
HypervisorHostHelper.setVMHardwareVersion(vmSpec, clusterMO, datacenterMO);
|
||||
verify(vmSpec, never()).setVersion(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPrepareNetwork() throws Exception {
|
||||
String networkName = "testAcc-VPC1-tier1";
|
||||
DynamicProperty property = new DynamicProperty();
|
||||
property.setVal(networkName);
|
||||
|
||||
when(hostMO.getHyperHostDatacenter()).thenReturn(mor);
|
||||
when(datacenterMO.getDvSwitchMor(any(String.class))).thenReturn(mor);
|
||||
when(vmwareClient.getDecendentMoRef(nullable(ManagedObjectReference.class), any(String.class), any(String.class))).thenReturn(mor);
|
||||
when(vimService.retrieveProperties(any(), anyList())).thenReturn(List.of(ocs));
|
||||
when(ocs.getPropSet()).thenReturn(List.of(property));
|
||||
when(ocs.getObj()).thenReturn(mor);
|
||||
|
||||
Pair<ManagedObjectReference, String> morNet = HypervisorHostHelper.prepareNetwork("NSX-VDS", "cloud.guest", hostMO, null, null,
|
||||
200, null, 900000, VirtualSwitchType.VMwareDistributedVirtualSwitch, 1, null,
|
||||
false, Networks.BroadcastDomainType.NSX, null,
|
||||
null, networkName);
|
||||
assertEquals(morNet.second(), networkName);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user