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