LoginSignup
35
35

More than 5 years have passed since last update.

Chef Solo / Vagrant / serverspecでインフラのTDDを行う

Last updated at Posted at 2014-08-28

Vagrantでつくった環境を、Chefでプロビジョニングし、それをserverspecでテストします。

前提環境

以下のものがインストールされている必要があります。

  • Vagnrat
  • chef-solo
  • knife
  • serverspec

作業ディレクトリの用意

> mkdir infra-tdd
> cd infra-tdd

Vagrantの立ち上げ

Vagrantを立ち上げます
vagrant box add **** であらかじめboxを登録する必要あり。

vagrant init opscode-centos-6.4
vagrant up

これでVagrantサーバーが立ち上がるので、

> vagrant ssh

でVagrantサーバーにsshログインできればOK。

Vagrantサーバーの接続情報を次のコマンドで.ssh/configに追記しておきます。

> vagrant ssh-config --host infra-tdd >> ~/.ssh/config

Chefレポジトリの準備

knife-soloを使ってChefレポジトリを初期化します。

> knife solo init .

次に、先ほどのVagrantサーバーをChefのnodeとして追加してあげます。

> knife solo bootstrap infra-tdd

これでnodeとして追加されるほか、VagrantサーバーにChefがインストールされます。

テストの用意

今回はTDDなので、serverspecを使ってテストを先に書いて行きます。

> serverspec-init

を実行すると、いろいろ聞かれるので答えていきます。

Select OS type:

  1) UN*X
  2) Windows

Select number: 1

Select a backend type:

  1) SSH
  2) Exec (local)

Select number: 1

Vagrant instance y/n: y
Auto-configure Vagrant from Vagrantfile? y/n: y
 + spec/
 + spec/default/
 + spec/default/httpd_spec.rb
 + spec/spec_helper.rb
 + Rakefile

spec/default/httpd_spec.rbというファイルが既に作られているので、以下のように編集(余計な部分を削っただけです)

spec/default/httpd_spec.rb
require 'spec_helper'

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

「httpdがインストールされているべき」といったTDDならぬBDD(振る舞い駆動開発)っぽい感じでテストが書けます。

テストの実施

いよいよテストを実施してみます。

> rake spec

これだけでテストが実行されます。実行結果は以下のようになります。


/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby -S rspec spec/default/httpd_spec.rb
F

Failures:

  1) Package "httpd" should be installed
     On host `default`
     Failure/Error: it { should be_installed }
       sudo rpm -q httpd
       パッケージ httpd はインストールされていません。
       expected Package "httpd" to be installed
     # ./spec/default/httpd_spec.rb:4:in `block (2 levels) in <top (required)>'

Finished in 3.6 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/default/httpd_spec.rb:4 # Package "httpd" should be installed
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby -S rspec spec/default/httpd_spec.rb failed

httpdがインストールされていないので、テストが通りません。

レシピの用意

次に、テストを通すためのapacheをインストールするレシピを準備していきます。

> knife cookbook create apache -o site-cookbooks

site-cookbooks/apache/recipes/default.rbが出来てますので、これを編集します

site-cookbooks/apache/recipes/default.rb
package "httpd" do
 action :install
end

service "httpd" do
 action [ :enable, :start ]
end

このレシピをnodeの設定ファイル(nodes/infra-tdd.json)に追加

nodes/infra-tdd.json
{
  "run_list": [
    "recipe[apache]"
  ]
}

プロビジョニングの実行とテスト再実施

以下のコマンドでinfra-add(Vagrantサーバー)に対してChefによるプロビジョニングが行われます。

> knife solo cook infra-tdd

これで、httpdがインストールされたはずなので、serverspecによるテストを再実施してみます。

> rake spec

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby -S rspec spec/default/httpd_spec.rb
.

Finished in 4.01 seconds
1 example, 0 failures

今回はちゃんとテストが成功しました。
以上のような流れでVagrant、Chef、serverspecをつかったTDDが行えます。

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