预计 "execute[Enable RHEL rhel7-x86_x64-linux-custom]" 采取行动:运行 成为主厨 运行

Expected "execute[Enable RHEL rhel7-x86_x64-linux-custom]" with action :run to be in Chef run

我的食谱中有两个看起来像这样的 Chef 资源块 install_packages:

%w[rhel7-x86_x64-linux-custom rhel7-x86_x64-linux-latest].each do |repo|
  execute "Enable RHEL #{repo}" do
    command "yumtool -a #{repo}"
    not_if { :: File.file?("/etc/yum.repos.d/#{repo}.repo") }
  end
end

PACKAGES.each do |pkg_entry|
  parts = pkg_entry.split('@')
  pkg_name = parts[0]
  pkg_version = parts[1]
  yum_package pkg_entry do
    allow_downgrade true
    package_name pkg_name
    version pkg_version
    action: install
  end
end

相应的规范单元测试块会是什么样子?对于我尝试过的第一个街区:

it 'executes command' do
  expect(chef_run).to run_execute('Enable RHEL rhel7-x86_x64-linux-custom')
  expect(chef_run).to run_execute('Enable RHEL rhel7-x86_x64-linux-latest')
end  

但我的厨师 运行 失败并显示错误消息:

expected "execute[Enable RHEL rhel7-x86_x64-linux-custom]" with action :run to be in Chef run. Other execute resources:

    execute[Enable RHEL rhel7-x86_x64-linux-custom]
    execute[Enable RHEL rhel7-x86_x64-linux-latest]

我不确定如何解决这个问题,对测试块应该是什么样子有什么想法吗?

我认为您的 execute 资源不是 运行,这就是 ChefSpec 无法识别 run_execute(但我认为会识别 nothing_execute)的原因。

可能您的计算机上有 /etc/yum.repos.d/rhel7-x86_x64-linux-custom.repo 文件,这就是资源不是 运行 的原因。您需要在 ChefSpec::SoloRunner 收敛配方之前对调用进行存根。

let :subject do 
  allow(::File).to receive(:file?).with('/etc/yum.repos.d/rhel7-x86_x64-linux-custom.repo').and_return(false)
  allow(::File).to receive(:file?).with('/etc/yum.repos.d/rhel7-x86_x64-linux-latest.repo').and_return(false)
  ChefSpec::Runner.new.converge described_recipe
end

如果您的 PACKAGES 常量是这样分配的:

# assignment real recipe: 
PACKAGES = get_user_data_config('packages', [])

你可以在规范中存根:

allow(Chef::Recipe).to receive(:get_user_data_config).and_return(<some value>)