Help us understand the problem. What is going on with this article?

Serverspecでインフラのテストコードを書いてみる

More than 5 years have passed since last update.

概要

前回: VagrantにServerspecをインストール
http://qiita.com/wkodate/items/c25482b5ae0bb59088e5

前回はServerspecのセットアップを完了したので、今回は実際にテストコードを書いてみる

テストコードのひな形をインストール

serverspec-initコマンドでテストコードのひな形をダウンロードできる

OS typeはUNIXの1

Backend typeは、今回はテスト対象のサーバ上でテストを実行するので2。実際にはリモートから確認する機会のほうが多そう

$ 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

 + spec/
 + spec/localhost/
 + spec/localhost/httpd_spec.rb
 + spec/spec_helper.rb
 + Rakefile

Rakefileとspecディレクトリが作られた。

テストを記述するのはこの中の spec/localhost/http_spec.rb というファイル

$ cat spec/localhost/httpd_spec.rb
require 'spec_helper'

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

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

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

describe file('/etc/httpd/conf/httpd.conf') do
  it { should be_file }
  its(:content) { should match /ServerName localhost/ }
end
  • httpdパッケージがインストールされているか
  • httpdが起動しているか
  • 80番ポートがlistenになっているか
  • /etc/httpd/conf/httpd.confがファイルとして存在していて、その中身が ServerName localhost にマッチするか

についてのテストを書いているようです

テスト実行(失敗)

試しにこのままテストを実行してみる。httpdはインストールしていないので失敗するはず

$ rake spec
-bash: rake: command not found
# rakeのインストール
$ sudo gem install rake
Fetching: rake-10.3.2.gem (100%)
Successfully installed rake-10.3.2
Installing ri documentation for rake-10.3.2
/usr/lib/ruby/1.8/rdoc/rdoc.rb:280: warning: conflicting chdir during another chdir block
/usr/lib/ruby/1.8/rdoc/rdoc.rb:287: warning: conflicting chdir during another chdir block
Done installing documentation for rake after 2 seconds
1 gem installed
$ which rake
/usr/bin/rake
# テスト実行
$ rake spec
/usr/bin/ruby -S rspec spec/localhost/httpd_spec.rb
Ferror reading information on service httpd: No such file or directory
FFFFF

Failures:

  1) Package "httpd" should be installed
     On host ``
     Failure/Error: it { should be_installed }
       rpm -q httpd
       package httpd is not installed
       expected Package "httpd" to be installed
     # ./spec/localhost/httpd_spec.rb:4

  2) Service "httpd" should be enabled
     On host ``
     Failure/Error: it { should be_enabled   }
       chkconfig --list httpd | grep 3:on
       expected Service "httpd" to be enabled
     # ./spec/localhost/httpd_spec.rb:8

  3) Service "httpd" should be running
     On host ``
     Failure/Error: it { should be_running   }
       ps aux | grep -w -- httpd | grep -qv grep
       expected Service "httpd" to be running
     # ./spec/localhost/httpd_spec.rb:9

  4) Port "80" should be listening
     On host ``
     Failure/Error: it { should be_listening }
       netstat -tunl | grep -- :80\
       expected Port "80" to be listening
     # ./spec/localhost/httpd_spec.rb:13

  5) File "/etc/httpd/conf/httpd.conf" should be file
     On host ``
     Failure/Error: it { should be_file }
       test -f /etc/httpd/conf/httpd.conf
       expected file? to return true, got false
     # ./spec/localhost/httpd_spec.rb:17

  6) File "/etc/httpd/conf/httpd.conf" content should match /ServerName localhost/
     On host ``
     Failure/Error: its(:content) { should match /ServerName localhost/ }
       cat /etc/httpd/conf/httpd.conf 2> /dev/null || echo -n
       expected "" to match /ServerName localhost/
       Diff:
       @@ -1,2 +1,2 @@
       -/ServerName localhost/
       +""
     # ./spec/localhost/httpd_spec.rb:18

Finished in 0.09874 seconds
6 examples, 6 failures

Failed examples:

rspec ./spec/localhost/httpd_spec.rb:4 # Package "httpd" should be installed
rspec ./spec/localhost/httpd_spec.rb:8 # Service "httpd" should be enabled
rspec ./spec/localhost/httpd_spec.rb:9 # Service "httpd" should be running
rspec ./spec/localhost/httpd_spec.rb:13 # Port "80" should be listening
rspec ./spec/localhost/httpd_spec.rb:17 # File "/etc/httpd/conf/httpd.conf" should be file
rspec ./spec/localhost/httpd_spec.rb:18 # File "/etc/httpd/conf/httpd.conf" content should match /ServerName localhost/
/usr/bin/ruby -S rspec spec/localhost/httpd_spec.rb failed

無事にテストが実行されて失敗したようです

テストコードの修正

自分でテストコードを作ってみる。

spec/localhost/http_spec.rb を以下のように修正

$ cat spec/localhost/httpd_spec.rb
require 'spec_helper'

describe command('whoami') do
  it { should return_stdout 'vagrant'}
end

実行ユーザのアカウントを表示させるコマンドで標準出力としてvagrantが返ってくる、という簡単なテスト

テスト実行(成功)

では、再度テストを実行してみましょう

$ rake spec
/usr/bin/ruby -S rspec spec/localhost/httpd_spec.rb
.

Finished in 0.01918 seconds
1 example, 0 failures

無事テストが成功したようです

他に公式サイトにいろいろなテストの書き方の例が公式サイト書いてあった。実際にテストを書くときはココを見ながら書くことになりそう
http://serverspec.org/resource_types.html

参考

rakeのインストール
http://blog.carbonfive.com/2010/03/29/how-to-install-rails-on-centos-5-4/

RSpecの構文
http://www39.atwiki.jp/startruby/pages/23.html

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした