9
9
import io .fabric8 .kubernetes .api .model .ObjectMetaBuilder ;
10
10
import io .fabric8 .kubernetes .client .CustomResource ;
11
11
import io .fabric8 .kubernetes .client .Watcher ;
12
+ import org .junit .jupiter .api .Assertions ;
12
13
import org .junit .jupiter .api .BeforeEach ;
13
14
import org .junit .jupiter .api .Test ;
14
15
import org .mockito .ArgumentMatchers ;
@@ -21,31 +22,31 @@ class EventDispatcherTest {
21
22
22
23
private CustomResource testCustomResource ;
23
24
private EventDispatcher eventDispatcher ;
24
- private ResourceController <CustomResource > resourceController = mock (ResourceController .class );
25
+ private ResourceController <CustomResource > controller = mock (ResourceController .class );
25
26
private EventDispatcher .CustomResourceFacade customResourceFacade = mock (EventDispatcher .CustomResourceFacade .class );
26
27
27
28
@ BeforeEach
28
29
void setup () {
29
- eventDispatcher = new EventDispatcher (resourceController ,
30
+ eventDispatcher = new EventDispatcher (controller ,
30
31
DEFAULT_FINALIZER , customResourceFacade , false );
31
32
32
33
testCustomResource = getResource ();
33
34
34
- when (resourceController .createOrUpdateResource (eq (testCustomResource ), any ())).thenReturn (UpdateControl .updateCustomResource (testCustomResource ));
35
- when (resourceController .deleteResource (eq (testCustomResource ), any ())).thenReturn (true );
35
+ when (controller .createOrUpdateResource (eq (testCustomResource ), any ())).thenReturn (UpdateControl .updateCustomResource (testCustomResource ));
36
+ when (controller .deleteResource (eq (testCustomResource ), any ())).thenReturn (true );
36
37
when (customResourceFacade .replaceWithLock (any ())).thenReturn (null );
37
38
}
38
39
39
40
@ Test
40
41
void callCreateOrUpdateOnNewResource () {
41
42
eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .ADDED , testCustomResource ));
42
- verify (resourceController , times (1 )).createOrUpdateResource (ArgumentMatchers .eq (testCustomResource ), any ());
43
+ verify (controller , times (1 )).createOrUpdateResource (ArgumentMatchers .eq (testCustomResource ), any ());
43
44
}
44
45
45
46
@ Test
46
47
void updatesOnlyStatusSubResource () {
47
48
testCustomResource .getMetadata ().getFinalizers ().add (DEFAULT_FINALIZER );
48
- when (resourceController .createOrUpdateResource (eq (testCustomResource ), any ()))
49
+ when (controller .createOrUpdateResource (eq (testCustomResource ), any ()))
49
50
.thenReturn (UpdateControl .updateStatusSubResource (testCustomResource ));
50
51
51
52
eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .ADDED , testCustomResource ));
@@ -58,13 +59,13 @@ void updatesOnlyStatusSubResource() {
58
59
@ Test
59
60
void callCreateOrUpdateOnModifiedResource () {
60
61
eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
61
- verify (resourceController , times (1 )).createOrUpdateResource (ArgumentMatchers .eq (testCustomResource ), any ());
62
+ verify (controller , times (1 )).createOrUpdateResource (ArgumentMatchers .eq (testCustomResource ), any ());
62
63
}
63
64
64
65
@ Test
65
66
void adsDefaultFinalizerOnCreateIfNotThere () {
66
67
eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
67
- verify (resourceController , times (1 ))
68
+ verify (controller , times (1 ))
68
69
.createOrUpdateResource (argThat (testCustomResource ->
69
70
testCustomResource .getMetadata ().getFinalizers ().contains (DEFAULT_FINALIZER )), any ());
70
71
}
@@ -76,7 +77,7 @@ void callsDeleteIfObjectHasFinalizerAndMarkedForDelete() {
76
77
77
78
eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
78
79
79
- verify (resourceController , times (1 )).deleteResource (eq (testCustomResource ), any ());
80
+ verify (controller , times (1 )).deleteResource (eq (testCustomResource ), any ());
80
81
}
81
82
82
83
/**
@@ -88,7 +89,7 @@ void callDeleteOnControllerIfMarkedForDeletionButThereIsNoDefaultFinalizer() {
88
89
89
90
eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
90
91
91
- verify (resourceController ).deleteResource (eq (testCustomResource ), any ());
92
+ verify (controller ).deleteResource (eq (testCustomResource ), any ());
92
93
}
93
94
94
95
@ Test
@@ -103,7 +104,7 @@ void removesDefaultFinalizerOnDelete() {
103
104
104
105
@ Test
105
106
void doesNotRemovesTheFinalizerIfTheDeleteMethodRemovesFalse () {
106
- when (resourceController .deleteResource (eq (testCustomResource ), any ())).thenReturn (false );
107
+ when (controller .deleteResource (eq (testCustomResource ), any ())).thenReturn (false );
107
108
markForDeletion (testCustomResource );
108
109
109
110
eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
@@ -114,7 +115,7 @@ void doesNotRemovesTheFinalizerIfTheDeleteMethodRemovesFalse() {
114
115
115
116
@ Test
116
117
void doesNotUpdateTheResourceIfNoUpdateUpdateControl () {
117
- when (resourceController .createOrUpdateResource (eq (testCustomResource ), any ())).thenReturn (UpdateControl .noUpdate ());
118
+ when (controller .createOrUpdateResource (eq (testCustomResource ), any ())).thenReturn (UpdateControl .noUpdate ());
118
119
119
120
eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
120
121
verify (customResourceFacade , never ()).replaceWithLock (any ());
@@ -124,7 +125,7 @@ void doesNotUpdateTheResourceIfNoUpdateUpdateControl() {
124
125
@ Test
125
126
void addsFinalizerIfNotMarkedForDeletionAndEmptyCustomResourceReturned () {
126
127
removeFinalizers (testCustomResource );
127
- when (resourceController .createOrUpdateResource (eq (testCustomResource ), any ())).thenReturn (UpdateControl .noUpdate ());
128
+ when (controller .createOrUpdateResource (eq (testCustomResource ), any ())).thenReturn (UpdateControl .noUpdate ());
128
129
129
130
eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
130
131
@@ -140,7 +141,49 @@ void doesNotCallDeleteIfMarkedForDeletionButNotOurFinalizer() {
140
141
eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
141
142
142
143
verify (customResourceFacade , never ()).replaceWithLock (any ());
143
- verify (resourceController , never ()).deleteResource (eq (testCustomResource ), any ());
144
+ verify (controller , never ()).deleteResource (eq (testCustomResource ), any ());
145
+ }
146
+
147
+ @ Test
148
+ void skipsControllerExecutionOnIfGenerationAwareModeIfNotLargerGeneration () {
149
+ generationAwareMode ();
150
+
151
+ eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
152
+ eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
153
+
154
+ verify (controller , times (1 )).createOrUpdateResource (eq (testCustomResource ), any ());
155
+ }
156
+
157
+ @ Test
158
+ void skipsExecutesControllerOnGenerationIncrease () {
159
+ generationAwareMode ();
160
+
161
+ eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
162
+ testCustomResource .getMetadata ().setGeneration (testCustomResource .getMetadata ().getGeneration () + 1 );
163
+ eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
164
+
165
+ verify (controller , times (2 )).createOrUpdateResource (eq (testCustomResource ), any ());
166
+ }
167
+
168
+ @ Test
169
+ void doesNotMarkNewGenerationInCaseOfException () {
170
+ generationAwareMode ();
171
+ when (controller .createOrUpdateResource (any (), any ()))
172
+ .thenThrow (new IllegalStateException ("Exception for testing purposes" ))
173
+ .thenReturn (UpdateControl .noUpdate ());
174
+
175
+ Assertions .assertThrows (IllegalStateException .class , () -> {
176
+ eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
177
+ });
178
+ eventDispatcher .handleEvent (customResourceEvent (Watcher .Action .MODIFIED , testCustomResource ));
179
+
180
+ verify (controller , times (2 )).createOrUpdateResource (eq (testCustomResource ), any ());
181
+
182
+ }
183
+
184
+ void generationAwareMode () {
185
+ eventDispatcher = new EventDispatcher (controller ,
186
+ DEFAULT_FINALIZER , customResourceFacade , true );
144
187
}
145
188
146
189
private void markForDeletion (CustomResource customResource ) {
0 commit comments