Chef-test-kitchen-setup

提供:Dev Guides
移動先:案内検索

シェフ-キッチンのセットアップをテストする

Test Kitchenは、シェフの統合テストフレームワークです。 クックブックを使用してインスタンス化および収束されると、VM上で実行されるテストレシピの作成が可能になります。 テストレシピはそのVMで実行され、すべてが期待どおりに機能するかどうかを確認できます。

*ChefSpec* は、Chefの実行のみをシミュレートするものです。 テストキッチンは実際のノードを起動し、Chefを実行します。
  • ステップ1 *-テストキッチンRuby gemおよびテストキッチンvagrant gemをインストールして、テストキッチンがテストのスピンアップにvagrantを使用できるようにします。
$ gem install kitchen
$ gem install kitchen-vagrant

ステップ2 *-テストキッチンをセットアップします。 これを行うには、クックブックディレクトリに *.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[minitest-handler]
   - recipe[my_cookbook_test]
attributes: { my_cookbook: { greeting: 'Ohai, Minitest!'} }

上記のコードの一部では、迷走者がVMをスピンアップする必要があることを定義し、OmnibusでターゲットノードにChefをインストールすることを定義しています。

2番目の部分では、クックブックをテストするプラットフォームを定義します。 Vagrantは常に新しいインスタンスを作成および破棄します。 Vagrantファイルを使用してスピンアップするVagrant VMの副作用を心配する必要はありません。

テストキッチンは、運用環境に似た一時的な環境でクックブックを実行およびテストするのに役立つ一時的な環境と考えることができます。 テストキッチンを有効にすると、テスト、運用前、および運用環境に実際に展開する前に、特定のコードが動作していることを確認できます。 テストキッチンのこの機能は、クックブックを実際の作業環境に配置する前に、多くの組織がセットで従います。

テストキッチンワークフロー

以下は、テストキッチンワークフローに含まれる手順です。

キッチンワークフロー

Chefを使用してクックブックを作成する

次のコードを使用してクックブックを作成します。

$ chef generate cookbook motd_rhel
Installing Cookbook Gems:

Compiling Cookbooks...
Recipe: code_generator::cookbook
 *directory[C:/chef/cookbooks/motd_rhel] action create
      - create new directory C:/chef/cookbooks/motd_rhel

  * template[C:/chef/cookbooks/motd_rhel/metadata.rb] action create_if_missing
      - create new file C:/chef/cookbooks/motd_rhel/metadata.rb
      - update content in file C:/chef/cookbooks/motd_rhel/metadata.rb from none to
      d6fcc2 (diff output suppressed by config)

 *template[C:/chef/cookbooks/motd_rhel/README.md] action create_if_missing
      - create new file C:/chef/cookbooks/motd_rhel/README.md
      - update content in file C:/chef/cookbooks/motd_rhel/README.md from none to 50deab
         (diff output suppressed by config)

  * cookbook_file[C:/chef/cookbooks/motd_rhel/chefignore] action create
      - create new file C:/chef/cookbooks/motd_rhel/chefignore
      - update content in file C:/chef/cookbooks/motd_rhel/chefignore from none to 15fac5
         (diff output suppressed by config)

 *cookbook_file[C:/chef/cookbooks/motd_rhel/Berksfile] action create_if_missing
      - create new file C:/chef/cookbooks/motd_rhel/Berksfile
      - update content in file C:/chef/cookbooks/motd_rhel/Berksfile from none to 9f08dc
         (diff output suppressed by config)

  * template[C:/chef/cookbooks/motd_rhel/.kitchen.yml] action create_if_missing
      - create new file C:/chef/cookbooks/motd_rhel/.kitchen.yml
      - update content in file C:/chef/cookbooks/motd_rhel/.kitchen.yml
         from none to 49b92b (diff output suppressed by config)

 *directory[C:/chef/cookbooks/motd_rhel/test/integration/default/serverspec]
      action create
      - create new directory
         C:/chef/cookbooks/motd_rhel/test/integration/default/serverspec

  * directory[C:/chef/cookbooks/motd_rhel/test/integration/helpers/serverspec]
      action create
      - create new directory
         C:/chef/cookbooks/motd_rhel/test/integration/helpers/serverspec

 *cookbook_file
      [C:/chef/cookbooks/motd_rhel/test/integration/helpers/serverspec/spec_helper.rb]
      action create_if_missing
      - create new file
         C:/chef/cookbooks/motd_rhel/test/integration/helpers/serverspec/spec_helper.rb
      - update content in file
         C:/chef/cookbooks/motd_rhel/test/integration/helpers/serverspec/spec_helper.rb
            from none to d85df4 (diff output suppressed by config)

  * template
      [C:/chef/cookbooks/motd_rhel/test/integration/default/serverspec/defaul t_spec.rb]
      action create_if_missing
      - create new file
         C:/chef/cookbooks/motd_rhel/test/integration/default/serverspec/default_spec.rb
      - update content in file
         C:/chef/cookbooks/motd_rhel/test/integration/default/serverspec/default_spec.rb
            from none to 3fbdbd (diff output suppressed by config)

 *directory[C:/chef/cookbooks/motd_rhel/spec/unit/recipes] action create
      - create new directory C:/chef/cookbooks/motd_rhel/spec/unit/recipes

  * cookbook_file
      [C:/chef/cookbooks/motd_rhel/spec/spec_helper.rb] action create_if_missing
      - create new file C:/chef/cookbooks/motd_rhel/spec/spec_helper.rb
      - update content in file
         C:/chef/cookbooks/motd_rhel/spec/spec_helper.rb from none to 587075
            (diff output suppressed by config)

 *template
      [C:/chef/cookbooks/motd_rhel/spec/unit/recipes/default_spec.rb]
      action create_if_missing
      - create new file C:/chef/cookbooks/motd_rhel/spec/unit/recipes/default_spec.rb
      - update content in file
         C:/chef/cookbooks/motd_rhel/spec/unit/recipes/default_spec.rb
            from none to ff3b17 (diff output suppressed by config)

  * directory[C:/chef/cookbooks/motd_rhel/recipes] action create
      - create new directory C:/chef/cookbooks/motd_rhel/recipes

 *template[C:/chef/cookbooks/motd_rhel/recipes/default.rb] action create_if_missing
      - create new file C:/chef/cookbooks/motd_rhel/recipes/default.rb
      - update content in file
         C:/chef/cookbooks/motd_rhel/recipes/default.rb from none to c4b029
            (diff output suppressed by config)

  * execute[initialize-git] action run
      - execute git init .

 *cookbook_file[C:/chef/cookbooks/motd_rhel/.gitignore] action create
      - create new file C:/chef/cookbooks/motd_rhel/.gitignore
      - update content in file C:/chef/cookbooks/motd_rhel/.gitignore from none to 33d469
         (diff output suppressed by config)

  * execute[git-add-new-files] action run
      - execute git add .

   * execute[git-commit-new-files] action run
      - execute git commit -m "Add generated cookbook content"

以下は、上記のコードの出力として作成されたクックブック構造です。

構造

キッチン構成ファイルのテスト

.kitchen.yamlファイル

driver:
   name: vagrant
provisioner:
   name: chef_zero
# verifier:
# name: inspec
# format: doc
platforms:
   - name: ubuntu-14.04
suites:
   - name: default
   run_list:
      - recipe[motd_rhel::default]
   attributes:

ドライバー-マシンを管理するソフトウェアを指定します。

*Provisioner* -Chefの実行方法に関する仕様を提供します。 chef_zeroを使用しているのは、ローカルマシン上のChefサーバー環境を模倣できるためです。 これにより、ノード属性とChefサーバー仕様を操作できます。

プラットフォーム-これは、ターゲットオペレーティングシステムを指定します。

スイート-仮想環境に適用するものを定義します。 ここでは、複数の定義を定義します。 実行リストを定義する場所であり、実行するレシピと実行する必要があるシーケンスを指定します。

コマンドを順番に実行する

キッチンリスト

$ kitchen list
Instance  Driver  Provisioner Verifier   Transport Last Action
ubuntu-1404 Vagrant ChefZero   Busser       Ssh   <Not Created>

キッチンクリエイト

$ kitchen create
-----> Starting Kitchen (v1.4.2)
-----> Creating <default-centos-72>...
      Bringing machine 'default' up with 'virtualbox' provider...
      ==> default: Box 'opscode-centos-7.2' could not be found.
         Attempting to find and install...
      default: Box Provider: virtualbox
      default: Box Version: >= 0
      ==> default: Box file was not detected as metadata. Adding it directly...
         ==> default: Adding box 'opscode-centos-7.2' (v0) for provider: virtualbox
         default: Downloading:
            https://opscode-vmbento.s3.amazonaws.com/vagrant/virtualbox/
            opscode_centos-7.1_chefprovisionerless.box[...]
         Vagrant instance <default-centos-72> created.
         Finished creating <default-centos-72> (3m12.01s).
         -----> Kitchen is finished. (3m12.60s)

キッチンコンバージ

$ kitchen converge
-----> Converging <default-centos-72>...
      Preparing files for transfer
      Preparing dna.json
      Resolving cookbook dependencies with Berkshelf 4.0.1...
      Removing non-cookbook files before transfer
      Preparing validation.pem
      Preparing client.rb
-----> Chef Omnibus   installation detected (install only if missing)
      Transferring files to <default-centos-72>
      Starting Chef Client, version 12.6.0
      resolving cookbooks for run list: ["motd_rhel::default"]
      Synchronizing Cookbooks: - motd_rhel (0.1.0)
      Compiling Cookbooks...       Converging 1 resources
      Recipe: motd_rhel::default        (up to date)
      Running handlers:       Running handlers complete
      Chef Client finished, 0/1 resources updated in 01 seconds
      Finished converging <default-centos-72> (0m3.57s).
      -----> Kitchen is finished. (0m4.55s)

テストのセットアップ

キッチンログインは、テストVMが正しくプロビジョニングされているかどうかをテストするために使用されます。

$ kitchen login
Last login: Thu Jan 30 19:02:14 2017 from 10.0.2.2
hostname:  default-centos-72
fqdn:      default-centos-72
memory:    244180kBcpu count: 1

最後に終了

$ exit
Logout
Connection to 127.0.0.1 closed.

セットアップの破棄

$ Kitchen destroy
-----> Starting Kitchen (v1.4.2)
-----> Destroying <default-centos-72>...
   ==> default: Forcing shutdown of VM...
   ==> default: Destroying VM and associated drives...
   Vagrant instance <default-centos-72> destroyed.
   Finished destroying <default-centos-72> (0m4.94s).
-----> Kitchen is finished. (0m5.93s)