Align rollouts and autoassign metrics (#2844)

* Refactor auto-assign locking and metrics
* Align rollouts and autoassign metrics

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-12-03 12:34:40 +02:00
committed by GitHub
parent 977b3fe40c
commit 904c8b180d
12 changed files with 211 additions and 136 deletions

View File

@@ -19,7 +19,7 @@ import static org.eclipse.hawkbit.repository.test.util.SecurityContextSwitch.cal
import java.util.Optional;
import org.eclipse.hawkbit.repository.AutoAssignExecutor;
import org.eclipse.hawkbit.repository.AutoAssignHandler;
import org.eclipse.hawkbit.repository.Identifiable;
import org.eclipse.hawkbit.repository.TargetFilterQueryManagement;
import org.eclipse.hawkbit.repository.TargetFilterQueryManagement.AutoAssignDistributionSetUpdate;
@@ -27,34 +27,30 @@ import org.eclipse.hawkbit.repository.jpa.scheduler.AutoAssignScheduler;
import org.eclipse.hawkbit.repository.model.TargetFilterQuery;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.locks.LockRegistry;
class AutoAssignTest extends AbstractAccessControllerManagementTest {
@Autowired
AutoAssignExecutor autoAssignExecutor;
@Autowired
LockRegistry lockRegistry;
AutoAssignHandler autoAssignHandler;
@Test
void verifyOnlyUpdatableTargetsArePartOfAutoAssignmentByScheduler() throws Exception {
// auto assign scheduler apply stored access control context and the context is correctly applied
verifyOnlyUpdatableTargetsArePartOfAutoAssignment(
() -> new AutoAssignScheduler(systemManagement, autoAssignExecutor, 1, lockRegistry, Optional.empty()).autoAssignScheduler());
() -> new AutoAssignScheduler(systemManagement, autoAssignHandler, 1, Optional.empty()).autoAssignScheduler());
}
@Test
void verifyOnlyUpdatableTargetsArePartOfAutoAssignment() throws Exception {
verifyOnlyUpdatableTargetsArePartOfAutoAssignment(autoAssignExecutor::checkAllTargets);
verifyOnlyUpdatableTargetsArePartOfAutoAssignment(autoAssignHandler::handleAll);
}
@Test
void verifyOnlyUpdatableTargetsWillGetAssignmentOnSingleCheck() throws Exception {
verifyOnlyUpdatableTargetsArePartOfAutoAssignment(() -> {
autoAssignExecutor.checkSingleTarget(target1Type1.getControllerId());
autoAssignExecutor.checkSingleTarget(target2Type2.getControllerId());
autoAssignExecutor.checkSingleTarget(target3Type2.getControllerId());
autoAssignHandler.handleSingleTarget(target1Type1.getControllerId());
autoAssignHandler.handleSingleTarget(target2Type2.getControllerId());
autoAssignHandler.handleSingleTarget(target3Type2.getControllerId());
});
}

View File

@@ -21,7 +21,7 @@ import java.util.function.Supplier;
import lombok.SneakyThrows;
import org.eclipse.hawkbit.auth.SpPermission;
import org.eclipse.hawkbit.context.AccessContext;
import org.eclipse.hawkbit.repository.AutoAssignExecutor;
import org.eclipse.hawkbit.repository.AutoAssignHandler;
import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest;
import org.eclipse.hawkbit.repository.model.Rollout;
import org.eclipse.hawkbit.repository.model.TargetFilterQuery;
@@ -45,7 +45,7 @@ class SecurityContextCtxTest extends AbstractJpaIntegrationTest {
private static final Set<String> AUTHORITIES = SpPermission.getAllAuthorities();
@Autowired
AutoAssignExecutor autoAssignExecutor;
AutoAssignHandler autoAssignHandler;
/**
* Verifies acm context is persisted when creating Rollout
@@ -97,7 +97,7 @@ class SecurityContextCtxTest extends AbstractJpaIntegrationTest {
try (final MockedStatic<AccessContext> mocked = mockStatic(AccessContext.class, Mockito.CALLS_REAL_METHODS)) {
withSecurityContext(userContext, testdataFactory::createTargetFilterWithTargetsAndActiveAutoAssignment);
withSecurityContext(userContext, () -> {
autoAssignExecutor.checkAllTargets();
autoAssignHandler.handleAll();
return null;
});
@@ -118,7 +118,7 @@ class SecurityContextCtxTest extends AbstractJpaIntegrationTest {
withSecurityContext(userContext, testdataFactory::createTargetFilterWithTargetsAndActiveAutoAssignment);
withSecurityContext(userContext, () -> {
autoAssignExecutor.checkSingleTarget(targetManagement.findAll(Pageable.ofSize(1)).getContent().get(0).getControllerId());
autoAssignHandler.handleSingleTarget(targetManagement.findAll(Pageable.ofSize(1)).getContent().get(0).getControllerId());
return null;
});

View File

@@ -44,18 +44,18 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
/**
* Test class for {@link JpaAutoAssignExecutor}.
* Test class for {@link JpaAutoAssignHandler}.
* <p/>
* Feature: Component Tests - Repository<br/>
* Story: Auto assign checker
*/
@SuppressWarnings("java:S6813") // constructor injects are not possible for test classes
class AutoAssignExecutorIntTest extends AbstractJpaIntegrationTest {
class AutoAssignHandlerIntTest extends AbstractJpaIntegrationTest {
private static final String SPACE_AND_DESCRIPTION = " description";
@Autowired
private JpaAutoAssignExecutor autoAssignChecker;
private JpaAutoAssignHandler autoAssignChecker;
@Autowired
private DeploymentManagement deploymentManagement;
@@ -81,7 +81,7 @@ class AutoAssignExecutorIntTest extends AbstractJpaIntegrationTest {
targetFilterQueryManagement.updateAutoAssignDS(
new AutoAssignDistributionSetUpdate(targetFilterQuery.getId()).ds(secondDistributionSet.getId()));
// Run the check
autoAssignChecker.checkAllTargets();
autoAssignChecker.handleAll();
// verify that manually created action is canceled and action
// created from AutoAssign is running
@@ -144,7 +144,7 @@ class AutoAssignExecutorIntTest extends AbstractJpaIntegrationTest {
assertThat(targetManagement.countByRsqlAndNonDsAndCompatibleAndUpdatable(setA.getId(), targetFilterQuery.getQuery())).isEqualTo(15);
// Run the check
autoAssignChecker.checkAllTargets();
autoAssignChecker.handleAll();
verifyThatTargetsHaveDistributionSetAssignment(setA, targets.subList(5, 25), targetsCount);
@@ -173,7 +173,7 @@ class AutoAssignExecutorIntTest extends AbstractJpaIntegrationTest {
final int targetsCount = targets.size();
// Run the check
autoAssignChecker.checkSingleTarget(targets.get(0).getControllerId());
autoAssignChecker.handleSingleTarget(targets.get(0).getControllerId());
verifyThatTargetsHaveDistributionSetAssignment(toAssignDs, targets.subList(0, 1), targetsCount);
@@ -204,7 +204,7 @@ class AutoAssignExecutorIntTest extends AbstractJpaIntegrationTest {
targetDsAIdPref.concat(SPACE_AND_DESCRIPTION));
// Run the check
autoAssignChecker.checkAllTargets();
autoAssignChecker.handleAll();
verifyThatTargetsHaveDistributionSetAssignedAndActionStatus(distributionSet, targets, expectedStatus);
}
@@ -232,7 +232,7 @@ class AutoAssignExecutorIntTest extends AbstractJpaIntegrationTest {
final List<Target> targets = testdataFactory.createTargets(25);
// Run the check
autoAssignChecker.checkSingleTarget(targets.get(0).getControllerId());
autoAssignChecker.handleSingleTarget(targets.get(0).getControllerId());
verifyThatTargetsHaveDistributionSetAssignedAndActionStatus(toAssignDs, targets.subList(0, 1), expectedStatus);
verifyThatTargetsNotHaveDistributionSetAssignment(targets.subList(1, 25));
@@ -281,7 +281,7 @@ class AutoAssignExecutorIntTest extends AbstractJpaIntegrationTest {
verifyThatTargetsHaveDistributionSetAssignment(setB, targetsF.subList(0, 5), targetsCount);
// Run the check
autoAssignChecker.checkAllTargets();
autoAssignChecker.handleAll();
// first 5 targets of the fail group should still have setB
verifyThatTargetsHaveDistributionSetAssignment(setB, targetsF.subList(0, 5), targetsCount);
@@ -309,7 +309,7 @@ class AutoAssignExecutorIntTest extends AbstractJpaIntegrationTest {
final int targetsCount = targetsA.size() + targetsB.size() + targetsC.size();
autoAssignChecker.checkAllTargets();
autoAssignChecker.handleAll();
verifyThatTargetsHaveDistributionSetAssignment(distributionSet, targetsA, targetsCount);
verifyThatTargetsHaveDistributionSetAssignment(distributionSet, targetsB, targetsCount);
@@ -334,7 +334,7 @@ class AutoAssignExecutorIntTest extends AbstractJpaIntegrationTest {
.name("a").query("name==*").autoAssignDistributionSet(ds).autoAssignWeight(weight)
.build());
testdataFactory.createTargets(amountOfTargets);
autoAssignChecker.checkAllTargets();
autoAssignChecker.handleAll();
final List<Action> actions = deploymentManagement.findActionsAll(PAGE).getContent();
assertThat(actions)
@@ -353,7 +353,7 @@ class AutoAssignExecutorIntTest extends AbstractJpaIntegrationTest {
enableMultiAssignments();
testdataFactory.createTargets(amountOfTargets);
autoAssignChecker.checkAllTargets();
autoAssignChecker.handleAll();
final List<Action> actions = deploymentManagement.findActionsAll(PAGE).getContent();
assertThat(actions)
@@ -404,7 +404,7 @@ class AutoAssignExecutorIntTest extends AbstractJpaIntegrationTest {
testFilter.getQuery());
assertThat(compatibleCount).isEqualTo(compatibleTargets.size());
autoAssignChecker.checkAllTargets();
autoAssignChecker.handleAll();
final List<Action> actions = deploymentManagement.findActionsAll(Pageable.unpaged()).getContent();
assertThat(actions).hasSize(compatibleTargets.size());

View File

@@ -9,6 +9,7 @@
*/
package org.eclipse.hawkbit.repository.jpa.scheduler;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
@@ -17,9 +18,12 @@ import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.locks.Lock;
import org.assertj.core.api.Assertions;
import org.eclipse.hawkbit.repository.DeploymentManagement;
import org.eclipse.hawkbit.repository.TargetFilterQueryManagement;
import org.eclipse.hawkbit.repository.TargetManagement;
@@ -35,6 +39,7 @@ import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.SliceImpl;
import org.springframework.integration.support.locks.LockRegistry;
import org.springframework.transaction.PlatformTransactionManager;
/**
@@ -42,7 +47,7 @@ import org.springframework.transaction.PlatformTransactionManager;
* Story: Auto assign checker
*/
@ExtendWith(MockitoExtension.class)
class AutoAssignExecutorTest {
class AutoAssignHandlerTest {
@Mock
private TargetFilterQueryManagement<? extends TargetFilterQuery> targetFilterQueryManagement;
@@ -53,19 +58,36 @@ class AutoAssignExecutorTest {
@Mock
private PlatformTransactionManager transactionManager;
private JpaAutoAssignExecutor autoAssignChecker;
@Mock
LockRegistry lockRegistry;
private JpaAutoAssignHandler autoAssignHandler;
@BeforeEach
void before() {
autoAssignChecker = new JpaAutoAssignExecutor(
targetFilterQueryManagement, targetManagement, deploymentManagement, transactionManager);
autoAssignHandler = new JpaAutoAssignHandler(
targetFilterQueryManagement, targetManagement, deploymentManagement, transactionManager, lockRegistry, Optional.empty());
}
/**
* Single device check triggers update for matching auto assignment filter.
*/
@Test
void checkForDevice() {
void failToLockInHandleAll() {
final Lock lock = mock(Lock.class);
when(lock.tryLock()).thenReturn(false);
when(lockRegistry.obtain(any())).thenReturn(lock);
final TargetFilterQuery matching = mock(TargetFilterQuery.class);
when(targetFilterQueryManagement.findWithAutoAssignDS(any())).thenReturn(new SliceImpl<>(Arrays.asList(matching)));
assertThatNoException().isThrownBy(autoAssignHandler::handleAll);
}
/**
* Single device check triggers update for matching auto assignment filter.
*/
@Test
void handleSingleTarget() {
final String target = getRandomString();
final long ds = getRandomLong();
final TargetFilterQuery matching = mockFilterQuery(ds);
@@ -75,7 +97,7 @@ class AutoAssignExecutorTest {
when(targetManagement.isTargetMatchingQueryAndDSNotAssignedAndCompatibleAndUpdatable(target, ds, notMatching.getQuery()))
.thenReturn(false);
autoAssignChecker.checkSingleTarget(target);
autoAssignHandler.handleSingleTarget(target);
verify(deploymentManagement).assignDistributionSets(Mockito.argThat(deployReqMatcher(target, ds)), any());
Mockito.verifyNoMoreInteractions(deploymentManagement);