Chef 测试Cookbook
如果说明书直接部署并在生产服务器上运行,说明书很可能会在生产中分解。防止这种情况发生的最好方法是在设置环境中测试说明书。
以下是测试步骤。
步骤 1-使用以下命令安装说明书。
vipin@laptop:~/chef-repo $ knife cookbook site install <cookbook name>
第 2 步-在工作说明书上运行knife 说明书测试命令。
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 测试命令。
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 说明书测试对说明书中的所有 Ruby 文件以及所有 ERB 模板执行 Ruby 语法检查。它循环遍历 Ruby 文件并对每个文件运行 Ruby
–c。 Ruby
–c 检查脚本的语法并退出而不运行它。
在遍历所有 Ruby 文件后,knife cookbook 测试遍历所有 ERB 模板和管道,即由
–x 到 Ruby
–c 创建的冗余版本。
限制
Knife Cookbook测试仅对 Ruby 文件和 ERB 模板进行简单的语法检查。我们可以通过使用 ChefSpec 和 test kitchen 进行全面的测试驱动。