LoginSignup
31
25

More than 5 years have passed since last update.

Serverspecの判定にサーバ内のコマンドの実行結果を使いたい

Posted at

serverspecでirqbalanceというデーモンが起動しているかどうかのチェックをしたのですが、このデーモンはコア数が1の場合は起動しないとのこと。
これを管理しているchefはいろいろなサーバで使われる可能性があったので、とりあえずコア数1のときはテストしないという方針にしました。

サーバのコア数を取得したかったので、cpuinfoをコマンドで取得し、その実行結果をもとに判定するかどうかを決めています。
テストの中でサーバに対しコマンドを実行するにはbackend.run_commandを使います。

describe service('irqbalance') do
  cmd_result = backend.run_command("cat /proc/cpuinfo|grep -c processor")
  cpu_total = cmd_result[:stdout].to_i
  it { should be_enabled if cpu_total > 1 }
  it { should be_running if cpu_total > 1 }
end

コマンドの実行結果は下記のようなハッシュで返ってきました。

{:stdout=>"1\n", :stderr=>"", :exit_status=>0, :exit_signal=>nil}

サンプル中では出力結果を整数型にキャストして比較に使用しています。
状況に応じた柔軟なテストを書く参考になればと思います。

31
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
25