[RESOLVED] ChefSpec & data_bags


#1

Hello guys,

I have an admins data_bag with admin1 user:
{
"id": "admin1",
"password": "P@ssw0rd"
}

I have the following recipe:
admins = data_bag('admins')
admins.each do |login|
admin = data_bag_item('admins', login)

user(login) do
password admin['password']
end
end

The integration tests are working, the user is created when chef converges.

The problem is that I don't know how to make my unit tests pass.
require 'spec_helper'

describe 'services::users' do
context 'When all attributes are default' do
let(:chef_run) do
runner = ChefSpec::ServerRunner.new
runner.converge(described_recipe)
end

before do
stub_data_bag('admins').and_return(['admin1'])
stub_data_bag_item('admins', 'admin1').and_return(
id: 'admin1',
password: 'P@ssw0rd'
)
end

it 'creates admin user' do
expect(chef_run).to create_user('admin1')
end
end
end

When I run chef exec rspec I receive the following error:
2018-10-17T15:08:20+03:00] ERROR: Failed to list data bag items in data bag: "admins"

================================================================================
Recipe Compile Error in /tmp/chefspec20181017-32131-4rx22sfile_cache_path/cookbooks/services/recipes/users.rb
================================================================================

Net::HTTPServerException
------------------------
404 "Not Found "

Can you help? Thank you.


#2

Solved.

let(:chef_run) do
  runner = ChefSpec::ServerRunner.new do |node, server|
    server.create_data_bag('my_data_bag',
      'item_1' => {
        password: 'abc123'
      }
                                          )
    server.create_data_bag('my_data_bag_2',
      item_2' => {
        password: 'def456'
      }
                                          )
  runner.converge(described_recipe)
end