AutoAssignChecker implements AutoAssignExecutor { @Override @Transactional(propagation = Propagation.REQUIRES_NEW) public void check() { LOGGER.debug("Auto assigned check call"); final PageRequest pageRequest = PageRequest.of(0, PAGE_SIZE); final Page<TargetFilterQuery> filterQueries = targetFilterQueryManagement.findWithAutoAssignDS(pageRequest); for (final TargetFilterQuery filterQuery : filterQueries) { checkByTargetFilterQueryAndAssignDS(filterQuery); } } AutoAssignChecker(final TargetFilterQueryManagement targetFilterQueryManagement,
            final TargetManagement targetManagement, final DeploymentManagement deploymentManagement,
            final PlatformTransactionManager transactionManager); @Override @Transactional(propagation = Propagation.REQUIRES_NEW) void check();  }
@Test @Description("Verifies that a running action is auto canceled by a AutoAssignment which assigns another distribution-set.") public void autoAssignDistributionSetAndAutoCloseOldActions() { tenantConfigurationManagement .addOrUpdateConfiguration(TenantConfigurationKey.REPOSITORY_ACTIONS_AUTOCLOSE_ENABLED, true); try { final String knownControllerId = "controller12345"; final DistributionSet firstDistributionSet = testdataFactory.createDistributionSet(); final DistributionSet secondDistributionSet = testdataFactory.createDistributionSet("second"); testdataFactory.createTarget(knownControllerId); final DistributionSetAssignmentResult assignmentResult = assignDistributionSet(firstDistributionSet.getId(), knownControllerId); final Long manuallyAssignedActionId = getFirstAssignedActionId(assignmentResult); final TargetFilterQuery targetFilterQuery = targetFilterQueryManagement .create(entityFactory.targetFilterQuery().create().name("filterA").query("name==*")); targetFilterQueryManagement.updateAutoAssignDS(entityFactory.targetFilterQuery() .updateAutoAssign(targetFilterQuery.getId()).ds(secondDistributionSet.getId())); autoAssignChecker.check(); final List<Action> actionsByKnownTarget = deploymentManagement.findActionsByTarget(knownControllerId, PAGE) .getContent(); assertThat(actionsByKnownTarget).hasSize(2); assertThat(deploymentManagement.findAction(manuallyAssignedActionId).get().getStatus()) .isEqualTo(Status.CANCELED); final Action rolloutCreatedAction = actionsByKnownTarget.stream() .filter(action -> !action.getId().equals(manuallyAssignedActionId)).findAny().get(); assertThat(rolloutCreatedAction.getStatus()).isEqualTo(Status.RUNNING); assertThat(rolloutCreatedAction.getActionType()).isEqualTo(ActionType.FORCED); } finally { tenantConfigurationManagement .addOrUpdateConfiguration(TenantConfigurationKey.REPOSITORY_ACTIONS_AUTOCLOSE_ENABLED, false); } }
@Test @Description("Test auto assignment of a DS to filtered targets") public void checkAutoAssign() { final DistributionSet setA = testdataFactory.createDistributionSet("dsA"); final DistributionSet setB = testdataFactory.createDistributionSet("dsB"); final TargetFilterQuery targetFilterQuery = targetFilterQueryManagement.updateAutoAssignDS( entityFactory.targetFilterQuery() .updateAutoAssign(targetFilterQueryManagement.create( entityFactory.targetFilterQuery().create().name("filterA").query("name==*")).getId()) .ds(setA.getId())); final String targetDsAIdPref = "targ"; final List<Target> targets = testdataFactory.createTargets(100, targetDsAIdPref, targetDsAIdPref.concat(" description")); final int targetsCount = targets.size(); assignDistributionSet(setA, targets.subList(0, 10)); verifyThatTargetsHaveDistributionSetAssignment(setA, targets.subList(0, 10), targetsCount); assignDistributionSet(setB, targets.subList(0, 5)); verifyThatTargetsHaveDistributionSetAssignment(setB, targets.subList(0, 5), targetsCount); assignDistributionSet(setB, targets.subList(10, 20)); verifyThatTargetsHaveDistributionSetAssignment(setB, targets.subList(10, 20), targetsCount); assertThat(targetManagement.countByRsqlAndNonDS(setA.getId(), targetFilterQuery.getQuery())).isEqualTo(90); autoAssignChecker.check(); verifyThatTargetsHaveDistributionSetAssignment(setA, targets.subList(5, 100), targetsCount); verifyThatTargetsHaveDistributionSetAssignment(setB, targets.subList(0, 5), targetsCount); verifyThatCreatedActionsAreInitiatedByCurrentUser(targetFilterQuery, setA, targets); }
@Test @Description("Test auto assignment of an incomplete DS to filtered targets, that causes failures") public void checkAutoAssignWithFailures() { final DistributionSet setF = distributionSetManagement.create(entityFactory.distributionSet().create() .name("dsA").version("1").type(testdataFactory.findOrCreateDefaultTestDsType())); final DistributionSet setA = testdataFactory.createDistributionSet("dsA"); final DistributionSet setB = testdataFactory.createDistributionSet("dsB"); final String targetDsAIdPref = "targA"; final String targetDsFIdPref = "targB"; assertThatExceptionOfType(InvalidAutoAssignDistributionSetException.class).isThrownBy(() -> { final Long filterId = targetFilterQueryManagement.create( entityFactory.targetFilterQuery().create().name("filterA").query("id==" + targetDsFIdPref + "*")) .getId(); targetFilterQueryManagement .updateAutoAssignDS(entityFactory.targetFilterQuery().updateAutoAssign(filterId).ds(setF.getId())); }); targetFilterQueryManagement.create(entityFactory.targetFilterQuery().create().name("filterB") .query("id==" + targetDsAIdPref + "*").autoAssignDistributionSet(setA.getId())); final List<Target> targetsF = testdataFactory.createTargets(10, targetDsFIdPref, targetDsFIdPref.concat(" description")); final List<Target> targetsA = testdataFactory.createTargets(10, targetDsAIdPref, targetDsAIdPref.concat(" description")); final int targetsCount = targetsA.size() + targetsF.size(); assignDistributionSet(setB, targetsF.subList(0, 5)); verifyThatTargetsHaveDistributionSetAssignment(setB, targetsF.subList(0, 5), targetsCount); autoAssignChecker.check(); verifyThatTargetsHaveDistributionSetAssignment(setB, targetsF.subList(0, 5), targetsCount); verifyThatTargetsHaveDistributionSetAssignment(setA, targetsA, targetsCount); }
@Test @Description("Test auto assignment of a distribution set with FORCED, SOFT and DOWNLOAD_ONLY action types") public void checkAutoAssignWithDifferentActionTypes() { final DistributionSet distributionSet = testdataFactory.createDistributionSet(); final String targetDsAIdPref = "A"; final String targetDsBIdPref = "B"; final String targetDsCIdPref = "C"; final List<Target> targetsA = createTargetsAndAutoAssignDistSet(targetDsAIdPref, 5, distributionSet, ActionType.FORCED); final List<Target> targetsB = createTargetsAndAutoAssignDistSet(targetDsBIdPref, 10, distributionSet, ActionType.SOFT); final List<Target> targetsC = createTargetsAndAutoAssignDistSet(targetDsCIdPref, 10, distributionSet, ActionType.DOWNLOAD_ONLY); final int targetsCount = targetsA.size() + targetsB.size() + targetsC.size(); autoAssignChecker.check(); verifyThatTargetsHaveDistributionSetAssignment(distributionSet, targetsA, targetsCount); verifyThatTargetsHaveDistributionSetAssignment(distributionSet, targetsB, targetsCount); verifyThatTargetsHaveDistributionSetAssignment(distributionSet, targetsC, targetsCount); verifyThatTargetsHaveAssignmentActionType(ActionType.FORCED, targetsA); verifyThatTargetsHaveAssignmentActionType(ActionType.SOFT, targetsB); verifyThatTargetsHaveAssignmentActionType(ActionType.DOWNLOAD_ONLY, targetsC); }
@Test @Description("An auto assignment target filter with weight creats actions with weights") public void actionsWithWeightAreCreated() throws Exception { final int amountOfTargets = 5; final DistributionSet ds = testdataFactory.createDistributionSet(); final int weight = 32; enableMultiAssignments(); targetFilterQueryManagement.create(entityFactory.targetFilterQuery().create().name("a").query("name==*") .autoAssignDistributionSet(ds).autoAssignWeight(weight)); testdataFactory.createTargets(amountOfTargets); autoAssignChecker.check(); final List<Action> actions = deploymentManagement.findActionsAll(PAGE).getContent(); assertThat(actions).hasSize(amountOfTargets); assertThat(actions).allMatch(action -> action.getWeight().get() == weight); }
@Test @Description("An auto assignment target filter without weight still works after multi assignment is enabled") public void filterWithoutWeightWorksInMultiAssignmentMode() throws Exception { final int amountOfTargets = 5; final DistributionSet ds = testdataFactory.createDistributionSet(); targetFilterQueryManagement.create( entityFactory.targetFilterQuery().create().name("a").query("name==*").autoAssignDistributionSet(ds)); enableMultiAssignments(); testdataFactory.createTargets(amountOfTargets); autoAssignChecker.check(); final List<Action> actions = deploymentManagement.findActionsAll(PAGE).getContent(); assertThat(actions).hasSize(amountOfTargets); assertThat(actions).allMatch(action -> !action.getWeight().isPresent()); }