Skip to content

Instantly share code, notes, and snippets.

@sawanoboly
Last active December 22, 2015 00:08
Show Gist options
  • Save sawanoboly/6386679 to your computer and use it in GitHub Desktop.
Save sawanoboly/6386679 to your computer and use it in GitHub Desktop.
cucumberのstepsでserverspecをつかう ref: http://qiita.com/sawanoboly/items/ced6f7f1bfe2a26943c2
require 'serverspec'
include RSpec::Matchers
include Serverspec::Helper::Exec # リモートが対象ならここはSsh
include Serverspec::Helper::DetectOS
# coding:utf-8
# language: ja
機能: epelリポジトリ
前提: 何かしらの仕組みで構築済みです
シナリオ: epelのリポジトリが登録されている
もし "/etc/yum.repos.d/epel.repo" ファイルが存在する
ならば "epel" の状態が "enable" だ
# coding: utf-8
もし(/^"(.*?)" ファイルが存在する$/) do |filename|
@file = Serverspec::Type::File.new(filename)
@file.should be_file
end
ならば(/^"(.*?)" の状態がenableだ$/) do |repo|
@file.should contain('^\s*enabled=1').from(/\[#{repo}\]/).to(/^\n/)
end
$ cucumber
# coding:utf-8
# language: ja
機能: サーバが適当なパッケージを導入している
前提: 何かしらの仕組みで構築済みです
シナリオテンプレート: パッケージチェック # features/package.feature:8
もしパッケージ "<pkgname>" の状態を調べる # features/step_definitions/package_steps.rb:4
ならばパッケージの状態は "<state>" で # features/step_definitions/package_steps.rb:8
サンプル:
| pkgname | state |
| git | installed |
| syslog-ng | not_installed |
| tmux | installed |
# coding:utf-8
# language: ja
機能: サーバが適当なデーモンを上げている
前提: 何かしらの仕組みで構築済みです
シナリオテンプレート: サービスチェック # features/service.feature:8
もしサービス "<servicename>" の状態を調べる # features/step_definitions/service_steps.rb:3
ならばサービスの状態は "<state>" で # features/step_definitions/service_steps.rb:7
かつサービスの自動起動は "<on_boot>" です # features/step_definitions/service_steps.rb:15
サンプル:
| servicename | state | on_boot |
| crond | running | enable |
| rsyslog | running | enable |
| mdmonitor | stopped | disable |
6 scenarios (6 passed)
15 steps (15 passed)
0m0.287s
# coding:utf-8
# language: ja
機能: サーバが適当なパッケージを導入している
前提: 何かしらの仕組みで構築済みです
シナリオテンプレート: パッケージチェック
もし パッケージ "<pkgname>" の状態を調べる
ならば パッケージの状態は "<state>" です
サンプル:
| pkgname | state |
| git | installed |
| syslog-ng | not_installed |
| tmux | installed |
# coding: utf-8
もし(/^パッケージ "(.*?)" の状態を調べる$/) do |pkgname|
@package = Serverspec::Type::Package.new(pkgname)
end
ならば(/^パッケージの状態は "(.*?)" で$/) do |state|
if state == 'installed'
@package.should be_installed
else
@package.should_not be_installed
end
end
# coding:utf-8
# language: ja
機能: サーバが適当なデーモンを上げている
前提: 何かしらの仕組みで構築済みです
シナリオテンプレート: サービスチェック
もし サービス "<servicename>" の状態を調べる
ならば サービスの状態は "<state>" で
かつ サービスの自動起動は "<on_boot>" です
サンプル:
| servicename | state | on_boot |
| crond | running | enable |
| rsyslog | running | enable |
| mdmonitor | stopped | disable |
# coding: utf-8
もし(/^サービス "(.*?)" の状態を調べる$/) do |servicename|
@service = Serverspec::Type::Service.new(servicename)
end
ならば(/^サービスの状態は "(.*?)" で$/) do |state|
if state == 'running'
@service.should be_running
else
@service.should_not be_running
end
end
ならば(/^サービスの自動起動は "(.*?)" です$/) do |on_boot|
if on_boot == 'enable'
@service.should be_enabled
else
@service.should_not be_enabled
end
end
Then(/^The server has cron entry '(.*)'/) do |line|
@cron = Serverspec::Type::Cron.new
Net::SSH.start('test.example.com', 'root') do |ssh|
expect(@cron.have_entry(line).with_user('www')).to be_true
end
end
You can implement step definitions for undefined steps with these snippets:
もし(/^"(.*?)" ファイルが存在する$/) do |arg1|
pending # express the regexp above with the code you wish you had
end
ならば(/^"(.*?)" の状態がenableだ$/) do |arg1|
pending # express the regexp above with the code you wish you had
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment