Kobi Ianko has uploaded a new change for review. Change subject: engine: Unit testing for HA Reservation scheduling weight function. ......................................................................
engine: Unit testing for HA Reservation scheduling weight function. Adding some unit testing for the HA Reservation weight finction, the test uses the AbstractWeightPolicyUnitTest and can serve as an example for how to use it. Change-Id: Ibcb5df48e126cc454a080c9c4d8af61db8b4b61c Bug-Url: https://bugzilla.redhat.com/?????? Signed-off-by: Kobi Ianko <k...@redhat.com> --- M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/HaReservationHandling.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationBalancePolicyUnit.java M backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationWeightPolicyUnit.java M backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationWeightPolicyUnitTest.java M backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/WeightFunctionTestChecker.java 5 files changed, 70 insertions(+), 80 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/57/25257/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/HaReservationHandling.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/HaReservationHandling.java index 630e42c..c3e0fb3 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/HaReservationHandling.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/HaReservationHandling.java @@ -52,7 +52,8 @@ // for the inner Pair, first is cpu second is ram List<Pair<Guid, Pair<Integer, Integer>>> hostsUnutilizedResources = getUnutilizedResources(hosts); - Map<Guid, List<VM>> hostToHaVmsMapping = mapHaVmToHostByCluster(cluster.getId()); + List<VM> vms = DbFacade.getInstance().getVmDao().getAllForVdsGroup(cluster.getId()); + Map<Guid, List<VM>> hostToHaVmsMapping = mapHaVmToHostByCluster(cluster.getId(), vms); for (VDS host : hosts) { if (hostToHaVmsMapping.get(host.getId()) != null) { @@ -184,9 +185,9 @@ } @SuppressWarnings("unchecked") - public static Map<Guid, List<VM>> mapHaVmToHostByCluster(Guid clusterId) { + public static Map<Guid, List<VM>> mapHaVmToHostByCluster(Guid clusterId, List<VM> vms) { - List<VM> vms = DbFacade.getInstance().getVmDao().getAllForVdsGroup(clusterId); + // List<VM> vms = DbFacade.getInstance().getVmDao().getAllForVdsGroup(clusterId); if (vms == null || vms.isEmpty()) { log.debugFormat("No VMs available for this cluster with id {0}", clusterId); // return empty map diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationBalancePolicyUnit.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationBalancePolicyUnit.java index e31d3dc..e953760 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationBalancePolicyUnit.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationBalancePolicyUnit.java @@ -18,6 +18,7 @@ import org.ovirt.engine.core.common.scheduling.PolicyUnit; import org.ovirt.engine.core.common.utils.Pair; import org.ovirt.engine.core.compat.Guid; +import org.ovirt.engine.core.dal.dbbroker.DbFacade; import org.ovirt.engine.core.utils.linq.LinqUtils; import org.ovirt.engine.core.utils.linq.Predicate; import org.ovirt.engine.core.utils.log.Log; @@ -58,7 +59,8 @@ int haVmsInCluster = 0; - Map<Guid, List<VM>> hostId2HaVmMapping = HaReservationHandling.mapHaVmToHostByCluster(cluster.getId()); + List<VM> vms = DbFacade.getInstance().getVmDao().getAllForVdsGroup(cluster.getId()); + Map<Guid, List<VM>> hostId2HaVmMapping = HaReservationHandling.mapHaVmToHostByCluster(cluster.getId(), vms); haVmsInCluster = countHaVmsInCluster(hostId2HaVmMapping); diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationWeightPolicyUnit.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationWeightPolicyUnit.java index f514642..54f6f54 100644 --- a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationWeightPolicyUnit.java +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationWeightPolicyUnit.java @@ -39,13 +39,15 @@ Map<Guid, Integer> hostsHaVmCount = new HashMap<Guid, Integer>(); // If the vm is not HA or the cluster is not marked as HA Reservation set default score. - VDSGroup vdsGroup = DbFacade.getInstance().getVdsGroupDao().get(hosts.get(0).getVdsGroupId()); + VDSGroup vdsGroup = getDbFacade().getVdsGroupDao().get(hosts.get(0).getVdsGroupId()); if (!vm.isAutoStartup() || !vdsGroup.supportsHaReservation()) { fillDefaultScores(hosts, scores); } else { // Use a single call to the DB to retrieve all VM in the Cluster and map them by Host id - Map<Guid, List<VM>> hostId2HaVmMapping = HaReservationHandling.mapHaVmToHostByCluster(vdsGroup.getId()); + List<VM> vms = getDbFacade().getVmDao().getAllForVdsGroup(vdsGroup.getId()); + Map<Guid, List<VM>> hostId2HaVmMapping = + HaReservationHandling.mapHaVmToHostByCluster(vdsGroup.getId(), vms); int maxCount = 0; for (VDS host : hosts) { @@ -91,6 +93,10 @@ return scores; } + public DbFacade getDbFacade() { + return DbFacade.getInstance(); + } + // Fill all host with a neutral score private void fillDefaultScores(List<VDS> hosts, List<Pair<Guid, Integer>> scores) { for (VDS host : hosts) { diff --git a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationWeightPolicyUnitTest.java b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationWeightPolicyUnitTest.java index f53a0a9..07381eb 100644 --- a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationWeightPolicyUnitTest.java +++ b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/HaReservationWeightPolicyUnitTest.java @@ -1,34 +1,26 @@ package org.ovirt.engine.core.bll.scheduling.policyunits; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.Map; +import java.util.UUID; -import org.junit.Before; import org.junit.Rule; -import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.runners.MockitoJUnitRunner; import org.ovirt.engine.core.bll.scheduling.PolicyUnitImpl; import org.ovirt.engine.core.common.businessentities.VDS; import org.ovirt.engine.core.common.businessentities.VDSGroup; import org.ovirt.engine.core.common.businessentities.VM; import org.ovirt.engine.core.common.businessentities.VMStatus; -import org.ovirt.engine.core.common.businessentities.VmBase; -import org.ovirt.engine.core.common.businessentities.VmDynamic; import org.ovirt.engine.core.common.config.ConfigValues; import org.ovirt.engine.core.common.scheduling.PolicyUnit; import org.ovirt.engine.core.common.utils.Pair; import org.ovirt.engine.core.compat.Guid; -import org.ovirt.engine.core.compat.Version; import org.ovirt.engine.core.dao.VdsGroupDAO; import org.ovirt.engine.core.dao.VmDAO; import org.ovirt.engine.core.utils.MockConfigRule; -@RunWith(MockitoJUnitRunner.class) public class HaReservationWeightPolicyUnitTest extends AbstractWeightPolicyUnitTest { @Rule @@ -46,18 +38,15 @@ @Mock VmDAO vmDaoMock; - @Before - public void setUp() throws Exception { - super.setup(); + VDSGroup vdsGroup = createVdsGroupMock(); + @Override + protected void postSetup() { Mockito.doReturn(vdsGroupDaoMock).when(dbFacadeMock).getVdsGroupDao(); - Mockito.doReturn(createVdsGroupMock()).when(vdsGroupDaoMock).get(Mockito.any(Guid.class)); - + Mockito.doReturn(vdsGroup).when(vdsGroupDaoMock).get(Mockito.any(Guid.class)); Mockito.doReturn(vmDaoMock).when(dbFacadeMock).getVmDao(); Mockito.doReturn(createVmsMock()).when(vmDaoMock).getAllForVdsGroup(Mockito.any(Guid.class)); - Mockito.doReturn(true).when(vmMock).isAutoStartup(); - } private VDSGroup createVdsGroupMock() { @@ -72,28 +61,22 @@ private List<VM> createVmsMock() { List<VM> l = new ArrayList<>(); - VM mockVM = null; + float ratio = 2f; + int max = 15; + for (int i = 0; i < max; i++) { + VM mockVM = new VM(); + mockVM.setId(new Guid(UUID.randomUUID())); + mockVM.setStatus(VMStatus.Up); + mockVM.setAutoStartup(true); - try { - Map<Method, Object> m2v = new HashMap<>(); - m2v.put(Version.class.getMethod("setValue", String.class), "*"); - m2v.put(VmDynamic.class.getMethod("setStatus", VMStatus.class), VMStatus.Up); - m2v.put(VmBase.class.getMethod("setAutoStartup", boolean.class), true); - m2v.put(VmDynamic.class.getMethod("setRunOnVds", Guid.class), new Guid(FIRST_GUID)); - - for (int i = 0; i < 10; i++) { - mockVM = new PopulateFactory().populate(VM.class, m2v); - l.add(mockVM); + if (((float) (max - i) / (i + 1)) <= ratio) { + mockVM.setRunOnVds(new Guid(FIRST_GUID)); + } else { + mockVM.setRunOnVds(new Guid(SECOND_GUID)); } - - m2v.put(VmDynamic.class.getMethod("setRunOnVds", Guid.class), new Guid(SECOND_GUID)); - for (int i = 0; i < 5; i++) { - mockVM = new PopulateFactory().populate(VM.class, m2v); - l.add(mockVM); - } - } catch (Throwable t) { - t.printStackTrace(); + l.add(mockVM); } + return l; } @@ -102,56 +85,54 @@ return new HaReservationWeightPolicyUnit(new PolicyUnit()); } - @Override - protected VM createVm() { - return vmMock; - } - - @Override - protected Map<String, String> createParameters() { - return new HashMap<String, String>(); - } - - @Override - protected VDS createVds() { - - VDS mockVds = null; - try { - Map<Method, Object> m2v = new HashMap<>(); - m2v.put(Version.class.getMethod("setValue", String.class), "*"); - mockVds = new PopulateFactory().populate(VDS.class, m2v); - } catch (Exception e) { - e.printStackTrace(); - } - return mockVds; - } - - @Override - protected boolean isScoreMethodTestSucceeded(List<Pair<Guid, Integer>> scores) { - - Integer firstScore = scores.get(0).getSecond(); - Integer secondScore = scores.get(1).getSecond(); - - return firstScore == secondScore * 2; - } - private static final String FIRST_GUID = "ABC00000-0000-0000-0000-123456789ABC"; private static final String SECOND_GUID = "CBA00000-0000-0000-0000-123456789CBA"; - @Override protected List<VDS> createVdsList() { - numOfHosts = 2; + int numOfHosts = 2; List<VDS> l = new ArrayList<>(numOfHosts); - VDS vds = createVds(); + VDS vds = new VDS(); vds.setId(new Guid(FIRST_GUID)); l.add(vds); - vds = createVds(); + vds = new VDS(); vds.setId(new Guid(SECOND_GUID)); l.add(vds); return l; } + @Override + protected List<WeightFunctionTestInput> getInputs() { + List<WeightFunctionTestInput> inputs = new ArrayList<>(); + + inputs.add(new WeightFunctionTestInput("test1", createVdsList(), vmMock, new HashMap<String, String>())); + + Mockito.doReturn(false).when(vmMock).isAutoStartup(); + inputs.add(new WeightFunctionTestInput("test2", createVdsList(), vmMock, new HashMap<String, String>())); + + Mockito.doReturn(true).when(vmMock).isAutoStartup(); + vdsGroup.setHaReservation(false); + inputs.add(new WeightFunctionTestInput("test3", createVdsList(), vmMock, new HashMap<String, String>())); + vdsGroup.setHaReservation(true); + return inputs; + + } + + @WeightFunctionTestChecker({ "test1" }) + public boolean checkResult(List<Pair<Guid, Integer>> scores) { + return scores.get(0).getSecond() / 2 == scores.get(1).getSecond(); + } + + @WeightFunctionTestChecker({ "test2" }) + public boolean checkResult2(List<Pair<Guid, Integer>> scores) { + return (1 == scores.get(0).getSecond()) == (1 == scores.get(1).getSecond()); + } + + @WeightFunctionTestChecker({ "test3" }) + public boolean checkResult3(List<Pair<Guid, Integer>> scores) { + return (0 == scores.get(0).getSecond()) == (scores.get(1).getSecond() == 0); + } + } diff --git a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/WeightFunctionTestChecker.java b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/WeightFunctionTestChecker.java index a1d0ea4..9b9a8af 100644 --- a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/WeightFunctionTestChecker.java +++ b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/WeightFunctionTestChecker.java @@ -6,8 +6,8 @@ import java.lang.annotation.Target; /** - * Annotation to indicate the method that checks the result of the test run. The value is the testName that was set when - * the test input was created. + * Annotation to indicate the method that checks the result of the test run. + * The value is the testName that was set when the test input was created. */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) -- To view, visit http://gerrit.ovirt.org/25257 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ibcb5df48e126cc454a080c9c4d8af61db8b4b61c Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Kobi Ianko <k...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches