Serverspec テストスクリプトの文法のサンプル

環境
Serverspec
RedHat 8.6

操作例
ファイル名 httpd_spec.rb
1.spec/spec_helper.rbを実行する
require 'spec_helper’

2.「httpd」パッケージに関するテスト

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
describe package('httpd') do
it { should be_installed } ←パッケージがインストールされているか?
end
describe package('httpd') do it { should be_installed } ←パッケージがインストールされているか? end
describe package('httpd') do  
  it { should be_installed }  ←パッケージがインストールされているか?
end

3.80番ポートに関するテスト

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
describe port(80) do
it { should be_listening } ←ポートが待ち受け状態になっているか?
end
describe port(80) do it { should be_listening } ←ポートが待ち受け状態になっているか? end
describe port(80) do 
  it { should be_listening }  ←ポートが待ち受け状態になっているか?
end

4.「httpd」サービスに関するテスト

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
describe service('httpd') do
it { should be_enabled } ←サービスが有効になっているか?
it { should be_running } ←サービスが実行されているか?
end
describe service('httpd') do it { should be_enabled } ←サービスが有効になっているか? it { should be_running } ←サービスが実行されているか? end
describe service('httpd') do  
  it { should be_enabled   }  ←サービスが有効になっているか?
  it { should be_running   }  ←サービスが実行されているか?
end

5.配置ファイルに関するテスト

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
describe file('/etc/httpd/conf/httpd.conf') do
it { should be_file } ←ファイルが存在するか?
its(:content) { should match /ServerName xxx/ } ←ファイル内に「/ServerName xxx/」にマッチするテキストが存在するか?
end
describe file('/etc/httpd/conf/httpd.conf') do it { should be_file } ←ファイルが存在するか? its(:content) { should match /ServerName xxx/ } ←ファイル内に「/ServerName xxx/」にマッチするテキストが存在するか? end
describe file('/etc/httpd/conf/httpd.conf') do 
  it { should be_file }  ←ファイルが存在するか?
  its(:content) { should match /ServerName xxx/ }  ←ファイル内に「/ServerName xxx/」にマッチするテキストが存在するか?
end

 

Ansible

Posted by arkgame