Kobi Ianko has uploaded a new change for review. Change subject: engine: Scheduling weight functins testing util ......................................................................
engine: Scheduling weight functins testing util A set of class to help structure and simplify the testing of scheduling weight functins. Change-Id: Ic4d359aa43ef11026224c85d20d983c0123e5078 Bug-Url: https://bugzilla.redhat.com/?????? Signed-off-by: Kobi Ianko <[email protected]> --- M backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/AbstractWeightPolicyUnitTest.java A backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/WeightFunctionTestChecker.java A backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/WeightFunctionTestInput.java 3 files changed, 209 insertions(+), 24 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/56/25256/1 diff --git a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/AbstractWeightPolicyUnitTest.java b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/AbstractWeightPolicyUnitTest.java index 8572701..372e1c4 100644 --- a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/AbstractWeightPolicyUnitTest.java +++ b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/AbstractWeightPolicyUnitTest.java @@ -1,9 +1,9 @@ package org.ovirt.engine.core.bll.scheduling.policyunits; import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; +import java.lang.reflect.Method; +import java.util.Arrays; import java.util.List; -import java.util.Map; import org.junit.Assert; import org.junit.Before; @@ -13,27 +13,52 @@ 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.VM; import org.ovirt.engine.core.common.utils.Pair; import org.ovirt.engine.core.compat.Guid; import org.ovirt.engine.core.dal.dbbroker.DbFacade; +/** + * <p> + * An abstract testing class for testing the scheduling weight function. To test a weight function, one should extend + * from this class, and implement its abstract methods. + * </p> + * <br> + * <p> + * The member <code>dbFacadeMock</code> is accessible from an extended object and can be used to mock DB operations. + * </p> + * <p> + * The method postSetup can be override to init setting before the test. + * </p> + * <p> + * The input parameter for the test are set in the <code>getInputs()</code> method. + * </p> + * <p> + * To test the result one should create a method and annotate it with <code>@WeightFunctionTestChecker</code> with the + * test name. + * </p> + */ @RunWith(MockitoJUnitRunner.class) public abstract class AbstractWeightPolicyUnitTest { + /** + * Mocked DB object, one can mock it's methods to simulate objects that are not in the DB. + */ @Mock protected DbFacade dbFacadeMock; + /** + * The actual policy to test + */ protected PolicyUnitImpl policyUnitImpl; - protected int numOfHosts = 10; - + /** + * A lifecycle method to be performed before the policy test. Users can add there code to be perform before the test + * by overriding the <code>postSetup()</code> method + */ @Before public void setup() { policyUnitImpl = Mockito.spy(setPolicyUnitImpl()); - // dbFacadeMock = Mockito.spy(setPathToDbFacade()); try { Mockito.doReturn(dbFacadeMock) @@ -47,34 +72,74 @@ e.printStackTrace(); } + postSetup(); + } + /** + * The actual test. + */ @Test public void testScoreMethod() { - List<Pair<Guid, Integer>> scores = policyUnitImpl.score(createVdsList(), createVm(), createParameters()); - boolean result = isScoreMethodTestSucceeded(scores); - Assert.assertTrue(result); + + List<WeightFunctionTestInput> inputs = getInputs(); + if (inputs == null) { + Assert.fail("Input for this test cannot be null"); + } + + for (WeightFunctionTestInput input : inputs) { + List<Pair<Guid, Integer>> scores = + policyUnitImpl.score(input.getVdsList(), input.getVm(), input.getParameters()); + + boolean result = checkScores(input.getTestName(), scores); + if (!result) { + Assert.fail("Test failed. input:\n" + input + " "); + } + } + + Assert.assertTrue(true); } - // protected abstract DbFacade setPathToDbFacade(); + /** + * Check the result of the test, is it the expected result. To do so this function invokes the method that is + * annotated with <code>@WeightFunctionTestChecker</code> and the value contains the test name. + */ + private boolean checkScores(String testName, List<Pair<Guid, Integer>> scores) { + Method[] methods = this.getClass().getMethods(); + for (Method method : methods) { + WeightFunctionTestChecker weightFunctionTestChecker = method.getAnnotation(WeightFunctionTestChecker.class); + if (weightFunctionTestChecker != null) { + String[] values = weightFunctionTestChecker.value(); + if (Arrays.asList(values).contains(testName)) { + try { + return (boolean) method.invoke(this, scores); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + } + } + } + // check method does not exist + return false; + } + + /** + * Set the policy to test + */ protected abstract PolicyUnitImpl setPolicyUnitImpl(); - protected abstract VM createVm(); + /** + * Set the inputs for the test, each WeightFunctionTestInput is a single test + */ + protected abstract List<WeightFunctionTestInput> getInputs(); - protected abstract Map<String, String> createParameters(); - - protected abstract VDS createVds(); - - protected List<VDS> createVdsList() { - List<VDS> listOfVds = new ArrayList<VDS>(numOfHosts); - for (int i = 0; i < numOfHosts; i++) { - listOfVds.add(createVds()); - } - return listOfVds; + /** + * Code to be performed before the test + */ + protected void postSetup() { + // Override this method if needed } - - protected abstract boolean isScoreMethodTestSucceeded(List<Pair<Guid, Integer>> scores); } 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 new file mode 100644 index 0000000..a1d0ea4 --- /dev/null +++ b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/WeightFunctionTestChecker.java @@ -0,0 +1,18 @@ +package org.ovirt.engine.core.bll.scheduling.policyunits; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +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. + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface WeightFunctionTestChecker { + + String[] value() default ""; + +} diff --git a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/WeightFunctionTestInput.java b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/WeightFunctionTestInput.java new file mode 100644 index 0000000..9ee7833 --- /dev/null +++ b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/scheduling/policyunits/WeightFunctionTestInput.java @@ -0,0 +1,102 @@ +package org.ovirt.engine.core.bll.scheduling.policyunits; + +import java.util.List; +import java.util.Map; + +import org.ovirt.engine.core.common.businessentities.VDS; +import org.ovirt.engine.core.common.businessentities.VM; + +/** + * The input class for the weight module tester + */ +public class WeightFunctionTestInput { + + private String testName; + private List<VDS> vdsList; + private VM vm; + private Map<String, String> parameters; + + public WeightFunctionTestInput(String testName, List<VDS> vdsList, VM vm, Map<String, String> parameters) { + super(); + this.testName = testName; + this.vdsList = vdsList; + this.vm = vm; + this.parameters = parameters; + } + + public List<VDS> getVdsList() { + return vdsList; + } + + public VM getVm() { + return vm; + } + + public Map<String, String> getParameters() { + return parameters; + } + + public String getTestName() { + return testName; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((parameters == null) ? 0 : parameters.hashCode()); + result = prime * result + ((testName == null) ? 0 : testName.hashCode()); + result = prime * result + ((vdsList == null) ? 0 : vdsList.hashCode()); + result = prime * result + ((vm == null) ? 0 : vm.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + WeightFunctionTestInput other = (WeightFunctionTestInput) obj; + if (parameters == null) { + if (other.parameters != null) + return false; + } else if (!parameters.equals(other.parameters)) + return false; + if (testName == null) { + if (other.testName != null) + return false; + } else if (!testName.equals(other.testName)) + return false; + if (vdsList == null) { + if (other.vdsList != null) + return false; + } else if (!vdsList.equals(other.vdsList)) + return false; + if (vm == null) { + if (other.vm != null) + return false; + } else if (!vm.equals(other.vm)) + return false; + return true; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("WeightFunctionTestInput [testName="); + builder.append(testName); + builder.append(", vdsList="); + builder.append(vdsList); + builder.append(", vm="); + builder.append(vm); + builder.append(", parameters="); + builder.append(parameters); + builder.append("]"); + return builder.toString(); + } + + +} -- To view, visit http://gerrit.ovirt.org/25256 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ic4d359aa43ef11026224c85d20d983c0123e5078 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Kobi Ianko <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
