Information Technology Reference
In-Depth Information
Test Automation with ChefSpec
Another great tool that can help you run tests early in your development cycle is
ChefSpec
.
You can even use it to catch errors
before
you code. ChefSpec can be used to produce
run-
nable documentation
. Its primary purpose is to help document and organize your code.
As a side benefit, ChefSpec tests and checks can uncover bugs when you make changes.
Plus, your Chef code will be improved when it is guided by tests.
Similar to Serverspec, ChefSpec builds on RSpec. ChefSpec uses the RSpec description form
to create runnable documentation. The form for ChefSpec documentation is slightly different
from Serverspec's, resembling the following:
describe
'<recipe_name>'
do
<perform in-memory Chef run>
<examples here>
end
For example, you would use the following
describe
block to contain examples performing
tests against the
apache-test::default
cookbook:
describe 'apache-test::default' do
...
end
To perform an in-memory Chef run, you would add the following statements to the basic
de-
scribe
form, using classes and methods from the
chefspec
gem. In this example, to test the
apache-test::default
cookbook, you would use the following code:
require 'chefspec'
describe 'apache::default' do
chef_run = ChefSpec::Runner.new.converge('apache-test::default')
<descriptions here>
end
ChefSpec uses an
expect
form similar to Serverspec's. There are just different commands
and matchers for ChefSpec. Following is a ChefSpec example that checks to make sure there
is a reference in your Chef code to install the
httpd
package: