Why do I get puppet-rspec 'class does not exist' when it does?

前端 未结 1 1741
名媛妹妹
名媛妹妹 2021-01-24 01:16

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

相关标签:
1条回答
  • 2021-01-24 01:42

    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

    0 讨论(0)
提交回复
热议问题