【问题标题】:Execute instructions in chef recipes only for Test Kitchen converge action仅为测试厨房收敛动作执行厨师食谱中的指令
【发布时间】:2021-03-20 07:58:25
【问题描述】:

我需要一种方法来运行部分厨师食谱,仅在测试厨房中发生收敛动作的情况下。

我为 ChefSpec 找到了一种方法:

unless defined?(ChefSpec)
  
  cookbook_file "/home/#{node['user']}/script.sh" do
    source 'install.sh'
    owner node['user']
    mode '0755'
    action :create
  end

end

厨房工具如何做到这一点?

【问题讨论】:

    标签: ruby chef-infra test-kitchen


    【解决方案1】:

    此要求已在 Github 上的 feature request 上进行了详细讨论。

    有两种方法可以做到这一点。一种是定义一个环境变量,例如TEST_KITCHEN,然后在带有if 条件、only_ifnot_if 守卫的配方中使用它。

    以下应该可以:

    设置环境变量:

    export TEST_KITCHEN="1"
    

    有条件地运行资源:

    if ENV['TEST_KITCHEN']
      cookbook_file "/home/#{node['user']}/script.sh" do
        source 'install.sh'
        owner node['user']
        mode '0755'
        action :create
      end
    end
    

    其他方法是使用节点属性,例如将node['test_kitchen'] 设置为true 并使用它有条件地运行操作。

      cookbook_file "/home/#{node['user']}/script.sh" do
        source 'install.sh'
        owner node['user']
        mode '0755'
        action :create
        only_if { node['test_kitchen'] }
      end
    

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多