7
7
import io .javaoperatorsdk .operator .api .UpdateControl ;
8
8
import io .javaoperatorsdk .operator .processing .EventDispatcher ;
9
9
import io .javaoperatorsdk .operator .processing .ExecutionScope ;
10
+ import io .javaoperatorsdk .operator .processing .ProcessingUtils ;
11
+ import io .javaoperatorsdk .operator .processing .event .Event ;
10
12
import io .javaoperatorsdk .operator .processing .event .internal .CustomResourceEvent ;
13
+ import io .javaoperatorsdk .operator .processing .event .internal .TimerEvent ;
11
14
import org .junit .jupiter .api .BeforeEach ;
12
15
import org .junit .jupiter .api .Test ;
13
16
import org .mockito .ArgumentMatchers ;
14
17
15
18
import java .util .ArrayList ;
16
19
import java .util .Arrays ;
17
20
import java .util .Collections ;
21
+ import java .util .List ;
18
22
19
23
import static org .junit .jupiter .api .Assertions .assertEquals ;
20
24
import static org .mockito .Mockito .*;
@@ -42,7 +46,7 @@ void setup() {
42
46
43
47
@ Test
44
48
void callCreateOrUpdateOnNewResource () {
45
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .ADDED , testCustomResource ));
49
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .ADDED , testCustomResource ));
46
50
verify (controller , times (1 )).createOrUpdateResource (ArgumentMatchers .eq (testCustomResource ), any ());
47
51
}
48
52
@@ -51,7 +55,7 @@ void updatesOnlyStatusSubResource() {
51
55
when (controller .createOrUpdateResource (eq (testCustomResource ), any ()))
52
56
.thenReturn (UpdateControl .updateStatusSubResource (testCustomResource ));
53
57
54
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .ADDED , testCustomResource ));
58
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .ADDED , testCustomResource ));
55
59
56
60
verify (customResourceFacade , times (1 )).updateStatus (testCustomResource );
57
61
verify (customResourceFacade , never ()).replaceWithLock (any ());
@@ -60,13 +64,13 @@ void updatesOnlyStatusSubResource() {
60
64
61
65
@ Test
62
66
void callCreateOrUpdateOnModifiedResource () {
63
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
67
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
64
68
verify (controller , times (1 )).createOrUpdateResource (ArgumentMatchers .eq (testCustomResource ), any ());
65
69
}
66
70
67
71
@ Test
68
72
void adsDefaultFinalizerOnCreateIfNotThere () {
69
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
73
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
70
74
verify (controller , times (1 ))
71
75
.createOrUpdateResource (argThat (testCustomResource ->
72
76
testCustomResource .getMetadata ().getFinalizers ().contains (DEFAULT_FINALIZER )), any ());
@@ -77,7 +81,7 @@ void callsDeleteIfObjectHasFinalizerAndMarkedForDelete() {
77
81
testCustomResource .getMetadata ().setDeletionTimestamp ("2019-8-10" );
78
82
testCustomResource .getMetadata ().getFinalizers ().add (DEFAULT_FINALIZER );
79
83
80
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
84
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
81
85
82
86
verify (controller , times (1 )).deleteResource (eq (testCustomResource ), any ());
83
87
}
@@ -89,7 +93,7 @@ void callsDeleteIfObjectHasFinalizerAndMarkedForDelete() {
89
93
void callDeleteOnControllerIfMarkedForDeletionButThereIsNoDefaultFinalizer () {
90
94
markForDeletion (testCustomResource );
91
95
92
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
96
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
93
97
94
98
verify (controller ).deleteResource (eq (testCustomResource ), any ());
95
99
}
@@ -98,7 +102,7 @@ void callDeleteOnControllerIfMarkedForDeletionButThereIsNoDefaultFinalizer() {
98
102
void removesDefaultFinalizerOnDelete () {
99
103
markForDeletion (testCustomResource );
100
104
101
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
105
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
102
106
103
107
assertEquals (0 , testCustomResource .getMetadata ().getFinalizers ().size ());
104
108
verify (customResourceFacade , times (1 )).replaceWithLock (any ());
@@ -109,7 +113,7 @@ void doesNotRemovesTheFinalizerIfTheDeleteNotMethodInstructsIt() {
109
113
when (controller .deleteResource (eq (testCustomResource ), any ())).thenReturn (DeleteControl .NO_FINALIZER_REMOVAL );
110
114
markForDeletion (testCustomResource );
111
115
112
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
116
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
113
117
114
118
assertEquals (1 , testCustomResource .getMetadata ().getFinalizers ().size ());
115
119
verify (customResourceFacade , never ()).replaceWithLock (any ());
@@ -119,7 +123,7 @@ void doesNotRemovesTheFinalizerIfTheDeleteNotMethodInstructsIt() {
119
123
void doesNotUpdateTheResourceIfNoUpdateUpdateControl () {
120
124
when (controller .createOrUpdateResource (eq (testCustomResource ), any ())).thenReturn (UpdateControl .noUpdate ());
121
125
122
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
126
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
123
127
verify (customResourceFacade , never ()).replaceWithLock (any ());
124
128
verify (customResourceFacade , never ()).updateStatus (testCustomResource );
125
129
}
@@ -129,7 +133,7 @@ void addsFinalizerIfNotMarkedForDeletionAndEmptyCustomResourceReturned() {
129
133
removeFinalizers (testCustomResource );
130
134
when (controller .createOrUpdateResource (eq (testCustomResource ), any ())).thenReturn (UpdateControl .noUpdate ());
131
135
132
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
136
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
133
137
134
138
assertEquals (1 , testCustomResource .getMetadata ().getFinalizers ().size ());
135
139
verify (customResourceFacade , times (1 )).replaceWithLock (any ());
@@ -140,29 +144,48 @@ void doesNotCallDeleteIfMarkedForDeletionButNotOurFinalizer() {
140
144
removeFinalizers (testCustomResource );
141
145
markForDeletion (testCustomResource );
142
146
143
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
147
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
144
148
145
149
verify (customResourceFacade , never ()).replaceWithLock (any ());
146
150
verify (controller , never ()).deleteResource (eq (testCustomResource ), any ());
147
151
}
148
152
153
+ @ Test
154
+ void executeControllerRegardlessGenerationInNonGenerationAwareMode () {
155
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
156
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
157
+
158
+ verify (controller , times (2 )).createOrUpdateResource (eq (testCustomResource ), any ());
159
+ }
160
+
149
161
@ Test
150
162
void skipsControllerExecutionOnIfGenerationAwareModeIfNotLargerGeneration () {
151
163
generationAwareMode ();
152
164
153
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
154
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
165
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
166
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
155
167
156
168
verify (controller , times (1 )).createOrUpdateResource (eq (testCustomResource ), any ());
157
169
}
158
170
159
171
@ Test
160
- void skipsExecutesControllerOnGenerationIncrease () {
172
+ void doNotSkipGenerationIfThereAreAdditionalEvents () {
173
+ generationAwareMode ();
174
+
175
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
176
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ,
177
+ nonCREvent (testCustomResource )));
178
+
179
+ verify (controller , times (2 )).createOrUpdateResource (eq (testCustomResource ), any ());
180
+ }
181
+
182
+ @ Test
183
+ void notSkipsExecutionOnGenerationIncrease () {
161
184
generationAwareMode ();
162
185
163
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
186
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
164
187
testCustomResource .getMetadata ().setGeneration (testCustomResource .getMetadata ().getGeneration () + 1 );
165
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
188
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
166
189
167
190
verify (controller , times (2 )).createOrUpdateResource (eq (testCustomResource ), any ());
168
191
}
@@ -174,8 +197,8 @@ void doesNotMarkNewGenerationInCaseOfException() {
174
197
.thenThrow (new IllegalStateException ("Exception for testing purposes" ))
175
198
.thenReturn (UpdateControl .noUpdate ());
176
199
177
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
178
- eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
200
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
201
+ eventDispatcher .handleEvent (executionScopeWithCREvent (Watcher .Action .MODIFIED , testCustomResource ));
179
202
180
203
verify (controller , times (2 )).createOrUpdateResource (eq (testCustomResource ), any ());
181
204
@@ -194,9 +217,16 @@ private void removeFinalizers(CustomResource customResource) {
194
217
customResource .getMetadata ().getFinalizers ().clear ();
195
218
}
196
219
197
-
198
- public ExecutionScope customResourceEvent (Watcher .Action action , CustomResource resource ) {
220
+ public ExecutionScope executionScopeWithCREvent (Watcher .Action action , CustomResource resource , Event ... otherEvents ) {
199
221
CustomResourceEvent event = new CustomResourceEvent (action , resource , null );
200
- return new ExecutionScope (Arrays .asList (event ), resource );
222
+ List <Event > eventList = new ArrayList <>(1 + otherEvents .length );
223
+ eventList .add (event );
224
+ eventList .addAll (Arrays .asList (otherEvents ));
225
+ return new ExecutionScope (eventList , resource );
226
+ }
227
+
228
+ public Event nonCREvent (CustomResource resource ) {
229
+ return new TimerEvent (ProcessingUtils .getUID (resource ), null );
201
230
}
231
+
202
232
}
0 commit comments