CL LAB

HOME > CL LAB > CookbookをTravis CIでテストする #opschef_ja

CookbookをTravis CIでテストする #opschef_ja

 ★ 6

CookbookをTravis CIでテストする #opschef_ja

Travis CIとは、SaaS型の継続的インテグレーションサービスで、GitHub上のプロジェクトのビルド・テストを行えます。

Travis CIはRubyにも対応しているため、これを利用してCookbookのテストを行うことができます。

Travis CIへの登録

Travis CIへの登録は、GitHubのアカウントを利用して行います。

詳細はTravis CI:Documentationを参照してください。

CookbookのテストをTravis CIで実施する

Travis CI上でビルド・テストを行うには、対象のプロジェクトのトップディレクトリに.travis.ymlというYAMLファイルを設置し、設定を記載します。

ここではMVT: Knife Test and Travis CIを参考に進めます。

まず、.travis.ymlを用意します。

rubyの環境はrvmを使って1.9.3を用意し、rakeコマンドからChefSpecを呼び出して実行するとします。


ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$ vi .travis.yml
language: ruby
gemfile:
- test/support/Gemfile
rvm:
- 1.9.3
script:
- bundle exec rake rspec
ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$

テストに必要なgemをインストールするためのGemfileを準備します。

なお、monetaは2013年4月23日現在、0.7系がインストールされますが、「cannot load such file -- moneta/memory」というエラーでChefSpecが動作しないため、ヴァージョンを0.6系に固定しています。

また、以前インストールできるChefSpec 0.9.0はChef 11には対応していませんでした。2013年4月23日以降はChef 11に対応したChefSpec 1.0.0がリリースされているため、そちらを使うようにヴァージョンを指定しています。


ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$ mkdir -p test/support
ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$ vi test/support/Gemfile
source 'https://rubygems.org'
gem 'rake'
gem 'moneta', '~> 0.6.0'
gem 'chefspec', '> 0.9.0'
ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$

Rakefileを準備します。


ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$ vi Rakefile
#!/usr/bin/env rake

desc 'chefspec'
task :rspec do |t|
if Gem::Version.new( '1.9.2' ) <= Gem::Version.new( RUBY_VERSION.dup ) require 'rspec/core/rake_task' RSpec::Core::RakeTask.new( :rspec ) do |t| spec_files_path = './spec/*_spec.rb' t.pattern = spec_files_path t.rspec_opts = ['-c'] end end end task :default => [ :rspec ]
ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$

これでrakeによってChefSpecを呼び出して実行可能です。

ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$ rake rspec
/usr/bin/ruby -S rspec ./spec/default_spec.rb -c
.............

Finished in 0.09303 seconds
13 examples, 0 failures
ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$

以上をGitHubにpushすることで、Travis CIへ投入され、テストが行われます。

Cookbookのテストの追加

さらにknife cookbook testfoodcriticによるテストを追加します。

.travis.ymlに実行するコマンドを追加します。


ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$ vi .travis.yml
language: ruby
gemfile:
- test/support/Gemfile
rvm:
- 1.9.3
script:
- bundle exec rake rspec
- bundle exec rake knife
- bundle exec rake foodcritic
ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$

test/support/Gemfileに必要なgemを追加します。


ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$ vi test/support/Gemfile
source 'https://rubygems.org'
gem 'rake'
gem 'moneta', '~> 0.6.0'
gem 'chefspec', '> 0.9.0'
gem 'chef'
gem 'foodcritic'
ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$

Rakefileにタスクを追加します。


ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$ vi Rakefile
#!/usr/bin/env rake

desc 'chefspec'
task :rspec do |t|
if Gem::Version.new( '1.9.2' ) <= Gem::Version.new( RUBY_VERSION.dup ) require 'rspec/core/rake_task' RSpec::Core::RakeTask.new( :rspec ) do |t| spec_files_path = './spec/*_spec.rb' t.pattern = spec_files_path t.rspec_opts = ['-c'] end end end desc 'foodcritic' task :foodcritic do |t| if Gem::Version.new( '1.9.2' ) <= Gem::Version.new( RUBY_VERSION.dup ) # "FC008: Generated cookbook metadata needs updating" is for handson sh "foodcritic -f any #{File.dirname( __FILE__ )}" else puts "WARN: foodcritic run is skipped as Ruby #{RUBY_VERSION} is < 1.9.2." end end desc 'knife cookbook test' task :knife do Rake::Task[ :prepare_sandbox ].execute sh "bundle exec knife cookbook test cookbook -c test/.chef/knife.rb -o #{sandbox_path}/../" end task :prepare_sandbox do files = %w{*.md *.rb attributes definitions files libraries providers recipes resources templates} rm_rf sandbox_path mkdir_p sandbox_path cp_r Dir.glob("{#{files.join(',')}}"), sandbox_path end private def sandbox_path File.join(File.dirname(__FILE__), %w(tmp cookbooks cookbook)) end task :default => [ :rspec, :knife, :foodcritic ]
ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$

knife cookbook testはtemplateのテストにキャッシュファイルの生成が必要になるため、テスト用のknife.rbを作成します。


ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$ mkdir -p test/.chef/
ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$ vi test/.chef/knife.rb
cache_type 'BasicFile'
cache_options(:path => "#{ENV['HOME']}/.chef/checksums")

rakeコマンドを実行すると、ChefSpec、knife cookbook test、foodcriticによるテストが行われます。

ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$ rake rspec
/usr/bin/ruby -S rspec ./spec/default_spec.rb -c
.............

Finished in 0.09312 seconds
13 examples, 0 failures
rm -rf /home/ubuntu/chef-repo/cookbooks/apache2-take/tmp/cookbooks/cookbook
mkdir -p /home/ubuntu/chef-repo/cookbooks/apache2-take/tmp/cookbooks/cookbook
cp -r CHANGELOG.md README.md metadata.rb attributes files recipes templates /home/ubuntu/chef-repo/cookbooks/apache2-take/tmp/cookbooks/cookbook
bundle exec knife cookbook test cookbook -c test/.chef/knife.rb -o /home/ubuntu/chef-repo/cookbooks/apache2-take/tmp/cookbooks/cookbook/../
checking cookbook
Running syntax check on cookbook
Validating ruby files
Validating templates
foodcritic -f any /home/ubuntu/chef-repo/cookbooks/apache2-take
FC008: Generated cookbook metadata needs updating: /home/ubuntu/chef-repo/cookbooks/apache2-take/metadata.rb:2
FC008: Generated cookbook metadata needs updating: /home/ubuntu/chef-repo/cookbooks/apache2-take/metadata.rb:3
ubuntu@kitchen:~/chef-repo/cookbooks/apache2-take$

まずはじめに、このようなテストのための設定を行っておけば、開発サイクルに効果をもたらすはずです。

CL LAB Mail Magazine

CL LABの情報を逃さずチェックしよう!

メールアドレスを登録すると記事が投稿されるとメールで通知します。

メールアドレス: 登録

※登録後メールに記載しているリンクをクリックして認証してください。

Related post

Neo4j[ホワイトペーパー]CCPA