Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The merging into the rspec-puppet project of PR#125 and PR#134 in December 2013 replaces the need for the rspec-hiera-puppet module to support testing of hiera data in rspec-puppet tests.

However I found that the documentation of the hiera use in the rspec-puppet README.md to be buggy.
In the end, this was the invocation which permitted my tests to pass:

# spec/spec_helper.rb 
require 'rspec-puppet'
require 'hiera'

and then in the tests themselves, rather than using let(:param) for setting hiera data, I found that this worked better:

# spec/classes/init_spec.rb
require 'spec_helper'

describe 'server_init' do
  # include_context "hieradata"

  let(:hiera_config) { 'spec/fixtures/hieradata/hiera.yaml' }
  hiera = Hiera.new({ :config => 'spec/fixtures/hieradata/hiera.yaml' })

  context 'with defaults for all parameters' do
    sensu_manage_user = hiera.lookup('sensu::manage_user', nil, nil)
    ntp_autoupdate = hiera.lookup('ntp::autoupdate', nil, nil)

    let(:hiera_data) { {
      :'sensu::manage_user' => sensu_manage_user,
      :'ntp::autoupdate'    => ntp_autoupdate,
    } }

  etc.

supported by this:

# spec/fixtures/hieradata/hiera.yaml 
---
:backends:
  - yaml
:hierarchy:
  - test
:yaml:
  :datadir: 'spec/fixtures/hieradata'

And hiera data following the usual format at: spec/fixtures/hieradata/test.yaml.
Trust this post might save me those two hours sorting this out the next time I need this.

-- Hugh

The merging into the rspec-puppet project of PR#125 and PR#134 in December 2013 replaces the need for the rspec-hiera-puppet module to support testing of hiera data in rspec-puppet tests.

However I found that the documentation of the hiera use in the rspec-puppet README.md to be buggy.
In the end, this was the invocation which permitted my tests to pass:

# spec/spec_helper.rb 
require 'rspec-puppet'
require 'hiera'

and then in the tests themselves, rather than using let(:param) for setting hiera data, I found that this worked better:

# spec/classes/init_spec.rb
require 'spec_helper'

describe 'server_init' do
  # include_context "hieradata"

  let(:hiera_config) { 'spec/fixtures/hieradata/hiera.yaml' }
  hiera = Hiera.new({ :config => 'spec/fixtures/hieradata/hiera.yaml' })

  context 'with defaults for all parameters' do
    sensu_manage_user = hiera.lookup('sensu::manage_user', nil, nil)
    ntp_autoupdate = hiera.lookup('ntp::autoupdate', nil, nil)

    let(:hiera_data) { {
      :'sensu::manage_user' => sensu_manage_user,
      :'ntp::autoupdate'    => ntp_autoupdate,
    } }

  etc.

supported by this:

# spec/fixtures/hieradata/hiera.yaml 
---
:backends:
  - yaml
:hierarchy:
  - test
:yaml:
  :datadir: 'spec/fixtures/hieradata'

And hiera data following the usual format at: spec/fixtures/hieradata/test.yaml.
Trust this post might save me those two hours sorting this out the next time I need this.

-- Hugh