rspec-puppet and local variable values

810 views
Skip to first unread message

Trevor Vaughan

unread,
Mar 28, 2014, 10:39:59 AM3/28/14
to puppe...@googlegroups.com
Hopefully I didn't just overlook this in the docs but, in rspec-puppet, how would you go about getting the value of $bar from the following?

class foo {
  $bar = interesting_function('baz')
}

I've inspected everything that I think is obvious and I don't want to try and stub what 'interesting_function' should be doing since it's dynamic.

Any ideas?

Thanks,

Trevor

--
Trevor Vaughan
Vice President, Onyx Point, Inc
(410) 541-6699
tvau...@onyxpoint.com

-- This account not approved for unencrypted proprietary information --

Johan De Wit

unread,
Mar 28, 2014, 11:26:03 AM3/28/14
to puppe...@googlegroups.com
I stumbled against this text :

you can write a mock function my_mock_function.rb and put it under

{module name}/spec / fixtures / modules / {module name}/ lib / puppet / parser /

If you put the real function(s) so there ?
--
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/CANs%2BFoVY%3DCtqRSjbOQYN-j%2B9PCkfYbXnGW-9qPOf1R6ZB%3DtNSA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


-- 
Johan De Wit

Open Source Consultant

Red Hat Certified Engineer              (805008667232363)
Puppet Certified Professional 2013/2014 (PCP0000006)
_________________________________________________________
 
Open-Future                 Phone     +32 (0)2/255 70 70
Zavelstraat 72              Fax       +32 (0)2/255 70 71
3071 KORTENBERG             Mobile    +32 (0)474/42 40 73
BELGIUM                     http://www.open-future.be
_________________________________________________________
 

Upcoming Events:

Puppet Introduction Course | http://www.open-future.be/puppet-introduction-course-14th-april

Puppet Advanced Training | http://www.open-future.be/puppet-advanced-training-15-till-17th-april

Linux Training | https://www.open-future.be/linux-training-5-till-9th-may

Puppet Introduction Course | https://www.open-future.be/puppet-introduction-course-12th-may

Subscribe to our newsletter: http://eepurl.com/BUG8H


Trevor Vaughan

unread,
Mar 28, 2014, 12:15:11 PM3/28/14
to puppe...@googlegroups.com
Hmm...so you're saying that I would mock the variable result with the real function to get the real result?

That's doesn't make me super happy but if it works, I'll take it!

Trevor



For more options, visit https://groups.google.com/d/optout.



--

James Sweeny

unread,
Mar 28, 2014, 12:23:31 PM3/28/14
to puppe...@googlegroups.com
I could be way off here, but shouldn't you be testing the resource $bar ends up in, rather than the value of $bar itself?



For more options, visit https://groups.google.com/d/optout.



--

James Sweeny

Professional Services
http://puppetlabs.com/

Join us at PuppetConf 2014, September 23-24 in San Francisco - http://2014.puppetconf.com

Trevor Vaughan

unread,
Mar 28, 2014, 1:53:43 PM3/28/14
to puppe...@googlegroups.com
Yep, but I need the value of $bar to figure out what the later resource name is!

A better example:


class foo {
  $bar = interesting_function('baz')

  file { "/some/path/$bar":
    content => 'whatever'
  }
}

So, given that I don't necessarily know what interesting_function is going to generate, I need to actually run it to get the result.

Thanks,

Trevor



For more options, visit https://groups.google.com/d/optout.

Johan De Wit

unread,
Mar 31, 2014, 2:07:21 AM3/31/14
to puppe...@googlegroups.com
On 28/03/14 18:53, Trevor Vaughan wrote:
Yep, but I need the value of $bar to figure out what the later resource name is!

A better example:

class foo {
  $bar = interesting_function('baz')

  file { "/some/path/$bar":
    content => 'whatever'
  }
}

Aren't we mixing some things here ? 

IMHO, what should be tested is that, given $bar a 'value', no matter what the source of that value is (be it generates using a function, provided by hiera or given as parameter during classification).

In your spec file, just assign a 'value' to $bar, and just test that value is the resource


let(:param) { { :bar => 'somevalue' } }

it should contain_file('/some/path/somevalue).with_content(/whatever/)

Thats about your test for your class.


The second test you could do, is to test your function with rspec :

it { should run.with_params('foo').and_return('somevalue') }






For more options, visit https://groups.google.com/d/optout.

Trevor Vaughan

unread,
Mar 31, 2014, 8:36:17 AM3/31/14
to puppe...@googlegroups.com
How would you modify non-param variables?

Basically, this is a temporary variable that should be generated and not used elsewhere.

I'm thinking that a mock is the only real way to solve this.

Thanks,

Trevor



For more options, visit https://groups.google.com/d/optout.

Corey Osman

unread,
Mar 27, 2015, 2:39:00 PM3/27/15
to puppe...@googlegroups.com
Coming from regular language testing your first thought with puppet is how do I control the flow of code when testing.  In normal languages you can mock the functions, getters, setters and possibly instance variables.  With puppet its a bit different.

You can control the flow of puppet code only by influencing puppet facts and class / define parameters, and a few other things.   You can also mock functions as well but generally you don't need to.

Since the rspec-puppet subject is always the compiled catalog, everything your testing against must be or not be in the catalog.  Rspec-puppet compiles that catalog and each test you make checks that the end result of the catalog compilation with your influenced facts and parameters.  Don't be concerned with checking variable content explicitly, since all variables will be present in some other resource or template that you can test against.  And yes you can test the rendered template for specific content.

What this usually means is that given a conditional block you need to test against the resource that contains your variable with a negative and positive test.

Example puppet code:

if $::osfamily == 'RedHat' {
   notify{"Hello from Conditional block":}

}

## Test code

describe :RedHat do
   let(:facts) {{ :osfamily => 'RedHat'}}
   it { should contain_notify('Hello from Conditional block') }

end
describe :Windows do
   let(:facts) {{ :osfamily => 'Windows'}}
   it { should_not contain_notify('Hello from Conditional block') }

end


Remember that your testing against the compiled catalog which either should or should_not contain the resource.

Now if you just want to see what is inside your variable you can use the dump_args function to output the variable, write a notify resource or use the inline template function.  I wrote this a while ago to making dumping variables to STDOUT easy.


dump_args($var1, $var2, $var3, ...)

Also for mocking functions I would recommend using the following gem.  https://github.com/Accuity/rspec-puppet-utils

Lastly, if you want your module to be setup automatically for testing, in addition to automated test generation, you should check this out.  https://github.com/logicminds/puppet-retrospec


Corey

Trevor Vaughan

unread,
Mar 27, 2015, 3:00:22 PM3/27/15
to puppe...@googlegroups.com
Wow, thread resurrection!

I figured out how to solve my problem but thanks for the pointers to the additional tools, quite useful.

I also ended up dumping variables either through Pry or through a function.

The issue that I was having is that I have some functions that modify items on the filesystem itself and needed to work around that. I ended up refactoring the functions to make everything properly variable which solved the issue for temporary testing scenarios.

Thanks,

Trevor

--
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.

For more options, visit https://groups.google.com/d/optout.

Corey Osman

unread,
Mar 27, 2015, 5:05:08 PM3/27/15
to puppe...@googlegroups.com
I didn't even realize this thread was old.  Oops.  I saw it come across email but I guess its how my email reader works.
Reply all
Reply to author
Forward
0 new messages