diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index b686f351adfc..231fde0c97e6 100644 --- a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java +++ b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java @@ -31,6 +31,8 @@ import java.util.Set; import java.util.Timer; import java.util.TreeSet; +import java.util.function.BiFunction; +import java.util.function.Function; import java.util.stream.Collectors; import javax.inject.Inject; @@ -199,6 +201,7 @@ public class DeploymentPlanningManagerImpl extends ManagerBase implements Deploy private static final long ADMIN_ACCOUNT_ROLE_ID = 1l; private static final long INITIAL_RESERVATION_RELEASE_CHECKER_DELAY = 30L * 1000L; // thirty seconds expressed in milliseconds + private static final String EXPLICIT_DEDICATION_AFFINITY_GROUP_TYPE = "ExplicitDedication"; protected long _nodeId = -1; protected static final List DEPLOYMENT_PLANNING_SKIP_HYPERVISORS = Arrays.asList( HypervisorType.BareMetal, HypervisorType.External); @@ -977,7 +980,7 @@ public void checkForNonDedicatedResources(VirtualMachineProfile vmProfile, DataC // check affinity group of type Explicit dedication exists. If No put // dedicated pod/cluster/host in avoid list - List vmGroupMappings = _affinityGroupVMMapDao.findByVmIdType(vm.getId(), "ExplicitDedication"); + List vmGroupMappings = _affinityGroupVMMapDao.findByVmIdType(vm.getId(), EXPLICIT_DEDICATION_AFFINITY_GROUP_TYPE); if (vmGroupMappings != null && !vmGroupMappings.isEmpty()) { isExplicit = true; @@ -1083,13 +1086,61 @@ private void findAvoiSetForRouterVM(ExcludeList avoids, VirtualMachine vm, List< } private void findAvoidSetForNonExplicitUserVM(ExcludeList avoids, VirtualMachine vm, List allPodsInDc, List allClustersInDc, List allHostsInDc) { + if (allPodsInDc.isEmpty() && allClustersInDc.isEmpty() && allHostsInDc.isEmpty()) { + // Nothing in this DC is dedicated to anyone, so there is nothing to avoid or to check dedication for. + return; + } + + long vmAccountId = vm.getAccountId(); + long vmDomainId = vm.getDomainId(); + + // If the VM owner's domain has an ExplicitDedication domain-level affinity group, + // resources dedicated to that domain are accessible to the VM owner as well. + boolean hasDomainExplicitDedicationGroup = + _affinityGroupDao.findDomainLevelGroupByType(vmDomainId, EXPLICIT_DEDICATION_AFFINITY_GROUP_TYPE) != null; + + // Sort by id ascending, no pagination — retrieve all dedicated resources for this domain/account + Filter filter = new Filter(DedicatedResourceVO.class, "id", true); + + // Only query the resource types that have dedicated entries in this DC. + removeDedicatedIdsAccessibleToVm(allPodsInDc, vmDomainId, vmAccountId, hasDomainExplicitDedicationGroup, + (domainId, accountId) -> _dedicatedDao.searchDedicatedPods(null, domainId, accountId, null, filter), + DedicatedResourceVO::getPodId); + removeDedicatedIdsAccessibleToVm(allClustersInDc, vmDomainId, vmAccountId, hasDomainExplicitDedicationGroup, + (domainId, accountId) -> _dedicatedDao.searchDedicatedClusters(null, domainId, accountId, null, filter), + DedicatedResourceVO::getClusterId); + removeDedicatedIdsAccessibleToVm(allHostsInDc, vmDomainId, vmAccountId, hasDomainExplicitDedicationGroup, + (domainId, accountId) -> _dedicatedDao.searchDedicatedHosts(null, domainId, accountId, null, filter), + DedicatedResourceVO::getHostId); + logger.debug(() -> LogUtils.logGsonWithoutException("Adding pods [%s], clusters [%s] and hosts [%s] to the avoid list in the deploy process of user VM [%s], " - + "because this VM is not explicitly dedicated to these components.", allPodsInDc, allClustersInDc, allHostsInDc, vm)); + + "because this VM is not dedicated to these components.", allPodsInDc, allClustersInDc, allHostsInDc, vm)); avoids.addPodList(allPodsInDc); avoids.addClusterList(allClustersInDc); avoids.addHostList(allHostsInDc); } + /** + * Removes from {@code dedicatedIdsInDc} the ids of the resources the VM owner may use: those dedicated to the + * owner's account and, if {@code includeDomainDedications} is set, those dedicated to the owner's domain. + */ + private void removeDedicatedIdsAccessibleToVm(List dedicatedIdsInDc, long vmDomainId, long vmAccountId, boolean includeDomainDedications, + BiFunction, Integer>> searchDedicated, Function idGetter) { + if (dedicatedIdsInDc.isEmpty()) { + return; + } + Set allowedIds = new HashSet<>(); + for (DedicatedResourceVO vo : searchDedicated.apply(vmDomainId, vmAccountId).first()) { + allowedIds.add(idGetter.apply(vo)); + } + if (includeDomainDedications) { + for (DedicatedResourceVO vo : searchDedicated.apply(vmDomainId, null).first()) { + allowedIds.add(idGetter.apply(vo)); + } + } + dedicatedIdsInDc.removeAll(allowedIds); + } + private void resetAvoidSet(ExcludeList avoidSet, ExcludeList removeSet) { if (avoidSet.getDataCentersToAvoid() != null && removeSet.getDataCentersToAvoid() != null) { avoidSet.getDataCentersToAvoid().removeAll(removeSet.getDataCentersToAvoid()); diff --git a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java index 978cbfd98af0..36e2565be407 100644 --- a/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java +++ b/server/src/test/java/com/cloud/deploy/DeploymentPlanningManagerImplTest.java @@ -27,6 +27,7 @@ import com.cloud.dc.ClusterVO; import com.cloud.dc.DataCenter; import com.cloud.dc.DataCenterVO; +import com.cloud.dc.DedicatedResourceVO; import com.cloud.dc.HostPodVO; import com.cloud.dc.dao.ClusterDao; import com.cloud.dc.dao.DataCenterDao; @@ -90,6 +91,7 @@ import com.cloud.vm.dao.VMInstanceDao; import org.apache.cloudstack.affinity.AffinityGroupProcessor; import org.apache.cloudstack.affinity.AffinityGroupService; +import org.apache.cloudstack.affinity.AffinityGroupVO; import org.apache.cloudstack.affinity.dao.AffinityGroupDao; import org.apache.cloudstack.affinity.dao.AffinityGroupDomainMapDao; import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao; @@ -243,6 +245,12 @@ public class DeploymentPlanningManagerImplTest { @Inject HostPodDao _podDao; + @Inject + HostDao _hostDao; + + @Mock + AffinityGroupDao _affinityGroupDao; + private static final long dataCenterId = 1L; private static final long instanceId = 123L; private static final long hostId = 0L; @@ -1105,6 +1113,312 @@ private void assertAvoidIsEmpty(ExcludeList avoids, boolean isDcEmpty, boolean i Assert.assertEquals(isHostsEmpty, CollectionUtils.isEmpty(avoids.getHostsToAvoid())); } + /** + * Helper: set up mocks for checkForNonDedicatedResources tests. + * Sets up a non-root, non-explicit user VM with the given domain and account. + */ + private VMInstanceVO setupNonExplicitUserVmMocks(long vmDomainId, long vmAccountId) { + // Zone is not dedicated + Mockito.when(_dedicatedDao.findByZoneId(Mockito.anyLong())).thenReturn(null); + // Not root admin + Mockito.when(_accountMgr.isRootAdmin(Mockito.anyLong())).thenReturn(false); + // No explicit dedication affinity group for the VM + Mockito.when(_affinityGroupVMMapDao.findByVmIdType(Mockito.anyLong(), Mockito.eq("ExplicitDedication"))) + .thenReturn(new ArrayList<>()); + + VMInstanceVO mockVm = Mockito.mock(VMInstanceVO.class); + Mockito.when(mockVm.getType()).thenReturn(VirtualMachine.Type.User); + Mockito.when(mockVm.getDomainId()).thenReturn(vmDomainId); + Mockito.when(mockVm.getAccountId()).thenReturn(vmAccountId); + Mockito.when(mockVm.getId()).thenReturn(instanceId); + + // No dedicated clusters or hosts in DC + Mockito.when(_clusterDao.listAllClusterIds(Mockito.anyLong())).thenReturn(new ArrayList<>()); + Mockito.when(_dedicatedDao.listAllClusters()).thenReturn(new ArrayList<>()); + Mockito.when(_hostDao.listAllHosts(Mockito.anyLong())).thenReturn(new ArrayList<>()); + Mockito.when(_dedicatedDao.listAllHosts()).thenReturn(new ArrayList<>()); + Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) + .thenReturn(new Pair<>(new ArrayList<>(), 0)); + Mockito.when(_dedicatedDao.searchDedicatedClusters(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) + .thenReturn(new Pair<>(new ArrayList<>(), 0)); + Mockito.when(_dedicatedDao.searchDedicatedHosts(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) + .thenReturn(new Pair<>(new ArrayList<>(), 0)); + + return mockVm; + } + + /** + * A regular user in the same domain as a domain-dedicated pod + * must be allowed to deploy on that pod (i.e., the pod must NOT appear in the avoid list). + */ + @Test + public void checkForNonDedicatedResources_domainDedicatedPod_sameDomainUser_podNotInAvoidList() { + long vmDomainId = 10L; + long vmAccountId = 200L; + long dedicatedPodId = 42L; + + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId); + + // Pod in DC and it is dedicated + List podsInDc = new ArrayList<>(Arrays.asList(dedicatedPodId)); + Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(podsInDc); + Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + + // Domain has an ExplicitDedication domain-level affinity group (pod dedicated to this domain). + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication")) + .thenReturn(Mockito.mock(AffinityGroupVO.class)); + + // The pod is found when searching by domain (accountId = null) + DedicatedResourceVO dedicatedPod = Mockito.mock(DedicatedResourceVO.class); + Mockito.when(dedicatedPod.getPodId()).thenReturn(dedicatedPodId); + Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.isNull(), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedPod)), 1)); + + VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); + Mockito.when(mockProfile.getVirtualMachine()).thenReturn(mockVm); + Mockito.when(mockProfile.getOwner()).thenReturn(Mockito.mock(com.cloud.user.Account.class)); + + ExcludeList avoids = new ExcludeList(); + _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); + + // The dedicated pod belongs to the same domain as the VM owner: must NOT be in avoid list + assertFalse("Domain-dedicated pod should not be in avoid list for same-domain user", + avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); + } + + /** + * A user from a different domain must NOT be allowed to deploy on a pod dedicated to another domain. + */ + @Test + public void checkForNonDedicatedResources_domainDedicatedPod_differentDomainUser_podInAvoidList() { + long podDomainId = 10L; // domain the pod is dedicated to + long vmDomainId = 20L; // VM owner belongs to a DIFFERENT domain + long vmAccountId = 300L; + long dedicatedPodId = 43L; + + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId); + + // Pod in DC and it is dedicated (to podDomainId, NOT to vmDomainId) + List podsInDc = new ArrayList<>(Arrays.asList(dedicatedPodId)); + Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(podsInDc); + Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + + // VM owner's domain has no ExplicitDedication domain-level affinity group + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication")) + .thenReturn(null); + + // No pods dedicated to the VM owner's account + Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(), 0)); + + VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); + Mockito.when(mockProfile.getVirtualMachine()).thenReturn(mockVm); + Mockito.when(mockProfile.getOwner()).thenReturn(Mockito.mock(com.cloud.user.Account.class)); + + ExcludeList avoids = new ExcludeList(); + _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); + + // The pod is dedicated to a different domain: must be in avoid list + assertTrue("Domain-dedicated pod should be in avoid list for different-domain user", + avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); + } + + /** + * An account-dedicated pod should be accessible to the owning account + * (no domain-level affinity groups, but account-level dedicated resource exists). + */ + @Test + public void checkForNonDedicatedResources_accountDedicatedPod_sameAccount_podNotInAvoidList() { + long vmDomainId = 10L; + long vmAccountId = 200L; + long dedicatedPodId = 44L; + + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId); + + // Pod in DC and it is dedicated + List podsInDc = new ArrayList<>(Arrays.asList(dedicatedPodId)); + Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(podsInDc); + Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + + // Domain has no ExplicitDedication domain-level affinity group (account-level dedication, not domain-level) + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication")) + .thenReturn(null); + + // The pod IS found when searching by account + DedicatedResourceVO dedicatedPod = Mockito.mock(DedicatedResourceVO.class); + Mockito.when(dedicatedPod.getPodId()).thenReturn(dedicatedPodId); + Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedPod)), 1)); + + VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); + Mockito.when(mockProfile.getVirtualMachine()).thenReturn(mockVm); + Mockito.when(mockProfile.getOwner()).thenReturn(Mockito.mock(com.cloud.user.Account.class)); + + ExcludeList avoids = new ExcludeList(); + _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); + + // The pod is dedicated to this account: must NOT be in avoid list + assertFalse("Account-dedicated pod should not be in avoid list for same-account user", + avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); + } + + /** + * A user whose account is different from the account a pod is dedicated to + * must NOT be able to deploy on that pod. + */ + @Test + public void checkForNonDedicatedResources_accountDedicatedPod_differentAccount_podInAvoidList() { + long vmDomainId = 10L; + long vmAccountId = 300L; // different account + long dedicatedPodId = 45L; + + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId); + + // Pod in DC and it is dedicated (to a different account) + List podsInDc = new ArrayList<>(Arrays.asList(dedicatedPodId)); + Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(podsInDc); + Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + + // Domain has no ExplicitDedication domain-level affinity group + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication")) + .thenReturn(null); + + // No pods dedicated to this account + Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(), 0)); + + VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); + Mockito.when(mockProfile.getVirtualMachine()).thenReturn(mockVm); + Mockito.when(mockProfile.getOwner()).thenReturn(Mockito.mock(com.cloud.user.Account.class)); + + ExcludeList avoids = new ExcludeList(); + _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); + + // The pod is dedicated to a different account: must be in avoid list + assertTrue("Account-dedicated pod should be in avoid list for different-account user", + avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); + } + + /** + * The same domain-dedication rule applied to pods above must also apply to + * clusters and hosts, since findAvoidSetForNonExplicitUserVM handles all three resource types. + */ + @Test + public void checkForNonDedicatedResources_domainDedicatedClusterAndHost_sameDomainUser_notInAvoidList() { + long vmDomainId = 10L; + long vmAccountId = 200L; + long dedicatedClusterId = 52L; + long dedicatedHostId = 62L; + + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId); + + // Cluster and host in DC are dedicated + Mockito.when(_clusterDao.listAllClusterIds(dataCenterId)).thenReturn(new ArrayList<>(Arrays.asList(dedicatedClusterId))); + Mockito.when(_dedicatedDao.listAllClusters()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedClusterId))); + Mockito.when(_hostDao.listAllHosts(dataCenterId)).thenReturn(new ArrayList<>(Arrays.asList(dedicatedHostId))); + Mockito.when(_dedicatedDao.listAllHosts()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedHostId))); + + // Domain has an ExplicitDedication domain-level affinity group + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication")) + .thenReturn(Mockito.mock(AffinityGroupVO.class)); + + // The cluster and host are found when searching by domain (accountId = null) + DedicatedResourceVO dedicatedCluster = Mockito.mock(DedicatedResourceVO.class); + Mockito.when(dedicatedCluster.getClusterId()).thenReturn(dedicatedClusterId); + Mockito.when(_dedicatedDao.searchDedicatedClusters(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.isNull(), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedCluster)), 1)); + + DedicatedResourceVO dedicatedHost = Mockito.mock(DedicatedResourceVO.class); + Mockito.when(dedicatedHost.getHostId()).thenReturn(dedicatedHostId); + Mockito.when(_dedicatedDao.searchDedicatedHosts(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.isNull(), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedHost)), 1)); + + VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); + Mockito.when(mockProfile.getVirtualMachine()).thenReturn(mockVm); + Mockito.when(mockProfile.getOwner()).thenReturn(Mockito.mock(com.cloud.user.Account.class)); + + ExcludeList avoids = new ExcludeList(); + _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); + + assertFalse("Domain-dedicated cluster should not be in avoid list for same-domain user", + avoids.getClustersToAvoid() != null && avoids.getClustersToAvoid().contains(dedicatedClusterId)); + assertFalse("Domain-dedicated host should not be in avoid list for same-domain user", + avoids.getHostsToAvoid() != null && avoids.getHostsToAvoid().contains(dedicatedHostId)); + } + + /** + * The same account-dedication rule applied to pods above must also apply to + * clusters and hosts, since findAvoidSetForNonExplicitUserVM handles all three resource types. + */ + @Test + public void checkForNonDedicatedResources_accountDedicatedClusterAndHost_sameAccount_notInAvoidList() { + long vmDomainId = 10L; + long vmAccountId = 200L; + long dedicatedClusterId = 53L; + long dedicatedHostId = 63L; + + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(vmDomainId, vmAccountId); + + // Cluster and host in DC are dedicated + Mockito.when(_clusterDao.listAllClusterIds(dataCenterId)).thenReturn(new ArrayList<>(Arrays.asList(dedicatedClusterId))); + Mockito.when(_dedicatedDao.listAllClusters()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedClusterId))); + Mockito.when(_hostDao.listAllHosts(dataCenterId)).thenReturn(new ArrayList<>(Arrays.asList(dedicatedHostId))); + Mockito.when(_dedicatedDao.listAllHosts()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedHostId))); + + // Domain has no ExplicitDedication domain-level affinity group (account-level dedication, not domain-level) + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(vmDomainId, "ExplicitDedication")) + .thenReturn(null); + + // The cluster and host ARE found when searching by account + DedicatedResourceVO dedicatedCluster = Mockito.mock(DedicatedResourceVO.class); + Mockito.when(dedicatedCluster.getClusterId()).thenReturn(dedicatedClusterId); + Mockito.when(_dedicatedDao.searchDedicatedClusters(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedCluster)), 1)); + + DedicatedResourceVO dedicatedHost = Mockito.mock(DedicatedResourceVO.class); + Mockito.when(dedicatedHost.getHostId()).thenReturn(dedicatedHostId); + Mockito.when(_dedicatedDao.searchDedicatedHosts(Mockito.isNull(), Mockito.eq(vmDomainId), + Mockito.eq(vmAccountId), Mockito.isNull(), Mockito.any(com.cloud.utils.db.Filter.class))) + .thenReturn(new Pair<>(new ArrayList<>(Arrays.asList(dedicatedHost)), 1)); + + VirtualMachineProfileImpl mockProfile = Mockito.mock(VirtualMachineProfileImpl.class); + Mockito.when(mockProfile.getVirtualMachine()).thenReturn(mockVm); + Mockito.when(mockProfile.getOwner()).thenReturn(Mockito.mock(com.cloud.user.Account.class)); + + ExcludeList avoids = new ExcludeList(); + _dpm.checkForNonDedicatedResources(mockProfile, mockDc, avoids); + + assertFalse("Account-dedicated cluster should not be in avoid list for same-account user", + avoids.getClustersToAvoid() != null && avoids.getClustersToAvoid().contains(dedicatedClusterId)); + assertFalse("Account-dedicated host should not be in avoid list for same-account user", + avoids.getHostsToAvoid() != null && avoids.getHostsToAvoid().contains(dedicatedHostId)); + } + @Configuration @ComponentScan(basePackageClasses = {DeploymentPlanningManagerImpl.class}, includeFilters = {@Filter(value = TestConfiguration.Library.class,