我的食谱代码
bash 'capture-aide-output' do
code <<-EOH
aide --check | grep differences | tee /tmp/output.txt
EOH
end
ruby_block 'check-output' do
block do
output_file = '/tmp/output.txt'
if File.exist?(output_file) && File.read(output_file).include?('differences')
raise('Halting Converge')
else
Chef::Log.info("Aide check output does not contain 'differences', proceeding converge")
end
if ::File.exist?(output_file)
::File.delete(output_file)
Chef::Log.info("Deleted #{output_file}")
end
end
end
对应的chefspec代码
require 'spec_helper'
require 'chefspec'
describe 'your_cookbook::your_recipe' do
let(:chef_run) { ChefSpec::SoloRunner.new.converge(described_recipe) }
context 'when the output file contains differences' do
before do
allow(File).to receive(:exist?).with('/tmp/output.txt').and_return(true)
allow(File).to receive(:read).with('/tmp/output.txt').and_return('differences')
end
it 'raises an exception and halts the converge' do
expect { chef_run }.to raise_error(RuntimeError, 'Halting Converge')
end
end
end
Chefspec 测试,抛出以下错误。
Failures:
1) your_cookbook::your_recipe , when the output file contains differences
Failures/Error: expect { chef_run }.to raise_error 'Halting Converge'
expected Exception with "Halting Converge" but nothing was raised
你能帮忙找出我在 ChefSpec 测试中遗漏了什么不起作用吗