|
| 1 | +package io.javaoperatorsdk.operator; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.*; |
| 4 | + |
| 5 | +import io.fabric8.kubernetes.api.model.Namespace; |
| 6 | +import io.fabric8.kubernetes.api.model.NamespaceBuilder; |
| 7 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
| 8 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 9 | +import io.fabric8.kubernetes.client.KubernetesClientBuilder; |
| 10 | +import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil; |
| 11 | +import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; |
| 12 | +import io.javaoperatorsdk.operator.sample.workflowactivationcleanup.WorkflowActivationCleanupCustomResource; |
| 13 | +import io.javaoperatorsdk.operator.sample.workflowactivationcleanup.WorkflowActivationCleanupReconciler; |
| 14 | +import io.javaoperatorsdk.operator.sample.workflowactivationcleanup.WorkflowActivationCleanupSpec; |
| 15 | + |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.awaitility.Awaitility.await; |
| 18 | + |
| 19 | +public class WorkflowActivationCleanupIT { |
| 20 | + |
| 21 | + private final KubernetesClient client = new KubernetesClientBuilder().build(); |
| 22 | + private Operator operator; |
| 23 | + |
| 24 | + private String testNamespace; |
| 25 | + |
| 26 | + @BeforeEach |
| 27 | + void beforeEach(TestInfo testInfo) { |
| 28 | + LocallyRunOperatorExtension.applyCrd(WorkflowActivationCleanupCustomResource.class, |
| 29 | + client); |
| 30 | + |
| 31 | + testInfo.getTestMethod() |
| 32 | + .ifPresent(method -> testNamespace = KubernetesResourceUtil.sanitizeName(method.getName())); |
| 33 | + client.namespaces().resource(testNamespace(testNamespace)).create(); |
| 34 | + operator = new Operator(o -> o.withCloseClientOnStop(false)); |
| 35 | + operator.register(new WorkflowActivationCleanupReconciler(), |
| 36 | + o -> o.settingNamespaces(testNamespace)); |
| 37 | + } |
| 38 | + |
| 39 | + @AfterEach |
| 40 | + void stopOperator() { |
| 41 | + client.namespaces().withName(testNamespace).delete(); |
| 42 | + await().untilAsserted(() -> { |
| 43 | + var ns = client.namespaces().withName(testNamespace).get(); |
| 44 | + assertThat(ns).isNull(); |
| 45 | + }); |
| 46 | + operator.stop(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void testCleanupOnMarkedResourceOnOperatorStartup() { |
| 51 | + var resource = client.resource(testResourceWithFinalizer()).create(); |
| 52 | + client.resource(resource).delete(); |
| 53 | + operator.start(); |
| 54 | + |
| 55 | + await().untilAsserted(() -> { |
| 56 | + var res = client.resource(resource).get(); |
| 57 | + assertThat(res).isNull(); |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + private WorkflowActivationCleanupCustomResource testResourceWithFinalizer() { |
| 62 | + var resource = new WorkflowActivationCleanupCustomResource(); |
| 63 | + resource.setMetadata(new ObjectMetaBuilder() |
| 64 | + .withName("test1") |
| 65 | + .withFinalizers("workflowactivationcleanupcustomresources.sample.javaoperatorsdk/finalizer") |
| 66 | + .withNamespace(testNamespace) |
| 67 | + .build()); |
| 68 | + resource.setSpec(new WorkflowActivationCleanupSpec()); |
| 69 | + resource.getSpec().setValue("val1"); |
| 70 | + return resource; |
| 71 | + } |
| 72 | + |
| 73 | + private Namespace testNamespace(String name) { |
| 74 | + return new NamespaceBuilder().withMetadata(new ObjectMetaBuilder() |
| 75 | + .withName(name) |
| 76 | + .build()).build(); |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments