Chef 简明教程
Chef - Testing Cookbooks
如果菜谱直接部署并在生产服务器上运行,菜谱可能会在生产中出现故障。防止这种情况发生的最佳方法是在设置环境中测试菜谱。
In case the cookbook is directly deployed and run on the production server, there are high chances that the cookbook can break up in production. The best way to prevent this from happening is, testing the cookbook in the setup environment.
以下是测试步骤。
Following are the steps for testing.
Step 1 − 使用以下命令安装菜谱。
Step 1 − Install the cookbook using the following command.
vipin@laptop:~/chef-repo $ knife cookbook site install <cookbook name>
Step 2 − 对正在使用的菜谱运行 knife cookbook test 命令。
Step 2 − Run the knife cookbook test commands on the working cookbook.
vipin@laptop:~/chef-repo $ knife cookbook test VTest
checking ntp
Running syntax check on ntp
Validating ruby files
Validating templates
Step 3 − 在菜谱中破坏一些内容并重新进行测试。
Step 3 − Break something in the cookbook and test again.
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
Step 4 − 再次运行 knife test 命令。
Step 4 − Run the knife test command again.
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:
Working Method
Knife cookbook test 对菜谱中所有 Ruby 文件以及所有 ERB 模板执行 Ruby 语法检查。它循环处理 Ruby 文件,并对每个文件运行 Ruby –c 。Ruby –c 检查脚本的语法并在不运行它的情况下退出。
Knife cookbook test executes a Ruby syntax check on all the Ruby files within the cookbook as well as all ERB templates. It loops through Ruby files and runs Ruby –c against each of them. Ruby –c checks the syntax of the script and quits without running it.
在遍历完所有 Ruby 文件后,knife cookbook test 将遍历所有 ERB 模板和通过 Ruby –c 创建的冗余版本 –x 。
After going through all the Ruby files, knife cookbook test goes through all ERB templates and pipes, the redundant version created by –x through Ruby –c.