10
10
import io .fabric8 .kubernetes .api .model .HasMetadata ;
11
11
import io .fabric8 .kubernetes .api .model .ObjectMeta ;
12
12
import io .fabric8 .kubernetes .client .CustomResource ;
13
+ import io .javaoperatorsdk .operator .MockKubernetesClient ;
13
14
import io .javaoperatorsdk .operator .TestUtils ;
14
15
import io .javaoperatorsdk .operator .api .config .ConfigurationService ;
15
16
import io .javaoperatorsdk .operator .api .config .ControllerConfiguration ;
17
+ import io .javaoperatorsdk .operator .api .config .ExecutorServiceManager ;
18
+ import io .javaoperatorsdk .operator .api .config .RetryConfiguration ;
16
19
import io .javaoperatorsdk .operator .api .monitoring .Metrics ;
17
20
import io .javaoperatorsdk .operator .api .reconciler .*;
18
21
import io .javaoperatorsdk .operator .processing .Controller ;
@@ -35,35 +38,41 @@ class ReconciliationDispatcherTest {
35
38
private ReconciliationDispatcher <TestCustomResource > reconciliationDispatcher ;
36
39
private final Reconciler <TestCustomResource > reconciler = mock (Reconciler .class ,
37
40
withSettings ().extraInterfaces (ErrorStatusHandler .class ));
38
- private final ControllerConfiguration <TestCustomResource > configuration =
39
- mock (ControllerConfiguration .class );
40
41
private final ConfigurationService configService = mock (ConfigurationService .class );
41
42
private final CustomResourceFacade <TestCustomResource > customResourceFacade =
42
43
mock (ReconciliationDispatcher .CustomResourceFacade .class );
43
44
44
45
@ BeforeEach
45
46
void setup () {
47
+ ExecutorServiceManager .useTestInstance ();
46
48
testCustomResource = TestUtils .testCustomResource ();
47
49
reconciliationDispatcher =
48
- init (testCustomResource , reconciler , configuration , customResourceFacade );
50
+ init (testCustomResource , reconciler , null , customResourceFacade , true );
49
51
}
50
52
51
53
private <R extends HasMetadata > ReconciliationDispatcher <R > init (R customResource ,
52
54
Reconciler <R > reconciler , ControllerConfiguration <R > configuration ,
53
- CustomResourceFacade <R > customResourceFacade ) {
54
- when (configuration .getFinalizer ()).thenReturn (DEFAULT_FINALIZER );
55
+ CustomResourceFacade <R > customResourceFacade , boolean useFinalizer ) {
56
+ configuration = configuration == null ? mock (ControllerConfiguration .class ) : configuration ;
57
+ final var finalizer = useFinalizer ? DEFAULT_FINALIZER : Constants .NO_FINALIZER ;
58
+ when (configuration .getFinalizer ()).thenReturn (finalizer );
55
59
when (configuration .useFinalizer ()).thenCallRealMethod ();
56
60
when (configuration .getName ()).thenReturn ("EventDispatcherTestController" );
57
- when (configService .getMetrics ()).thenReturn (Metrics .NOOP );
61
+ when (configuration .getResourceClass ()).thenReturn ((Class <R >) customResource .getClass ());
62
+ when (configuration .getRetryConfiguration ()).thenReturn (RetryConfiguration .DEFAULT );
58
63
when (configuration .getConfigurationService ()).thenReturn (configService );
64
+
65
+ when (configService .getMetrics ()).thenReturn (Metrics .NOOP );
59
66
when (configService .getResourceCloner ()).thenReturn (ConfigurationService .DEFAULT_CLONER );
67
+
60
68
when (reconciler .reconcile (eq (customResource ), any ()))
61
69
.thenReturn (UpdateControl .updateResource (customResource ));
62
70
when (reconciler .cleanup (eq (customResource ), any ()))
63
71
.thenReturn (DeleteControl .defaultDelete ());
64
72
when (customResourceFacade .replaceWithLock (any ())).thenReturn (null );
65
- Controller <R > controller =
66
- new Controller <>(reconciler , configuration , null );
73
+ Controller <R > controller = new Controller <>(reconciler , configuration ,
74
+ MockKubernetesClient .client (customResource .getClass ()));
75
+ controller .start ();
67
76
68
77
return new ReconciliationDispatcher <>(controller , customResourceFacade );
69
78
}
@@ -140,10 +149,12 @@ void callsDeleteIfObjectHasFinalizerAndMarkedForDelete() {
140
149
*/
141
150
@ Test
142
151
void callDeleteOnControllerIfMarkedForDeletionWhenNoFinalizerIsConfigured () {
143
- configureToNotUseFinalizer ();
152
+ final ReconciliationDispatcher <TestCustomResource > dispatcher =
153
+ init (testCustomResource , reconciler ,
154
+ null , customResourceFacade , false );
144
155
markForDeletion (testCustomResource );
145
156
146
- reconciliationDispatcher .handleExecution (executionScopeWithCREvent (testCustomResource ));
157
+ dispatcher .handleExecution (executionScopeWithCREvent (testCustomResource ));
147
158
148
159
verify (reconciler ).cleanup (eq (testCustomResource ), any ());
149
160
}
@@ -157,23 +168,13 @@ void doNotCallDeleteIfMarkedForDeletionWhenFinalizerHasAlreadyBeenRemoved() {
157
168
verify (reconciler , never ()).cleanup (eq (testCustomResource ), any ());
158
169
}
159
170
160
- private void configureToNotUseFinalizer () {
161
- ControllerConfiguration <HasMetadata > configuration =
162
- mock (ControllerConfiguration .class );
163
- when (configuration .getName ()).thenReturn ("EventDispatcherTestController" );
164
- when (configService .getMetrics ()).thenReturn (Metrics .NOOP );
165
- when (configuration .getConfigurationService ()).thenReturn (configService );
166
- when (configuration .useFinalizer ()).thenReturn (false );
167
- reconciliationDispatcher =
168
- new ReconciliationDispatcher (new Controller (reconciler , configuration , null ),
169
- customResourceFacade );
170
- }
171
-
172
171
@ Test
173
172
void doesNotAddFinalizerIfConfiguredNotTo () {
174
- configureToNotUseFinalizer ();
173
+ final ReconciliationDispatcher <TestCustomResource > dispatcher =
174
+ init (testCustomResource , reconciler ,
175
+ null , customResourceFacade , false );
175
176
176
- reconciliationDispatcher .handleExecution (executionScopeWithCREvent (testCustomResource ));
177
+ dispatcher .handleExecution (executionScopeWithCREvent (testCustomResource ));
177
178
178
179
assertEquals (0 , testCustomResource .getMetadata ().getFinalizers ().size ());
179
180
}
@@ -312,7 +313,7 @@ void setObservedGenerationForStatusIfNeeded() {
312
313
ControllerConfiguration <ObservedGenCustomResource > config =
313
314
mock (ControllerConfiguration .class );
314
315
CustomResourceFacade <ObservedGenCustomResource > facade = mock (CustomResourceFacade .class );
315
- var dispatcher = init (observedGenResource , reconciler , config , facade );
316
+ var dispatcher = init (observedGenResource , reconciler , config , facade , true );
316
317
317
318
when (config .isGenerationAware ()).thenReturn (true );
318
319
when (reconciler .reconcile (any (), any ()))
@@ -337,7 +338,7 @@ void updatesObservedGenerationOnNoUpdateUpdateControl() {
337
338
when (reconciler .reconcile (any (), any ()))
338
339
.thenReturn (UpdateControl .noUpdate ());
339
340
when (facade .updateStatus (observedGenResource )).thenReturn (observedGenResource );
340
- var dispatcher = init (observedGenResource , reconciler , config , facade );
341
+ var dispatcher = init (observedGenResource , reconciler , config , facade , true );
341
342
342
343
PostExecutionControl <ObservedGenCustomResource > control = dispatcher .handleExecution (
343
344
executionScopeWithCREvent (observedGenResource ));
0 commit comments