test-kitchen 1.0 概略 前編 #opschef_ja
この記事は1年以上前に投稿されました。情報が古い可能性がありますので、ご注意ください。
Cookbookテストフレームワークtest-kitchenの1.0が2013年5月11日、masterブランチとなりました。
ドライバ
以前のtest-kitchen安定版ではVagrantやOpenStackをクリーンなインスタンスを作成するための仮想化基盤として利用する機構が内蔵されていました。1.0では各仮想化プラットフォームを利用する機構はドライバとして外部に切り出されており、豊富な種類が用意されています(Kitchen::Driver::)。ただ、2013年6月4日現在、ドキュメントには反映されていません(Documentation)。
デフォルトではkitchen-vagrantが使われるようになっています。また、このドライバはVagrant 1.1以上を必要としているため注意が必要です(Requirements Vagrant, L145, L94を参照してください)。
RubyGemsでインストールできるVagrantは旧安定版の1.0系です。1.1系は単体パッケージとして提供されています(Vagrant Downloads)。kitchen-vagrantを利用する際はVagrant 1.1系をインストールしてください。
なお、RubyGemsの依存関係などでVagrant 1.0系がインストールされている場合、パスの優先順位でそちらが使われないように注意してください。
Busser
以前のtest-kitchen安定版ではMiniTest::Spec (minitest-handler, Minitest Chef Handler)とCucumberをCookbookのテストに用いていました。1.0ではBusserという仕組みでテストフレームワークをプラグインとして利用できるようになっています(Busser runner plugin)。
デフォルトではBusser::RunnerPlugin::Batsが使われるようになっています。Bats: the Bash Automated Testing SystemをBusserで扱うためのプラグインです。
以前のMiniTest::Specのテストケースは2013年6月4日現在、そのままでは使えないようです。
test-kitchenの適用
以降はGetting Startedを参考に実施しています。なお、冒頭の注意書きにもある通り、test-kitchen 1.0 はまだアルファ版でドキュメントも作業中であるため、そのまま適用できない可能性があります。
test-kitchenを適用したいCookbookのディレクトリに移動し、Gemfileに次のようにgem 'test-kitchen'の行を追加してください。以前のgem 'test-kitchen'の行がある場合は置き換えてください。
source 'http://rubygems.org' gem 'test-kitchen', '~> 1.0.0.alpha.7'
bundle installを実行し、test-kichenと必要なパッケージをインストールします。
% bundle install Fetching gem metadata from https://rubygems.org/......... Fetching gem metadata from https://rubygems.org/.. Resolving dependencies... Using timers (1.1.0) Using celluloid (0.14.1) Using coderay (1.0.9) Using method_source (0.8.1) Using mixlib-shellout (1.1.0) Using net-ssh (2.6.7) Using net-scp (1.1.1) Using slop (3.4.5) Using pry (0.9.12.2) Using safe_yaml (0.9.2) Using thor (0.18.1) Using test-kitchen (1.0.0.alpha.7) Using bundler (1.3.5) Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed. %
bundle exec kitchen initを実行すると、test-kitchenに必要なファイルが用意されます。なお、デフォルトではドライバとしてkitchen-vagrantが指定されます。
% bundle exec kitchen init create .kitchen.yml append Rakefile create test/integration/default append .gitignore append .gitignore append Gemfile You must run `bundle install' to fetch any new gems. %
再度bundle installを実行すると、更新されたGemfileに従い、kitchen-vagrantがインストールされます。
% bundle install Resolving dependencies... Using timers (1.1.0) Using celluloid (0.14.1) Using coderay (1.0.9) Using mixlib-shellout (1.1.0) Using net-ssh (2.6.7) Using net-scp (1.1.1) Using method_source (0.8.1) Using slop (3.4.5) Using pry (0.9.12.2) Using safe_yaml (0.9.2) Using thor (0.18.1) Using test-kitchen (1.0.0.alpha.7) Using kitchen-vagrant (0.10.0) Using bundler (1.3.5) Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed. %
テスト用の仮想OSの情報は.kitchen.ymlファイルに記載されています。デフォルトではVagrant用の雛形が準備されています。必要であれば、boxやbox_urlなどを変更します。
なお、2013年5月13日以降、Opscodeで提供されているBaseboxesはChef Clientが含まれていません。provisionerless baseboxesと表現されています。require_chef_omnibus: trueというパラメタを与えることでChef ClientがOmnibusインストーラを使ってインストールされます。
次は変更した.kitchen.ymlファイルです。CentOSのエントリを削除しています。
---
driver_plugin: vagrant
driver_config:
require_chef_omnibus: true
platforms:
- name: ubuntu-12.04
driver_config:
box: opscode-ubuntu-12.04
box_url: https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box
- name: ubuntu-10.04
driver_config:
box: opscode-ubuntu-10.04
box_url: https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-10.04_provisionerless.box
suites:
- name: default
run_list: ["recipe[apache2-take]"]
attributes: {}
まだテストケースがありませんが、test-kitchenを動かしてみます。
% bundle exec kitchen test -----> Starting Kitchen (v1.0.0.alpha.7) -----> Cleaning up any prior instances of -----> Destroying Finished destroying (0m0.00s). -----> Testing -----> Creating [kitchen::driver::vagrant command] BEGIN (vagrant up --no-provision) Bringing machine 'default' up with 'virtualbox' provider... [default] Importing base box 'opscode-ubuntu-12.04'... [default] Matching MAC address for NAT networking... [default] Setting the name of the VM... [default] Clearing any previously set forwarded ports... [default] Creating shared folders metadata... [default] Clearing any previously set network interfaces... [default] Preparing network interfaces based on configuration... [default] Forwarding ports... [default] -- 22 => 2222 (adapter 1) [default] Running any VM customizations... [default] Booting VM... [default] Waiting for VM to boot. This can take a few minutes. [default] VM booted and ready for use! [default] Setting hostname... [default] Configuring and enabling network interfaces... [default] Mounting shared folders... [default] -- /vagrant [kitchen::driver::vagrant command] END (1m15.53s) [kitchen::driver::vagrant command] BEGIN (vagrant ssh-config) [kitchen::driver::vagrant command] END (0m0.63s) Vagrant instance created. Finished creating (1m17.01s).
Vagrant上にUbuntu 12.04のインスタンスを起動しています。
-----> Converging Uploaded apache2-take/templates/default/default.erb (425 bytes) Uploaded apache2-take/templates/default/ports.conf.erb (51 bytes) Uploaded apache2-take/templates/default/twitter-search.html.erb (266 bytes) Uploaded apache2-take/resources/default.rb (99 bytes) Uploaded apache2-take/recipes/default.rb (2843 bytes) Uploaded apache2-take/providers/default.rb (677 bytes) Uploaded apache2-take/attributes/default.rb (155 bytes) Uploaded apache2-take/README.md (1469 bytes) Uploaded apache2-take/metadata.rb (298 bytes) Starting Chef Client, version 11.2.0 [2013-06-04T03:20:16+00:00] INFO: *** Chef 11.2.0 *** [2013-06-04T03:20:17+00:00] INFO: Setting the run_list to ["recipe[apache2-take]"] from JSON
Cookbookをインスタンスに転送し、Chef Clientを実行しています。
実際の実行ログは省略します。
Chef Client finished, 15 resources updated Finished converging (0m42.00s). -----> Setting up Finished setting up (0m0.00s). -----> Verifying Finished verifying (0m0.00s). -----> Destroying [kitchen::driver::vagrant command] BEGIN (vagrant destroy -f) [default] Forcing shutdown of VM... [default] Destroying VM and associated drives... [kitchen::driver::vagrant command] END (0m3.43s) Vagrant instance destroyed. Finished destroying (0m3.48s). Finished testing (2m2.52s).
Chef Clientを実行が完了しました。
テストケースがまだ存在していないため、テストは実行されていません。
-----> Cleaning up any prior instances of -----> Destroying Finished destroying (0m0.00s). -----> Testing -----> Creating
引き続き、Vagrant上にUbuntu 10.04のインスタンスを起動しています。
結果はUbuntu 12.04とほぼ同様なので省略します。
次回は実際にテストケースを作成してみます。
