はじめに
以前の記事で Chef で実行したコードのテストコードとして、ServerSpec でのテスト実行を確認しました。
環境
・RHEL 8.10 ppc64le (on IBM Power S1012)
# cat /etc/os-release
NAME="Red Hat Enterprise Linux"
VERSION="8.10 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="8.10"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.10 (Ootpa)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:8::baseos"
HOME_URL="https://www.redhat.com/"
DOCUMENTATION_URL="https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_BUGZILLA_PRODUCT_VERSION=8.10
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8.10"
dnf の設定
下記の記事をご参考ください。
ruby の導入
導入可能バージョンの確認
[root@testrhel810 work]# dnf module list ruby
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered with an entitlement server. You can use subscription-manager to register.
Last metadata expiration check: 0:00:09 ago on Sun 07 Sep 2025 04:36:17 AM EDT.
RHEL810_AppStream
Name Stream Profiles Summary
ruby 2.5 [d] common [d] An interpreter of object-oriented scripting language
ruby 2.6 common [d] An interpreter of object-oriented scripting language
ruby 2.7 common [d] An interpreter of object-oriented scripting language
ruby 3.0 common [d] An interpreter of object-oriented scripting language
ruby 3.1 common [d] An interpreter of object-oriented scripting language
ruby 3.3 common [d] An interpreter of object-oriented scripting language
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled
ServerSpec のバージョンに合わせるために、ruby 3.0 を指定導入します。
[root@testrhel810 work]# dnf module enable ruby:3.0
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered with an entitlement server. You can use subscription-manager to register.
Last metadata expiration check: 0:00:24 ago on Sun 07 Sep 2025 04:36:17 AM EDT.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Enabling module streams:
ruby 3.0
Transaction Summary
================================================================================
Is this ok [y/N]: y
Complete!
- ruby の導入
dnf install ruby
を実行します。
実行ログ
[root@testrhel810 work]# dnf install ruby
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered with an entitlement server. You can use subscription-manager to register.
Last metadata expiration check: 0:01:38 ago on Sun 07 Sep 2025 04:36:17 AM EDT.
Dependencies resolved.
=============================================================================================
Package Arch Version Repository Size
=============================================================================================
Installing:
ruby ppc64le 3.0.4-141.module+el8.6.0+16311+3e5e17e9 RHEL-AppStream 90 k
Installing dependencies:
ruby-libs ppc64le 3.0.4-141.module+el8.6.0+16311+3e5e17e9 RHEL-AppStream 3.3 M
rubygem-io-console ppc64le 0.5.7-141.module+el8.6.0+16311+3e5e17e9 RHEL-AppStream 74 k
rubygem-json ppc64le 2.5.1-141.module+el8.6.0+16311+3e5e17e9 RHEL-AppStream 102 k
rubygem-psych ppc64le 3.3.2-141.module+el8.6.0+16311+3e5e17e9 RHEL-AppStream 100 k
Installing weak dependencies:
ruby-default-gems noarch 3.0.4-141.module+el8.6.0+16311+3e5e17e9 RHEL-AppStream 81 k
rubygem-bigdecimal ppc64le 3.0.0-141.module+el8.6.0+16311+3e5e17e9 RHEL-AppStream 105 k
rubygem-bundler noarch 2.2.33-141.module+el8.6.0+16311+3e5e17e9 RHEL-AppStream 450 k
rubygem-rdoc noarch 6.3.3-141.module+el8.6.0+16311+3e5e17e9 RHEL-AppStream 460 k
rubygems noarch 3.2.33-141.module+el8.6.0+16311+3e5e17e9 RHEL-AppStream 328 k
Transaction Summary
=============================================================================================
Install 10 Packages
Total size: 5.1 M
Installed size: 21 M
Is this ok [y/N]: y
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : ruby-libs-3.0.4-141.module+el8.6.0+16311+3e5e17e9. 1/10
Installing : rubygem-bigdecimal-3.0.0-141.module+el8.6.0+16311+ 2/10
Installing : ruby-default-gems-3.0.4-141.module+el8.6.0+16311+3 3/10
Installing : rubygem-bundler-2.2.33-141.module+el8.6.0+16311+3e 4/10
Installing : rubygem-io-console-0.5.7-141.module+el8.6.0+16311+ 5/10
Installing : rubygem-json-2.5.1-141.module+el8.6.0+16311+3e5e17 6/10
Installing : rubygem-psych-3.3.2-141.module+el8.6.0+16311+3e5e1 7/10
Installing : rubygem-rdoc-6.3.3-141.module+el8.6.0+16311+3e5e17 8/10
Installing : rubygems-3.2.33-141.module+el8.6.0+16311+3e5e17e9. 9/10
Installing : ruby-3.0.4-141.module+el8.6.0+16311+3e5e17e9.ppc64 10/10
Running scriptlet: ruby-3.0.4-141.module+el8.6.0+16311+3e5e17e9.ppc64 10/10
Verifying : ruby-3.0.4-141.module+el8.6.0+16311+3e5e17e9.ppc64 1/10
Verifying : ruby-default-gems-3.0.4-141.module+el8.6.0+16311+3 2/10
Verifying : ruby-libs-3.0.4-141.module+el8.6.0+16311+3e5e17e9. 3/10
Verifying : rubygem-bigdecimal-3.0.0-141.module+el8.6.0+16311+ 4/10
Verifying : rubygem-bundler-2.2.33-141.module+el8.6.0+16311+3e 5/10
Verifying : rubygem-io-console-0.5.7-141.module+el8.6.0+16311+ 6/10
Verifying : rubygem-json-2.5.1-141.module+el8.6.0+16311+3e5e17 7/10
Verifying : rubygem-psych-3.3.2-141.module+el8.6.0+16311+3e5e1 8/10
Verifying : rubygem-rdoc-6.3.3-141.module+el8.6.0+16311+3e5e17 9/10
Verifying : rubygems-3.2.33-141.module+el8.6.0+16311+3e5e17e9. 10/10
Installed products updated.
Installed:
ruby-3.0.4-141.module+el8.6.0+16311+3e5e17e9.ppc64le
ruby-default-gems-3.0.4-141.module+el8.6.0+16311+3e5e17e9.noarch
ruby-libs-3.0.4-141.module+el8.6.0+16311+3e5e17e9.ppc64le
rubygem-bigdecimal-3.0.0-141.module+el8.6.0+16311+3e5e17e9.ppc64le
rubygem-bundler-2.2.33-141.module+el8.6.0+16311+3e5e17e9.noarch
rubygem-io-console-0.5.7-141.module+el8.6.0+16311+3e5e17e9.ppc64le
rubygem-json-2.5.1-141.module+el8.6.0+16311+3e5e17e9.ppc64le
rubygem-psych-3.3.2-141.module+el8.6.0+16311+3e5e17e9.ppc64le
rubygem-rdoc-6.3.3-141.module+el8.6.0+16311+3e5e17e9.noarch
rubygems-3.2.33-141.module+el8.6.0+16311+3e5e17e9.noarch
Complete!
[root@testrhel810 work]#
- ruby と gem のバージョン確認
[root@testrhel810 work]# ruby -v
ruby 3.0.4p208 (2022-04-12 revision 3fa771dded) [powerpc64le-linux]
[root@testrhel810 work]# gem -v
3.2.33
- ServerSpec モジュールの展開
・unzip Serverspec_gems.zip
を実行します。
[root@testrhel810 work]# mkdir serverspec
[root@testrhel810 work]# mv Serverspec_gems.zip serverspec/
[root@testrhel810 work]# cd serverspec/
[root@testrhel810 serverspec]# ls
Serverspec_gems.zip
[root@testrhel810 serverspec]# unzip Serverspec_gems.zip
Archive: Serverspec_gems.zip
creating: Serverspec_gems/
inflating: Serverspec_gems/diff-lcs-1.5.0.gem
inflating: Serverspec_gems/multi_json-1.15.0.gem
inflating: Serverspec_gems/net-scp-4.0.0.gem
inflating: Serverspec_gems/net-ssh-7.1.0.gem
inflating: Serverspec_gems/net-telnet-0.1.1.gem
inflating: Serverspec_gems/rake-13.0.6.gem
inflating: Serverspec_gems/rspec-3.12.0.gem
inflating: Serverspec_gems/rspec-core-3.12.2.gem
inflating: Serverspec_gems/rspec-expectations-3.12.3.gem
inflating: Serverspec_gems/rspec-its-1.3.0.gem
inflating: Serverspec_gems/rspec-mocks-3.12.6.gem
inflating: Serverspec_gems/rspec-support-3.12.1.gem
inflating: Serverspec_gems/serverspec-2.42.2.gem
inflating: Serverspec_gems/sfl-2.3.gem
inflating: Serverspec_gems/specinfra-2.86.0.gem
-
gem install serverspec --local
を実行し、serverspec をインストールします。
gem install serverspec --loca 実行ログ
``` [root@testrhel810 serverspec]# cd Serverspec_gems/ [root@testrhel810 Serverspec_gems]# gem install serverspec --local Successfully installed sfl-2.3 Successfully installed net-telnet-0.1.1 Successfully installed net-ssh-7.1.0 Successfully installed net-scp-4.0.0 Successfully installed specinfra-2.86.0 Successfully installed multi_json-1.15.0 Successfully installed diff-lcs-1.5.0 Successfully installed rspec-support-3.12.1 Successfully installed rspec-expectations-3.12.3 Successfully installed rspec-core-3.12.2 Successfully installed rspec-its-1.3.0 Successfully installed rspec-mocks-3.12.6 Successfully installed rspec-3.12.0 Successfully installed serverspec-2.42.2 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-7.1.0 Installing ri documentation for net-ssh-7.1.0 Parsing documentation for net-scp-4.0.0 Installing ri documentation for net-scp-4.0.0 Parsing documentation for specinfra-2.86.0 Installing ri documentation for specinfra-2.86.0 Parsing documentation for multi_json-1.15.0 Installing ri documentation for multi_json-1.15.0 Parsing documentation for diff-lcs-1.5.0 Installing ri documentation for diff-lcs-1.5.0 Parsing documentation for rspec-support-3.12.1 Installing ri documentation for rspec-support-3.12.1 Parsing documentation for rspec-expectations-3.12.3 Installing ri documentation for rspec-expectations-3.12.3 Parsing documentation for rspec-core-3.12.2 Installing ri documentation for rspec-core-3.12.2 Parsing documentation for rspec-its-1.3.0 Installing ri documentation for rspec-its-1.3.0 Parsing documentation for rspec-mocks-3.12.6 Installing ri documentation for rspec-mocks-3.12.6 Parsing documentation for rspec-3.12.0 Installing ri documentation for rspec-3.12.0 Parsing documentation for serverspec-2.42.2 Installing ri documentation for serverspec-2.42.2 Done installing documentation for sfl, net-telnet, net-ssh, net-scp, specinfra, multi_json, diff-lcs, rspec-support, rspec-expectations, rspec-core, rspec-its, rspec-mocks, rspec, serverspec after 7 seconds 14 gems installed [root@testrhel810 Serverspec_gems]# ```-
serverspec-init
の実行
serverspec-init を実行するOS type と backend type の選択が出てきます。
OS type は RHEL のため、UN*X を、
backend type はローカル実行のため Exec (local) を選択しました。
# serverspec-init
Select OS type:
1) UN*X
2) Windows
Select number: 1
Select a backend type:
1) SSH
2) Exec (local)
Select number: 2
+ spec/
+ spec/localhost/
+ spec/localhost/sample_spec.rb
+ spec/spec_helper.rb
+ Rakefile
+ .rspec
以上で ServerSpec 稼働環境が整いました。
実行コード作成
・
└── spec
├── attributes.yml # テスト対象のノード属性を定義する YAML ファイル
│
├── localhost # テスト対象ホスト名のテストファイルを格納するディレクトリ
│ │
│ └── lvm_filter_spec.rb # LVM のフィルター設定に関するテストコード
│
└── spec_helper.rb # 全てのテストファイルで読み込まれ、共通の前処理や設定を提供
・spec/attributes.yml
テスト属性のファイルでは、対象行を確認します。
rhel8_os_base:
OS_Base:
Other:
LVM:
file:
/etc/lvm/lvm.conf:
content:
filter: '[ "a|/dev/md.*|", "a|/dev/mapper/mpath.*|", "r|.*|" ]'
テストコードの内容です。
・spec/localhost/lvm_filter_spec.rb
###############################################################################
# name : LVM フィルター設定確認
# serverspec : lvm_filter_spec.rb
###############################################################################
#-----------------------------------------------------------------------------#
require 'spec_helper.rb'
dirname = File.basename(File.dirname(__FILE__))
filename = File.basename(__FILE__).gsub('_spec.rb', '')
#-----------------------------------------------------------------------------#
# 1.LVM 設定
#-----------------------------------------------------------------------------#
# 1-1.filter 設定確認
# /etc/lvm/lvm.conf
unless property['rhel8_os_base']['OS_Base']['Other']['LVM'].nil?
description = 'LVM設定_filterの確認'
testname = "UT_#{dirname}_#{filename}:#{description}"
describe "#{testname}\n" do
hash = property['rhel8_os_base']['OS_Base']['Other']['LVM']
hash['file'].each do |f_name, f_attr|
case f_name
when '/etc/lvm/lvm.conf'
f_attr['content'].each do |key, val|
describe file(f_name) do
# インデント(タブまたはスペース)を許容しつつ、正確な内容を確認
expected = Regexp.escape(val)
its(:content) { should match(/^\s*#{Regexp.escape(key)}\s*=\s*#{expected}$/) }
end
end
end
end
end
end
前処理では、変数ファイルの読み込みを記載しています。
・ spec/spec_helper.rb
require 'serverspec'
require 'yaml'
set :backend, :exec
# 属性ファイルの読み込み
property_file = File.expand_path('../attributes.yml', __FILE__)
if File.exist?(property_file)
set_property YAML.load_file(property_file)
else
set_property({})
end
ServerSpec lvm_filter_spec.rb の実行
テストケースは/etc/lvm/lvm.conf に追加した行が入っているかを確認します。
[root@testrhel810 chef_test]# rspec spec/localhost/lvm_filter_spec.rb
UT_localhost_lvm_filter:LVM設定_filterの確認
File "/etc/lvm/lvm.conf"
content
is expected to match /^\s*filter\s*=\s*\[\ "a\|\/dev\/md\.\*\|",\ "a\|\/dev\/mapper\/mpath\.\*\|",\ "r\|\.\*\|"\ \]$/
Finished in 0.03409 seconds (files took 0.3039 seconds to load)
1 example, 0 failures
0 failures で返ってきました。テストは問題なしでした。
おわりに
こちらもAIと壁打ちして設定とコード作成を実施しました。
正規表現記載は人間側の方では苦手ですが、AIの方ではすぐに出してくれるので助かります。
以上です。