Skip to content

Instantly share code, notes, and snippets.

@fernandoaleman
Last active February 28, 2021 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandoaleman/e7df780d1c2dddc4ad519db3417967c2 to your computer and use it in GitHub Desktop.
Save fernandoaleman/e7df780d1c2dddc4ad519db3417967c2 to your computer and use it in GitHub Desktop.
chefspec invalid data bag path

Problem

Using chef_vault_item in a recipe and testing via ChefSpec returns the following error:

Chef::Exceptions::InvalidDataBagPath:
       Data bag path '/var/folders/5d/46p9wf6976l9xptq3fwsrj9r0000gp/T/d20210228-64571-1lao3dr/data_bags' not found. Please create this directory.

Solution

Stub data bag and data bag item. See files below for full example.

before do
  allow(Chef::DataBag)
    .to receive(:load).with('secrets').and_return('passwords' => {})
  allow(Chef::DataBagItem)
    .to receive(:load).with('secrets', 'passwords').and_return(secret_data)

  stub_data_bag_item('secrets', 'passwords').and_return(secret_data)
end

let(:secret_data) do
  {
    'id' => 'passwords',
    'db_pass' => 'secretdbpass'
  }
end
# Set databag_fallback to true in order to use a data bag
# for testing in ChefSpec
#
node.override['chef-vault']['databag_fallback'] = true
vault = chef_vault_item('secrets', 'passwords')
file '/tmp/file' do
content "db_pass: #{vault['db_pass']}"
end
require 'spec_helper'
describe 'cookbook::default' do
platform 'ubuntu'
before do
allow(Chef::DataBag)
.to receive(:load).with('secrets').and_return('passwords' => {})
allow(Chef::DataBagItem)
.to receive(:load).with('secrets', 'passwords').and_return(secret_data)
stub_data_bag_item('secrets', 'passwords').and_return(secret_data)
end
let(:secret_data) do
{
'id' => 'passwords',
'db_pass' => 'secretdbpass'
}
end
it do
is_expected.to render_file('/tmp/file')
.with_content('db_pass: secretdbpass')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment