Test Driver Infrastructure with chef

By Praveen Shukla

Elevator Pitch

Traditionally, much of infrastructure is managed manually or by using scripts. This inconsistency causes many issues in infrastructure. TDD is known for increasingCodeQuality, improvingOverallDesign, and allowingSafeRefactoring . Explaining about how why and which tools we should use to test code.


As we all know that test-driven development (TDD) is well known for increasing code quality, improving overall design, and allowing safe refactoring throughout a project. So, I am going to explain about how and why we should test our code and what are tools are available to test our infrastructure code.

Before Getting into actual usage of tool we should understand the phases of 1. Pre-convergence: Testing phase that does not require any node to run the test. 2. Convergence: Chef runs and make changes to the system on node. 3. Post-convergence: Node finishes running Chef, also verifying that node is in the desired state.

Principles of TDI(Test-Driven Infrastructure): Paul Duvall of Stelligent has written about a 5-step process of automating environments for continuous delivery: document, test, script, version, and continuous. These 5 steps can be taken as the basic principles for Test-Driven Infrastructure. Each principle is described in more detail below:

  1. Document: Identify and record the high-level details of the system, and what needs to be accomplished.
  2. Test: Write tests that describe the desired behavior or outcome.
  3. Script: Write code to implement the processes that will produce the behavior or outcome.
  4. Version: Use version control to track changes and make collaboration easier.
  5. Continuous: Whenever changes are made, test everything. Be sure that the system as a whole still behaves properly.

Type of Testing: 1. Unit Testing The intent of unit testing is to confirm that recipes yields the expected output. Unit testing Chef cookbooks is done with ChefSpec

For example:

it “installs the httpd package” do expect(chef_run).to install_package(“httpd”) end

package “httpd” do action :install end

  1. Integration Testing The intent of integration testing is to confirm the end-to-end functionality. Integration testing Chef cookbooks is often performed in Test Kitchen.

Common Chef Testing Tools 1. RuboCop Rubocop is a Ruby command-line tool that performs lint and style checks based on the community driven Ruby Style Guide.

  1. Foodcritic Foodcritic is a Ruby command-line tool to perform lint checking against Chef cookbooks

  2. ChefSpec ChefSpec is a unit testing framework for Chef cookbooks.

  3. Serverspec Serverspec is an “outside-in” integration test framework.

  4. Test Kitchen Test Kitchen is an integration tool for developing and testing infrastructure code and software on isolated target platforms.


Coming from the development side of the software building to DevOps teams expecting every line of code should have descent amount of test around it. But Traditionally, much of infrastructure is managed. manually or using scripts. This inconsistency causes many issues in infrastructure. So just wanted to remove this inconsistency and try to follow TDI.