# ChefSpec question

**URL:** <https://discourse.chef.io/t/chefspec-question/6410>\
**Category:** Chef Infra (archive)\
**Created:** [March 16, 2015, 12:52pm UTC](https://discourse.chef.io/t/chefspec-question/6410 "2015-03-16T12:52:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ANGELA\_EBIRIM](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@ANGELA\_EBIRIM](https://discourse.chef.io/u/ANGELA_EBIRIM)\
**Post date:** [March 16, 2015, 12:52pm UTC](https://discourse.chef.io/t/chefspec-question/6410/1 "2015-03-16T12:52:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tensibai](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/tensibai/32/29_2.png) [@Tensibai](https://discourse.chef.io/u/Tensibai)\
**Post date:** [March 16, 2015, 1:26pm UTC](https://discourse.chef.io/t/chefspec-question/6410/2 "2015-03-16T13:26:47Z")

</div>

Quoting the README of ChefSpec on this subject:  
([GitHub - chefspec/chefspec: Write RSpec examples and generate coverage reports for Chef recipes!](https://github.com/sethvargo/chefspec#data-bag--data-bag-item))

If you are using ENCRYPTED DATA BAG ITEMS, you'll need to dive into the  
RSpec layer and stub that class method instead:

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/chefspec/chefspec/issues/249>
>
> I can't figure out how to stub encrypted data bag items. I've tried a number of …various approaches, without success.
> 
> I'm getting different errors with different approaches:
> 
> \`\`\`
> Failure/Error: chef\_run.converge(described\_recipe)
> ...
> Failure/Error: \<something-about-key-probably-not-being-valid\>
> \`\`\`
> 
> Isn't there support for this yet? Or am I just not understanding how to use it?
> 
> Thanks.
> 
> – – –
> 
> This is what my actual impl looks like in \`mycookbook\` (which obviously has another name in real life):
> 
> recipes/default.rb:
> 
> \`\`\` ruby
> aws\_secret = node\['mycookbook'\]\['key'\];
> aws\_credentials = Chef::EncryptedDataBagItem.load("credentials", "aws", aws\_secret)
> \`\`\`
> 
> spec/default\_spec.rb:
> 
> \`\`\` ruby
> chef\_run.node.set\['s3cmd'\]\['key'\] = "mysecret"
> chef\_run.converge(described\_recipe)
> 
> \# Failed attempt 1
> Chef::EncryptedDataBagItem.any\_instance.stub(:load).and\_return(Hash.new)
> Chef::EncryptedDataBagItem.any\_instance.stub(:load).with("credentials", "aws").and\_return({"access\_key\_id" =\> "abc123", "secrect\_access\_key" =\> "secret", "gpg\_passphrase" =\> "pass"})
> 
> \# Failed attempt 2
> Chef::Recipe.any\_instance
> .stub(:data\_bag\_item)
> .and\_return(Hash.new)
> 
> Chef::Recipe.any\_instance
> .stub(:data\_bag\_item)
> .with("credentials", "aws")
> .and\_return({"access\_key\_id" =\> "abc123", "secrect\_access\_key" =\> "secret", "gpg\_passphrase" =\> "pass"})
> 
> \# Failed attempt 3
> before do
> cookbook\_root = File.expand\_path('../../../../', \_\_FILE\_\_)
> Chef::Config\[:data\_bag\_path\] = File.join(cookbook\_root, 'data\_bags')
> Chef::Config\[:encrypted\_data\_bag\_secret\] = "mysecret"
> end
> \`\`\`

Hope this helps

Le 2015-03-16 13:52, ANGELA EBIRIM a écrit :

> 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:gitcookbook-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:chefclient.pem: #\<Errno::ENOENT: No such file or directory - C:chefclient.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:chefclient.pem\> errror). Can anyone help me with this?
> 
> Thanks

---

<div class="post-metadata">

**Author:** ![ANGELA\_EBIRIM](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@ANGELA\_EBIRIM](https://discourse.chef.io/u/ANGELA_EBIRIM)\
**Post date:** [March 16, 2015, 2:47pm UTC](https://discourse.chef.io/t/chefspec-question/6410/3 "2015-03-16T14:47:49Z")

</div>

Still getting the same error which means the stub isn’t working.

thanks anyway

Sent from iCloud

On Mar 16, 2015, at 06:27 AM, Tensibai [tensibai@iabis.net](mailto:tensibai@iabis.net) wrote:

Quoting the README of ChefSpec on this subject: ([https://github.com/sethvargo/chefspec#data-bag--data-bag-item](https://github.com/sethvargo/chefspec#data-bag--data-bag-item))  
If you are using Encrypted Data Bag Items, you’ll need to dive into the RSpec layer and stub that class method instead:  
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](https://github.com/sethvargo/chefspec/issues/249)

Hope this helps  
Le 2015-03-16 13:52, ANGELA EBIRIM a écrit :  
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

---

<div class="post-metadata">

**Author:** ![Brandon\_Raabe](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/brandon_raabe/32/40_2.png) [@Brandon\_Raabe](https://discourse.chef.io/u/Brandon_Raabe)\
**Post date:** [March 16, 2015, 4:45pm UTC](https://discourse.chef.io/t/chefspec-question/6410/4 "2015-03-16T16:45:27Z")

</div>

Angela, Are you loading more than one encrypted data bag item in that  
recipe? From the backtrace, it looks like you might be iterating through a  
list of users.

On Mon, Mar 16, 2015 at 7:47 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com) wrote:

> Still getting the same error which means the stub isn't working.
> 
> thanks anyway
> 
> Sent from iCloud
> 
> On Mar 16, 2015, at 06:27 AM, Tensibai [tensibai@iabis.net](mailto:tensibai@iabis.net) wrote:
> 
> Quoting the README of ChefSpec on this subject: (  
> [GitHub - chefspec/chefspec: Write RSpec examples and generate coverage reports for Chef recipes!](https://github.com/sethvargo/chefspec#data-bag--data-bag-item))
> 
> If you are using _Encrypted Data Bag Items_, you'll need to dive into the  
> RSpec layer and stub that class method instead:
> 
> describe 'example::default' do  
> before do  
> allow(Chef::EncryptedDataBagItem).to receive(:load).with('users', 'svargo').and\_return(...)  
> endend
> 
> Seems it had to be done in the before block.  
> Another entry of how to: [Can't stub encrypted data bag items · Issue #249 · chefspec/chefspec · GitHub](https://github.com/sethvargo/chefspec/issues/249)
> 
> Hope this helps
> 
> Le 2015-03-16 13:52, ANGELA EBIRIM a écrit :
> 
> 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

---

<div class="post-metadata">

**Author:** ![ANGELA\_EBIRIM](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@ANGELA\_EBIRIM](https://discourse.chef.io/u/ANGELA_EBIRIM)\
**Post date:** [March 17, 2015, 5:09pm UTC](https://discourse.chef.io/t/chefspec-question/6410/5 "2015-03-17T17:09:29Z")

</div>

Hi Brandon,

Yes.

In the main recipe, it iterates through the userlist and decrypts each encrypted data bag.

What I would like to do is to stub the code that performs the decryption (have refactored my code so have put the decryption into a method) so essentially pass it a canned encrypted data bag and have it spit out a canned id and password. This will ensure that this error Failed to read the private key C:\chef\client.pem: #\<Errno::ENOENT: No such file or directory - C:\chef\client.pem\> doens’t arise as the method that calls the decryption will be stubbed.

Hope that helps.

Thanks  
Sent from iCloud

On Mar 16, 2015, at 09:45 AM, Brandon Raabe [brandocorp@gmail.com](mailto:brandocorp@gmail.com) wrote:

Angela, Are you loading more than one encrypted data bag item in that recipe? From the backtrace, it looks like you might be iterating through a list of users.

On Mon, Mar 16, 2015 at 7:47 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com) wrote:  
Still getting the same error which means the stub isn’t working.

thanks anyway

Sent from iCloud

On Mar 16, 2015, at 06:27 AM, Tensibai [tensibai@iabis.net](mailto:tensibai@iabis.net) wrote:

Quoting the README of ChefSpec on this subject: ([https://github.com/sethvargo/chefspec#data-bag--data-bag-item](https://github.com/sethvargo/chefspec#data-bag--data-bag-item))  
If you are using Encrypted Data Bag Items, you’ll need to dive into the RSpec layer and stub that class method instead:  
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](https://github.com/sethvargo/chefspec/issues/249)

Hope this helps  
Le 2015-03-16 13:52, ANGELA EBIRIM a écrit :  
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

---

<div class="post-metadata">

**Author:** ![Brandon\_Raabe](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/brandon_raabe/32/40_2.png) [@Brandon\_Raabe](https://discourse.chef.io/u/Brandon_Raabe)\
**Post date:** [March 17, 2015, 11:13pm UTC](https://discourse.chef.io/t/chefspec-question/6410/6 "2015-03-17T23:13:57Z")

</div>

Angela,

You'll need to stub the data bag calls for each of the users, or better  
yet, if that list of users is being provided through attributes, stub that  
attribute with a mock list of user(s). Something like this:

runner.node.set['my']['userlist'] = ['foo']

You can quickly test this is the issue by adding the following into your  
before block.

allow(Chef::EncryptedDataBagItem).to receive(:load).and\_return({})

This will stub the load method regardless of the parameters passed to the  
load method, and will return an empty hash, which you can update to your  
liking.

Thanks,

Brandon

On Tue, Mar 17, 2015 at 10:09 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com) wrote:

> Hi Brandon,
> 
> Yes.
> 
> In the main recipe, it iterates through the userlist and decrypts each  
> encrypted data bag.
> 
> What I would like to do is to stub the code that performs the decryption  
> (have refactored my code so have put the decryption into a method) so  
> essentially pass it a canned encrypted data bag and have it spit out a  
> canned id and password. This will ensure that this error Failed to read  
> the private key C:\chef\client.pem: #\<Errno::ENOENT: No such file or  
> directory - C:\chef\client.pem\> doens't arise as the method that calls the  
> decryption will be stubbed.
> 
> Hope that helps.
> 
> Thanks
> 
> Sent from iCloud
> 
> On Mar 16, 2015, at 09:45 AM, Brandon Raabe [brandocorp@gmail.com](mailto:brandocorp@gmail.com) wrote:
> 
> Angela, Are you loading more than one encrypted data bag item in that  
> recipe? From the backtrace, it looks like you might be iterating through a  
> list of users.
> 
> On Mon, Mar 16, 2015 at 7:47 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com) wrote:
> 
> > Still getting the same error which means the stub isn't working.
> > 
> > thanks anyway
> > 
> > Sent from iCloud
> > 
> > On Mar 16, 2015, at 06:27 AM, Tensibai [tensibai@iabis.net](mailto:tensibai@iabis.net) wrote:
> > 
> > Quoting the README of ChefSpec on this subject: (  
> > [GitHub - chefspec/chefspec: Write RSpec examples and generate coverage reports for Chef recipes!](https://github.com/sethvargo/chefspec#data-bag--data-bag-item))
> > 
> > If you are using _Encrypted Data Bag Items_, you'll need to dive into  
> > the RSpec layer and stub that class method instead:
> > 
> > describe 'example::default' do  
> > before do  
> > allow(Chef::EncryptedDataBagItem).to receive(:load).with('users', 'svargo').and\_return(...)  
> > endend
> > 
> > Seems it had to be done in the before block.  
> > Another entry of how to: [Can't stub encrypted data bag items · Issue #249 · chefspec/chefspec · GitHub](https://github.com/sethvargo/chefspec/issues/249)
> > 
> > Hope this helps
> > 
> > Le 2015-03-16 13:52, ANGELA EBIRIM a écrit :
> > 
> > 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

---

<div class="post-metadata">

**Author:** ![ANGELA\_EBIRIM](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@ANGELA\_EBIRIM](https://discourse.chef.io/u/ANGELA_EBIRIM)\
**Post date:** [March 18, 2015, 5:29pm UTC](https://discourse.chef.io/t/chefspec-question/6410/7 "2015-03-18T17:29:12Z")

</div>

Hi Brandon,

This is so frustrating.

I’ve been trying all afternoon to successfully stub the Chef::EncryptedDataBagItem.load method without success!

This is my test file:

describe ‘core::local\_users\_linux’ do

```
let(:chef_run) do
	ChefSpec::SoloRunner.new do |node|
		node.set['core']['userlist'] = 'angela'
	 end.converge(described_recipe)	
	end 

before do
	allow(Chef::EncryptedDataBagItem).to receive(:load).and_return({
            "id" => "angela",
            "password" => "London2014"
			})
end	  
	context 'linux' do
	
       it 'stubbed databag does not raise error' do

       	expect(chef_run).to_not raise_error
       end
	end

```

end

When I test this file, I get the following error:-  
F

Failures:

1. core::local\_users\_linux linux stubbed databag does not raise error  
Failure/Error: expect(chef\_run).to\_not raise\_error  
expected no Exception but was not given a block
# ./spec/local\_users\_linux\_spec.rb:27:in `block (3 levels) in \<top (required)\>’

Finished in 11.94 seconds (files took 52.17 seconds to load)  
1 example, 1 failure

Failed examples:

rspec ./spec/local\_users\_linux\_spec.rb:25 # core::local\_users\_linux linux stubbed databag does not raise error

ChefSpec Coverage report generated…

Total Resources: 12  
Touched Resources: 0  
Touch Coverage: 0.0%

Untouched Resources:

chef\_gem[ruby-shadow] C:0  
package[whois] C:0  
package[members] C:0  
group[sysadmin] C:0  
group[sudo] C:0  
user[svc\_goagent] C:0  
users\_manage[sudo] C:0  
template[/etc/sudoers.d/svc\_goagent.conf] C:0  
user[jg-admin] C:0  
template[/etc/sudoers.d/jg-admin.conf] C:0  
user[svc\_appinfo] C:0  
template[/etc/sudoers.d/svc\_appinfo.conf] C:0

Should it be trying to run the load method in my recipe?. Shouldn’t that be stubbed and shouldn’t the test be returning my canned hash?

Please help!

Angela

Sent from iCloud

On Mar 17, 2015, at 04:14 PM, Brandon Raabe [brandocorp@gmail.com](mailto:brandocorp@gmail.com) wrote:

Angela,

You’ll need to stub the data bag calls for each of the users, or better yet, if that list of users is being provided through attributes, stub that attribute with a mock list of user(s). Something like this:

runner.node.set[‘my’][‘userlist’] = [‘foo’]

You can quickly test this is the issue by adding the following into your before block.

allow(Chef::EncryptedDataBagItem).to receive(:load).and\_return({})

This will stub the load method regardless of the parameters passed to the load method, and will return an empty hash, which you can update to your liking.

Thanks,

Brandon

On Tue, Mar 17, 2015 at 10:09 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com) wrote:  
Hi Brandon,

Yes.

In the main recipe, it iterates through the userlist and decrypts each encrypted data bag.

What I would like to do is to stub the code that performs the decryption (have refactored my code so have put the decryption into a method) so essentially pass it a canned encrypted data bag and have it spit out a canned id and password. This will ensure that this error Failed to read the private key C:\chef\client.pem: #\<Errno::ENOENT: No such file or directory - C:\chef\client.pem\> doens’t arise as the method that calls the decryption will be stubbed.

Hope that helps.

Thanks  
Sent from iCloud

On Mar 16, 2015, at 09:45 AM, Brandon Raabe [brandocorp@gmail.com](mailto:brandocorp@gmail.com) wrote:

Angela, Are you loading more than one encrypted data bag item in that recipe? From the backtrace, it looks like you might be iterating through a list of users.

On Mon, Mar 16, 2015 at 7:47 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com) wrote:  
Still getting the same error which means the stub isn’t working.

thanks anyway

Sent from iCloud

On Mar 16, 2015, at 06:27 AM, Tensibai [tensibai@iabis.net](mailto:tensibai@iabis.net) wrote:

Quoting the README of ChefSpec on this subject: ([https://github.com/sethvargo/chefspec#data-bag--data-bag-item](https://github.com/sethvargo/chefspec#data-bag--data-bag-item))  
If you are using Encrypted Data Bag Items, you’ll need to dive into the RSpec layer and stub that class method instead:  
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](https://github.com/sethvargo/chefspec/issues/249)

Hope this helps  
Le 2015-03-16 13:52, ANGELA EBIRIM a écrit :  
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

---

<div class="post-metadata">

**Author:** ![AJ\_Christensen2](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/aj_christensen2/32/42_2.png) [@AJ\_Christensen2](https://discourse.chef.io/u/AJ_Christensen2)\
**Post date:** [March 18, 2015, 6:26pm UTC](https://discourse.chef.io/t/chefspec-question/6410/8 "2015-03-18T18:26:38Z")

</div>

Yo,

Try this bad boy, hinted at by the error message in your example: (You  
didn't pass a block):  
expect { chef\_run }.to raise\_error(SomeErrorClass)

> <https://github.com/chefspec/chefspec/blob/main/examples/expect_exception/spec/compile_error_spec.rb>

> <https://github.com/chefspec/chefspec/blob/main/examples/expect_exception/spec/converge_error_spec.rb>

> <https://github.com/chefspec/chefspec/blob/main/examples/expect_exception/spec/no_error_spec.rb>

when in doubt, peruse the chefspec examples - they are organized very  
intuitively.

cheers,

--aj

On Thu, Mar 19, 2015 at 6:29 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com) wrote:

> Hi Brandon,
> 
> This is so frustrating.
> 
> I've been trying all afternoon to successfully stub the  
> Chef::EncryptedDataBagItem.load method without success!
> 
> This is my test file:
> 
> describe 'core::local\_users\_linux' do
> 
> let(:chef\_run) do  
> ChefSpec::SoloRunner.new do |node|  
> node.set['core']['userlist'] = 'angela'  
> end.converge(described\_recipe)  
> end
> 
> before do  
> allow(Chef::EncryptedDataBagItem).to receive(:load).and\_return({  
> "id" =\> "angela",  
> "password" =\> "London2014"  
> })  
> end  
> context 'linux' do  
> it 'stubbed databag does not raise error' do
> 
> ```
> expect(chef_run).to_not raise_error
> end
> 
> ```
> 
> end  
> end
> 
> When I test this file, I get the following error:-  
> F
> 
> Failures:
> 
> 1. core::local\_users\_linux linux stubbed databag does not raise error  
> Failure/Error: expect(chef\_run).to\_not raise\_error  
> expected no Exception but was not given a block
> # ./spec/local\_users\_linux\_spec.rb:27:in `block (3 levels) in \<top
> 
> (required)\>'
> 
> Finished in 11.94 seconds (files took 52.17 seconds to load)  
> 1 example, 1 failure
> 
> Failed examples:
> 
> rspec ./spec/local\_users\_linux\_spec.rb:25 # core::local\_users\_linux linux  
> stubbed databag does not raise error
> 
> ChefSpec Coverage report generated...
> 
> Total Resources: 12  
> Touched Resources: 0  
> Touch Coverage: 0.0%
> 
> Untouched Resources:
> 
> chef\_gem[ruby-shadow] C:0  
> package[whois] C:0  
> package[members] C:0  
> group[sysadmin] C:0  
> group[sudo] C:0  
> user[svc\_goagent] C:0  
> users\_manage[sudo] C:0  
> template[/etc/sudoers.d/svc\_goagent.conf] C:0  
> user[jg-admin] C:0  
> template[/etc/sudoers.d/jg-admin.conf] C:0  
> user[svc\_appinfo] C:0  
> template[/etc/sudoers.d/svc\_appinfo.conf] C:0
> 
> Should it be trying to run the load method in my recipe?. Shouldn't that be  
> stubbed and shouldn't the test be returning my canned hash?
> 
> Please help!
> 
> Angela
> 
> Sent from iCloud
> 
> On Mar 17, 2015, at 04:14 PM, Brandon Raabe [brandocorp@gmail.com](mailto:brandocorp@gmail.com) wrote:
> 
> Angela,
> 
> You'll need to stub the data bag calls for each of the users, or better yet,  
> if that list of users is being provided through attributes, stub that  
> attribute with a mock list of user(s). Something like this:
> 
> runner.node.set['my']['userlist'] = ['foo']
> 
> You can quickly test this is the issue by adding the following into your  
> before block.
> 
> allow(Chef::EncryptedDataBagItem).to receive(:load).and\_return({})
> 
> This will stub the load method regardless of the parameters passed to the  
> load method, and will return an empty hash, which you can update to your  
> liking.
> 
> Thanks,
> 
> Brandon
> 
> On Tue, Mar 17, 2015 at 10:09 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com) wrote:
> 
> > Hi Brandon,
> > 
> > Yes.
> > 
> > In the main recipe, it iterates through the userlist and decrypts each  
> > encrypted data bag.
> > 
> > What I would like to do is to stub the code that performs the decryption  
> > (have refactored my code so have put the decryption into a method) so  
> > essentially pass it a canned encrypted data bag and have it spit out a  
> > canned id and password. This will ensure that this error Failed to read the  
> > private key C:\chef\client.pem: #\<Errno::ENOENT: No such file or directory -  
> > C:\chef\client.pem\> doens't arise as the method that calls the decryption  
> > will be stubbed.
> > 
> > Hope that helps.
> > 
> > Thanks
> > 
> > Sent from iCloud
> > 
> > On Mar 16, 2015, at 09:45 AM, Brandon Raabe [brandocorp@gmail.com](mailto:brandocorp@gmail.com) wrote:
> > 
> > Angela, Are you loading more than one encrypted data bag item in that  
> > recipe? From the backtrace, it looks like you might be iterating through a  
> > list of users.
> > 
> > On Mon, Mar 16, 2015 at 7:47 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com) wrote:
> > 
> > > Still getting the same error which means the stub isn't working.
> > > 
> > > thanks anyway
> > > 
> > > Sent from iCloud
> > > 
> > > On Mar 16, 2015, at 06:27 AM, Tensibai [tensibai@iabis.net](mailto:tensibai@iabis.net) wrote:
> > > 
> > > Quoting the README of ChefSpec on this subject:  
> > > ([GitHub - chefspec/chefspec: Write RSpec examples and generate coverage reports for Chef recipes!](https://github.com/sethvargo/chefspec#data-bag--data-bag-item))
> > > 
> > > If you are using Encrypted Data Bag Items, you'll need to dive into the  
> > > RSpec layer and stub that class method instead:
> > > 
> > > 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: [Can't stub encrypted data bag items · Issue #249 · chefspec/chefspec · GitHub](https://github.com/sethvargo/chefspec/issues/249)
> > > 
> > > Hope this helps
> > > 
> > > Le 2015-03-16 13:52, ANGELA EBIRIM a écrit :
> > > 
> > > 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

---

<div class="post-metadata">

**Author:** ![holoway](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/holoway/32/44_2.png) [@holoway](https://discourse.chef.io/u/holoway)\
**Post date:** [March 18, 2015, 6:40pm UTC](https://discourse.chef.io/t/chefspec-question/6410/9 "2015-03-18T18:40:14Z")

</div>

So, you're going to hate this, but it's true. Right now, because of the way  
Chef loads things under the hood, ChefSpec appears to not be able to mock  
class methods at all. Instead, try mocking with any\_instance\_of, and see  
how that works for you.

On Wed, Mar 18, 2015 at 10:29 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com) wrote:

> Hi Brandon,
> 
> This is so frustrating.
> 
> I've been trying all afternoon to successfully stub the  
> Chef::EncryptedDataBagItem.load method without success!
> 
> This is my test file:
> 
> describe 'core::local\_users\_linux' do
> 
> let(:chef\_run) do  
> ChefSpec::SoloRunner.new do |node|  
> node.set['core']['userlist'] = 'angela'  
> end.converge(described\_recipe)  
> end
> 
> before do  
> allow(Chef::EncryptedDataBagItem).to receive(:load).and\_return({  
> "id" =\> "angela",  
> "password" =\> "London2014"  
> })  
> end  
> context 'linux' do  
> it 'stubbed databag does not raise error' do
> 
> ```
> expect(chef_run).to_not raise_error
> end
> 
> ```
> 
> end  
> end
> 
> When I test this file, I get the following error:-  
> F
> 
> Failures:
> 
> 1. core::local\_users\_linux linux stubbed databag does not raise error  
> Failure/Error: expect(chef\_run).to\_not raise\_error  
> expected no Exception but was not given a block
> # ./spec/local\_users\_linux\_spec.rb:27:in `block (3 levels) in \<top
> 
> (required)\>'
> 
> Finished in 11.94 seconds (files took 52.17 seconds to load)  
> 1 example, 1 failure
> 
> Failed examples:
> 
> rspec ./spec/local\_users\_linux\_spec.rb:25 # core::local\_users\_linux linux  
> stubbed databag does not raise error
> 
> ChefSpec Coverage report generated...
> 
> Total Resources: 12  
> Touched Resources: 0  
> Touch Coverage: 0.0%
> 
> Untouched Resources:
> 
> chef\_gem[ruby-shadow] C:0  
> package[whois] C:0  
> package[members] C:0  
> group[sysadmin] C:0  
> group[sudo] C:0  
> user[svc\_goagent] C:0  
> users\_manage[sudo] C:0  
> template[/etc/sudoers.d/svc\_goagent.conf] C:0  
> user[jg-admin] C:0  
> template[/etc/sudoers.d/jg-admin.conf] C:0  
> user[svc\_appinfo] C:0  
> template[/etc/sudoers.d/svc\_appinfo.conf] C:0
> 
> Should it be trying to run the load method in my recipe?. Shouldn't that  
> be stubbed and shouldn't the test be returning my canned hash?
> 
> Please help!
> 
> Angela
> 
> Sent from iCloud
> 
> On Mar 17, 2015, at 04:14 PM, Brandon Raabe [brandocorp@gmail.com](mailto:brandocorp@gmail.com) wrote:
> 
> Angela,
> 
> You'll need to stub the data bag calls for each of the users, or better  
> yet, if that list of users is being provided through attributes, stub that  
> attribute with a mock list of user(s). Something like this:
> 
> runner.node.set['my']['userlist'] = ['foo']
> 
> You can quickly test this is the issue by adding the following into your  
> before block.
> 
> allow(Chef::EncryptedDataBagItem).to receive(:load).and\_return({})
> 
> This will stub the load method regardless of the parameters passed to the  
> load method, and will return an empty hash, which you can update to your  
> liking.
> 
> Thanks,
> 
> Brandon
> 
> On Tue, Mar 17, 2015 at 10:09 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com)  
> wrote:
> 
> > Hi Brandon,
> > 
> > Yes.
> > 
> > In the main recipe, it iterates through the userlist and decrypts each  
> > encrypted data bag.
> > 
> > What I would like to do is to stub the code that performs the decryption  
> > (have refactored my code so have put the decryption into a method) so  
> > essentially pass it a canned encrypted data bag and have it spit out a  
> > canned id and password. This will ensure that this error Failed to read  
> > the private key C:\chef\client.pem: #\<Errno::ENOENT: No such file or  
> > directory - C:\chef\client.pem\> doens't arise as the method that calls the  
> > decryption will be stubbed.
> > 
> > Hope that helps.
> > 
> > Thanks
> > 
> > Sent from iCloud
> > 
> > On Mar 16, 2015, at 09:45 AM, Brandon Raabe [brandocorp@gmail.com](mailto:brandocorp@gmail.com) wrote:
> > 
> > Angela, Are you loading more than one encrypted data bag item in that  
> > recipe? From the backtrace, it looks like you might be iterating through a  
> > list of users.
> > 
> > On Mon, Mar 16, 2015 at 7:47 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com)  
> > wrote:
> > 
> > > Still getting the same error which means the stub isn't working.
> > > 
> > > thanks anyway
> > > 
> > > Sent from iCloud
> > > 
> > > On Mar 16, 2015, at 06:27 AM, Tensibai [tensibai@iabis.net](mailto:tensibai@iabis.net) wrote:
> > > 
> > > Quoting the README of ChefSpec on this subject: (  
> > > [GitHub - chefspec/chefspec: Write RSpec examples and generate coverage reports for Chef recipes!](https://github.com/sethvargo/chefspec#data-bag--data-bag-item))
> > > 
> > > If you are using _Encrypted Data Bag Items_, you'll need to dive into  
> > > the RSpec layer and stub that class method instead:
> > > 
> > > describe 'example::default' do  
> > > before do  
> > > allow(Chef::EncryptedDataBagItem).to receive(:load).with('users', 'svargo').and\_return(...)  
> > > endend
> > > 
> > > Seems it had to be done in the before block.  
> > > Another entry of how to: [Can't stub encrypted data bag items · Issue #249 · chefspec/chefspec · GitHub](https://github.com/sethvargo/chefspec/issues/249)
> > > 
> > > Hope this helps
> > > 
> > > Le 2015-03-16 13:52, ANGELA EBIRIM a écrit :
> > > 
> > > 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

---

<div class="post-metadata">

**Author:** ![ANGELA\_EBIRIM](https://avatars.discourse-cdn.com/v4/letter/a/e5b9ba/32.png) [@ANGELA\_EBIRIM](https://discourse.chef.io/u/ANGELA_EBIRIM)\
**Post date:** [March 18, 2015, 6:52pm UTC](https://discourse.chef.io/t/chefspec-question/6410/10 "2015-03-18T18:52:52Z")

</div>

Adam,

So you have come across it too!

Grrrr!

I will try your suggestion.

Thanks!

Sent from iCloud

On Mar 18, 2015, at 11:40 AM, Adam Jacob [adam@chef.io](mailto:adam@chef.io) wrote:

So, you’re going to hate this, but it’s true. Right now, because of the way Chef loads things under the hood, ChefSpec appears to not be able to mock class methods at all. Instead, try mocking with any\_instance\_of, and see how that works for you.

On Wed, Mar 18, 2015 at 10:29 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com) wrote:  
Hi Brandon,

This is so frustrating.

I’ve been trying all afternoon to successfully stub the Chef::EncryptedDataBagItem.load method without success!

This is my test file:

describe ‘core::local\_users\_linux’ do

let(:chef\_run) do  
ChefSpec::SoloRunner.new do |node|  
node.set[‘core’][‘userlist’] = 'angela’  
end.converge(described\_recipe)  
end

before do  
allow(Chef::EncryptedDataBagItem).to receive(:load).and\_return({  
“id” =\> “angela”,  
“password” =\> “London2014”  
})  
end  
context ‘linux’ do

```
  it 'stubbed databag does not raise error' do

 expect(chef_run).to_not raise_error
  end

```

end

end

When I test this file, I get the following error:-  
F

Failures:

1. core::local\_users\_linux linux stubbed databag does not raise error  
Failure/Error: expect(chef\_run).to\_not raise\_error  
expected no Exception but was not given a block
# ./spec/local\_users\_linux\_spec.rb:27:in `block (3 levels) in \<top (required)\>’

Finished in 11.94 seconds (files took 52.17 seconds to load)  
1 example, 1 failure

Failed examples:

rspec ./spec/local\_users\_linux\_spec.rb:25 # core::local\_users\_linux linux stubbed databag does not raise error

ChefSpec Coverage report generated…

Total Resources: 12  
Touched Resources: 0  
Touch Coverage: 0.0%

Untouched Resources:

chef\_gem[ruby-shadow] C:0  
package[whois] C:0  
package[members] C:0  
group[sysadmin] C:0  
group[sudo] C:0  
user[svc\_goagent] C:0  
users\_manage[sudo] C:0  
template[/etc/sudoers.d/svc\_goagent.conf] C:0  
user[jg-admin] C:0  
template[/etc/sudoers.d/jg-admin.conf] C:0  
user[svc\_appinfo] C:0  
template[/etc/sudoers.d/svc\_appinfo.conf] C:0

Should it be trying to run the load method in my recipe?. Shouldn’t that be stubbed and shouldn’t the test be returning my canned hash?

Please help!

Angela

Sent from iCloud

On Mar 17, 2015, at 04:14 PM, Brandon Raabe [brandocorp@gmail.com](mailto:brandocorp@gmail.com) wrote:

Angela,

You’ll need to stub the data bag calls for each of the users, or better yet, if that list of users is being provided through attributes, stub that attribute with a mock list of user(s). Something like this:

runner.node.set[‘my’][‘userlist’] = [‘foo’]

You can quickly test this is the issue by adding the following into your before block.

allow(Chef::EncryptedDataBagItem).to receive(:load).and\_return({})

This will stub the load method regardless of the parameters passed to the load method, and will return an empty hash, which you can update to your liking.

Thanks,

Brandon

On Tue, Mar 17, 2015 at 10:09 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com) wrote:  
Hi Brandon,

Yes.

In the main recipe, it iterates through the userlist and decrypts each encrypted data bag.

What I would like to do is to stub the code that performs the decryption (have refactored my code so have put the decryption into a method) so essentially pass it a canned encrypted data bag and have it spit out a canned id and password. This will ensure that this error Failed to read the private key C:\chef\client.pem: #\<Errno::ENOENT: No such file or directory - C:\chef\client.pem\> doens’t arise as the method that calls the decryption will be stubbed.

Hope that helps.

Thanks  
Sent from iCloud

On Mar 16, 2015, at 09:45 AM, Brandon Raabe [brandocorp@gmail.com](mailto:brandocorp@gmail.com) wrote:

Angela, Are you loading more than one encrypted data bag item in that recipe? From the backtrace, it looks like you might be iterating through a list of users.

On Mon, Mar 16, 2015 at 7:47 AM, ANGELA EBIRIM [aebirim@icloud.com](mailto:aebirim@icloud.com) wrote:  
Still getting the same error which means the stub isn’t working.

thanks anyway

Sent from iCloud

On Mar 16, 2015, at 06:27 AM, Tensibai [tensibai@iabis.net](mailto:tensibai@iabis.net) wrote:

Quoting the README of ChefSpec on this subject: ([https://github.com/sethvargo/chefspec#data-bag--data-bag-item](https://github.com/sethvargo/chefspec#data-bag--data-bag-item))  
If you are using Encrypted Data Bag Items, you’ll need to dive into the RSpec layer and stub that class method instead:  
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](https://github.com/sethvargo/chefspec/issues/249)

Hope this helps  
Le 2015-03-16 13:52, ANGELA EBIRIM a écrit :  
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
