As more companies become more DevOps focused, code testing is creeping its way out of just the software development realm. With software "eating the world," we are now seeing operations/system administrators writing more code. A lot of this code is "infrastructure code". This code automates tasks like VM creation, standing up databases, adding/removing users from machines and so on.
With the ever-increasing dependence on this "infrastructure code", it's becoming necessary to perform tests on this code through concepts like unit and integration testing.
One open source product that expedites integration testing is called Test Kitchen. Test Kitchen, developed by Chef, allows the user to dynamically bring up virtual machines, perform tests on them and quickly tear them down. It's a great tool to use to actually run your infrastructure code in a sandbox to ensure it does what you would expect.
To get started with Test Kitchen, you'll need to install a few prerequisites.
- Ruby (Test Kitchen is packed as a Ruby gem)
- Vagrant (to provision the VMs)
- VirtualBox (as a Test Kitchen provider)
Once you've got each of these tools installed, you can proceed. Test Kitchen can be run on Windows, Linux or MacOS but for this article, I will be using MacOS.
First, install the Test Kitchen Ruby gem in your Bash console. This will download Test Kitchen from the online repository and get it installed. For this post, I'll be working with version 1.13.2.
> sudo gem install test-kitchen
Initializing Test Kitchen
The first task you must complete is initializing Test Kitchen. This is done through the
kitchen init command.
Below I'm running
kitchen init and specifying the kitchen-vagrant driver. In Test Kitchen, drivers are used to simulate testing on various providers. They are the layer between you and the provider (VirtualBox in this instance).
If the driver is not installed, it will be installed for you. This also creates the main kitchen.yml file where most of your configuration will be.
Let's take a look at the kitchen.yml file.
By default, the kitchen.yml file contains a template. This templates specifies that you'd like to bring up an Ubuntu 14.04 and a CentOS via Vagrant. Leave the default configuration file the way it is and now run
kitchen list to see what will be created.
Bringing up VMs
Now bring up the VMs by running
kitchen create. This will download the VMs if not on the local machine and get them configured using Vagrant.
Once the VMs are created is where your choices are open. Test Kitchen is built to test Chef recipes but can also run Pester tests and more. You're now on the way to building and running tests against the infrastructure that was just created!
At this point, the world is your oyster. You are encouraged to dive deeper into Test Kitchen by visiting the official Test Kitchen site. This is where you'll find similar examples of what was covered here as well as how to take your Test Kitchen installation further by incorporating Chef test recipes and more.
In this post, you were introduced to Chef's Test Kitchen product. This product allows developers and administrators to quickly provision virtual infrastructure. This virtual infrastructure can then be used for testing infrastructure as code scenarios ensuring your code is solid.
Join the Jar Tippers on Patreon
It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!Become a Patron!
Subscribe to Adam the Automator
Get the latest posts delivered right to your inbox