rspec木偶中的动态值分配



是否有任何选项可以为rspec puppet中的参数动态赋值?

下面是我的示例rspec木偶测试用例。现在,我已经对所有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

是的,由于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_examplesshared_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

相关内容

  • 没有找到相关文章

最新更新