Skip to content

Commit

Permalink
Added Integration test for simple mode Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanKanojia authored Jun 11, 2020
1 parent 87f7857 commit 5dfc170
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
fail-fast: false
matrix:
kubernetes: [v1.12.0,v1.18.3]
suite: ['quarkus','quarkus-native','springboot','webapp','other']
suite: ['quarkus','quarkus-native','springboot','webapp','other', 'dockerfile']
steps:
- name: Checkout
uses: actions/[email protected]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/**
* 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.dockerfile.simpledockerfile;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.io.FileUtils;
import org.apache.maven.shared.invoker.InvocationResult;
import org.apache.maven.shared.invoker.PrintStreamHandler;
import org.eclipse.jkube.integrationtests.JKubeCase;
import org.eclipse.jkube.integrationtests.maven.BaseMavenCase;
import org.eclipse.jkube.integrationtests.maven.MavenUtils;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.MethodOrderer;
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.extension.ExtendWith;
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 org.eclipse.jkube.integrationtests.docker.RegistryExtension;

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;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.parallel.ResourceAccessMode.READ_WRITE;
import static org.eclipse.jkube.integrationtests.assertions.PodAssertion.awaitPod;

@Tag(KUBERNETES)
@ExtendWith(RegistryExtension.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class SimpleDockerfileK8sITCase extends BaseMavenCase implements JKubeCase {

private static final String SIMPLE_DOCKERFILE = "projects-to-be-tested/dockerfile/simple-dockerfile";

private KubernetesClient k;

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

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

@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", "simple-dockerfile");
final File targetDockerfile = new File(
String.format("../%s/target/docker/integration-tests/" + getApplication() + "/latest/build/Dockerfile", getProject()));
final File expectedDockerfile = new File(String.format("../%s/Dockerfile", getProject()));
assertTrue(FileUtils.contentEquals(expectedDockerfile, targetDockerfile));
final List<String> imageFiles = listImageFiles(String.format("%s/%s", "integration-tests", getApplication()),
"/tmp");
assertThat(imageFiles, hasItem("/tmp/my-file.txt"));
assertThat(imageFiles, hasItem("/tmp/simple-dockerfile-0.0.0-SNAPSHOT.jar"));
}

@Test
@Order(2)
@DisplayName("k8s:push, should push image to remote registry")
void k8sPush() throws Exception {
// Given
final Properties properties = new Properties();
properties.setProperty("docker.push.registry", "localhost:5000");
// When
final InvocationResult invocationResult = maven("k8s:push", properties);
// Then
assertThat(invocationResult.getExitCode(), Matchers.equalTo(0));
final Response response = new OkHttpClient.Builder().build().newCall(new Request.Builder()
.get().url("http://localhost:5000/v2/integration-tests/simple-dockerfile/tags/list").build())
.execute();
assertNotNull(response.body());
assertThat(response.body().string(),
containsString("{\"name\":\"integration-tests/simple-dockerfile\",\"tags\":[\"latest\"]}"));
}

@Test
@Order(3)
@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/simple-dockerfile-deployment.yml"), yaml(not(anEmptyMap())));
}

@Test
@Order(4)
@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/simple-dockerfile:latest")),
hasProperty("name", equalTo("integration-tests-simple-dockerfile")),
hasProperty("ports", hasSize(1)),
hasProperty("ports", hasItems(allOf(
hasProperty("name", equalTo("http")),
hasProperty("containerPort", equalTo(8080))
)))
)));
}

@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));
assertThat(baos.toString(StandardCharsets.UTF_8),
stringContainsInOrder("Tomcat started on port(s): 8080", "Started Application in", "seconds"));
}

@Test
@Order(6)
@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));
}

final Pod assertThatShouldApplyResources() throws Exception {
final Pod pod = awaitPod(this)
.logContains("Started Application in", 40)
.getKubernetesResource();
awaitService(this, pod.getMetadata().getNamespace())
.assertExposed()
.assertIsNodePort()
.assertPorts(hasSize(1))
.assertPort("http", 8080, true)
.assertNodePortResponse("http", equalTo("Hello world!"));
return pod;
}

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

@Override
public String getApplication() {
return "simple-dockerfile";
}
}

26 changes: 26 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,32 @@
</plugins>
</build>
</profile>
<profile>
<id>dockerfile</id>
<modules>
<module>projects-to-be-tested/dockerfile/simple-dockerfile</module>
</modules>
<activation>
<file>
<exists>
projects-to-be-tested/dockerfile/simple-dockerfile/target
</exists>
</file>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>**/integrationtests/dockerfile/**/*ITCase.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>springboot</id>
<modules>
Expand Down
18 changes: 18 additions & 0 deletions projects-to-be-tested/dockerfile/simple-dockerfile/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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
#

FROM openjdk:latest
COPY maven/target/simple-dockerfile-0.0.0-SNAPSHOT.jar /tmp/simple-dockerfile-0.0.0-SNAPSHOT.jar
COPY maven/static-dir-in-project-root/my-file.txt /tmp/my-file.txt
CMD ["java", "-jar", "/tmp/simple-dockerfile-0.0.0-SNAPSHOT.jar"]
90 changes: 90 additions & 0 deletions projects-to-be-tested/dockerfile/simple-dockerfile/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.eclipse.jkube.integration-tests</groupId>
<artifactId>jkube-integration-tests-project</artifactId>
<version>${revision}</version>
<relativePath>../../../pom.xml</relativePath>
</parent>

<artifactId>simple-dockerfile</artifactId>
<name>${global.name} :: Dockerfile :: Simple Dockerfile</name>
<description>
Simple Dockerfile project
</description>

<properties>
<!-- Configuration for Service Enricher -->
<!-- TODO: Remove port related property once https://github.com/eclipse/jkube/issues/240 gets fixed -->
<jkube.enricher.jkube-service.port>8080</jkube.enricher.jkube-service.port>
<jkube.enricher.jkube-service.type>NodePort</jkube.enricher.jkube-service.type>
</properties>

<dependencies>
<!-- Dependencies only for debugging purposes -->
<dependency>
<groupId>org.eclipse.jkube</groupId>
<artifactId>kubernetes-maven-plugin</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jkube</groupId>
<artifactId>openshift-maven-plugin</artifactId>
<scope>compile</scope>
</dependency>
<!-- *************************************** -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.jkube</groupId>
<artifactId>kubernetes-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.eclipse.jkube</groupId>
<artifactId>openshift-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Loading

0 comments on commit 5dfc170

Please sign in to comment.