ksaitoの日記

日々試したことの覚え書き

vagrant-serverspecを使ってサーバをテストする。

移転しました。

自動的にリダイレクトします。

vagrantを使って作成したサーバをserverspecでテストします。 vagrant upでインスタンスの作成とサーバのテストを統合するためにvagrant-serverspecプラグインを使う手順です。

vagrant-serverspecでテストする手順

インストール

vagrant-serverspecプラグインをインストールします。

$ vagrant --version
Vagrant 1.8.1
$ vagrant plugin install vagrant-serverspec
Installing the 'vagrant-serverspec' plugin. This can take a few minutes...
Installed the plugin 'vagrant-serverspec (1.1.0)'!
$

テストケースの作成

serverspecのテストケースを書きます。

$ cat spec/vagrant_spec.rb 
require 'serverspec'

set :backend, :ssh

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

テストケースの実行

Vagrantfileのprovisionに作成したserverspecのテストケースを追加します。

$ git diff Vagrantfile | grep \^[-+]
--- a/Vagrantfile
+++ b/Vagrantfile
+  # Test
+  config.vm.provision :serverspec do |spec|
+    spec.pattern = "spec/*_spec.rb"
+  end
$ 

vagrantの操作でserverspecのテストケースが実行されます。

$ vagrant provision
==> default: Running provisioner: serverspec...
.

Finished in 0.56938 seconds (files took 2.84 seconds to load)
1 example, 0 failures

$ 

serverspec-initで作った雛形が動かない

本当は、serverspec-initでVagrantfileから雛形を自動生成したかったのですがうまく動きませんでした。 解決には、至らなかったのですが試したことを残しておきます。

serverspecの雛形を作成

Vagrantfileが保存されているディレクトリでserverspec-initを実行します。 "Vagrant instance y/n:"に"y"と入力します。 "Auto-config"は、うまく動かないようなので"n"を入力してインスタンス名を入力すると雛形一式が作成されます。

$ serverspec-init 
Select OS type:

  1) UN*X
  2) Windows

Select number: 1

Select a backend type:

  1) SSH
  2) Exec (local)

Select number: 1

Vagrant instance y/n: y
Auto-configure Vagrant from Vagrantfile? y/n: n
Input vagrant instance name: default
 + spec/
 + spec/default/
 + spec/default/httpd_spec.rb
 + spec/spec_helper.rb
$ find spec
spec
spec/spec_helper.rb
spec/default
spec/default/httpd_spec.rb
$ 

Vagrantfileにvagrant-serverspecの設定を追加

$ git diff Vagrantfile | grep \^[-+]
--- a/Vagrantfile
+++ b/Vagrantfile
+  # Test
+  config.vm.provision :serverspec do |spec|
+    spec.pattern = "spec/default/*_spec.rb"
+  end
$ 

実行

実行するとエラーになりました。

$ vagrant provision
==> default: Running provisioner: serverspec...
/home/ubuntu/git/vagrant/spec/spec_helper.rb:5:in `<top (required)>': uninitialized constant SpecInfra (NameError)
        from /home/ubuntu/git/vagrant/spec/default/httpd_spec.rb:1:in `require'
        from /home/ubuntu/git/vagrant/spec/default/httpd_spec.rb:1:in `<top (required)>'
...省略...
        from /opt/vagrant/embedded/gems/gems/vagrant-1.8.1/plugins/commands/provision/command.rb:29:in `execute'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/cli.rb:42:in `execute'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.8.1/lib/vagrant/environment.rb:302:in `cli'
        from /opt/vagrant/embedded/gems/gems/vagrant-1.8.1/bin/vagrant:174:in `<main>'
$

生成されたspec_helper.rbのincludeのSpecInfraが見つけられないようです。

$ head spec/spec_helper.rb 
require 'serverspec'
require 'pathname'
require 'net/ssh'

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

RSpec.configure do |c|
  if ENV['ASK_SUDO_PASSWORD']
    require 'highline/import'
$ 

Ubuntu 14.04.2でserverspecは、aptのパッケージを入れています。 エラーメッセージからvagrantは、バンドルされている/opt/vagrant/embedded/gems/gemsを使っていることが原因かもしれません。 残念...

$ grep VERSION /etc/os-release | head -1
VERSION="14.04.2 LTS, Trusty Tahr"
$ dpkg -l | grep ruby | egrep "serverspec|specinfra"
ii  ruby-serverspec                     0.14.2-1                            all          RSpec tests for your servers configured by Puppet, Chef or anything else
ii  ruby-specinfra                      0.1.1-1                             all          Common layer for serverspec and configspec
$