Ask Your Question
6

What is the recommended method for testing modules that use hiera?

asked 2012-12-18 11:51:32 -0500

jlambert121 gravatar image

updated 2013-04-15 12:31:43 -0500

ramindk gravatar image

I have been writing rspec tests using rspec-puppet and puppetlabs_spec_helper for my modules using this blog post as one point of reference. This post doesn't mention how to test modules that require hiera lookups. Is there an updated recommendation for testing these types of modules?

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted
0

answered 2013-04-15 11:25:16 -0500

jlambert121 gravatar image

Where possible I have defaults in place, but there are a few locations where there is no reasonable default (such as passwords or keys). I finally found a maintained (and updated) version of rspec-hiera-puppet named hiera-puppet-helper. Being able to test through hiera itself also helps ensure more complex data structures get merged as expected/hoped and the 'advanced' setup allows testing of more complex environments.

edit flag offensive delete link more
3

answered 2013-03-09 16:54:20 -0500

Daenney gravatar image

updated 2013-03-09 16:57:07 -0500

I don't think you should test for that. The idea with rspec is that you test your module, so the logic within it and its structure, to see if it will behave as expected.

Where your data comes from is not part of how your module functions, hence it shouldn't be part of your module's tests. Your module should have parameters through which data is passed into your module. Wether that data then comes from Hiera or some other source is in essence irrelevant.

As an example:

class ntp($servers = hiera('ntp_servers') {
  ... do stuff ...
}

What you should ... (more)

edit flag offensive delete link more

Comments

It's pretty common to use dependency injection to test data input in other languages. I could see wanting to test various hashes or arrays to make sure that templates ...(more)

Ancillas gravatar imageAncillas ( 2013-04-15 13:05:57 -0500 )edit

That still doesn't require Hiera. You can easily test various types of arrays and hashes with a template, just create a new context block in your spec and pass ...(more)

Daenney gravatar imageDaenney ( 2013-05-05 04:28:06 -0500 )edit
1

answered 2015-06-13 22:14:16 -0500

hesco gravatar image

updated 2015-06-13 22:34:40 -0500

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

  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

edit flag offensive delete link more

Comments

That helps a lot...do you have a good pattern for testing hiera data in modules yet?

DevOpsProDude gravatar imageDevOpsProDude ( 2017-05-18 22:07:30 -0500 )edit
0

answered 2012-12-18 15:19:30 -0500

llowder gravatar image

updated 2013-01-03 13:29:13 -0500

At present, there isn't a really good way.

If you refer to this puppet-dev mailing list thread, there is a patch to puppetlabs_spec_helper that has worked very well for me while I was using puppet 2.7.

Another possibility is the use of rspec-hiera-puppet, though I have not used it personally and have seen a number of people who have had difficulty getting it to work.

edit flag offensive delete link more

Comments

Why did you obscure the HTTPS schema in the provided links?

joschi gravatar imagejoschi ( 2012-12-20 05:25:27 -0500 )edit

I don't have enough karma to post the link with munging it.

llowder gravatar imagellowder ( 2012-12-20 08:00:24 -0500 )edit

llowder - thanks for your response. Neither of these solutions has worked well or consistently for me. Are you aware of any official puppetlabs method of doing these tests since hiera ...(more)

jlambert121 gravatar imagejlambert121 ( 2012-12-28 20:13:53 -0500 )edit

I've recently updated to puppet 3, but I have not had the cycles to look into this. I should be able to this coming week or so. I will ...(more)

llowder gravatar imagellowder ( 2012-12-31 10:11:50 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

6 followers

Stats

Asked: 2012-12-18 11:51:32 -0500

Seen: 2,745 times

Last updated: Jun 13 '15