以前から気になってたServerspecに入門してみました。
インストール(実行元)
ServerSpecを実行するマシンにインストールします。
テスト対象のマシンではなくて、テストを実行する側のサーバーです。
Rubyが入っている前提です。
1
| $ sudo gem install serverspec
|
初期化します。
テスト対象はUN*X、バックエンドはSSHでVagrantではない環境です。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| $ 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: gside.org
+ spec/
+ spec/gside.org/
+ spec/gside.org/sample_spec.rb
+ spec/spec_helper.rb
+ Rakefile
+ .rspec
|
Apacheの起動をテストする(NG編)
自動生成された spec/gside.org/sample_spec.rb を編集します。
OSはGentooですが、対応しているOSであればOSの違いは吸収してくれます。
1
2
3
4
5
6
7
8
9
10
| require 'spec_helper'
describe service('apache2'), :if => os[:family] == 'gentoo' do
it { should be_enabled }
it { should be_running }
end
describe port(80) do
it { should be_listening }
end
|
このコードでApacheが起動していて、自動起動が設定されていること、Port80がListenであることがテストされます。
テストを実行すると、Apacheもインストールしていなけりゃ起動もしていないのでエラーになります。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| $ rake spec
(in /home/hoge)
/usr/bin/ruby21 -I/usr/local/lib64/ruby/gems/2.1.0/gems/rspec-support-3.5.0/lib:/usr/local/lib64/ruby/gems/2.1.0/gems/rspec-core-3.5.4/lib /usr/local/lib64/ruby/gems/2.1.0/gems/rspec-core-3.5.4/exe/rspec --pattern spec/gside.org/\*_spec.rb
/usr/local/lib64/ruby/gems/2.1.0/gems/rspec-core-3.5.4/lib/rspec/core/rake_task.rb:79: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040757
Service "apache2"
should be enabled (FAILED - 1)
should be running (FAILED - 2)
Port "80"
should be listening (FAILED - 3)
Failures:
1) Service "apache2" should be enabled
On host `gside.org'
Failure/Error: it { should be_enabled }
expected Service "apache2" to be enabled
sudo -p 'Password: ' /bin/sh -c rc-update\ show\ \|\ grep\ --\ \\\\s\\\*apache2\\\\s\\\*\\\\\\\|\\\\s\\\*\\\(boot\\\|default\\\)
# ./spec/gside.org/sample_spec.rb:4:in `block (2 levels) in <top (required)>'
2) Service "apache2" should be running
On host `gside.org'
Failure/Error: it { should be_running }
expected Service "apache2" to be running
sudo -p 'Password: ' /bin/sh -c /etc/init.d/apache2\ status
* status: stopped
# ./spec/gside.org/sample_spec.rb:5:in `block (2 levels) in <top (required)>'
3) Port "80" should be listening
On host `gside.org'
Failure/Error: it { should be_listening }
expected Port "80" to be listening
sudo -p 'Password: ' /bin/sh -c netstat\ -tunl\ \|\ grep\ --\ :80\\\
# ./spec/gside.org/sample_spec.rb:9:in `block (2 levels) in <top (required)>'
Finished in 0.09776 seconds (files took 0.81785 seconds to load)
3 examples, 3 failures
Failed examples:
rspec ./spec/gside.org/sample_spec.rb:4 # Service "apache2" should be enabled
rspec ./spec/gside.org/sample_spec.rb:5 # Service "apache2" should be running
rspec ./spec/gside.org/sample_spec.rb:9 # Port "80" should be listening
|
Apacheの起動をテストする(OK編)
Apacheをインストール・起動・デフォルトランレベルへの登録まで済ますと、ServerSpec実行後は下記のようになります。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| $ rake spec
(in /home/home)
/usr/bin/ruby21 -I/usr/local/lib64/ruby/gems/2.1.0/gems/rspec-support-3.5.0/lib:/usr/local/lib64/ruby/gems/2.1.0/gems/rspec-core-3.5.4/lib /usr/local/lib64/ruby/gems/2.1.0/gems/rspec-core-3.5.4/exe/rspec --pattern spec/gside.org/\*_spec.rb
/usr/local/lib64/ruby/gems/2.1.0/gems/rspec-core-3.5.4/lib/rspec/core/rake_task.rb:79: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040757
Service "apache2"
should be enabled
should be running
Port "80"
should be listening
Finished in 0.09332 seconds (files took 0.83872 seconds to load)
3 examples, 0 failures
|
まとめ
導入も簡単だし、テスト対象のOSも自動検出してくれたりと素敵ですね。