Serverspecテストコード実例の紹介とコード記述の際のポイント

Linuxセキュリティ設定テスト
spec/server-01/security_spec.rb
require 'spec_helper'
describe selinux do
it { should be_enforcing }
end
describe command('getsebool httpd_can_network_connect') do
it { should return_stdout /on$/ }
end
### ポイント: 複数繰り返し処理する際のパラメータ定義 ###
accept_ports = [{:protocol => 'tcp', :port => 22 },
{:protocol => 'tcp', :port => 80},
{:protocol => 'tcp', :port => 10051}]
accept_ports.each do |p| # ポイント: 同じ処理をループ処理化
describe iptables do
it { should have_rule("-A INPUT -p #{p[:protocol]} -m state --state NEW -m #{p[:protocol]} --dport #{p[:port]} -j ACCEPT") }
end
end
【ポイント】
同じSELinux関連の設定の確認であっても、Matcherとして対応していない部分(SELinuxのルール設定)の確認をしたいケースもあります。そういった場合には別途commandというResourceTypeを活用して、任意のコマンドの処理結果をテストする形の記述方法を採ることができます。また、よく利用するResourceTypeやMatcherは独自にカスタム定義して利用することも可能です。Serverspecのカスタマイズについては下記の記事を参照して下さい。
Tech-Sketch「serverspecを環境にあわせてカスタマイズ」
また、ServerspecのテストコードはRubyのコードとして記述します。そのため、iptablesの設定確認など異なるポート番号毎に同じような確認処理を繰り返し行う場合は、Rubyのループ文で回して記述できます。同じ内容の記述をできる限り減らし、メンテナンスしやすい形でテストコードを管理することをお勧めします。このようにRubyの基本的な書き方を知ることでServerspecのコードもより管理しやすくなります。
DNS設定テスト
spec/server-01/dns_spec.rb
require 'spec_helper'
dns_server = 'dns-server01'
### ポイント: 名前解決に関する設定の確認 ###
describe file('/etc/resolv.conf') do
its(:content) { should match /^nameserver #{dns_server}/ }
end
describe file('/etc/nsswitch.conf') do
its(:content) { should match /^hosts:\s*files\s+dns/ }
end
### ポイント: 本当に名前解決できるかの確認 ###
describe host('server-01') do
it { should be_resolvable }
end
【ポイント】
ServerspecにはfileというResourceTypeがあり、ファイルの中にどういった設定が行われているかのテストを書くことができます。設定としてどういった記述が行われているかをテストすることも必要ですが、その設定が正しく反映されてその通りに稼働しているかをテストすることも重要です。そのため、ここではDNSサーバの設定ファイルの中身の確認だけでなく、実際に名前解決ができるかどうかを確認しています。
Webサーバ(Apache HTTP server)部分のテストコード例
ZabbixのWebGUIを動かすために、Webサーバ部分の正常な稼働状態としては例えば以下のような状態をテストします。
正常な稼働状態
- Apacheのパッケージが導入されている
- Apacheのサービスが正常に稼働している
- ApacheのサービスがTCPの80番ポートでListenしている
- ApacheのプロセスがPHPのモジュールを読み込んでいる
- Apacheの設定ファイルにてZabbixのWebGUI用のPHPファイルを参照できる設定がされている
テストコード
spec/server-01/httpd_spec.rb
require 'spec_helper'
### ポイント: OSの種類毎に異なる処理の分岐 ###
if os[:family] == 'RedHat'
describe package('httpd') do
it { should be_installed }
end
describe service('httpd') do
it { should be_enabled }
it { should be_running }
end
describe file('/etc/httpd/conf/httpd.conf') do
it { should be_file }
its(:content) { should match /Include conf.d\/\*\.conf/ }
end
describe file('/etc/httpd/conf.d/zabbix.conf') do
it { should be_file }
its(:content) { should match /Alias \/zabbix \/usr\/share\/zabbix/ }
end
elsif ['Debian', 'Ubuntu'].include?(os[:family])
describe package('apache2') do
it { should be_installed }
end
describe service('apache2') do
it { should be_enabled }
it { should be_running }
end
describe file('/etc/apache2/apache2.conf') do
it { should be_file }
its(:content) { should match /IncludeOptional conf-enabled \/\*\.conf/ }
end
describe file('/etc/apache2/conf-enabled/zabbix.conf') do
it { should be_linked_to '/etc/apache2/conf-available/zabbix.conf' }
its(:content) { should match /Alias \/zabbix \/usr\/share\/zabbix/ }
end
end
describe port(80) do
it { should be_listening }
end
describe command('apachectl -M |grep php5_module') do
it { should return_stdout /php5_module/ }
end
【ポイント】
上記テストコードの例ではApacheのパッケージの導入確認部分において、OSの種別毎に分岐処理を行っています。第1回の記事でも紹介した通り、Serverspecではある程度OSの違いを吸収してテスト処理が実行できるようになっていますが、OS毎にパッケージ名が異なっていたり、特定のOSでのみ実施したい処理がある場合などは、テスト実行対象サーバのOS情報をもとに条件分岐させて処理を記述することが必要となります。


