rspec - 为什么我会得到 puppet-rspec 'class does not exist' ?

标签 rspec puppet rspec-puppet

我使用以下 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]

该类已存在,并且我已经完成了 puppet 运行。有人可以帮助我理解为什么会出现这种情况吗?

更多信息

    # 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,导致无法找到类错误。

立即解决

我必须运行 rspec-puppet-init ,它将我的 spec 目录(包括 spec_helper.rb 文件)更新为以下内容:

    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。初始化您的 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

4。从模块根目录运行 bundle exec rake spec

关于rspec - 为什么我会得到 puppet-rspec 'class does not exist' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31097453/

相关文章:

ruby-on-rails - 使用 rspec 测试具有子域约束的​​路由

ruby - Puppet 中的字符串替换?

puppet - 如何使用 rspec 在 puppet 中模拟 puppet validate_cmd?

rspec - 我如何模拟事实来测试对事实进行十进制乘法的 Puppet 模板?

ruby - 使用 rspec 进行 Puppet 代码覆盖率测试

ruby-on-rails - 无效的 gemspec - 格式错误的要求 ["#<YAML::Syck::DefaultKey:0xb5f9c990> 3.2.0"]

mysql - rspec 测试失败 b/c application_controller 正在进行数据库调用

ruby-on-rails - RSpec - 如何创建可用于自动嵌入 "it"测试的测试的辅助方法

vagrant - 如何在 Puppet 配置的 Vagrant 项目中跨环境共享 list

regex - 如何在 puppet 上使用参数化正则表达式(例如/${user}/)进行测试?