Steve Hayes
Steve Hayes

Reputation: 505

Why can't chefspec find the chef_handler cookbook?

in trying to write chefspec tests, following the examples on the chefspec README (https://github.com/acrmp/chefspec), I get the following error. I tried adding "depends 'chef_handler'" to my metadata.rb, without success:

$ bundle exec rspec
*[2013-08-15T11:55:01-07:00] WARN: found a directory cookbooks in the cookbook path, but it contains no cookbook files. skipping.
F*

Pending:
  example::default should include
    # Your recipe examples go here.
    # ./spec/default_spec.rb:6
  example::single_node should do something
    # Your recipe examples go here.
    # ./spec/single_node_spec.rb:5

Failures:

  1) example::default logs the foo attribute
     Failure/Error: chef_run.converge 'example::default'
     Chef::Exceptions::CookbookNotFound:
       Cookbook chef_handler not found. If you're loading chef_handler from another cookbook, make sure you configure the dependency in your metadata
     # ./spec/default_spec.rb:16:in `block (2 levels) in <top (required)>'

Upvotes: 4

Views: 1843

Answers (2)

Sneal
Sneal

Reputation: 2586

I had the same problem trying to test a custom Chef handler, but I was trying to use Berkshelf to pull dependencies down via ChefSpec's native support for Berkshelf. Here's what worked for me:

Add a spec/spec_helper.rb with

require 'chefspec'
require 'chefspec/berkshelf'

Add a .rspec file to the root of the cookbook project with

--color
--format progress
--require spec_helper

Ensure your spec (spec/default_spec.rb) is setup correctly

describe 'my_chef_handlers::default' do
  handler_path = File.join('files', 'default')

  let(:chef_run) do
    chef_runner = ChefSpec::Runner.new do |node|
      node.set['chef_handler']['handler_path'] = handler_path
      node.set['statsd']['server'] = '127.0.0.1'
    end
    chef_runner.converge 'my_chef_handlers::default'
  end

end

Setting up the ChefSpec runner outside the let statement caused cookbook not found errors.

Upvotes: 9

Precipitous
Precipitous

Reputation: 5343

I ran into the same problem recently. Because chefspec aims to be fast and only simulate chef runs, it doesn't clone cookbooks from the chef server. It requires that the chef_handler cookbook needs to be local. By default, it looks for it at the same level as the cookbook you are testing.

e.g.

./test_cookbook
./chef_handler

Upvotes: 1

Related Questions