Reputation: 1725
I am trying to run unit tests using chefspec. I added databags to my recipe. Without databags, chefspec test running fine. After adding databags chefspec showing below error:
1) database::prerequisites installs a package
Failure/Error: let(:chef_run) { ChefSpec::ServerRunner.new(platform: 'oracle', version: '7.2').converge(described_recipe) }
Net::HTTPServerException:
404 "Not Found "
# /tmp/chefspec20170616-8187-olziw2file_cache_path/cookbooks/database/recipes/prerequisites.rb:9:in `from_file'
# ./prerequisites_spec.rb:4:in `block (2 levels) in <top (required)>'
# ./prerequisites_spec.rb:25:in `block (2 levels) in <top (required)>'
I dont know how to handle databags in chefspec. When i execute recipe using chef-client in workstation, it is working fine. but the chefspec testing is getting failed.
recipe spec file:
require 'chefspec'
describe 'database::prerequisites' do
let(:chef_run) { ChefSpec::ServerRunner.new(platform: 'oracle', version: '7.2').converge(described_recipe) }
before(:each) do
stub_command("cat /etc/oracle-release | grep 7.*").and_return(true)
end
before(:each) do
stub_command("cat /etc/oracle-release | grep 6.*").and_return(true)
end
before(:each) do
stub_command("cat /etc/selinux/config | grep SELINUX=disabled").and_return(true)
end
before(:each) do
stub_command("sestatus | grep enabled").and_return(true)
end
it 'installs a package ' do
expect(chef_run).to install_package('oracle-rdbms-server-12cR1-preinstall')
expect(chef_run).to install_package('net-tools')
expect(chef_run).to install_package('xorg-x11-apps')
end
it 'creates a directory with the default action' do
expect(chef_run).to create_directory('/u01/app/oraInventory')
expect(chef_run).to create_directory('/oradata')
end
end
recipe file:
my_secret_key = Chef::EncryptedDataBagItem.load_secret("/etc/secret_key")
passwords = Chef::EncryptedDataBagItem.load("databags", "databag_passwords", my_secret_key)
Could you please suggest how to solve this issue. I am running tests in my chef workstation only.
Upvotes: 0
Views: 834
Reputation: 54267
You need to create the data bags in the test server, https://github.com/chefspec/chefspec#dsl shows how to do this using ServerRunner
or you can use rspec-mocks and mock the load_secret
and load
methods. I prefer the latter as it is faster (in terms of test run time) and more unit-y but it's up to you.
Upvotes: 1