Skip to content

Instantly share code, notes, and snippets.

@achilles42
Last active September 3, 2016 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achilles42/6913eb13d4de9ac5d83b5b68d123a121 to your computer and use it in GitHub Desktop.
Save achilles42/6913eb13d4de9ac5d83b5b68d123a121 to your computer and use it in GitHub Desktop.

Traditionally, most of infrastructure is managed manually or by using scripts. This inconsistency causes many issues in infrastructure. TDD is known for increasing code quality, improving overall design, and allowing safe refactoring throughout a project. Explaining about how, why and which tools we should use to test code. So, I am going to explain about the 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:

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.

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

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

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment