是否有任何选项可以在rspec-puppet中动态分配参数值?
以下是我的示例rspec-puppet测试用例。现在,我已经对所有FQDN和主机名详细信息进行了硬编码。
是否可以动态分配这些值?
let :facts do
{
:fqdn => 'puppet.example.com',
:hostname => 'puppet',
:ipaddress => 'xxx.yy.ee.zzz'
}
end
it do
should contain_host('puppet.example.com').with(
'ensure' => 'present',
'host_aliases' => 'puppet',
'ip' => 'xxx.yy.ee.zzz'
)
end
答案 0 :(得分:0)
是的,因为rspec本身就是ruby代码本身,你可以这样做:
['1.1.1.1','2.2.2.2'].each do | ip_address |
context "with an IP Address of #{ip_address}" do
let :facts do
{
:fqdn => 'puppet.example.com',
:hostname => 'puppet',
:ipaddress => ip_address,
}
end
it do
is_expected.to contain_host('puppet.example.com').with(
'ensure' => 'present',
'host_aliases' => 'puppet',
'ip' => ip_address,
)
end
end
end
您还可以使用shared_examples
和shared_contexts
执行类似操作:
shared_examples :compile, :compile => true do
it { should compile.with_all_deps }
end
shared_context 'redhat_facts' do |operatingsystem, operatingsystemrelease|
let(:facts) { super().merge({
:osfamily => "RedHat",
:operatingsystem => operatingsystem,
:operatingsystemrelease => operatingsystemrelease
}) }
end