こんにちは、いなむーです。

最近Apacheの設定変更案件がありまして、設定自体は大した内容ではないのですが、テストをどうしようかと考えた際にInfratasterを試すのはどうかという話になりました。
そこで、実際にいくつか試してみたので備忘録的に残しておきます。

まずは、導入です。
Gemfileに書くか、gemでインストールします。

[code]
$ gem install infrataster
または、下記を実施。
$ vim ~/Gemfile
source ‘https://rubygems.org’

gem ‘infrataster’

$ bundle install

$ gem list | grep infrataster
infrataster (0.3.2)

次に、Infratasterを実施したいディレクトリで、下記を実行します。

[code]
$ mkdir 任意のディレクトリ
$ cd 任意のディレクトリ
$ rspec –init
[/code]

次にspec/spec_helper.rbに接続情報を記載します。
※今回はVagrantを使用し、且つ、Vagrantfileとは違う場所にInfratasterのコードを書いたと想定します。
※vagrant: trueとするとvagrant ssh-configで読み取れる接続情報で接続できるようですが、もしvagrant ssh-configが読めるディレクトリでなくても、下記のようにすればssh接続情報で接続可能です。もちろん秘密鍵で接続も可能です。

[code]
$ vim spec_helper.rb

require ‘infrataster/rspec’

Infrataster::Server.define(
:vm1,
‘192.168.33.11’,
ssh: {user: ‘vagrant’, password: ‘vagrant’}
)
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
[/code]
※下記サイトを参考にさせていただきました。
http://qiita.com/KitaitiMakoto/items/35924ed40238b0a435ef

実際にテストするコードは下記のとおりです。

[code]
require ‘spec_helper’

describe server(:vm1) do
describe http(‘http://vm1’) do
it "responds content including ‘Hello Infrataster’" do
expect(response.body).to include(‘Hello Infrataster’)
end

it "responds as ‘text/html; charset=UTF-8’" do
expect(response.headers[‘content-type’]).to eq("text/html; charset=UTF-8")
end
end
end
[/code]

rspecを実行してテストします。
[code]
$ rspec
F

Failures:

1) server ‘vm1’ http ‘http://vm1’ with {:params=>{}, :method=>:get, :headers=>{}} responds content including ‘Hello Infrataster’
Failure/Error: expect(response.body).to include(‘Hello Infrataster’)
expected "" to include "Hello Infrataster"
# ./spec/example_spec.rb:6:in `block (3 levels) in <top (required)>’

Finished in 0.03041 seconds (files took 0.77082 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/example_spec.rb:5 # server ‘vm1’ http ‘http://vm1’ with {:params=>{}, :method=>:get, :headers=>{}} responds content including ‘Hello Infrataster’
[/code]

失敗すると上記のように表示されるので、どこで失敗したか確認できます。
では、修正して再度rspecを実行してみます。

[code]
rspec
..

Finished in 0.01625 seconds (files took 0.76998 seconds to load)
2 examples, 0 failures
[/code]

うまくいきました。

Post

次にフォームなどの入力テストをしてみます。

Infrataterでは、FaradayというHTTPのクライアントライブラリを使用しているようです。
[blogcard url=http://qiita.com/yudoufu/items/60bd5c8bcd906d4a572f]

このFaradayの記法に則って記述することで、フォームの入力テストが行えます。
入力テストのテストをするために簡単なPHPのフォームを用意してみました。

[code]
$ vim test.html

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
<head>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8" />
<meta http-equiv="content-style-type" content="text/css" />
<title>test form</title>
<body>
<h3>test form</h3>

<form action="./form.php" method="post" name="form1">
<p>内容</p>
<textarea name="naiyo" rows="7" cols="50">
</textarea><br />
<input type="submit" value="送信">
</form>

</body>
</html>
[/code]
[code]
$ vim form.php

<?php
$naiyo = $_POST[‘naiyo’];
echo $naiyo;
?>
[/code]

次にテストコードをInfrataterで書いてみます。※フォームで値を入力し、それがPOSTされたと仮定します。
[code]
describe server(:vm1) do
describe http(‘http://192.168.33.11/form.php’, body: { ‘naiyo’ => ‘testvalue1’}, method: :post) do
it "Check moji" do
expect(response.body).to include(‘testvalue1’)
end
end
end
[/code]

実行してみます。
[code]
.

Finished in 0.0131 seconds (files took 1.1 seconds to load)
1 example, 0 failures
[/code]

うまくいきました。一応正しく動作しているか確認するために、naiyouのvalueをtestvalue2にしてみます。

[code]
rspec
F

Failures:

1) server ‘vm1’ http ‘http://192.168.33.11/form.php’ with {:params=>{}, :method=>:post, :headers=>{}, :body=>{"naiyo"=>"testvalue2"}} Check moji
Failure/Error: expect(response.body).to include(‘testvalue1′)
expected "testvalue2" to include "testvalue1"
# ./spec/vm1_spec.rb:4:in `block (3 levels) in <top (required)>’

Finished in 0.0527 seconds (files took 1.28 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/vm1_spec.rb:3 # server ‘vm1’ http ‘http://192.168.33.11/form.php’ with {:params=>{}, :method=>:post, :headers=>{}, :body=>{"naiyo"=>"testvalue2"}} Check moji
[/code]

ちゃんと失敗しましたので、正しく動作しているようです。

フォームの入力テストまでInfratasterで実施するのか?という疑問とか、上記だと正しいテストにならないというご意見はあるかもしれません。
ただ、POSTのテストが出来るのでテストの幅が広がりそうですし、単純に面白く感じました。
もう少し勉強してみて、色々やった上で仕事につなげていけたらなぁと思いました。

以上。

カテゴリー: RubyTech