使用not_if guard进行厨师规范测试

时间:2018-04-17 16:00:39

标签: chef-recipe recipe

我有以下厨师辞职

  rpm_package 'Install BESAgent' do
    source node['besclient']['package_url']
    action :install
    not_if { "rpm -qa | grep -qx 'BESAgent-9.5.9.62-rhe6.x86_64'" || "rpm -qa | grep -qx 'BESAgent-9.5.9.62-sle11.x86_64'" }
  end

对应于此我有以下厨师规格测试

it 'installs rpm_package for BES Client' do
  expect(chef_run).to install_rpm_package('Install BESAgent').with(source: 'http://software.bigfix.com/download/bes/95/BESAgent-9.5.9.62-rhe6.x86_64.rpm' , not_if: '{ \"rpm -qa | grep -qx \'BESAgent-9.5.9.62-rhe6.x86_64\'\" || \"rpm -qa | grep -qx \'BESAgent-9.5.9.62-sle11.x86_64\'\"}'))
end

发出警告

[2018-04-17T21:18:03+05:30] WARN: not_if block for rpm_package[Install BESAgent]
returned "rpm -qa | grep -qx 'BESAgent-9.5.9.62-rhe6.x86_64'", did you mean to
run a command? If so use 'not_if "rpm -qa | grep -qx 'BESAgent-9.5.9.62-rhe6.x86_64'"' in your code. 

以及以下错误

  1) besclient::linux redhat 6.8: when all attributes are default installs rpm_
ackage for BES Client
     Failure/Error: expect(chef_run).to install_rpm_package('Install BESAgent')
with(source: 'http://software.bigfix.com/download/bes/95/BESAgent-9.5.9.62-rhe6
x86_64.rpm', not_if: '{ \"rpm -qa | grep -qx \'BESAgent-9.5.9.62-rhe6.x86_64\'\
 || \"rpm -qa | grep -qx \'BESAgent-9.5.9.62-sle11.x86_64\'\"}')
       expected "rpm_package[Install BESAgent]" actions [] to include :install

1 个答案:

答案 0 :(得分:1)

not_if块中使用命令时,不需要使用花括号。您只需使用双引号或单引号

即可
not_if "rpm -qa | grep -qx 'BESAgent-9.5.9.62-rhe6.x86_64'" || "rpm -qa | grep -qx 'BESAgent-9.5.9.62-sle11.x86_64'"