diff --git a/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java b/server/src/main/java/com/cloud/deploy/DeploymentPlanningManagerImpl.java index b686f351adfc..d97cd41283b9 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; @@ -94,6 +96,7 @@ import com.cloud.deploy.DeploymentPlanner.ExcludeList; import com.cloud.deploy.DeploymentPlanner.PlannerResourceUsage; import com.cloud.deploy.dao.PlannerHostReservationDao; +import com.cloud.domain.dao.DomainDao; import com.cloud.exception.AffinityConflictException; import com.cloud.exception.ConnectionException; import com.cloud.exception.InsufficientServerCapacityException; @@ -176,6 +179,8 @@ public class DeploymentPlanningManagerImpl extends ManagerBase implements Deploy @Inject protected AffinityGroupDomainMapDao _affinityGroupDomainMapDao; @Inject + protected DomainDao _domainDao; + @Inject AffinityGroupService _affinityGroupService; @Inject DataCenterDao _dcDao; @@ -199,6 +204,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 +983,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 +1089,85 @@ 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(); + + // Domains whose domain-level dedications the VM owner may use: its own domain and the ancestors + // whose ExplicitDedication group grants subdomain access. + Set accessibleDedicationDomainIds = findDomainsWithAccessibleExplicitDedication(vmDomainId); + + // 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, accessibleDedicationDomainIds, + (domainId, accountId) -> _dedicatedDao.searchDedicatedPods(null, domainId, accountId, null, filter), + DedicatedResourceVO::getPodId); + removeDedicatedIdsAccessibleToVm(allClustersInDc, vmDomainId, vmAccountId, accessibleDedicationDomainIds, + (domainId, accountId) -> _dedicatedDao.searchDedicatedClusters(null, domainId, accountId, null, filter), + DedicatedResourceVO::getClusterId); + removeDedicatedIdsAccessibleToVm(allHostsInDc, vmDomainId, vmAccountId, accessibleDedicationDomainIds, + (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); } + /** + * Returns the domains whose domain-level dedications are available to a VM owned in {@code vmDomainId}: the + * VM's own domain if it has an ExplicitDedication domain-level affinity group, and each ancestor domain whose + * ExplicitDedication group grants subdomain access (as in {@code AffinityGroupService.isAffinityGroupAvailableInDomain}). + */ + protected Set findDomainsWithAccessibleExplicitDedication(long vmDomainId) { + Set accessibleDomainIds = new HashSet<>(); + for (Long domainId : _domainDao.getDomainParentIds(vmDomainId)) { + AffinityGroupVO group = _affinityGroupDao.findDomainLevelGroupByType(domainId, EXPLICIT_DEDICATION_AFFINITY_GROUP_TYPE); + if (group == null) { + continue; + } + if (domainId == vmDomainId) { + accessibleDomainIds.add(domainId); + continue; + } + AffinityGroupDomainMapVO domainMap = _affinityGroupDomainMapDao.findByAffinityGroup(group.getId()); + if (domainMap != null && Boolean.TRUE.equals(domainMap.isSubdomainAccess())) { + accessibleDomainIds.add(domainId); + } + } + return accessibleDomainIds; + } + + /** + * Removes from {@code dedicatedIdsInDc} the ids of the resources the VM owner may use: those dedicated to the + * owner's account and those dedicated (without an account) to any of {@code accessibleDedicationDomainIds}. + * Resources dedicated to other accounts, in the VM's domain or an ancestor, stay in the list. + */ + private void removeDedicatedIdsAccessibleToVm(List dedicatedIdsInDc, long vmDomainId, long vmAccountId, Set accessibleDedicationDomainIds, + 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)); + } + for (Long domainId : accessibleDedicationDomainIds) { + for (DedicatedResourceVO vo : searchDedicated.apply(domainId, 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..ed57768f19fc 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; @@ -35,6 +36,7 @@ import com.cloud.deploy.DeploymentPlanner.ExcludeList; import com.cloud.deploy.DeploymentPlanner.PlannerResourceUsage; import com.cloud.deploy.dao.PlannerHostReservationDao; +import com.cloud.domain.dao.DomainDao; import com.cloud.exception.AffinityConflictException; import com.cloud.exception.InsufficientServerCapacityException; import com.cloud.gpu.GPU; @@ -89,7 +91,9 @@ import com.cloud.vm.dao.VMInstanceDetailsDao; import com.cloud.vm.dao.VMInstanceDao; import org.apache.cloudstack.affinity.AffinityGroupProcessor; +import org.apache.cloudstack.affinity.AffinityGroupDomainMapVO; 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 +247,18 @@ public class DeploymentPlanningManagerImplTest { @Inject HostPodDao _podDao; + @Inject + HostDao _hostDao; + + @Mock + AffinityGroupDao _affinityGroupDao; + + @Mock + AffinityGroupDomainMapDao _affinityGroupDomainMapDao; + + @Mock + DomainDao _domainDao; + private static final long dataCenterId = 1L; private static final long instanceId = 123L; private static final long hostId = 0L; @@ -1105,6 +1121,420 @@ 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); + + // The VM's domain has no parent domain unless a test says otherwise + Mockito.when(_domainDao.getDomainParentIds(vmDomainId)).thenReturn(Set.of(vmDomainId)); + + // 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)); + } + + /** + * Sets up a VM in {@code childDomainId} whose parent domain {@code parentDomainId} has a pod dedicated to it. + * The parent's ExplicitDedication group grants subdomain access as given; the child has no group of its own. + */ + private ExcludeList deployInChildDomainOfPodDedicatedToParent(long parentDomainId, long childDomainId, long vmAccountId, + long dedicatedPodId, boolean parentGroupSubdomainAccess) { + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(childDomainId, vmAccountId); + Mockito.when(_domainDao.getDomainParentIds(childDomainId)).thenReturn(Set.of(childDomainId, parentDomainId)); + + Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + + long parentGroupId = 77L; + AffinityGroupVO parentGroup = Mockito.mock(AffinityGroupVO.class); + Mockito.when(parentGroup.getId()).thenReturn(parentGroupId); + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(childDomainId, "ExplicitDedication")).thenReturn(null); + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(parentDomainId, "ExplicitDedication")).thenReturn(parentGroup); + Mockito.when(_affinityGroupDomainMapDao.findByAffinityGroup(parentGroupId)) + .thenReturn(new AffinityGroupDomainMapVO(parentGroupId, parentDomainId, parentGroupSubdomainAccess)); + + // The pod is dedicated to the parent domain as a whole (accountId = null) + DedicatedResourceVO dedicatedPod = Mockito.mock(DedicatedResourceVO.class); + Mockito.when(dedicatedPod.getPodId()).thenReturn(dedicatedPodId); + Mockito.when(_dedicatedDao.searchDedicatedPods(Mockito.isNull(), Mockito.eq(parentDomainId), + 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); + return avoids; + } + + /** + * A user in a child domain inherits a pod dedicated to its parent domain, as explicit dedication does + * through subdomain access, so the pod must not be in the avoid list. + */ + @Test + public void checkForNonDedicatedResources_podDedicatedToParentDomain_childDomainUser_podNotInAvoidList() { + long dedicatedPodId = 46L; + ExcludeList avoids = deployInChildDomainOfPodDedicatedToParent(10L, 11L, 210L, dedicatedPodId, true); + + assertFalse("Pod dedicated to the parent domain should not be in avoid list for a child-domain user", + avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); + } + + /** + * If the parent domain's ExplicitDedication group does not grant subdomain access, + * a child-domain user must stay off the pod dedicated to the parent domain. + */ + @Test + public void checkForNonDedicatedResources_podDedicatedToParentDomainWithoutSubdomainAccess_childDomainUser_podInAvoidList() { + long dedicatedPodId = 47L; + ExcludeList avoids = deployInChildDomainOfPodDedicatedToParent(10L, 11L, 210L, dedicatedPodId, false); + + assertTrue("Pod dedicated to the parent domain without subdomain access should be in avoid list for a child-domain user", + avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); + } + + /** + * A pod dedicated to an account in the parent domain is not inherited by a child-domain user: + * only domain-wide dedications of ancestor domains are. + */ + @Test + public void checkForNonDedicatedResources_podDedicatedToAccountInParentDomain_childDomainUser_podInAvoidList() { + long parentDomainId = 10L; + long childDomainId = 11L; + long vmAccountId = 210L; + long dedicatedPodId = 48L; + + DataCenter mockDc = Mockito.mock(DataCenter.class); + Mockito.when(mockDc.getId()).thenReturn(dataCenterId); + + VMInstanceVO mockVm = setupNonExplicitUserVmMocks(childDomainId, vmAccountId); + Mockito.when(_domainDao.getDomainParentIds(childDomainId)).thenReturn(Set.of(childDomainId, parentDomainId)); + + Mockito.when(_podDao.listAllPods(dataCenterId)).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + Mockito.when(_dedicatedDao.listAllPods()).thenReturn(new ArrayList<>(Arrays.asList(dedicatedPodId))); + + // Parent domain has an ExplicitDedication group with subdomain access, but no domain-wide dedications: + // the pod is dedicated to another account in the parent domain, which the default search stub does not return. + long parentGroupId = 78L; + AffinityGroupVO parentGroup = Mockito.mock(AffinityGroupVO.class); + Mockito.when(parentGroup.getId()).thenReturn(parentGroupId); + Mockito.when(_affinityGroupDao.findDomainLevelGroupByType(parentDomainId, "ExplicitDedication")).thenReturn(parentGroup); + Mockito.when(_affinityGroupDomainMapDao.findByAffinityGroup(parentGroupId)) + .thenReturn(new AffinityGroupDomainMapVO(parentGroupId, parentDomainId, true)); + + 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); + + assertTrue("Pod dedicated to an account in the parent domain should be in avoid list for a child-domain user", + avoids.getPodsToAvoid() != null && avoids.getPodsToAvoid().contains(dedicatedPodId)); + } + @Configuration @ComponentScan(basePackageClasses = {DeploymentPlanningManagerImpl.class}, includeFilters = {@Filter(value = TestConfiguration.Library.class, @@ -1267,6 +1697,11 @@ public AffinityGroupDao affinityGroupDao() { return Mockito.mock(AffinityGroupDao.class); } + @Bean + public DomainDao domainDao() { + return Mockito.mock(DomainDao.class); + } + @Bean public AffinityGroupVMMapDao affinityGroupVMMapDao() { return Mockito.mock(AffinityGroupVMMapDao.class);