4

在尝试编写 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)>'
4

2 回答 2

9

我在尝试测试自定义 Chef 处理程序时遇到了同样的问题,但我试图使用 Berkshelf 通过 ChefSpec 对 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 运行程序会导致未找到食谱错误。

于 2014-01-30T19:46:03.443 回答
1

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

例如

./test_cookbook
./chef_handler
于 2013-08-15T19:23:53.897 回答