|
6 | 6 | import java.util.concurrent.TimeUnit;
|
7 | 7 | import java.util.function.BiFunction;
|
8 | 8 |
|
9 |
| -import org.junit.jupiter.api.BeforeAll; |
10 | 9 | import org.junit.jupiter.api.BeforeEach;
|
11 | 10 | import org.junit.jupiter.api.Test;
|
12 | 11 | import org.mockito.ArgumentCaptor;
|
@@ -60,37 +59,23 @@ class ReconciliationDispatcherTest {
|
60 | 59 | mock(ReconciliationDispatcher.CustomResourceFacade.class);
|
61 | 60 | private static ConfigurationService configurationService;
|
62 | 61 |
|
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 |
| - |
85 | 62 | @BeforeEach
|
86 | 63 | void setup() {
|
| 64 | + initConfigService(true); |
87 | 65 | testCustomResource = TestUtils.testCustomResource();
|
88 | 66 | reconciler = spy(new TestReconciler());
|
89 | 67 | reconciliationDispatcher =
|
90 | 68 | init(testCustomResource, reconciler, null, customResourceFacade, true);
|
91 | 69 | }
|
92 | 70 |
|
93 | 71 | 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 | + */ |
94 | 79 | configurationService =
|
95 | 80 | ConfigurationService.newOverriddenConfigurationService(new BaseConfigurationService(),
|
96 | 81 | overrider -> overrider.checkingCRDAndValidateLocalModel(false)
|
@@ -139,6 +124,21 @@ public boolean useFinalizer() {
|
139 | 124 | void addFinalizerOnNewResource() {
|
140 | 125 | assertFalse(testCustomResource.hasFinalizer(DEFAULT_FINALIZER));
|
141 | 126 | 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)); |
142 | 142 | verify(reconciler, never())
|
143 | 143 | .reconcile(ArgumentMatchers.eq(testCustomResource), any());
|
144 | 144 | verify(customResourceFacade, times(1))
|
@@ -346,14 +346,14 @@ void doesNotUpdateTheResourceIfNoUpdateUpdateControlIfFinalizerSet() {
|
346 | 346 | void addsFinalizerIfNotMarkedForDeletionAndEmptyCustomResourceReturned() {
|
347 | 347 | removeFinalizers(testCustomResource);
|
348 | 348 | reconciler.reconcile = (r, c) -> UpdateControl.noUpdate();
|
349 |
| - when(customResourceFacade.patchResource(any(), any())) |
| 349 | + when(customResourceFacade.patchResourceWithSSA(any())) |
350 | 350 | .thenReturn(testCustomResource);
|
351 | 351 |
|
352 | 352 | var postExecControl =
|
353 | 353 | reconciliationDispatcher.handleExecution(executionScopeWithCREvent(testCustomResource));
|
354 | 354 |
|
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())); |
357 | 357 | assertThat(postExecControl.updateIsStatusPatch()).isFalse();
|
358 | 358 | assertThat(postExecControl.getUpdatedCustomResource()).isPresent();
|
359 | 359 | }
|
@@ -509,12 +509,12 @@ void doesNotPatchObservedGenerationOnCustomResourcePatch() throws Exception {
|
509 | 509 | when(reconciler.reconcile(any(), any()))
|
510 | 510 | .thenReturn(UpdateControl.patchResource(observedGenResource));
|
511 | 511 | 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); |
514 | 513 |
|
515 |
| - PostExecutionControl<ObservedGenCustomResource> control = dispatcher.handleExecution( |
| 514 | + dispatcher.handleExecution( |
516 | 515 | executionScopeWithCREvent(observedGenResource));
|
517 |
| - assertThat(control.getUpdatedCustomResource()).isEmpty(); |
| 516 | + |
| 517 | + verify(facade, never()).patchStatus(any(), any()); |
518 | 518 | }
|
519 | 519 |
|
520 | 520 | @Test
|
@@ -667,7 +667,11 @@ void canSkipSchedulingMaxDelayIf() {
|
667 | 667 | }
|
668 | 668 |
|
669 | 669 | @Test
|
670 |
| - void retriesAddingFinalizer() { |
| 670 | + void retriesAddingFinalizerWithoutSSA() { |
| 671 | + initConfigService(false); |
| 672 | + reconciliationDispatcher = |
| 673 | + init(testCustomResource, reconciler, null, customResourceFacade, true); |
| 674 | + |
671 | 675 | removeFinalizers(testCustomResource);
|
672 | 676 | reconciler.reconcile = (r, c) -> UpdateControl.noUpdate();
|
673 | 677 | when(customResourceFacade.patchResource(any(), any()))
|
|
0 commit comments