为什么chefspec找不到chef_handler食谱?

时间:2013-08-15 19:17:37

标签: unit-testing testing chef

在尝试编写chefspec测试时,按照chefspec README(https://github.com/acrmp/chefspec)上的示例,我收到以下错误。我尝试将“depends' chef_handler'”添加到我的metadata.rb中,但没有成功:

$ 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)>'

2 个答案:

答案 0 :(得分:9)

我在尝试测试自定义Chef处理程序时遇到了同样的问题,但我试图通过ChefSpec对Berkshelf的本机支持来使用Berkshelf来提取依赖关系。这对我有用:

使用

添加spec / spec_helper.rb
require 'chefspec'
require 'chefspec/berkshelf'

使用

将.rspec文件添加到食谱项目的根目录
--color
--format progress
--require spec_helper

确保您的规范(spec / default_spec.rb)设置正确

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

在let语句之外设置ChefSpec运行器导致找不到cookbook错误。

答案 1 :(得分:1)

我最近遇到了同样的问题。因为chefspec旨在快速并且仅模拟厨师运行,所以它不会从厨师服务器克隆烹饪书。它要求chef_handler食谱需要是本地的。默认情况下,它会在与您正在测试的食谱相同的级别上查找它。

e.g。

./test_cookbook
./chef_handler