Skip to content

Commit 2818b49

Browse files
committed
fix: make test intent more explicit, disable wrong tests
1 parent aff6db3 commit 2818b49

File tree

1 file changed

+50
-21
lines changed

1 file changed

+50
-21
lines changed

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

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
57
import static org.mockito.Mockito.any;
68
import static org.mockito.Mockito.argThat;
79
import static org.mockito.Mockito.eq;
@@ -23,9 +25,9 @@
2325
import io.javaoperatorsdk.operator.processing.event.internal.CustomResourceEvent;
2426
import java.util.ArrayList;
2527
import java.util.Arrays;
26-
import java.util.Collections;
2728
import java.util.List;
2829
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Disabled;
2931
import org.junit.jupiter.api.Test;
3032
import org.mockito.ArgumentCaptor;
3133
import org.mockito.ArgumentMatchers;
@@ -44,9 +46,6 @@ void setup() {
4446
eventDispatcher = new EventDispatcher(controller, DEFAULT_FINALIZER, customResourceFacade);
4547

4648
testCustomResource = TestUtils.testCustomResource();
47-
testCustomResource
48-
.getMetadata()
49-
.setFinalizers(new ArrayList<>(Collections.singletonList(DEFAULT_FINALIZER)));
5049

5150
when(controller.createOrUpdateResource(eq(testCustomResource), any()))
5251
.thenReturn(UpdateControl.updateCustomResource(testCustomResource));
@@ -56,15 +55,29 @@ void setup() {
5655
}
5756

5857
@Test
59-
void callCreateOrUpdateOnNewResource() {
58+
void addFinalizerOnNewResource() {
59+
assertFalse(testCustomResource.hasFinalizer(DEFAULT_FINALIZER));
60+
eventDispatcher.handleExecution(
61+
executionScopeWithCREvent(Watcher.Action.ADDED, testCustomResource));
62+
verify(controller, never())
63+
.createOrUpdateResource(ArgumentMatchers.eq(testCustomResource), any());
64+
verify(customResourceFacade, times(1)).replaceWithLock(testCustomResource);
65+
assertTrue(testCustomResource.hasFinalizer(DEFAULT_FINALIZER));
66+
}
67+
68+
@Test
69+
void callCreateOrUpdateOnNewResourceIfFinalizerSet() {
70+
testCustomResource.addFinalizer(DEFAULT_FINALIZER);
6071
eventDispatcher.handleExecution(
6172
executionScopeWithCREvent(Watcher.Action.ADDED, testCustomResource));
6273
verify(controller, times(1))
6374
.createOrUpdateResource(ArgumentMatchers.eq(testCustomResource), any());
6475
}
6576

6677
@Test
67-
void updatesOnlyStatusSubResource() {
78+
void updatesOnlyStatusSubResourceIfFinalizerSet() {
79+
testCustomResource.addFinalizer(DEFAULT_FINALIZER);
80+
6881
when(controller.createOrUpdateResource(eq(testCustomResource), any()))
6982
.thenReturn(UpdateControl.updateStatusSubResource(testCustomResource));
7083

@@ -76,7 +89,9 @@ void updatesOnlyStatusSubResource() {
7689
}
7790

7891
@Test
79-
void updatesBothResourceAndStatus() {
92+
void updatesBothResourceAndStatusIfFinalizerSet() {
93+
testCustomResource.addFinalizer(DEFAULT_FINALIZER);
94+
8095
when(controller.createOrUpdateResource(eq(testCustomResource), any()))
8196
.thenReturn(UpdateControl.updateCustomResourceAndStatus(testCustomResource));
8297
when(customResourceFacade.replaceWithLock(testCustomResource)).thenReturn(testCustomResource);
@@ -89,29 +104,32 @@ void updatesBothResourceAndStatus() {
89104
}
90105

91106
@Test
92-
void callCreateOrUpdateOnModifiedResource() {
107+
void callCreateOrUpdateOnModifiedResourceIfFinalizerSet() {
108+
testCustomResource.addFinalizer(DEFAULT_FINALIZER);
109+
93110
eventDispatcher.handleExecution(
94111
executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
95112
verify(controller, times(1))
96113
.createOrUpdateResource(ArgumentMatchers.eq(testCustomResource), any());
97114
}
98115

99116
@Test
100-
void adsDefaultFinalizerOnCreateIfNotThere() {
117+
@Disabled(
118+
"This test is wrong, if the finalizer is not present, it is added, bypassing calling the controller")
119+
void addsDefaultFinalizerOnCreateIfNotThere() {
101120
eventDispatcher.handleExecution(
102121
executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
103122
verify(controller, times(1))
104123
.createOrUpdateResource(
105-
argThat(
106-
testCustomResource ->
107-
testCustomResource.getMetadata().getFinalizers().contains(DEFAULT_FINALIZER)),
124+
argThat(testCustomResource -> testCustomResource.hasFinalizer(DEFAULT_FINALIZER)),
108125
any());
109126
}
110127

111128
@Test
112129
void callsDeleteIfObjectHasFinalizerAndMarkedForDelete() {
113-
testCustomResource.getMetadata().setDeletionTimestamp("2019-8-10");
114-
testCustomResource.getMetadata().getFinalizers().add(DEFAULT_FINALIZER);
130+
// we need to add the finalizer before marking it for deletion, as otherwise it won't get added
131+
assertTrue(testCustomResource.addFinalizer(DEFAULT_FINALIZER));
132+
markForDeletion(testCustomResource);
115133

116134
eventDispatcher.handleExecution(
117135
executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
@@ -121,6 +139,8 @@ void callsDeleteIfObjectHasFinalizerAndMarkedForDelete() {
121139

122140
/** Note that there could be more finalizers. Out of our control. */
123141
@Test
142+
@Disabled(
143+
"This test is wrong, it only passes if the finalizer is set despite what its name implies")
124144
void callDeleteOnControllerIfMarkedForDeletionButThereIsNoDefaultFinalizer() {
125145
markForDeletion(testCustomResource);
126146

@@ -131,7 +151,8 @@ void callDeleteOnControllerIfMarkedForDeletionButThereIsNoDefaultFinalizer() {
131151
}
132152

133153
@Test
134-
void removesDefaultFinalizerOnDelete() {
154+
void removesDefaultFinalizerOnDeleteIfSet() {
155+
testCustomResource.addFinalizer(DEFAULT_FINALIZER);
135156
markForDeletion(testCustomResource);
136157

137158
eventDispatcher.handleExecution(
@@ -142,7 +163,9 @@ void removesDefaultFinalizerOnDelete() {
142163
}
143164

144165
@Test
145-
void doesNotRemovesTheFinalizerIfTheDeleteNotMethodInstructsIt() {
166+
void doesNotRemovesTheSetFinalizerIfTheDeleteNotMethodInstructsIt() {
167+
testCustomResource.addFinalizer(DEFAULT_FINALIZER);
168+
146169
when(controller.deleteResource(eq(testCustomResource), any()))
147170
.thenReturn(DeleteControl.NO_FINALIZER_REMOVAL);
148171
markForDeletion(testCustomResource);
@@ -155,7 +178,9 @@ void doesNotRemovesTheFinalizerIfTheDeleteNotMethodInstructsIt() {
155178
}
156179

157180
@Test
158-
void doesNotUpdateTheResourceIfNoUpdateUpdateControl() {
181+
void doesNotUpdateTheResourceIfNoUpdateUpdateControlIfFinalizerSet() {
182+
testCustomResource.addFinalizer(DEFAULT_FINALIZER);
183+
159184
when(controller.createOrUpdateResource(eq(testCustomResource), any()))
160185
.thenReturn(UpdateControl.noUpdate());
161186

@@ -191,7 +216,8 @@ void doesNotCallDeleteIfMarkedForDeletionButNotOurFinalizer() {
191216
}
192217

193218
@Test
194-
void executeControllerRegardlessGenerationInNonGenerationAwareMode() {
219+
void executeControllerRegardlessGenerationInNonGenerationAwareModeIfFinalizerSet() {
220+
testCustomResource.addFinalizer(DEFAULT_FINALIZER);
195221
eventDispatcher.handleExecution(
196222
executionScopeWithCREvent(Watcher.Action.MODIFIED, testCustomResource));
197223
eventDispatcher.handleExecution(
@@ -201,7 +227,9 @@ void executeControllerRegardlessGenerationInNonGenerationAwareMode() {
201227
}
202228

203229
@Test
204-
void propagatesRetryInfoToContext() {
230+
void propagatesRetryInfoToContextIfFinalizerSet() {
231+
testCustomResource.addFinalizer(DEFAULT_FINALIZER);
232+
205233
eventDispatcher.handleExecution(
206234
new ExecutionScope(
207235
Arrays.asList(),
@@ -223,8 +251,9 @@ public boolean isLastAttempt() {
223251
verify(controller, times(1))
224252
.createOrUpdateResource(eq(testCustomResource), contextArgumentCaptor.capture());
225253
Context<CustomResource> context = contextArgumentCaptor.getValue();
226-
assertThat(context.getRetryInfo().get().getAttemptCount()).isEqualTo(2);
227-
assertThat(context.getRetryInfo().get().isLastAttempt()).isEqualTo(true);
254+
final var retryInfo = context.getRetryInfo().get();
255+
assertThat(retryInfo.getAttemptCount()).isEqualTo(2);
256+
assertThat(retryInfo.isLastAttempt()).isEqualTo(true);
228257
}
229258

230259
private void markForDeletion(CustomResource customResource) {

0 commit comments

Comments
 (0)