LoginSignup
2
3

More than 3 years have passed since last update.

serverspecでWindowsをテストするための設定

Posted at

仕事でWindowsサーバをserverspecでテストするぞ!ということがあったので、その時の設定をまとめておきます。
同じようなことをやろうとしている人の助けになれば幸いです。

前提条件

Gemfileにserverspec、winrm等、必要なgemを書いてインストールする。
今回はserverspec(2.41.3)を使用しています。
テスト対象のWindowsサーバにWinRM(Windows Remote Management)がインストールされ、初期設定が完了していること。

serverspecの設定

serverspecを実行する環境にserverspecをインストールする。
serverspec用のディレクトリを作る。

$ mkdir serverspec
$ cd serverspec

serverspec-initコマンドを実行し、初期設定を行う。
Select OS typeは「2」、Select a backend type「1」を選択する。

$ serverspec-init
Select OS type:

  1) UN*X
  2) Windows

Select number: 2

Select a backend type:

  1) WinRM
  2) Cmd (local)

Select number: 1

Vagrant instance y/n: n
Input target host name: test

 + spec/
 + spec/test/
 + spec/test/httpd_spec.rb
 + spec/spec_helper.rb

spec_helper.rbはこのままでは使えないので、書き換えます。

spec/spec_helper.rb
require 'serverspec'
require 'winrm'

set :backend, :winrm

#対象ホストのIPアドレス、またはサーバ名
host = "test_server"

opts = {
  user = "(WIndowsユーザー名)",
  pass = "(ユーザのパスワード)",
  endpoint = "http://"+ host +":5985/wsman",
  operation_timeout: 300 ,
}

winrm = WinRM::Connection.new(opts)
Specinfra.configuration.winrm = winrm

デフォルトで作成されるhttpd_spec.rbは使わないので削除しちゃってください。
とりあえず確認用の簡単なシナリオを作成しておきましょう。

spec/test/test_spec.rb
require 'spec_helper'

describe command('hostname') do
  its(:stdout) { should match /test-server/ }
end

テスト実行

以下のコマンドでテストを実行

rake spec
/usr/local/rvm/rubies/ruby-1.9.3-p448/bin/ruby -S rspec spec/test/test_spec.rb
.

Finished in 0.64601 seconds
1 example, 0 failures

これでserverspecでWindowsのテストが出来るようになるはずです。
後はserverspec-runnerなどを使えばさらに使いやすくなるのではないでしょうか。

参考

serverspecでWindowsのテストをお試しする

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