8000 IntegrationTest uses a curl Pod to verify if Webapp is correctly depl… · java-operator-sdk/samples@b5ca270 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jan 4, 2022. It is now read-only.

Commit b5ca270

Browse files
committed
IntegrationTest uses a curl Pod to verify if Webapp is correctly deployed into Tomcat
1 parent c38821f commit b5ca270

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed
Lines changed: 37 additions & 31 deletions
+
});
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
11
package io.javaoperatorsdk.operator.sample;
22

33
import io.fabric8.kubernetes.api.model.*;
4-
import io.fabric8.kubernetes.client.*;
54
import io.fabric8.kubernetes.client.Config;
65
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;
98
import io.javaoperatorsdk.operator.Operator;
109
import io.javaoperatorsdk.operator.config.runtime.DefaultConfigurationService;
1110
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;
1813

1914
import static java.util.concurrent.TimeUnit.MINUTES;
20-
import static org.hamcrest.CoreMatchers.*;
21-
2215
import static org.awaitility.Awaitility.await;
16+
import static org.hamcrest.CoreMatchers.equalTo;
17+
import static org.hamcrest.CoreMatchers.is;
2318
import static org.hamcrest.MatcherAssert.assertThat;
2419
import static org.hamcrest.Matchers.notNullValue;
2520

2621
public class IntegrationTest {
2722

2823
final static String TEST_NS = "tomcat-test";
2924

25+
final static Logger log = LoggerFactory.getLogger(IntegrationTest.class);
26+
3027
@Test
3128
public void test() {
3229
Config config = new ConfigBuilder().withNamespace(null).build();
3330
KubernetesClient client = new DefaultKubernetesClient(config);
34-
Operator operator = new Operator(client, DefaultConfigurationService.instance());
3531

32+
Operator operator = new Operator(client, DefaultConfigurationService.instance());
3633
operator.register(new TomcatController(client));
3734
operator.register(new WebappController(client));
3835

@@ -61,40 +58,49 @@ public void test() {
6158
Namespace testNs = new NamespaceBuilder().withMetadata(
6259
new ObjectMetaBuilder().withName(TEST_NS).build()).build();
6360

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+
}
7069

71-
client.namespaces().createOrReplace(testNs);
70+
log.info("Creating test namespace {}", TEST_NS);
71+
client.namespaces().create(testNs);
7272

73+
log.info("Creating test resources");
7374
tomcatClient.inNamespace(TEST_NS).create(tomcat);
7475
webappClient.inNamespace(TEST_NS).create(webapp1);
7576

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");
7978
await().atMost(2, MINUTES).untilAsserted(() -> {
8079
Tomcat updatedTomcat = tomcatClient.inNamespace(TEST_NS).withName(tomcat.getMetadata().getName()).get();
8180
Webapp updatedWebapp = webappClient.inNamespace(TEST_NS).withName(webapp1.getMetadata().getName()).get();
8281
assertThat(updatedTomcat.getStatus(), is(notNullValue()));
8382
assertThat(updatedTomcat.getStatus().getReadyReplicas(), equalTo(3));
8483
assertThat(updatedWebapp.getStatus(), is(notNullValue()));
8584
assertThat(updatedWebapp.getStatus().getDeployedArtifact(), is(notNullValue()));
85
8686

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(() -> {
8897
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);
97102
}
98103
});
104+
99105
}
100106
}

0 commit comments

Comments
 (0)
0