Serverspec and Data Bags


#1

Hi,

I am very new to Chef and the TDD workflow. I was able to write a simple
unit test with ChefSpec to make sure users in my users data bag get created
with some mocking using the create_data_bag inside
the ChefSpec::ServerRunner.new block. I would like to extend this check to
my Serverspec integration testing, but I am struggling with how to
stub/mock my users data bag in Serverspec. Thank you for your help and
guidance.

Jimmy


#2

Jimmy,

In Serverspec, you need to have “real” data bags to send to the node being
tested for the remote chef-client to have something to retrieve, vs
ChefSpec’s stubbing of data bags.

There’s a few ways to do it, but one we’ve adopted for the simplicity is
placing a json file:

/test/integration/data_bags//.json

Test Kitchen will then auto-load any items from that path.

If you want to change the path, you can, see here for more:
https://docs.chef.io/config_yml_kitchen.html#provisioner-specific-settings

-M

On Thu, Mar 12, 2015 at 1:58 PM, Jimmy Huang jimmy.huang@duragility.com
wrote:

Hi,

I am very new to Chef and the TDD workflow. I was able to write a simple
unit test with ChefSpec to make sure users in my users data bag get created
with some mocking using the create_data_bag inside
the ChefSpec::ServerRunner.new block. I would like to extend this check to
my Serverspec integration testing, but I am struggling with how to
stub/mock my users data bag in Serverspec. Thank you for your help and
guidance.

Jimmy


#3

you could also use chef-zero and point test-kitchen at your data_bags

.kitchen.yml

provisioner:
name: chef_zero
data_bags_path: …/…/data_bags

On Thu, Mar 12, 2015 at 12:34 PM, Mike miketheman@gmail.com wrote:

Jimmy,

In Serverspec, you need to have “real” data bags to send to the node being
tested for the remote chef-client to have something to retrieve, vs
ChefSpec’s stubbing of data bags.

There’s a few ways to do it, but one we’ve adopted for the simplicity is
placing a json file:

/test/integration/data_bags//.json

Test Kitchen will then auto-load any items from that path.

If you want to change the path, you can, see here for more:
https://docs.chef.io/config_yml_kitchen.html#provisioner-specific-settings

-M

On Thu, Mar 12, 2015 at 1:58 PM, Jimmy Huang jimmy.huang@duragility.com
wrote:

Hi,

I am very new to Chef and the TDD workflow. I was able to write a simple
unit test with ChefSpec to make sure users in my users data bag get created
with some mocking using the create_data_bag inside
the ChefSpec::ServerRunner.new block. I would like to extend this check to
my Serverspec integration testing, but I am struggling with how to
stub/mock my users data bag in Serverspec. Thank you for your help and
guidance.

Jimmy


#4

Hi Jimmy,

Check out the pattern in this cookbook for a way to handle this in Test Kitchen with Serverspec. Of particular note is the ‘data_bag_path’ line in the .kitchen.yml, and the files in tests/fixtures.

--Charles

On March 12, 2015 at 10:59:22 AM, Jimmy Huang (jimmy.huang@duragility.com) wrote:
Hi,

I am very new to Chef and the TDD workflow. I was able to write a simple unit test with ChefSpec to make sure users in my users data bag get created with some mocking using the create_data_bag inside the ChefSpec::ServerRunner.new block. I would like to extend this check to my Serverspec integration testing, but I am struggling with how to stub/mock my users data bag in Serverspec. Thank you for your help and guidance.

Jimmy


#5

For TK, you’d want the data bag on disk, but for chefspec, I don’t know how
to get that stubbed file into chefspec. You can stub the response in your
rspec directlly, though. Here’s an example from our drone-ci cookbook’s
specs:

    before do
      allow(Chef::EncryptedDataBagItem).to receive(:load).with('ci',
'config').and_return(
       '{"id": "config","client": "ABC","secret": "123"}'
      )
    end

On Thu, Mar 12, 2015 at 11:40 AM, Charles Johnson charles@chef.io wrote:

Hi Jimmy,

Check out the pattern in this cookbook for a way to handle this in Test
Kitchen with Serverspec. Of particular note is the ‘data_bag_path’ line in
the .kitchen.yml, and the files in tests/fixtures.

https://github.com/charlesjohnson/fundamentals-with-tests/tree/master/chef-repo/cookbooks/users

--Charles

On March 12, 2015 at 10:59:22 AM, Jimmy Huang (jimmy.huang@duragility.com)
wrote:

