Puppet:rake spec 找不到 class ::splunk

Puppet: rake spec could not find class ::splunk

我正在尝试测试我的人偶模块,但我收到一条错误消息,提示找不到 class。

我写了几个内部使用的 Puppet 模块;既然我已经开始关注 puppet,我想编写测试。由于我 运行 遇到问题,我决定从本质上重新开始使用一个新的干净模块来弄清楚我需要做什么。

$puppet module generate wet-splunk
<<snip interactive questions>>
$cd splunk
$rake spec
<path snip>ruby -I/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-support-3.4.1/lib:/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/lib /Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration,types\}/\*\*/\*_spec.rb --color
F

Failures:

  1) splunk with defaults for all parameters should contain Class[splunk]
     Failure/Error: it { should contain_class('splunk') }

     Puppet::PreformattedError:
       Evaluation Error: Error while evaluating a Function Call, Could not find class ::splunk for host.example.com at line 1:1 on node host.example.com
     # ./spec/classes/init_spec.rb:5:in `block (3 levels) in <top (required)>'

Finished in 0.21712 seconds (files took 3.14 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/classes/init_spec.rb:5 # splunk with defaults for all parameters should contain Class[splunk]

/Users/wet/.rbenv/versions/1.9.3-p0/bin/ruby -I/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-support-3.4.1/lib:/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/lib /Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration,types\}/\*\*/\*_spec.rb --color failed

正在检查文件,但我认为它们包含应有的内容:

manifests/init.pp

# <snip documentation boilterplate>
class splunk {    
}

spec/classes/init_spec.rb

require 'spec_helper'
describe 'splunk' do

  context 'with defaults for all parameters' do
    it { should contain_class('splunk') }
  end
end

Could not find class ::splunk 是让我感到困惑的部分。

我读取规范文件的方式是在 init 内部寻找 class{'splunk': ... } 这可能是我误读了它,因为它们会从损坏的规范文件开始看起来很奇怪,但以防万一我尝试测试改为打包。

manifests/init.pp

class splunk {
  package{'splunk':
    ensure => 'present',
  }
}

spec/classes/init_spec.rb

require 'spec_helper'
describe 'splunk' do

  context 'with defaults for all parameters' do
    it { should contain_package('splunk') }
  end
end

我仍然收到类似的错误消息:

$ rake spec
/Users/wet/.rbenv/versions/1.9.3-p0/bin/ruby -I/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-support-3.4.1/lib:/Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/lib /Users/wet/.rbenv/versions/1.9.3-p0/lib/ruby/gems/1.9.1/gems/rspec-core-3.4.1/exe/rspec --pattern spec/\{classes,defines,unit,functions,hosts,integration,types\}/\*\*/\*_spec.rb --color
F

Failures:

  1) splunk with defaults for all parameters should contain Package[splunk]
     Failure/Error: it { should contain_package('splunk') }

     Puppet::PreformattedError:
       Evaluation Error: Error while evaluating a Function Call, Could not find class ::splunk for host.example.com at line 1:1 on node host.example.com
     # ./spec/classes/init_spec.rb:6:in `block (3 levels) in <top (required)>'

Finished in 0.21168 seconds (files took 3.17 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/classes/init_spec.rb:6 # splunk with defaults for all parameters should contain Package[splunk]

很抱歉问这么基本的问题;非常感谢你的帮助。我真的很喜欢有关测试 Puppet 代码的优秀教程的链接;我喜欢使用 Vag运行t 来确信我的代码实际上会按照我的意愿去做。

更新: 我相信我已经听从了@Peter_Souter 的建议;我仍然收到找不到 class 的错误。我正在发布所引用文件的内容以尝试验证我做了正确的事情,以防出现其他错误。

$ cat .fixtures.yml 
fixtures:
  symlinks:
    "splunk": "#{source_dir}"
$ cat spec_helper.rb 
require 'puppetlabs_spec_helper/module_spec_helper'
$ cat Rakefile 
require 'puppetlabs_spec_helper/rake_tasks'
require 'puppet-lint/tasks/puppet-lint'
PuppetLint.configuration.send('disable_80chars')
PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "pkg/**/*.pp"]

desc "Validate manifests, templates, and ruby files"
task :validate do
  Dir['manifests/**/*.pp'].each do |manifest|
    sh "puppet parser validate --noop #{manifest}"
  end
  Dir['spec/**/*.rb','lib/**/*.rb'].each do |ruby_file|
    sh "ruby -c #{ruby_file}" unless ruby_file =~ /spec\/fixtures/
  end
  Dir['templates/**/*.erb'].each do |template|
    sh "erb -P -x -T '-' #{template} | ruby -c"
  end
end
$ cat Gemfile
source 'https://rubygems.org'

puppetversion = ENV.key?('PUPPET_VERSION') ? "#{ENV['PUPPET_VERSION']}" : ['>= 3.3']
gem 'puppet', puppetversion
gem 'puppetlabs_spec_helper', '>= 0.8.2'
gem 'puppet-lint', '>= 1.0.0'
gem 'facter', '>= 1.7.0'

gem 'rspec-puppet', '~> 2.1',  :require => false
gem 'rspec-core', '3.1.7',     :require => false

rspec-puppet 模拟测试目录的方式,它会创建一个 fixtures 目录,其中包含所需的模块。所以现在它 运行 它在一个空目录中,所以它找不到你的模块。我们通常使用 puppetlabs-spec-helper 来解决这个问题,它会在该 fixtures 目录中创建一个到您当前存储库的 simlink。

我建议将以下内容添加到您的文件中:

# .fixtures.yml 
fixtures:
  symlinks:
    "splunk": "#{source_dir}"

# spec_helper.rb
require 'puppetlabs_spec_helper/module_spec_helper'

# Rakefile
require 'puppetlabs_spec_helper/rake_tasks'

# Gemfile
gem 'rspec-puppet', '~> 2.1',  :require => false
gem 'rspec-core', '3.1.7',     :require => false

你能试试看它是否适合你吗?

如果有人像我一样偶然发现这个线程。对我来说正确答案在:

意味着你必须做:

puppet module generate foo-bar

然后解决这个问题

bundle exec rspec-puppet-init

这不是很好,但比处理隐藏的点文件更好。