Unit Testing Chef Cookbooks with ChefSpec

Sauce AI for Test Authoring: Move from intent to execution in minutes.|xBack to ResourcesBlogPosted

May 17, 2026 · 7 min read · Testing Guide

Sauce AI for Test Authoring: Move from intent to execution in minutes.

|

x

Back to Resources

Blog

Posted November 28, 2016

Unit Testing Chef Cookbooks with ChefSpec

quote

I lately had the opportunity to get involved withChefcookbook ontogeny, and begin learning how to write some examination for infrastructure codification by apply various testing frameworks. One critical aspect of developing any codification is writing unit tests. We useChefSpec- it & # x27; s a unit testing framework for testing Chef cookbooks.

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

Below, I & # x27; ll show off ChefSpec in action. I & # x27; ll do that by 1st setting up a workstation to run Chef topically, so use the existing Chef community cookbook to deploy the application on the local workstation environment. Finally, I & # x27; ll walk through some of the unit to make sure everything is work properly.

Workstation Prerequisite

To set up your local development environment, you & # x27; ll first need to download theChef Development Kit(DK). Why? I can specifically download Chef DK variant 0.15.15 since I cognise it is compatible with my environment. The version will change as the Chef DK get new update (and it & # x27; s your obligation to formalize the changes are compatible). Once you & # x27; ve downloaded and installed Chef DK, open up your terminal window and check the Chef version installed.

$ chef -v
Chef Development Kit Version: 0.15.15
chef-client version: 12.11.18
speech version: 0.0.23
berks version: 4.3.5
kitchen variation: 1.10.0

Once you & # x27; ve downloaded and extracted theChef Selenium community cookbookfrom the Chef Supermarket, open up your terminal window and navigate to the chef-selenium directory to finish preparing your local surroundings.

$ cd ~/work/chef-selenium
$ chef exec bundle install
...
...
Bundle complete! 10 Gemfile dependencies, 113 gems now install.
Use bundle display [gemname] to see where a bundled gem is installed.

Verify Chef Cookbook with Chefspec Tests (aka Unit Testing)

ChefSpec allows us to simulate the intersection of resources to show that the cookbook code works correctly before pushing changes to production. It & # x27; s much easier and cheaper to catch bugs locally, as opposed to get a production coating crash and burn. ChefSpec is the fast way to screen 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) fabric that allows writing readable Ruby tests. Teams should aim for 100 % coverage of code for unit examination. Every Chef cookbook should receive a folder called “ spec. ” (This is where ChefSpec unit tests live). Let & # x27; s review the chef-selenium file structure for unit testing:

|- chef-selenium/
| -- recipes/
| -- -- default.rb
| -- -- hub.rb
| -- -- node.rb
| -- ... | -- spec/
| -- -- unit/
| -- -- -- default_spec.rb
| -- -- -- hub_spec.rb
| -- -- -- node_spec.rb
| -- -- spec_helper.rb

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

We & # x27; re only reviewing the spec test & # x27; default_spec.rb & # x27; that report the & # x27; default.rb & # x27; recipe. This recipe downloads and installs the Selenium Standalone JAR file for Windows, Linux, and Mac OS. TheSoloRunnerallows us to copy essay against the different function 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 outlined node attributes that we & # x27; ll use in our spec tests afterwards.

6 ChefSpec: :SoloRunner.new (platform: & # x27; windows & # x27;, version: & # x27; 2008R2 & # x27; do |node|
...
...
46 ChefSpec: :SoloRunner.new (platform: & # x27; centos & # x27;, variation: & # x27; 7.0 & # x27; do |node|
...
87 ChefSpec: :SoloRunner.new (platform: & # x27; mac_os_x & # x27;, version: & # x27; 10.10 & # x27;) do |node|

Let & # x27; s looking at the different ways to test whether apackageresource is installed or not installed with ChefSpec. Line 1 tells Chef to unzip the bundle unless the platform is Windows or Mac OS.

1 package & # x27; unzip & # x27; unless program? (& # x27; windows & # x27;, & # x27; mac_os_x & # x27;)

source: https: //github.com/dhoer/chef-selenium/recipes/default.rb (Line 1)

SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.

For the next test, Line 6 tells Chef to simulate a test run against Windows 2008R2 by calling the nonremittal recipe to determine if the package will be unzipped. In Line 14, by habituate ChefSpecPackageMatcher, you can formalize that a packet was not installed for the Windows platform.

Windows

6 ChefSpec: :SoloRunner.new (platform: & # x27; windows & # x27;, version: & # x27; 2008R2 & # x27; do |node|
...
13 it & # x27; make not install zip software & # x27; do
14 expect (chef_run) .to_not install_package (& # x27; unzip & # x27;)
15 end

Next test - Line 57 proves that a package was installed for the Linux platform.

Linux

46 ChefSpec: :SoloRunner.new (program: & # x27; centos & # x27;, version: & # x27; 7.0 & # x27; do |node|
...
56 it & # x27; installs software & # x27; do
57 expect (chef_run) .to install_package (& # x27; unzip & # x27;)
58 end

Next test - Line 7 sets environment variable & quot; SYSTEMDRIVE & quot; 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 expend the ChefSpecRemoteFileMatcherto verify the removed file was created.

7 ENV [& # x27; SYSTEMDRIVE & # x27;] = & # x27; C: & # x27;
8 node.override [& # x27; selenium & # x27;] [& # x27; url & # x27;] =
9 & # x27; https: //selenium-release.storage.googleapis.com/2.48/selenium-server-standalone-2.48.0.jar & # x27;
...
...
33 it & # x27; downloads jar & # x27; do
34 expect (chef_run) .to create_remote_file (& # x27; C: /selenium/server/selenium-server-standalone-2.45.0.jar & # x27;)
35 end

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

Terminal Console Output

$ chef exec bundle exec rake
Finished in 23.33 s (files took 2.27 seconds to load)
52 examples, 0 failures

ChefSpec Coverage report generated ...

Entire Resources: 36
Touched Resources: 35
Touch Coverage: 97.22 %

Uninfluenced Resources:

remote_file [/opt/selenium/server/selenium-server-standalone-3.0.0.jar] selenium/recipes/default.rb:16

The test coverage yield is disabled as the nonpayment setting. The following code snippet needs to be inside the spec/spec_helper.rb file to enable test coverage. I commend enable examination coverage for any application code.

Enable ChefSpec Test Coverage

ChefSpec: :Coverage.start!

Conclusion

In this blog situation, we set up a clean development surroundings and reviewed some of the ChefSpec tests for the chef-selenium default recipe, executed them across multiple platform, 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 cookbook. Just taking the time to learn something new can be a dash task. There are a lot of new concepts to learn and best practices to see when developing and testing base code. To preserve 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 technologist at Gannett and co-founder of Quality Element. He is a passionate automation engineer assay to optimise software development quality, while coach team members on how to write great automation scripts and helping the testing community become better testers. Greg has spent most of his calling 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 machine-controlled test strategies, frameworks, puppet and platforms.

Published:
Nov 28, 2016
Topics
Share this post
Copy Share Link
LinkedIn
© 2026 Sauce Labs Inc., all right earmark. SAUCE and SAUCE LABS are registered stylemark owned by Sauce Labs Inc. in the United States, EU, and may be register in early jurisdictions.
robot
quote

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free