Hi,

I am very new to Chef and the TDD workflow. I was able to write a simple
unit test with ChefSpec to make sure users in my users data bag get created
with some mocking using the create_data_bag inside
the ChefSpec::ServerRunner.new block. I would like to extend this check to
my Serverspec integration testing, but I am struggling with how to
stub/mock my users data bag in Serverspec. Thank you for your help and
guidance.

Jimmy


#6

heh, and i totally misunderstood the question >__<. nm!

On Thu, Mar 12, 2015 at 11:57 AM, Nathan Williams nath.e.will@gmail.com
wrote:

For TK, you’d want the data bag on disk, but for chefspec, I don’t know
how to get that stubbed file into chefspec. You can stub the response in
your rspec directlly, though. Here’s an example from our drone-ci
cookbook’s specs:

    before do
      allow(Chef::EncryptedDataBagItem).to receive(:load).with('ci',
'config').and_return(
       '{"id": "config","client": "ABC","secret": "123"}'
      )
    end

On Thu, Mar 12, 2015 at 11:40 AM, Charles Johnson charles@chef.io wrote:

Hi Jimmy,

Check out the pattern in this cookbook for a way to handle this in Test
Kitchen with Serverspec. Of particular note is the ‘data_bag_path’ line in
the .kitchen.yml, and the files in tests/fixtures.

https://github.com/charlesjohnson/fundamentals-with-tests/tree/master/chef-repo/cookbooks/users

--Charles

On March 12, 2015 at 10:59:22 AM, Jimmy Huang (jimmy.huang@duragility.com)
wrote:

Hi,

I am very new to Chef and the TDD workflow. I was able to write a simple
unit test with ChefSpec to make sure users in my users data bag get created
with some mocking using the create_data_bag inside
the ChefSpec::ServerRunner.new block. I would like to extend this check to
my Serverspec integration testing, but I am struggling with how to
stub/mock my users data bag in Serverspec. Thank you for your help and
guidance.

Jimmy


#7

Hi Everyone,

Thank you so, so much for the amazingly fast response to my question and
supplying such clear examples. I really appreciate it. I will point my
TestKitchen to the appropriate data_bag_path. Thank you again.

Jimmy

On Thu, Mar 12, 2015 at 11:59 AM, Nathan Williams nath.e.will@gmail.com
wrote:

heh, and i totally misunderstood the question >__<. nm!

On Thu, Mar 12, 2015 at 11:57 AM, Nathan Williams nath.e.will@gmail.com
wrote:

For TK, you’d want the data bag on disk, but for chefspec, I don’t know
how to get that stubbed file into chefspec. You can stub the response in
your rspec directlly, though. Here’s an example from our drone-ci
cookbook’s specs:

    before do
      allow(Chef::EncryptedDataBagItem).to receive(:load).with('ci',
'config').and_return(
       '{"id": "config","client": "ABC","secret": "123"}'
      )
    end

On Thu, Mar 12, 2015 at 11:40 AM, Charles Johnson charles@chef.io
wrote:

Hi Jimmy,

Check out the pattern in this cookbook for a way to handle this in Test
Kitchen with Serverspec. Of particular note is the ‘data_bag_path’ line in
the .kitchen.yml, and the files in tests/fixtures.

https://github.com/charlesjohnson/fundamentals-with-tests/tree/master/chef-repo/cookbooks/users

--Charles

On March 12, 2015 at 10:59:22 AM, Jimmy Huang (
jimmy.huang@duragility.com) wrote:

Hi,

I am very new to Chef and the TDD workflow. I was able to write a
simple unit test with ChefSpec to make sure users in my users data bag get
created with some mocking using the create_data_bag inside
the ChefSpec::ServerRunner.new block. I would like to extend this check to
my Serverspec integration testing, but I am struggling with how to
stub/mock my users data bag in Serverspec. Thank you for your help and
guidance.

Jimmy


#8

Sorry to be pinging the list again, but I was actually wondering if there
are example specs that use the data bag fixtures? Thank you.

Sincerely,
Jimmy

On Thursday, March 12, 2015, Jimmy Huang jimmy.huang@duragility.com wrote:

Hi Everyone,

Thank you so, so much for the amazingly fast response to my question and
supplying such clear examples. I really appreciate it. I will point my
TestKitchen to the appropriate data_bag_path. Thank you again.

Jimmy

