fbpx

test-kitchen 1.0 と Berkshelf #opschef_ja

この記事は1年以上前に投稿されました。情報が古い可能性がありますので、ご注意ください。

Cookbookテストフレームワークtest-kitchen 1.0は、Berkshelfを用いてテスト対象のCookbookと依存関係にあるCookbookの取得などを容易に行うことができます。

例として、apache2-take Cookbookがxmlbuild-essentialの両Opscode Community Cookbookに依存させるとします。

.kitchen.yml

apache2-take Cookbookの.kitchen.ymlのplatformsのrun_listを追加します。複数のRecipeを指定する場合はYAMLの配列形式で記載してください。


---
driver_plugin: vagrant
driver_config:
require_chef_omnibus: true

platforms:
- name: ubuntu-12.04
driver_config:
box: canonical-ubuntu-12.04
box_url: http://cloud-images.ubuntu.com/vagrant/precise/current/precise-serv
er-cloudimg-amd64-vagrant-disk1.box
run_list:
- recipe[xml]
- recipe[build-essential]


suites:
- name: default
run_list: ["recipe[apache2-take]"]
attributes: {}

platformsのrun_listで指定されたRecipeのCookbookはapache2-take Cookbookの一部ではないため別途取得が必要になります。そのためにBerkshelfを利用します。

Gemfile

apache2-take CookbookのGemfileにgem 'berkshelf'を追加します。


source 'https://rubygems.org'
gem 'test-kitchen', '~> 1.0.0.alpha.7'
gem 'kitchen-vagrant', :group => :integration
gem 'berkshelf'

bundle installを実行します。


% bundle install
Fetching gem metadata from https://rubygems.org/........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Using i18n (0.6.1)
Using multi_json (1.7.6)
Using activesupport (3.2.13)
Using addressable (2.3.4)
Using builder (3.2.2)
Using gyoku (1.0.0)
Using nokogiri (1.5.10)
Using akami (1.2.0)
Using timers (1.1.0)
Using celluloid (0.14.1)
Using hashie (2.0.5)
Using chozo (0.6.1)
Using multipart-post (1.2.0)
Using faraday (0.8.7)
Using minitar (0.5.4)
Using retryable (1.3.3)
Using nio4r (0.4.6)
Using celluloid-io (0.14.1)
Using erubis (2.7.0)
Using mixlib-log (1.6.0)
Using mixlib-authentication (1.3.0)
Using net-http-persistent (2.8)
Using net-ssh (2.6.7)
Using json (1.8.0)
Using solve (0.4.4)
Using ffi (1.9.0)
Using gssapi (1.0.3)
Using httpclient (2.2.0.2)
Using little-plugger (1.1.3)
Using logging (1.6.2)
Using rubyntlm (0.1.1)
Using rack (1.5.2)
Using httpi (0.9.7)
Using nori (1.1.5)
Using wasabi (1.0.0)
Using savon (0.9.5)
Using uuidtools (2.1.4)
Using winrm (1.1.2)
Using ridley (1.0.2)
Using mixlib-shellout (1.1.0)
Using net-scp (1.1.1)
Using coderay (1.0.9)
Using method_source (0.8.1)
Using slop (3.4.5)
Using pry (0.9.12.2)
Using safe_yaml (0.9.3)
Using thor (0.18.1)
Using test-kitchen (1.0.0.alpha.7)
Using berkshelf (2.0.3)
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.
%

Berksfile

apache2-take CookbookのBerksfileを作成します。


site :opscode

metadata


cookbook 'xml'
cookbook 'build-essential'

test-kitchenの実行

では、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
:
:
:
Setting up chef (11.4.4-2.ubuntu.11.04) ...
Thank you for installing Chef!
[local command] BEGIN (if ! command -v berks >/dev/null; then exit 1; fi)
[local command] END (0m0.01s)
[local command] BEGIN (berks install --path /tmp/default-ubuntu-1204-cookbooks20130611-18570-lqof7g)
Using apache2-take (0.1.0) at path: '/tmp/apache2-take'
Using xml (1.1.2) at path
Using build-essential (1.4.0) at path
[local command] END (0m0.92s)
Uploaded build-essential/attributes/default.rb (1487 bytes)
Uploaded build-essential/CHANGELOG.md (1016 bytes)
Uploaded build-essential/metadata.json (940 bytes)
:
:
:
Uploaded xml/attributes/default.rb (280 bytes)
Uploaded xml/CHANGELOG.md (306 bytes)
Uploaded xml/metadata.json (1938 bytes)
:
:
:
Uploaded apache2-take/attributes/default.rb (155 bytes)
Uploaded apache2-take/CHANGELOG.md (430 bytes)
Uploaded apache2-take/providers/default.rb (677 bytes)
:
:
:

chef clientがインストールされた後、berksコマンドが実行され、必要なCookbookがテスト環境に揃えられます。


Starting Chef Client, version 11.4.4
[2013-06-11T10:11:22+00:00] INFO: *** Chef 11.4.4 ***
[2013-06-11T10:11:22+00:00] INFO: Setting the run_list to ["recipe[xml]", "recipe[build-essential]", "recipe[apache2-take]"] from JSON
[2013-06-11T10:11:22+00:00] INFO: Run List is [recipe[xml], recipe[build-essential], recipe[apache2-take]]
[2013-06-11T10:11:22+00:00] INFO: Run List expands to [xml, build-essential, apache2-take]
[2013-06-11T10:11:22+00:00] INFO: Starting Chef Run for default-ubuntu-1204
[2013-06-11T10:11:22+00:00] INFO: Running start handlers
[2013-06-11T10:11:22+00:00] INFO: Start handlers complete.
Compiling Cookbooks...
Converging 22 resources
Recipe: xml::default
* package[libxml2-dev] action install[2013-06-11T10:11:22+00:00] INFO: Processing package[libxml2-dev] action install (xml::default line 21)

- install version 2.7.8.dfsg-5.1ubuntu4.4 of package libxml2-dev
:
:
:
Recipe: build-essential::debian
* package[autoconf] action install[2013-06-11T10:11:35+00:00] INFO: Processing package[autoconf] action install (build-essential::debian line 40)


- install version 2.68-1ubuntu2 of package autoconf
:
:
:
Recipe: apache2-take::default
* execute[apt-get update] action run[2013-06-11T10:11:55+00:00] INFO: Processing execute[apt-get update] action run (apache2-take::default line 11)
Hit http://archive.ubuntu.com precise Release.gpg
:
:
:

run_listに指定されたRecipeが実行されます。

この後はテストが行われますが省略します。

以上のようにBerkshelfにより、test-kitchenからのCookbookの取得・管理が容易になっています。他のCookbookに依存するCookbookのテストを行いたい方は参考にしてみてください。

Author

Chef・Docker・Mirantis製品などの技術要素に加えて、会議の進め方・文章の書き方などの業務改善にも取り組んでいます。「Chef活用ガイド」共著のほか、Debian Official Developerもやっています。

Daisuke Higuchiの記事一覧

新規CTA