Product and service reviews are conducted independently by our editorial team, but we sometimes make money when you click on links. Learn more.
 

How to Use Test Kitchen to Check Infrastructure Code

By - Source: Toms IT Pro

Chef's Test Kitchen makes it easy to automate and streamline the process of testing infrastructure code.

Credit: ShutterstockCredit: ShutterstockAs more companies become more DevOps focused, code testing is creeping its ways out of just the software development realm. More and more operations/system administrators are writing more code. And a lot of this code is "infrastructure code" meaning it's automating tasks such as 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 more necessary to perform tests on this code through concepts like unit testing 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.

MORE: Best Computer Programming Certifications

Prerequisites

To get started with Test-Kitchen, you'll need to install a few prerequisites. First, Test-Kitchen is packaged as a RubyGem. You'll also need to install Git. To make VM provisioning easy, I'll be using Vagrant so we'll need that as well. And finally, use VirtualBox as a Test-Kitchen provider that will actually bring up the VMs. 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.

Installation

First, I'll install the Test-Kitchen RubyGem.›
sudo gem install test-kitchen

I can now confirm the version of Test-Kitchen I have installed.

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, it contains a template. This model specifies that we'd like to bring up an Ubuntu 14.04 and a CentOS via Vagrant. I'll leave the default configuration file the way it is, and run kitchen list to see what will be created.

Bringing up VMs

I'll now bring up these two 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.

Comments