chef-infrachefspecdatabags

How can I test for data_bag modifications in ChefSpec?


I have a recipe that modifies a DataBag value, and I'm trying to write a test for that. The relevant part of the recipe is:

def get_deployment_data(data_bag_name)
  data_bag_item(data_bag_name, 'deployment')
end

# Update master data bag
master_deployment_data = get_deployment_data(node['data_bag']['master'])
master_deployment_data['latest_build']['number'] = latest_build_number
master_deployment_data['latest_build']['packages_path'] = latest_packages_path
master_deployment_data.save

and the test looks like this:

require 'spec_helper'

describe 'my_cookbook::configure_deployment' do
  let(:chef_runner) do
    ChefSpec::SoloRunner.new
  end

  let(:chef_node) do
    chef_runner.node
  end

  let(:chef_run) do
    chef_runner.converge(described_recipe)
  end

  context 'When all attributes are default' do
# snip #
    context 'in a specified environment' do
# snip #
      context 'with an assigned role' do
# snip #
        context 'equal to the deployment master role' do
          data_item = { 'latest_build' => {} }

          before do
            stub_data_bag_item('my_data_bag', 'deployment').and_return(data_item)
            allow_any_instance_of(Hash).to receive('save')
            chef_run
          end

# snip #

          it 'sets the master data bag build number correctly' do
            expect(data_item['latest_build']['number']).to match(/an appropriate regex/)
          end

          it 'sets the master data bag packages path correctly' do
            expect(data_item['latest_build']['packages_path'])
              .to match(/an appropriate regex/)
          end
        end
      end
    end
  end
end

Both tests fail with an error saying "expected nil to match /an appropriate regex/", so I guess something is amiss with the way I'm stubbing the Data_Bag_Item. From other tests that I've remove from the posted code I know that the code in the recipe that modifies the data bag item is actually running.

What am I missing?


Solution

  • The problem is probably that ChefSpec's stubbing system converts values into a Mash before returning, which is a copy operation. Rather than returning a hash from the stub, use a real DataBagItem instance.

    Also put the item in a let variable so it is scoped better:

    let(:data_item) do
      Chef::DataBagItem.from_hash('latest_build' => {}).tap do |item|
        expect(item).to receive(:save)
      end
    end