rspec and hiera lookup within puppet functions

755 views
Skip to first unread message

Remi Ferrand

unread,
Jan 30, 2015, 7:23:44 AM1/30/15
to puppe...@googlegroups.com
Hi list,

I've written a custom puppet function that I'd like to test using rspec

This function (is useless, I know :-)) only does a hiera lookup of variable name 'variable'.
Thus, the following two puppet codes:

# code 1
$value = hiera('variable')

# code 2
$value = test_hiera()

should be equivalent.

So far, no problem.
Then comes rspec testing of my function 'test_hiera()'.

This mean:
I've already been using the method above for testing puppet code that involves hiera lookups, and it worked as expected.

What I don't want is to stub or mock hiera function.
I just want that the hiera() lookup actually uses my custom hierarchy specified in my configuration file (https://github.com/riton/rspec-hiera-test-function/blob/master/spec/fixtures/hiera/hiera.yaml)

As described in my sample repository README (https://github.com/riton/rspec-hiera-test-function/blob/master/README.md), right now I wasn't able to make it work.

It seems that hiera is still using it's default configuration file.
A strace of the following command:

$ strace bundle exec rake spec SPEC_OPTS='--color --format documentation --tag focus'

reveals that /var/lib/hiera/common.yaml is searched for.

My test environment is:

  • CentOS 6
  • ruby 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]
  • Bundler version 1.7.12

My bundle is:
  • bundler (1.7.12)
  • diff-lcs (1.2.5)
  • facter (1.7.6)
  • hiera (1.3.4)
  • json_pure (1.8.2)
  • metaclass (0.0.4)
  • mocha (1.1.0)
  • puppet (3.6.2)
  • puppet-lint (1.1.0)
  • puppet-syntax (1.4.1)
  • puppetlabs_spec_helper (0.8.2)
  • rake (10.4.2)
  • rgen (0.6.6)
  • rspec (2.99.0)
  • rspec-core (2.99.2)
  • rspec-expectations (2.99.2)
  • rspec-mocks (2.99.3)
  • rspec-puppet (2.0.0 8459e14)

If anyone has already succeeded testing custom functions that internally uses hiera lookup, I'll be glad to learn how.

I've tested everything I can for now and I'm quite out of ideas.

Thanks in advance

Rémi

Nan Liu

unread,
Jan 30, 2015, 11:17:49 AM1/30/15
to puppet-dev

Remi Ferrand

unread,
Jan 31, 2015, 1:16:56 PM1/31/15
to puppe...@googlegroups.com
Hi Nan,

Thanks for your answer,


On Friday, January 30, 2015 at 5:17:49 PM UTC+1, Nan Liu wrote:

Try using one of the following gems:

https://github.com/Accuity/rspec-puppet-utils

I already know this Gem and I've used it for stubbing functions.
I don"t really see how it could help me for my current problem ...
 

This Gem isn't supposed to be required anylonger because rspec-puppet officially supports hiera lookups in a few steps.

Still, since nothing worked for me till now, I've gave it a try and the results could be found here :: https://github.com/riton/rspec-hiera-test-function/tree/hiera-puppet-helper.

I've added a dummy puppet class that just does a standard hiera lookup. This test ensures me that standard hiera lookups work fine from puppet classes.
They're still failing from a custom puppet function tested with the same "shared_context".
 
I'm still stuck since no method seem to work and enable hiera lookups from a custom puppet function...

Cheers

Rémi

Wil Cooley

unread,
Feb 19, 2015, 3:13:46 PM2/19/15
to puppet-dev group
On Fri, Jan 30, 2015 at 4:23 AM, Remi Ferrand <remi.mathi...@gmail.com> wrote:

This mean:
I've already been using the method above for testing puppet code that involves hiera lookups, and it worked as expected.

What I don't want is to stub or mock hiera function.
I just want that the hiera() lookup actually uses my custom hierarchy specified in my configuration file (https://github.com/riton/rspec-hiera-test-function/blob/master/spec/fixtures/hiera/hiera.yaml)

As described in my sample repository README (https://github.com/riton/rspec-hiera-test-function/blob/master/README.md), right now I wasn't able to make it work.

There are a couple of problems with your test because you're doing what should ordinarily work, rather than slavishly following the docs, forgetting about the layers and layers of magic that is rspec and rspec-puppet:

1. You're using the lower-level function interface directly into Puppet rather than the rspec-puppet function-testing interface, so rspec-puppet magic does not get called, so the setup that rspec-pupppet does for your `c.hiera_config = ...` never happens. Instead, try:

context 'Using rspec-puppet wrapper' do                                        
  it 'should work' do                                                          
    expect(subject).to run.with_params().and_return('plop')                    
  end                                                                          
end                                                                           

2. Once you've worked that out, you find that you get an undefined method error:

Failure/Error: expect(subject).to run.with_params().and_return('plop')
NoMethodError:
  undefined method `call' for nil:NilClass

The name of the thing under test w/rspec-puppet *must* be the the top-level `describe` argument:

describe 'test_hiera', :focus => true do

This is how rspec-puppet knows what function to make available.

Note that you can still use the `scope.function_test_hiera([])` stuff, as long as you put it after the rspec-puppet-style function call. (If you were still using rspec-puppet 1.0.1, you might want to duplicate the test with this style, because rspec-puppet had a bug in the error handler that made it impossible determine how your method call errored!)

For me, one of the most frustrating aspects of rspec and rspec-puppet is the amount of magic that goes into making the whole thing seem like a DSL rather than an API. For example, try changing 'plop' in the test to see it fail and then run rspec with the --backtrace option -- you'll find no mention of rspec-puppet at all! And don't bother trying to use ruby -rtracer either -- I just ran it with your simple test and got a 170MiB file -- 750,000 lines long.

Sorry for the rant.

Wil

Javier Palacios

unread,
Feb 20, 2015, 5:17:23 AM2/20/15
to puppe...@googlegroups.com
Hello Remi,
 
I've had partial success by making some changes on rspec-puppet, targeting a function similar to the params_lookup one on puppi module from example42. You can have a look at branches feature/scoped_resources & feature/parent_module_name from my fork (https://github.com/javiplx/rspec-puppet).


--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-dev/59a73336-4f64-4c35-9a74-7288dc773dcd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages