Switch branches/tags
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
..
Failed to load latest commit information.
src
ReadMe.md
pom.xml

ReadMe.md

Kubernetes Assertions

Maven Central Javadocs

This library provides a bunch of helpful assertj assertions for working with the kubernetes-api.

Default system test

The following code provides a default system test:

             assertThat(client).deployments().pods().isPodReadyForPeriod();

This will assert that the current project's Deployment creates at least one pod; that it becomes Ready within a time period (30 seconds by default), then that the pod keeps being Ready for a period (defaults to 10 seconds).

This may seem a fairly simple test case; but it catches most errors with the Deployment being invalid or failing to start; the pod starting then failing due to some configuration issue etc.

If your application uses liveness checks (which are used by default with Spring Boot apps) then this test also asserts that the liveness checks keep valid for the period too. So if your application fails to connect to a database or your Camel route fails to start a route or whatever; then the test fails!

This means to improve your system tests you can just improve your liveness checks; which also helps Kubernetes manage your production environment too!

Other examples

Some quick examples:

Navigating and asserting around resources

When working with Kubernetes and OpenShift resources in test cases you'll find that objects can be massively nested.

For example even using something as simple as a Pod you need to navigate to compare its name:

Pod pod = kubernetesClient.pods().inNamespace(ns).withName("foo").get();
assertThat(pod).metadata().name().isEqualTo("foo");

Things get even more complex when asserting a ReplicationController

ReplicationController rc = kubernetesClient.replicationControllers().inNamespace(ns).withName("foo").get();
assertThat(rc).spec().template().spec().containers().first().image().isEqualTo("someDockerImageName");

Whats great about Kubernetes Assertions is that you can chain methods together to navigate the model; if any navigation fails you get meaninful errors in the test failure telling you exactly which object was null or list was empty or other assertion failed (e.g. a list index was out of range) etc.

For example here's some example error messages from assertj when navigation or assertions fail in these tests:

    org.junit.ComparisonFailure: [podListWith2Items.items.first().metadata.name] expected:<"[shouldNotMatch]"> but was:<"[abc]">
    
    java.lang.AssertionError: [podListWith2Items.items.index]
    Expecting:
     <-1>
    to be greater than or equal to:
     <0>

### Add it to your Maven pom.xml

To be able to use the Java code in your Apache Maven based project add this into your pom.xml

        <dependency>
            <groupId>io.fabric8</groupId>
            <artifactId>kubernetes-assertions</artifactId>
            <version>2.2.101</version>
            <scope>test</scope>
        </dependency>