LoginSignup
5
1

More than 3 years have passed since last update.

Infratasterを触ってみる

Posted at

概要

構築したサーバの動作確認をする時に、URLが正しく閲覧できるか確認したいということがあるかと思います。一つずつ確認するのは簡単ですが、それが大量にあったりすると時間もかかりますし、確認漏れなどが出るかもしれません。

そんな作業を自動化できそうな Infrataster というツールを見つけましたので、試しに使ってみたいと思います。(Infrataster=インフラテイスター、インフラの味見・毒味という意味らしい)

初期設定

Rubyで作られたツールなので、Rubyのコンパイラが必要です。また、RSpecの仕組みを使って確認していくようです。

まずは Gemfile を用意します。最低限 Infrataster と、JSONで値の確認をするためにお好みで追加のライブラリも一緒に指定します。

source 'https://rubygems.org'

gem 'infrataster'
gem 'rspec-json_matcher'

依存関係のインストールを行います。

$ bundle install
...
Bundle complete! 1 Gemfile dependency, 28 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

Infratasterはrspecの上で動作するので設定ファイルの生成を行います。

$ bundle exec rspec --init
  create   .rspec
  create   spec/spec_helper.rb

ここまでで基本的な準備が完了したので、テストの構築に入っていきます。

まず、テスト対象を定義します。spec/spec_helper.rbの先頭に下記を定義していきます。

require 'rspec/json_matcher'
require 'infrataster/rspec'

RSpec.configuration.include RSpec::JsonMatcher
Infrataster::Server.define(
  :production,
  'www.holmescloud.com'
)

テストしたい内容を spec/production_spec.rb に記載していきます。テスト対象のページに対してテストケースを複数書くことももちろん可能です。

require 'spec_helper'

describe server(:production) do
  describe http('https://www.holmescloud.com/index.html') do
    it 'returns 200' do
      expect(response.status).to eq(200)
    end
  end
  describe http('https://www.holmescloud.com/login.html') do
    it 'returns 200' do
      expect(response.status).to eq(200)
    end
    it 'has header' do
      expect(response.headers['X-Application-Context']).to eq('application')
    end
  end
  describe http('https://www.holmescloud.com/news/') do
    it 'returns 200' do
      expect(response.status).to eq(200)
    end
  end
end

一通りテストが書けたら実行してみましょう。

$ bundle exec rspec spec/production_spec.rb 
....

Finished in 0.76616 seconds (files took 1.31 seconds to load)
4 examples, 0 failures

作成したものは下記の場所にアップロードしてあります。
https://github.com/nabe256/infratest

今回はテストが通る前提の書き方で進めていますが、インフラの構築中であればテスト駆動開発にも有効に使えると思います。
率直に便利だと感じたので今後どんどん使っていきたいですね。

参考ページ

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1