gpt4 book ai didi

testing - Puppet:rake spec 找不到 class::splunk

转载 作者:行者123 更新时间:2023-11-28 19:59:12 29 4
gpt4 key购买 nike

我正在尝试测试我的 puppet 模块,但我收到一条错误消息,指出它找不到类。

我写了几个内部使用的 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

检查文件,虽然我认为它们包含它们应该包含的内容:

ma​​nifests/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': ... } 这可能是我误读了它,因为看起来很奇怪他们会以损坏的规范开始文件,但以防万一我尝试测试包。

ma​​nifests/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 代码的优秀教程的链接;我很乐意使用 Vagrant 来确信我的代码实际上会执行我想要的操作。

更新:我相信我已经听从了@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

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

关于testing - Puppet:rake spec 找不到 class::splunk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33902643/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com