No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.mvn/wrapper
src
.gitignore
README.adoc
mvnw
mvnw.cmd
pom.xml

README.adoc

Zero Config Deployment Test

The project aims at demonstrating how to bring your Java applications on to Kubernetes and OpenShift by building Docker images and creating Kubernetes and OpenShift resource descriptors with a Zero-Config setup that allows for a quick ramp-up with some opinionated defaults using Fabric8 Maven Plugin and executing deployment tests using Arquillian Kube for a SpringBoot GuestBook Application to ensure that the application will work correcty when deployed on an OpenShift cluster.

How To Build And Deploy Your Application ?

Fabric8 Maven Plugin makes Kubernetes/OpenShift look and feel like an application server to a Java developer by letting you build and deploy your application from maven just like you would with other maven plugins.

Setup your maven project

To enable fabric8 on your existing maven project just type fabric8:setup command which adds the fabric8-maven-plugin to your pom.xml.

Alternatively, you can manually add the following plugin definition to your pom.xml file:

<plugin>
    <groupId>io.fabric8</groupId>
    <artifactId>fabric8-maven-plugin</artifactId>
    <version>3.5.32</version>

    <!-- Connect fabric8:resource and fabric8:build to lifecycle phases -->
    <executions>
        <execution>
            <id>fmp</id>
            <phase>package</phase>
               <goals>
                    <goal>resource</goal>
                    <goal>build</goal>
                </goals>
        </execution>
    </executions>
</plugin>

It enables automatic generation of a Docker image and Kubernetes / OpenShift descriptors including the application.

For more and complete configuration details check out fabric8 maven plugin documentation.

How Do You Test The Deployment ?

Arquillian Kube Extension provides a black box approach to testing your deployment that neither mutates the containers (by deploying, reconfiguring etc) nor the Kubernetes/OpenShift resources.

It is used for 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 an OpenShift cluster.

Preparing the Environment

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

Setup OpenShift Cluster Locally

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

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

Setup Maven Project Dependencies

For OpenShift 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 OpenShift
<dependency>
    <groupId>org.arquillian.cube</groupId>
    <artifactId>arquillian-cube-openshift</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 OpenShift Client, include the following dependency in the pom.xml

Fabric8 OpenShift Client
dependency>
    <groupId>io.fabric8</groupId>
    <artifactId>openshift-client</artifactId>
    <version>${latest_released_version}</version>
</dependency>

Setup Maven Project Dependencies For Assertion Libraries

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/openshift resources as defined in the resource descriptors generated by fabric8 maven plugin for your environment.

Kubernetes/OpenShift 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)
    OpenShiftClient client;

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

    @RouteURL("guestbook")    (3)
    @AwaitRoute
    private URL route;

    @Test
    public void service_instance_should_not_be_null() throws Exception {
        assertThat(service).isNotNull();
    }

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

    @Test
    public void verify_route_is_configured_and_service_is_accessible() throws IOException {
        assertThat(route).isNotNull();
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder().get().url(route).build();
        Response response = okHttpClient.newCall(request).execute();

        assertThat(response).isNotNull();
        assertThat(response.code()).isEqualTo(200);
    }
}
  1. 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)

    The OpenShift extension also provides:

    • An openshift client as an instance of OpenShiftClient.

    • Deployment Configs (by id or as a list of all deployment configs created during the session)

  2. Resources can be injected into test cases by id or as a list of all deployments created during the session, optionally filtered by label.

  3. Test Enrichers like @RouteURL further aid in injection of container resources like route to the deployed service.

    For farbric8 maven plugin to identify the route, @RouteURL should be set to artifactId of the project by default, or explicity configured otherwise.

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

Once everything is ready, Arquillian Kube 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@github.com:hemanik/zero-config-deployment-test.git

  • minishift start (tested with OpenShift Origin 3.6.0)

  • oc login

  • mvn clean verify -Denv.init.enabled=true -DenableImageStreamDetection=true