Chefspec runs the first portion of your Chef run, the complication phase, but not the execution phase.
Next, it tests the resulting objects against the set of objects you defined in your unit tests. If they match, you pass. The key here is that you have all of the recipe stuff accessible to you from inside your unit tests, so you can use it to simplify your life. (where applicable, last thing you want is a set of tests that never fails)
For example, in situations where I build a string and some of the attributes are from upstream cookbooks - DNS server, Bill Gates favorite color, population of Ukraine...
So, with an attribute driven cookbook, a traditional unit test looks something like this:
Attribute:
default['download_dir'] = '/var/windows/rocks'
Recipe:
directory node['download_dir'] doUnit test (traditional):
owner "root"
group "root"
mode 0755
action :create
recursive true
end
it 'create directory' do
expect(chef_run).to create_directory('/var/windows/rocks')
.with_owner('root')
.with_group('root')
.with_mode(0755)
end
Unit test (simplified):
Now if you modify the attribute your unit test will still pass. Whether you want it or not is a whole different story, but so far it's been working much better than placing an exact string in the unit test.
it 'create directory' do
expect(chef_run).to create_directory(chef_run.node['download_dir'])
.with_owner('root')
.with_group('root')
.with_mode(0755)
end
Now if you modify the attribute your unit test will still pass. Whether you want it or not is a whole different story, but so far it's been working much better than placing an exact string in the unit test.
No comments:
Post a Comment
Comments are welcomed and appreciated.