Unit Testing Chef Cookbooks with ChefSpec

I recently had the opportunity to get involved with Chef cookbook development, and begin learning how to write some tests for infrastructure code by using various testing frameworks. One critical aspect of developing any code is writing unit tests. We use ChefSpec - it's a unit testing framework for testing Chef cookbooks.

ChefSpec is simple to write. It reads a whole lot easier than other unit testing frameworks, and provides fast feedback on cookbook changes without the need for a virtual machine or cloud machine.

Below, I'll show off ChefSpec in action. I'll do that by first setting up a workstation to run Chef locally, then use the existing Chef community cookbook to deploy the application on the local workstation environment. Finally, I'll walk through some of the unit to make sure everything is working properly.

Workstation Prerequisite

To set up your local development environment, you'll first need to download the Chef Development Kit (DK). Why? I can specifically download Chef DK version 0.15.15 since I know it is compatible with my environment. The version will change as the Chef DK receives new updates (and it's your responsibility to validate the changes are compatible). Once you've downloaded and installed Chef DK, open up your terminal window and check the Chef version installed.

Once you've downloaded and extracted the Chef Selenium community cookbook from the Chef Supermarket, open up your terminal window and navigate to the chef-selenium directory to finish preparing your local environment.

Verify Chef Cookbook with Chefspec Tests (aka Unit Testing)

ChefSpec allows us to simulate the convergence of resources to prove that the cookbook code works correctly before pushing changes to production. It's much easier and cheaper to catch bugs locally, as opposed to having a production application crash and burn. ChefSpec is the fastest way to test resources and recipes as part of a simulated chef-client on a local developer machine. It is an extension of RSpec, a behavior-driven development (BDD) framework that allows writing readable Ruby tests. Teams should aim for 100% coverage of code for unit testing. Every Chef cookbook should have a folder called “spec.” (This is where ChefSpec unit tests live). Let's review the chef-selenium file structure for unit testing:

When writing unit tests for your cookbook, the standard is to create a separate spec test for each recipe in the /spec folder.

We're only reviewing the spec test 'default_spec.rb' that describes the 'default.rb' recipe. This recipe downloads and installs the Selenium Standalone JAR file for Windows, Linux, and Mac OS. The SoloRunner allows us to simulate testing against the different operating systems (platforms) and versions by passing a parameter. Lines 6, 46, and 87 simulate the Chef run-in memory, as previously described, and override any defined node attributes that we'll use in our spec tests later.

For the next test, Line 6 tells Chef to simulate a test run against Windows 2008R2 by calling the default recipe to determine if the package will be unzipped. In Line 14, by using ChefSpec PackageMatcher, you can validate that a package was not installed for the Windows platform.

Next test - Line 7 sets environment variable "SYSTEMDRIVE" for Windows, and Lines 8-9 “override” a node attribute [‘selenium’] [‘url’] that we’ll use in our specs later. Line 34 simulates the download of the Selenium Standalone JAR file by using the ChefSpec RemoteFileMatcher to verify the remote file was created.

To run ChefSpec, from the terminal, go to the root of your cookbook (chef-selenium) and run Rake. As you can see by reviewing the console output, the chef-selenium cookbook does not have 100% test coverage, but all ChefSpec tests passed.

The test coverage output is disabled as the default setting. The following code snippet needs to be inside the spec/spec_helper.rb file to enable test coverage. I recommend enabling test coverage for any application code.

Conclusion

In this blog post, we set up a clean development environment and reviewed some of the ChefSpec tests for the chef-selenium default recipe, executed them across multiple platforms, and analyzed the test coverage output. Your infrastructure code deserves testing, and before deploying those servers, prove that your cookbook works.

This blog post only scratches the surface of testing Chef cookbooks. Just taking the time to learn something new can be a daunting task. There are a lot of new concepts to learn and best practices to understand when developing and testing infrastructure code. To continue learning about ChefSpec and RSpec testing extensions, you can reference these documentation links to test cookbooks, recipes, and resources:

Greg Sypolt (@gregsypolt) is a senior engineer at Gannett and co-founder of Quality Element. He is a passionate automation engineer seeking to optimize software development quality, while coaching team members on how to write great automation scripts and helping the testing community become better testers. Greg has spent most of his career working on software quality — concentrating on web browsers, APIs, and mobile. For the past five years, he has focused on the creation and deployment of automated test strategies, frameworks, tools and platforms.