|
| 1 | +/* |
| 2 | + * Copyright Kroxylicious Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +package io.kroxylicious.kubernetes.operator; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.util.stream.Stream; |
| 14 | + |
| 15 | +import org.assertj.core.api.Assertions; |
| 16 | +import org.junit.jupiter.api.AfterAll; |
| 17 | +import org.junit.jupiter.api.BeforeAll; |
| 18 | +import org.junit.jupiter.api.condition.EnabledIf; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.Arguments; |
| 21 | +import org.junit.jupiter.params.provider.MethodSource; |
| 22 | + |
| 23 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
| 26 | + |
| 27 | +import io.fabric8.kubernetes.api.model.HasMetadata; |
| 28 | +import io.fabric8.kubernetes.api.model.Namespace; |
| 29 | +import io.fabric8.kubernetes.api.model.NamespaceBuilder; |
| 30 | +import io.fabric8.kubernetes.api.model.Status; |
| 31 | +import io.fabric8.kubernetes.client.KubernetesClient; |
| 32 | +import io.fabric8.kubernetes.client.KubernetesClientException; |
| 33 | +import io.fabric8.kubernetes.client.dsl.NamespaceableResource; |
| 34 | +import io.fabric8.kubernetes.client.dsl.NonDeletingOperation; |
| 35 | +import io.javaoperatorsdk.operator.junit.LocallyRunOperatorExtension; |
| 36 | + |
| 37 | +import io.kroxylicious.kubernetes.api.v1alpha1.KafkaProxy; |
| 38 | +import io.kroxylicious.kubernetes.api.v1alpha1.KafkaProxyIngress; |
| 39 | +import io.kroxylicious.kubernetes.api.v1alpha1.KafkaService; |
| 40 | +import io.kroxylicious.kubernetes.api.v1alpha1.VirtualKafkaCluster; |
| 41 | +import io.kroxylicious.kubernetes.filter.api.v1alpha1.KafkaProtocolFilter; |
| 42 | + |
| 43 | +import static org.assertj.core.api.Assertions.assertThat; |
| 44 | + |
| 45 | +@EnabledIf(value = "io.kroxylicious.kubernetes.operator.OperatorTestUtils#isKubeClientAvailable", disabledReason = "no viable kube client available") |
| 46 | +class CustomResourceValidationIT { |
| 47 | + |
| 48 | + public static final Namespace NAMESPACE = new NamespaceBuilder().withNewMetadata().withName("proxy-ns").endMetadata().build(); |
| 49 | + public static final ObjectMapper YAML_MAPPER = new ObjectMapper(new YAMLFactory()); |
| 50 | + |
| 51 | + @BeforeAll |
| 52 | + static void beforeAll() { |
| 53 | + KubernetesClient client = OperatorTestUtils.kubeClient(); |
| 54 | + LocallyRunOperatorExtension.applyCrd(KafkaProtocolFilter.class, client); |
| 55 | + LocallyRunOperatorExtension.applyCrd(KafkaProxy.class, client); |
| 56 | + LocallyRunOperatorExtension.applyCrd(VirtualKafkaCluster.class, client); |
| 57 | + LocallyRunOperatorExtension.applyCrd(KafkaService.class, client); |
| 58 | + LocallyRunOperatorExtension.applyCrd(KafkaProxyIngress.class, client); |
| 59 | + client.namespaces().resource(NAMESPACE).createOr(NonDeletingOperation::update); |
| 60 | + } |
| 61 | + |
| 62 | + @AfterAll |
| 63 | + static void afterAll() { |
| 64 | + try (KubernetesClient kubernetesClient = OperatorTestUtils.kubeClient()) { |
| 65 | + kubernetesClient.namespaces().resource(NAMESPACE).delete(); |
| 66 | + kubernetesClient.resources(KafkaProtocolFilter.class).delete(); |
| 67 | + kubernetesClient.resources(KafkaProxyIngress.class).delete(); |
| 68 | + kubernetesClient.resources(KafkaProxy.class).delete(); |
| 69 | + kubernetesClient.resources(VirtualKafkaCluster.class).delete(); |
| 70 | + kubernetesClient.resources(KafkaService.class).delete(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public static Stream<Path> testDerivedResourceInputsValid() { |
| 75 | + return TestFiles.recursiveFilesInDirectoryForTest(DerivedResourcesTest.class, "in-*.yaml").stream(); |
| 76 | + } |
| 77 | + |
| 78 | + public static Stream<Arguments> testResourceInvalid() { |
| 79 | + return TestFiles.recursiveFilesInDirectoryForTest(CustomResourceValidationIT.class, "invalid-*.yaml").stream().map(p -> { |
| 80 | + try { |
| 81 | + return Arguments.argumentSet(p.toString(), YAML_MAPPER.readValue(p.toFile(), InvalidResource.class)); |
| 82 | + } |
| 83 | + catch (IOException e) { |
| 84 | + throw new RuntimeException(e); |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + @MethodSource |
| 90 | + @ParameterizedTest |
| 91 | + void testDerivedResourceInputsValid(Path validYaml) { |
| 92 | + try (InputStream is = Files.newInputStream(validYaml)) { |
| 93 | + NamespaceableResource<HasMetadata> resource = OperatorTestUtils.kubeClient().resource(is); |
| 94 | + Assertions.assertThatCode(resource::create).doesNotThrowAnyException(); |
| 95 | + Assertions.assertThatCode(resource::delete).doesNotThrowAnyException(); |
| 96 | + } |
| 97 | + catch (IOException e) { |
| 98 | + throw new RuntimeException(e); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + record InvalidResource(String expectFailureMessageToContain, Object resource) { |
| 103 | + String resourceAsString() { |
| 104 | + try { |
| 105 | + return YAML_MAPPER.writeValueAsString(resource); |
| 106 | + } |
| 107 | + catch (JsonProcessingException e) { |
| 108 | + throw new RuntimeException(e); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @MethodSource |
| 114 | + @ParameterizedTest |
| 115 | + void testResourceInvalid(InvalidResource invalidYaml) { |
| 116 | + NamespaceableResource<HasMetadata> resource = OperatorTestUtils.kubeClient().resource(invalidYaml.resourceAsString()); |
| 117 | + try { |
| 118 | + Assertions.assertThatThrownBy(resource::create).isInstanceOfSatisfying(KubernetesClientException.class, e -> { |
| 119 | + Status status = e.getStatus(); |
| 120 | + assertThat(status).isNotNull(); |
| 121 | + assertThat(status.getCode()).isEqualTo(422); |
| 122 | + assertThat(status.getMessage()).contains(invalidYaml.expectFailureMessageToContain); |
| 123 | + }); |
| 124 | + } |
| 125 | + finally { |
| 126 | + try { |
| 127 | + resource.delete(); |
| 128 | + } |
| 129 | + catch (KubernetesClientException e) { |
| 130 | + // ignored, redundantly deleting in case the resource was accidentally valid |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments