LoginSignup
8
8

More than 5 years have passed since last update.

Serverspecを使ってみた

Last updated at Posted at 2015-03-22

前提条件

  • OS X 10.9.5
  • Ruby 2.1.0

インストール

  • Serverspec用のディレクトリを作成し、下記のGEMをインストールする。
    
    $ mkdir serverspec
    $ cd serverspec
    $ bundle init
    $ vim Gemfile
    
Gemfile
gem serverspec

$ bundle install --path vendor/bundle

初期設定

  • Serverspecの初期設定を対話式で行う。
  • 今回はvagrantというホストに対して、SSH接続する設定とした。
    
    $ bundle exec 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: n
Input target host name: vagrant
 + spec/
 + spec/vagrant/
 + spec/vagrant/sample_spec.rb
 + spec/spec_helper.rb
 + Rakefile
 + .rspec

Serverspecテストコード実装

  • NGINXのテストコードを実装する。
    
    $ cd spec/vagrant
    $ vim nginx_spec.rb
    
nginx_spec.rb
require 'spec_helper'

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

describe command('nginx -v') do
  its(:stdout) { should match /nginx\/1.6.*/ }
end

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

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

Serverspecテスト実施

  • Serverspecのテストを実施する。
    
    $ bundle exec rake spec
    
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8