ChefSpec runs your cookbook but without converging the node that your examples are being executed on. This allows you to write specs that make assertions about the created resources given the combination of your recipes and arbitrary node attributes that you provide.
This is alpha-quality at the moment. The API is likely to change substantially. ChefSpec currently works only with Chef 0.9.12 and has been tested on Ruby 1.8.7. It may make your machine burst into flames or install OS X Lion.
Given an existing cookbook directory structure, create a new
spec directory within it to hold your specs.
Writing a cookbook example
This is a recipe that installs the foo package, and the matching example that expresses that expectation.
package "foo" do action :install end
require "chefspec" describe "example::default" do before(:all) do @chef_run = ChefSpec::ChefRunner.new @chef_run.converge "example::default" end it "should install foo" do @chef_run.should install_package "foo" end end
Setting node attributes
ChefSpec lets you override node attributes (including OHAI attributes) as you'd expect:
it "should log the node platform" do @chef_run = ChefSpec::ChefRunner.new @chef_run.node.foo = "bar" @chef_run.converge "example::default" @chef_run.should log "The value of node.foo is: bar" end
You can use RSpec
should_not to define the expected outcome of applying your recipe using the following
Files and Directories
@chef_run.should create_file "/var/log/bar.log"
@chef_run.should delete_file "/var/log/bar.log"
@chef_run.should create_directory "/var/lib/foo"
@chef_run.should delete_directory "/var/lib/foo"
@chef_run.file("/var/log/bar.log").should be_owned_by("user", "group") @chef_run.directory("/var/lib/foo").should be_owned_by("user", "group")
Matchers for the Chef Package resource.
@chef_run.should install_package "foo" @chef_run.should install_package_at_version "foo", "1.2.3"
@chef_run.should purge_package "foo"
@chef_run.should remove_package "foo"
@chef_run.should upgrade_package "foo"
Matcher for the Chef Execute resource.
@chef_run.should execute_command "whoami"
Matcher for the Chef Log resource.
@chef_run.should log "A log message from my recipe"
Matchers for the Chef Service resource.
@chef_run.should start_service "food"
@chef_run.should set_service_to_start_on_boot "food"
@chef_run.should stop_service "food"
@chef_run.should restart_service "food"
@chef_run.should reload_service "food"
There are further examples of using ChefSpec in the
$ bundle install $ rake
MIT - see the accompanying LICENSE file for details.
Additional matchers and bugfixes are welcome! Please fork and submit a pull request on an individual branch per change.