Merge pull request #845 from borisroman/CLOUDSTACK-8763

[4.6][BLOCKER]CLOUDSTACK-8763: Resolved POD/ZONE deletion failure.Instead of having both checkIfPodIsDeletable() and checkIfZoneIsDeletable have there own SQL query, I've refactored them so they use DAO SQL Queries.

This resolves the SQL Exception thrown by both classes.

Test to confirm working order:
- deploy ACS
- Add zones / pods. -> Try to delete without resources. -> Direct success.
- Add resources to zones / pods. -> Try to delete with resources in the pod / zone. -> Correct exception thrown. (Error message is why it cannot remove the zone / pod. IE: There is still a hosts or vm or .... )

* pr/845:
  Added unit tests for checkIfPodIsDeletable() and checkIfZoneIsDeletable().
  Updated Dao classes with correct field names.
  Refactored checkIfZoneIsDeletable().
  Added findByDc(long dcId) to VolumeDao and VolumeDaoImpl.
  Added countIPs(long dcId, boolean onlyCountAllocated) to IPAddressDao and IPAddressDaoImpl.
  Added countIPs(long dcId, boolean onlyCountAllocated) to DataCenterIpAddressDao and DataCenterIpAddressDaoImpl.
  Refactored checkIfPodIsDeletable().
  Added findByPodId(Long podId) to HostDao and HostDaoImpl.

Signed-off-by: Wido den Hollander <wido@widodh.nl>
This commit is contained in:
Wido den Hollander 2015-09-23 14:00:04 +02:00
commit a0f8f56a5d
9 changed files with 405 additions and 162 deletions

View File

@ -43,6 +43,8 @@ public interface DataCenterIpAddressDao extends GenericDao<DataCenterIpAddressVO
int countIPs(long podId, long dcId, boolean onlyCountAllocated);
int countIPs(long dcId, boolean onlyCountAllocated);
boolean deleteIpAddressByPod(long podId);
}

View File

@ -45,7 +45,9 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase<DataCenterIpAddre
private final SearchBuilder<DataCenterIpAddressVO> AllFieldsSearch;
private final GenericSearchBuilder<DataCenterIpAddressVO, Integer> AllIpCount;
private final GenericSearchBuilder<DataCenterIpAddressVO, Integer> AllIpCountForDc;
private final GenericSearchBuilder<DataCenterIpAddressVO, Integer> AllAllocatedIpCount;
private final GenericSearchBuilder<DataCenterIpAddressVO, Integer> AllAllocatedIpCountForDc;
@Override
@DB
@ -221,6 +223,20 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase<DataCenterIpAddre
return count.get(0);
}
@Override
public int countIPs(long dcId, boolean onlyCountAllocated) {
SearchCriteria<Integer> sc;
if (onlyCountAllocated) {
sc = AllAllocatedIpCountForDc.create();
} else {
sc = AllIpCountForDc.create();
}
sc.setParameters("data_center_id", dcId);
List<Integer> count = customSearch(sc, null);
return count.get(0);
}
public DataCenterIpAddressDaoImpl() {
super();
@ -239,10 +255,21 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase<DataCenterIpAddre
AllIpCount.and("pod", AllIpCount.entity().getPodId(), SearchCriteria.Op.EQ);
AllIpCount.done();
AllIpCountForDc = createSearchBuilder(Integer.class);
AllIpCountForDc.select(null, Func.COUNT, AllIpCountForDc.entity().getId());
AllIpCountForDc.and("data_center_id", AllIpCountForDc.entity().getPodId(), SearchCriteria.Op.EQ);
AllIpCountForDc.done();
AllAllocatedIpCount = createSearchBuilder(Integer.class);
AllAllocatedIpCount.select(null, Func.COUNT, AllAllocatedIpCount.entity().getId());
AllAllocatedIpCount.and("pod", AllAllocatedIpCount.entity().getPodId(), SearchCriteria.Op.EQ);
AllAllocatedIpCount.and("removed", AllAllocatedIpCount.entity().getTakenAt(), SearchCriteria.Op.NNULL);
AllAllocatedIpCount.done();
AllAllocatedIpCountForDc = createSearchBuilder(Integer.class);
AllAllocatedIpCountForDc.select(null, Func.COUNT, AllAllocatedIpCountForDc.entity().getId());
AllAllocatedIpCountForDc.and("data_center_id", AllAllocatedIpCountForDc.entity().getDataCenterId(), SearchCriteria.Op.EQ);
AllAllocatedIpCountForDc.and("removed", AllAllocatedIpCountForDc.entity().getTakenAt(), SearchCriteria.Op.NNULL);
AllAllocatedIpCountForDc.done();
}
}

