Ask Your Question

how to pass selector value to hiera in rspec-puppet

asked 2016-12-16 06:53:35 -0600

Chutki gravatar image


I have following code in puppet,

class module_name {

   $val = hiera('ONOFF')

      case $val {
        'off'  : {
          service { 'servicename' :
            ensure => stopped,
            enable => false,
        'on'  : {
          service { 'servicename' :
            ensure => running,
            enable => true,
        default: {}

Below is the test case

describe 'module_name' do

  context 'with defaults valuess' do
    let(:facts) {{ :operatingsystemmajrelease => '7', :osfamily => 'RedHat'}}

    it { should contain_class('module_name') }

  context 'test case for  off' do
    let(:facts) {{ :operatingsystemmajrelease => '7', :osfamily => 'RedHat' }}
     let(:hiera_config) { 'spec/fixtures/hiera.yaml' }
     hiera ={ :config => 'spec/fixtures/hiera.yaml' })

     it { should contain_service('servicename').with(
      'ensure' => 'stopped',
      'enable' => 'false'

   context 'test case for  on' do
    let(:facts) {{ :operatingsystemmajrelease => '7', :osfamily => 'RedHat' }}
     let(:hiera_config) { 'spec/fixtures/hiera.yaml' }
     hiera ={ :config => 'spec/fixtures/hiera.yaml' })

     it { should contain_service('servicename').with(
      'ensure' => 'running',
      'enable' => 'true'

ONOFF: 'on'

Getting below error

expected that the catalogue would contain Service[servicename] with ensure set to "stopped" but it is set to "running", and parameter enable set to "false" but it is set to true

How I can write test case to pass both selector "on" and "off".

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2017-04-17 13:47:39 -0600

DevOpsProDude gravatar image

First off, replace hiera() with new lookup() function as hiera() function has been deprecated.

I highly recommend reading how to set up hiera in modules

Your class should look more like

class foo (
) {

  if $enable_bar {
    $_state = running
    $_run_at_boot = true
  } else {
    $_state = stopped
    $_run_at_boot = false

  service { 'bar':
    ensure => $_state,
    enable => $_run_at_boot,



foo::enable_bar: true

In your test, you should no longer need the hiera stuff, but you will need to stub out the enable_bar param for the 'disabled' test

  context 'test case for bar in disabled state' do
    let(:facts) { :operatingsystemmajrelease => '7', :osfamily => 'RedHat' }
    let(:param) { enable_bar: false }

    it { contain_service('bar').with(
        ensure: 'stopped',
        enable: 'false',

Hope that all makes sense

edit flag offensive delete link more

answered 2016-12-16 08:20:54 -0600

DarylW gravatar image

updated 2016-12-16 08:21:03 -0600

This doesn't directly answer your question (how to run rspec tests with two different sets of hiera data), but I think it would be valuable to refactor your module to make it more testable.

I would suggest to change your approach. Sadly, this gitbook is not complete, but it does have a lot of good points on how to design the API of your puppet classes to allow them to be testable

In their case, they recommend that if you are using hiera, it should terminate in the params.pp class (which gets it's variables assigned to class parameters in your init.pp main class), which you could easily override while testing.

That said, I don't know what the testing pattern going forward is with lookup() and data in modules.

edit flag offensive delete link more


Thanks for your help :)

Chutki gravatar imageChutki ( 2016-12-20 01:11:57 -0600 )edit

params.pp is now outdated and should be replaced with data in modules.

DevOpsProDude gravatar imageDevOpsProDude ( 2017-04-17 13:19:39 -0600 )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

1 follower


Asked: 2016-12-16 06:53:35 -0600

Seen: 385 times

Last updated: Dec 16 '16