Skip to content

test: Cleanup reschedule IT #1808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class CleanerForReconcilerIT {

@Test
void addsFinalizerAndCallsCleanupIfCleanerImplemented() {
CleanerForReconcilerTestReconciler reconciler =
operator.getReconcilerOfType(CleanerForReconcilerTestReconciler.class);
reconciler.setReScheduleCleanup(false);

var testResource = createTestResource();
operator.create(testResource);

Expand All @@ -34,12 +38,32 @@ void addsFinalizerAndCallsCleanupIfCleanerImplemented() {
await().until(
() -> operator.get(CleanerForReconcilerCustomResource.class, TEST_RESOURCE_NAME) == null);

CleanerForReconcilerTestReconciler reconciler =
(CleanerForReconcilerTestReconciler) operator.getFirstReconciler();
assertThat(reconciler.getNumberOfExecutions()).isEqualTo(1);
assertThat(reconciler.getNumberOfCleanupExecutions()).isEqualTo(1);
}

@Test
void reSchedulesCleanupIfInstructed() {
CleanerForReconcilerTestReconciler reconciler =
operator.getReconcilerOfType(CleanerForReconcilerTestReconciler.class);
reconciler.setReScheduleCleanup(true);

var testResource = createTestResource();
operator.create(testResource);

await().until(() -> !operator.get(CleanerForReconcilerCustomResource.class, TEST_RESOURCE_NAME)
.getMetadata().getFinalizers().isEmpty());

operator.delete(testResource);

await().untilAsserted(
() -> assertThat(reconciler.getNumberOfCleanupExecutions()).isGreaterThan(5));

reconciler.setReScheduleCleanup(false);
await().until(
() -> operator.get(CleanerForReconcilerCustomResource.class, TEST_RESOURCE_NAME) == null);
}

private CleanerForReconcilerCustomResource createTestResource() {
CleanerForReconcilerCustomResource cr = new CleanerForReconcilerCustomResource();
cr.setMetadata(new ObjectMeta());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ public class CleanerForReconcilerTestReconciler
Cleaner<CleanerForReconcilerCustomResource>,
TestExecutionInfoProvider {

public static final int RESCHEDULE_DELAY = 150;
private final AtomicInteger numberOfExecutions = new AtomicInteger(0);
private final AtomicInteger numberOfCleanupExecutions = new AtomicInteger(0);

private volatile boolean reScheduleCleanup = false;

@Override
public UpdateControl<CleanerForReconcilerCustomResource> reconcile(
CleanerForReconcilerCustomResource resource,
Expand All @@ -33,7 +36,17 @@ public int getNumberOfCleanupExecutions() {
@Override
public DeleteControl cleanup(CleanerForReconcilerCustomResource resource,
Context<CleanerForReconcilerCustomResource> context) {
numberOfCleanupExecutions.addAndGet(1);
return DeleteControl.defaultDelete();
if (reScheduleCleanup) {
numberOfCleanupExecutions.addAndGet(1);
return DeleteControl.noFinalizerRemoval().rescheduleAfter(RESCHEDULE_DELAY);
} else {
numberOfCleanupExecutions.addAndGet(1);
return DeleteControl.defaultDelete();
}
}

public CleanerForReconcilerTestReconciler setReScheduleCleanup(boolean reScheduleCleanup) {
this.reScheduleCleanup = reScheduleCleanup;
return this;
}
}