Edited at

Serverspec書き方


概要

Serverspecの書き方メモ。随時追加します。


関連記事

Serverspec環境構築手順


書式


service起動評価

describe service('nginx') do

it { should be_enabled }
it { should be_running }
end


fileの中身評価

describe file('/usr/local/nginx/conf/nginx.conf') do

text = 'server_name 192.168.33.10;'
# should match /server_name\ 192\.168\.33\.10;/
its(:content) { should match /#{Regexp.escape(text)}/ }
# should match "server_name 192.168.33.10;"
its(:content) { should match text }
end


command実行結果評価

describe command('curl -L http://xxxx.com') do

# should match /"status":201/
its(:stdout) { should match /#{Regexp.escape('"status":200')}/ }
# should match "\"status\":201"
its(:stdout) { should match '"status":200' }
end


commandを叩くだけ(結果は評価しない)


spec/sample_spec.rb

require 'spec_helper'

stdout = Specinfra.backend.run_command('ls').stdout.strip
puts stdout



参考

serverspecのリソースタイプ・マッチャー