
- Chef 教程
- Chef - 首頁
- Chef - 概述
- Chef - 架構
- Chef - 版本控制系統設定
- Chef - 工作站設定
- Chef - 客戶端設定
- Chef - Test Kitchen 設定
- Chef - Knife 設定
- Chef - Solo 設定
- Chef - Cookbook
- Chef - Cookbook 依賴關係
- Chef - 角色
- Chef - 環境
- Chef - Chef-Client 作為守護程序
- Chef - Chef-Shell
- Chef - 測試 Cookbook
- Chef - Foodcritic
- Chef - ChefSpec
- 使用 Test Kitchen 測試 Cookbook
- Chef - 節點
- Chef - Chef-Client 執行
- 高階 Chef
- 動態配置菜譜
- Chef - 模板
- Chef - 使用 Chef DSL 的純 Ruby
- Chef - 使用菜譜的 Ruby Gems
- Chef - 庫
- Chef - 定義
- Chef - 環境變數
- Chef - 資料包
- Chef - 資料包指令碼
- Chef - 跨平臺 Cookbook
- Chef - 資源
- 輕量級資源提供程式
- Chef - 藍圖
- Chef - 檔案和包
- Chef - 社群 Cookbook
- Chef 有用資源
- Chef - 快速指南
- Chef - 有用資源
- Chef - 討論
Chef - 使用 Test Kitchen 測試 Cookbook
Test Kitchen 是 Chef 的整合測試框架。它允許編寫測試,這些測試在虛擬機器例項化並使用 Cookbook 收斂後執行。測試在虛擬機器上執行,可以驗證一切是否按預期工作。
這是 ChefSpec 的節點契約,ChefSpec 僅模擬 Chef 執行。Test Kitchen 啟動一個真實的節點並在其上執行 Chef。
設定
為了做到這一點,我們需要在機器上安裝 Vagrant,它有助於管理虛擬機器。然後,我們需要安裝 Bookshelf 並將其與 Vagrant 連線,以便管理 Cookbook 依賴項。
步驟 1 - 編輯 Cookbook 中的預設菜譜。
vipin@laptop:~/chef-repo $ subl cookbooks/<Cookbook Name>/recipes/default.rb file "/tmp/greeting.txt" do content node['my_cookbook']['greeting'] end
步驟 2 - 編輯 Cookbook 屬性。
vipin@laptop:~/chef-repo $ subl cookbooks/<Cookbook Name>/attributes/default.rb default['my_cookbook']['greeting'] = "Ohai, Chefs!"
步驟 3 - 編輯 gem 檔案以安裝必要的 Ruby gem。
vipin@laptop:~/chef-repo $ subl Gemfile gem 'test-kitchen', '~> 2.0.0.alpha.7' gem 'kitchen-vagrant'
步驟 4 - 安裝必要的 Ruby gem。
vipin@laptop:~/chef-repo $ bundle install ...TRUNCATED OUTPUT... Installing test-kitchen (1.0.0.alpha.7) Installing kitchen-vagrant (0.10.0) ...TRUNCATED OUTPUT...
步驟 5 - 在 Cookbook 中建立 .kitchen.yml 檔案。
vipin@laptop:~/chef-repo/cookbooks/my_cookbook $ subl .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_ubuntu12.04_provisionerless.box suites: - name: default run_list: - recipe[minitest-handler] - recipe[my_cookbook_test] attributes: { my_cookbook: { greeting: 'Ohai, Minitest!'} }
步驟 6 - 在 Cookbook 中建立測試目錄。
vipin@laptop:~/chef-repo/cookbooks/<Cookbook Name>$ mkdir test
步驟 7 - 為整合測試建立一個測試 Cookbook。
vipin@laptop:~/chef-repo/cookbooks/<Cookbook Name>/test $ knife cookbook create my_cookbook_test ** Creating cookbook my_cookbook_test ** Creating README for cookbook: my_cookbook_test ** Creating CHANGELOG for cookbook: my_cookbook_test ** Creating metadata for cookbook: my_cookbook_test
步驟 8 - 編輯測試 Cookbook 的預設菜譜。
vipin@laptop:~/chef-repo/cookbooks/my_cookbook $ subl test/cookbooks/my_cookbook_test/recipes/default.rb include_recipe 'my_cookbook::default'
步驟 9 - 在 Cookbook 中建立 Minitest Spec。
vipin@laptop:~/chef-repo/cookbooks/my_cookbook $ mkdir -p test/cookbooks/my_cookbook_test/files/default/tests/minitest vipin@laptop:~/chef-repo/cookbooks/my_cookbook $ subl test/cookbooks/my_cookbook_test/files/default/tests/minitest/default_test.rb require 'minitest/spec' describe_recipe 'my_cookbook::default' do describe "greeting file" do it "creates the greeting file" do file("/tmp/greeting.txt").must_exist end it "contains what's stored in the 'greeting' node attribute" do file('/tmp/greeting.txt').must_include 'Ohai, Minitest!' end end
步驟 10 - 編輯主 Cookbook 的 Berksfile。
vipin@laptop:~/chef-repo/cookbooks/my_cookbook $ subl Berksfile site :opscode metadata cookbook "apt" cookbook "minitest-handler" cookbook "my_cookbook_test", path: "./test/cookbooks/my_cookbook_test"
測試設定
vipin@laptop:~/chef-repo/cookbooks/my_cookbook $ kitchen test -----> Starting Kitchen (v1.0.0.alpha.7) ...TRUNCATED OUTPUT... -----> Converging <default-ubuntu-1204> -----> Installing Chef Omnibus (true) ...TRUNCATED OUTPUT... Starting Chef Client, version 11.4.4 [2013-06-29T18:33:57+00:00] INFO: *** Chef 11.4.4 *** [2013-06-29T18:33:58+00:00] INFO: Setting the run_list to ["recipe[minitest-handler]", "recipe[my_cookbook_test]"] from JSON ...TRUNCATED OUTPUT... # Running tests: recipe::my_cookbook::default::greeting file#test_0001_creates the greeting file = 0.00 s = . recipe::my_cookbook::default::greeting file#test_0002_contains what's stored in the 'greeting' node attribute = 0.00 s = . Finished tests in 0.011190s, 178.7277 tests/s, 178.7277 assertions/s. 2 tests, 2 assertions, 0 failures, 0 errors, 0 skips ...TRUNCATED OUTPUT... -----> Kitchen is finished. (2m5.69s)
廣告