diff --git a/server/src/test/java/org/apache/cloudstack/cluster/ClusterDrsServiceImplTest.java b/server/src/test/java/org/apache/cloudstack/cluster/ClusterDrsServiceImplTest.java index 98b18c663051..02bf3bd6f3ef 100644 --- a/server/src/test/java/org/apache/cloudstack/cluster/ClusterDrsServiceImplTest.java +++ b/server/src/test/java/org/apache/cloudstack/cluster/ClusterDrsServiceImplTest.java @@ -852,6 +852,49 @@ public void testGetBestMigration() throws ConfigurationException { assertEquals(vm1, bestMigration.first()); } + @Test + public void testGetBestMigrationSkipsPassthroughVm() 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(1L); + + VMInstanceVO vmPassthrough = Mockito.mock(VMInstanceVO.class); + Mockito.when(vmPassthrough.getId()).thenReturn(1L); + Mockito.when(vmPassthrough.getType()).thenReturn(VirtualMachine.Type.User); + Mockito.when(vmPassthrough.getState()).thenReturn(VirtualMachine.State.Running); + Mockito.when(vmPassthrough.getDetails()).thenReturn(Collections.emptyMap()); + + VMInstanceVO vmNormal = Mockito.mock(VMInstanceVO.class); + Mockito.when(vmNormal.getId()).thenReturn(2L); + Mockito.when(vmNormal.getType()).thenReturn(VirtualMachine.Type.User); + Mockito.when(vmNormal.getState()).thenReturn(VirtualMachine.State.Running); + Mockito.when(vmNormal.getDetails()).thenReturn(Collections.emptyMap()); + + List vmList = new ArrayList<>(); + vmList.add(vmPassthrough); + vmList.add(vmNormal); + + ServiceOffering serviceOffering = Mockito.mock(ServiceOffering.class); + Map vmIdServiceOfferingMap = new HashMap<>(); + vmIdServiceOfferingMap.put(vmPassthrough.getId(), serviceOffering); + vmIdServiceOfferingMap.put(vmNormal.getId(), serviceOffering); + + Mockito.when(managementServer.listHostsForMigrationOfVM(vmPassthrough, 0L, 500L, null, vmList)) + .thenThrow(new InvalidParameterValueException("Unsupported operation, VM uses host passthrough, cannot migrate")); + Mockito.when(managementServer.listHostsForMigrationOfVM(vmNormal, 0L, 500L, null, vmList)).thenReturn( + new Ternary<>(new Pair<>(List.of(destHost), 1), List.of(destHost), Map.of(destHost, false))); + Mockito.when(balancedAlgorithm.getMetrics(cluster, vmNormal, serviceOffering, destHost, new HashMap<>(), + new HashMap<>(), false)).thenReturn(new Ternary<>(1.0, 0.5, 1.5)); + + Pair bestMigration = clusterDrsService.getBestMigration(cluster, balancedAlgorithm, + vmList, vmIdServiceOfferingMap, new HashMap<>(), new HashMap<>()); + + assertEquals(vmNormal, bestMigration.first()); + assertEquals(destHost, bestMigration.second()); + } + @Test public void testGetBestMigrationDifferentCluster() throws ConfigurationException { ClusterVO cluster = Mockito.mock(ClusterVO.class);