- 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 - 測試 Cookbook
如果 Cookbook 直接部署並執行在生產伺服器上,Cookbook 在生產環境中崩潰的可能性很高。防止這種情況發生的最佳方法是在設定環境中測試 Cookbook。
以下是測試步驟。
步驟 1 - 使用以下命令安裝 Cookbook。
vipin@laptop:~/chef-repo $ knife cookbook site install <cookbook name>
步驟 2 - 在可工作的 Cookbook 上執行 knife cookbook test 命令。
vipin@laptop:~/chef-repo $ knife cookbook test VTest checking ntp Running syntax check on ntp Validating ruby files Validating templates
步驟 3 - 破壞 Cookbook 中的內容,然後再次測試。
vipin@laptop:~/chef-repo $ subl cookbooks/VTest/recipes/default.rb
...
[ node['ntp']['varlibdir']
node['ntp']['statsdir'] ].each do |ntpdir|
directory ntpdir do
owner node['ntp']['var_owner']
group node['ntp']['var_group']
mode 0755
end
end
步驟 4 - 再次執行 knife test 命令。
vipin@laptop:~/chef-repo $ knife cookbook test ntp checking ntp Running syntax check on ntp Validating ruby files FATAL: Cookbook file recipes/default.rb has a ruby syntax error: FATAL: cookbooks/ntp/recipes/default.rb:25: syntax error, unexpected tIDENTIFIER, expecting ']' FATAL: node['ntp']['statsdir'] ].each do |ntpdir| FATAL: ^ FATAL: cookbooks/ntp/recipes/default.rb:25: syntax error, unexpected ']', expecting $end FATAL: node['ntp']['statsdir'] ].each do |ntpdir| FATAL:
工作方法
Knife cookbook test 會對 Cookbook 中的所有 Ruby 檔案以及所有 ERB 模板執行 Ruby 語法檢查。它迴圈遍歷 Ruby 檔案,並對每個檔案執行 Ruby –c。Ruby –c 檢查指令碼的語法,並在不執行指令碼的情況下退出。
遍歷所有 Ruby 檔案後,knife cookbook test 會遍歷所有 ERB 模板,並將 –x 建立的冗餘版本透過管道傳遞給 Ruby –c。
侷限性
Knife cookbook test 只對 Ruby 檔案和 ERB 模板進行簡單的語法檢查。我們可以使用 ChefSpec 和 Test Kitchen 進行完全測試驅動。
廣告