orenoblog

エンジニアになりたいExcel方眼紙erの物語

meezを使ってテストの準備を楽する

こんばんは、ターミナル業してない私です。cookbookのテスト書くのめんどいですよね。 ってのとTravisCIでcookbookのテストまわしたいなーと思ってまして

グーグル先生に伺ったところ meezというcookbookを作成すると同時に、様々なテストを自動生成してくれる素敵なgemを見つけました.

Chefクックブックのテスト用定型コードを生成する新gem

らくがしたくなるのも いきものの サガ か・・・

  • Gemfile

bundlerを使います。毎日ありがとうbundler.

gem 'chef', '~> 11.8.0'
gem 'meez'

chef 11.10.4未対応の模様です。バージョンは固定しましょう。

You have requested:
  chef ~> 11.8.0

The bundle currently has chef locked at 11.10.4.
Try running `bundle update chef`
  • cookbookの作成

cookbookを生成すると、テストのひな形も同時に作成されます

※指定したcookbookが存在すると、テストだけ生成します!!

orenomac$ bundle exec meez postfix -o site-cookbooks/
* Initializing Cookbook
** Creating cookbook postfix
** Creating README for cookbook: postfix
** Creating CHANGELOG for cookbook: postfix
** Creating metadata for cookbook: postfix
        Rewriting metadata.rb
        Rewriting recipes/default.rb
* Initializing Berkshelf
      create  site-cookbooks/postfix/Berksfile
      create  site-cookbooks/postfix/Thorfile
      create  site-cookbooks/postfix/.gitignore
         run  git init from "./site-cookbooks/postfix"
      create  site-cookbooks/postfix/Gemfile
* Initializing Vagranfile
        Creating site-cookbooks/postfix/Vagrantfile from template
* Initializing Knife
adding chef gem to Gemfile
* Initializing Rakefile
        Creating site-cookbooks/postfix/Rakefile from template
adding rake gem to Gemfile
* Initializing Rubocop
adding rubocop gem to Gemfile
* Initializing Food Critic
adding foodcritic gem to Gemfile
* Initializing Chef Spec
        Creating site-cookbooks/postfix/test/unit/spec/spec_helper.rb from template
        Creating site-cookbooks/postfix/test/unit/spec/default_spec.rb from template
adding chefspec gem to Gemfile
* Initializing Server Spec
        Creating site-cookbooks/postfix/test/integration/default/serverspec/spec_helper.rb from template
        Creating site-cookbooks/postfix/test/integration/default/serverspec/default_spec.rb from template
adding serverspec gem to Gemfile
* Initializing Test Kitchen
      create  .kitchen.yml
      append  Rakefile
      append  Thorfile
       exist  test/integration/default
      append  .gitignore
      append  .gitignore
      append  Gemfile
      append  Gemfile
You must run `bundle install' to fetch any new gems.
        Creating site-cookbooks/postfix/.kitchen.yml from template
* Initializing Guard
        Creating site-cookbooks/postfix/Guardfile from template
adding guard gem to Gemfile
adding guard-rubocop gem to Gemfile
adding guard-foodcritic gem to Gemfile
* Initializing Drone
        Creating site-cookbooks/postfix/.drone.yml from template
Cookbook postfix created successfully
Next steps...
  $ cd site-cookbooks/postfix
  $ bundle install
  $ bundle exec berks install
  $ bundle exec rake

生成されたディレクトリやファイルを見てみます。chefspecとserverspecがありますね。

branch:[master]pwd:[~/Documents/dev/myserver/site-cookbooks/postfix]
orenomac$ tree test
test
├── integration
│   └── default
│       └── serverspec
│           ├── default_spec.rb
│           └── spec_helper.rb
└── unit
    └── spec
        ├── default_spec.rb
        └── spec_helper.rb

chefspecのspec_helper.rbを見てみます。おっとubuntu....ですが、ここまで作ってくれるのはとてもありがたいです。

# Encoding: utf-8
require 'chefspec'
require 'chefspec/berkshelf'
require 'chef/application'

::LOG_LEVEL = :fatal
::UBUNTU_OPTS = {
  platform: 'ubuntu',
  version: '12.04',
  log_level: ::LOG_LEVEL
}
::CHEFSPEC_OPTS = {
  log_level: ::LOG_LEVEL
}

def stub_resources
end

at_exit { ChefSpec::Coverage.report! } 

UnitテストとIntegrationテストが楽になるのではないでしょうか。

今度はこの記事を読みながら自分のcookbookたちをTravisCIでテストして見たいと思います。

つづく..?