KubeLibrary: Testing Kubernetes with RobotFramework
Tags: robotframework, opensource, testautomation, kubernetes
Published: 2020-10-19 15:58:54

#Summary
This article explores the RobotFramework library [KubeLibrary](https://github.com/devopsspiral/KubeLibrary). KubeLibrary is a wrapper for the [Python Kubernetes Client](https://github.com/kubernetes-client/python/tree/master/kubernetes). It enables you to assert the status of various objects in your Kubernetes Clusters. As the library can be integrated with any RobotFramework test suite, it is ideal to verify the testability of your System-under-Test by asserting the status of your nodes, deployments, pods, configmaps, and others Kubernetes objects before running any end to end tests.

#What is the KubeLibrary?
As KubeLibrary is based on the [official python kubernetes client](https://github.com/kubernetes-client/python/blob/master/kubernetes) you can connect to your Kubernetes cluster while executing any Kubernetes API command.
 
Being part of the broader RobotFramework Library, all code is wrapped into keywords that can be used in test cases defined in ATDD (Acceptance Test Driven Development) or in BDD (Behavioral Driven Development) syntax.

#Why use the KubeLibrary?
When running end to end tests, you need to ensure that your system-under-test is fully available and runs the latest deployed version. With the KubeLibrary you can easily build tests to verify that all objects in your Kubernetes cluster are up and running and in the state that you expect them. This ensures that a successful end to end test run can be performed.

#How to use the KubeLibrary
There are [many different examples available within the GitHub repository of the KubeLibrary](https://github.com/devopsspiral/KubeLibrary/tree/master/testcases). But let’s show you a small example here: 

Let’s say you want to make sure that all pods in a certain namespace are running and use a specific image version.

{% gist https://gist.github.com/Nilsty/83ad625eb76a3fe5f9891919bb4f169a %}
https://gist.github.com/Nilsty/83ad625eb76a3fe5f9891919bb4f169a

If you run this test in your Kubernetes cluster, it would check for pods matching the name pattern in ${POD_NAME_PATTERN} in a namespace ${NAMESPACE}. It will search for running pods for 2 minutes. Once the pods are confirmed running it will confirm the image they are using.
After you have confirmed that your pods are running you can continue with any application-level testing.
The KubeLibrary can be also used for checking nodes, jobs, configmaps, persistent volume claims, services, and other Kubernetes objects. Current keyword documentation is available on GitHub ([link](https://github.com/devopsspiral/KubeLibrary/blob/master/docs/KubeLibrary.html)).

# Contributing to the KubeLibrary
Almost every week new keywords are added to the library, but the Kubernetes API is big and not all of its functionality is covered in the KubeLibrary, yet. If you are missing certain functionality please file an issue in the GitHub project or create a pull request. Adding new functionality can be quite easy as the [python client for kubernetes](https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md) is well documented and often only a wrapper for the existing definitions is required.

# How do we use the KubeLibrary at Humanitec?
At Humanitec our end to end test automation is organized in Robot Framework test suites. We utilize several libraries to gain a holistic view through our testing. For API testing we use the [Requests Library](https://github.com/MarketSquare/robotframework-requests), for UI testing we use the [Selenium Library](https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html).
Humanitec enables the user to develop, maintain, and deploy applications to Kubernetes clusters. Thus, we also utilize the KubeLibrary to verify the expected state of Kubernetes objects created and updated by Humanitec. For example, if you choose to update environment variables for your application through Humanitec, this will trigger an update to a configmap object in your applications cluster. In our tests, we trigger this update with an API call to the Humanitec API, then verify with the KubeLibrary if the update was done as expected in the cluster.

This way, we can build easily automated regression tests for all the features provided by Humanitec.

If you want to learn more about KubeLibrary, join our next [webinar](https://humanitec.com/webinars/test-automation-in-continuous-deployment)!