Skip to content

Commit ebc8e74

Browse files
Verify equivalent VMs share one candidate host lookup
The grouping could have been disabled and every test would still have passed. Asserts the expensive call is made once for two interchangeable VMs. Signed-off-by: Brad House <bhouse@nexthop.ai>
1 parent a154255 commit ebc8e74

1 file changed

Lines changed: 54 additions & 1 deletion

File tree

server/src/test/java/org/apache/cloudstack/cluster/ClusterDrsServiceImplTest.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,4 +1139,57 @@ public void testNonStrictAntiAffinityIsHonoured() {
11391139
assertFalse("a preferred host must stay available",
11401140
excludes.getHostsToAvoid().contains(31L));
11411141
}
1142-
}
1142+
1143+
@Test
1144+
public void testEquivalentVmsShareOneCandidateHostLookup() throws ConfigurationException {
1145+
// the expensive call is listHostsForMigrationOfVM. Two interchangeable VMs must cost one
1146+
// pass, not two - otherwise the grouping is not actually doing anything.
1147+
ClusterVO cluster = Mockito.mock(ClusterVO.class);
1148+
Mockito.when(cluster.getId()).thenReturn(1L);
1149+
Mockito.when(cluster.getAllocationState()).thenReturn(Grouping.AllocationState.Enabled);
1150+
1151+
HostVO host1 = Mockito.mock(HostVO.class);
1152+
Mockito.when(host1.getId()).thenReturn(1L);
1153+
1154+
List<VMInstanceVO> vmList = new ArrayList<>();
1155+
for (long id : new long[] {1L, 2L}) {
1156+
VMInstanceVO vm = Mockito.mock(VMInstanceVO.class);
1157+
Mockito.when(vm.getId()).thenReturn(id);
1158+
Mockito.when(vm.getHostId()).thenReturn(1L);
1159+
Mockito.when(vm.getType()).thenReturn(VirtualMachine.Type.User);
1160+
Mockito.when(vm.getState()).thenReturn(VirtualMachine.State.Running);
1161+
Mockito.when(vm.getServiceOfferingId()).thenReturn(9L);
1162+
Mockito.lenient().when(vm.getTemplateId()).thenReturn(8L);
1163+
vmList.add(vm);
1164+
}
1165+
1166+
ServiceOfferingVO offering = Mockito.mock(ServiceOfferingVO.class);
1167+
Mockito.when(offering.isDynamic()).thenReturn(false);
1168+
Mockito.when(serviceOfferingDao.findByIdIncludingRemoved(Mockito.anyLong(), Mockito.anyLong()))
1169+
.thenReturn(offering);
1170+
Mockito.when(vmInstanceDetailsDao.listDetailsKeyPairs(Mockito.anyLong()))
1171+
.thenReturn(Collections.emptyMap());
1172+
Mockito.when(volumeDao.findCreatedByInstance(Mockito.anyLong())).thenReturn(Collections.emptyList());
1173+
1174+
HostJoinVO hostJoin1 = Mockito.mock(HostJoinVO.class);
1175+
Mockito.when(hostJoin1.getId()).thenReturn(1L);
1176+
Mockito.when(hostJoin1.getCpus()).thenReturn(4);
1177+
Mockito.when(hostJoin1.getSpeed()).thenReturn(1000L);
1178+
Mockito.when(hostJoin1.getTotalMemory()).thenReturn(8192L);
1179+
1180+
Mockito.when(hostDao.findByClusterId(1L)).thenReturn(List.of(host1));
1181+
Mockito.when(vmInstanceDao.listByClusterId(1L)).thenReturn(vmList);
1182+
Mockito.when(hostJoinDao.searchByIds(Mockito.any())).thenReturn(List.of(hostJoin1));
1183+
Mockito.when(balancedAlgorithm.needsDrs(Mockito.any(), Mockito.anyMap(), Mockito.anyMap(), Mockito.anyMap()))
1184+
.thenReturn(false);
1185+
Mockito.when(managementServer.listHostsForMigrationOfVM(Mockito.any(), Mockito.anyLong(),
1186+
Mockito.anyLong(), Mockito.any(), Mockito.anyList()))
1187+
.thenReturn(new Ternary<>(new Pair<>(Collections.emptyList(), 0),
1188+
List.of(host1), Collections.emptyMap()));
1189+
1190+
clusterDrsService.getDrsPlan(cluster, 5);
1191+
1192+
Mockito.verify(managementServer, Mockito.times(1)).listHostsForMigrationOfVM(
1193+
Mockito.any(), Mockito.anyLong(), Mockito.anyLong(), Mockito.any(), Mockito.anyList());
1194+
}
1195+
}

0 commit comments

Comments
 (0)