On Thu, Mar 12, 2015 at 11:59 AM, Nathan Williams <nath.e.will@gmail.com
<javascript:_e(%7B%7D,‘cvml’,‘nath.e.will@gmail.com’);>> wrote:

heh, and i totally misunderstood the question >__<. nm!

On Thu, Mar 12, 2015 at 11:57 AM, Nathan Williams <nath.e.will@gmail.com
<javascript:_e(%7B%7D,‘cvml’,‘nath.e.will@gmail.com’);>> wrote:

For TK, you’d want the data bag on disk, but for chefspec, I don’t know
how to get that stubbed file into chefspec. You can stub the response in
your rspec directlly, though. Here’s an example from our drone-ci
cookbook’s specs:

    before do
      allow(Chef::EncryptedDataBagItem).to receive(:load).with('ci',
'config').and_return(
       '{"id": "config","client": "ABC","secret": "123"}'
      )
    end

On Thu, Mar 12, 2015 at 11:40 AM, Charles Johnson <charles@chef.io
<javascript:_e(%7B%7D,‘cvml’,‘charles@chef.io’);>> wrote:

Hi Jimmy,

Check out the pattern in this cookbook for a way to handle this in Test
Kitchen with Serverspec. Of particular note is the ‘data_bag_path’ line in
the .kitchen.yml, and the files in tests/fixtures.

https://github.com/charlesjohnson/fundamentals-with-tests/tree/master/chef-repo/cookbooks/users

--Charles

On March 12, 2015 at 10:59:22 AM, Jimmy Huang (
jimmy.huang@duragility.com
<javascript:_e(%7B%7D,‘cvml’,‘jimmy.huang@duragility.com’);>) wrote:

Hi,

I am very new to Chef and the TDD workflow. I was able to write a
simple unit test with ChefSpec to make sure users in my users data bag get
created with some mocking using the create_data_bag inside
the ChefSpec::ServerRunner.new block. I would like to extend this check to
my Serverspec integration testing, but I am struggling with how to
stub/mock my users data bag in Serverspec. Thank you for your help and
guidance.

Jimmy


#9

Jimmy,



https://github.com/opscode-cookbooks/jenkins/blob/master/test/fixtures/cookbooks/authentication/recipes/default.rb#L8-L10

–Seth

On Sun, Mar 15, 2015 at 4:57 PM, Jimmy Huang jimmy.huang@duragility.com
wrote:

Sorry to be pinging the list again, but I was actually wondering if there
are example specs that use the data bag fixtures? Thank you.

Sincerely,
Jimmy

On Thursday, March 12, 2015, Jimmy Huang jimmy.huang@duragility.com
wrote:

Hi Everyone,

Thank you so, so much for the amazingly fast response to my question and
supplying such clear examples. I really appreciate it. I will point my
TestKitchen to the appropriate data_bag_path. Thank you again.

Jimmy

On Thu, Mar 12, 2015 at 11:59 AM, Nathan Williams nath.e.will@gmail.com
wrote:

heh, and i totally misunderstood the question >__<. nm!

On Thu, Mar 12, 2015 at 11:57 AM, Nathan Williams <nath.e.will@gmail.com

wrote:

For TK, you’d want the data bag on disk, but for chefspec, I don’t know
how to get that stubbed file into chefspec. You can stub the response in
your rspec directlly, though. Here’s an example from our drone-ci
cookbook’s specs:

    before do
      allow(Chef::EncryptedDataBagItem).to receive(:load).with('ci',
'config').and_return(
       '{"id": "config","client": "ABC","secret": "123"}'
      )
    end

On Thu, Mar 12, 2015 at 11:40 AM, Charles Johnson charles@chef.io
wrote:

Hi Jimmy,

Check out the pattern in this cookbook for a way to handle this in
Test Kitchen with Serverspec. Of particular note is the 'data_bag_path’
line in the .kitchen.yml, and the files in tests/fixtures.

https://github.com/charlesjohnson/fundamentals-with-tests/tree/master/chef-repo/cookbooks/users

--Charles

On March 12, 2015 at 10:59:22 AM, Jimmy Huang (
jimmy.huang@duragility.com) wrote:

Hi,

I am very new to Chef and the TDD workflow. I was able to write a
simple unit test with ChefSpec to make sure users in my users data bag get
created with some mocking using the create_data_bag inside
the ChefSpec::ServerRunner.new block. I would like to extend this check to
my Serverspec integration testing, but I am struggling with how to
stub/mock my users data bag in Serverspec. Thank you for your help and
guidance.

Jimmy