LoginSignup
6
9

More than 5 years have passed since last update.

Serverspec書き方

Last updated at Posted at 2017-04-14

概要

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のリソースタイプ・マッチャー

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