LoginSignup
0
0

More than 5 years have passed since last update.

Berkshelf で依存解決しつつ chespec を流す

Last updated at Posted at 2015-01-14

普通のやりかた

berkshelf 本家のやり方はこんなかんじ

spec/spec_helper.rb
# Added by ChefSpec
require 'chefspec'

# Uncomment to use ChefSpec's Berkshelf extension
require 'chefspec/berkshelf'

ただこれだと、spec を流すたびに berks vendor が走ってめっちゃ遅い。

工夫したやり方

berks install (または berks vendor) を前もって1度だけ走らせておく、という戦法。

berks install 先のディレクトリを指定するの記事に書いた Rakefile を作っておくと、rake install や rake vendor タスクが生えて、クックブックを vendor/cookbooks 以下にダウンロードできるようになる。これを使って前もって依存 cookbook をダウンロードしておく。

$ rake install

その上で、spec/spec_helper.rb をこんなかんじにする。ダウンロード先の vendor/cookbookscookbook_path に指定している。

spec/spec_helper.rb
require 'chefspec'

RSpec.configure do |config|
  config.cookbook_path = %w(vendor/cookbooks)
end

これで rspec を流せば、Berkshelf で依存解決しつつ、素早く単体テストが実行できる

$ rspec

Berksfile でローカルパスを指定している場合

次のように Berksfile でローカルパスの cookbook を指定している場合:

Berskfile
source 'https://supermarket.chef.io'

metadata
cookbook 'awesome_cookbook', path: '../awesome_cookbook'

berks install (rake install) で path 指定の cookbook は vendor/cookbooks にコピーされないので、cookbook_path に .. も含めれば良い

spec/spec_helper.rb
-   config.cookbook_path = %w(vendor/cookbooks)
+   config.cookbook_path = %w(vendor/cookbooks ..)

まとめ

vendor/bookbooks 以下に berks install する rake install タスクを作る

Rakefile
# berks install to vendor/cookbooks
desc "berks install (download cookbooks)"
task :install do
  require 'berkshelf'
  puts 'berks install to vendor/cookbooks'
  Berkshelf.ui.mute do
    ENV['BERKSHELF_PATH'] = 'vendor'
    Berkshelf::Berksfile.from_file('Berksfile').install
  end
end

spec/spec_helper.rb で cookbook_path を指定する

spec/spec_helper.rb
require 'chefspec'

RSpec.configure do |config|
  config.cookbook_path = %w(vendor/cookbooks ..)
end

これで毎回 berks vendor を走らせずに素早く単体テストを実行できる。

$ rake install # 最初の1回のみ
$ rspec
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0