Skip to content

Instantly share code, notes, and snippets.

@minoritea
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minoritea/ea4f9f3513bb06d7fa90 to your computer and use it in GitHub Desktop.
Save minoritea/ea4f9f3513bb06d7fa90 to your computer and use it in GitHub Desktop.
Serverspec Workshop

Serverspec Workshop

Minori TOKUDA 2015-Jan-27

1. Serverspec のインストール

  • gem install rake serverspec --no-ri --no-rdoc # rake と serverspec gemをインストール
  • which serverspec-init # ひな形生成コマンドが入っていることを確認

( sudoでインストールするのが嫌な方、ruby 1.8.7 は論外という方はrbenvを使おう )


2. ひな形の生成

  • mkdir local-directories-spec
  • cd local-directories-spec
  • serverspec-init
    • OS typeはUN*Xを選択
    • backendはExec(local)を選択
  • cd local-directories-spec
  • rake -T # rake spec:localhost というタスクが出来ていることを確認
  • sed -i "1i require \"rubygems\"" spec/spec_helper.rb # 1.8.7 はgemを自動で呼んでくれないので・・・
  • cat spec/localhost/sample_spec.rb # 実際のテストコードの確認
  • rake spec:localhost # テストの実行
  • 4件エラーが出る。テンプレートだとApacheのインストールをテストしているが、今回はファイル構成テストを行う。

3. ディレクトリが作られているかどうかの確認のテスト

  • mkdir -p ~/test/testdir # テスト対象のディレクトリを作成
  • rm spec/localhost/sample_spec.rb # サンプルは消す
  • vim spec/localhost/directories_spec.rb # テストコードの作成
require 'spec_helper'

describe file("#{ENV['HOME']}/test/testdir") do
  it { should be_directory }
  it { should be_writable }
end
  • rake spec:localhost # テストの実行

4. ファイルが作成されているかどうかの確認テスト

  • cat > ~/test/testdir/testfile # 以下の内容を書き込む
hello
  • vim spec/localhost/directories_spec.rb # テストケースの追加
require 'spec_helper'

describe file("#{ENV['HOME']}/test/testdir") do
  it { should be_directory }
  it { should be_writable }
end

describe file("#{ENV['HOME']}/test/testdir/testfile") do
  it { should be_file }
  it { should be_readable }
  its(:content) { should match /hello/ }
end
  • rake spec:localhost # テストの実行

5. いろいろマッチャを試してみよう

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment