如何检查Puppet资源的数组参数是否包含值

时间:2015-07-13 04:33:42

标签: rspec puppet rspec-puppet

我正在编写一个包含:: apache :: vhost资源的自定义puppet模块,并希望在我的rspec测试中验证目录参数是否包含某个值,而不重现整个目录配置,这些配置主要是硬编码的规范测试。

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变量

2 个答案:

答案 0 :(得分:1)

我最近偶然发现了同样的问题。没有一种干净的方法可以直接访问参数的内部部分。

我在freenode的voxpupuli频道与dev_el_ops交谈,他说“rspec-pupet的一个设计问题是它没有将属性值暴露给常规的rspec匹配器”

我不确定在ruby中找到“在数组中使用键的哈希”的最佳方法,所以我引用了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

答案 1 :(得分:0)

这个请求有些旧了,但是仍然与rspec-puppet和puppet 5+有关。至少对于简单(顶级)数组,可以使用正则表达式值来测试参数中是否包含该值。

以简单的木偶类为例:

# simple test with array parameter
class array_test(
  Array[String] $values = [],
) {
  notify{"And the values are: ${values}": }
}

以下测试包括对预期值的正则表达式检查:

# frozen_string_literal: true

require 'spec_helper'
describe 'array_test' do

  context 'with default values for all parameters' do
    it { is_expected.to compile.with_all_deps }
    it { is_expected.to contain_class('array_test') }
  end
  context 'with there parameters' do
    let(:params) {{
      'values' => ['a','b','c','abcd']
    }}

    it { is_expected.to contain_class('array_test').with('values' => %r{"a"}) }
    it { is_expected.to contain_class('array_test').with('values' => %r{"b"}) }
    it { is_expected.to contain_class('array_test').with('values' => %r{"c"}) }
    it { is_expected.to contain_class('array_test').with('values' => %r{"abcd"}) }
    it { is_expected.not_to contain_class('array_test').with('values' => %r{"d"}) }
  end
end

运行时会产生以下输出:

$ bundle exec rake spec
(in /home/vagrant/git/array_test)
I, [2019-08-01T15:35:28.987120 #6373]  INFO -- : Creating symlink from spec/fixtures/modules/array_test to /home/vagrant/git/array_test
/home/vagrant/.rvm/rubies/ruby-2.4.4/bin/ruby -I/home/vagrant/.rvm/gems/ruby-2.4.4/gems/rspec-core-3.8.2/lib:/home/vagrant/.rvm/gems/ruby-2.4.4/gems/rspec-support-3.8.2/lib /home/vagrant/.rvm/gems/ruby-2.4.4/gems/rspec-core-3.8.2/exe/rspec --pattern spec/\{aliases,classes,defines,functions,hosts,integration,plans,tasks,type_aliases,types,unit\}/\*\*/\*_spec.rb

array_test
  with default values for all parameters
    should compile into a catalogue without dependency cycles
    should contain Class[array_test]
  with there parameters
    should contain Class[array_test] with values =~ /"a"/
    should contain Class[array_test] with values =~ /"b"/
    should contain Class[array_test] with values =~ /"c"/
    should contain Class[array_test] with values =~ /"abcd"/
    should not contain Class[array_test] with values =~ /"d"/

Finished in 1.03 seconds (files took 4.26 seconds to load)
7 examples, 0 failures


Code coverage
  must cover at least 0% of resources
Total resources:   2
Touched resources: 0
Resource coverage:  0.00%

Untouched resources:
  Notify[And the values are: []]
  Notify[And the values are: [a, b, c, abcd]]