DRS: Ensure the destination host is part of the same cluster (#9245)

* DRS: Ensure the destination host is part of the same cluster

* Add and fix unit tests
This commit is contained in:
Vishesh 2024-06-26 02:09:26 +05:30 committed by GitHub
parent 6d7c042bc1
commit 2bf36ef935
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 55 additions and 7 deletions

View File

@ -467,7 +467,7 @@ public class ClusterDrsServiceImpl extends ManagerBase implements ClusterDrsServ
Map<Host, Boolean> requiresStorageMotion = hostsForMigrationOfVM.third();
for (Host destHost : compatibleDestinationHosts) {
if (!suitableDestinationHosts.contains(destHost)) {
if (!suitableDestinationHosts.contains(destHost) || cluster.getId() != destHost.getClusterId()) {
continue;
}
Ternary<Double, Double, Double> metrics = algorithm.getMetrics(cluster.getId(), vm,

View File

@ -73,6 +73,7 @@ import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
@RunWith(MockitoJUnitRunner.class)
public class ClusterDrsServiceImplTest {
@ -353,6 +354,7 @@ public class ClusterDrsServiceImplTest {
Mockito.when(cluster.getId()).thenReturn(1L);
HostVO destHost = Mockito.mock(HostVO.class);
Mockito.when(destHost.getClusterId()).thenReturn(1L);
HostVO host = Mockito.mock(HostVO.class);
Mockito.when(host.getId()).thenReturn(2L);
@ -386,13 +388,9 @@ public class ClusterDrsServiceImplTest {
}
Mockito.when(managementServer.listHostsForMigrationOfVM(vm1, 0L, 500L, null, vmList)).thenReturn(
new Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Host, Boolean>>(
new Pair<>(List.of(destHost), 1), List.of(destHost), Map.of(destHost,
false)));
new Ternary<>(new Pair<>(List.of(destHost), 1), List.of(destHost), Map.of(destHost, false)));
Mockito.when(managementServer.listHostsForMigrationOfVM(vm2, 0L, 500L, null, vmList)).thenReturn(
new Ternary<Pair<List<? extends Host>, Integer>, List<? extends Host>, Map<Host, Boolean>>(
new Pair<>(List.of(destHost), 1), List.of(destHost), Map.of(destHost,
false)));
new Ternary<>(new Pair<>(List.of(destHost), 1), List.of(destHost), Map.of(destHost, false)));
Mockito.when(balancedAlgorithm.getMetrics(cluster.getId(), vm1, serviceOffering, destHost, new HashMap<>(),
new HashMap<>(), false)).thenReturn(new Ternary<>(1.0, 0.5, 1.5));
@ -406,6 +404,56 @@ public class ClusterDrsServiceImplTest {
assertEquals(vm1, bestMigration.first());
}
@Test
public void testGetBestMigrationDifferentCluster() throws ConfigurationException {
ClusterVO cluster = Mockito.mock(ClusterVO.class);
Mockito.when(cluster.getId()).thenReturn(1L);
HostVO destHost = Mockito.mock(HostVO.class);
Mockito.when(destHost.getClusterId()).thenReturn(2L);
HostVO host = Mockito.mock(HostVO.class);
Mockito.when(host.getId()).thenReturn(2L);
VMInstanceVO vm1 = Mockito.mock(VMInstanceVO.class);
Mockito.when(vm1.getId()).thenReturn(1L);
Mockito.when(vm1.getType()).thenReturn(VirtualMachine.Type.User);
Mockito.when(vm1.getState()).thenReturn(VirtualMachine.State.Running);
Mockito.when(vm1.getDetails()).thenReturn(Collections.emptyMap());
VMInstanceVO vm2 = Mockito.mock(VMInstanceVO.class);
Mockito.when(vm2.getId()).thenReturn(2L);
Mockito.when(vm2.getType()).thenReturn(VirtualMachine.Type.User);
Mockito.when(vm2.getState()).thenReturn(VirtualMachine.State.Running);
Mockito.when(vm2.getDetails()).thenReturn(Collections.emptyMap());
List<VirtualMachine> vmList = new ArrayList<>();
vmList.add(vm1);
vmList.add(vm2);
Map<Long, List<VirtualMachine>> hostVmMap = new HashMap<>();
hostVmMap.put(host.getId(), new ArrayList<>());
hostVmMap.get(host.getId()).add(vm1);
hostVmMap.get(host.getId()).add(vm2);
Map<Long, ServiceOffering> vmIdServiceOfferingMap = new HashMap<>();
ServiceOffering serviceOffering = Mockito.mock(ServiceOffering.class);
for (VirtualMachine vm : vmList) {
vmIdServiceOfferingMap.put(vm.getId(), serviceOffering);
}
Mockito.when(managementServer.listHostsForMigrationOfVM(vm1, 0L, 500L, null, vmList)).thenReturn(
new Ternary<>(new Pair<>(List.of(destHost), 1), List.of(destHost), Map.of(destHost, false)));
Mockito.when(managementServer.listHostsForMigrationOfVM(vm2, 0L, 500L, null, vmList)).thenReturn(
new Ternary<>(new Pair<>(List.of(destHost), 1), List.of(destHost), Map.of(destHost, false)));
Pair<VirtualMachine, Host> bestMigration = clusterDrsService.getBestMigration(cluster, balancedAlgorithm,
vmList, vmIdServiceOfferingMap, new HashMap<>(), new HashMap<>());
assertNull(bestMigration.second());
assertNull(bestMigration.first());
}
@Test
public void testSavePlan() {
Mockito.when(drsPlanDao.persist(Mockito.any(ClusterDrsPlanVO.class))).thenReturn(