はじめに
- 以前から気になっていた
chefのcookbookテストフレームワーク test-kitchen を使ってcookbookのテストをしてみる(テストする為の環境を作った) - 仮想環境として標準の
vagrantに合わせてlxcを利用してみる
参考
- LXC & Test-Kitchen Tutorial by Bryan Berry
- Using test-kitchen with Berkshelf, LXC and chef-zero
- Cookbookテストフレームワーク「test-kitchen」前編 #opschef_ja
- test-kitchenのつかいかた
- opscode-cookbooks / apache2
- RuntimeError: :json is not registered on Faraday::Request
試した環境
- Ubuntu 13.10
- ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
- Vagrant 1.3.3
- VirtualBox 4.2.16-dfsg-3
- lxc 1.0.0.alpha2
普通に test-kitchen
準備
bundler のインストール
sudo gem install bundler --no-ri --no-rdoc -V
Gemfile を準備する
今回はこちらの cookbook を利用するので git clone してくると Gemfile が同梱されているのでこちらを利用する。
git clone https://github.com/opscode-cookbooks/apache2.git
Gemfile は以下の通り。
# source "https://rubygems.org" # gem 'cucumber', '~> 1.2.0' # gem 'httparty', '~> 0.8.3' # gem 'minitest', '~> 3.0.0' # gem 'nokogiri', '~> 1.5.0' # group :kitchen do # gem 'test-kitchen', '< 1.0' # end source 'https://rubygems.org' gem 'berkshelf', '~> 2.0' gem 'chefspec', '~> 2.0' gem 'foodcritic', '~> 3.0' gem 'rubocop', '~> 0.12' group :integration do gem 'test-kitchen', '~> 1.0.0.beta' gem 'kitchen-vagrant', '~> 0.11' end
bundle install を実行して必要なパッケージをインストール後に bundle exec kitchen init を実行する。
bundle install bundle exec kitchen init
その後、bundle exec kitchen init を実行するとカレントディレクトリ .kitchen.yml と test ディレクトリが作成される。
$ tree -L 1 . ├── Berksfile ├── Berksfile.lock ├── CHANGELOG.md ├── CONTRIBUTING.md ├── Gemfile ├── Gemfile.lock ├── LICENSE ├── README.md ├── TESTING.md ├── attributes ├── definitions ├── files ├── metadata.rb ├── recipes ├── templates └── test 6 directories, 11 files
.kitchen.yml は以下の通り。
--- 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-bento.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box suites: - name: default run_list: ["recipe[apache2]"] attributes: {}
Let's テスト
ということで、早速テストを実行してみる。
bundle exec kitchen test
を実行後、しばらく待つと以下のようにテストが終了する。
(省略)
Finished converging <default-ubuntu-1204> (2m25.53s).
-----> Setting up <default-ubuntu-1204>
Finished setting up <default-ubuntu-1204> (0m0.00s).
-----> Verifying <default-ubuntu-1204>
Finished verifying <default-ubuntu-1204> (0m0.00s).
-----> Destroying <default-ubuntu-1204>
[kitchen::driver::vagrant command] BEGIN (vagrant destroy -f)
[default] Forcing shutdown of VM...
[default] Destroying VM and associated drives...
[kitchen::driver::vagrant command] END (0m5.46s)
Vagrant instance <default-ubuntu-1204> destroyed.
Finished destroying <default-ubuntu-1204> (0m5.71s).
Finished testing <default-ubuntu-1204> (3m47.60s).
-----> Kitchen is finished. (3m48.39s)
感じたこと
ほぼ test-kitchen 初体験の自分が感じたこと...
- 初回の
boxのダウンロードに時間がかかる... - 毎回の仮想マシンの起動に待たされる...
gemの依存関係に振り回されることがある...(テストをする前に疲れる)
仮想環境に lxc を利用してみる
前述の通り vagrant の box ファイルダウンロードや仮想マシンの起動時間を短縮するべく仮想環境として lxc のコンテナを利用してみる。こちらやこちらを読む限りでは、test-kitchen で lxc を使う時のキモは kitchen-lxc と lxc-awesome-ephemeral かなと。
事前に lxc コンテナを作っておく
sudo lxc-create -t ubuntu -n ubuntu-master
作成後、以下をコンテナに設定する。
rootパスワードを設定wgetやcurlをインストールする
Gemfile
前述の通り test-kitchen の lxc ドライバである kitchen-lxc とコンテナファイルシステムを tmpfs に作成する lxc-awesome-ephemeral 等の gem が追加となる。
source 'https://rubygems.org' gem 'berkshelf' , '~> 2.0.0' gem 'minitest-chef-handler' gem 'test-kitchen', '1.0.0.beta.2' gem 'chef' gem 'chef-zero' gem 'lxc-awesome-ephemeral', :git => 'https://github.com/portertech/lxc-awesome-ephemeral.git' gem 'kitchen-lxc' gem 'kitchen-vagrant', :group => :integration gem 'faraday_middleware'
.kitchen.yml
--- driver_plugin: lxc driver_config: require_chef_omnibus: true platforms: - name: ubuntu-master driver_config: base_container: ubuntu-master # your base container name username: root # defaults to "root" password: root # defaults to "root" suites: - name: default run_list: ["recipe[apache2]"] attributes: {}
Let's テスト!!
コンテナのセットアップ状態次第では vagrant でのテストが通った cookbook が lxc コンテナではコケてしまうこともあるので注意する。このあたりはコンテナの構築の際にルールを設けて構築しておくべきかと。
sudo bundle exe kitchen test
テストが正常に終了した場合には以下のように表示される。
(省略)
- restart service service[apache2]
[2013-11-08T22:14:20+00:00] INFO: Chef Run complete in 35.768406648 seconds
[2013-11-08T22:14:20+00:00] INFO: Running report handlers
[2013-11-08T22:14:20+00:00] INFO: Report handlers complete
Chef Client finished, 19 resources updated
Finished converging <default-ubuntu-1204-master> (1m18.52s).
-----> Setting up <default-ubuntu-1204-master>
Finished setting up <default-ubuntu-1204-master> (0m0.00s).
-----> Verifying <default-ubuntu-1204-master>
Finished verifying <default-ubuntu-1204-master> (0m0.00s).
-----> Destroying <default-ubuntu-1204-master>
[kitchen::driver::lxc command] BEGIN (lxc-awesome-ephemeral -c -o ubuntu_1204_master -n default-ubuntu-1204-master-328e26)
[kitchen::driver::lxc command] END (0m5.55s)
Finished destroying <default-ubuntu-1204-master> (0m5.74s).
Finished testing <default-ubuntu-1204-master> (1m28.91s).
-----> Kitchen is finished. (1m28.93s)
最後の Kitchen is finished. (1m28.93s) までの所要時間が vagrant と比較すると約 1/3 である点が素晴らしい!
最後に
test-kitchen で lxc を使うメリットとデメリットを自分なりにまとめてみた。
lxc を使うメリット
- 仮想環境の起動時間が半端無く速い
- 軽い
lxc を使うデメリット
- 事前にある程度の環境が構築されたコンテナを用意しておく必要がある
- 共通の環境を用意する場合には別途環境構築するフレームワークが必要(
chefとかpuppet等で構築する)
ということで、仮想環境の起動時間が短くて軽いという利点は事前のコンテナ準備の煩わしさを一蹴すると思われるので、特別な理由が無い限りは test-kitchen に lxc は使えると思う!