0
  • Using Puppet 3
  • Testing using rspec-puppet
  • Iterating over an array of hashes using a Defined Type
  • Getting an Error, telling me that my parameter (which defaults to the value of $title) cannot be accessed the way I am because it is not an Array or Hash

I'm using old-style iteration in a puppet module, creating a defined type to iterate over an array of hashes. I'm trying to write a test for this define in rspec-puppet, attempting to assign a hash to the :title using let(). The $title is then supposed to be set to my variable called $daemon, yet my tests keep throwing errors saying that $daemon is not a hash or array.

Here's how I'm creating my defined type:

define my_module::daemon_install ($daemon = $title) {

  package {"${daemon['package_name']}":
    ensure => "${daemon['package_version']}",
  }

  file {"${some_fact}/${daemon['binary']}.conf":
    ensure => file,
    content => "blah"
    notify => Service["${daemon['name']}"],
  }

  service {"${daemon['name']}":
    ensure => running,
    enable => true,
  }
}

And here's how I'm trying to set the title:

describe 'my_module::daemon_install' do

  context 'with foo' do

    let(:title) {
      {
        "name" => "foo",
        "package_name" => "bar",
        "package_version" => "1.0.1",
        "binary" => "food",
      }
    }

    # ...

  end
end

And here's the error:

daemon is not a hash or array when accessing it with package_version

I'm actually abit new to using defined types for iteration, and very new at rspec-puppet, so I'm not sure if I'm missing something obvious here or not.

But why is it only complaining about package_version and not package_name? And more importantly: why is it not a hash, when (I believe) I'm setting it to a hash correctly in the spec file.

I should mention that another test, of a class which uses this defined type, completes successfully. So it seems related to how I'm trying to set the title when directly testing the define, if I were to guess.

CRThaze
  • 544
  • 3
  • 16

1 Answers1

1

Rspec always converts title into String.

Use $name in define() instead of $title and add the following into tests:

let :title do
  { ... }
end

let :params do
  { :name => title }
end

Please note$name should be equal of $title.

Garf365
  • 3,619
  • 5
  • 29
  • 41