【问题标题】:"Cookbook apache_wrapper not found" when running chefspec in apache_wrapper cookbook在 apache_wrapper 食谱中运行 chefspec 时“未找到食谱 apache_wrapper”
【发布时间】:2015-01-14 13:33:41
【问题描述】:

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

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

spec/recipes/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

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

更新:

伯克斯文件:

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

我不明白,我做错了什么,今天我开始了新的实例,在那里安装 ruby​​2.1 和所有 gem,如 chefspec 和其他。现在使用 rake 运行测试,但仍然得到同样的错误

解决方案: 只需将“元数据”添加到食谱 Berksfile

【问题讨论】:

  • 请同时发布 Berksfile 和 Berksfile.lock
  • 尝试在 Berksfile 中添加metadata 作为第三行。此外,如果您使用的是chefspec/berkshelf 插件,则不应使用cookbooks_path

标签: rspec chef-infra chefspec


【解决方案1】:

您必须在您的食谱metadata.rb 中指定“apache2”作为依赖项:

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

version          '0.0.1'

depends          'apache2'

之后,您可以将“apache2”中的食谱包含在您的食谱中。

更新:

您还需要指定“apache_wrapper”食谱的位置,请查看 chefspec 的 configuration options 了解更多信息

更新 2

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

来自 berkshelf.com:

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

【讨论】:

  • 谢谢,但我已经设置了依赖和名称,还将 config.cookbook_path 设置为包含食谱 apache_wrapper 的文件夹
  • 您是否尝试过使用不同的跑步者运行测试,例如:`subject { ChefSpec::SoloRunner.new.converge( describe_recipe) }` 似乎 ChefSpec::SoloRuner is depricated.
  • 是的,我也尝试使用下一个let (:chef_run) { ChefSpec::ServerRunner.new.converge 'apache_wrapper::default' } ,现在收到Missing Cookbooks: ------------------ No such cookbook: apache_wrapper 412 "Precondition Failed" 我不明白,我做错了什么,今天我开始了新的实例,安装有 ruby​​2.1 和所有宝石 kice chefspec 和其他。
  • 你可以尝试将你的食谱移动到“./cookbooks”目录,并删除自定义的cookbooks_path设置,这样,你应该有“默认”的厨师目录结构。
  • 我认为我已经找到了问题,您可以尝试将您的 Berksfile 更新为像这样gist.github.com/rastasheep/e600fad6d7e37a0da90a(检查您当地食谱的路径)。之后,安装诸如“berks vendor vendor/cookbooks”之类的食谱并将 cookbook_path 设置为同一位置“vendor/cookbooks”。有了它,您将在同一个地方“供应商/食谱”上拥有所有必要的食谱。
猜你喜欢
  • 1970-01-01
  • 2016-04-26
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 2021-11-01
相关资源
最近更新 更多