Unit testing v4 functions in modules

76 views
Skip to first unread message

Erik Dalén

unread,
Apr 28, 2015, 9:16:23 AM4/28/15
to Puppet Developers
Do any of you have any tips in regards to unit testing function using the v4 API in modules?


But PuppetSpec::Compiler & Matchers::Resource are in the spec/lib directory of puppet which I can't depend on from a module AFAIK.

Is there something equivalent in puppetlabs_spec_helper that works for v4 API functions?



Henrik Lindberg

unread,
Apr 28, 2015, 10:54:09 AM4/28/15
to puppe...@googlegroups.com
There is an updated version of puppet-rspec that will help with this. (I
heard about this yesterday). Hunner knows more.

- henrik

--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Henrik Lindberg

unread,
Apr 28, 2015, 11:00:08 AM4/28/15
to puppe...@googlegroups.com
On 2015-28-04 16:53, Henrik Lindberg wrote:
> On 2015-28-04 15:16, Erik Dalén wrote:
>> Do any of you have any tips in regards to unit testing function using
>> the v4 API in modules?
>>
>> I would like to do something like this:
>> https://github.com/puppetlabs/puppet/blob/master/spec/unit/functions/scanf_spec.rb
>>
>>
>> But PuppetSpec::Compiler & Matchers::Resource are in the spec/lib
>> directory of puppet which I can't depend on from a module AFAIK.
>>
>> Is there something equivalent in puppetlabs_spec_helper that works for
>> v4 API functions?
>>
> There is an updated version of puppet-rspec that will help with this. (I
> heard about this yesterday). Hunner knows more.
>
https://github.com/rodjek/rspec-puppet/commit/a66f9654671f8cd935599209b56eee2768c7c8c1#diff-2374d7eefa56aafa2b25c2914973fecbR14

Erik Dalén

unread,
Apr 29, 2015, 6:49:44 AM4/29/15
to puppe...@googlegroups.com
I'm not getting it to work. Here's a minimal example project with a simple max function: https://github.com/dalen/puppet-v4functiontest

And here is the failing travis build for it:

This is requiring the latest rspec-puppet (2.1.0) and puppet 4.0.0. What am I doing wrong?

--
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/mho73o%24dv8%242%40ger.gmane.org.
For more options, visit https://groups.google.com/d/optout.

Erik Dalén

unread,
Apr 30, 2015, 8:18:25 AM4/30/15
to puppe...@googlegroups.com
I'm not even getting the custom functions to load if I set $RUBYLIB, set libdir or copy them manually to the default libdir (emulating pluginsync). If I build and install this module it works as expected, but at the moment I can't find any good way to test v4 functions without installing them first.

Has anyone verified that pluginsync with v4 functions works at all?

Joshua Hoblitt

unread,
Apr 30, 2015, 12:10:04 PM4/30/15
to puppe...@googlegroups.com
The core v4 function unit tests appear to be using Puppet::Pops::Loaders
to find the function method. E.g.

Puppet.lookup(:loaders).puppet_system_loader.load(:function,
'regsubst').call({}, *args)

From:

https://github.com/puppetlabs/puppet/blob/master/spec/unit/functions/regsubst_spec.rb

-Josh

--
On 04/30/2015 05:17 AM, Erik Dalén wrote:
> I'm not even getting the custom functions to load if I set $RUBYLIB,
> set libdir or copy them manually to the default libdir (emulating
> pluginsync). If I build and install this module it works as expected,
> but at the moment I can't find any good way to test v4 functions
> without installing them first.
>
> Has anyone verified that pluginsync with v4 functions works at all?
>
> On Wed, 29 Apr 2015 at 12:49 Erik Dalén <erik.gus...@gmail.com
> <mailto:erik.gus...@gmail.com>> wrote:
>
> I'm not getting it to work. Here's a minimal example project with
> a simple max function: https://github.com/dalen/puppet-v4functiontest
>
> And here is the failing travis build for it:
> https://travis-ci.org/dalen/puppet-v4functiontest/builds/60517933
>
> This is requiring the latest rspec-puppet (2.1.0) and puppet
> 4.0.0. What am I doing wrong?
>
> On Tue, 28 Apr 2015 at 17:00 Henrik Lindberg
> <henrik....@cloudsmith.com
> <mailto:puppet-dev%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-dev/mho73o%24dv8%242%40ger.gmane.org.
> For more options, visit https://groups.google.com/d/optout.
>
> --
> 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
> <mailto:puppet-dev+...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-dev/CAAAzDLcnh-ha1s72tJwLBUkH%2BZDmnqAAN5uowz9n03R1afeU-A%40mail.gmail.com
> <https://groups.google.com/d/msgid/puppet-dev/CAAAzDLcnh-ha1s72tJwLBUkH%2BZDmnqAAN5uowz9n03R1afeU-A%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Henrik Lindberg

