Audit with reference to node attribute array

Hello!

I’m trying to figure out how to do an audit using a reference to a node attribute that is an array and have the audit iterate through the array.

For example, my audit file would contain something like this:

default[‘cookbook’][‘packages’] = [ “sssd”, “sshd”, “nc”, “tcpdump”, “httpd” ]

Then, in my audit, I want to do something to the effect of:

it 'Verify required packages' do
  expect(package("#{node['cookbook']['packages'].each}")).to be_installed
end

Of course, this is not the correct way to do it or else I wouldn’t be asking the question. :slight_smile: But hopefully, it makes clear what I’m trying to do.

Is there a way to do this?

Thanks!!

Similarly, and I think it's the same issue, I have another recipe with a Control Group for auditing in which I want to reference a node attribute. For example:

control_group 'Check Something' do
control 'Individual Test' do
let(:var) { file("#{node['default']['filename']}" )}
end
end

You can do like this:

for unit testing

it 'installs a mypackages with the default action' do
   mypackages = %w[sssd sshd nc tcpdump httpd]
     mypackages.each do |pkg|
     expect(chef_run).to install_package(pkg)
    end
end

for integration tests

mypackages = %w[sssd sshd nc tcpdump httpd]

mypackages.each do |pkg|
  describe package pkg do
    it { should be_installed }
 end
end
1 Like