2015-07-13 179 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 
相關問題