can you override paramterized class variables in rspec-puppet?

1.2k Views Asked by At

I am retrofitting unit tests to an existing module (not authored by myself). I can't seem to override the params class undef value to a hash value.

the params class has the following (excerpt only):

class myclass::params {
 splunk = undef,
 ...
}

Main class (excerpt only):

class myclass(
  $splunk = $myclass::params::splunk,
  ...
) inherits ::myclass::params
{...}

And the following config class (excerpt only):

class mylcass::config inherits myclass{

  if  $myclass::splunk['install']['package_manage'] {
    file { "somefile.conf":
     ensure  => file,
     mode    => '0444',
     source  => 'puppet:///modules/myclass/splunk/somefile.conf',
}
   ...
  }
}

In config_spec.rb file I have the following:

require 'spec_helper'
require 'shared_contexts'

describe 'myclass::splunk::config' do

  hiera = Hiera.new(:config => 'spec/fixtures/hiera/hiera.yaml')
  splunk = hiera.lookup('splunk',nil,nil)

  let(:params) do
    {
    }
  end

  it do
    is_expected.to contain_file('somefile.conf')
      .with(
        'ensure' => 'file',
        'mode'   => '0444',
        'source' => 'puppet:///modules/myclass/splunk/somefile.conf'
      )
  end

end

I plugged the hiera lookup for splunk into the myclass_spec.rb file in the hope it will override:

require 'spec_helper'
require 'shared_contexts'

describe 'myclass' do

  hiera = Hiera.new(:config => 'spec/fixtures/hiera/hiera.yaml')
  splunk = hiera.lookup('splunk',nil,nil)

let(:params) do
    {
      :splunk => splunk
      ...
    }

But I continue to get the following error:

myclass::splunk is not a hash or array when accessing it

How do I override the splunk variable?

2

There are 2 best solutions below

0
On

A wild guess, but very likely your problem will be fixed by adding this to your describe:

let(:pre_condition) { "class { 'myclass': splunk => { install => { package_manage => true } } }" }

It will cause the myclass::splunk parameter to be set.

1
On

$::myclass::params::splunk is an ordinary class variable, not a class parameter. Hiera is not involved in assigning its value. You cannot change or override its assigned value in Puppet DSL, and as far as I know, not in rspec-puppet, either.

If what you're ultimately after is to test different values of class parameter$::myclass::splunk, then you should inject values for that parameter into your Hiera data. You do not need to test different values of $::myclass::params::splunk, because the only way you can get a different value of that variable is if the code of class myclass::params is modified.