こんにちは!beardです!
Serverspecでテスト対象のOSごとにテスト項目を変える処理を実行してみます。
これとは別にHDDの容量をサーバースペックで確認する方法についても紹介します。
Serverspecのインストールと基本的な操作については、過去の記事『 Amazon OSS編~Serverspecパート①~ 』をご覧ください。
1. 今回テストする環境
OSごとにテスト項目を切り替えたいということで以下の二つのテスト対象を用意します。CentOSではhttpdを、Ubuntuではapache2のサービスとなっております。
CentOS6.5 IPアドレス:54.64.15.205
Ubuntu14.04 IPアドレス:54.64.126.211
ディレクトリ構成とRakefileとspec_helper.rbは『 Serverspecでサーバ情報をファイルに保存してテストする方法 』の記事と同じです。
1Serverspec2├Rakefile3├ubuntu-centos.yml ←上記二つのサーバ情報を書きます4├spec5 ├spec_helper.rb6 └web7 └httpd_spec.rb ←今回はHTTPD(apache)のみubuntu-centos.ymlの内容
154.64.15.205:2 :roles:3- web454.64.126.211:5 :roles:6 - webhttpd_spec.rbbの内容
1require 'spec_helper'2#CentOS3if os[:family] == 'RedHat' ←CentOSはRedHat系4describe package('httpd') do5 it { should be_installed }6end78describe service('httpd') do9 it { should be_enabled }10 it { should be_running }11end12end1314#Ubuntu15if os[:family] == 'Ubuntu' ←OSがUbuntuの場合16describe package('apache2') do ←Ubuntuの場合httpdではなくapache217 it { should be_installed }18end1920describe service('apache2') do21 it { should be_enabled }22 it { should be_running }23end24end2526#CentOS&Ubuntu27describe port(80) do ←両OS共通のテスト項目(ポート番号)28 it { should be_listening }29end- このようにファイルを変更した後、「rake spec」コマンドでテストを開始します。
1$rake spec2Enter Servers.yml(full path): /usr/local/serverspec/ubuntu-centos.yml3/usr/bin/ruby2.0 -S rspec spec/web/httpd_spec.rb4....56Finished in 0.15157 seconds74 examples, 0 failures ←1つのサーバに対し4つのテスト項目を実施している8/usr/bin/ruby2.0 -S rspec spec/web/httpd_spec.rb9....1011Finished in 0.05528 seconds124 examples, 0 failures ←1つのサーバに対し4つのテスト項目を実施している2. 異なるOSのサーバでもひとつのコードでテストされました!
解説
Serverspecでは os[:family] を使うことでテスト対象のOSを認識して、以下のようなif文による制御も可能となっております。
1if os[:family] == 'テスト対象のOS'2 テスト項目3endいかがでしたか?
異なるOSでもひとつのコードで簡単にテストすることが可能です。
次回もお楽しみに!!