Facter not initialized in rspec in puppetlabs-stdlib?

226 views
Skip to first unread message

Wil Cooley

unread,
Apr 11, 2012, 2:00:55 AM4/11/12
to puppet...@googlegroups.com
I have been working on adding some predicate functions to puppetlabs-stdlib that test things based on 'interfaces' and related facts.

The functions themselves seem to work fine in my manifest-based smoketests, but I am running into trouble that may or may not be due to my ignorance about rspec or Facter. Something needs to be done to initialize Facter because lookupvar('interfaces') just returns :undefined. I have not found any examples in the existing rspec tests; getvar_spec.rb seems like it should have something but the tests it does are pretty basic.

You can see some of what I've been trying at https://github.com/wcooley/puppetlabs-stdlib/blob/master/spec/unit/puppet/parser/functions/has_interface_with_spec.rb

Is there something obvious I'm missing?

Wil

Jeff McCune

unread,
Apr 11, 2012, 12:03:22 PM4/11/12
to puppet...@googlegroups.com
On Tue, Apr 10, 2012 at 11:00 PM, Wil Cooley <wilc...@gmail.com> wrote:
> I have been working on adding some predicate functions to puppetlabs-stdlib
> that test things based on 'interfaces' and related facts.

Sweet!

> The functions themselves seem to work fine in my manifest-based smoketests,
> but I am running into trouble that may or may not be due to my ignorance
> about rspec or Facter. Something needs to be done to initialize Facter
> because lookupvar('interfaces') just returns :undefined. I have not found
> any examples in the existing rspec tests; getvar_spec.rb seems like it
> should have something but the tests it does are pretty basic.

In general, you should assume the spec_helper for stdlib will
initialize facter for you. In fact, you don't need to do require
'facter' in your example specification.

Cool, I went ahead and pulled down your branch. If I might make a
suggestion, I notice you're developing on the "master branch" and you
have a number of commits that have not actually been merged into the
"master" branch of the puppetlabs repository.

This situation of having two different branches, both named master, is
confusing and makes it a bit difficult to work with.

When using Git, creating new branches is almost effortless. It's just
two commands.

When you start hacking please create a topic branch:

# Create a branch indicating this is a new feature based on master
having something to do with an interface function
git branch feature/master/has_interface_with_function origin/master
# Checkout the branch
git checkout feature/master/has_interface_with_spec
# Hack away

It's also fairly easy to rename your current topic branch which is
named "master"

# Rename your local master branch to a topic branch
git branch rename master feature/master/has_interface_with_function

# Push the new name to your github repository
git push wcooley
feature/master/has_interface_with_function:feature/master/has_interface_with_function

# Set your local topic branch to track the published copy of it.
git branch --set-upstream feature/master/has_interface_with_function
wcooley/feature/master/has_interface_with_function

# Re-synchronize the Puppet Labs master branch (I assume origin is
git://github.com/puppetlabs/puppetlabs-stdlib.git)
git fetch origin

# Create your local master tracking Puppet Labs master (These should
always be in sync. Don't commit on this branch...)
git branch master --track origin/master

# Replace your own master branch on Github with the Puppet Labs master
branch (because it's not actually master right now)
git push wcooley +origin/master:master

# Finally, work in your topic branch
git co feature/master/has_interface_with_function

> Is there something obvious I'm missing?

The setup looks to be quite strange. You shouldn't ever need to
define a get_scope method. I recommend looking at the
validate_re_spec.rb for a good example.

I'm also re-working your topic branch in my own if you'd like to take
a look at the changes.

-Jeff

Jeff McCune

unread,
Apr 11, 2012, 12:28:36 PM4/11/12
to puppet...@googlegroups.com
Wil,

I went ahead and re-worked the example specification.

Please take a look at the commit message and the way I'm setting an
expectation that mocks the behavior of lookupvar for both Mac OS X and
Linux.

https://github.com/jeffmccune/puppetlabs-stdlib/tree/feature/master/has_interface_with

$ rspec --format d spec/unit/puppet/parser/functions/has_interface_with_spec.rb

function_has_interface_with
On Mac OS X Systems
should have loopback (lo0)
should not have loopback (lo)
On Linux Systems
should have loopback (lo)
should not have loopback (lo0)

Finished in 0.04262 seconds
4 examples, 0 failures

Please feel free to reply here if you have any questions. We might
also want to consider moving this thread to puppet-dev since it's
developing a new function.

Hope this helps,
-Jeff

Jeff McCune

unread,
Apr 11, 2012, 1:23:10 PM4/11/12
to puppet...@googlegroups.com
On Wed, Apr 11, 2012 at 9:28 AM, Jeff McCune <je...@puppetlabs.com> wrote:
> Wil,
>
> I went ahead and re-worked the example specification.
>
> Please take a look at the commit message and the way I'm setting an
> expectation that mocks the behavior of lookupvar for both Mac OS X and
> Linux.
>
> https://github.com/jeffmccune/puppetlabs-stdlib/tree/feature/master/has_interface_with

Forgot to mention, the most recent commit in that branch is where I
re-worked the spec you asked about.

I've found it's better to link to branches instead of commits because
I often re-write history and commits are absolute references that
might go away inside of a branch whereas a branch is a relative
reference that can be updated frequently.

The actual commit I was referring to is:
https://github.com/jeffmccune/puppetlabs-stdlib/commit/dac3c508659bfc7d034509d3860afe5291aed722

Hope this helps,
-Jeff

Wil Cooley

unread,
Apr 12, 2012, 12:59:20 AM4/12/12
to puppet...@googlegroups.com


On Wednesday, April 11, 2012 9:03:22 AM UTC-7, Jeff McCune wrote:
On Tue, Apr 10, 2012 at 11:00 PM, Wil Cooley <wilc...@gmail.com> wrote:

In general, you should assume the spec_helper for stdlib will
initialize facter for you.  In fact, you don't need to do require
'facter' in your example specification.

> You can see some of what I've been trying at
> https://github.com/wcooley/puppetlabs-stdlib/blob/master/spec/unit/puppet/parser/functions/has_interface_with_spec.rb

Cool, I went ahead and pulled down your branch.  If I might make a
suggestion, I notice you're developing on the "master branch" and you
have a number of commits that have not actually been merged into the
"master" branch of the puppetlabs repository.


Yeah, usually I do make branches; I think I mistakenly committed to master and then did not want to take the time to figure out how to move it on to a branch; your instructions below were very helpful. It would've taken me some hours between re-reading Pro Git and searching stackoverflow to figure it out on my own.
 

The setup looks to be quite strange.  You shouldn't ever need to define a get_scope method.  I recommend looking at the validate_re_spec.rb for a good example.

It is strange; it's the result of an hour or two of thrashing about, throwing bits at the wall and trying to see if anything stuck. Toss in some frustrations with gem and getting a working rspec environment and it's a really bad dish.
 

I'm also re-working your topic branch in my own if you'd like to take
a look at the changes.

Thanks;  I think I was expecting to be able to get facts from the live system, which is obviously inferior to mocking the facts that should be expected. I've merged in your branch and looked at your reworked version; I think it makes sense and I see how to proceed with adding the rest of my tests.

Unfortunately, I am unable to run any of the rspec tests--not just in has_interface_with_spec.rb, but on any of the specs. They fail with:

     Failure/Error: Unable to find matching line from backtrace
     NoMethodError:
       undefined method `initialize_everything_for_tests' for #<Puppet::Util::Settings:0x2b7d43d59940>

I've got 2.7.13, RPMs built yesterday, both puppet and puppet-master installed:

$ rpm -qi puppet
Name        : puppet                       Relocations: (not relocatable)
Version     : 2.7.13                            Vendor: (none)
Release     : 1.el5                         Build Date: Tue 10 Apr 2012 01:52:57 PM PDT
Install Date: Wed 11 Apr 2012 09:46:53 PM PDT      Build Host: rpm-builder.puppetlabs.lan
...

ISTR seeing something about this method in the commit logs recently...

Also, go ahead and reply to puppet-dev if you wish; I'm trying to use the Google Groups interface and it does not appear to let me direct my response to a different group.

Wil

Jeff McCune

unread,
Apr 12, 2012, 1:06:25 PM4/12/12
to puppet...@googlegroups.com
On Wed, Apr 11, 2012 at 9:59 PM, Wil Cooley <wilc...@gmail.com> wrote:
> Unfortunately, I am unable to run any of the rspec tests--not just in
> has_interface_with_spec.rb, but on any of the specs. They fail with:
>
>      Failure/Error: Unable to find matching line from backtrace
>      NoMethodError:
>        undefined method `initialize_everything_for_tests' for
> #<Puppet::Util::Settings:0x2b7d43d59940>
>
> I've got 2.7.13, RPMs built yesterday, both puppet and puppet-master
> installed:

Ah, this is a problem our platform team is actively working on right now.

The problem is that modules like stdlib need Puppet to be initialized
in a manner suitable for testing. Setting things like confdir and
what not. This means different actions for different versions of
Puppet, and stdlib works all the way back through 2.6.

In any event, if you run against Puppet 2.7.x from source, this method
should already be added to Puppet.

We should also have stdlib fixed up shortly.

If you'd like to actively work on this, please feel free to hop into
#puppet and or #puppet-dev on freenode and ping cprice or myself.

More background information on this problem is located at:
http://projects.puppetlabs.com/issues/13693

Hope this helps,
-Jeff

Wil Cooley

unread,
Apr 14, 2012, 11:34:10 PM4/14/12
to puppet...@googlegroups.com
On Thursday, April 12, 2012 10:06:25 AM UTC-7, Jeff McCune wrote:

The problem is that modules like stdlib need Puppet to be initialized
in a manner suitable for testing.  Setting things like confdir and
what not.  This means different actions for different versions of
Puppet, and stdlib works all the way back through 2.6.

I took the expedient way out and commented out the requires from spec_helper. :-\ My tests pass now, at least.

I have now added tests for the has_ip_address and has_ip_network, which are more porcelain than has_interface_with.


If you like it, I will open a ticket and submit a pull request.

Wil
 

Jeff McCune

unread,
Apr 16, 2012, 5:01:11 PM4/16/12
to puppet...@googlegroups.com

Yes, please file a ticket at:

http://projects.puppetlabs.com/projects/stdlib

You'll need to register an account if you don't already have one.

If it's a somewhat substantial patch could you also please sign the CLA at:
https://projects.puppetlabs.com/contributor_licenses/sign

Thanks,
-Jeff McCune

Wil Cooley

unread,
May 18, 2012, 1:52:52 PM5/18/12
to Puppet Users
On Apr 16, 2:01 pm, Jeff McCune <j...@puppetlabs.com> wrote:
> On Sat, Apr 14, 2012 at 8:34 PM, Wil Cooley <wilcoo...@gmail.com> wrote:
> > On Thursday, April 12, 2012 10:06:25 AM UTC-7, Jeff McCune wrote:
>
> >> The problem is that modules like stdlib need Puppet to be initialized
> >> in a manner suitable for testing.  Setting things like confdir and
> >> what not.  This means different actions for different versions of
> >> Puppet, and stdlib works all the way back through 2.6.
>
> > I took the expedient way out and commented out the requires from
> > spec_helper. :-\ My tests pass now, at least.
>
> > I have now added tests for the has_ip_address and has_ip_network, which are
> > more porcelain than has_interface_with.
>
> >https://github.com/wcooley/puppetlabs-stdlib/compare/puppetlabs:maste...
>
> > If you like it, I will open a ticket and submit a pull request.
>
> Yes, please file a ticket at:
>
> http://projects.puppetlabs.com/projects/stdlib
>
> You'll need to register an account if you don't already have one.
>
> If it's a somewhat substantial patch could you also please sign the CLA at:https://projects.puppetlabs.com/contributor_licenses/sign

I thought I responded that I had opened a ticket and submitted a pull
request, but I guess I didn't.

Anyway, here's the ticket:

https://projects.puppetlabs.com/issues/13974

And here's the pull request:

https://github.com/puppetlabs/puppetlabs-stdlib/pull/64

Would someone with access fix the typo in the subject of the ticket?
Change "not to use" to "not easy to use".

Thanks!
Wil
Reply all
Reply to author
Forward
0 new messages