ブログ

ryuzeeによるブログ記事。不定期更新

vagrant-serverspecを使ってプロビジョニング結果をテストする

全国1000万人のVagrant利用者のみなさんこんにちは。 Vagrantいいですよね!そしてインフラの状態をテストするserverspecもいいですよね!この2つがシームレスに統合されるとかなりうれしいですよね! ということで本日12/2にvagrant-serverspecというプラグインがリリースされたので早速紹介します。

インストール

インストールは簡単です。いつも通り
vagrant plugin install vagrant-serverspec

としてください。 コード自体は https://github.com/jvoorhis/vagrant-serverspec で公開されています。まだバージョン0.0.1なので、問題を見つけたらPR送るなりIssueを切るなりすると良いと思います。

使い方

使い方も簡単です。まずVagrantfileを見てみましょう。 これは何をやっているかというと、Shell Provisionerを使って、httpdなどをインストールするようになっています。 別にここはChef SoloやChef ServerやPuppetなどを使ってプロビジョニングしても構いません。 要はVagrantの中でプロビジョニングしているということです。 肝は最後の部分です。
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "CentOS-6.4-x86_64-ja"
  config.vm.box_url = "https://dl.dropboxusercontent.com/u/428597/vagrant_boxes/CentOS-6.4-x86_64-ja.box"
  # config.vm.network :private_network, ip: "192.168.33.10"

  config.vm.provision :shell, inline: < <-EOF
    sudo yum install -y httpd mysql-server php php-mbstring php-mysql && \
    sudo /sbin/chkconfig --level 2345 httpd on && \
    sudo rm /etc/httpd/conf.d/welcome.conf && \
    sudo touch /var/www/html/index.html && \
    sudo chmod 666 /var/www/html/index.html && \
    echo "Hello World" > /var/www/html/index.html && \
    sudo /etc/rc.d/init.d/httpd start && \
    sudo /sbin/chkconfig --level 2345 iptables off && \
    sudo /etc/rc.d/init.d/iptables stop
  EOF

  config.vm.provision :serverspec do |spec|
    spec.pattern = '*_spec.rb'
  end
end

最後の部分を見ると、新たにserverspecのプロビジョニング設定が追加されています。ここではspec.rbで終わるファイルをserverspecのテストとして実行しろ!ということを意味しています。 Vagrantfileと同じ場所に、テスト用のスクリプトを以下のような中身で配置します。ファイル名はserverspecのプロビジョニング設定に合致すればなんでも良いです。

require 'serverspec'
require 'pathname'
require 'net/ssh'

include SpecInfra::Helper::Ssh
include SpecInfra::Helper::DetectOS

describe package('httpd') do
  it { should be_installed }
end

describe service('httpd') do
    it { should be_enabled }
end

describe service('httpd') do
    it { should be_running }
end

describe port(80) do
    it { should be_listening }
end

describe file('/var/www/html/index.html') do
    it { should be_file }
end

今回はシェルを使ってhttpdをインストールしているので、それが動作しているのかをテストしています。 serverspecは直感的で分かりやすいのですが、上記は、httpdがインストールされたか、サービスとして登録されているか、起動しているか、80番ポートでListenしているか、そして/var/www/html/index.htmlが存在するかをテストするという内容です。(実際はもっと色々テストすることがあるかもしれません)

ここまでできたら準備完了です。

テスト実行

テストの実行は簡単です。 インスタンスが起動していなければ
vagrant up --provision

すでにインスタンスが起動していれば

vagrant provision

とすればOKです。

結果は以下のように出力されるはずです。

[default] Running provisioner: serverspec...
.....

Finished in 13.01 seconds
5 examples, 0 failures

今回は無事に成功したみたいですね!

失敗した場合は

[default] Running provisioner: serverspec...
.F..F

Failures:

  1) Service "httpd" should be enabled
     Failure/Error: it { should be_enabled }
       sudo chkconfig --list httpd | grep 3:on
       expected Service "httpd" to be enabled
     # ./httpd_spec.rb:13:in `block (2 levels) in <top (required)>'

  2) File "/var/www/html/index.html" should be file
     Failure/Error: it { should be_file }
       sudo test -f /var/www/html/index.html
       expected file? to return true, got false
     # ./httpd_spec.rb:25:in `block (2 levels) in <top (required)>'

Finished in 14.67 seconds
5 examples, 2 failures

のような形で、serverspecの結果がそのまま出力されます。ここまで表示されるので何がおかしいか簡単にわかるでしょう。

まとめ

これなにげに便利です。Vagrantfileや関連ファイル一式を用意しておいて、開発環境をチームのメンバーに配布しているヒトは多いと思いますが、新たに環境を作った時に本当に動作するのか、テストを用意しておけばいつでも検証可能です。 さらに、vagrant-awsを使ってAWS上にAmazon EC2のインスタンスを立ち上げて、Chefでプロビジョニングしたあとにテストも行う、みたいなこともVagrantfileを起点にして全部行うことができそうです。 まだできたてホヤホヤなので何があるのか分かりませんが、今後にかなり期待ができると思います!

アジャイルコーチングやトレーニングを提供しています

株式会社アトラクタでは、アジャイル開発に取り組むチーム向けのコーチングや、認定スクラムマスター研修などの各種トレーニングを提供しています。ぜひお気軽にご相談ください。

詳細はこちら