Hi @jkwysdom, welcome to the community!
The examples directory in the chefspec repo is a wealth of info for chefspec tests, which are considered a type of ‘unit’ test in Chef, with the unit being an individual resource.
For the template
resource you pasted:
template ‘/usr/local/nginx/conf/modsecurity.conf’ do
source ‘modsecurity.conf.erb’
notifies :reload, “service[nginx]”, :delayed
end
You are telling chef-client to render the modsecurity.conf
file at that path, using the template source modsecurity.conf.erb
and to send a delayed (executed at the end of the chef-client run) notification to the chef service resource named ‘nginx’ telling it to run the :reload
action.
You would need a chefspec statement like:
it 'some description of resource under test here' do
expect(chef_run).to create_template('/usr/local/nginx/conf/modsecurity.conf').with(
source 'modsecurity.conf.erb'
)
expect(chef_run).to notify('service[nginx]').to(:reload).delayed
end
There’s some more boilerplate to making this work, like ensuring chef_run
is an instance of ChefSpec::SoloRunner
or ChefSpec::ServerRunner
but you can find these examples in the repo.
I’d also encourage you to look at test-kitchen and inspec testing, which is how integration testing is performed for chef cookbooks. Test-kitchen creates temporary machines and converges your recipe, inspec verifies the resutls.
An inspec test for this resource could look like:
describe file('/usr/local/nginx/conf/modsecurity.conf') do
it { should exist }
its('mode') { should cmp '0644' }
end
You can test other properties as well, this is documented with examples on the inspec file resource page
Hope that is clear.