Test-driven development for cookbooks using ChefSpec
Test-driven development (TDD) is a way to write unit tests before writing any recipe code. By writing the test first, you design what your recipe should do and ensure that your test is for real because it should fail, as long as you haven't written your recipe code.
As soon as you've completed your recipe, your unit tests should pass.
ChefSpec is built on the popular RSpec framework and offers a tailored syntax to test Chef recipes.
Let's develop a very simple recipe using the TDD approach with ChefSpec.
Getting ready
Make sure you have a cookbook called my_cookbook
and run_list
of your node includes my_cookbook
, as described in the Creating and using cookbooks recipe in Chapter 1, Chef Infrastructure.
How to do it...
Let's write a failing test first and then a recipe, which will pass the test:
- Create the
spec
directory for your cookbook:mma@laptop:~/chef-repo $ mkdir cookbooks/my_cookbook/spec
- Create your
spec
file:mma@laptop...