3

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?

kaizenCoder
  • 2,211
  • 6
  • 33
  • 64
  • Your examples are not lining up. You test is for `myclass::splunk::config` while you are showing `mylcass::config`. In general when approaching such issues try to prune down the example to the smallest possible case that still shows the issue. – David Schmitt Sep 26 '17 at 12:42

2 Answers2

1

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.

David Schmitt
  • 58,259
  • 26
  • 121
  • 165
0

$::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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • thanks, so I did that by adding `myclass::splunk` to my hieradata file. Yet I get the same error. I am also now wondering if there is a fundamental flaw in the way this module is designed because of the inheritance relationship between `myclass` and `myclass::config` - it seems like it is the only way for `myclass::config` to access the `$myclass::splunk` variable which is perhaps not loaded via rspec puppet? – kaizenCoder Dec 05 '15 at 23:22