Test your Kubernetes Deployments
Switch branches/tags
Nothing to show
Clone or download
Latest commit ce95b60 Nov 23, 2017
Permalink
Failed to load latest commit information.
src/test Updates pom.xml Nov 21, 2017
.gitignore
README.adoc Update README.adoc Nov 23, 2017
pom.xml Updates pom.xml Nov 21, 2017

README.adoc

Kubernetes Deployment Testing

This project uses Arquillian Kube Extension and Kubernetes Assertions to test a sample Kubernetes deployment for the PHP Guestbook application with Redis.

What Do You Test ?

The project aims at demonstrating a black box approach to testing your deployment that neither mutates the containers (by deploying, reconfiguring etc) nor the Kubernetes resources.

The focus of the project is on immutable infrastructure and integration testing, wherein the test cases are meant to, consume and test the provided services and assert that the environment is in the expected state, providing you with the confidence that your application will work correctly when deployed on a Kubernetes cluster.

How Do You Test The Deployment ?

The following sections explain how to leverage the Arquillian Kube Extension and Fabric8 Kubernetes Assertions to test your Kubernetes deployments.

Preparing the Environment

Setup Kubernetes Cluster Locally

One of the pre-requisites to use Arquillian Kubernetes Extension, is to have the Kubernetes cluster running on your host machine.

An easier way to setup and start the Kubernetes cluter on your local host is to use Minikube.

Setup Maven Project Dependencies

For Kubernetes deployment testing using Arquillian Kube extension, include the following dependencies in pom.xml:

Arquillian Cube BOM - Unified Dependencies
<properties>
    <version.arquillian_cube>${latest_released_version}</version.arquillian_cube>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.arquillian.cube</groupId>
            <artifactId>arquillian-cube-bom</artifactId>
            <version>${version.arquillian_cube}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>
Arquillian Cube Requirement
<dependency>
    <groupId>org.arquillian.cube</groupId>
    <artifactId>arquillian-cube-requirement</artifactId>
    <scope>test</scope>
</dependency>
Arquillian Cube Kubernetes
<dependency>
    <groupId>org.arquillian.cube</groupId>
    <artifactId>arquillian-cube-kubernetes</artifactId>
    <scope>test</scope>
</dependency>
Arquillian JUnit
<dependency>
    <groupId>org.jboss.arquillian.junit</groupId>
    <artifactId>arquillian-junit-standalone</artifactId>
    <version>${latest_released_version}</version>
    <scope>test</scope>
</dependency>

For fabric8 kubernetes assertions, include the following dependency in the pom.xml

Fabric8 Kubernetes Assertions
<dependency>
    <groupId>io.fabric8</groupId>
    <artifactId>kubernetes-assertions</artifactId>
    <version>${latest_released_version}</version>
    <scope>test</scope>
</dependency>

Writing Deployment Tests

Arquillian Kube extension provides out of the box functionality to create and manage a temporary namespace per test suite for your tests and then applies all the required kubernetes resources as defined in the kubernetes manifest file provided by you to create your environment.

The extension by default looks for the manifest file named kubernetes.json or kubernetes.yaml in the classpath.

The name of the manifest file can be changed by setting the property env.config.resource.name either as an environment variable or as system property or by defining it in the arquillian.xml as shown below with latter having the maximum priority.

arquillian.xml
<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://jboss.org/schema/arquillian"
     xsi:schemaLocation="http://jboss.org/schema/arquillianhttp://jboss.org/schema/arquillian/arquillian_1_0.xsd">

    <extension qualifier="kubernetes">
        <property name="env.config.resource.name">guestbook-all-in-one.yaml</property>
    </extension>

</arquillian>

Kubernetes resources can then be made accessible within the Test Cases by injecting them using Arquillian’s @ArquillianResources annotation (see example test below).

ExampleTest.java
@RunWith(Arquillian.class)
public class ExampleTest {

    @ArquillianResource       (1)
    KubernetesClient client;

    @ArquillianResource
    Session session;

    @Named("dummy")           (2)
    @ArquillianResource
    Service dummyService;

    @Test
    public void testAtLeastOnePod() throws Exception {
       assertThat(client).pods().runningStatus().filterNamespace(session.getNamespace()).hasSize(1);  (3)
    }
}

The resource providers available, can be used to inject to your test cases the following resources:

  • A kubernetes client as an instance of KubernetesClient.

  • Session object that contains information (e.g. the namespace) or the uuid of the test session.

  • Services (by id or as a list of all services created during the session, optionally filtered by label)

  • Deployments (by id or as a list of all deployments created during the session, optionally filtered by label)

  • Pods (by id or as a list of all pods created during the session, optionally filtered by label)

  • Replication Controllers (by id or as a list of all replication controllers created during the session, optionally filtered by label)

  • Replica Sets (by id or as a list of all replica sets created during the session, optionally filtered by label)

Further, using Fabric8 Kubernetes Assertions, a nice library based on assert4j, aids in performing meaningful and expressive assertions on top of the Kubernetes model.

Once everything is ready, Arquillian runs your tests, enriched with resources required to access service and finally cleaning up everything after the testing is over.

For more details and available configuration options check arquillian kube documentation.

Running Sample Tests

To run the sample deployment tests in this project follow the steps below:

  • git clone git@github.com:hemanik/kubernetes-deployment-testing.git

  • minikube start (to start the Kubernetes cluster)

  • mvn clean verify