0

I have this piece of code in my recipe now i want to write a chefspec to test the code before it is being executed on the node.

I have googled for some sample spec recipe but i couldn't find anything related to databags.

userlist = data_bag('systemuser')

userlist.each do | identifier|
  users = data_bag_item('systemuser', identifier)

  user(users['id']) do
    comment users['comment']
  end
end

I need some help on how to write spec for testing the above mentioned code.

rastasheep
  • 10,416
  • 3
  • 27
  • 37
bbsys
  • 33
  • 1
  • 8

1 Answers1

0

Without your data bag sample, here is a basic model for you to further develop upon.

require 'chefspec'

describe 'cookbookname::recipename' do
  let(:chef_run) { ChefSpec::SoloRunner.converge(described_recipe) }

  context 'when the data_bag is not stubbed' do
    it 'raises an exception' do
      expect {
        chef_run
      }.to raise_error(ChefSpec::Error::DataBagNotStubbed)
    end
  end
  context 'stub array test' do
     it 'does not raise an exception' do
       stub_data_bag('systemuser').and_return([
          { id: 1, comment: 'delicious' },
          { id: 2, comment: 'also good' }
         ])
       expect { chef_run }.to_not raise_error
     end
  end

end
display name
  • 4,165
  • 2
  • 27
  • 52