|
1 | 1 | package io.javaoperatorsdk.operator.sample;
|
2 | 2 |
|
3 | 3 | import io.fabric8.kubernetes.api.model.*;
|
4 |
| -import io.fabric8.kubernetes.client.*; |
5 | 4 | import io.fabric8.kubernetes.client.Config;
|
6 | 5 | import io.fabric8.kubernetes.client.ConfigBuilder;
|
7 |
| -import io.fabric8.kubernetes.client.dsl.MixedOperation; |
8 |
| -import io.fabric8.kubernetes.client.dsl.Resource; |
| 6 | +import io.fabric8.kubernetes.client.*; |
| 7 | +import io.fabric8.kubernetes.client.extended.run.RunConfigBuilder; |
9 | 8 | import io.javaoperatorsdk.operator.Operator;
|
10 | 9 | import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService;
|
11 | 10 | import org.junit.Test;
|
12 |
| - |
13 |
| -import java.io.IOException; |
14 |
| -import java.net.URI; |
15 |
| -import java.net.http.HttpClient; |
16 |
| -import java.net.http.HttpRequest; |
17 |
| -import java.net.http.HttpResponse; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
18 | 13 |
|
19 | 14 | import static java.util.concurrent.TimeUnit.MINUTES;
|
20 |
| -import static org.hamcrest.CoreMatchers.*; |
21 |
| - |
22 | 15 | import static org.awaitility.Awaitility.await;
|
| 16 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 17 | +import static org.hamcrest.CoreMatchers.is; |
23 | 18 | import static org.hamcrest.MatcherAssert.assertThat;
|
24 | 19 | import static org.hamcrest.Matchers.notNullValue;
|
25 | 20 |
|
26 | 21 | public class IntegrationTest {
|
27 | 22 |
|
28 | 23 | final static String TEST_NS = "tomcat-test";
|
29 | 24 |
|
| 25 | + final static Logger log = LoggerFactory.getLogger(IntegrationTest.class); |
| 26 | + |
30 | 27 | @Test
|
31 | 28 | public void test() {
|
32 | 29 | Config config = new ConfigBuilder().withNamespace(null).build();
|
33 | 30 | KubernetesClient client = new DefaultKubernetesClient(config);
|
34 |
| - Operator operator = new Operator(client, DefaultConfigurationService.instance()); |
35 | 31 |
|
| 32 | + Operator operator = new Operator(client, DefaultConfigurationService.instance()); |
36 | 33 | operator.register(new TomcatController(client));
|
37 | 34 | operator.register(new WebappController(client));
|
38 | 35 |
|
@@ -61,40 +58,49 @@ public void test() {
|
61 | 58 | Namespace testNs = new NamespaceBuilder().withMetadata(
|
62 | 59 | new ObjectMetaBuilder().withName(TEST_NS).build()).build();
|
63 | 60 |
|
64 |
| - // We perform a pre-run cleanup instead of a post-run cleanup. This is to help with debugging test results |
65 |
| - // when running against a persistent cluster. The test namespace would stay after the test run so we can |
66 |
| - // check what's there, but it would be cleaned up during the next test run. |
67 |
| - client.namespaces().delete(testNs); |
68 |
| - |
69 |
| - await().atMost(5, MINUTES).until(() -> client.namespaces().withName("tomcat-test").get() == null); |
| 61 | + if (testNs != null) { |
| 62 | + // We perform a pre-run cleanup instead of a post-run cleanup. This is to help with debugging test results |
| 63 | + // when running against a persistent cluster. The test namespace would stay after the test run so we can |
| 64 | + // check what's there, but it would be cleaned up during the next test run. |
| 65 | + log.info("Cleanup: deleting test namespace {}", TEST_NS); |
| 66 | + client.namespaces().delete(testNs); |
| 67 | + await().atMost(5, MINUTES).until(() -> client.namespaces().withName("tomcat-test").get() == null); |
| 68 | + } |
70 | 69 |
|
71 |
| - client.namespaces().createOrReplace(testNs); |
| 70 | + log.info("Creating test namespace {}", TEST_NS); |
| 71 | + client.namespaces().create(testNs); |
72 | 72 |
|
| 73 | + log.info("Creating test resources"); |
73 | 74 | tomcatClient.inNamespace(TEST_NS).create(tomcat);
|
74 | 75 | webappClient.inNamespace(TEST_NS).create(webapp1);
|
75 | 76 |
|
76 |
| - await().atMost(1, MINUTES).until(() -> client.services().inNamespace(TEST_NS).withName(tomcat.getMetadata().getName()).get() != null); |
77 |
| - LocalPortForward localPortForward = client.services().inNamespace(TEST_NS).withName(tomcat.getMetadata().getName()).portForward(80); |
78 |
| - |
| 77 | + log.info("Waiting 2 minutes for Tomcat and Webapp CR statuses to be updated"); |
79 | 78 | await().atMost(2, MINUTES).untilAsserted(() -> {
|
80 | 79 | Tomcat updatedTomcat = tomcatClient.inNamespace(TEST_NS).withName(tomcat.getMetadata().getName()).get();
|
81 | 80 | Webapp updatedWebapp = webappClient.inNamespace(TEST_NS).withName(webapp1.getMetadata().getName()).get();
|
82 | 81 | assertThat(updatedTomcat.getStatus(), is(notNullValue()));
|
83 | 82 | assertThat(updatedTomcat.getStatus().getReadyReplicas(), equalTo(3));
|
84 | 83 | assertThat(updatedWebapp.getStatus(), is(notNullValue()));
|
85 | 84 | assertThat(updatedWebapp.getStatus().getDeployedArtifact(), is(notNullValue()));
|
| 85 | + });
86 | 86 |
|
87 |
| - URI uri = URI.create("http://localhost:" + localPortForward.getLocalPort() + "/" + webapp1.getSpec().getContextPath()); |
| 87 | + String url = "http://" + tomcat.getMetadata().getName() + "/" + webapp1.getSpec().getContextPath() + "/"; |
| 88 | + log.info("Starting curl Pod and waiting 2 minutes for GET of {} to return 200", url); |
| 89 | + Pod curlPod = client.run().inNamespace(TEST_NS) |
| 90 | + .withRunConfig(new RunConfigBuilder() |
| 91 | + .withArgs("-s", "-o", "/dev/null", "-w", "%{http_code}", url) |
| 92 | + .withName("curl") |
| 93 | + .withImage("curlimages/curl:7.78.0") |
| 94 | + .withRestartPolicy("Never") |
| 95 | + .build()).done(); |
| 96 | + await().atMost(2, MINUTES).untilAsserted(() -> { |
88 | 97 | try {
|
89 |
| - HttpClient httpClient = HttpClient.newHttpClient(); |
90 |
| - HttpRequest request = HttpRequest.newBuilder() |
91 |
| - .uri(uri) |
92 |
| - .build(); |
93 |
| - int statusCode = httpClient.send(request, HttpResponse.BodyHandlers.ofString()).statusCode(); |
94 |
| - assertThat("Failed to access " + uri, statusCode, equalTo(200)); |
95 |
| - } catch (IOException ex) { |
96 |
| - throw new AssertionError("Failed to access " + uri, ex); |
| 98 | + String curlOutput = client.pods().inNamespace(TEST_NS).withName(curlPod.getMetadata().getName()).getLog(); |
| 99 | + assertThat(curlOutput, equalTo("200")); |
| 100 | + } catch (KubernetesClientException ex) { |
| 101 | + throw new AssertionError(ex); |
97 | 102 | }
|
98 | 103 | });
|
| 104 | + |
99 | 105 | }
|
100 | 106 | }
|
0 commit comments