我用下面的Gemfile
设置了一个新的puppet demo
模块,当我运行一个简单的puppet-rspec测试时,它按预期工作。
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
然而,当我克隆了一个现有的模块(silex
)并更新了Gemfile,使其看起来如上所述时,我运行:
bundle exec rake spec
我得到以下错误:
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]
这个班已经存在了,我已经做了一次木偶赛跑。有人能帮助我理解为什么会出现这种情况吗?
更多信息
# 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
所以我在fixtures
目录中丢失了module
,导致了could not find class
错误。
即时解决方案
我必须运行rspec-puppet-init
,它将我的spec
目录(包括spec_helper.rb
文件)更新为以下内容:
需要"rspec木偶"fixture_path=文件扩展路径(文件联接(__File__,'..','fixtures'))RSpec.config do|c|c.module_path=文件.join(fixture_path,'modules')c.manifest_dir=文件.join(fixture_path,'manifest')c.environmentpath=文件.join(Dir.pwd,'spec')终止
详细解决方案
以下是我为实现这一目标所采取的所有步骤。如果这是最好的方法,我很想得到一些反馈:
1.添加到Gemfile
并运行:
#您可能需要根据您的环境来调整版本。捆绑安装
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.初始化您的spec
目录。从模块根目录运行
bundle exec rspec-puppet-init
3.我不得不将自动生成的Rakefile
更新为以下内容:
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