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

Rspec 如何检查Puppet资源的数组参数是否包含值,rspec,puppet,rspec-puppet,Rspec,Puppet,Rspec Puppet,我正在编写一个包含::apache::vhost资源的自定义puppet模块,并希望在我的rspec测试中验证directories参数是否包含某个值,而无需复制整个目录配置,该配置在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

我正在编写一个包含::apache::vhost资源的自定义puppet模块,并希望在我的rspec测试中验证directories参数是否包含某个值,而无需复制整个目录配置,该配置在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
但是directories参数是长且静态的,我希望避免这种情况


rspec
include
matcher看起来像我需要的,但是我不知道如何使用它来验证参数,或者
$allowed\u subnets\u指令
变量

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

我在freenode上的voxpupuli频道与dev_el_ops交谈,他说“rspec pupet的设计问题之一是它不会向常规的rspec匹配器公开属性值”

我不确定在ruby中“查找数组中带有键的哈希”的最佳方法,所以我引用了 我测试上面的方法是这样的

it do
  vhost_directories = catalogue.resource('apache__vhost', 'foo_vhost').send(:parameters)[:directories]
  expect(vhost_directories.find {|x| x[:path] == '~^.*$'}).to be_truthy
end
如果假设它位于数组中的第一个条目中,那么可以在散列上使用可读性稍高的

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

这个请求有些过时,但是它仍然与rspecpuble和puppet5+相关。至少对于简单(顶级)数组,可以使用regex值来测试参数中是否包含该值

例如,对于简单的puppet类:

# 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]]

FWIW,在旧版本中,可以使用为数组中的所有内容预先添加内容。
$ 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]]