|
| 1 | +package com.github.containersolutions.operator; |
| 2 | + |
| 3 | +import com.github.containersolutions.operator.sample.*; |
| 4 | +import io.fabric8.kubernetes.api.model.ConfigMap; |
| 5 | +import io.fabric8.kubernetes.api.model.NamespaceBuilder; |
| 6 | +import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; |
| 7 | +import io.fabric8.kubernetes.api.model.apiextensions.CustomResourceDefinition; |
| 8 | +import io.fabric8.kubernetes.client.DefaultKubernetesClient; |
| 9 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 10 | +import io.fabric8.kubernetes.client.dsl.MixedOperation; |
| 11 | +import io.fabric8.kubernetes.client.dsl.Resource; |
| 12 | +import io.fabric8.kubernetes.client.utils.Serialization; |
| 13 | +import io.fabric8.kubernetes.internal.KubernetesDeserializer; |
| 14 | +import org.junit.jupiter.api.*; |
| 15 | +import org.mockito.internal.matchers.Any; |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | + |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | +import static org.awaitility.Awaitility.await; |
| 25 | + |
| 26 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 27 | +public class IntegrationTest { |
| 28 | + |
| 29 | + public static final String TEST_NAMESPACE = "java-operator-sdk-int-test"; |
| 30 | + |
| 31 | + public final KubernetesClient k8sClient = new DefaultKubernetesClient(); |
| 32 | + public MixedOperation<TestCustomResource, TestCustomResourceList, TestCustomResourceDoneable, Resource<TestCustomResource, TestCustomResourceDoneable>> crOperations; |
| 33 | + |
| 34 | + private final Logger log = LoggerFactory.getLogger(getClass()); |
| 35 | + |
| 36 | + private Operator operator; |
| 37 | + |
| 38 | + @BeforeAll |
| 39 | + public void setup() { |
| 40 | + log.info("Running integration test in namespace " + TEST_NAMESPACE); |
| 41 | + |
| 42 | + CustomResourceDefinition crd = loadYaml(CustomResourceDefinition.class, "test-crd.yaml"); |
| 43 | + k8sClient.customResourceDefinitions().createOrReplace(crd); |
| 44 | + |
| 45 | + if (k8sClient.namespaces().withName(TEST_NAMESPACE).get() == null) { |
| 46 | + k8sClient.namespaces().create(new NamespaceBuilder() |
| 47 | + .withMetadata(new ObjectMetaBuilder().withName(TEST_NAMESPACE).build()).build()); |
| 48 | + } |
| 49 | + |
| 50 | + operator = new Operator(k8sClient); |
| 51 | + operator.registerController(new TestCustomResourceController()); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + public void cleanup() { |
| 57 | + |
| 58 | + CustomResourceDefinition crd = loadYaml(CustomResourceDefinition.class, "test-crd.yaml"); |
| 59 | + k8sClient.customResourceDefinitions().createOrReplace(crd); |
| 60 | + KubernetesDeserializer.registerCustomKind(crd.getApiVersion(), crd.getKind(), TestCustomResource.class); |
| 61 | + |
| 62 | + k8sClient.configMaps().inNamespace(TEST_NAMESPACE) |
| 63 | + .withLabel("managedBy", TestCustomResourceController.class.getSimpleName()) |
| 64 | + .delete(); |
| 65 | + |
| 66 | + crOperations = k8sClient.customResources(crd, TestCustomResource.class, TestCustomResourceList.class, TestCustomResourceDoneable.class); |
| 67 | + crOperations.inNamespace(TEST_NAMESPACE).delete(crOperations.list().getItems()); |
| 68 | + //we depend on the actual operator from the startup to handle the finalizers and clean up |
| 69 | + //resources from previous test runs |
| 70 | + |
| 71 | + await("all resources cleaned up").atMost(60, TimeUnit.SECONDS) |
| 72 | + .untilAsserted(() -> { |
| 73 | + assertThat(crOperations.inNamespace(TEST_NAMESPACE).list().getItems()).isEmpty(); |
| 74 | + assertThat(k8sClient.configMaps().inNamespace(TEST_NAMESPACE).list().getItems()).isEmpty(); |
| 75 | + }); |
| 76 | + |
| 77 | + log.info("Cleaned up namespace " + TEST_NAMESPACE); |
| 78 | + } |
| 79 | + |
| 80 | + @AfterAll |
| 81 | + public void teardown() { |
| 82 | +// CustomResourceDefinition crd = loadYaml(CustomResourceDefinition.class, "test-crd.yaml"); |
| 83 | +// k8sClient.customResourceDefinitions().delete(crd); |
| 84 | + operator.stop(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void configMapGetsCreatedForTestCustomResource() { |
| 89 | + TestCustomResource resource = new TestCustomResource(); |
| 90 | + resource.setMetadata(new ObjectMetaBuilder() |
| 91 | + .withName("test-custom-resource") |
| 92 | + .withNamespace(TEST_NAMESPACE) |
| 93 | + .build()); |
| 94 | + resource.setKind("CustomService"); |
| 95 | + resource.setSpec(new TestCustomResourceSpec()); |
| 96 | + resource.getSpec().setConfigMapName("test-config-map"); |
| 97 | + resource.getSpec().setKey("test-key"); |
| 98 | + resource.getSpec().setValue("test-value"); |
| 99 | + crOperations.inNamespace(TEST_NAMESPACE).create(resource); |
| 100 | + |
| 101 | + await("configmap created").atMost(5, TimeUnit.SECONDS) |
| 102 | + .untilAsserted(() -> { |
| 103 | + ConfigMap configMap = k8sClient.configMaps().inNamespace(TEST_NAMESPACE) |
| 104 | + .withName("test-config-map").get(); |
| 105 | + assertThat(configMap).isNotNull(); |
| 106 | + assertThat(configMap.getData().get("test-key")).isEqualTo("test-value"); |
| 107 | + }); |
| 108 | + await("cr status updated").atMost(5, TimeUnit.SECONDS) |
| 109 | + .untilAsserted(() -> { |
| 110 | + TestCustomResource cr = crOperations.inNamespace(TEST_NAMESPACE).withName("test-custom-resource").get(); |
| 111 | + assertThat(cr).isNotNull(); |
| 112 | + assertThat(cr.getStatus()).isNotNull(); |
| 113 | + assertThat(cr.getStatus().getConfigMapStatus()).isEqualTo("ConfigMap Ready"); |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + private <T> T loadYaml(Class<T> clazz, String yaml) { |
| 118 | + try (InputStream is = getClass().getResourceAsStream(yaml)) { |
| 119 | + return Serialization.unmarshal(is, clazz); |
| 120 | + } catch (IOException ex) { |
| 121 | + throw new IllegalStateException("Cannot find yaml on classpath: " + yaml); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments