Serverspecを使ってテストは自動で実施しよう

コンピュータ
スポンサーリンク

Serverspecとは、winrmやsshで対象ホストへ接続し、設定値やフィルの有無など確認することができるツールです。もちろんAWSやAzureに構築しているサーバに対しても実施可能です。

本記事は、specを動かすための最低限の設定のみ記載しております。多数のホストへの実行、デバッグ出力しながらの実行などは別途時間があれば記事を準備いたします。

Linux側は少しハマったのでメモっておきます。

テストをコード化してしまい、単純作業はspecで回しましょう。

スポンサーリンク

Windows環境にServerspecを準備

rubyの導入

Rubyのオフィシャルより、Windows向けのパッケージダウンロード

Rubyのページより、Windows向けのパッケージを環境に合わせてダウンロードする

パッケージは、普通に追加すればok。

serverspec、rake、winrmの導入

gemで追加するが、proxy経由の場合、オプションpでプロキシ経由で導入可能。

プロキシ経由の場合:「gem install rake -p http://proxy.test.local:8080」


C:\Windows\system32>gem install rake
"rake" conflicts with C:/Ruby31-x64/bin/rake
Overwrite the executable? [yN]  y
Successfully installed rake-13.0.6
Parsing documentation for rake-13.0.6
Installing ri documentation for rake-13.0.6
Done installing documentation for rake after 1 seconds
1 gem installed

C:\Windows\system32>
C:\Windows\system32>
C:\Windows\system32>gem install serverspec
Fetching net-telnet-0.1.1.gem
Fetching specinfra-2.83.1.gem
Fetching net-ssh-6.1.0.gem
Fetching net-scp-3.0.0.gem
Fetching rspec-expectations-3.10.2.gem
Fetching rspec-support-3.10.3.gem
Fetching diff-lcs-1.5.0.gem
Fetching sfl-2.3.gem
Fetching serverspec-2.42.0.gem
Fetching rspec-core-3.10.2.gem
Fetching rspec-its-1.3.0.gem
Fetching rspec-mocks-3.10.3.gem
Fetching rspec-3.10.0.gem
Fetching multi_json-1.15.0.gem
Successfully installed sfl-2.3
Successfully installed net-telnet-0.1.1
Successfully installed net-ssh-6.1.0
Successfully installed net-scp-3.0.0
Successfully installed specinfra-2.83.1
Successfully installed rspec-support-3.10.3
Successfully installed diff-lcs-1.5.0
Successfully installed rspec-expectations-3.10.2
Successfully installed rspec-core-3.10.2
Successfully installed rspec-its-1.3.0
Successfully installed rspec-mocks-3.10.3
Successfully installed rspec-3.10.0
Successfully installed multi_json-1.15.0
Successfully installed serverspec-2.42.0
Parsing documentation for sfl-2.3
Installing ri documentation for sfl-2.3
Parsing documentation for net-telnet-0.1.1
Installing ri documentation for net-telnet-0.1.1
Parsing documentation for net-ssh-6.1.0
Installing ri documentation for net-ssh-6.1.0
Parsing documentation for net-scp-3.0.0
Installing ri documentation for net-scp-3.0.0
Parsing documentation for specinfra-2.83.1
Installing ri documentation for specinfra-2.83.1
Parsing documentation for rspec-support-3.10.3
Installing ri documentation for rspec-support-3.10.3
Parsing documentation for diff-lcs-1.5.0
Installing ri documentation for diff-lcs-1.5.0
Parsing documentation for rspec-expectations-3.10.2
Installing ri documentation for rspec-expectations-3.10.2
Parsing documentation for rspec-core-3.10.2
Installing ri documentation for rspec-core-3.10.2
Parsing documentation for rspec-its-1.3.0
Installing ri documentation for rspec-its-1.3.0
Parsing documentation for rspec-mocks-3.10.3
Installing ri documentation for rspec-mocks-3.10.3
Parsing documentation for rspec-3.10.0
Installing ri documentation for rspec-3.10.0
Parsing documentation for multi_json-1.15.0
Installing ri documentation for multi_json-1.15.0
Parsing documentation for serverspec-2.42.0
Installing ri documentation for serverspec-2.42.0
Done installing documentation for sfl, net-telnet, net-ssh, net-scp, specinfra, rspec-support, diff-lcs, rspec-expectations, rspec-core, rspec-its, rspec-mocks, rspec, multi_json, serverspec after 19 seconds
14 gems installed

C:\Windows\system32>
C:\Windows\system32>
C:\Windows\system32>
C:\Windows\system32>gem install winrm
Fetching httpclient-2.8.3.gem
Fetching ffi-1.15.5-x64-mingw-ucrt.gem
Fetching builder-3.2.4.gem
Fetching gyoku-1.3.1.gem
Fetching rubyntlm-0.6.3.gem
Fetching little-plugger-1.1.4.gem
Fetching nori-2.6.0.gem
Fetching gssapi-1.3.1.gem
Fetching winrm-2.3.6.gem
Fetching logging-2.3.0.gem
Fetching erubi-1.10.0.gem
Successfully installed rubyntlm-0.6.3
Successfully installed nori-2.6.0
Successfully installed little-plugger-1.1.4
Successfully installed logging-2.3.0
Successfully installed httpclient-2.8.3
Successfully installed builder-3.2.4
Successfully installed gyoku-1.3.1
Successfully installed ffi-1.15.5-x64-mingw-ucrt
Successfully installed gssapi-1.3.1
Successfully installed erubi-1.10.0
Successfully installed winrm-2.3.6
Parsing documentation for rubyntlm-0.6.3
Installing ri documentation for rubyntlm-0.6.3
Parsing documentation for nori-2.6.0
Installing ri documentation for nori-2.6.0
Parsing documentation for little-plugger-1.1.4
Installing ri documentation for little-plugger-1.1.4
Parsing documentation for logging-2.3.0
Installing ri documentation for logging-2.3.0
Parsing documentation for httpclient-2.8.3
Installing ri documentation for httpclient-2.8.3
Parsing documentation for builder-3.2.4
Installing ri documentation for builder-3.2.4
Parsing documentation for gyoku-1.3.1
Installing ri documentation for gyoku-1.3.1
Parsing documentation for ffi-1.15.5-x64-mingw-ucrt
Installing ri documentation for ffi-1.15.5-x64-mingw-ucrt
Parsing documentation for gssapi-1.3.1
Installing ri documentation for gssapi-1.3.1
Parsing documentation for erubi-1.10.0
Installing ri documentation for erubi-1.10.0
Parsing documentation for winrm-2.3.6
Installing ri documentation for winrm-2.3.6
Done installing documentation for rubyntlm, nori, little-plugger, logging, httpclient, builder, gyoku, ffi, gssapi, erubi, winrm after 7 seconds
11 gems installed

C:\Windows\system32>
C:\Windows\system32>
C:\Windows\system32>
C:\Windows\system32>

初期設定

C:\Windows\system32>
C:\Windows\system32>cd c:\

C:\>mkdir serverspec
C:\Windows\system32>cd /serverspec

C:\serverspec>
C:\serverspec>
C:\serverspec>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

Input target host name: win2022
 + spec/
 + spec/win2022/
 + spec/win2022/sample_spec.rb
C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/serverspec-2.42.0/lib/serverspec/setup.rb:155: warning: Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments.
C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/serverspec-2.42.0/lib/serverspec/setup.rb:155: warning: Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode: ...) instead.
 + spec/spec_helper.rb
 + Rakefile
 + .rspec

C:\serverspec>
C:\serverspec>
C:\serverspec>cd spec



spec配下にspec_helper.rbがあるため修正する。

require 'serverspec'
require 'winrm'

set :backend, :winrm

user = <username>
pass = <password>
endpoint = "http://#{ENV['TARGET_HOST']}:5985/wsman"

if Gem::Version.new(WinRM::VERSION) < Gem::Version.new('2')
  winrm = ::WinRM::WinRMWebService.new(endpoint, :ssl, :user => user, :pass => pass, :basic_auth_only => true)
  winrm.set_timeout 300 # 5 minutes max timeout for any operation
else
  opts = {
    user: user,
    password: pass,
    endpoint: endpoint,
    operation_timeout: 300,
    no_ssl_peer_verification: false,
  }

  winrm = ::WinRM::Connection.new(opts)
end

Specinfra.configuration.winrm = winrm

上記がデフォルトであり、以下の通り修正。

require 'serverspec'
require 'winrm'

set :backend, :winrm

opts = {
  user: "administrator",
  password: "password",
  endpoint: "http://#{ENV['TARGET_HOST']}:5985/wsman",
  operation_timeout: 300,
}

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

テスト項目を、serverspec-initを実行した際に作成されたフォルダ配下に準備。以下は、hostnameを確認するファイルを準備した。

c:\serverspec\spec>cd win2022

c:\serverspec\spec\win2022>
c:\serverspec\spec\win2022>

c:\serverspec\spec\win2022>type hostname_spec.rb
require 'spec_helper'

# hostname
describe command('hostname') do
  its(:stdout) { should match /win2022/ }
end

c:\serverspec\spec\win2022>

動作確認

win2022というホストに対し実施するため、DNSで名前解決できない場合、hostsファイルでエントリを追加しておく。

以下、成功パターン。

c:\serverspec>rake spec:win2022
C:/Ruby31-x64/bin/ruby.exe -I'C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/rspec-support-3.10.3/lib';'C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/rspec-core-3.10.2/lib' 'C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/rspec-core-3.10.2/exe/rspec' --pattern 'spec/win2022/*_spec.rb'

Command "hostname"
  stdout
    is expected to match /win2022/

Finished in 0.94322 seconds (files took 3.52 seconds to load)
1 example, 0 failures


c:\serverspec>

以下、失敗パターン。。設定値の確認をwin10にしたが、取得した値はwin2022のため失敗している。

c:\serverspec>rake spec:win2022
C:/Ruby31-x64/bin/ruby.exe -I'C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/rspec-support-3.10.3/lib';'C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/rspec-core-3.10.2/lib' 'C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/rspec-core-3.10.2/exe/rspec' --pattern 'spec/win2022/*_spec.rb'

Command "hostname"
  stdout
    is expected to match /win10/ (FAILED - 1)

Failures:

  1) Command "hostname" stdout is expected to match /win10/
     On host `win2022'
     Failure/Error: its(:stdout) { should match /win10/ }
       expected "win2022\r\n" to match /win10/
       Diff:
       @@ -1 +1 @@
       -/win10/
       +win2022

       hostname
       win2022

     # ./spec/win2022/hostname_spec.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.98427 seconds (files took 3.37 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/win2022/hostname_spec.rb:5 # Command "hostname" stdout is expected to match /win10/

C:/Ruby31-x64/bin/ruby.exe -I'C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/rspec-support-3.10.3/lib';'C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/rspec-core-3.10.2/lib' 'C:/Ruby31-x64/lib/ruby/gems/3.1.0/gems/rspec-core-3.10.2/exe/rspec' --pattern 'spec/win2022/*_spec.rb' failed

c:\serverspec>

Linux環境にServerspecを準備

Red Hat Enterprise Linux(RHEL)、CnetOS関連に導入する場合の手順となります。RHELもCnetOSもwinrmの導入に失敗しましたが、同じ手順で回避できることを確認済みです。

rubyの導入

[root@localhost ~]# dnf install ruby
CentOS Stream 8 - AppStream                                                                                             21 MB/s |  19 MB     00:00
CentOS Stream 8 - BaseOS                                                                                               9.9 MB/s |  18 MB     00:01
CentOS Stream 8 - Extras                                                                                                49 kB/s |  17 kB     00:00
依存関係が解決しました。
=======================================================================================================================================================
 パッケージ                           アーキテクチャー       バージョン                                                リポジトリー              サイズ
=======================================================================================================================================================
インストール:
 ruby                                 x86_64                 2.5.9-107.module_el8.5.0+811+d98a1657                     appstream                  87 k
依存関係のインストール:
 ruby-irb                             noarch                 2.5.9-107.module_el8.5.0+811+d98a1657                     appstream                 102 k
 ruby-libs                            x86_64                 2.5.9-107.module_el8.5.0+811+d98a1657                     appstream                 2.9 M
 rubygem-json                         x86_64                 2.1.0-107.module_el8.5.0+811+d98a1657                     appstream                  90 k
 rubygem-psych                        x86_64                 3.0.2-107.module_el8.5.0+811+d98a1657                     appstream                  95 k
弱い依存関係のインストール:
 rubygem-bigdecimal                   x86_64                 1.3.4-107.module_el8.5.0+811+d98a1657                     appstream                  97 k
 rubygem-did_you_mean                 noarch                 1.2.0-107.module_el8.5.0+811+d98a1657                     appstream                  81 k
 rubygem-io-console                   x86_64                 0.4.6-107.module_el8.5.0+811+d98a1657                     appstream                  67 k
 rubygem-openssl                      x86_64                 2.1.2-107.module_el8.5.0+811+d98a1657                     appstream                 189 k
 rubygem-rdoc                         noarch                 6.0.1.1-107.module_el8.5.0+811+d98a1657                   appstream                 456 k
 rubygems                             noarch                 2.7.6.3-107.module_el8.5.0+811+d98a1657                   appstream                 308 k
モジュールストリームの有効化中:
 ruby                                                        2.5

トランザクションの概要
=======================================================================================================================================================
インストール  11 パッケージ

ダウンロードサイズの合計: 4.5 M
インストール後のサイズ: 14 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
(1/11): ruby-2.5.9-107.module_el8.5.0+811+d98a1657.x86_64.rpm                                                          1.5 MB/s |  87 kB     00:00
(2/11): ruby-irb-2.5.9-107.module_el8.5.0+811+d98a1657.noarch.rpm                                                      1.7 MB/s | 102 kB     00:00
(3/11): rubygem-bigdecimal-1.3.4-107.module_el8.5.0+811+d98a1657.x86_64.rpm                                            3.0 MB/s |  97 kB     00:00
(4/11): rubygem-did_you_mean-1.2.0-107.module_el8.5.0+811+d98a1657.noarch.rpm                                          2.4 MB/s |  81 kB     00:00
(5/11): rubygem-io-console-0.4.6-107.module_el8.5.0+811+d98a1657.x86_64.rpm                                            1.8 MB/s |  67 kB     00:00
(6/11): rubygem-json-2.1.0-107.module_el8.5.0+811+d98a1657.x86_64.rpm                                                  2.4 MB/s |  90 kB     00:00
(7/11): ruby-libs-2.5.9-107.module_el8.5.0+811+d98a1657.x86_64.rpm                                                      21 MB/s | 2.9 MB     00:00
(8/11): rubygem-psych-3.0.2-107.module_el8.5.0+811+d98a1657.x86_64.rpm                                                 4.6 MB/s |  95 kB     00:00
(9/11): rubygem-openssl-2.1.2-107.module_el8.5.0+811+d98a1657.x86_64.rpm                                               6.9 MB/s | 189 kB     00:00
(10/11): rubygem-rdoc-6.0.1.1-107.module_el8.5.0+811+d98a1657.noarch.rpm                                               8.7 MB/s | 456 kB     00:00
(11/11): rubygems-2.7.6.3-107.module_el8.5.0+811+d98a1657.noarch.rpm                                                   5.8 MB/s | 308 kB     00:00
-------------------------------------------------------------------------------------------------------------------------------------------------------
合計                                                                                                                   6.6 MB/s | 4.5 MB     00:00
CentOS Stream 8 - AppStream                                                                                            1.1 MB/s | 1.6 kB     00:00
GPG 鍵 0x8483C65D をインポート中:
 Userid     : "CentOS (CentOS Official Signing Key) <security@centos.org>"
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
これでよろしいですか? [y/N]: y
鍵のインポートに成功しました
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                                               1/1
  インストール中   : ruby-libs-2.5.9-107.module_el8.5.0+811+d98a1657.x86_64                                                                       1/11
  インストール中   : ruby-irb-2.5.9-107.module_el8.5.0+811+d98a1657.noarch                                                                        2/11
  インストール中   : rubygem-bigdecimal-1.3.4-107.module_el8.5.0+811+d98a1657.x86_64                                                              3/11
  インストール中   : rubygem-did_you_mean-1.2.0-107.module_el8.5.0+811+d98a1657.noarch                                                            4/11
  インストール中   : rubygem-io-console-0.4.6-107.module_el8.5.0+811+d98a1657.x86_64                                                              5/11
  インストール中   : rubygem-json-2.1.0-107.module_el8.5.0+811+d98a1657.x86_64                                                                    6/11
  インストール中   : rubygem-openssl-2.1.2-107.module_el8.5.0+811+d98a1657.x86_64                                                                 7/11
  インストール中   : rubygem-psych-3.0.2-107.module_el8.5.0+811+d98a1657.x86_64                                                                   8/11
  インストール中   : rubygem-rdoc-6.0.1.1-107.module_el8.5.0+811+d98a1657.noarch                                                                  9/11
  インストール中   : rubygems-2.7.6.3-107.module_el8.5.0+811+d98a1657.noarch                                                                     10/11
  インストール中   : ruby-2.5.9-107.module_el8.5.0+811+d98a1657.x86_64                                                                           11/11
  scriptletの実行中: ruby-2.5.9-107.module_el8.5.0+811+d98a1657.x86_64                                                                           11/11
  検証             : ruby-2.5.9-107.module_el8.5.0+811+d98a1657.x86_64                                                                            1/11
  検証             : ruby-irb-2.5.9-107.module_el8.5.0+811+d98a1657.noarch                                                                        2/11
  検証             : ruby-libs-2.5.9-107.module_el8.5.0+811+d98a1657.x86_64                                                                       3/11
  検証             : rubygem-bigdecimal-1.3.4-107.module_el8.5.0+811+d98a1657.x86_64                                                              4/11
  検証             : rubygem-did_you_mean-1.2.0-107.module_el8.5.0+811+d98a1657.noarch                                                            5/11
  検証             : rubygem-io-console-0.4.6-107.module_el8.5.0+811+d98a1657.x86_64                                                              6/11
  検証             : rubygem-json-2.1.0-107.module_el8.5.0+811+d98a1657.x86_64                                                                    7/11
  検証             : rubygem-openssl-2.1.2-107.module_el8.5.0+811+d98a1657.x86_64                                                                 8/11
  検証             : rubygem-psych-3.0.2-107.module_el8.5.0+811+d98a1657.x86_64                                                                   9/11
  検証             : rubygem-rdoc-6.0.1.1-107.module_el8.5.0+811+d98a1657.noarch                                                                 10/11
  検証             : rubygems-2.7.6.3-107.module_el8.5.0+811+d98a1657.noarch                                                                     11/11

インストール済み:
  ruby-2.5.9-107.module_el8.5.0+811+d98a1657.x86_64                           ruby-irb-2.5.9-107.module_el8.5.0+811+d98a1657.noarch
  ruby-libs-2.5.9-107.module_el8.5.0+811+d98a1657.x86_64                      rubygem-bigdecimal-1.3.4-107.module_el8.5.0+811+d98a1657.x86_64
  rubygem-did_you_mean-1.2.0-107.module_el8.5.0+811+d98a1657.noarch           rubygem-io-console-0.4.6-107.module_el8.5.0+811+d98a1657.x86_64
  rubygem-json-2.1.0-107.module_el8.5.0+811+d98a1657.x86_64                   rubygem-openssl-2.1.2-107.module_el8.5.0+811+d98a1657.x86_64
  rubygem-psych-3.0.2-107.module_el8.5.0+811+d98a1657.x86_64                  rubygem-rdoc-6.0.1.1-107.module_el8.5.0+811+d98a1657.noarch
  rubygems-2.7.6.3-107.module_el8.5.0+811+d98a1657.noarch

完了しました!
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

serverspec、rake、winrmの導入

winrmで導入失敗する場合、ruby-develのパッケージ追加で回避できました。

[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# gem install rake
Fetching: rake-13.0.6.gem (100%)
Successfully installed rake-13.0.6
1 gem installed
[root@localhost ~]#
[root@localhost ~]# gem install serverspec
Fetching: rspec-support-3.10.3.gem (100%)
Successfully installed rspec-support-3.10.3
Fetching: diff-lcs-1.5.0.gem (100%)
Successfully installed diff-lcs-1.5.0
Fetching: rspec-mocks-3.10.3.gem (100%)
Successfully installed rspec-mocks-3.10.3
Fetching: rspec-expectations-3.10.2.gem (100%)
Successfully installed rspec-expectations-3.10.2
Fetching: rspec-core-3.10.2.gem (100%)
Successfully installed rspec-core-3.10.2
Fetching: rspec-3.10.0.gem (100%)
Successfully installed rspec-3.10.0
Fetching: rspec-its-1.3.0.gem (100%)
Successfully installed rspec-its-1.3.0
Fetching: multi_json-1.15.0.gem (100%)
Successfully installed multi_json-1.15.0
Fetching: sfl-2.3.gem (100%)
Successfully installed sfl-2.3
Fetching: net-telnet-0.1.1.gem (100%)
Successfully installed net-telnet-0.1.1
Fetching: net-ssh-6.1.0.gem (100%)
Successfully installed net-ssh-6.1.0
Fetching: net-scp-3.0.0.gem (100%)
Successfully installed net-scp-3.0.0
Fetching: specinfra-2.83.1.gem (100%)
Successfully installed specinfra-2.83.1
Fetching: serverspec-2.42.0.gem (100%)
Successfully installed serverspec-2.42.0
14 gems installed
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# gem install winrm
Fetching: rubyntlm-0.6.3.gem (100%)
Successfully installed rubyntlm-0.6.3
Fetching: nori-2.6.0.gem (100%)
Successfully installed nori-2.6.0
Fetching: little-plugger-1.1.4.gem (100%)
Successfully installed little-plugger-1.1.4
Fetching: logging-2.3.0.gem (100%)
Successfully installed logging-2.3.0
Fetching: httpclient-2.8.3.gem (100%)
Successfully installed httpclient-2.8.3
Fetching: builder-3.2.4.gem (100%)
Successfully installed builder-3.2.4
Fetching: gyoku-1.3.1.gem (100%)
Successfully installed gyoku-1.3.1
Fetching: ffi-1.15.5.gem (100%)
Building native extensions. This could take a while...
ERROR:  Error installing winrm:
        ERROR: Failed to build gem native extension.

    current directory: /usr/local/share/gems/gems/ffi-1.15.5/ext/ffi_c
/usr/bin/ruby -r ./siteconf20220205-33159-ikkglz.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/share/include/ruby.h

extconf failed, exit code 1

Gem files will remain installed in /usr/local/share/gems/gems/ffi-1.15.5 for inspection.
Results logged to /usr/local/lib64/gems/ruby/ffi-1.15.5/gem_make.out
[root@localhost ~]#
gem install bundler
Fetching: bundler-2.3.6.gem (100%)
Successfully installed bundler-2.3.6
1 gem installed
[root@localhost ~]#

ここで、失敗するようであれば、ruby-develを追加で導入すると。。。


[root@localhost ~]#
[root@localhost ~]# dnf install ruby-devel
メタデータの期限切れの最終確認: 0:10:39 時間前の 2022年02月05日 04時15分10秒 に実施しました。
依存関係が解決しました。
=======================================================================================================================================================
 パッケージ                    アーキテクチャー          バージョン                                                 リポジトリー                 サイズ
=======================================================================================================================================================
インストール:
 ruby-devel                    x86_64                    2.5.9-107.module_el8.5.0+811+d98a1657                      appstream                    126 k

トランザクションの概要
=======================================================================================================================================================
インストール  1 パッケージ

ダウンロードサイズの合計: 126 k
インストール後のサイズ: 294 k
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
ruby-devel-2.5.9-107.module_el8.5.0+811+d98a1657.x86_64.rpm                                                            2.6 MB/s | 126 kB     00:00
-------------------------------------------------------------------------------------------------------------------------------------------------------
合計                                                                                                                   172 kB/s | 126 kB     00:00
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                                                                               1/1
  インストール中   : ruby-devel-2.5.9-107.module_el8.5.0+811+d98a1657.x86_64                                                                       1/1
  scriptletの実行中: ruby-devel-2.5.9-107.module_el8.5.0+811+d98a1657.x86_64                                                                       1/1
  検証             : ruby-devel-2.5.9-107.module_el8.5.0+811+d98a1657.x86_64                                                                       1/1

インストール済み:
  ruby-devel-2.5.9-107.module_el8.5.0+811+d98a1657.x86_64

完了しました!
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# gem install winrm
Building native extensions. This could take a while...
Successfully installed ffi-1.15.5
Fetching: gssapi-1.3.1.gem (100%)
Successfully installed gssapi-1.3.1
Fetching: erubi-1.10.0.gem (100%)
Successfully installed erubi-1.10.0
Fetching: winrm-2.3.6.gem (100%)
Successfully installed winrm-2.3.6
4 gems installed
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]#

初期設定

[root@localhost ~]# cd /
[root@localhost /]# mkdir serverspec
[root@localhost /]#
[root@localhost /]#
[root@localhost /]#
[root@localhost /]# cd serverspec/
[root@localhost serverspec]#
[root@localhost serverspec]#
[root@localhost serverspec]# 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

Input target host name: win2022
 + spec/
 + spec/win2022/
 + spec/win2022/sample_spec.rb
 + spec/spec_helper.rb
 + Rakefile
 + .rspec
[root@localhost serverspec]#
[root@localhost serverspec]#
[root@localhost serverspec]#
[root@localhost serverspec]# cd spec/
[root@localhost spec]#

Windows同様、spec配下のspec_helper.rbを修正する。

require 'serverspec'
require 'winrm'

set :backend, :winrm

user = <username>
pass = <password>
endpoint = "http://#{ENV['TARGET_HOST']}:5985/wsman"

if Gem::Version.new(WinRM::VERSION) < Gem::Version.new('2')
  winrm = ::WinRM::WinRMWebService.new(endpoint, :ssl, :user => user, :pass => pass, :basic_auth_only => true)
  winrm.set_timeout 300 # 5 minutes max timeout for any operation
else
  opts = {
    user: user,
    password: pass,
    endpoint: endpoint,
    operation_timeout: 300,
    no_ssl_peer_verification: false,
  }

  winrm = ::WinRM::Connection.new(opts)
end

Specinfra.configuration.winrm = winrm
~

上記がデフォルトのため、以下の通り修正する。Winodwsと全く同じ内容です。

[root@localhost spec]# vi /serverspec/spec/spec_helper.rb

require 'serverspec'
require 'winrm'

set :backend, :winrm

opts = {
  user: "administrator",
  password: "password",
  endpoint: "http://#{ENV['TARGET_HOST']}:5985/wsman",
  operation_timeout: 300,
}

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

テスト項目も、Windows側で準備したものを、そのまま流用可能。

[root@localhost serverspec]# cd spec/win2022/
[root@localhost win2022]# ls
hostname_spec.rb
[root@localhost win2022]#
[root@localhost win2022]#
[root@localhost win2022]#
[root@localhost win2022]# cat hostname_spec.rb
require 'spec_helper'

# hostname
describe command('hostname') do
  its(:stdout) { should match /win2022/ }
end

動作確認

動作確認も、Winodwsと全く同じ手順となります。

[root@localhost serverspec]# rake spec:win2022
/usr/bin/ruby -I/usr/local/share/gems/gems/rspec-support-3.10.3/lib:/usr/local/share/gems/gems/rspec-core-3.10.2/lib /usr/local/share/gems/gems/rspec-core-3.10.2/exe/rspec --pattern spec/win2022/\*_spec.rb

Command "hostname"
  stdout
    is expected to match /win2022/

Finished in 0.6971 seconds (files took 0.48753 seconds to load)
1 example, 0 failures

[root@localhost serverspec]#

失敗パターンは以下の通り。

[root@localhost serverspec]# rake spec:win2022
/usr/bin/ruby -I/usr/local/share/gems/gems/rspec-support-3.10.3/lib:/usr/local/share/gems/gems/rspec-core-3.10.2/lib /usr/local/share/gems/gems/rspec-core-3.10.2/exe/rspec --pattern spec/win2022/\*_spec.rb

Command "hostname"
  stdout
    is expected to match /win10/ (FAILED - 1)

Failures:

  1) Command "hostname" stdout is expected to match /win10/
     On host `win2022'
     Failure/Error: its(:stdout) { should match /win10/ }
       expected "win2022\r\n" to match /win10/
       Diff:
       @@ -1 +1 @@
       -/win10/
       +win2022

       hostname
       win2022

     # ./spec/win2022/hostname_spec.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.6436 seconds (files took 0.4183 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/win2022/hostname_spec.rb:5 # Command "hostname" stdout is expected to match /win10/

/usr/bin/ruby -I/usr/local/share/gems/gems/rspec-support-3.10.3/lib:/usr/local/share/gems/gems/rspec-core-3.10.2/lib /usr/local/share/gems/gems/rspec-core-3.10.2/exe/rspec --pattern spec/win2022/\*_spec.rb failed
[root@localhost serverspec]#
タイトルとURLをコピーしました