LoginSignup
3
3

More than 5 years have passed since last update.

CucumberでSpecInfraのBackendを使ってみる

Last updated at Posted at 2014-04-25

この記事は最終更新から1年以上経過しています。 気をつけてね。

サンプルコードはこちら。

specinfraの使い方ついてはこちらもどうぞ。specinfraを使ってみよう - Qiita

まずはEnv

spcinfraの.backendをステップでお手軽に使えるよう、モジュールにしてWorldで読み込んだ。

features/support/env.rb
require 'specinfra'
require 'net/ssh'

module InfraHelper
  include SpecInfra::Helper::DetectOS
  include SpecInfra::Helper::Ssh

  def default_backend(host, user = 'root', ssh_opts = {})
    SpecInfra.configuration.ssh = Net::SSH.start(host, user, ssh_opts)
  end
end

World(InfraHelper)

Githubのサンプルコードでは、Givenで対象ホストを指定するためにつくった#return_backendが残ってます。

そしてBefore

とりあえず対象が1台なのと、コードをそのまま公開するために、Beforeからバックエンド設定をしてみました。

features/support/hooks.rb
Before do
  default_backend(ENV['CUCUMBER_REMOTE_HOST'])
end

複数ならBackgroundに並べるなり、対象を変更しながら叩くなり色々やり方があると思います。

シナリオへ

サンプルシナリオ1

ではサンプルその一、OSファミリーがSmartOSであることを確かめるシナリオを、ちょっと汎用的なStepで作ってみます。

features/_get_os_family.feature
Feature: Get OS Family

  Scenario: Success Login
    When I ask to backend with "check_os"
    Then I will found "SmartOS" from backend at "family"

ステップ1

ハッシュで返ってくるメソッドに対して、任意のキーの中身を確かめます。

features/step_definitions/ask_with_step.rb
When(/^I ask to backend with "(.*)"$/) do |method|
  @result = backend.send(method.to_sym)
end

Then(/^I will found "(.*)" from backend at "(.*)"$/) do |exp, key|
  raise unless @result[key.to_sym] == exp
end

ネストしたキーや、型も合わせたい場合はそれ用に工夫したステップが必要ですね。

サンプルシナリオ2

特定バージョンのパッケージがインストールされていることを確かめます。
こちらはspecinfracheck_installedを決め打ちする代わりに、シナリオアウトラインでまとめて記述できるようにしてみました。

features/check_package.feature
Feature: Check package

  Scenario Outline: Detect package
    Given I check package "<package>" installed with "<version>"

    Scenarios: Installed
      | package  | version   |
      | openssl  | 1.0.1fnb1 |
      | nginx    | 1.5.7     |
      | gmake    | 4.0       |
      | gcc47    | 4.7.3nb1  |
      | autoconf | 2.69nb3   |

ステップ2

specinfraがブーリアンで返してくれるので、ステップ側は一行で済みます。

features/step_definitions/check_by_step.rb
When(/^I check package "(.*)" installed with "(.*)"$/) do |pkg, version|
  raise unless backend.check_installed(pkg, version)
end

ステップが完結でかつ、対象が他のOSでも使いまわせるためとても助かります。

結果発表

環境変数CUCUMBER_REMOTE_HOSTをセットして、いざcucumber。

cucumber-result
$ cucumber 
Feature: Get OS Family

  Scenario: Success Login                                # features/_get_os_family.feature:3
    When I ask to backend with "check_os"                # features/step_definitions/ask_with_step.rb:1
    Then I will found "SmartOS" from backend at "family" # features/step_definitions/ask_with_step.rb:5

Feature: Check package

  Scenario Outline: Detect package                               # features/check_package.feature:3
    Given I check package "<package>" installed with "<version>" # features/step_definitions/check_by_step.rb:1

    Scenarios: Installed
      | package  | version   |
      | openssl  | 1.0.1fnb1 |
      | nginx    | 1.5.7     |
      | gmake    | 4.0       |
      | gcc47    | 4.7.3nb1  |
      | autoconf | 2.69nb3   |

6 scenarios (6 passed)
7 steps (7 passed)
0m14.372s

ナイスグリーン。

nice green.

3
3
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
3
3