hiera-puppet-helper: unexpected behaviour when testing conditions

99 views
Skip to first unread message

Brian Warsing

unread,
Feb 24, 2014, 11:48:44 AM2/24/14
to puppe...@googlegroups.com
Hi,

I hope this on-topic. Let me know if I should post this somewhere more appropriate.

I'm using rspec-puppet, puppetlabs_spec_helper, and a fork of hiera-puppet-helper* while developing a new module for puppet 3.4 and ran into an issue I don't understand.

I have posted a mock-up of the module on Github, here...

Admittedly, the module uses an unconventional pattern, but I think it should work.

# init.pp
class foo {
  $bar_options = is_hash(hiera('foo::bar::options', false))
  if $bar_options == true {
    contain foo::bar
  }
}

# bar.pp
class foo::bar ($options) {
  validate_hash($options)
}

Basically, I want to contain the class 'foo::bar' if hiera supplies a hash for 'foo::bar::options'. If not, we simply compile without it.

In my spec for the foo class, I have two contextual tests:

# spec/classes/foo_spec.rb
describe 'foo', :type => 'class' do
  
  context "when there are no BAR options set" do
    it { should_not contain_class("foo::bar") }
  end
  
  context "when BAR configuration options are set" do
    hash = { 'some_key' => "Some Value" }
    let :hiera_data do
      {
        'foo::bar::options' => hash
      }
    end
    it { should contain_class('foo::bar') }
  end
  
end

I would expect that both these tests pass, but the second test always fails.

However, if I remove the first contextual test and just do:

# spec/classes/foo_spec.rb
describe 'foo', :type => 'class' do
  
  context "when BAR configuration options are set" do
    hash = { 'some_key' => "Some Value" }
    let :hiera_data do
      {
        'foo::bar::options' => hash
      }
    end
    it { should contain_class('foo::bar') }
  end
  
end

The test passes. And, as you might guess, if I change :hiera_data to something other than a hash, the same test fails:

# spec/classes/foo_spec.rb
describe 'foo', :type => 'class' do
  
  context "when BAR configuration options are set" do
    hash = false
    let :hiera_data do
      {
        'foo::bar::options' => hash
      }
    end
    it { should contain_class('foo::bar') }
  end
  
end
# This fails, as it should.

It is only when I test both contexts that ONE of them fails -- the second one always fails.

Is this a bug or am I missing something more fundamental? 

Thanks,

--
B.

*Source for hiera-puppet-helper is: "git://github.com/mmz-srf/hiera-puppet-helper.git" which patches this gem for use with Puppet >= 3.4.0

Daniele Sluijters

unread,
Mar 3, 2014, 2:58:17 PM3/3/14
to puppe...@googlegroups.com
Hi,

First of all, you don't need ", :type => 'class'" since it's in spec/classes.

I had a look at the README and the following stood out:
> If you plan to use multiple hiera_config contexts or assign different values to the hiera_data variable, then you must put the tests in different files.

The way I'm interpreting this is that you can only set hiera_data once at the top just after the opening describe block. Since you're not, every hiera() function call will do what it's defaults do. Trying to override it in context's by re-setting hiera_data doesn't work.

I have no experience whatsoever with hiera-puppet-helper though so I might be getting this all wrong.

-- 
Daniele Sluijters

Brian Warsing

unread,
Mar 4, 2014, 10:36:00 AM3/4/14
to puppe...@googlegroups.com
Thanks, Daniele. I think you are correct -- I missed that in the README. 
Reply all
Reply to author
Forward
0 new messages