Quoting the README of ChefSpec on this subject: (https://github.com/sethvargo/chefspec#data-bag--data-bag-item)
describe 'example::default' do
before do
allow(Chef::EncryptedDataBagItem).to receive(:load).with('users', 'svargo').and_return(...)
end
end
Seems it had to be done in the before block.
Another entry of how to: https://github.com/sethvargo/chefspec/issues/249
Hope this helps
Hello everyone,
Does anyone here use ChefSpec regularly? If so, please help!
I've got a situation where I have encrypted databags which I decrypt to find the passwords for users (in my local_users recipe). I have generated an encrypted_data_bag_secret that I use with my Chef Solo.
This is my spec file:
#local_users_spec.rb
require 'spec_helper'
describe 'core::local_users' do
let(:chef_run) { ChefSpec::SoloRunner.new.converge(described_recipe)}
context 'windows' do
let(:chef_run) do
runner = ChefSpec::SoloRunner.new(
'platform' => 'windows',
'version' => '2012'
)
runner.node.set['core']['install_flavor'] = 'windows'
runner.converge('core::local_users')
end
it 'stubbed databag does not raise error' do
plain_pass = "London2014"
allow(Chef::EncryptedDataBagItem).to receive(:load).with("aws-admin-passwords", "svc_goagent")["password"].and_return(plain_pass)
expect(chef_run).to_not raise_error
end
end
end
When I run this, I get the following:-
C:\git\cookbook-core>rspec spec/local_users_spec.rb
ffi-yajl/json_gem is deprecated, these monkeypatches will be dropped shortly
[2015-03-16T12:24:31+00:00] WARN: Failed to read the private key C:\chef\client.pem: #<Errno::ENOENT: No such file or directory - C:\chef\client.pem>
[2015-03-16T12:24:31+00:00] WARN: Can't decrypt password for svc_goagent
"Plain password for svc_goagent is "
C:/git/cookbook-core/recipes/local_users.rb:32:in `block in <top (required)>': undefined local variable or method `node' for main:Object (NameError)
from C:/git/cookbook-core/recipes/local_users.rb:22:in `each'
from C:/git/cookbook-core/recipes/local_users.rb:22:in `<top (required)>'
from C:/git/cookbook-core/spec/spec_helper.rb:8:in `require_relative'
from C:/git/cookbook-core/spec/spec_helper.rb:8:in `<top (required)>'
from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from C:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from C:/git/cookbook-core/spec/local_users_spec.rb:1:in `<top (required)>'
...
I expect the test to pass and not generate an error. However I receive an error which indicates the test is not stubbing my encrypted data bag (especially the No such file or directory - C:\chef\client.pem> errror). Can anyone help me with this?
Thanks