testing - 如果可以将变量传递给 Puppet 中的类,则运行 Rspec 会失败

标签 testing rspec tdd puppet

问题

如果变量可以传递给 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. 根据 this Q&A hiera-puppet-helper 导致 问题。 Rspec-puppet 似乎支持测试 hiera 和 'hiera-puppet-helper' 可以被替换。好吧,也许这可以解决问题,但导致问题的原因是什么?
  2. This post包含相同的问题,但不是要解决的解决方案 问题
  3. This post表示删除类参数可以解决问题,但是这个类被多个模块使用,因此这不是解决方案。

最佳答案

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-helper gem 确实不是必需的,事实上它不应该存在。我建议使用 Garethr's skeleton 生成模块结构其中已经包含 Hiera 的工作设置,可以为您节省大量时间。

关于testing - 如果可以将变量传递给 Puppet 中的类,则运行 Rspec 会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29309341/

相关文章:

c# - 尝试...... catch block 侵扰

selenium - 使用 selenium 自动化 MSD365 的问题

ruby - 之间的差异。和 #

javascript - casperjs的测试属性

phpvendor\bin\phpunit 正在控制台中打印文本

ruby-on-rails - Capybara 的 stub 浏览器时间和时区

ruby - 运行 RSpec 时找不到库类

javascript - 为 Math.Random(); 编写测试用例;密码战套路

java - 减少测试用例之间的耦合

unit-testing - 为什么使用集成测试而不是单元测试是一个坏主意?