I'm learning to write integration tests for my Chef cookbooks. Is it possible to reference variables from the attributes
folder within my test?
Here's my test to make sure httpd
and php
are installed properly. However, I have additional packages I want to check for.
%w(httpd php).each do |rpm_package|
describe package(rpm_package) do
it { should be_installed }
end
end
default['ic_apachephp']['php_packages'] = [
'php-mysqlnd',
'php-mbstring',
'php-gd',
'php-xml',
'php'
]
Yes, this is possible, however not "directly". Matt Wrock described it in this blog entry.
Necessary steps are:
add a cookbook (e.g. called export-node
, below test/fixtures/cookbooks/
) that includes a recipe with the following content (dumping node attributes into the specified JSON file):
ruby_block "Save node attributes" do
block do
IO.write("/tmp/kitchen_chef_node.json", node.to_json)
end
end
add this recipe to the the run list in .kitchen.yml
load the node
object in your inspec test using
node = json('/tmp/kitchen_chef_node.json').params
Beware that you manually have to pick from the right attribute precedence level (automatic/default/normal/override), as those are not merged.
You can find an example of mine also in this cookbook: TYPO3-cookbooks/t3-pdns.
EDIT: I forgot the step to tell Berkshelf about that cookbook. Add to your Berksfile
:
group :integration do
cookbook 'export-node', path: 'test/fixtures/cookbooks/export-node'
end