Chef infra 如何使用文件缓存路径测试厨师资源

Chef infra 如何使用文件缓存路径测试厨师资源,chef-infra,chef-recipe,Chef Infra,Chef Recipe,我的食谱中有以下资源 download_dir = "#{Chef::Config[:file_cache_path]}\\BESClient" task_done = "#{Chef::Config[:file_cache_path]}\\BESClient\\installed.txt" file task_done do content Date.today.to_s action :create_if_missing end 与此对应,我编写了以下chef spec测试

我的食谱中有以下资源

download_dir = "#{Chef::Config[:file_cache_path]}\\BESClient"
task_done = "#{Chef::Config[:file_cache_path]}\\BESClient\\installed.txt"

file task_done do
  content Date.today.to_s
  action :create_if_missing
end
与此对应,我编写了以下chef spec测试

    context 'Windows 2012R2: when all attributes are default' do
    let(:chef_run) do
      runner = ChefSpec::ServerRunner.new(platform: 'windows', version: '2012R2', file_cache_path: 'C:\Program Files (x86)\BigFix Enterprise')
      runner.converge(described_recipe)
    end

    it 'converges successfully' do
      expect { chef_run }.to_not raise_error
    end

    download_dir = 'C:\\Program Files (x86)\\BigFix Enterprise\\BESClient'
    task_done = 'C:\\Program Files (x86)\\BigFix Enterprise\\BESClient\\installed.txt'

    it 'creates a file with attributes' do
       expect(chef_run).to create_file_if_missing(task_done.to_s).with(
         content: Date.today.to_s)
    end
这将尝试在我的工作站上创建
C:\ProgramFiles(x86)\BigFix Enterprise
目录,但如果我从运行程序中删除
file\u cache\u path
变量,单元测试将失败,并出现以下错误

  1) besclient::windows Windows 2012R2: when all attributes are default creates
a file with attributes
     Failure/Error:
       expect(chef_run).to create_file_if_missing(task_done.to_s).with(
         content: Date.today.to_s
       )

       expected "file[C:\Program Files (x86)\BigFix Enterprise\BESClient\install
ed.txt]" with action :create_if_missing to be in Chef run. Other file resources:


         file[C:/Users/AKANKS~1/AppData/Local/Temp/chefspec20180413-11784-1dp9wh
rfile_cache_path\BESClient\installed.txt]

是否有人帮助测试此方案?

默认情况下,chefspec在每次运行中为文件缓存设置一个新的临时目录,该目录可能不存在于您的计算机中。为了避免这种情况,我们需要设置文件缓存路径。除非指定,否则规格测试将失败。可以在spec_helper.rb文件中设置此选项,以避免在每个spec文件中重复

RSpec.configure do |config|
  config.file_cache_path = Chef::Config[:file_cache_path]
end

只需在规范中使用
Chef::Config[:file\u cache\u path]
。这通常不是一个好主意(使用与输入和测试相同的值),但在这种情况下,它非常简单,值得一试

expect(chef_run).to create_file_if_missing("#{Chef::Config[:file_cache_path]}\\BESClient\\installed.txt")

因此,通过此更改,我是否应该在运行程序中添加
file\u cache\u path
参数?我应该只更新
spec\u helper.rb
文件并从runner中删除吗?如果我在运行程序中保留参数,尽管测试没有失败,我会得到
未触及的资源:文件[C:\chef\cache\BESClient\installed.txt]C:0
我从运行程序中删除参数,测试也会以同样的错误失败。添加spec_helper.rb应该可以修复,您可以从运行程序中删除。您需要在spec
require spec\u helper
require'spec\u helper'
中包含spec\u helper。当我从运行程序中删除参数时,我会遇到上述错误。我在spec\u帮助程序中使用了
RSpec.configure do | config | config.file\u cache\u path=Chef::config[:file\u cache\u path]end
。希望可以吗?这里不需要编辑任何路径?我认为这将与这两行相关,我认为download_dir='C:\\Program Files(x86)\\BigFix Enterprise\\BESClient'task_done='C:\\Program Files(x86)\\BigFix Enterprise\\BESClient\\installed.txt'将其更改为配方下载_dir=“{Chef::Config[:file_cache_path]}\\BESClient”task_done=“#{Chef::Config[:file_cache_path]}\\BESClient\\installed.txt”