Write RSpec examples and generate coverage reports for Chef recipes!
Write RSpec examples and generate coverage reports for Chef recipes!
ChefSpec is a unit testing framework for testing Chef cookbooks. ChefSpec makes it easy to write examples and get fast feedback on cookbook changes without the need for virtual machines or cloud servers.
ChefSpec runs your cookbooks locally while skipping making actual changes. This has two primary benefits:
ChefSpec aims to maintain compatibility with at least the two most recent minor versions of Chef. If you are running an older version of Chef it may work, or you will need to run an older version of ChefSpec.
As a general rule, if it is tested in the Travis CI matrix, it is a supported version.
As mentioned before, ChefSpec is built for speed. In order to run your tests as quickly as possible (and to allow running tests on your workstation), ChefSpec runs your recipe code with all the resource actions disabled. This means that ChefSpec excels at testing complex logic in a cookbook, but can't actually tell you if a cookbook is doing the right thing. Integration testing is provided by the Test Kitchen project, and for most simple cookbooks without much logic in them we recommend you start off with integration tests and only return to ChefSpec and unit tests as your code gets more complicated.
There are two common "units" of code in Chef cookbooks, custom resources and recipes. If you find yourself with a lot of recipes that are so complex they require unit tests, consider if they can be refactored as custom resources.
If you have have a cookbook with a custom resource resources/greet.rb like:
resource_name :mycookbook_greet
property :greeting, String, default: 'Hello'
action :run do
log "#{new_resource.greeting} world"
endYou can test that resource by creating a spec file spec/greet_spec.rb:
…And then run your test using chef exec rspec.
As a general rule of thumb, only very complex recipes benefit from ChefSpec unit
tests. If you find yourself writing a lot of recipe unit tests, consider converting
the recipes to custom resources instead. For the sake of example we'll use a
simple recipe, recipes/farewell.rb:
log "#{node["mycookbook"]["farewell"]} world"You can test that recipe by creating a spec file spec/farewell_spec.rb:
…If your cookbook depends on other cookbooks, you must ensure ChefSpec knows how
to fetch those dependencies. If you use a monorepo-style layout with all your
cookbooks in a single cookbooks/ folder, you don't need to do anything.
If you are using Berkshelf, require 'chefspec/berkshelf' in your spec file (or spec_helper.rb):
require 'chefspec'
require 'chefspec/berkshelf'If you are using a Policyfile, require 'chefspec/policyfile' in you spec file (or spec_helper.rb):
require 'chefspec'
require 'chefspec/policyfile'Your Policyfile.rb should look something like this:
# The policy name is ignored but you need to specify one.
name 'my_cookbook'
# Pull dependent cookbooks from https://supermarket.chef.io/
default_source :supermarket
# The run list is also ignored by ChefSpec but you need to specify one.
run_list 'my_cookbook::default'
# The name here must match the name in metadata.rb.
cookbook 'my_cookbook', path: '.'ChefSpec is an RSpec library, so if you're already familiar with RSpec you can
use all the normal spec-y goodness to which you are accustomed. The usual structure
of an RSpec test file is a file named like spec/something_spec.rb containing:
…The primary matcher used with ChefSpec are resource matchers:
it { expect(chef_run).to ACTION_RESOURCE('NAME') }
# Or equivalently.
it { is_expected.to ACTION_RESOURCE('NAME') }This checks that a resource like RESOURCE 'NAME' would have run the specified
action if the cookbook was executing normally. You can also test for specific
property values:
it { is_expected.to create_user('asmithee').with(uid: 512, gid: 45) }
# You can also use other RSpec matchers to create a "compound matcher". Check
# RSpec documentation for a full reference on the built-in matchers.
it { is_expected.to install_package('myapp').with(version: starts_with("3.")) }render_fileFor the common case of testing that a file is rendered to disk via either a
template, file, or cookbook_file resource, you can use a render_file
matcher:
…As actions do not normally run in ChefSpec, testing for notifications is a special case. Unlike the resource matchers which evaluate against the ChefSpec runner, the notification matchers evaluate against a resource object:
# To match `notifies :run, 'execute[unpack]', :immediately
it { expect(chef_run.remote_file('/download')).to notify('execute[unpack]') }
# To check for a specific notification action.
it { expect(chef_run.remote_file('/download')).to notify('execute[unpack]').to(:run) }
# And to check for a specific timing.
it { expect(chef_run.remote_file('/download')).to notify('execute[unpack]').to(:run).immediately }And similarly for subscriptions:
it { expect(chef_run.execute('unpack')).to subscribe_to('remote_file[/download]').on(:create) }RSpec expectations always need a value to run against, with the main value being
tested for a given example group (describe or context block) is called the
subject. In ChefSpec this is almost always ChefSpec::Runner that has converge
some recipe code.
There are two ways to set which recipe code should be run for the test. More commonly
for testing custom resources, you use the recipe helper method in the test to
provide an in-line block of recipe code:
describe 'something' do
recipe do
my_custom_resource 'something' do
debug true
end
end
endBy using an in-line block of recipe code, you can try many variations to test different configurations of your custom resource.
If no recipe block is present, ChefSpec will use the name of the top-level
describe block as a recipe name to run. So for the case of testing a recipe
in your cookbook, use the cookbookname::recipename string as the label:
describe 'mycookbook'
# Or.
describe 'mycookbook::myrecipe'Most ChefSpec configuration is set in your example groups (describe or context
blocks) using helper methods. These all follow the RSpec convention of inheriting
from a parent group to the groups inside it. So a setting in your top-level describe
will automatically be set in any context unless overridden:
describe 'something' do
platform 'ubuntu'
# Platform is Ubuntu for any tests here.
it { is_expected.to ... }
context 'when something' do
# Platform is still Ubuntu for any tests here.
end
context 'when something else' do
platform 'fedora'
# But platform here will be Fedora.
end
endTo support simulating Chef runs on the same OS as you use your cookbooks on, ChefSpec
loads pre-fabricated Ohai data from Fauxhai.
To configure which OS' data is set for your test, use the platform helper method:
describe 'something' do
platform 'ubuntu', '18.04'
# ...
endYou can specify a partial version number to get the latest version of that OS matching the provided prefix, or leave the version off entirely to get the latest version overall:
# Will use the latest RedHat 7.x.
platform 'redhat', '7'
# Will use the latest version of Windows.
platform 'windows'WARNING: If you leave off the version or use a partial version prefix, the behavior of your tests may change between versions of Chef Workstation as new data is available in Fauxhai. Only use this feature if you're certain that your tests do not (or should not) depend on the specifics of OS version.
Node attributes are set using the default_attributes, normal_attributes,
override_attributes, and automatic_attributes helper methods. These inherit
from a parent group to its children using a deep merge, like in other places in
Chef:
describe 'something' do
default_attributes['myapp']['name'] = 'one'
default_attributes['myapp']['email'] = '[email protected]'
context 'when something' do
default_attributes['myapp']['name'] = 'two'
end
endAny values set using automatic_attributes take priority over Fauxhai data.
Normally ChefSpec skips all resource (and provider) actions. When testing the implementation of a custom resource, we need to tell ChefSpec to run actions on our specific custom resource so it can be tested:
describe 'something' do
step_into :my_custom_resource
endYou can specify any other ChefSpec configuration options using the chefspec_options
helper:
describe 'something' do
chefspec_options[:log_level] = :debug
endIn order to keep unit tests fast and independent of the target system, we have to make sure that any interaction with the system (either the target node or the Chef Server, both parts of the system just in opposite directions) is replaced with a fake, local version. For some thing, like ensuring that resource actions are replaced with a no-op, the stubbing is automatic. For others, we need to tell ChefSpec how to handle things.
The most common case of interacting with the system is a guard clause on a resource:
not_if 'some command'
# Or.
only_if 'some command'In order for ChefSpec to know how to evaluate the resource, we need to tell it how the command would have returned for this test if it was running on the actual machine:
describe 'something' do
recipe do
execute '/opt/myapp/install.sh' do
# Check if myapp is installed and runnable.
not_if 'myapp --version'
end
end
before do
# Tell ChefSpec the command would have succeeded.
stub_command('myapp --version').and_return(true)
# Tell ChefSpec the command would have failed.
stub_command('myapp --version').and_return(false)
# You can also use a regexp to stub multiple commands at once.
stub_command(/^myapp/).and_return(false)
end
endIf using the Ruby code block form of a guard (e.g. not_if { something }), see
the Ruby stubbing section below.
When testing code that uses the search() API in Chef, we have to stub out the
results that would normally come from the Chef Server:
describe 'something' do
recipe do
web_servers = search(:node, 'roles:web').map { |n| n['hostname'] }
end
before do
stub_search(:node, 'roles:web').and_return([{hostname: 'one'}, {hostname: two}])
end
endWhen testing code in a library that uses Chef::Search::Query.new.search(), we have
to stub out the results that would normally come from the Chef Server:
…Similar to the Search API, the data_bag() and data_bag_item() APIs normally
fetch data from Chef Server so we need to stub their results:
describe 'something' do
recipe do
# Side note: don't write recipe code like this. This should be `search(:users, '*:*')`.
users = data_bag('users').map do |user|
data_bag_item('users', user['id'])
end
end
before do
stub_data_bag('users').and_return(['asmitheNo open issues yet, or sync has not completed.