在apache_wrapper cookbook中运行chefspec时,“找不到Cookbook apache_wrapper”

时间:2014-11-17 15:30:16

标签: rspec chef chefspec

我是厨师规范的新手,并试图弄清楚如何在我的食谱中使用单元测试。 我已经安装了chefdk(不同的ubuntu实例上的v2和v3)和刀规插件。 在" apache_wrapper"之后cookbook创建我已经更改了下一个文件:

spec / spec_helper.rb

require 'chefspec'
require 'chefspec/berkshelf'

RSpec.configure do |config|
config.log_level = :debug
  config.platform = 'ubuntu'
  config.version = '12.04'
end

规格/食谱/ default_spec.rb

require_relative '../spec_helper'

describe 'apache_wrapper::default' do
  subject { ChefSpec::Runner.new.converge(described_recipe) }

  it 'includes community cookbook apache2' do
    expect(subject).to include_recipe('apache2')
  end

  it 'creates a template with attributes' do
    expect(subject).to create_template('/var/www/html/index.html').with(
      user:   'root',
      group:  'root',
      backup: true,
    )

    expect(subject).to_not create_template('/var/www/html/index.html').with(
      user:   'bacon',
      group:  'fat',
      backup: true,
    )
  end
end

在我的食谱default.eb:

include_recipe 'apache2'

template "/var/www/html/index.html" do
  source "index.html.erb"
  mode 00644
end

但是当我调用rspec时,我接下来了:

$ pwd
/tmp/apache_wrapper
$ rspec
...
Failures:

  1) apache_wrapper::default includes community cookbook apache2
     Failure/Error: subject { ChefSpec::Runner.new.converge(described_recipe) }
     Chef::Exceptions::CookbookNotFound:
       Cookbook apache_wrapper not found. If you're loading apache_wrapper from another cookbook, make sure you configure the dependency in your metadata
     # ./spec/recipes/default_spec.rb:24:in `block (2 levels) in <top (required)>'
     # ./spec/recipes/default_spec.rb:27:in `block (2 levels) in <top (required)>'

  2) apache_wrapper::default creates a template with attributes
     Failure/Error: subject { ChefSpec::Runner.new.converge(described_recipe) }
     Chef::Exceptions::CookbookNotFound:
       Cookbook apache_wrapper not found. If you're loading apache_wrapper from another cookbook, make sure you configure the dependency in your metadata
     # ./spec/recipes/default_spec.rb:24:in `block (2 levels) in <top (required)>'
     # ./spec/recipes/default_spec.rb:31:in `block (2 levels) in <top (required)>'

Finished in 0.32355 seconds (files took 2.39 seconds to load)
2 examples, 2 failures

Failed examples:

rspec ./spec/recipes/default_spec.rb:26 # apache_wrapper::default includes community cookbook apache2
rspec ./spec/recipes/default_spec.rb:30 # apache_wrapper::default creates a template with attributes

在cookbook文件夹中出现了Berksfile.lock。 谁能告诉我我做错了什么?

UPD:

Berksfile:

source "https://supermarket.getchef.com"
cookbook 'apache2', '= 1.9.6'

Berksfile.lock

DEPENDENCIES
  apache2 (= 1.9.6)

GRAPH
  apache2 (1.9.6)
    iptables (>= 0.0.0)
    logrotate (>= 0.0.0)
    pacman (>= 0.0.0)
  iptables (0.14.0)
  logrotate (1.7.0)
  pacman (1.1.1)

UPD2:

cat ../apache_wrapper/metadata.rb | grep -E 'dep|nam'
name             'apache_wrapper'
depends 'apache2'

UPD3:

我也尝试使用下一个

let (:chef_run) { ChefSpec::ServerRunner.new.converge 'apache_wrapper::default' } 

现在收到

Missing Cookbooks:
------------------
No such cookbook: apache_wrapper

Expanded Run List:
------------------
* apache_wrapper::default

F

Failures:

  1) apache_wrapper::default includes community cookbook apache2
     Failure/Error: let (:chef_run) { ChefSpec::ServerRunner.new.converge 'apache_wrapper::default' }
     Net::HTTPServerException:
       412 "Precondition Failed "
     # ./spec/default_spec.rb:23:in `block (2 levels) in <top (required)>'
     # ./spec/default_spec.rb:26:in `block (2 levels) in <top (required)>

我不明白,我做错了什么,今天我开始了新的实例,安装了ruby2.1和所有宝石,如chefspec和其他人。现在使用rake来运行测试,但仍然得到相同的错误

解: 只需添加&#34;元数据&#34;烹饪书Berksfile

1 个答案:

答案 0 :(得分:0)

您必须指定&#34; apache2&#34;作为食谱中的依赖metadata.rb

name             'COOKBOOK NAME'
maintainer       'YOUR NAME'
maintainer_email 'YOUR_EMAIL'
...

version          '0.0.1'

depends          'apache2'

之后,您可以添加来自&#34; apache2&#34;的食谱。在你的食谱中。

<强>更新

此外,您还需要指定&#34; apache_wrapper&#34;有关cookbook的信息,请查看chefspec&#39; configuration options了解更多信息

更新2

metadata添加到您的Berks文件中,Berkshelf会将您的本地食谱添加到本地食谱列表中。

来自berkshelf.com:

  

元数据关键字就像在Bundler的Gemfile中说gemspec一样。它说,“我的Berksfile在相同的相对路径中有一个metadata.rb文件”。这使您可以解决当前正在处理的Cookbook依赖关系,就像解决当前使用Bundler的Gem的依赖关系一样。