Creating scope object in Puppet v3.x

154 views
Skip to first unread message

KomodoDave

unread,
Apr 29, 2014, 12:26:20 PM4/29/14
to puppet...@googlegroups.com
Hi all,

I wrote a neat little script for Puppet 2.x to test custom functions without needing to do a Puppet run. This involved creating a Scope object using `Puppet::Parser::Scope.new` then calling `send` to send a function call to it.

In Puppet 3 however, creating a Scope instance this way does not work. Now a Compiler object is a required argument of the Scope constructor, and a Node argument is a required argument of the Compiler constructor, and the list goes on.

Does anyone know how I can most simply create a Scope object like I did before?

Many thanks,

Dave

Notice:  This email is confidential and may contain copyright material of Ocado Limited (the "Company"). Opinions and views expressed in this message may not necessarily reflect the opinions and views of the Company.

If you are not the intended recipient, please notify us immediately and delete all copies of this message. Please note that it is your responsibility to scan this message for viruses.

Company reg. no. 3875000.

Ocado Limited
Titan Court
3 Bishops Square
Hatfield Business Park
Hatfield
Herts
AL10 9NE

KomodoDave

unread,
Apr 30, 2014, 1:19:17 PM4/30/14
to puppet...@googlegroups.com
So I guess no-one knows how to achieve this?

Henrik Lindberg

unread,
Apr 30, 2014, 2:07:25 PM4/30/14
to puppet...@googlegroups.com
On 2014-30-04 15:19, KomodoDave wrote:
> So I guess no-one knows how to achieve this?
>
>

Suggest that you write rspec tests for your custom functions.
Just mimic what is done for the built in functions.
As an example of a simple function - look at
spec/unit/parser/functions/sprintf_spec.rb

- henrik
> delete all copies of this message.. Please note that it is your
> responsibility to scan this message for viruses.
>
> Company reg. no. 3875000.
>
> Ocado Limited
> Titan Court
> 3 Bishops Square
> Hatfield Business Park
> Hatfield
> Herts
> AL10 9NE
>
> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to puppet-users...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/3b007c68-5e67-4cfa-b4e1-d4ddea4b1429%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/3b007c68-5e67-4cfa-b4e1-d4ddea4b1429%40googlegroups..com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


KomodoDave

unread,
May 1, 2014, 9:18:52 AM5/1/14
to puppet...@googlegroups.com

Thanks for the response, Henrik.

I’ve tried what you suggested already:

node=Puppet::Node.new('localhost')
compiler=Puppet::Parser::Compiler.new(node)
scope=Puppet::Parser::Scope.new(compiler)

..but the result is:


/usr/lib/ruby/vendor_ruby/puppet/context.rb:43:in `lookup': no 'environments' in {:root_environment=>#<Puppet::Node::Environment:0x7f31f0383230 @modulepath=[], @name=:"*root*", @manifest="path_to_my_script_dir">, :current_environment=>#<Puppet::Node::Environment:0x7f31f0383230 @modulepath=[], @name=:"*root*", @manifest="path_to_my_script_dir">} at top of [] (Puppet::Context::UndefinedBindingError)

..where path_to_my_script_dir is the absolute path of the directory containing my ruby script.

Is there a way for me to set up the correct context and/or environments mentioned in this error message?

Cheers,

Dave


On Tuesday, 29 April 2014 13:26:20 UTC+1, KomodoDave wrote:

Henrik Lindberg

unread,
May 2, 2014, 2:18:02 PM5/2/14
to puppet...@googlegroups.com
On 2014-01-05 11:18, KomodoDave wrote:
> Thanks for the response, Henrik.
>
> I’ve tried what you suggested already:
>
> |node=Puppet::Node.new('localhost')
> compiler=Puppet::Parser::Compiler.new(node)
> scope=Puppet::Parser::Scope.new(compiler)
> |
>
> ..but the result is:
>
> |
> /usr/lib/ruby/vendor_ruby/puppet/context.rb:43:in `lookup': no 'environments' in {:root_environment=>#<Puppet::Node::Environment:0x7f31f0383230 @modulepath=[], @name=:"*root*", @manifest="path_to_my_script_dir">, :current_environment=>#<Puppet::Node::Environment:0x7f31f0383230 @modulepath=[], @name=:"*root*", @manifest="path_to_my_script_dir">} at top of [] (Puppet::Context::UndefinedBindingError)
> |
>
> ..where |path_to_my_script_dir| is the absolute path of the directory
> containing my ruby script.
>
> Is there a way for me to set up the correct context and/or environments
> mentioned in this error message?
>

Sorry, your function is obviously not inside puppet itself and needs to
be setup. Look at how tests are done for functions in puppet-stdlib, it
uses rspec-puppet to help with setting up things for the tests.

https://github.com/puppetlabs/puppetlabs-stdlib/tree/master/spec/functions

Hope that helps.

- henrik

KomodoDave

unread,
May 2, 2014, 2:33:30 PM5/2/14
to puppet...@googlegroups.com
You're right that my function isn't inside Puppet, Henrik. But the whole point of it is to be able to run Puppet functions without needing to run Puppet itself so they can be live debugged very quickly.

It worked fine with Puppet 2.6, but I guess the upgrades that have been to Puppet made since then prevent it from operating in the same way with Puppet 3.

I'll try to stick it into the test framework as you've suggested.

Many thanks,

Dave
Reply all
Reply to author
Forward
0 new messages