Skip to content

Commit e970d46

Browse files
committed
unit tests
Signed-off-by: Attila Mészáros <csviri@gmail.com>
1 parent b0f1447 commit e970d46

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/ReconciliationDispatcherTest.java

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.concurrent.TimeUnit;
77
import java.util.function.BiFunction;
88

9-
import org.junit.jupiter.api.BeforeAll;
109
import org.junit.jupiter.api.BeforeEach;
1110
import org.junit.jupiter.api.Test;
1211
import org.mockito.ArgumentCaptor;
@@ -60,37 +59,23 @@ class ReconciliationDispatcherTest {
6059
mock(ReconciliationDispatcher.CustomResourceFacade.class);
6160
private static ConfigurationService configurationService;
6261

63-
@BeforeAll
64-
static void classSetup() {
65-
/*
66-
* We need this for mock reconcilers to properly generate the expected UpdateControl: without
67-
* this, calls such as `when(reconciler.reconcile(eq(testCustomResource),
68-
* any())).thenReturn(UpdateControl.updateStatus(testCustomResource))` will return null because
69-
* equals will fail on the two equal but NOT identical TestCustomResources because equals is not
70-
* implemented on TestCustomResourceSpec or TestCustomResourceStatus
71-
*/
72-
initConfigService(true);
73-
// configurationService =
74-
// ConfigurationService.newOverriddenConfigurationService(new BaseConfigurationService(),
75-
// overrider -> overrider.checkingCRDAndValidateLocalModel(false)
76-
// .withResourceCloner(new Cloner() {
77-
// @Override
78-
// public <R extends HasMetadata> R clone(R object) {
79-
// return object;
80-
// }
81-
// })
82-
// .withUseSSAToPatchPrimaryResource(false));
83-
}
84-
8562
@BeforeEach
8663
void setup() {
64+
initConfigService(true);
8765
testCustomResource = TestUtils.testCustomResource();
8866
reconciler = spy(new TestReconciler());
8967
reconciliationDispatcher =
9068
init(testCustomResource, reconciler, null, customResourceFacade, true);
9169
}
9270

9371
static void initConfigService(boolean useSSA) {
72+
/*
73+
* We need this for mock reconcilers to properly generate the expected UpdateControl: without
74+
* this, calls such as `when(reconciler.reconcile(eq(testCustomResource),
75+
* any())).thenReturn(UpdateControl.updateStatus(testCustomResource))` will return null because
76+
* equals will fail on the two equal but NOT identical TestCustomResources because equals is not
77+
* implemented on TestCustomResourceSpec or TestCustomResourceStatus
78+
*/
9479
configurationService =
9580
ConfigurationService.newOverriddenConfigurationService(new BaseConfigurationService(),
9681
overrider -> overrider.checkingCRDAndValidateLocalModel(false)
@@ -139,6 +124,21 @@ public boolean useFinalizer() {
139124
void addFinalizerOnNewResource() {
140125
assertFalse(testCustomResource.hasFinalizer(DEFAULT_FINALIZER));
141126
reconciliationDispatcher.handleExecution(executionScopeWithCREvent(testCustomResource));
127+
verify(reconciler, never())
128+
.reconcile(ArgumentMatchers.eq(testCustomResource), any());
129+
verify(customResourceFacade, times(1))
130+
.patchResourceWithSSA(
131+
argThat(testCustomResource -> testCustomResource.hasFinalizer(DEFAULT_FINALIZER)));
132+
}
133+
134+
@Test
135+
void addFinalizerOnNewResourceWithoutSSA() {
136+
initConfigService(false);
137+
final ReconciliationDispatcher<TestCustomResource> dispatcher =
138+
init(testCustomResource, reconciler, null, customResourceFacade, true);
139+
140+
assertFalse(testCustomResource.hasFinalizer(DEFAULT_FINALIZER));
141+
dispatcher.handleExecution(executionScopeWithCREvent(testCustomResource));
142142
verify(reconciler, never())
143143
.reconcile(ArgumentMatchers.eq(testCustomResource), any());
144144
verify(customResourceFacade, times(1))
@@ -346,14 +346,14 @@ void doesNotUpdateTheResourceIfNoUpdateUpdateControlIfFinalizerSet() {
346346
void addsFinalizerIfNotMarkedForDeletionAndEmptyCustomResourceReturned() {
347347
removeFinalizers(testCustomResource);
348348
reconciler.reconcile = (r, c) -> UpdateControl.noUpdate();
349-
when(customResourceFacade.patchResource(any(), any()))
349+
when(customResourceFacade.patchResourceWithSSA(any()))
350350
.thenReturn(testCustomResource);
351351

352352
var postExecControl =
353353
reconciliationDispatcher.handleExecution(executionScopeWithCREvent(testCustomResource));
354354

355-
assertEquals(1, testCustomResource.getMetadata().getFinalizers().size());
356-
verify(customResourceFacade, times(1)).patchResource(any(), any());
355+
verify(customResourceFacade, times(1))
356+
.patchResourceWithSSA(argThat(a -> !a.getMetadata().getFinalizers().isEmpty()));
357357
assertThat(postExecControl.updateIsStatusPatch()).isFalse();
358358
assertThat(postExecControl.getUpdatedCustomResource()).isPresent();
359359
}
@@ -509,12 +509,12 @@ void doesNotPatchObservedGenerationOnCustomResourcePatch() throws Exception {
509509
when(reconciler.reconcile(any(), any()))
510510
.thenReturn(UpdateControl.patchResource(observedGenResource));
511511
when(facade.patchResource(any(), any())).thenReturn(observedGenResource);
512-
when(facade.patchStatus(eq(observedGenResource), any())).thenReturn(observedGenResource);
513-
var dispatcher = init(observedGenResource, reconciler, config, facade, true);
512+
var dispatcher = init(observedGenResource, reconciler, config, facade, false);
514513

515-
PostExecutionControl<ObservedGenCustomResource> control = dispatcher.handleExecution(
514+
dispatcher.handleExecution(
516515
executionScopeWithCREvent(observedGenResource));
517-
assertThat(control.getUpdatedCustomResource()).isEmpty();
516+
517+
verify(facade, never()).patchStatus(any(), any());
518518
}
519519

520520
@Test
@@ -667,7 +667,11 @@ void canSkipSchedulingMaxDelayIf() {
667667
}
668668

669669
@Test
670-
void retriesAddingFinalizer() {
670+
void retriesAddingFinalizerWithoutSSA() {
671+
initConfigService(false);
672+
reconciliationDispatcher =
673+
init(testCustomResource, reconciler, null, customResourceFacade, true);
674+
671675
removeFinalizers(testCustomResource);
672676
reconciler.reconcile = (r, c) -> UpdateControl.noUpdate();
673677
when(customResourceFacade.patchResource(any(), any()))

0 commit comments

Comments
 (0)