Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to write some rspec tests for my modules. Most of them now use hiera.

I have a .fixures.yml:

fixtures:
  repositories:
    stdlib: https://github.com/puppetlabs/puppetlabs-stdlib.git
    hiera-puppet: https://github.com/puppetlabs/hiera-puppet.git
  symlinks:
    mongodb: "#{source_dir}"    

and a spec/classes/mongodb_spec.rb:

require 'spec_helper'

describe 'mongodb', :type => 'class' do

  context "On an Ubuntu install, admin and single user" do
    let :facts do
      {
        :osfamily => 'Debian',
        :operatingsystem => 'Ubuntu',
        :operatingsystemrelease => '12.04'
      }
    end

    it {
      should contain_user('XXXX').with( { 'uid' => '***' } )
      should contain_group('XXXX').with( { 'gid' => '***' } )
      should contain_package('mongodb').with( { 'name' => 'mongodb' } )
      should contain_service('mongodb').with( { 'name' => 'mongodb' } )
    }
  end
end

but when I run the spec test, I get:

# rake spec
/usr/bin/ruby1.8 -S rspec spec/classes/mongodb_spec.rb --color
F

Failures:

  1) mongodb On an Ubuntu install, admin and single user
     Failure/Error: should contain_user('XXXX').with( { 'uid' => '***' } )
     LoadError:
       no such file to load -- hiera_puppet
     # ./spec/fixtures/modules/hiera-puppet/lib/puppet/parser/functions/hiera.rb:3:in `function_hiera'
     # ./spec/classes/mongodb_spec.rb:15

Finished in 0.05415 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/classes/mongodb_spec.rb:14 # mongodb On an Ubuntu install, admin and single user
rake aborted!
/usr/bin/ruby1.8 -S rspec spec/classes/mongodb_spec.rb --color failed

Tasks: TOP => spec_standalone
(See full trace by running task with --trace)

Module spec testing is relatively new, as is hiera. So far I have been unable to find any suitable solutions. (the back and forth on puppet-dev was interesting, but not helpful).

What changes do I need to make to get this to work? Installing puppet from a gem and hacking on rubylib isn't a viable solution due to corporate policy.

I am using Ubuntu 12.04 LTS + Puppet 2.7.17 + hiera 0.3.0.

share|improve this question
add comment

4 Answers

In case anyone else finds this, have a look at this sample repo for a way to mock the hiera function, if that's what you're looking for!

https://github.com/TomPoulton/rspec-puppet-unit-testing#mocking-hiera

share|improve this answer
add comment

The rspec-hiera-puppet gem

https://github.com/amfranz/rspec-hiera-puppet

In your Gemfile:

gem 'puppet'

gem 'rspec-puppet'
gem 'rspec-hiera-puppet'
gem 'puppetlabs_spec_helper'

gem 'hiera'
gem 'hiera-puppet'

In your spec_helper.rb: `require 'rspec-hiera-puppet'

In spec/shared_context.rb:

require 'rspec-hiera-puppet'

shared_context "hieradata" do
  let :hiera_config do
    {
     # this specifies that rspec overrides what's been defined in `riak::params`
     :backends => ['rspec', 'puppet'],
     :hierarchy => ['%{location}', '%{environment}', '%{calling_module}'],
     :puppet   => { :datasource => 'params' },
     :rspec    => respond_to?(:hiera_data) ? send(:hiera_data) : {}
    }   
  end 
end

I'm using it here:

https://github.com/haf/puppet-riak

Feel free to have a look.

share|improve this answer
add comment
up vote 0 down vote accepted

Officially, hiera is not supported for puppetlabs_spec_helper with 2.7, and will be available in a future, 3.x compatible release.

Unofficially, the Puppet Developers mailing list told me there is a "hackish" patch you can apply to puppetlabs_spec_helper that will allow spec tests to be written with hiera support. The downside is, it's likely to break / require some rewrites and such when 3.0 finally comes out.

share|improve this answer
add comment

Unfortunately the puppetlabs_spec_helper doesn't support hiera at the moment. I'm hopeful there will be a good solution either from somebody in Puppet or somebody in the community sooner than later. I know lots of people use Hiera, and I really want them to be able to use the tools we have for testing modules.

share|improve this answer
    
I have found a potential workaround from the puppet-users mailing list. I will give an update once I have had a chance to test it, but it looks promising. –  Lee Lowder Jul 20 '12 at 13:47
    
the workaround did not work. However, since this is something provided by puppetlabs, and puppet 3 will have hiera tightly integrated into it, I have a hard time believing it can't be done. I suspect it is a configuration issue, but I don't know for sure. –  Lee Lowder Jul 30 '12 at 18:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.