4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Chef+Kithcen環境でServerspecのテスト実行までの手順

Last updated at Posted at 2014-11-09

いろいろな記事や書籍で記述が揺らいでいたので、一番minimalな手順を探ったのでメモ。

前提

ChefとKitchenの設定は済んでいてkitchen convergeまでが問題なく成功する状態とします。目的のレシピは書かれていて、Berksfileや.kitchen.ymlの設定は済んでいる状態。serverspec-init未使用。

serverspecをインストール。自分の環境では以下。sudobundle execはお好みで。(私はつけない派)

$ gem install serverspec

バージョンや実行環境は以下のとおり
OS X 10.10 Yosemite
Ruby 2.1.3p242
Chef 11.16.4
Vagrant 1.6.3
serverspec 2.3.1

ディレクトリ構成

まずは自分の実環境から。/はchefrepoのルートです。

/test/integration/default/serverspec/base_spec.rb
/test/integration/default/serverspec/apache_spec.rb
/test/integration/[テストsuite名]/[テストライブラリの名前]/[テストファイル]

実環境だと以下のようになりました。

/test/integration/default/serverspec/base_spec.rb

テストsuite名

「テスト一式の名前」として理解している。.kitchen.ymlに書かれているsuiteという部分。kitchen initのデフォルトだとdefaultとなるかと思います。

.kitchen.yml
---
driver:
  name: vagrant

provisioner:
  name: chef_solo

platforms:
  - name: centos-6.5

suites:
  - name: default # <- ここです
    run_list:
        - recipe[base]
        - recipe[apache]
    attributes:

テストライブラリ名

テストライブラリの名前。batsminitestが使えるらしい。今回はserverspecと記述。batsもシェルスクリプトで直感的にかけるのでおすすめ。

テストファイル

実際にテストを記述するファイル。_spec.rbで終わる必要がある。自分はrecipeの名前にするとわかりやすかったので上のような命名。

テストコード

packageの確認だけやります。

base_spec.rb
require 'serverspec'

%w{vim-common tree}.each do |pkg|
    describe package(pkg) do
        it { should be_installed }
    end
end
apache_spec.rb
require 'serverspec'

describe package('httpd24-httpd') do
    it { should be_installed }
end

Chefのレシピの書き方と非常に似ています。

ちょっと注意

レシピの方で

package 'vim' do
  action :install
end

と書いても、テストコードで

describe package('vim-common') do
    it { should be_installed }
end

と実際のパッケージの名前を書かなきゃいけない。 ここが非対称でややこしい。インストールは簡単だけどテストはきちんと全て明示する、みたいな。

実行

$ cd [chefrepoのルート]
$ kitchen test

事前にkitchen convergeで仮想マシンが煮立っているならkitchen verifyでよい。

実行結果は以下。

$ kitchen verify
-----> Starting Kitchen (v1.2.1)
-----> Verifying <default-centos-65>...
       Removing /tmp/busser/suites/serverspec
       Uploading /tmp/busser/suites/serverspec/apache_spec.rb (mode=0644)
       Uploading /tmp/busser/suites/serverspec/base_spec.rb (mode=0644)
-----> Running serverspec test suite
       /opt/chef/embedded/bin/ruby -I/tmp/busser/suites/serverspec -I/tmp/busser/gems/gems/rspec-support-3.1.2/lib:/tmp/busser/gems/gems/rspec-core-3.1.7/lib /opt/chef/embedded/bin/rspec --pattern /tmp/busser/suites/serverspec/\*\*/\*_spec.rb --color --format documentation --default-path /tmp/busser/suites/serverspec

       Package "httpd24-httpd"
       No backend type is specified. Fall back to :exec type.
       No backend type is specified. Fall back to :exec type.
       No backend type is specified. Fall back to :exec type.
       No backend type is specified. Fall back to :exec type.
       No backend type is specified. Fall back to :exec type.
       No backend type is specified. Fall back to :exec type.
       No backend type is specified. Fall back to :exec type.
       No backend type is specified. Fall back to :exec type.
       No backend type is specified. Fall back to :exec type.
       No backend type is specified. Fall back to :exec type.
         should be installed

       Package "vim-common"
       No backend type is specified. Fall back to :exec type.
       No backend type is specified. Fall back to :exec type.
       No backend type is specified. Fall back to :exec type.

         should be installed

       Package "tree"
       No backend type is specified. Fall back to :exec type.
       No backend type is specified. Fall back to :exec type.
       No backend type is specified. Fall back to :exec type.
         should be installed

       Finished in 0.20124 seconds (files took 0.27194 seconds to load)
       3 examples, 0 failures
       Finished verifying <default-centos-65> (0m2.30s).
-----> Kitchen is finished. (0m3.81s)

疑問

一応ここまでの手順で通るテストが書けるようになったものの、No backend type is specified. Fall back to :exec type.が大量に出てくる。解決法がまだわからない。

backend typeというのはおそらくserverspec-initをしたときに問われる SSH or Execなのかなと推測はできるが、どう指定したらいいのか。serverspec-initと同様の構成にしたほうがいいのか。もう少し実験が必要か。情報求む。

参考資料

「Chef実践入門」
http://kitchen.ci/docs/getting-started/writing-test
http://kitchen.ci/docs/getting-started/adding-suite

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?