Ohaiの値を元にServerspecでテスト

概要

Chefで物理サーバーの場合のみ適用するレシピを作る - tsunokawaのはてなダイアリー
ChefとOhaiを使って物理サーバーにのみ必要なパッケージをインストールするという手順を書きました。

今回はServerspecでOhaiの値を元に物理サーバーか仮想サーバーか判定し、
物理サーバーのみ必要なパッケージがインストールされているかテストする手順です。

テスト

以下はKVM以外のみ物理サーバーとしてmcelogパッケージがインストールされているかテストしています。

require 'spec_helper'
require 'rubygems'
require 'ohai'

ohai = Ohai::System.new
ohai.all_plugins


if ohai[:dmi][:system][:product_name] != 'KVM' then
  describe package('mcelog') do
    it { should be_installed }
  end
end


ちなみに

この例以外でも以前OpenSSLの脆弱性が発表された際にも
Ohaiを元にServerspecを使ってアップデートされているかどうかのテストを行っていました。

CentOS6系かどうかをOhaiの値を元に判定して脆弱性対策済みバージョンがインストールされているかテストしています。

require 'spec_helper'
require 'rubygems'
require 'ohai'

ohai = Ohai::System.new
ohai.all_plugins


if ohai[:platform_version] >= '6.0' && '6.5' >= ohai[:platform_version] then
  describe package('openssl') do
    it { should be_installed.with_version("1.0.1e-16.el6_5.4") }
  end
end