Skip to content

starburstdata/kubernetes-assertions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kubernetes Assertions

CircleCI 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>

Example of checking your cluster

public class StarburstPlatformClusterTest {

  @Test
  public void validateAllPrestoPodsArePresentInCluster() {
    final KubernetesClient cluster = new DefaultKubernetesClient();

    // We need to collect what we expect to be running
    assertThat(cluster).pods().runningStatus().hasSize(27);

    // Retreive podIds from the simple names stated in the Pod metadata
    Map<String, String> podIds = assertThat(cluster).podIdsFromNames("presto",
        "coordinator", "worker", "hive-metastore", "hive-postgresql", "minio");

    // Assert that all the pods we expect to be present are there
    assertThat(cluster).namespace("presto").pod(podIds.get("coordinator"));
    assertThat(cluster).namespace("presto").pod(podIds.get("worker"));
    assertThat(cluster).namespace("presto").pod(podIds.get("hive-metastore"));
    assertThat(cluster).namespace("presto").pod(podIds.get("hive-postgresql"));
    assertThat(cluster).namespace("presto").pod(podIds.get("minio"));
  }
}

### 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>com.starburstdata</groupId>
            <artifactId>kubernetes-assertions</artifactId>
            <version>1.0.0</version>
            <scope>test</scope>
        </dependency>

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages