Hi chefs, I have a question regarding using Inspec to test my cookbooks.
I have a cookbook that installs numerous Windows features. Some of which aren’t always necessary (such as management tools). So, I wanted to use an attribute so that those certain install components are easily disabled.
To verify that my recipe is working as expected I need a test for when the attribute is true and one when it is false; however, I am not able to access the node attributes from the test. I did try the JSON trick here but Inspec doesn’t let me use the conditional inside the describe block and if I define that method outside of the blocks it runs in the context of my dev workstation.
if node['clustering']['install_ps_tools']
describe windows_feature('rsat-clustering-powershell') do
it { should be_installed }
end
end
Of course, if this is bad design I’d take that criticism as well.
You can access the node from inside the describe and I would use skip which is the idiomatic rspec way for doing what you are trying to achieve. So assuming your node has a foo attribute. and you want to skip the test(s) if its value is bar, you could do this:
describe file(File.join("/tmp", "export-node", "node.json")) do
let(:parent) { File.join("/tmp", "kitchen") }
let(:node) { json(File.join(parent, "chef_node.json")).params }
before do
skip if node["normal"]["foo"] == "bar"
end
it { should be_file }
its(:content) {
should match /^mac: #{node["automatic"]["macaddress"]}$/
}
end
describe file('/u01/app/oracle/product/11.2.0.4/dbhome_1') do
let(:node) { json('/tmp/kitchen/chef_node.json').params }
before do
skip unless node['oracle']['db_version'] == "11.2.0.4"
end
it { should be_directory }
its('owner') { should eq 'oracle' }
its('group') { should eq 'oinstall' }
end
Try node['automatic']['oracle']['db_version'] keeping in mind that the json includes a level for the attribute type that the node object would conveniently hide in a recipe.
Unless there’s been a big change to InSpec in the past couple of months, I don’t see how this ever worked.
[1, 10] in test/recipes/default/default_test.rb
1: require 'byebug'
2: describe package 'nagios-plugins-minion' do
3: before do
4: byebug
=> 5: skip unless ... (redacted)
6: end
7:
8: it { should be_installed }
9: end
10:
(byebug) node
*** NameError Exception: undefined local variable or method `node' for #<RSpec::ExampleGroups::SystemPackageNagiosPluginsMinion:0x007fbbc3076440>
The node object is not accessible via the describe block.
Further, is this really how it should work? Wouldn’t it be a lot better if there was a facility to say “in this circumstance, test this” and so on?
This means that when using resources like json(’/tmp/attrs.json’) or command(‘ls /tmp’) in your tests, they will use the target node/container to read ‘/tmp/attrs.json’ or to execute the command on.
Hi
how we can skip any condition based on role attached to that node
describe service(‘xyz’) do
before do
skip if node[“roles”] == "abc"
end
it { should be_installed }
it { should be_enabled }
it { should be_running }
end
this one is giving me error that undefined function node.
describe service(‘xyz’) do
let(:node) { json('/tmp/kitchen/nodes/default-rhel-73.json').params }
before do
skip if node['roles'] == "abc"
end
it { should be_installed }
it { should be_enabled }
it { should be_running }
end
/tmp/kitchen/nodes/default-rhel-73.json change it accordingly.!!