View File

@ -40,6 +40,8 @@ public interface IPAddressDao extends GenericDao<IPAddressVO, Long> {
List<IPAddressVO> listStaticNatPublicIps(long networkId);
int countIPs(long dcId, boolean onlyCountAllocated);
int countIPs(long dcId, long vlanDbId, boolean onlyCountAllocated);
int countIPs(long dcId, Long accountId, String vlanId, String vlanGateway, String vlanNetmask);

View File

@ -55,7 +55,9 @@ public class IPAddressDaoImpl extends GenericDaoBase<IPAddressVO, Long> implemen
protected SearchBuilder<IPAddressVO> AllFieldsSearch;
protected SearchBuilder<IPAddressVO> VlanDbIdSearchUnallocated;
protected GenericSearchBuilder<IPAddressVO, Integer> AllIpCount;
protected GenericSearchBuilder<IPAddressVO, Integer> AllIpCountForDc;
protected GenericSearchBuilder<IPAddressVO, Integer> AllocatedIpCount;
protected GenericSearchBuilder<IPAddressVO, Integer> AllocatedIpCountForDc;
protected GenericSearchBuilder<IPAddressVO, Integer> AllIpCountForDashboard;
protected SearchBuilder<IPAddressVO> DeleteAllExceptGivenIp;
protected GenericSearchBuilder<IPAddressVO, Long> AllocatedIpCountForAccount;
@ -100,6 +102,11 @@ public class IPAddressDaoImpl extends GenericDaoBase<IPAddressVO, Long> implemen
AllIpCount.and("vlan", AllIpCount.entity().getVlanId(), Op.EQ);
AllIpCount.done();
AllIpCountForDc = createSearchBuilder(Integer.class);
AllIpCountForDc.select(null, Func.COUNT, AllIpCountForDc.entity().getAddress());
AllIpCountForDc.and("dc", AllIpCountForDc.entity().getDataCenterId(), Op.EQ);
AllIpCountForDc.done();
AllocatedIpCount = createSearchBuilder(Integer.class);
AllocatedIpCount.select(null, Func.COUNT, AllocatedIpCount.entity().getAddress());
AllocatedIpCount.and("dc", AllocatedIpCount.entity().getDataCenterId(), Op.EQ);
@ -107,6 +114,12 @@ public class IPAddressDaoImpl extends GenericDaoBase<IPAddressVO, Long> implemen
AllocatedIpCount.and("allocated", AllocatedIpCount.entity().getAllocatedTime(), Op.NNULL);
AllocatedIpCount.done();
AllocatedIpCountForDc = createSearchBuilder(Integer.class);
AllocatedIpCountForDc.select(null, Func.COUNT, AllocatedIpCountForDc.entity().getAddress());
AllocatedIpCountForDc.and("dc", AllocatedIpCountForDc.entity().getDataCenterId(), Op.EQ);
AllocatedIpCountForDc.and("allocated", AllocatedIpCountForDc.entity().getAllocatedTime(), Op.NNULL);
AllocatedIpCountForDc.done();
AllIpCountForDashboard = createSearchBuilder(Integer.class);
AllIpCountForDashboard.select(null, Func.COUNT, AllIpCountForDashboard.entity().getAddress());
AllIpCountForDashboard.and("dc", AllIpCountForDashboard.entity().getDataCenterId(), Op.EQ);
@ -281,6 +294,14 @@ public class IPAddressDaoImpl extends GenericDaoBase<IPAddressVO, Long> implemen
return findOneBy(sc);
}
@Override
public int countIPs(long dcId, boolean onlyCountAllocated) {
SearchCriteria<Integer> sc = onlyCountAllocated ? AllocatedIpCountForDc.create() : AllIpCountForDc.create();
sc.setParameters("dc", dcId);
return customSearch(sc, null).get(0);
}
@Override
public int countIPs(long dcId, long vlanId, boolean onlyCountAllocated) {
SearchCriteria<Integer> sc = onlyCountAllocated ? AllocatedIpCount.create() : AllIpCount.create();

View File

@ -45,6 +45,10 @@ public interface VolumeDao extends GenericDao<VolumeVO, Long>, StateDao<Volume.S
List<VolumeVO> findByInstanceIdDestroyed(long vmId);
List<VolumeVO> findByPod(long podId);
List<VolumeVO> findByDc(long dcId);
List<VolumeVO> findByAccountAndPod(long accountId, long podId);
List<VolumeVO> findByTemplateAndZone(long templateId, long zoneId);

View File

@ -189,6 +189,22 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
return listBy(sc);
}
@Override
public List<VolumeVO> findByPod(long podId) {
SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
sc.setParameters("pod", podId);
return listBy(sc);
}
@Override
public List<VolumeVO> findByDc(long dcId) {
SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
sc.setParameters("dcId", dcId);
return listBy(sc);
}
@Override
public List<VolumeVO> findByAccountAndPod(long accountId, long podId) {
SearchCriteria<VolumeVO> sc = AllFieldsSearch.create();
@ -306,6 +322,7 @@ public class VolumeDaoImpl extends GenericDaoBase<VolumeVO, Long> implements Vol
AllFieldsSearch = createSearchBuilder();
AllFieldsSearch.and("state", AllFieldsSearch.entity().getState(), Op.EQ);
AllFieldsSearch.and("accountId", AllFieldsSearch.entity().getAccountId(), Op.EQ);
AllFieldsSearch.and("dcId", AllFieldsSearch.entity().getDataCenterId(), Op.EQ);
AllFieldsSearch.and("pod", AllFieldsSearch.entity().getPodId(), Op.EQ);
AllFieldsSearch.and("instanceId", AllFieldsSearch.entity().getInstanceId(), Op.EQ);
AllFieldsSearch.and("deviceId", AllFieldsSearch.entity().getDeviceId(), Op.EQ);

View File

@ -155,6 +155,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
AllFieldsSearch.and("lastHost", AllFieldsSearch.entity().getLastHostId(), Op.EQ);
AllFieldsSearch.and("state", AllFieldsSearch.entity().getState(), Op.EQ);
AllFieldsSearch.and("zone", AllFieldsSearch.entity().getDataCenterId(), Op.EQ);
AllFieldsSearch.and("pod", AllFieldsSearch.entity().getPodIdToDeployIn(), Op.EQ);
AllFieldsSearch.and("type", AllFieldsSearch.entity().getType(), Op.EQ);
AllFieldsSearch.and("account", AllFieldsSearch.entity().getAccountId(), Op.EQ);
AllFieldsSearch.done();

View File

@ -19,8 +19,6 @@ package com.cloud.configuration;
import java.net.URI;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -134,6 +132,7 @@ import com.cloud.exception.PermissionDeniedException;
import com.cloud.exception.ResourceAllocationException;
import com.cloud.exception.ResourceUnavailableException;
import com.cloud.gpu.GPU;
import com.cloud.host.dao.HostDao;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.network.IpAddressManager;
import com.cloud.network.Network;
@ -182,6 +181,7 @@ import com.cloud.storage.Storage.ProvisioningType;
import com.cloud.storage.Storage.StoragePoolType;
import com.cloud.storage.StorageManager;
import com.cloud.storage.dao.DiskOfferingDao;
import com.cloud.storage.dao.VolumeDao;
import com.cloud.test.IPRangeConfig;
import com.cloud.user.Account;
import com.cloud.user.AccountDetailVO;
@ -214,6 +214,7 @@ import com.cloud.vm.dao.NicDao;
import com.cloud.vm.dao.NicIpAliasDao;
import com.cloud.vm.dao.NicIpAliasVO;
import com.cloud.vm.dao.NicSecondaryIpDao;
import com.cloud.vm.dao.VMInstanceDao;
@Local(value = {ConfigurationManager.class, ConfigurationService.class})
public class ConfigurationManagerImpl extends ManagerBase implements ConfigurationManager, ConfigurationService, Configurable {
@ -228,6 +229,14 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
@Inject
HostPodDao _podDao;
@Inject
HostDao _hostDao;
@Inject
VolumeDao _volumeDao;
@Inject
VMInstanceDao _vmInstanceDao;
//@Inject
//VmwareDatacenterZoneMapDao _vmwareDatacenterZoneMapDao;
@Inject
AccountVlanMapDao _accountVlanMapDao;
@Inject
PodVlanMapDao _podVlanMapDao;
@ -878,81 +887,34 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
return count > 0;
}
@DB
protected void checkIfPodIsDeletable(final long podId) {
final List<List<String>> tablesToCheck = new ArrayList<List<String>>();
final HostPodVO pod = _podDao.findById(podId);
final String errorMsg = "The pod cannot be deleted because ";
// Check if there are allocated private IP addresses in the pod
if (_privateIpAddressDao.countIPs(podId, pod.getDataCenterId(), true) != 0) {
throw new CloudRuntimeException("There are private IP addresses allocated for this pod");
throw new CloudRuntimeException(errorMsg + "there are private IP addresses allocated in this pod.");
}
final List<String> volumes = new ArrayList<String>();
volumes.add(0, "volumes");
volumes.add(1, "pod_id");
volumes.add(2, "there are storage volumes for this pod");
tablesToCheck.add(volumes);
// Check if there are any non-removed volumes in the pod.
if (!_volumeDao.findByPod(podId).isEmpty()) {
throw new CloudRuntimeException(errorMsg + "there are storage volumes in this pod.");
}
final List<String> host = new ArrayList<String>();
host.add(0, "host");
host.add(1, "pod_id");
host.add(2, "there are servers running in this pod");
tablesToCheck.add(host);
// Check if there are any non-removed hosts in the pod.
if (!_hostDao.findByPodId(podId).isEmpty()) {
throw new CloudRuntimeException(errorMsg + "there are servers in this pod.");
}
final List<String> vmInstance = new ArrayList<String>();
vmInstance.add(0, "vm_instance");
vmInstance.add(1, "pod_id");
vmInstance.add(2, "there are virtual machines running in this pod");
tablesToCheck.add(vmInstance);
// Check if there are any non-removed vms in the pod.
if (!_vmInstanceDao.listByPodId(podId).isEmpty()) {
throw new CloudRuntimeException(errorMsg + "there are virtual machines in this pod.");
}
final List<String> cluster = new ArrayList<String>();
cluster.add(0, "cluster");
cluster.add(1, "pod_id");
cluster.add(2, "there are clusters in this pod");
tablesToCheck.add(cluster);
for (final List<String> table : tablesToCheck) {
final String tableName = table.get(0);
final String column = table.get(1);
final String errorMsg = table.get(2);
String dbName;
if (tableName.equals("event") || tableName.equals("cloud_usage") || tableName.equals("usage_vm_instance") || tableName.equals("usage_ip_address")
|| tableName.equals("usage_network") || tableName.equals("usage_job") || tableName.equals("account") || tableName.equals("user_statistics")) {
dbName = "cloud_usage";
} else {
dbName = "cloud";
}
String selectSql = "SELECT * FROM `?`.`?` WHERE ? = ?";
if(tableName.equals("vm_instance")) {
selectSql += " AND state != ? AND removed IS NULL";
}
if (tableName.equals("host") || tableName.equals("cluster") || tableName.equals("volumes")) {
selectSql += " and removed IS NULL";
}
final TransactionLegacy txn = TransactionLegacy.currentTxn();
try {
final PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql);
stmt.setString(1,dbName);
stmt.setString(2,tableName);
stmt.setString(3,column);
stmt.setLong(4, podId);
if(tableName.equals("vm_instance")) {
stmt.setString(5, VirtualMachine.State.Expunging.toString());
}
final ResultSet rs = stmt.executeQuery();
if (rs != null && rs.next()) {
throw new CloudRuntimeException("The pod cannot be deleted because " + errorMsg);
}
} catch (final SQLException ex) {
throw new CloudRuntimeException("The Management Server failed to detect if pod is deletable. Please contact Cloud Support.");
}
// Check if there are any non-removed clusters in the pod.
if (!_clusterDao.listByPodId(podId).isEmpty()) {
throw new CloudRuntimeException(errorMsg + "there are clusters in this pod.");
}
}
@ -1334,104 +1296,49 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
@DB
protected void checkIfZoneIsDeletable(final long zoneId) {
final List<List<String>> tablesToCheck = new ArrayList<List<String>>();
final String errorMsg = "The zone cannot be deleted because ";
final List<String> host = new ArrayList<String>();
host.add(0, "host");
host.add(1, "data_center_id");
host.add(2, "there are servers running in this zone");
tablesToCheck.add(host);
final List<String> hostPodRef = new ArrayList<String>();
hostPodRef.add(0, "host_pod_ref");
hostPodRef.add(1, "data_center_id");
hostPodRef.add(2, "there are pods in this zone");
tablesToCheck.add(hostPodRef);
final List<String> privateIP = new ArrayList<String>();
privateIP.add(0, "op_dc_ip_address_alloc");
privateIP.add(1, "data_center_id");
privateIP.add(2, "there are private IP addresses allocated for this zone");
tablesToCheck.add(privateIP);
final List<String> publicIP = new ArrayList<String>();
publicIP.add(0, "user_ip_address");
publicIP.add(1, "data_center_id");
publicIP.add(2, "there are public IP addresses allocated for this zone");
tablesToCheck.add(publicIP);
final List<String> vmInstance = new ArrayList<String>();
vmInstance.add(0, "vm_instance");
vmInstance.add(1, "data_center_id");
vmInstance.add(2, "there are virtual machines running in this zone");
tablesToCheck.add(vmInstance);
final List<String> volumes = new ArrayList<String>();
volumes.add(0, "volumes");
volumes.add(1, "data_center_id");
volumes.add(2, "there are storage volumes for this zone");
tablesToCheck.add(volumes);
final List<String> physicalNetworks = new ArrayList<String>();
physicalNetworks.add(0, "physical_network");
physicalNetworks.add(1, "data_center_id");
physicalNetworks.add(2, "there are physical networks in this zone");
tablesToCheck.add(physicalNetworks);
final List<String> vmwareDcs = new ArrayList<String>();
vmwareDcs.add(0, "vmware_data_center_zone_map");
vmwareDcs.add(1, "zone_id");
vmwareDcs.add(2, "there are VMware datacenters associated with this zone. Remove VMware DC from this zone.");
tablesToCheck.add(vmwareDcs);
for (final List<String> table : tablesToCheck) {
final String tableName = table.get(0);
final String column = table.get(1);
final String errorMsg = table.get(2);
final String dbName = "cloud";
String selectSql = "SELECT * FROM `?`.`?` WHERE ? = ?";
if (tableName.equals("op_dc_vnet_alloc")) {
selectSql += " AND taken IS NOT NULL";
}
if (tableName.equals("user_ip_address")) {
selectSql += " AND state!='Free'";
}
if (tableName.equals("op_dc_ip_address_alloc")) {
selectSql += " AND taken IS NOT NULL";
}
if (tableName.equals("host_pod_ref") || tableName.equals("host") || tableName.equals("volumes") || tableName.equals("physical_network")) {
selectSql += " AND removed is NULL";
}
if (tableName.equals("vm_instance")) {
selectSql += " AND state != ? AND removed IS NULL";
}
final TransactionLegacy txn = TransactionLegacy.currentTxn();
try {
final PreparedStatement stmt = txn.prepareAutoCloseStatement(selectSql);
stmt.setString(1,dbName);
stmt.setString(2,tableName);
stmt.setString(3,column);
stmt.setLong(4, zoneId);
if (tableName.equals("vm_instance")) {
stmt.setString(5, VirtualMachine.State.Expunging.toString());
}
final ResultSet rs = stmt.executeQuery();
if (rs != null && rs.next()) {
throw new CloudRuntimeException("The zone is not deletable because " + errorMsg);
}
} catch (final SQLException ex) {
throw new CloudRuntimeException("The Management Server failed to detect if zone is deletable. Please contact Cloud Support.");
}
// Check if there are any non-removed hosts in the zone.
if (!_hostDao.listByDataCenterId(zoneId).isEmpty()) {
throw new CloudRuntimeException(errorMsg + "there are servers in this zone.");
}
// Check if there are any non-removed pods in the zone.
if (!_podDao.listByDataCenterId(zoneId).isEmpty()) {
throw new CloudRuntimeException(errorMsg + "there are pods in this zone.");
}
// Check if there are allocated private IP addresses in the zone.
if (_privateIpAddressDao.countIPs(zoneId, true) != 0) {
throw new CloudRuntimeException(errorMsg + "there are private IP addresses allocated in this zone.");
}
// Check if there are allocated public IP addresses in the zone.
if (_publicIpAddressDao.countIPs(zoneId, true) != 0) {
throw new CloudRuntimeException(errorMsg + "there are public IP addresses allocated in this zone.");
}
// Check if there are any non-removed vms in the zone.
if (!_vmInstanceDao.listByZoneId(zoneId).isEmpty()) {
throw new CloudRuntimeException(errorMsg + "there are virtual machines in this zone.");
}
// Check if there are any non-removed volumes in the zone.
if (!_volumeDao.findByDc(zoneId).isEmpty()) {
throw new CloudRuntimeException(errorMsg + "there are storage volumes in this zone.");
}
// Check if there are any non-removed physical networks in the zone.
if (!_physicalNetworkDao.listByZone(zoneId).isEmpty()) {
throw new CloudRuntimeException(errorMsg + "there are physical networks in this zone.");
}
// Check if there are any non-removed VMware datacenters in the zone.
//if (_vmwareDatacenterZoneMapDao.findByZoneId(zoneId) != null) {
// throw new CloudRuntimeException(errorMsg + "there are VMware datacenters in this zone.");
//}
}
private void checkZoneParameters(final String zoneName, final String dns1, final String dns2, final String internalDns1, final String internalDns2, final boolean checkForDuplicates, final Long domainId,

View File

@ -30,6 +30,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import com.cloud.user.User;
@ -48,14 +49,21 @@ import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationSe
import com.cloud.configuration.Resource.ResourceType;
import com.cloud.dc.AccountVlanMapVO;
import com.cloud.dc.ClusterVO;
import com.cloud.dc.DataCenter.NetworkType;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.HostPodVO;
import com.cloud.dc.Vlan;
import com.cloud.dc.VlanVO;
import com.cloud.dc.dao.AccountVlanMapDao;
import com.cloud.dc.dao.ClusterDao;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.dc.dao.DataCenterIpAddressDao;
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.IpAddressManager;
import com.cloud.network.Network;
import com.cloud.network.NetworkModel;
@ -63,7 +71,11 @@ import com.cloud.network.Network.Capability;
import com.cloud.network.dao.FirewallRulesDao;
import com.cloud.network.dao.IPAddressDao;
import com.cloud.network.dao.IPAddressVO;
import com.cloud.network.dao.PhysicalNetworkDao;
import com.cloud.network.dao.PhysicalNetworkVO;
import com.cloud.projects.ProjectManager;
import com.cloud.storage.VolumeVO;
import com.cloud.storage.dao.VolumeDao;
import com.cloud.user.Account;
import com.cloud.user.AccountManager;
import com.cloud.user.AccountVO;
@ -71,7 +83,10 @@ import com.cloud.user.ResourceLimitService;
import com.cloud.user.UserVO;
import com.cloud.user.dao.AccountDao;
import com.cloud.utils.db.TransactionLegacy;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.net.Ip;
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.dao.VMInstanceDao;
public class ConfigurationManagerTest {
@ -109,12 +124,25 @@ public class ConfigurationManagerTest {
IpAddressManager _ipAddrMgr;
@Mock
NetworkModel _networkModel;
@Mock
DataCenterIpAddressDao _privateIpAddressDao;
@Mock
VolumeDao _volumeDao;
@Mock
HostDao _hostDao;
@Mock
VMInstanceDao _vmInstanceDao;
@Mock
ClusterDao _clusterDao;
@Mock
HostPodDao _podDao;
@Mock
PhysicalNetworkDao _physicalNetworkDao;
VlanVO vlan = new VlanVO(Vlan.VlanType.VirtualNetwork, "vlantag", "vlangateway", "vlannetmask", 1L, "iprange", 1L, 1L, null, null, null);
@Mock
Network network;
@Mock
Account account;
@ -133,6 +161,14 @@ public class ConfigurationManagerTest {
configurationMgr._firewallDao = _firewallDao;
configurationMgr._ipAddrMgr = _ipAddrMgr;
configurationMgr._networkModel = _networkModel;
configurationMgr._privateIpAddressDao = _privateIpAddressDao;
configurationMgr._volumeDao = _volumeDao;
configurationMgr._hostDao = _hostDao;
configurationMgr._vmInstanceDao = _vmInstanceDao;
configurationMgr._clusterDao = _clusterDao;
configurationMgr._podDao = _podDao;
configurationMgr._physicalNetworkDao = _physicalNetworkDao;
Account account = new AccountVO("testaccount", 1, "networkdomain", (short)0, UUID.randomUUID().toString());
when(configurationMgr._accountMgr.getAccount(anyLong())).thenReturn(account);
@ -533,4 +569,230 @@ public class ConfigurationManagerTest {
Mockito.when(_accountMgr.getAccount(1l)).thenReturn(account);
Assert.assertNotNull(configurationMgr.getVlanAccount(42l));
}
}
@Test
public void checkIfPodIsDeletableSuccessTest() {
HostPodVO hostPodVO = Mockito.mock(HostPodVO.class);
Mockito.when(hostPodVO.getDataCenterId()).thenReturn(new Random().nextLong());
Mockito.when(_podDao.findById(anyLong())).thenReturn(hostPodVO);
Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList<VolumeVO>());
Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList<HostVO>());
Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList<ClusterVO>());
configurationMgr.checkIfPodIsDeletable(new Random().nextLong());
}
@Test(expected = CloudRuntimeException.class)
public void checkIfPodIsDeletableFailureOnPrivateIpAddressTest() {
HostPodVO hostPodVO = Mockito.mock(HostPodVO.class);
Mockito.when(hostPodVO.getDataCenterId()).thenReturn(new Random().nextLong());
Mockito.when(_podDao.findById(anyLong())).thenReturn(hostPodVO);
Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(1);
Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList<VolumeVO>());
Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList<HostVO>());
Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList<ClusterVO>());
configurationMgr.checkIfPodIsDeletable(new Random().nextLong());
}
@Test(expected = CloudRuntimeException.class)
public void checkIfPodIsDeletableFailureOnVolumeTest() {
HostPodVO hostPodVO = Mockito.mock(HostPodVO.class);
Mockito.when(hostPodVO.getDataCenterId()).thenReturn(new Random().nextLong());
Mockito.when(_podDao.findById(anyLong())).thenReturn(hostPodVO);
VolumeVO volumeVO = Mockito.mock(VolumeVO.class);
ArrayList<VolumeVO> arrayList = new ArrayList<VolumeVO>();
arrayList.add(volumeVO);
Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(arrayList);
Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList<HostVO>());
Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList<ClusterVO>());
configurationMgr.checkIfPodIsDeletable(new Random().nextLong());
}
@Test(expected = CloudRuntimeException.class)
public void checkIfPodIsDeletableFailureOnHostTest() {
HostPodVO hostPodVO = Mockito.mock(HostPodVO.class);
Mockito.when(hostPodVO.getDataCenterId()).thenReturn(new Random().nextLong());
Mockito.when(_podDao.findById(anyLong())).thenReturn(hostPodVO);
HostVO hostVO = Mockito.mock(HostVO.class);
ArrayList<HostVO> arrayList = new ArrayList<HostVO>();
arrayList.add(hostVO);
Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList<VolumeVO>());
Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(arrayList);
Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList<ClusterVO>());
configurationMgr.checkIfPodIsDeletable(new Random().nextLong());
}
@Test(expected = CloudRuntimeException.class)
public void checkIfPodIsDeletableFailureOnVmInstanceTest() {
HostPodVO hostPodVO = Mockito.mock(HostPodVO.class);
Mockito.when(hostPodVO.getDataCenterId()).thenReturn(new Random().nextLong());
Mockito.when(_podDao.findById(anyLong())).thenReturn(hostPodVO);
VMInstanceVO vMInstanceVO = Mockito.mock(VMInstanceVO.class);
ArrayList<VMInstanceVO> arrayList = new ArrayList<VMInstanceVO>();
arrayList.add(vMInstanceVO);
Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList<VolumeVO>());
Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList<HostVO>());
Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(arrayList);
Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList<ClusterVO>());
configurationMgr.checkIfPodIsDeletable(new Random().nextLong());
}
@Test(expected = CloudRuntimeException.class)
public void checkIfPodIsDeletableFailureOnClusterTest() {
HostPodVO hostPodVO = Mockito.mock(HostPodVO.class);
Mockito.when(hostPodVO.getDataCenterId()).thenReturn(new Random().nextLong());
Mockito.when(_podDao.findById(anyLong())).thenReturn(hostPodVO);
ClusterVO clusterVO = Mockito.mock(ClusterVO.class);
ArrayList<ClusterVO> arrayList = new ArrayList<ClusterVO>();
arrayList.add(clusterVO);
Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList<VolumeVO>());
Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList<HostVO>());
Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(arrayList);
configurationMgr.checkIfPodIsDeletable(new Random().nextLong());
}
@Test
public void checkIfZoneIsDeletableSuccessTest() {
Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
}
@Test(expected = CloudRuntimeException.class)
public void checkIfZoneIsDeletableFailureOnHostTest() {
HostVO hostVO = Mockito.mock(HostVO.class);
ArrayList<HostVO> arrayList = new ArrayList<HostVO>();
arrayList.add(hostVO);
Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(arrayList);
Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
}
@Test(expected = CloudRuntimeException.class)
public void checkIfZoneIsDeletableFailureOnPodTest() {
HostPodVO hostPodVO = Mockito.mock(HostPodVO.class);
ArrayList<HostPodVO> arrayList = new ArrayList<HostPodVO>();
arrayList.add(hostPodVO);
Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(arrayList);
Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
}
@Test(expected = CloudRuntimeException.class)
public void checkIfZoneIsDeletableFailureOnPrivateIpAddressTest() {
Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(1);
Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
}
@Test(expected = CloudRuntimeException.class)
public void checkIfZoneIsDeletableFailureOnPublicIpAddressTest() {
Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(1);
Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
}
@Test(expected = CloudRuntimeException.class)
public void checkIfZoneIsDeletableFailureOnVmInstanceTest() {
VMInstanceVO vMInstanceVO = Mockito.mock(VMInstanceVO.class);
ArrayList<VMInstanceVO> arrayList = new ArrayList<VMInstanceVO>();
arrayList.add(vMInstanceVO);
Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(arrayList);
Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
}
@Test(expected = CloudRuntimeException.class)
public void checkIfZoneIsDeletableFailureOnVolumeTest() {
VolumeVO volumeVO = Mockito.mock(VolumeVO.class);
ArrayList<VolumeVO> arrayList = new ArrayList<VolumeVO>();
arrayList.add(volumeVO);
Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(arrayList);
Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
}
@Test(expected = CloudRuntimeException.class)
public void checkIfZoneIsDeletableFailureOnPhysicalNetworkTest() {
PhysicalNetworkVO physicalNetworkVO = Mockito.mock(PhysicalNetworkVO.class);
ArrayList<PhysicalNetworkVO> arrayList = new ArrayList<PhysicalNetworkVO>();
arrayList.add(physicalNetworkVO);
Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(new ArrayList<VMInstanceVO>());
Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(arrayList);
configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
}
}