LoginSignup
6
6

More than 5 years have passed since last update.

はじめに

  • 最近、Infrastructure as Code の精神で chef を使ったインフラ構築を試しているが、そろそろテストについて考えるタイミングに来たので serverspec を触ってみる

環境

serverspec のインストール

$ gem install serverspec

テスト対象の準備

  • 今回は nginx をインストールしたサーバをテスト対象とする
  • サーバは Vagrant で仮想サーバを用意し、 nginx のインストールは chef でおこなう
    • vagrant で用意する OS は Ubuntu とする

仮想サーバの用意

  • Box がない場合は、公式の Ubuntu を用意しておく
    • すでに Box がある場合はスキップ
$ vagrant box add precise32 http://files.vagrantup.com/precise32.box
  • 任意のディレクトリを用意し、仮想サーバの初期化をする
$ vagrant init precise32
  • 設定ファイルの編集

    • Vagrantfile の L.26 の下記設定のコメントアウトを外す
    • "config.vm.network :private_network, ip: "192.168.33.10""
  • 仮想マシンを起動

$ vagrant up
  • ssh config に設定を保存する
    • 任意の名前を今回は testmachine とする
$ vagrant ssh-config --host “任意の名前” >> ~/.ssh/config

chef による Recipe の用意

  • chef のリポジトリ作成
$ knife solo init testForServerspec
  • 作成したリポジトリに移動
$ cd testForServerspec
  • knife solo を使用し、vagrant で作成した ubuntu に Chef をインストール
$ knife solo prepare testmachine
  • 下記が出力されるとインストール完了 > Thank you for installing Chef!

同時に testForServerspec/nodes/testmachine.json が作成される

  • nginx をインストールするための cookbook を作成する
$ knife cookbook create nginx -o site-cookbooks
  • 上記コマンド入力後に作成される site-cookbooks/nginx/recipes/default.rb を編集する
default.rb
############################## nginx install

log "Nginx install ..."
package "nginx" do
  action :install
end

service "nginx" do
  supports :status => true, :restart => true, :reload => true
  action [ :enable , :start ]
end
log "Nginx install Done!!!"
  • 続けて、 node/testmachine.json も編集する
testmachine.json
{
  "run_list":[
    "recipe[nginx]"
    ]
}
  • chef の実行
$ knife solo cook testmachine
  • 実行後、ブラウザから "http://192.168.33.10/" にアクセスすると "Welcome to Nginx!" のページが表示される
    • 上述の Vagrantfile の設定で 192.168.33.10 の IP を有効にしておくこと

serverspec の設定

  • Vagrantfile があるディレクトリで下記コマンドを入力し、初期化をおこなう
$ 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: y
 + spec/
 + spec/default/
 + spec/default/httpd_spec.rb
 + spec/spec_helper.rb
 + Rakefile
  • 作成された spec/default/httpd_spec.rb はサンプルコードになっているので、リネームして編集していく
    • spec/default/httpd_spec.rb -> spec/default/nginx_spec.rb
nginx_spec.rb
require 'spec_helper'

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

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

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

テストの実行

  • RSpec と同様に下記コマンドで実行可能
$ rake spec

....
Finished in 1.56 seconds
4 examples, 0 failures
  • vagrant の仮想サーバを一度 destroy した後、再度 vagrant up した状態で(chef で nginx を入れる前に) 再度テストを実行するとテストが失敗することが確認できる

参考

6
6
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
6
6