Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

serverspec | インストール + 仮想環境内ローカルサンプル実行

概要

インストール

前提

・設定試行用の仮想環境を用意しておき、仮想環境内で自環境に対するテストを実施
今回は
Ubuntu1204
rbenv 0.4.0-67-g3300587
Ruby 2.0.0-p247
bundler 1.3.5
を設定。

手順

Gemfileを記述します

gem "serverspec", "~> 0.10.10"

bundlerを実行します

bundle

serverspec初期化
今回はUnix,local実行を選択

serverspec-init
Select OS type:

  1) UN*X
  2) Windows

Select number: 1

Select a backend type:

  1) SSH
  2) Exec (local)

Select number: 2

これでserverspec関連のファイルが生成されます。

テストケースを編集。
curlがインストールされていること
・ntpが起動していること
・3000ポートのlistenが成功(rails + weblicのデフォルト構成起動中の確認)
・ファイルの確認(RailsのGemfileがあるか、execjsのインポートが設定してあるか)

require 'spec_helper'

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

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

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

describe file('/home/vagrant/serverspec/sample/hoge/Gemfile') do
  it { should be_file }
  it { should contain "gem 'execjs'" }
end

テストファースト

rake spec
:
Finished in 0.03999 seconds
6 examples, 6 failures

railsをインストールし、サンプル作成

bundle exec gem i rails
bundle exec rails new hoge
cd hoge
bundle exec rails s

NTPサービスをスタート

sudo service ntp start

テスト再実行

vagrant@precise64:~/serverspec$ rake spec
/home/vagrant/.rbenv/versions/2.0.0-p247/bin/ruby -S rspec spec/localhost/httpd_spec.rb
......

Finished in 0.04838 seconds
6 examples, 0 failures