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

2014年8月22日(金)
池田 大輔

Linuxセキュリティ設定テスト

spec/server-01/security_spec.rb

01require 'spec_helper'
02 
03describe selinux do
04  it { should be_enforcing }
05end
06 
07describe command('getsebool httpd_can_network_connect') do
08  it { should return_stdout /on$/ }
09end
10 
11### ポイント: 複数繰り返し処理する際のパラメータ定義 ###
12accept_ports = [{:protocol => 'tcp', :port => 22 },
13                {:protocol => 'tcp', :port => 80},
14                {:protocol => 'tcp', :port => 10051}]
15 
16accept_ports.each do |p|  # ポイント: 同じ処理をループ処理化
17  describe iptables do
18    it { should have_rule("-A INPUT -p #{p[:protocol]} -m state --state NEW -m #{p[:protocol]} --dport #{p[:port]} -j ACCEPT") }
19  end
20end

【ポイント】
同じSELinux関連の設定の確認であっても、Matcherとして対応していない部分(SELinuxのルール設定)の確認をしたいケースもあります。そういった場合には別途commandというResourceTypeを活用して、任意のコマンドの処理結果をテストする形の記述方法を採ることができます。また、よく利用するResourceTypeやMatcherは独自にカスタム定義して利用することも可能です。Serverspecのカスタマイズについては下記の記事を参照して下さい。

Tech-Sketch「serverspecを環境にあわせてカスタマイズ

また、ServerspecのテストコードはRubyのコードとして記述します。そのため、iptablesの設定確認など異なるポート番号毎に同じような確認処理を繰り返し行う場合は、Rubyのループ文で回して記述できます。同じ内容の記述をできる限り減らし、メンテナンスしやすい形でテストコードを管理することをお勧めします。このようにRubyの基本的な書き方を知ることでServerspecのコードもより管理しやすくなります。

DNS設定テスト

spec/server-01/dns_spec.rb

01require 'spec_helper'
02 
03dns_server = 'dns-server01'
04 
05### ポイント: 名前解決に関する設定の確認 ###
06describe file('/etc/resolv.conf') do
07  its(:content) { should match /^nameserver #{dns_server}/ }
08end
09 
10describe file('/etc/nsswitch.conf') do
11  its(:content) { should match /^hosts:\s*files\s+dns/ }
12end
13 
14### ポイント: 本当に名前解決できるかの確認 ###
15describe host('server-01') do
16  it { should be_resolvable }
17end

【ポイント】
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

01require 'spec_helper'
02 
03### ポイント: OSの種類毎に異なる処理の分岐 ###
04 
05if os[:family] == 'RedHat'
06  describe package('httpd') do
07    it { should be_installed }
08  end
09  describe service('httpd') do
10    it { should be_enabled   }
11    it { should be_running   }
12  end
13  describe file('/etc/httpd/conf/httpd.conf') do
14    it { should be_file }
15    its(:content) { should match /Include conf.d\/\*\.conf/ }
16  end
17  describe file('/etc/httpd/conf.d/zabbix.conf') do
18    it { should be_file }
19    its(:content) { should match /Alias \/zabbix \/usr\/share\/zabbix/ }
20  end
21elsif ['Debian', 'Ubuntu'].include?(os[:family])
22  describe package('apache2') do
23    it { should be_installed }
24  end
25  describe service('apache2') do
26    it { should be_enabled   }
27    it { should be_running   }
28  end
29  describe file('/etc/apache2/apache2.conf') do
30    it { should be_file }
31    its(:content) { should match /IncludeOptional conf-enabled \/\*\.conf/ }
32  end
33  describe file('/etc/apache2/conf-enabled/zabbix.conf') do
34    it { should be_linked_to '/etc/apache2/conf-available/zabbix.conf' }
35    its(:content) { should match /Alias \/zabbix \/usr\/share\/zabbix/ }
36  end
37end
38 
39 
40 
41 
42 
43describe port(80) do
44  it { should be_listening }
45end
46 
47describe command('apachectl -M |grep php5_module') do
48  it { should return_stdout /php5_module/ }
49end

【ポイント】
上記テストコードの例ではApacheのパッケージの導入確認部分において、OSの種別毎に分岐処理を行っています。第1回の記事でも紹介した通り、Serverspecではある程度OSの違いを吸収してテスト処理が実行できるようになっていますが、OS毎にパッケージ名が異なっていたり、特定のOSでのみ実施したい処理がある場合などは、テスト実行対象サーバのOS情報をもとに条件分岐させて処理を記述することが必要となります。

TIS株式会社

Twitter : @ike_dai
TIS株式会社OSS推進室所属。社内向けシステムの保守運用業務を経験後、クラウド時代の効率的な統合運用管理をテーマに活動中。特に、OSSを駆使した運用のエコシステム実現を目指し、Zabbix,fluentd,Serverspec,Ansibleなどの導入や検証に取り組む。技術検証成果などを技術ブログ『Tech-Sketch』にて発信中。著書:『Zabbix統合監視徹底活用 - 複雑化・大規模化するインフラの一元管理』

連載バックナンバー

Think ITメルマガ会員登録受付中

Think ITでは、技術情報が詰まったメールマガジン「Think IT Weekly」の配信サービスを提供しています。メルマガ会員登録を済ませれば、メルマガだけでなく、さまざまな限定特典を入手できるようになります。

Think ITメルマガ会員のサービス内容を見る

他にもこの記事が読まれています