「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を合わせて使っているような安心感です。