chefspec:render_file未能检查在另一种情况下
问题描述:
考虑以下配方去除模板的内容:chefspec:render_file未能检查在另一种情况下
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
...及以下chefspec测试:
require_relative 'spec_helper'
describe 'szdigi::mongo_test' do
before(:all) do
@conf_file = '/etc/mongodb.conf'
end
context 'Mongo 2.4' do
let(:chef_run) do
runner = ChefSpec::SoloRunner.new
runner.node.normal['szdigi']['mongodb']['version'] = '2.4.14'
runner.converge(described_recipe)
end
it 'creates mongo conf file' do
expect(chef_run).to create_template(@conf_file)
.with_mode(0o644)
.with_owner('root')
.with_group('root')
end
it 'does not remove /etc/mongodb.conf' do
expect(chef_run).not_to delete_file(@conf_file)
end
it 'defines authentication options' do
expect(chef_run).to render_file(@conf_file).with_content { |content|
expect(content).to match(/^\s*auth\s*=\s*true\s*$/)
}
end
end
context 'Mongo 2.6' do
before :all do
@conf_file = '/etc/mongod.conf'
end
let(:chef_run) do
runner = ChefSpec::SoloRunner.new
runner.node.normal['szdigi']['mongodb']['version'] = '2.6.12'
runner.converge(described_recipe)
end
it 'creates mongo conf file' do
expect(chef_run).to create_template(@conf_file)
.with_mode(0o644)
.with_owner('root')
.with_group('root')
end
it 'defines authentication options' do
expect(chef_run).to render_file(@conf_file).with_content { |content|
expect(content).to match(/^\s*auth\s*=\s*true\s*$/)
}
end
it 'removes 2.4 configuration file /etc/mongodb.conf' do
expect(chef_run).to delete_file('/etc/mongodb.conf')
end
end
end
配方不正是我想要什么:当版本属性设置为2.4.x和/etc/mongod.conf
时,它会创建/etc/mongodb.conf
,否则将使用auth = true
。在后一种情况下,它也删除了现在已过时的/etc/mongodb.conf
。
然而,只要配方包含file '/etc/mongodb.conf'
资源,chefspec在蒙戈2.4范围内的it 'defines authentication options'
失败:
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:
任何想法,我应该怎么改写render_file
测试2.6范围内才能取得成功?
感谢
帕特里夏
答
我会用一个单一的*if node['szdigi'][recipe_name]['version'].to_f >= 2.6
,然后重组方进入的两个分支。聪明的flow-y位的使用很好,但不。
谢谢,这个工程。但不是卫兵建议最佳做法? – trish
并非总是如此。这取决于你的代码的结构,在这种情况下,你有两个非常不同的分支,所以明确可能更具可读性。 – coderanger