Chef infra chefspec:render_文件无法检查在其他上下文中删除的模板内容

Chef infra chefspec:render_文件无法检查在其他上下文中删除的模板内容,chef-infra,chefspec,Chef Infra,Chefspec,考虑以下配方: recipe_name = 'mongodb' service_name = if node['szdigi'][recipe_name]['version'].to_f < 2.6 'mongodb' else 'mongod' end conffile = "/etc/#{service_name}.conf"

考虑以下配方:

  recipe_name = 'mongodb'
  service_name = if node['szdigi'][recipe_name]['version'].to_f < 2.6
                   'mongodb'
                 else
                   'mongod'
                 end

  conffile = "/etc/#{service_name}.conf"

  # Add configuration
  template conffile do
    source 'mongodb/mongodb-conf.erb'
    owner 'root'
    group 'root'
    mode 0o644
  end

  # Remove old config file
  file '/etc/mongodb.conf' do
    action :delete
    only_if { node['szdigi'][recipe_name]['version'].to_f >= 2.6 }
  end
这个配方正是我想要的:当version属性设置为2.4.x时,它创建
/etc/mongodb.conf
,否则它创建
/etc/mongod.conf
,两者都使用
auth=true
。在后一种情况下,它还删除了现在已经过时的
/etc/mongodb.conf

但是,只要配方包含
文件'/etc/mongodb.conf'
资源,chefspec就会在Mongo 2.4上下文中的
上失败,它“定义身份验证选项”

Failure/Error:
   expect(chef_run).to render_file(@conf_file).with_content { |content|
     expect(content).to match(/^\s*auth\s*=\s*true\s*$/)
   }

   expected Chef run to render "/etc/mongodb.conf" matching:

   (the result of a proc)

   but got:
你知道我应该如何在2.6环境下重写
render\u文件
测试才能成功吗

谢谢


Patricia

如果节点['szdigi'][recipe\u name]['version'],我将使用一个顶级
,to\u f>=2.6
,然后将配方重组为该节点上的两个分支。使用巧妙的flow-y位很好,但不要这样做。

谢谢,这很有效。但卫兵不是被推荐的最佳实践吗?不总是这样。这取决于代码的结构,在本例中,您有两个非常不同的分支,因此使其显式可能更具可读性。
Failure/Error:
   expect(chef_run).to render_file(@conf_file).with_content { |content|
     expect(content).to match(/^\s*auth\s*=\s*true\s*$/)
   }

   expected Chef run to render "/etc/mongodb.conf" matching:

   (the result of a proc)

   but got: