Search code examples
rspectddpuppethierarspec-puppet

How do I override hiera_data in rspec-puppet?


Lets say I have the following tests:

context 'test' do
  let(:hiera_data) { { :number => '2' } }

  it { should have_module__define_resource_count(2) }
end

context 'test2' do
  let(:hiera_data) { { :number => '10' } }

  it { should have_module__define_resource_count(10) }
end

The first test passes, but when the second test is ran it fails, because the hiera variable number is still 2.

It seems that let(:hiera_data) is unable to override the previous declared variable.

According to this readme it should work if the hiera data is set in different files, but it does not work.

How do I test hiera multiple times in one specfile?


Solution

  • I ran into this same issue, and was stumped for a while. Eventually I found out that the hiera information is. You can fix it like so:

    let(:facts) {{ :cache_bust => Time.now }}
    

    So for example:

    Puppet code:

    class example::bar {
      notify { 'bar': message => hiera('bar_message') }
    }
    

    rspec-puppet code:

    describe "example::bar" do
    
      let(:facts) {{ :cache_bust => Time.now }}
    
      describe "first in-line hiera_data test with a" do
        let(:hiera_data) { { :bar_message => "a" } }
        it { should contain_notify("bar").with_message("a") }
      end
    
      describe "second in-line hiera_data test with b" do
        let(:hiera_data) { { :bar_message => "b" } }
        it { should contain_notify("bar").with_message("b") }
      end
    end
    

    This works! I have a PR to add it to the docs. The botfish fork is the most recently maintained fork of hiera-puppet-helper (see the note on the original)