|
15 | 15 | */ |
16 | 16 | package io.javaoperatorsdk.operator.api.reconciler; |
17 | 17 |
|
| 18 | +import java.util.function.UnaryOperator; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.BeforeEach; |
18 | 21 | import org.junit.jupiter.api.Disabled; |
19 | 22 | import org.junit.jupiter.api.Test; |
20 | 23 | import org.slf4j.Logger; |
|
23 | 26 | import io.fabric8.kubernetes.api.model.HasMetadata; |
24 | 27 | import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
25 | 28 | import io.fabric8.kubernetes.api.model.PodBuilder; |
| 29 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 30 | +import io.fabric8.kubernetes.client.KubernetesClientException; |
| 31 | +import io.fabric8.kubernetes.client.dsl.MixedOperation; |
| 32 | +import io.fabric8.kubernetes.client.dsl.Resource; |
| 33 | +import io.javaoperatorsdk.operator.TestUtils; |
| 34 | +import io.javaoperatorsdk.operator.api.config.ControllerConfiguration; |
| 35 | +import io.javaoperatorsdk.operator.processing.event.EventSourceRetriever; |
| 36 | +import io.javaoperatorsdk.operator.processing.event.source.controller.ControllerEventSource; |
| 37 | +import io.javaoperatorsdk.operator.sample.simple.TestCustomResource; |
26 | 38 |
|
27 | 39 | import static org.assertj.core.api.Assertions.assertThat; |
28 | 40 | import static org.junit.jupiter.api.Assertions.*; |
| 41 | +import static org.mockito.ArgumentMatchers.any; |
| 42 | +import static org.mockito.Mockito.*; |
29 | 43 |
|
30 | 44 | class ReconcileUtilsTest { |
31 | 45 |
|
32 | 46 | private static final Logger log = LoggerFactory.getLogger(ReconcileUtilsTest.class); |
| 47 | + private static final String FINALIZER_NAME = "test.javaoperatorsdk.io/finalizer"; |
| 48 | + |
| 49 | + private Context<TestCustomResource> context; |
| 50 | + private KubernetesClient client; |
| 51 | + private MixedOperation mixedOperation; |
| 52 | + private Resource resourceOp; |
| 53 | + private ControllerEventSource<TestCustomResource> controllerEventSource; |
| 54 | + private ControllerConfiguration<TestCustomResource> controllerConfiguration; |
| 55 | + |
| 56 | + @BeforeEach |
| 57 | + @SuppressWarnings("unchecked") |
| 58 | + void setupMocks() { |
| 59 | + context = mock(Context.class); |
| 60 | + client = mock(KubernetesClient.class); |
| 61 | + mixedOperation = mock(MixedOperation.class); |
| 62 | + resourceOp = mock(Resource.class); |
| 63 | + controllerEventSource = mock(ControllerEventSource.class); |
| 64 | + controllerConfiguration = mock(ControllerConfiguration.class); |
| 65 | + |
| 66 | + var eventSourceRetriever = mock(EventSourceRetriever.class); |
| 67 | + |
| 68 | + when(context.getClient()).thenReturn(client); |
| 69 | + when(context.eventSourceRetriever()).thenReturn(eventSourceRetriever); |
| 70 | + when(context.getControllerConfiguration()).thenReturn(controllerConfiguration); |
| 71 | + when(controllerConfiguration.getFinalizerName()).thenReturn(FINALIZER_NAME); |
| 72 | + when(eventSourceRetriever.getControllerEventSource()).thenReturn(controllerEventSource); |
| 73 | + |
| 74 | + when(client.resources(TestCustomResource.class)).thenReturn(mixedOperation); |
| 75 | + when(mixedOperation.inNamespace(any())).thenReturn(mixedOperation); |
| 76 | + when(mixedOperation.withName(any())).thenReturn(resourceOp); |
| 77 | + } |
33 | 78 |
|
34 | 79 | @Test |
35 | 80 | void validateAndCompareResourceVersionsTest() { |
@@ -162,17 +207,94 @@ public void compareResourcePerformanceTest() { |
162 | 207 |
|
163 | 208 | @Test |
164 | 209 | void retriesAddingFinalizerWithoutSSA() { |
| 210 | + var resource = TestUtils.testCustomResource1(); |
| 211 | + resource.getMetadata().setResourceVersion("1"); |
| 212 | + |
| 213 | + when(context.getPrimaryResource()).thenReturn(resource); |
| 214 | + |
| 215 | + // First call throws conflict, second succeeds |
| 216 | + when(controllerEventSource.eventFilteringUpdateAndCacheResource( |
| 217 | + any(), any(UnaryOperator.class))) |
| 218 | + .thenThrow(new KubernetesClientException("Conflict", 409, null)) |
| 219 | + .thenAnswer( |
| 220 | + invocation -> { |
| 221 | + var res = TestUtils.testCustomResource1(); |
| 222 | + res.getMetadata().setResourceVersion("2"); |
| 223 | + res.addFinalizer(FINALIZER_NAME); |
| 224 | + return res; |
| 225 | + }); |
| 226 | + |
| 227 | + // Return fresh resource on retry |
| 228 | + when(resourceOp.get()).thenReturn(resource); |
| 229 | + |
| 230 | + var result = ReconcileUtils.addFinalizer(context, FINALIZER_NAME); |
165 | 231 |
|
166 | | - // todo |
| 232 | + assertThat(result).isNotNull(); |
| 233 | + assertThat(result.hasFinalizer(FINALIZER_NAME)).isTrue(); |
| 234 | + verify(controllerEventSource, times(2)) |
| 235 | + .eventFilteringUpdateAndCacheResource(any(), any(UnaryOperator.class)); |
| 236 | + verify(resourceOp, times(1)).get(); |
167 | 237 | } |
168 | 238 |
|
| 239 | + // todo double check |
169 | 240 | @Test |
170 | 241 | void nullResourceIsGracefullyHandledOnFinalizerRemovalRetry() { |
171 | | - // todo |
| 242 | + var resource = TestUtils.testCustomResource1(); |
| 243 | + resource.getMetadata().setResourceVersion("1"); |
| 244 | + resource.addFinalizer(FINALIZER_NAME); |
| 245 | + |
| 246 | + when(context.getPrimaryResource()).thenReturn(resource); |
| 247 | + |
| 248 | + // First call throws conflict |
| 249 | + when(controllerEventSource.eventFilteringUpdateAndCacheResource( |
| 250 | + any(), any(UnaryOperator.class))) |
| 251 | + .thenThrow(new KubernetesClientException("Conflict", 409, null)); |
| 252 | + |
| 253 | + // Return null on retry (resource was deleted) |
| 254 | + when(resourceOp.get()).thenReturn(null); |
| 255 | + |
| 256 | + // Should throw NullPointerException when resource is null after retry |
| 257 | + assertThrows( |
| 258 | + NullPointerException.class, () -> ReconcileUtils.removeFinalizer(context, FINALIZER_NAME)); |
| 259 | + |
| 260 | + verify(controllerEventSource, times(1)) |
| 261 | + .eventFilteringUpdateAndCacheResource(any(), any(UnaryOperator.class)); |
| 262 | + verify(resourceOp, times(1)).get(); |
172 | 263 | } |
173 | 264 |
|
174 | 265 | @Test |
175 | 266 | void retriesFinalizerRemovalWithFreshResource() { |
176 | | - // todo |
| 267 | + var originalResource = TestUtils.testCustomResource1(); |
| 268 | + originalResource.getMetadata().setResourceVersion("1"); |
| 269 | + originalResource.addFinalizer(FINALIZER_NAME); |
| 270 | + |
| 271 | + when(context.getPrimaryResource()).thenReturn(originalResource); |
| 272 | + |
| 273 | + // First call throws unprocessable (422), second succeeds |
| 274 | + when(controllerEventSource.eventFilteringUpdateAndCacheResource( |
| 275 | + any(), any(UnaryOperator.class))) |
| 276 | + .thenThrow(new KubernetesClientException("Unprocessable", 422, null)) |
| 277 | + .thenAnswer( |
| 278 | + invocation -> { |
| 279 | + var res = TestUtils.testCustomResource1(); |
| 280 | + res.getMetadata().setResourceVersion("3"); |
| 281 | + // finalizer should be removed |
| 282 | + return res; |
| 283 | + }); |
| 284 | + |
| 285 | + // Return fresh resource with newer version on retry |
| 286 | + var freshResource = TestUtils.testCustomResource1(); |
| 287 | + freshResource.getMetadata().setResourceVersion("2"); |
| 288 | + freshResource.addFinalizer(FINALIZER_NAME); |
| 289 | + when(resourceOp.get()).thenReturn(freshResource); |
| 290 | + |
| 291 | + var result = ReconcileUtils.removeFinalizer(context, FINALIZER_NAME); |
| 292 | + |
| 293 | + assertThat(result).isNotNull(); |
| 294 | + assertThat(result.getMetadata().getResourceVersion()).isEqualTo("3"); |
| 295 | + assertThat(result.hasFinalizer(FINALIZER_NAME)).isFalse(); |
| 296 | + verify(controllerEventSource, times(2)) |
| 297 | + .eventFilteringUpdateAndCacheResource(any(), any(UnaryOperator.class)); |
| 298 | + verify(resourceOp, times(1)).get(); |
177 | 299 | } |
178 | 300 | } |
0 commit comments