diff --git a/operator-framework-junit5/src/main/java/io/javaoperatorsdk/operator/junit/InClusterCurl.java b/operator-framework-junit5/src/main/java/io/javaoperatorsdk/operator/junit/InClusterCurl.java new file mode 100644 index 0000000000..927723209c --- /dev/null +++ b/operator-framework-junit5/src/main/java/io/javaoperatorsdk/operator/junit/InClusterCurl.java @@ -0,0 +1,54 @@ +package io.javaoperatorsdk.operator.junit; + +import java.util.UUID; + +import io.fabric8.kubernetes.api.model.Pod; +import io.fabric8.kubernetes.client.KubernetesClient; +import io.fabric8.kubernetes.client.extended.run.RunConfigBuilder; +import io.fabric8.kubernetes.client.utils.KubernetesResourceUtil; + +import static java.util.concurrent.TimeUnit.MINUTES; +import static org.awaitility.Awaitility.await; + +public class InClusterCurl { + + private final KubernetesClient client; + private final String namespace; + + public InClusterCurl(KubernetesClient client, String namespace) { + this.client = client; + this.namespace = namespace; + } + + public String checkUrl(String url) { + String podName = KubernetesResourceUtil.sanitizeName("curl-" + UUID.randomUUID()); + try { + Pod curlPod = client.run().inNamespace(namespace) + .withRunConfig(new RunConfigBuilder() + .withArgs("-s", "-o", "/dev/null", "-w", "%{http_code}", url) + .withName(podName) + .withImage("curlimages/curl:7.78.0") + .withRestartPolicy("Never") + .build()) + .done(); + await("wait-for-curl-pod-run").atMost(2, MINUTES) + .until(() -> { + String phase = + client.pods().inNamespace(namespace).withName(podName).get() + .getStatus().getPhase(); + return phase.equals("Succeeded") || phase.equals("Failed"); + }); + + String curlOutput = + client.pods().inNamespace(namespace) + .withName(curlPod.getMetadata().getName()).getLog(); + + return curlOutput; + } finally { + client.pods().inNamespace(namespace).withName(podName).delete(); + await("wait-for-curl-pod-stop").atMost(1, MINUTES) + .until(() -> client.pods().inNamespace(namespace).withName(podName) + .get() == null); + } + } +} diff --git a/sample-operators/tomcat-operator/src/test/java/io/javaoperatorsdk/operator/sample/TomcatOperatorE2E.java b/sample-operators/tomcat-operator/src/test/java/io/javaoperatorsdk/operator/sample/TomcatOperatorE2E.java index 042f4973cf..3fecdb710e 100644 --- a/sample-operators/tomcat-operator/src/test/java/io/javaoperatorsdk/operator/sample/TomcatOperatorE2E.java +++ b/sample-operators/tomcat-operator/src/test/java/io/javaoperatorsdk/operator/sample/TomcatOperatorE2E.java @@ -10,10 +10,10 @@ import io.fabric8.kubernetes.api.model.*; import io.fabric8.kubernetes.client.*; -import io.fabric8.kubernetes.client.extended.run.RunConfigBuilder; import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService; import io.javaoperatorsdk.operator.junit.AbstractOperatorExtension; import io.javaoperatorsdk.operator.junit.E2EOperatorExtension; +import io.javaoperatorsdk.operator.junit.InClusterCurl; import io.javaoperatorsdk.operator.junit.OperatorExtension; import static java.util.concurrent.TimeUnit.MINUTES; @@ -107,42 +107,15 @@ public void test() { String url = "http://" + tomcat.getMetadata().getName() + "/" + webapp1.getSpec().getContextPath() + "/"; + var inClusterCurl = new InClusterCurl(client, operator.getNamespace()); log.info("Starting curl Pod and waiting 5 minutes for GET of {} to return 200", url); await("wait-for-webapp").atMost(6, MINUTES).untilAsserted(() -> { try { - - log.info("Starting curl Pod to test if webapp was deployed correctly"); - Pod curlPod = client.run().inNamespace(operator.getNamespace()) - .withRunConfig(new RunConfigBuilder() - .withArgs("-s", "-o", "/dev/null", "-w", "%{http_code}", url) - .withName("curl") - .withImage("curlimages/curl:7.78.0") - .withRestartPolicy("Never") - .build()) - .done(); - log.info("Waiting for curl Pod to finish running"); - await("wait-for-curl-pod-run").atMost(2, MINUTES) - .until(() -> { - String phase = - client.pods().inNamespace(operator.getNamespace()).withName("curl").get() - .getStatus().getPhase(); - return phase.equals("Succeeded") || phase.equals("Failed"); - }); - - String curlOutput = - client.pods().inNamespace(operator.getNamespace()) - .withName(curlPod.getMetadata().getName()).getLog(); - log.info("Output from curl: '{}'", curlOutput); + var curlOutput = inClusterCurl.checkUrl(url); assertThat(curlOutput, equalTo("200")); } catch (KubernetesClientException ex) { throw new AssertionError(ex); - } finally { - log.info("Deleting curl Pod"); - client.pods().inNamespace(operator.getNamespace()).withName("curl").delete(); - await("wait-for-curl-pod-stop").atMost(1, MINUTES) - .until(() -> client.pods().inNamespace(operator.getNamespace()).withName("curl") - .get() == null); } }); }