前回、ServerSpecを使ってインフラのテストを書いてみましたが、
同じ思想でpythonベースのプロダクトを教えてもらったので試してみました。
1
| Testinfra aims to be a Serverspec equivalent in python
|
インストール
pipが入っていれば簡単にインストールできちゃいます。
1
2
3
4
5
6
7
8
9
10
11
12
| $ sudo pip install testinfra
Collecting testinfra
Downloading testinfra-1.5.4-py2.py3-none-any.whl (60kB)
100% |████████████████████████████████| 61kB 1.9MB/s
Requirement already satisfied (use --upgrade to upgrade): six>=1.4 in /usr/lib64/python2.7/site-packages (from testinfra)
Collecting pytest!=3.0.2 (from testinfra)
Downloading pytest-3.0.7-py2.py3-none-any.whl (172kB)
100% |████████████████████████████████| 176kB 1.7MB/s
Requirement already satisfied (use --upgrade to upgrade): py>=1.4.29 in /usr/lib64/python2.7/site-packages (from pytest!=3.0.2->testinfra)
Requirement already satisfied (use --upgrade to upgrade): setuptools in /usr/lib64/python2.7/site-packages (from pytest!=3.0.2->testinfra)
Installing collected packages: pytest, testinfra
Successfully installed pytest-3.0.7 testinfra-1.5.4
|
テストの実行
テストを書きます。
assertで書けるのはxunitの方が慣れている身としては書きやすいですね。
1
2
3
4
| def test_apache_running_and_enabled(Service):
apache = Service("apache2")
assert apache.is_running
# assert apache.is_enabled
|
実行してみます。
Ansibleのダイナミックインベントリをそのまま使えるのは便利ですね。
下記のようにNameタグの値でテスト対象を指定しています。
1
2
3
4
5
6
7
8
9
10
11
12
13
| $ testinfra -v --connection=ansible --ansible-inventory=./ec2.py --hosts=tag_Name_gside_gentoo test.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.12, pytest-3.0.7, py-1.4.30, pluggy-0.4.0 -- /usr/bin/python2.7
cachedir: .cache
rootdir: /home/hoge/Dropbox/ansible_private, inifile:
plugins: testinfra-1.5.4
collected 1 items
test.py::test_apache_running_and_enabled[ansible://13.112.48.230] PASSED
============================================================================= pytest-warning summary =============================================================================
WP1 None Module already imported so can not be re-written: testinfra
================================================================== 1 passed, 1 pytest-warnings in 7.90 seconds ===================================================================
|
ランレベルに登録されているかのテストは、今回テスト対象だったGentooには対応していないようでした。
そこはServerSpecはきっちり対応してるのがすごい所ですね。
まとめ
assertで書ける、ansibleの接続設定がそのまま使えるところは非常に使いよいです。
「infratasterも使いましょう」という神の啓示が降りたので導入してみました。
infratasterのインストール
Infratasterを実行するマシンにインストールします。
テスト対象のマシンではなくて、テストを実行する側のサーバーです。
Ruby2.2が入っている前提です。
1
2
| $ sudo gem install serverspec
$ rspec --init
|
infratasterはruby2.2が必要なので、注意してください。
Apacheの起動をテストする
spec/spec_helper.rbを編集します。
Infratasterのインクルードと、サーバーのIPアドレスの定義をします。
1
2
3
4
5
| require 'infrataster/rspec'
Infrataster::Server.define(:app) do |server|
server.address = '54.64.10.41/32'
end
|
spec/gside.org/sample_spec.rb を編集します。
HTTPで接続して、コンテンツの中身に “It works!“が含まれること、content-typeが text/htmlであることをテストしています。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| describe port(80) do
it { should be_listening }
end
describe server(:app) do
describe http('http://app') do
it "responds content including 'It works!'" do
expect(response.body).to include('It works!')
end
it "responds as 'text/html'" do
expect(response.headers['content-type']).to eq("text/html")
end
end
end
|
それではテストを実行していきます。
先日作成したserverspecのテストも同時に流れます。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| $ rake spec
/usr/bin/ruby22 -I/usr/local/lib64/ruby/gems/2.2.0/gems/rspec-support-3.5.0/lib:/usr/local/lib64/ruby/gems/2.2.0/gems/rspec-core-3.5.4/lib /usr/local/lib64/ruby/gems/2.2.0/gems/rspec-core-3.5.4/exe/rspec --pattern spec/gside.org/\*_spec.rb
/usr/local/lib64/ruby/gems/2.2.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
server 'app'
http 'http://app' with {:params=>{}, :method=>:get, :headers=>{}}
responds content including 'It works!'
responds as 'text/html'
Finished in 0.18223 seconds (files took 1.36 seconds to load)
5 examples, 0 failures
|
失敗した時の出力はこんな感じ。
“It works!” -> “t works!” にしてみました。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| server 'app'
http 'http://app' with {:params=>{}, :method=>:get, :headers=>{}}
responds content including 'It works!' (FAILED - 1)
responds as 'text/html'
Failures:
1) server 'app' http 'http://app' with {:params=>{}, :method=>:get, :headers=>{}} responds content including 'It works!'
On host `gside.org'
Failure/Error: expect(response.body).to include('It works!')
expected "<html><body><h1>t works!</h1></body></html>\n" to include "It works!"
Diff:
@@ -1,2 +1,2 @@
-It works!
+<html><body><h1>t works!</h1></body></html>
# ./spec/gside.org/sample_spec.rb:15:in `block (3 levels) in <top (required)>'
|
まとめ
serverspecとinfratasterを合わせれば内部・外部からのインフラのテストまで自動化できて、非常に安心です。
UnitTestとSeleniumを合わせて使っているような安心感です。
以前から気になってた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も自動検出してくれたりと素敵ですね。