# Chefspec and chef-sugar

**URL:** <https://discourse.chef.io/t/chefspec-and-chef-sugar/6863>\
**Category:** Chef Infra (archive)\
**Created:** [August 7, 2015, 4:58pm UTC](https://discourse.chef.io/t/chefspec-and-chef-sugar/6863 "2015-08-07T16:58:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Givens](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/daniel_givens/32/122_2.png) [@Daniel\_Givens](https://discourse.chef.io/u/Daniel_Givens)\
**Post date:** [August 7, 2015, 4:58pm UTC](https://discourse.chef.io/t/chefspec-and-chef-sugar/6863/1 "2015-08-07T16:58:24Z")

</div>

When I test a recipe with chefspec that has ‘encrypted\_data\_bag\_item’ from chef-sugar, I get an error about the encrypted data bag secret config not being set. I’m not sure how to stub that out.

I don’t know ruby much more than what’s necessary for chef, so I’m struggling with how to stub that out. This was my attempt:

allow(Chef::Sugar::Databag).to recieve(:encrypted\_data\_bag\_item).with(‘ssl’, ‘dhparam’, ‘not\_nil’).and\_return({  
‘parameters’ =\> ‘parameters’  
})

I get the exception ‘uninitialized constant Chef::Sugar’. Any help would be very much appreciated.

Thanks!

Daniel

---

<div class="post-metadata">

**Author:** ![Yoshi\_Spendiff](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/yoshi_spendiff/32/13_2.png) [@Yoshi\_Spendiff](https://discourse.chef.io/u/Yoshi_Spendiff)\
**Post date:** [August 7, 2015, 6:02pm UTC](https://discourse.chef.io/t/chefspec-and-chef-sugar/6863/2 "2015-08-07T18:02:20Z")

</div>

This is the example on the ChefSpec page:

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

I've not actually done it, but in your case maybe something along these  
lines?

allow(Chef::EncryptedDataBagItem).to receive(:load).with('ssl',  
'dhparam', 'not\_nil').and\_return(

'parameters' =\> 'parameters'

)

On Fri, Aug 7, 2015 at 9:58 AM, Daniel Givens [daniel.givens@rackspace.com](mailto:daniel.givens@rackspace.com)  
wrote:

> When I test a recipe with chefspec that has 'encrypted\_data\_bag\_item' from  
> chef-sugar, I get an error about the encrypted data bag secret config not  
> being set. I'm not sure how to stub that out.
> 
> I don't know ruby much more than what's necessary for chef, so I'm  
> struggling with how to stub that out. This was my attempt:
> 
> allow(Chef::Sugar::Databag).to  
> recieve(:encrypted\_data\_bag\_item).with('ssl', 'dhparam',  
> 'not\_nil').and\_return({  
> 'parameters' =\> 'parameters'  
> })
> 
> I get the exception 'uninitialized constant Chef::Sugar'. Any help would  
> be very much appreciated.
> 
> Thanks!
> 
> Daniel

--  
Yoshi Spendiff  
Ops Engineer  
Indochino  
Mobile: +1 778 952 2025  
Email: [yoshi.spendiff@indochino.com](mailto:yoshi.spendiff@indochino.com)

---

<div class="post-metadata">

**Author:** ![Daniel\_Givens](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/daniel_givens/32/122_2.png) [@Daniel\_Givens](https://discourse.chef.io/u/Daniel_Givens)\
**Post date:** [August 7, 2015, 6:23pm UTC](https://discourse.chef.io/t/chefspec-and-chef-sugar/6863/3 "2015-08-07T18:23:08Z")

</div>

Yes, that works for doing it the “regular” way as I use that method where I’m using other encrypted data bags, but I’m depending on a cookbook that uses encrypted\_data\_bag\_item from chef-sugar. It relies on the encrypted\_data\_bag\_item config setting or else it throws an exception. That isn’t set when chefspec runs, so I’m thinking that I need to stub that method. I just don’t know how.

* * *

From: Yoshi Spendiff [yoshi.spendiff@indochino.com](mailto:yoshi.spendiff@indochino.com)  
Sent: Friday, August 7, 2015 1:02 PM  
To: chef  
Subject: [chef] Re: chefspec and chef-sugar

This is the example on the ChefSpec page:

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

I’ve not actually done it, but in your case maybe something along these lines?

allow(Chef::EncryptedDataBagItem).to receive(:load).with(‘ssl’, ‘dhparam’, ‘not\_nil’).and\_return(

‘parameters’ =\> ‘parameters’

)

On Fri, Aug 7, 2015 at 9:58 AM, Daniel Givens \<[daniel.givens@rackspace.com](mailto:daniel.givens@rackspace.com)[mailto:daniel.givens@rackspace.com](mailto:daniel.givens@rackspace.com)\> wrote:

When I test a recipe with chefspec that has ‘encrypted\_data\_bag\_item’ from chef-sugar, I get an error about the encrypted data bag secret config not being set. I’m not sure how to stub that out.

I don’t know ruby much more than what’s necessary for chef, so I’m struggling with how to stub that out. This was my attempt:

allow(Chef::Sugar::Databag).to recieve(:encrypted\_data\_bag\_item).with(‘ssl’, ‘dhparam’, ‘not\_nil’).and\_return({  
‘parameters’ =\> ‘parameters’  
})

I get the exception ‘uninitialized constant Chef::Sugar’. Any help would be very much appreciated.

Thanks!

Daniel

–  
Yoshi Spendiff  
Ops Engineer  
Indochino  
Mobile: +1 778 952 2025  
Email: [yoshi.spendiff@indochino.com](mailto:yoshi.spendiff@indochino.com)[mailto:yoshi.spendiff@indochino.com](mailto:yoshi.spendiff@indochino.com)

---

<div class="post-metadata">

**Author:** ![Daniel\_Givens](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/daniel_givens/32/122_2.png) [@Daniel\_Givens](https://discourse.chef.io/u/Daniel_Givens)\
**Post date:** [August 7, 2015, 6:24pm UTC](https://discourse.chef.io/t/chefspec-and-chef-sugar/6863/4 "2015-08-07T18:24:10Z")

</div>

?Correction - encrypted\_data\_bag\_secret config setting

* * *

From: Daniel Givens  
Sent: Friday, August 7, 2015 1:23 PM  
To: chef  
Subject: Re: [chef] Re: chefspec and chef-sugar

Yes, that works for doing it the “regular” way as I use that method where I’m using other encrypted data bags, but I’m depending on a cookbook that uses encrypted\_data\_bag\_item from chef-sugar. It relies on the encrypted\_data\_bag\_item config setting or else it throws an exception. That isn’t set when chefspec runs, so I’m thinking that I need to stub that method. I just don’t know how.

* * *

From: Yoshi Spendiff [yoshi.spendiff@indochino.com](mailto:yoshi.spendiff@indochino.com)  
Sent: Friday, August 7, 2015 1:02 PM  
To: chef  
Subject: [chef] Re: chefspec and chef-sugar

This is the example on the ChefSpec page:

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

I’ve not actually done it, but in your case maybe something along these lines?

allow(Chef::EncryptedDataBagItem).to receive(:load).with(‘ssl’, ‘dhparam’, ‘not\_nil’).and\_return(

‘parameters’ =\> ‘parameters’

)

On Fri, Aug 7, 2015 at 9:58 AM, Daniel Givens \<[daniel.givens@rackspace.com](mailto:daniel.givens@rackspace.com)[mailto:daniel.givens@rackspace.com](mailto:daniel.givens@rackspace.com)\> wrote:

When I test a recipe with chefspec that has ‘encrypted\_data\_bag\_item’ from chef-sugar, I get an error about the encrypted data bag secret config not being set. I’m not sure how to stub that out.

I don’t know ruby much more than what’s necessary for chef, so I’m struggling with how to stub that out. This was my attempt:

allow(Chef::Sugar::Databag).to recieve(:encrypted\_data\_bag\_item).with(‘ssl’, ‘dhparam’, ‘not\_nil’).and\_return({  
‘parameters’ =\> ‘parameters’  
})

I get the exception ‘uninitialized constant Chef::Sugar’. Any help would be very much appreciated.

Thanks!

Daniel

–  
Yoshi Spendiff  
Ops Engineer  
Indochino  
Mobile: +1 778 952 2025  
Email: [yoshi.spendiff@indochino.com](mailto:yoshi.spendiff@indochino.com)[mailto:yoshi.spendiff@indochino.com](mailto:yoshi.spendiff@indochino.com)

---

<div class="post-metadata">

**Author:** ![jeffbyrnes](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/jeffbyrnes/32/1478_2.png) [@jeffbyrnes](https://discourse.chef.io/u/jeffbyrnes)\
**Post date:** [August 10, 2015, 2:05pm UTC](https://discourse.chef.io/t/chefspec-and-chef-sugar/6863/5 "2015-08-10T14:05:32Z")

</div>

I should point out that, as of Chef 12, the `encrypted_data_bag_item` method in Chef Sugar is less useful, as the `data_bag_item` method that is built-in to Chef can now determine if a data bag item is encrypted or not, and act appropriately.

This means, for testing a cookbook, you can use plaintext versions of the data bag item(s) (i.e., in `test/integration/data_bags`), while keeping your real ones as encrypted items in your Chef Server.

–  
Jeff Byrnes  
@thejeffbyrnes  
Lead DevOps Engineer  
EverTrue  
704.516.4628

On August 7, 2015 at 2:24:26 PM, Daniel Givens ([daniel.givens@rackspace.com](mailto:daniel.givens@rackspace.com)) wrote:

​Correction - encrypted\_data\_bag\_secret config setting

From: Daniel Givens  
Sent: Friday, August 7, 2015 1:23 PM  
To: chef  
Subject: Re: [chef] Re: chefspec and chef-sugar

Yes, that works for doing it the “regular” way as I use that method where I’m using other encrypted data bags, but I’m depending on a cookbook that uses encrypted\_data\_bag\_item from chef-sugar. It relies on the encrypted\_data\_bag\_item config setting or else it throws an exception. That isn’t set when chefspec runs, so I’m thinking that I need to stub that method. I just don’t know how.

From: Yoshi Spendiff [yoshi.spendiff@indochino.com](mailto:yoshi.spendiff@indochino.com)  
Sent: Friday, August 7, 2015 1:02 PM  
To: chef  
Subject: [chef] Re: chefspec and chef-sugar

This is the example on the ChefSpec page:

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  
I’ve not actually done it, but in your case maybe something along these lines?  
allow(Chef::EncryptedDataBagItem).to receive(:load).with(‘ssl’, ‘dhparam’, ‘not\_nil’).and\_return(

‘parameters’ =\> ‘parameters’

)

On Fri, Aug 7, 2015 at 9:58 AM, Daniel Givens [daniel.givens@rackspace.com](mailto:daniel.givens@rackspace.com) wrote:  
When I test a recipe with chefspec that has ‘encrypted\_data\_bag\_item’ from chef-sugar, I get an error about the encrypted data bag secret config not being set. I’m not sure how to stub that out.

I don’t know ruby much more than what’s necessary for chef, so I’m struggling with how to stub that out. This was my attempt:

allow(Chef::Sugar::Databag).to recieve(:encrypted\_data\_bag\_item).with(‘ssl’, ‘dhparam’, ‘not\_nil’).and\_return({  
‘parameters’ =\> ‘parameters’  
})

I get the exception ‘uninitialized constant Chef::Sugar’. Any help would be very much appreciated.

Thanks!

Daniel

–  
Yoshi Spendiff  
Ops Engineer  
Indochino  
Mobile: +1 778 952 2025  
Email: [yoshi.spendiff@indochino.com](mailto:yoshi.spendiff@indochino.com)
