📜  Chef-带测试厨房的测试食谱

📅  最后修改于: 2020-11-02 03:23:13             🧑  作者: Mango


测试厨房是Chef的集成测试框架。它支持编写测试,该测试在使用菜谱实例化和融合VM之后运行。这些测试在VM上运行,并且可以验证一切是否按预期进行。

这是ChefSpec的节点合同,ChefSpec仅模拟Chef运行。测试厨房启动一个真实节点并在其上运行Chef。

配置

为此,我们需要在计算机上安装Vagrant,这有助于管理虚拟机。然后,我们需要安装书架并与Vagrant挂钩,以管理菜谱依赖。

步骤1-在食谱中编辑默认食谱。

vipin@laptop:~/chef-repo $ subl cookbooks//recipes/default.rb 
file "/tmp/greeting.txt" do 
   content node['my_cookbook']['greeting'] 
end

步骤2-编辑食谱属性。

vipin@laptop:~/chef-repo $ subl cookbooks//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-在食谱中创建.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-在食谱内部创建测试目录。

vipin@laptop:~/chef-repo/cookbooks/$ mkdir test 

步骤7-创建用于集成测试的测试手册。

vipin@laptop:~/chef-repo/cookbooks//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-编辑测试食谱的默认配方。

vipin@laptop:~/chef-repo/cookbooks/my_cookbook $ subl 
test/cookbooks/my_cookbook_test/recipes/default.rb 
include_recipe 'my_cookbook::default'

步骤9-在食谱内部创建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-编辑主食谱的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  
-----> 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)