Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: E2E tests for webapp-jetty #44

Merged
merged 1 commit into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.integrationtests.webapp.jetty;

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

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.containsString;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.stringContainsInOrder;

abstract class Jetty extends BaseMavenCase implements JKubeCase {

static final String PROJECT_JETTY = "projects-to-be-tested/webapp/jetty";

@Override
public String getProject() {
return PROJECT_JETTY;
}

@Override
public String getApplication() {
return "webapp-jetty";
}

final Pod assertThatShouldApplyResources() throws Exception {
final Pod pod = awaitPod(this).getKubernetesResource();
assertPod(pod).apply(this).logContains("Server:main: Started", 10);
awaitService(this, pod.getMetadata().getNamespace())
.assertExposed()
.assertPorts(hasSize(1))
.assertPort("http", 8080, true)
.assertNodePortResponse("http", containsString("<h2>Eclipse JKube on Jetty rocks!</h2>"));
return pod;
}

final void assertLog(String log) {
assertThat(log,
stringContainsInOrder(
"/var/lib/jetty/webapps",
"Deployment monitor [file:///var/lib/jetty/webapps/]",
"Server:main: Started"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.integrationtests.webapp.jetty;

import io.fabric8.kubernetes.api.model.Pod;
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;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
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.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;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.parallel.ResourceAccessMode.READ_WRITE;

@Tag(KUBERNETES)
@TestMethodOrder(OrderAnnotation.class)
class JettyK8sITCase extends Jetty {

private KubernetesClient k;

@BeforeEach
void setUp() {
k = new DefaultKubernetesClient();
}

@AfterEach
void tearDown() {
k.close();
k = null;
}

@Override
public KubernetesClient getKubernetesClient() {
return k;
}

@Test
@Order(1)
@DisplayName("k8s:build, should create image")
void k8sBuild() throws Exception {
// When
final InvocationResult invocationResult = maven("k8s:build");
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
assertImageWasRecentlyBuilt("integration-tests", getApplication());
final List<String> imageFiles = listImageFiles(String.format("%s/%s", "integration-tests", getApplication()),
"/deployments");
assertThat(imageFiles, hasItem("/deployments/ROOT.war"));
}

@Test
@Order(2)
@DisplayName("k8s:resource, should create manifests")
void k8sResource() throws Exception {
// When
final InvocationResult invocationResult = maven("k8s:resource");
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
final File metaInfDirectory = new File(
String.format("../%s/target/classes/META-INF", getProject()));
assertThat(metaInfDirectory.exists(), equalTo(true));
assertListResource(new File(metaInfDirectory, "jkube/kubernetes.yml"));
assertThat(new File(metaInfDirectory, "jkube/kubernetes/webapp-jetty-deployment.yml"), yaml(not(anEmptyMap())));
assertThat(new File(metaInfDirectory, "jkube/kubernetes/webapp-jetty-service.yml"), yaml(not(anEmptyMap())));
}

@Test
@Order(3)
@ResourceLock(value = CLUSTER_RESOURCE_INTENSIVE, mode = READ_WRITE)
@DisplayName("k8s:apply, should deploy pod and service")
@SuppressWarnings("unchecked")
void k8sApply() throws Exception {
// When
final InvocationResult invocationResult = maven("k8s:apply");
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
final Pod pod = assertThatShouldApplyResources();
awaitDeployment(this, pod.getMetadata().getNamespace())
.assertReplicas(equalTo(1))
.assertContainers(hasSize(1))
.assertContainers(hasItems(allOf(
hasProperty("image", equalTo("integration-tests/webapp-jetty:latest")),
hasProperty("name", equalTo("webapp")),
hasProperty("ports", hasSize(1)),
hasProperty("ports", hasItems(allOf(
hasProperty("name", equalTo("http")),
hasProperty("containerPort", equalTo(8080))
)))
)));
}


@Test
@Order(4)
@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(5)
@DisplayName("k8s:undeploy, should delete all applied resources")
void k8sUndeploy() throws Exception {
// When
final InvocationResult invocationResult = maven("k8s:undeploy");
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
assertThatShouldDeleteAllAppliedResources(this);
assertDeploymentExists(this, equalTo(false));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.integrationtests.webapp.jetty;

import io.fabric8.kubernetes.api.model.Pod;
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;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
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.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.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.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)
@TestMethodOrder(OrderAnnotation.class)
class JettyOcITCase extends Jetty {

private OpenShiftClient oc;

@BeforeEach
void setUp() {
oc = new DefaultKubernetesClient().adapt(OpenShiftClient.class);
}

@AfterEach
void tearDown() {
oc.close();
oc = null;
}

@Override
public KubernetesClient getKubernetesClient() {
return oc;
}

@Test
@Order(1)
@DisplayName("oc:build, should create image")
void ocBuild() throws Exception {
// When
final InvocationResult invocationResult = maven("oc:build");
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
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 {
// When
final InvocationResult invocationResult = maven("oc:resource");
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
final File metaInfDirectory = new File(
String.format("../%s/target/classes/META-INF", getProject()));
assertThat(metaInfDirectory.exists(), equalTo(true));
assertListResource(new File(metaInfDirectory, "jkube/openshift.yml"));
assertThat(new File(metaInfDirectory, "jkube/openshift/webapp-jetty-deploymentconfig.yml"), yaml(not(anEmptyMap())));
assertThat(new File(metaInfDirectory, "jkube/openshift/webapp-jetty-route.yml"), yaml(not(anEmptyMap())));
assertThat(new File(metaInfDirectory, "jkube/openshift/webapp-jetty-service.yml"), yaml(not(anEmptyMap())));
}

@Test
@Order(3)
@ResourceLock(value = CLUSTER_RESOURCE_INTENSIVE, mode = READ_WRITE)
@DisplayName("oc:apply, should deploy pod and service")
void ocApply() throws Exception {
// When
final InvocationResult invocationResult = maven("oc:apply");
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
assertThatShouldApplyResources();
}

@Test
@Order(4)
@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(5)
@DisplayName("oc:undeploy, should delete all applied resources")
void ocUndeploy() throws Exception {
// When
final InvocationResult invocationResult = maven("oc:undeploy");
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
assertThatShouldDeleteAllAppliedResources(this);
cleanUpCluster(oc, this);
}

}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
<profile>
<id>webapp</id>
<modules>
<module>projects-to-be-tested/webapp/jetty</module>
<module>projects-to-be-tested/webapp/zero-config</module>
</modules>
<activation>
Expand Down
Loading