0

是否有任何选项可以在 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
4

1 回答 1

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你也可以用and做类似的事情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
于 2015-07-07T13:05:33.210 回答