busser-serverspec とは #opschef_ja
この記事は1年以上前に投稿されました。情報が古い可能性がありますので、ご注意ください。
Busser::RunnerPlugin::Serverspec(以下 busser-serverspec)とは、テストフレームワークBusserのserverspecプラグインです。
Cookbookテストフレームワークtest-kitchen 1.0にて、serverspecを用いたテストを行えます。
serverspecの適用
まず、作業マシンにてRubyGemsでserverspecをインストールしてください。
% gem install serverspec --no-rdoc --no-ri Fetching: serverspec-0.5.0.gem (100%) Successfully installed serverspec-0.5.0 1 gem installed %
適用したいCookbookの test/integration/default/ ディレクトリに移動してください。
% cd apache2-take/test/integration/default %
serverspec-initコマンドを実行し、テストケース雛形を生成します。
% serverspec-init Select a backend type: 1) SSH 2) Exec (local) Select number: 2 + spec/ + spec/localhost/ + spec/localhost/httpd_spec.rb + spec/spec_helper.rb + Rakefile %
backend typeは2を選択してください。
また、Rakefileは不要なので削除し、specディレクトリはserverspecとリネームしてください。
% rm Rakefile % mv spec serverspec %
test/integration/default/serverspec/localhost/httpd_spec.rbがテストケース雛形です。
require 'spec_helper'
describe package('httpd') do
it { should be_installed }
end
describe service('httpd') do
it { should be_enabled }
it { should be_running }
end
describe port(80) do
it { should be_listening }
end
describe file('/etc/httpd/conf/httpd.conf') do
it { should be_file }
it { should contain "ServerName localhost" }
end
これをリネームし、Cookbookに合った変更を加えていきます。
なお、*_spec.rbというファイル名でなければいけないことに注意してください(lib/busser/serverspec/runner.rb#L29)
% cd serverspec/localhost % mv httpd_spec.rb apache2-take_spec.rb % vi apache2-take_spec.rb
次のように、apache2-take Cookbookに対応したテストケースを作成しました。
require 'spec_helper'
%w{ apache2 git-core curl unzip }.each do |i|
describe package( i ) do
it { should be_installed }
end
end
%w{ /etc/apache2/ports.conf /etc/apache2/sites-available/default }.each do |i|
describe file( i ) do
it { should be_file }
end
end
describe file( '/var/www/index.html' ) do
it { should be_file }
end
describe file( '/var/www/img' ) do
it { should be_directory }
end
describe service( 'apache2' ) do
it { should be_enabled }
it { should be_running }
end
これでtest-kitchenを実行してみます。
Chef Client finished, 15 resources updated Finished converging (0m45.41s). -----> Setting up Fetching: thor-0.18.1.gem (100%) Fetching: busser-0.4.1.gem (100%) Successfully installed thor-0.18.1 Successfully installed busser-0.4.1 2 gems installed -----> Setting up Busser Creating BUSSER_ROOT in /opt/busser Creating busser binstub Plugin serverspec installed (version 0.1.0) -----> Running postinstall for serverspec plugin Finished setting up (0m34.28s).
Chef Clientの実行によりCookbookの適用が終わった後、busserとbusser-serverspecのインストールが行われます。
-----> Verifying Suite path directory /opt/busser/suites does not exist, skipping. Uploading /opt/busser/suites/serverspec/spec_helper.rb (mode=0644) Uploading /opt/busser/suites/serverspec/localhost/apache2-take_spec.rb (mode=0644) -----> Running serverspec test suite /opt/chef/embedded/bin/ruby -I/opt/busser/suites/serverspec -S /opt/chef/embedded/bin/rspec /opt/busser/suites/serverspec/localhost/apache2-take_spec.rb .......... Finished in 0.12424 seconds 10 examples, 0 failures Finished verifying (0m2.63s).
テストケースがインスタンスにコピーされ、serverspecによるテストが実行されます。
問題なくすべてのテストをパスしました。
以上のようにserverspecを用いることで、Chefで用意されているリソースには頼らないCookbookのテストが可能です。serverspecにはテストに便利なリソースが準備されていることから、テストを記述する負担も小さいと思われます。是非活用してみてください。
