我的食谱install_packages
中有两个如下所示的Chef资源块:
%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
但是我的Chef运行失败并显示错误消息:
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