为什么我会得到 puppet-rspec '类不存在'? [英] Why do I get puppet-rspec 'class does not exist' when it does?
问题描述
我使用以下 Gemfile
设置了一个新的 puppet demo
模块,当我运行一个简单的 puppet-rspec 测试时,它按预期工作.
Gemfile
source 'https://rubygems.org'如果 puppetversion = ENV['PUPPET_GEM_VERSION']gem 'puppet', puppetversion, :require =>错误的别的宝石傀儡",3.7.5"结尾宝石'元数据-json-lint'宝石 'puppetlabs_spec_helper','> = 0.1.0'gem 'puppet-lint','> = 1.0.0'宝石'因素','> = 1.7.0'gem 'rspec-puppet-facts'# rspec 必须是 ruby 1.8.7 的 v2如果 RUBY_VERSION >= '1.8.7' 并且 RUBY_VERSION <'1.9'gem 'rspec', '~>2.0'结尾
但是,当我克隆现有模块 (silex
) 并将 Gemfile 更新为如上所示并运行时:
bundle exec rake 规范
我收到以下错误:
vagrant$ bundle exec rake 规范/usr/bin/ruby -S rspec spec/classes/init_spec.rb --colorF失败:1) 具有所有参数默认值的 silex 应包含 Class[silex]失败/错误:它 { 应该包含_class('silex') }木偶::错误:在节点 centos65-box-1 上找不到 centos65-box-1 的类 silex# ./spec/classes/init_spec.rb:5在 0.31963 秒内完成1个例子,1个失败失败的例子:rspec ./spec/classes/init_spec.rb:5 # 带有所有参数默认值的 silex 应该包含 Class[silex]
班级存在,我已经完成了木偶运行.有人可以帮助我理解为什么会这样吗?
更多信息
# 树.├── 文件│ └── httpd.patch├── Gemfile├── Gemfile.lock├── 库│ └── 因素│ └── vendor_dir.rb├── 清单│ ├──备份.pp│ ├── config.pp│ ├── init.pp│ ├── install.pp│ ├── params.pp│ └── service.pp├──元数据.json├── Rakefile├── README.md├── 规格│ ├──类│ │ └── init_spec.rb│ ├──灯具│ │ ├── 清单│ │ │ └── site.pp│ │ └── 模块│ └── spec_helper.rb├── 模板│ └── var│ └── 配置│ └── settings.json.erb└── 测试└── init.pp#spec_helper.rb需要'puppetlabs_spec_helper/module_spec_helper'需要'rspec-puppet-facts'包括 RspecPuppetFacts#init_spec.rb需要'spec_helper'描述silex"做上下文所有参数都有默认值"做它 { 应该包含_class('silex') }结尾结尾
所以事实证明我在 fixtures
目录中缺少 module
导致 可能找不到类
错误.
立即解决
我必须运行 rspec-puppet-init
它将我的 spec
目录更新为以下内容:spec_helper.rb><前>需要'rspec-傀儡'fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))RSpec.configure 做 |c|c.module_path = File.join(fixture_path, 'modules')c.manifest_dir = File.join(fixture_path, 'manifests')c.environmentpath = File.join(Dir.pwd, 'spec')结尾
<小时>
详细解决方案
这是我为使其工作而采取的所有步骤.如果这是最好的方法,我很想得到一些反馈:
1.添加到 Gemfile
并运行:
<前>#您可能需要根据您的环境来调整版本.捆绑安装Gemfile:
source 'https://rubygems.org'如果 puppetversion = ENV['PUPPET_GEM_VERSION']gem 'puppet', puppetversion, :require =>错误的别的宝石傀儡",3.7.5"结尾宝石'元数据-json-lint'宝石 'puppetlabs_spec_helper','> = 0.1.0'gem 'puppet-lint', '>= 1.0.0'宝石'因素','> = 1.7.0'gem 'rspec-puppet-facts'# rspec 必须是 ruby 1.8.7 的 v2如果 RUBY_VERSION >= '1.8.7' 并且 RUBY_VERSION <'1.9'gem 'rspec', '~>2.0'结尾
2.初始化您的 spec
目录.从模块根目录运行.
bundle exec rspec-puppet-init
3.我必须将自动生成的 Rakefile
更新为以下内容:
需要'rubygems'需要'puppetlabs_spec_helper/rake_tasks'需要 'puppet-lint/tasks/puppet-lint'PuppetLint.configuration.send('disable_80chars')PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "pkg/**/*.pp"]desc "验证清单、模板和 ruby 文件"任务:验证做目录['manifests/**/*.pp'].each do |manifest|sh "木偶解析器验证 --noop #{manifest}"结尾dir['spec/**/*.rb','lib/**/*.rb'].each do |ruby_file|sh "ruby -c #{ruby_file}" 除非 ruby_file =~/spec\/fixtures/结尾目录['templates/**/*.erb'].each do |template|sh "erb -P -x -T '-' #{template} | ruby -c"结尾结尾
4.从模块根目录运行 bundle exec rake spec
I setup a new puppet demo
module with the following Gemfile
and it worked as expected when I ran a simple puppet-rspec test.
Gemfile
source 'https://rubygems.org'
if puppetversion = ENV['PUPPET_GEM_VERSION']
gem 'puppet', puppetversion, :require => false
else
gem 'puppet', '3.7.5'
end
gem 'metadata-json-lint'
gem 'puppetlabs_spec_helper', '>= 0.1.0'
gem 'puppet-lint', '>= 1.0.0'
gem 'facter', '>= 1.7.0'
gem 'rspec-puppet-facts'
# rspec must be v2 for ruby 1.8.7
if RUBY_VERSION >= '1.8.7' and RUBY_VERSION < '1.9'
gem 'rspec', '~> 2.0'
end
However when I cloned an existing module (silex
) and updated the Gemfile to look like the above and I run:
bundle exec rake spec
I get the following error:
vagrant$ bundle exec rake spec
/usr/bin/ruby -S rspec spec/classes/init_spec.rb --color
F
Failures:
1) silex with defaults for all parameters should contain Class[silex]
Failure/Error: it { should contain_class('silex') }
Puppet::Error:
Could not find class silex for centos65-box-1 on node centos65-box-1
# ./spec/classes/init_spec.rb:5
Finished in 0.31963 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/classes/init_spec.rb:5 # silex with defaults for all parameters should contain Class[silex]
The class exists and I have already done a puppet run. Can someone assist me understanding why this would be the case?
More information
# Tree
.
├── files
│ └── httpd.patch
├── Gemfile
├── Gemfile.lock
├── lib
│ └── facter
│ └── vendor_dir.rb
├── manifests
│ ├── backup.pp
│ ├── config.pp
│ ├── init.pp
│ ├── install.pp
│ ├── params.pp
│ └── service.pp
├── metadata.json
├── Rakefile
├── README.md
├── spec
│ ├── classes
│ │ └── init_spec.rb
│ ├── fixtures
│ │ ├── manifests
│ │ │ └── site.pp
│ │ └── modules
│ └── spec_helper.rb
├── templates
│ └── var
│ └── config
│ └── settings.json.erb
└── tests
└── init.pp
# spec_helper.rb
require 'puppetlabs_spec_helper/module_spec_helper'
require 'rspec-puppet-facts'
include RspecPuppetFacts
#init_spec.rb
require 'spec_helper'
describe 'silex' do
context 'with defaults for all parameters' do
it { should contain_class('silex') }
end
end
So it turns out I was missing the module
in the fixtures
dir causing the could not find class
error.
Immediate Solution
I had to run rspec-puppet-init
which updates my spec
dir including the spec_helper.rb
file to the following:
require 'rspec-puppet' 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.environmentpath = File.join(Dir.pwd, 'spec') end
Detailed Solution
Here are all the steps I took to get this to work. I would love to get some feed back on if this is the best approach:
1. Add to Gemfile
and run:
#You may need to tweak the versions depending on what your environment is like. bundle install
Gemfile:
source 'https://rubygems.org'
if puppetversion = ENV['PUPPET_GEM_VERSION']
gem 'puppet', puppetversion, :require => false
else
gem 'puppet', '3.7.5'
end
gem 'metadata-json-lint'
gem 'puppetlabs_spec_helper', '>= 0.1.0'
gem 'puppet-lint', '>= 1.0.0'
gem 'facter', '>= 1.7.0'
gem 'rspec-puppet-facts'
# rspec must be v2 for ruby 1.8.7
if RUBY_VERSION >= '1.8.7' and RUBY_VERSION < '1.9'
gem 'rspec', '~> 2.0'
end
2. Initialize your spec
dir. Run from module root dir.
bundle exec rspec-puppet-init
3. I had to update the auto generated Rakefile
to the following:
require 'rubygems'
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
4. Run bundle exec rake spec
from module root dir
这篇关于为什么我会得到 puppet-rspec '类不存在'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!