testingrspecpuppetrspec-puppet

Puppet: rake spec could not find class ::splunk


I'm trying to tests my puppet modules, but I get an error message that it can't find class.

I've written a couple of internally used Puppet modules; now that I have my head around puppet, I want to write tests. Since I ran into issues, I decided to essentially start over with a new clean module to figure out what I need to do.

$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

Checking the files though I think they contain what they should:

manifests/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

The Could not find class ::splunk is the part that has me confused.

The way I read the spec file is it looking inside init for class{'splunk': ... } That could be me misreading it as it seems odd they would start with a broken spec file, but just in case I tried testing for package instead.

manifests/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

I still get a similar error message:

$ 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]

Sorry to ask such a basic question; I really appreciate your help. I'd really love links to good tutorials on testing Puppet code; I'd love to use Vagrant to have some confidence my code is actually going to do what I want it to.

Update: I believe I have followed @Peter_Souter's suggestions; I still get the error that it can't find the class. I'm posting the contents of the files referenced to try and validate I did the right thing and in case there are other errors.

$ 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

Solution

  • The way that rspec-puppet simulates the catalog for testing, it makes a fixtures directory with the required modules in it. So right now it's running it on an empty directory so it can't find your module. We generally fix that with the puppetlabs-spec-helper which makes a simlink to your current repository in that fixtures directory.

    I'd recommend adding the following into your files:

    # .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
    

    Can you try that and tell me if it works for you?