Skip to content

Commit 7a2018d

Browse files
author
attila.meszaros
committed
- event dispatcher tests
1 parent b28f9df commit 7a2018d

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

operator-framework/src/test/java/com/github/containersolutions/operator/EventDispatcherTest.java

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
1010
import io.fabric8.kubernetes.client.CustomResource;
1111
import io.fabric8.kubernetes.client.Watcher;
12+
import org.junit.jupiter.api.Assertions;
1213
import org.junit.jupiter.api.BeforeEach;
1314
import org.junit.jupiter.api.Test;
1415
import org.mockito.ArgumentMatchers;
@@ -21,31 +22,31 @@ class EventDispatcherTest {
2122

2223
private CustomResource testCustomResource;
2324
private EventDispatcher eventDispatcher;
24-
private ResourceController<CustomResource> resourceController = mock(ResourceController.class);
25+
private ResourceController<CustomResource> controller = mock(ResourceController.class);
2526
private EventDispatcher.CustomResourceFacade customResourceFacade = mock(EventDispatcher.CustomResourceFacade.class);
2627

2728
@BeforeEach
2829
void setup() {
29-
eventDispatcher = new EventDispatcher(resourceController,
30+
eventDispatcher = new EventDispatcher(controller,
3031
DEFAULT_FINALIZER, customResourceFacade, false);
3132

3233
testCustomResource = getResource();
3334

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);
3637
when(customResourceFacade.replaceWithLock(any())).thenReturn(null);
3738
}
3839

3940
@Test
4041
void callCreateOrUpdateOnNewResource() {
4142
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());
4344
}
4445

4546
@Test
4647
void updatesOnlyStatusSubResource() {
4748
testCustomResource.getMetadata().getFinalizers().add(DEFAULT_FINALIZER);
48-
when(resourceController.createOrUpdateResource(eq(testCustomResource), any()))
49+
when(controller.createOrUpdateResource(eq(testCustomResource), any()))
4950
.thenReturn(UpdateControl.updateStatusSubResource(testCustomResource));
5051

5152
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.ADDED, testCustomResource));
@@ -58,13 +59,13 @@ void updatesOnlyStatusSubResource() {
5859
@Test
5960
void callCreateOrUpdateOnModifiedResource() {
6061
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());
6263
}
6364

6465
@Test
6566
void adsDefaultFinalizerOnCreateIfNotThere() {
6667
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
67-
verify(resourceController, times(1))
68+
verify(controller, times(1))
6869
.createOrUpdateResource(argThat(testCustomResource ->
6970
testCustomResource.getMetadata().getFinalizers().contains(DEFAULT_FINALIZER)), any());
7071
}
@@ -76,7 +77,7 @@ void callsDeleteIfObjectHasFinalizerAndMarkedForDelete() {
7677

7778
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
7879

79-
verify(resourceController, times(1)).deleteResource(eq(testCustomResource), any());
80+
verify(controller, times(1)).deleteResource(eq(testCustomResource), any());
8081
}
8182

8283
/**
@@ -88,7 +89,7 @@ void callDeleteOnControllerIfMarkedForDeletionButThereIsNoDefaultFinalizer() {
8889

8990
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
9091

91-
verify(resourceController).deleteResource(eq(testCustomResource), any());
92+
verify(controller).deleteResource(eq(testCustomResource), any());
9293
}
9394

9495
@Test
@@ -103,7 +104,7 @@ void removesDefaultFinalizerOnDelete() {
103104

104105
@Test
105106
void doesNotRemovesTheFinalizerIfTheDeleteMethodRemovesFalse() {
106-
when(resourceController.deleteResource(eq(testCustomResource), any())).thenReturn(false);
107+
when(controller.deleteResource(eq(testCustomResource), any())).thenReturn(false);
107108
markForDeletion(testCustomResource);
108109

109110
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
@@ -114,7 +115,7 @@ void doesNotRemovesTheFinalizerIfTheDeleteMethodRemovesFalse() {
114115

115116
@Test
116117
void doesNotUpdateTheResourceIfNoUpdateUpdateControl() {
117-
when(resourceController.createOrUpdateResource(eq(testCustomResource), any())).thenReturn(UpdateControl.noUpdate());
118+
when(controller.createOrUpdateResource(eq(testCustomResource), any())).thenReturn(UpdateControl.noUpdate());
118119

119120
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
120121
verify(customResourceFacade, never()).replaceWithLock(any());
@@ -124,7 +125,7 @@ void doesNotUpdateTheResourceIfNoUpdateUpdateControl() {
124125
@Test
125126
void addsFinalizerIfNotMarkedForDeletionAndEmptyCustomResourceReturned() {
126127
removeFinalizers(testCustomResource);
127-
when(resourceController.createOrUpdateResource(eq(testCustomResource), any())).thenReturn(UpdateControl.noUpdate());
128+
when(controller.createOrUpdateResource(eq(testCustomResource), any())).thenReturn(UpdateControl.noUpdate());
128129

129130
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
130131

@@ -140,7 +141,49 @@ void doesNotCallDeleteIfMarkedForDeletionButNotOurFinalizer() {
140141
eventDispatcher.handleEvent(customResourceEvent(Watcher.Action.MODIFIED, testCustomResource));
141142

142143
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);
144187
}
145188

146189
private void markForDeletion(CustomResource customResource) {

0 commit comments

Comments
 (0)