2
2
3
3
import static org .assertj .core .api .Assertions .assertThat ;
4
4
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 ;
5
7
import static org .mockito .Mockito .any ;
6
8
import static org .mockito .Mockito .argThat ;
7
9
import static org .mockito .Mockito .eq ;
23
25
import io .javaoperatorsdk .operator .processing .event .internal .CustomResourceEvent ;
24
26
import java .util .ArrayList ;
25
27
import java .util .Arrays ;
26
- import java .util .Collections ;
27
28
import java .util .List ;
28
29
import org .junit .jupiter .api .BeforeEach ;
30
+ import org .junit .jupiter .api .Disabled ;
29
31
import org .junit .jupiter .api .Test ;
30
32
import org .mockito .ArgumentCaptor ;
31
33
import org .mockito .ArgumentMatchers ;
@@ -44,9 +46,6 @@ void setup() {
44
46
eventDispatcher = new EventDispatcher (controller , DEFAULT_FINALIZER , customResourceFacade );
45
47
46
48
testCustomResource = TestUtils .testCustomResource ();
47
- testCustomResource
48
- .getMetadata ()
49
- .setFinalizers (new ArrayList <>(Collections .singletonList (DEFAULT_FINALIZER )));
50
49
51
50
when (controller .createOrUpdateResource (eq (testCustomResource ), any ()))
52
51
.thenReturn (UpdateControl .updateCustomResource (testCustomResource ));
@@ -56,15 +55,29 @@ void setup() {
56
55
}
57
56
58
57
@ 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 );
60
71
eventDispatcher .handleExecution (
61
72
executionScopeWithCREvent (Watcher .Action .ADDED , testCustomResource ));
62
73
verify (controller , times (1 ))
63
74
.createOrUpdateResource (ArgumentMatchers .eq (testCustomResource ), any ());
64
75
}
65
76
66
77
@ Test
67
- void updatesOnlyStatusSubResource () {
78
+ void updatesOnlyStatusSubResourceIfFinalizerSet () {
79
+ testCustomResource .addFinalizer (DEFAULT_FINALIZER );
80
+
68
81
when (controller .createOrUpdateResource (eq (testCustomResource ), any ()))
69
82
.thenReturn (UpdateControl .updateStatusSubResource (testCustomResource ));
70
83
@@ -76,7 +89,9 @@ void updatesOnlyStatusSubResource() {
76
89
}
77
90
78
91
@ Test
79
- void updatesBothResourceAndStatus () {
92
+ void updatesBothResourceAndStatusIfFinalizerSet () {
93
+ testCustomResource .addFinalizer (DEFAULT_FINALIZER );
94
+
80
95
when (controller .createOrUpdateResource (eq (testCustomResource ), any ()))
81
96
.thenReturn (UpdateControl .updateCustomResourceAndStatus (testCustomResource ));
82
97
when (customResourceFacade .replaceWithLock (testCustomResource )).thenReturn (testCustomResource );
@@ -89,29 +104,32 @@ void updatesBothResourceAndStatus() {
89
104
}
90
105
91
106
@ Test
92
- void callCreateOrUpdateOnModifiedResource () {
107
+ void callCreateOrUpdateOnModifiedResourceIfFinalizerSet () {
108
+ testCustomResource .addFinalizer (DEFAULT_FINALIZER );
109
+
93
110
eventDispatcher .handleExecution (
94
111
executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
95
112
verify (controller , times (1 ))
96
113
.createOrUpdateResource (ArgumentMatchers .eq (testCustomResource ), any ());
97
114
}
98
115
99
116
@ 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 () {
101
120
eventDispatcher .handleExecution (
102
121
executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
103
122
verify (controller , times (1 ))
104
123
.createOrUpdateResource (
105
- argThat (
106
- testCustomResource ->
107
- testCustomResource .getMetadata ().getFinalizers ().contains (DEFAULT_FINALIZER )),
124
+ argThat (testCustomResource -> testCustomResource .hasFinalizer (DEFAULT_FINALIZER )),
108
125
any ());
109
126
}
110
127
111
128
@ Test
112
129
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 );
115
133
116
134
eventDispatcher .handleExecution (
117
135
executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
@@ -121,6 +139,8 @@ void callsDeleteIfObjectHasFinalizerAndMarkedForDelete() {
121
139
122
140
/** Note that there could be more finalizers. Out of our control. */
123
141
@ Test
142
+ @ Disabled (
143
+ "This test is wrong, it only passes if the finalizer is set despite what its name implies" )
124
144
void callDeleteOnControllerIfMarkedForDeletionButThereIsNoDefaultFinalizer () {
125
145
markForDeletion (testCustomResource );
126
146
@@ -131,7 +151,8 @@ void callDeleteOnControllerIfMarkedForDeletionButThereIsNoDefaultFinalizer() {
131
151
}
132
152
133
153
@ Test
134
- void removesDefaultFinalizerOnDelete () {
154
+ void removesDefaultFinalizerOnDeleteIfSet () {
155
+ testCustomResource .addFinalizer (DEFAULT_FINALIZER );
135
156
markForDeletion (testCustomResource );
136
157
137
158
eventDispatcher .handleExecution (
@@ -142,7 +163,9 @@ void removesDefaultFinalizerOnDelete() {
142
163
}
143
164
144
165
@ Test
145
- void doesNotRemovesTheFinalizerIfTheDeleteNotMethodInstructsIt () {
166
+ void doesNotRemovesTheSetFinalizerIfTheDeleteNotMethodInstructsIt () {
167
+ testCustomResource .addFinalizer (DEFAULT_FINALIZER );
168
+
146
169
when (controller .deleteResource (eq (testCustomResource ), any ()))
147
170
.thenReturn (DeleteControl .NO_FINALIZER_REMOVAL );
148
171
markForDeletion (testCustomResource );
@@ -155,7 +178,9 @@ void doesNotRemovesTheFinalizerIfTheDeleteNotMethodInstructsIt() {
155
178
}
156
179
157
180
@ Test
158
- void doesNotUpdateTheResourceIfNoUpdateUpdateControl () {
181
+ void doesNotUpdateTheResourceIfNoUpdateUpdateControlIfFinalizerSet () {
182
+ testCustomResource .addFinalizer (DEFAULT_FINALIZER );
183
+
159
184
when (controller .createOrUpdateResource (eq (testCustomResource ), any ()))
160
185
.thenReturn (UpdateControl .noUpdate ());
161
186
@@ -191,7 +216,8 @@ void doesNotCallDeleteIfMarkedForDeletionButNotOurFinalizer() {
191
216
}
192
217
193
218
@ Test
194
- void executeControllerRegardlessGenerationInNonGenerationAwareMode () {
219
+ void executeControllerRegardlessGenerationInNonGenerationAwareModeIfFinalizerSet () {
220
+ testCustomResource .addFinalizer (DEFAULT_FINALIZER );
195
221
eventDispatcher .handleExecution (
196
222
executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
197
223
eventDispatcher .handleExecution (
@@ -201,7 +227,9 @@ void executeControllerRegardlessGenerationInNonGenerationAwareMode() {
201
227
}
202
228
203
229
@ Test
204
- void propagatesRetryInfoToContext () {
230
+ void propagatesRetryInfoToContextIfFinalizerSet () {
231
+ testCustomResource .addFinalizer (DEFAULT_FINALIZER );
232
+
205
233
eventDispatcher .handleExecution (
206
234
new ExecutionScope (
207
235
Arrays .asList (),
@@ -223,8 +251,9 @@ public boolean isLastAttempt() {
223
251
verify (controller , times (1 ))
224
252
.createOrUpdateResource (eq (testCustomResource ), contextArgumentCaptor .capture ());
225
253
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 );
228
257
}
229
258
230
259
private void markForDeletion (CustomResource customResource ) {
0 commit comments