1
0

More than 1 year has passed since last update.

初心者がServerspecのサンプルについて調べてみた

Last updated at Posted at 2023-07-26

はじめに

AWSのCloudFormationで構築した環境をServerspecでテストをする予定です。
そのために、まずは公式サイトのサンプルについて調べてみたので、簡単にまとめました。

Serverspecとは

Serverspecは、サーバーが正しく構成されているかをテストできるツールです。
Rubyで実装されています。
詳細は公式サイトを参照ください。

Serverspecのサンプルについて調べる

サンプル

先ほどの公式サイトにサンプルが載っています。
抜粋した内容が下記になります。

require 'spec_helper'

describe package('httpd'), :if => os[:family] == 'redhat' do
  it { should be_installed }
end

調べたこと

require 'spec_helper'

サンプルで使用する外部ファイル'spec_helper'を読み込んでいます。
('spec_helper'の詳細については割愛)

describe package('httpd'), :if => os[:family] == 'redhat' do
  it { should be_installed }
end

テストファイルの基本構造になります。
テストしたい内容を記載します。
詳細は公式サイトの「TUTORIAL」に記載されています。

package('httpd')はテストするリソースの種類を記載します。
サンプルは、httpdパッケージに関するテストになります。

package以外にもportcommandなどがあります。
詳細は公式サイトの「RESOURCE TYPES」に記載されています。

it { should be_installed }は期待する値を記載します。
サンプルはパッケージがインストールされていることを確認しています。

be_installedのことをmatcherと言い、「期待する値と実際の値を比較し、一致した(しなかった)という結果を返すオブジェクト」になります。
先ほどの「RESOURCE TYPES」にmatcherの例が記載されています。

まとめ

Serverspecのサンプルについて調べた内容は以上になります。
何かみなさんの参考になれば嬉しいです。

1
0
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
1
0