unread,
Apr 30, 2015, 6:55:16 PM4/30/15
to puppe...@googlegroups.com
On 2015-30-04 18:10, Joshua Hoblitt wrote:
> The core v4 function unit tests appear to be using Puppet::Pops::Loaders
> to find the function method. E.g.
>
> Puppet.lookup(:loaders).puppet_system_loader.load(:function,
> 'regsubst').call({}, *args)
>
> From:
>
> https://github.com/puppetlabs/puppet/blob/master/spec/unit/functions/regsubst_spec.rb
>

There is some unused and experimental "gem-loader" code (GemBased
ModuleLoader). The loading of things from the $RUBYLIB path in general
opened a lot of questions about the visibility - are those system
functions (like the ones shipped in puppet with the same privileges,
what are the rules for name-spacing them, if they are in a namespace,
are they in a module, what is the name of that module - the gem they are
in, what if it is not a gem, but just some random files pointed to? How
is the "pointing" done (or should it search all of the paths on the
$RUBYLIB path - which is very expensive) etc. etc. Also, gems are not
available everywhere.

Also note that the 4.x function API does not use ruby "require file", it
explicitly reads and evaluates the code to enable hot-reloading.

So, for the time being 4.x. functions can only be shipped in puppet modules.

I also want to learn more about use cases where it is wanted to ship
functions (or indeed puppet modules as gems).

Regards

- henrik

Erik Dalén

unread,
May 4, 2015, 9:33:46 AM5/4/15
to puppe...@googlegroups.com
On Fri, 1 May 2015 at 00:55 Henrik Lindberg <henrik....@cloudsmith.com> wrote:
On 2015-30-04 18:10, Joshua Hoblitt wrote:
> The core v4 function unit tests appear to be using Puppet::Pops::Loaders
> to find the function method.  E.g.
>
>      Puppet.lookup(:loaders).puppet_system_loader.load(:function,
> 'regsubst').call({}, *args)
>
> From:
>
> https://github.com/puppetlabs/puppet/blob/master/spec/unit/functions/regsubst_spec.rb
>

There is some unused and experimental "gem-loader" code (GemBased
ModuleLoader). The loading of things from the $RUBYLIB path in general
opened a lot of questions about the visibility - are those system
functions (like the ones shipped in puppet with the same privileges,
what are the rules for name-spacing them, if they are in a namespace,
are they in a module, what is the name of that module - the gem they are
in, what if it is not a gem, but just some random files pointed to? How
is the "pointing" done (or should it search all of the paths on the
$RUBYLIB path - which is very expensive) etc. etc. Also, gems are not
available everywhere.

Also note that the 4.x function API does not use ruby "require file", it
explicitly reads and evaluates the code to enable hot-reloading.

So, for the time being 4.x. functions can only be shipped in puppet modules.

I also want to learn more about use cases where it is wanted to ship
functions (or indeed puppet modules as gems).

Well, I was mostly referring to the case where a node runs puppet agent, pluginsyncs some functions and then those should supposedly be available to puppet apply on that node afterwards.

But for testing on the command line in the past I've just set rubylib to point to the function I'm developing and then run puppet apply -e with some test case. That usage seems broken now.
 
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/mhubrn%24u77%241%40ger.gmane.org.

Erik Dalén

unread,
May 4, 2015, 10:07:23 AM5/4/15
to puppe...@googlegroups.com
On Thu, 30 Apr 2015 at 18:10 Joshua Hoblitt <jo...@hoblitt.com> wrote:
The core v4 function unit tests appear to be using Puppet::Pops::Loaders
to find the function method.  E.g.

    Puppet.lookup(:loaders).puppet_system_loader.load(:function,
'regsubst').call({}, *args)

From:

https://github.com/puppetlabs/puppet/blob/master/spec/unit/functions/regsubst_spec.rb

Thanks for the pointer. But copying that into a external module doesn't seem to work: https://travis-ci.org/dalen/puppet-v4functiontest/builds/61150674
 
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/554253D8.1060303%40hoblitt.com.

David Schmitt

unread,
May 7, 2015, 7:16:35 AM5/7/15
to puppe...@googlegroups.com
Hi,


On Tuesday, April 28, 2015 at 3:54:09 PM UTC+1, henrik lindberg wrote:
On 2015-28-04 15:16, Erik Dalén wrote:
> Do any of you have any tips in regards to unit testing function using
> the v4 API in modules?
>
> I would like to do something like this:
> https://github.com/puppetlabs/puppet/blob/master/spec/unit/functions/scanf_spec.rb
>
> But PuppetSpec::Compiler & Matchers::Resource are in the spec/lib
> directory of puppet which I can't depend on from a module AFAIK.
>
> Is there something equivalent in puppetlabs_spec_helper that works for
> v4 API functions?
>
There is an updated version of puppet-rspec that will help with this. (I
heard about this yesterday). Hunner knows more.

rspec-puppet 2.1.0 should have all that work. There are a few more patches and bug fixes in the pipeline, but 2.1.0 should already get you far.

Some examples of rspec-puppet only function testing can be found in the upcoming refresh of stdlib's function specs, currently in my WIP branch here:

https://github.com/DavidS/puppetlabs-stdlib/commits/modules-1882-rspec-puppet-migration



Regards, David
 

Erik Dalén

unread,
May 8, 2015, 7:19:51 AM5/8/15
to puppe...@googlegroups.com
It seems like you only have one function using the v4 API (type_of), and the unit test for that is only run on Puppet 4.0, but the travis matrix doesn't specify puppet 4.0 at all. So the test for that doesn't run. Does it work if you try it locally with Puppet 4.0?


To demonstrate the issue I created a minimal test module (https://github.com/dalen/puppet-v4functiontest), and it still doesn't work with rspec-puppet 2.1.0: https://travis-ci.org/dalen/puppet-v4functiontest/builds/60517933

Do you have any idea what is wrong with that code?

David Schmitt

unread,
May 11, 2015, 8:17:25 AM5/11/15
to puppe...@googlegroups.com
No, but I've been able to reproduce this now locally and I'll have a look at what's going on there.


Regards, David 

David Schmitt

unread,
May 13, 2015, 6:35:59 AM5/13/15
to puppe...@googlegroups.com
Hi Erik, *!

 
> Is there something equivalent in puppetlabs_spec_helper that works for
> v4 API functions?
>
There is an updated version of puppet-rspec that will help with this. (I
heard about this yesterday). Hunner knows more.

rspec-puppet 2.1.0 should have all that work. There are a few more patches and bug fixes in the pipeline, but 2.1.0 should already get you far.

Some examples of rspec-puppet only function testing can be found in the upcoming refresh of stdlib's function specs, currently in my WIP branch here:

https://github.com/DavidS/puppetlabs-stdlib/commits/modules-1882-rspec-puppet-migration


It seems like you only have one function using the v4 API (type_of), and the unit test for that is only run on Puppet 4.0, but the travis matrix doesn't specify puppet 4.0 at all. So the test for that doesn't run. Does it work if you try it locally with Puppet 4.0?


To demonstrate the issue I created a minimal test module (https://github.com/dalen/puppet-v4functiontest), and it still doesn't work with rspec-puppet 2.1.0: https://travis-ci.org/dalen/puppet-v4functiontest/builds/60517933

Do you have any idea what is wrong with that code?

Yeah, I've now finally found out what is wrong with this. Puppet4 requires the `environment path` setting to find the code. spec-puppet's own test suite sets this, but it does not setup projects with this setting. I've submitted a PR to fix this. module projects will have to update this manually.

fixed code in your test module I've used for debugging: https://github.com/DavidS/puppet-v4functiontest/tree/update-rspec-infra

Cheers, David

Erik Dalén

unread,
May 21, 2015, 4:13:22 AM5/21/15
to puppe...@googlegroups.com

Thanks a lot for looking into it and solving it. I'm traveling a bit ATM, but will give it a try once I'm back.


--
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.
Reply all
Reply to author
Forward
0 new messages