如果变量可以传递到Puppet中的类,则运行Rspec失败



问题

如果变量可以传递到Puppet类,例如:

class module_name (
  $variable='hello_world'
) {
  package { 'package_name': }
}

并且rspec运行时失败,即:

[user@host module_name]$ rspec
...............................FFFFFFFFFFFF..........................................
Failures:
  1) opsview should contain Class[module_name]
     Failure/Error: it { should contain_class('module_name') }
     Puppet::Error:
       Error from DataBinding 'hiera' while looking up 'module_name::variable': 
       FileSystem implementation expected Pathname, got: 'Hash' on node host
     # /usr/share/ruby/vendor_ruby/puppet/resource.rb:393:
         in `rescue in lookup_with_databinding'
     # /usr/share/ruby/vendor_ruby/puppet/resource.rb:387:
         in `lookup_with_databinding'
     # /usr/share/ruby/vendor_ruby/puppet/resource.rb:381:
         in `lookup_external_default_for'

主要问题

Error from DataBinding while looking up FileSystem implementation expected Pathname, 
got: 'Hash' on node

配置

版本

[vagrant@vm-one opsview]$ puppet --version
3.7.5
[vagrant@vm-one opsview]$ rspec --version
3.2.2

Spec_helper

[vagrant@vm-one opsview]$ cat spec/spec_helper.rb
require 'rspec-puppet'
require 'hiera-puppet-helper'
fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
RSpec.configure do |c|
  c.module_path = File.join(fixture_path, 'modules')
  c.manifest_dir = File.join(fixture_path, 'manifests')
  c.hiera_config = '/etc/puppet/hiera.yaml'
end
at_exit { RSpec::Puppet::Coverage.report! }

尝试

  1. 根据这个问答;hiera-puppet-helper导致问题Rspec木偶似乎支持对hiera和"hiera木偶助手"可以被替换。好吧,也许这解决了问题,但是什么导致了问题
  2. 这篇文章包含相同的问题,但不是要解决的解决方案问题
  3. 这篇文章指出,删除类参数可以解决这个问题,但这个类被多个模块使用,因此这不是一个解决方案

hiera_config有问题,应该是:

c.hiera_config = 'spec/fixtures/hiera/hiera.yaml'

假设您有如下目录结构:

  spec
    fixtures
      hiera
        hiera.yaml
        default.yaml

hiera.yaml:

---
:backends:
  - yaml
:hierarchy:
  - '%{::clientcert}'
  - default
:yaml:
  :datadir: './spec/fixtures/hiera'

hiera-puppet-helpergem确实不是必需的,事实上它不应该存在。我建议使用Garethr的骨架生成模块结构,该骨架已经包含Hiera的工作设置,可以节省大量时间。

最新更新