Reader small image

You're reading from  Kubernetes for Developers

Product typeBook
Published inApr 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788834759
Edition1st Edition
Languages
Right arrow
Author (1)
Joseph Heck
Joseph Heck
author image
Joseph Heck

Joseph Heck has broad development and management experience across start-ups and large companies. He has architected, developed, and deployed a wide variety of solutions, ranging from mobile and desktop applications to cloud-based distributed systems. He builds and directs teams and mentors individuals to improve the way they build, validate, deploy, and run software. He also works extensively with and in open source, collaborating across many projects, including Kubernetes.
Read more about Joseph Heck

Right arrow

Chapter 9. Integration Testing

So far, we have reviewed how to run your code and describe your services in Kubernetes. We also looked into how to utilize additional tools to get information about how your code is running on each pod and on aggregate. This chapter builds on this to look at how to use Kubernetes to validate your code, with examples of different ways to accomplish that testing, and suggestions for how to leverage Kubernetes for validation testing.

Topics for this chapter include:

  • Testing strategies using Kubernetes
  • Simple validation with Bats
  • Example – testing code with Python
  • Example – testing code with Node.js
  • Continuous integration with Kubernetes

Testing strategies using Kubernetes


There is a wide variety of kinds of tests that are used during development and validation, in software engineering. Across this taxonomy, there are some kinds of tests that utilize the strengths of Kubernetes extremely well. The terms associated with testing can be vague and confusing, so for clarity, we will briefly review the terms I'll use and the differences between these kinds of tests. There are many more variants of these themes that I haven't detailed here, but for the purpose of describing where Kubernetes is most effective, this list is sufficient:

  • Unit tests: Unit tests are the lowest level of testing; this focuses on the interfaces, implementations, and modules within your application. Unit testing frequently implies isolated testing of just the component that the test focuses on. These tests are generally intended to be very fast, easily run on a developer's system directly, and often with no access to external services upon which the relevant...

Simple validation with Bats


A fairly common desire is to simply get everything deployed and make a few queries to validate that the resulting system is operational. As you do these operations, they are frequently captured in either Makefiles or shell scripts as simple programs to validate a baseline of functionality. Several years ago, a system called Bats, which stands for Bash Automated Testing System, was developed to make it slightly more convenient to run tests using shell scripts.

There are several examples of using Bats to test systems deployed in Kubernetes. The tests are generally straightforward and easy to read, and it is easy to extend and use. You can find more information on Bats at its GitHub home https://github.com/sstephenson/bats. You may see Bats used in some Kubernetes-related projects as well, for simple validation.

Bitnami has set up an example GitHub repository to use as a starting point that uses Bats and Minikube, and was designed to also work with external CI systems...

Example – integration testing with Python


In the case of Python, the example code here uses PyTest as a test framework. The example code can be found on GitHub, in the 0.7.0 branch of the repository https://github.com/kubernetes-for-developers/kfd-flask/.

You can download the example using the following command:

git clone https://github.com/kubernetes-for-developers/kfd-flask/ -b 0.7.0

In the example, I changed the code structure to move all the Python code for the application itself under the src directory, following the recommended pattern from PyTest. If you have not used PyTest before, reviewing their best practices at https://docs.pytest.org/en/latest/goodpractices.html is very worthwhile.

If you view the code or download it, you will also notice a new file, test-dependencies.txt, which defines a number of dependencies specific to testing. Python does not have a manifest where they separate out dependencies for production from ones used during development or testing, so I separated the...

Example – integration testing with Node.js


The Node.js example uses mocha, chai, supertest, and the JavaScript kubernetes client in much the same fashion as the Python example. The example code can be found on GitHub, in the 0.7.0 branch of the repository at https://github.com/kubernetes-for-developers/kfd-nodejs/.

You can download the example using the following command:

git clone https://github.com/kubernetes-for-developers/kfd-nodejs/ -b 0.7.0

I took advantage of Node.js's mechanism to have development dependencies separate from production dependencies, and added most of these dependencies into package.json. I also went ahead and set up a simple unit test directly in a test directory, and a separate integration test in an e2e-tests directory. I also set up the commands so that you can run these tests through npm:

npm test

For the unit tests, the code runs locally and takes advantage of supertest to access everything within a JavaScript runtime on your local machine. This doesn't account for...

Continuous integration with Kubernetes


Once you have integration tests, getting something operational to validate those tests is very important. If you don't run the tests, they're effectively useless—so having a means of consistently invoking the tests while you're doing development is important. It is fairly common to see continuous integration do a lot of the automated lifting for development.

There are a number of options available to development teams to help you with continuous integration, or even its more advanced cousin, continuous deployment. The following tools are an overview of what was available at the time of writing, and in use by developers working with their code in containers and/or in Kubernetes:

  • Travis.CI: Travis.CI (https://travis-ci.org/) is a hosted continuous integration service, and it is quite popular as the company offers free service with an easy means of plugging into GitHub for public and open source repositories. Quite a number of open source projects leverage...

Summary


In this chapter, we delved into how to use Kubernetes when testing your code. We looked at the patterns you might explore with integration testing. We pointed at a simple example of using shell scripts to run integration tests within Kubernetes, and then dove more deeply into examples using Python and Node.js that run integration tests using Kubernetes. Finally, we wrapped up the chapter with an overview of options that are readily available for continuous integration that can use a cluster, and explored two options: using Travis.CI as a hosted solution and how to use Jenkins on your own Kubernetes cluster.

In the next chapter, we will look at how we can pull together multiple pieces that we have explored and show how to benchmark your code running on Kubernetes.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Kubernetes for Developers
Published in: Apr 2018Publisher: PacktISBN-13: 9781788834759
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Joseph Heck

Joseph Heck has broad development and management experience across start-ups and large companies. He has architected, developed, and deployed a wide variety of solutions, ranging from mobile and desktop applications to cloud-based distributed systems. He builds and directs teams and mentors individuals to improve the way they build, validate, deploy, and run software. He also works extensively with and in open source, collaborating across many projects, including Kubernetes.
Read more about Joseph Heck