Ask Your Question

Revision history [back]

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 https://docs.puppet.com/puppet/4.9/hiera_migrate_modules.html

Your class should look more like

class foo (
  $enable_bar,
) {

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

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

}

/data/common.yaml

---
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 { 
      is_expected.to contain_service('bar').with(
        ensure: 'stopped',
        enable: 'false',
      )
    }
  end

Hope that all makes sense