2015-07-13 180 views
2

我在写一个自定义的puppet模块,其中包含一个:: apache :: vhost资源,并且想验证我的rspec测试中的目录参数是否包含一定的值,而不会重现在spec测试中大部分硬编码的整个目录配置。如何检查数组参数是否包含一个值

class foo::apache { 

    # prepend 'from ' to each element in array of subnets 
    # Change this ugliness to use map once we've upgraded to puppet v4 
    # open to suggetions on better way to do this too... 
    $subnets = $::foo::subnets 
    $subnets_yaml = inline_template('<%= subnets.map {|s| "from " +s}.to_yaml %>') 
    $allowed_subnets_directives = parseyaml($subnets_yaml) 

    ::apache::vhost { 'foo_vhost': 
    directories => [ 
     -- snip -- 
     ##### How can I check just the path & allow keys of this element? 
     { 'path' => '~^.*$', 
     'Order' => 'deny,allow', 
     'allow' => concat(['from localhost'], 
        $allowed_subnets_directives), 
     'provider' => 'location', 
     }, 
    ] 
    } # foo_vhost 
} # foo::apache 

为了简洁,我已经删除了大部分清单。

我可以测试整个指令参数与沿

describe 'foo::apache' do 
    it { is_expected.to contain_apache__vhost('foo_vhost').with(
    'directories' => [{'path' => '~^.*$', 
         'allow' => ['from localhost', 
            'from 10.20.30/24', 
            ],}, 
        ] 

线的东西,但目录的参数是长和静,和我热衷于避免这种情况。

RSpec的include匹配看起来像我需要什么,但我不能工作了如何使用它来验证参数,或$allowed_subnets_directives可变

+1

FWIW,在阵列预先计算的东西一切都可以使用[该regsubst函数]旧版本进行(http://docs.puppetlabs.com/references/stable/function.html#regsubst)。 –

回答

0

我最近偶然在这个同样的问题。没有一种干净的方式可以直接访问参数的内部部分。

我在freenode上的voxpupuli通道与dev_el_ops说话,他说:“RSpec的的 - pupet的设计问题之一是,它不公开属性值到正规rspec的匹配器”

我不知道到“发现在一个阵列的一个关键的哈希”在红宝石的最好办法,所以我引用this answer ,我会测试上面是这样

it do 
    vhost_directories = catalogue.resource('apache__vhost', 'foo_vhost').send(:parameters)[:directories] 
    expect(vhost_directories.find {|x| x[:path] == '~^.*$'}).to be_truthy 
end 

如果你做的方式假设它在数组中的第一个条目中,则可以使用更易读的'include' matcher上的散列。

it do 
    vhost_directories = catalogue.resource('apache__vhost', 'foo_vhost').send(:parameters)[:directories] 
    expect(vhost_directories.first).to include(:path => '~^.*$') 
end 
相关问题