Skip to content

Commit

Permalink
feat: Improved webapp zero-config tests
Browse files Browse the repository at this point in the history
- Test is compliant for new updated base images (eclipse-jkube/jkube#206)
- Service edited and switched to NodePort to verify port response
- Added k8s:log goal verifications
  • Loading branch information
manusa authored and anandrkskd committed May 28, 2020
1 parent 2080e75 commit badd522
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public ServiceAssertion assertExposed() {
return this;
}

public ServiceAssertion assertIsClusterIp() {
assertThat(getKubernetesResource().getSpec().getType(), equalTo("ClusterIP"));
return this;
}

public ServiceAssertion assertIsNodePort() {
assertThat(getKubernetesResource().getSpec().getType(), equalTo("NodePort"));
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.jkube.integrationtests.webapp.zeroconfig;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.Service;
import org.eclipse.jkube.integrationtests.JKubeCase;
import org.eclipse.jkube.integrationtests.maven.BaseMavenCase;

Expand All @@ -22,7 +23,9 @@
import static org.eclipse.jkube.integrationtests.assertions.PodAssertion.assertPod;
import static org.eclipse.jkube.integrationtests.assertions.PodAssertion.awaitPod;
import static org.eclipse.jkube.integrationtests.assertions.ServiceAssertion.awaitService;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.stringContainsInOrder;

abstract class ZeroConfig extends BaseMavenCase implements JKubeCase {

Expand All @@ -48,4 +51,19 @@ final Pod assertThatShouldApplyResources() throws InterruptedException, IOExcept
return pod;
}

final Service serviceSpecTypeToNodePort() throws InterruptedException, IOException {
final Pod pod = awaitPod(this).getKubernetesResource();
return getKubernetesClient().services()
.inNamespace(pod.getMetadata().getNamespace())
.withName(getApplication())
.edit().editSpec().withType("NodePort").endSpec().done();
}

final void assertLog(String log) {
assertThat(log,
stringContainsInOrder(
"Deploying web application archive [/usr/local/tomcat/webapps/ROOT.war]",
"Deployment of web application archive [/usr/local/tomcat/webapps/ROOT.war] has finished",
"org.apache.catalina.startup.Catalina.start Server startup in", "seconds"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
package org.eclipse.jkube.integrationtests.webapp.zeroconfig;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.PrintStreamHandler;
import org.eclipse.jkube.integrationtests.maven.MavenUtils;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -28,18 +31,27 @@
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.parallel.ResourceLock;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Properties;

import static org.eclipse.jkube.integrationtests.Locks.CLUSTER_RESOURCE_INTENSIVE;
import static org.eclipse.jkube.integrationtests.Tags.KUBERNETES;
import static org.eclipse.jkube.integrationtests.assertions.DeploymentAssertion.assertDeploymentExists;
import static org.eclipse.jkube.integrationtests.assertions.DeploymentAssertion.awaitDeployment;
import static org.eclipse.jkube.integrationtests.assertions.DockerAssertion.assertImageWasRecentlyBuilt;
import static org.eclipse.jkube.integrationtests.assertions.ServiceAssertion.awaitService;
import static org.eclipse.jkube.integrationtests.assertions.YamlAssertion.yaml;
import static org.eclipse.jkube.integrationtests.docker.DockerUtils.listImageFiles;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.hasSize;
Expand Down Expand Up @@ -76,7 +88,10 @@ void k8sBuild() throws Exception {
final InvocationResult invocationResult = maven("k8s:build");
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
assertImageWasRecentlyBuilt("integration-tests", "webapp-zero-config");
assertImageWasRecentlyBuilt("integration-tests", getApplication());
final List<String> imageFiles = listImageFiles(String.format("%s/%s", "integration-tests", getApplication()),
"/deployments");
assertThat(imageFiles, hasItem("/deployments/ROOT.war"));
}

@Test
Expand Down Expand Up @@ -112,16 +127,47 @@ void k8sApply() throws Exception {
.assertContainers(hasItems(allOf(
hasProperty("image", equalTo("integration-tests/webapp-zero-config:latest")),
hasProperty("name", equalTo("webapp")),
hasProperty("ports", hasSize(2)),
hasProperty("ports", hasSize(1)),
hasProperty("ports", hasItems(allOf(
hasProperty("name", equalTo("http")),
hasProperty("containerPort", equalTo(8080))
)))
)));
awaitService(this, pod.getMetadata().getNamespace())
.assertIsClusterIp();
}

@Test
@Order(4)
@DisplayName("Service as NodePort response should return String")
void testNodePortResponse() throws Exception {
// Given
final Service service = serviceSpecTypeToNodePort();
// Then
awaitService(this, service.getMetadata().getNamespace())
.assertNodePortResponse("http", containsString("Eclipse JKube rocks!!"));
}

@Test
@Order(5)
@DisplayName("k8s:log, should retrieve log")
void k8sLog() throws Exception {
// Given
final Properties properties = new Properties();
properties.setProperty("jkube.log.follow", "false");
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final MavenUtils.InvocationRequestCustomizer irc = invocationRequest -> {
invocationRequest.setOutputHandler(new PrintStreamHandler(new PrintStream(baos), true));
};
// When
final InvocationResult invocationResult = maven("k8s:log", properties, irc);
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
assertLog(baos.toString(StandardCharsets.UTF_8));
}

@Test
@Order(6)
@DisplayName("k8s:undeploy, should delete all applied resources")
void k8sUndeploy() throws Exception {
// When
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
*/
package org.eclipse.jkube.integrationtests.webapp.zeroconfig;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.openshift.api.model.ImageStream;
import io.fabric8.openshift.client.OpenShiftClient;
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.PrintStreamHandler;
import org.eclipse.jkube.integrationtests.maven.MavenUtils;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -28,19 +33,23 @@
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.parallel.ResourceLock;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

import static org.eclipse.jkube.integrationtests.Hacks.hackToPreventNullPointerInRegistryServiceCreateAuthConfig;
import static org.eclipse.jkube.integrationtests.Locks.CLUSTER_RESOURCE_INTENSIVE;
import static org.eclipse.jkube.integrationtests.OpenShift.cleanUpCluster;
import static org.eclipse.jkube.integrationtests.Tags.OPEN_SHIFT;
import static org.eclipse.jkube.integrationtests.assertions.DockerAssertion.assertImageWasRecentlyBuilt;
import static org.eclipse.jkube.integrationtests.assertions.ServiceAssertion.awaitService;
import static org.eclipse.jkube.integrationtests.assertions.YamlAssertion.yaml;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.jupiter.api.parallel.ResourceAccessMode.READ_WRITE;

@Tag(OPEN_SHIFT)
Expand All @@ -67,28 +76,23 @@ public KubernetesClient getKubernetesClient() {

@Test
@Order(1)
@DisplayName("oc:build, in docker mode, should create image")
@DisplayName("oc:build, should create image")
void ocBuild() throws Exception {
// Given
hackToPreventNullPointerInRegistryServiceCreateAuthConfig("fabric8/tomcat-9:1.2.1");
final Properties properties = new Properties();
properties.setProperty("jkube.mode", "kubernetes"); // S2I doesn't support webapp yet
// When
final InvocationResult invocationResult = maven("oc:build", properties);
final InvocationResult invocationResult = maven("oc:build");
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
assertImageWasRecentlyBuilt("integration-tests", "webapp-zero-config");
final ImageStream is = oc.imageStreams().withName(getApplication()).get();
assertThat(is, notNullValue());
assertThat(is.getStatus().getTags().iterator().next().getTag(), equalTo("latest"));
}

@Test
@Order(2)
@DisplayName("oc:resource, should create manifests")
void ocResource() throws Exception {
// Given
final Properties properties = new Properties();
properties.setProperty("jkube.mode", "kubernetes"); // S2I doesn't support webapp yet
// When
final InvocationResult invocationResult = maven("oc:resource", properties);
final InvocationResult invocationResult = maven("oc:resource");
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
final File metaInfDirectory = new File(
Expand All @@ -109,11 +113,42 @@ void ocApply() throws Exception {
final InvocationResult invocationResult = maven("oc:apply");
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
assertThatShouldApplyResources();
final Pod pod = assertThatShouldApplyResources();
awaitService(this, pod.getMetadata().getNamespace())
.assertIsClusterIp();
}

@Test
@Order(4)
@DisplayName("Service as NodePort response should return String")
void testNodePortResponse() throws Exception {
// Given
final Service service = serviceSpecTypeToNodePort();
// Then
awaitService(this, service.getMetadata().getNamespace())
.assertNodePortResponse("http", containsString("Eclipse JKube rocks!!"));
}

@Test
@Order(5)
@DisplayName("oc:log, should retrieve log")
void ocLog() throws Exception {
// Given
final Properties properties = new Properties();
properties.setProperty("jkube.log.follow", "false");
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final MavenUtils.InvocationRequestCustomizer irc = invocationRequest -> {
invocationRequest.setOutputHandler(new PrintStreamHandler(new PrintStream(baos), true));
};
// When
final InvocationResult invocationResult = maven("oc:log", properties, irc);
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
assertLog(baos.toString(StandardCharsets.UTF_8));
}

@Test
@Order(6)
@DisplayName("oc:undeploy, should delete all applied resources")
void ocUndeploy() throws Exception {
// When
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<apache.maven.maven-war-plugin.version>2.6</apache.maven.maven-war-plugin.version>
<apache.maven-failsafe-plugin.version>3.0.0-M3</apache.maven-failsafe-plugin.version>
<apache.maven.maven-invoker.version>3.0.1</apache.maven.maven-invoker.version>
<fabric8.kubernetes-client.version>4.9.0</fabric8.kubernetes-client.version>
<fabric8.kubernetes-client.version>4.9.2</fabric8.kubernetes-client.version>
<jkube.version>1.0.0-SNAPSHOT</jkube.version>
<com.fasterxml.jackson.core.version>2.10.0</com.fasterxml.jackson.core.version>
<com.fasterxml.jackson.dataformat.yaml.version>2.10.0</com.fasterxml.jackson.dataformat.yaml.version>
Expand Down

0 comments on commit badd522

Please sign in to comment.