LoginSignup
25
25

More than 5 years have passed since last update.

Chef のテスト環境を整える

Last updated at Posted at 2014-01-08

WIP!

概要

  • test-kitchen と serverspec で受け入れテスト
    • virtualbox
    • ec2 (TODO)
    • docker (TODO)
  • foodcritic で chef syntax check (TODO)
  • robocop で ruby syntax check (TODO)

手順

Chef 自体のインストール

console
$ gem install chef

VirtualBox のインストール

Vagrant のインストール

opscode 公式の BOX を追加

console
$ vagrant box add opscode-ubuntu-12.04 https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box

Test 関連 Gem のインストール

console
$ cd your-cookbook

test-kitchen, vagrant, busser, serverspec など gem をインストール

Gemfile
source 'https://rubygems.org'

gem 'berkshelf'
gem 'foodcritic'
gem 'rubocop'

group :integration do
  gem 'test-kitchen'
  gem 'kitchen-vagrant'
  gem 'busser'
  gem 'serverspec'
end
console
$ bundle install

vagrant plugin のインストール

console
$ vagrant plugin install vagrant-berkshelf

busser plugin のインストール

console
$ busser plugin install serverspec

test-kitchen スケルトン作成

test-kitchen のスケルトン作成

console
$ kitchen-init

serverspec のスケルトン作成

console
$ cd test/integration/default/
$ serverspec-init
Select OS type:

  1) UN*X
  2) Windows

Select number: 1

Select a backend type:

  1) SSH
  2) Exec (local)

Select number: 2

 + spec/
 + spec/localhost/
 + spec/localhost/httpd_spec.rb
 + spec/spec_helper.rb
 + Rakefile
console
$ rm Rakefile
$ mv spec serverspec

テストを書く

test/integration/default/serverspec/localhost/sample_spec.rb
require "spec_helper"

describe file("/tmp/hello") do
  it { should be_file }
  it { should contain "Hello World" }
end

レシピを書く

recipes/default.rb
file "/tmp/hello" do
  content "Hello World"
end

テストを走らせる

Chef の Recipe の Lint Check

console
$ cd your-cookbook
$ foodcritic .

test-kitchen の設定ファイル書く

.kitchen.yml
---
driver_plugin: vagrant
driver_config:
  require_chef_omnibus: true

platforms:
- name: ubuntu-12.04
  driver_config:
    box: opscode-ubuntu-12.04
    box_url: https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-12.04_provisionerless.box

suites:
- name: default
  run_list:
    - recipe[your-cookbook::default]
  attributes:

テストを走らせる。

console
$ kitchen test

platforms × suites の数だけ

  1. VM 作成
  2. VM へChef のインストール
  3. Cookbook ダウンロード
  4. Chef-Client 実行
  5. Serverspec 実行

が行われる

25
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
25
25