Automated Testing for Chef Using ChefSpec, InSpec, and Kitchen

Hands-On Lab


Photo of Keith Thompson

Keith Thompson

DevOps Training Architect II in Content





As infrastruture configuration is moved into code, using tools like Chef, it is important to have confidence in the code before deploying configuration changes. Source control management, automated testing, and continuous integration are key to providing this confidence. In this activity, we'll modify a Chef cookbook to ensure that it meets the requirements laid out in both unit and integration tests. To complete this activity, you will need to demonstrate an understanding of ChefSpec and InSpec syntax and assertions as well as the ability to implement the configuration necessary to make the tests pass.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Automated Testing for Chef Using ChefSpec, InSpec, and Kitchen

The Scenario

Now that we've got a good understanding of how Chef works, and how to create and deploy our own cookbooks, it's time to start following the best practices of the Chef community. To start with, we need to start writing tests. This will both guide our cookbook design, and provide regression tests for the future.

We're in the process of writing a cookbook to install Redis and run it as a service. The unit and integration tests are already written, and now it is time to implement the cookbook's recipe(s).

As we're writing the recipe we need to be sure we take small steps running the unit tests along the way, then finally run the integration tests after we think we're finished.

Get logged in

Use the credentials and server IP in the hands-on lab overview page to log into our lab server. Once we're in, we can get moving.

Our Environment

Once we're logged into the server, we can see chef-repo in our home directory, and a Redis cookbook in there. Let's get into that directory now:

cloud_user@host]$ cd ~/chef-repo/cookbooks/redis/

Unit Tests Pass

We'll start off with a unit test, and we'll actually run the cookbook:

cloud_user@host]$ chef exec rspec

We'll see errors. It looks like a couple of packages didn't get installed, and that the Redis daemon didn't start during the Chef run.

These are pretty simple things we can fix by creating and editing ~/chef-repo/cookbooks/redis/recipes/default.rb. Add this text to it:

package "epel-release"

package "redis"

service "redis" do
  action: [:enable :start}

Let's run it again:

cloud_user@host]$ chef exec rspec

Now let's try the integration test.

Integration Tests Pass

We'll do this with a simple command:

cloud_user@host]$ kitchen test

Once we've waited a few minutes, we'll see that this test passes fine.


We tested a cookbook, and figured out how to deal with a failure. Congratulations!