Ralph Urban
Ralph Urban

Reputation: 45

How to check the value of a local variable in puppet unit test?

I have the following (simplified) setup:

mymod/manifests/as/myressource.pp:

    define mymod::as::myressource (
  Integer $abc,
) {
  notice('mymod::as::myressource start ...')

  $maxheap3 = '3G'
  notice("maxheap3 = ${maxheap3}")
}

mymod/manifests/init.pp:

    class mymod(
  Optional[String] $maxheap,
) {
  notice("${title} wird installiert...")

  mymod::as::myressource {'no.1':
    abc => 35
  }
  mymod::as::myressource {'no.2':
    abc => 70
  }
}

mymod/spec/classes/sometest.rb:

    describe 'mymod' do
  on_supported_os.each do |os, os_facts|
    context "on #{os}" do
      let(:facts) { os_facts }
      let(:params) { {
      } }
      it { is_expected.to compile }

      it { is_expected.not_to contain_mymod__as__myressource('no.3') }
      it { is_expected.to contain_mymod__as__myressource('no.1').with({
        :abc => 35,
#        :maxheap3 => '3G'
              }) }
    end
  end
end

The test works without errors, but if I uncomment the line with maxheap3 it fails and tells me: "expected that the catalogue would contain Mymod::As::Myressource[no.1] with maxheap3 set to "3G" but it is set to nil"

How comes it, that I can check the value of a parameter in this manner, but not a local variable? What can I do to check the value of $maxheap3 in my test?

Upvotes: 0

Views: 621

Answers (1)

John Bollinger
John Bollinger

Reputation: 180201

How comes it, that I can check the value of a parameter in this manner, but not a local variable?

Unit tests with Rspec puppet test the contents of the catalog. The catalog contains class and resource parameters, but it does not contain information about local variables.

What can I do to check the value of $maxheap3 in my test?

You cannot test local variables, at least not directly, and you should not want to do. They are an implementation detail. You can, however, test their impact on classes and resources that appear in the catalog. That may take the form of how many or which resources of given types are declared, which classes are declared, what values class and resource parameters take, among others.

In your example case, the value of $maxheap3 in your mymod::as::myressource instances has no impact on the catalog at all, so you cannot test it via Rspec tests. But so what? That it has no effect on the catalog means that it does not affect how Puppet configures target nodes, so at the level of Rspec_puppet, it does not matter.

Upvotes: 2

Related Questions