私はシェフの仕様が初めてで、クックブックで単体テストを使用する方法を理解しようとしています。私はchefdk(異なるubuntuインスタンスにv2とv3)とknife specプラグインをインストールしました。「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
仕様/レシピ/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)>
何を間違っているのかわかりません。今日、新しいインスタンスを開始し、そこに ruby2.1 と、chefspec などのすべての gem をインストールしました。そして今、テストを実行するためにrakeを使用していますが、それでも同じエラーが発生します
解決策: クックブック Berksfile に「メタデータ」を追加するだけです