Hi,

I have been thinking about an approach similar to this lately, I would love
to see some sensible rspec APIs for testing Puppet code. I am happy to help
you get this together (since I would like to see some standards in place
ASAP)

I would have a look at 2 things:

you can easily tell puppet to use certain code to create a catalog with:
Puppet[:code]  (this is the variable that is set when you pass code to
puppet apply via STDIN)

The following exert comes from some unit tests I had written for testing my
interface-utils(it is testing a compiler wrapper I wrote):

    it "should be able to compile a catalog for a node" do
      Puppet[:code]='node node-daddy {notify{$foo:}}'

      catalog = compile_from_node('node-daddy')

      catalog.name.should == 'node-daddy'

      catalog.resource('Notify', 'bar').to_s.should == 'Notify[bar]'
    end


It looks pretty close to what you are trying to accomplish

I would also have a look at create_resources (which was accepted into core
for 2.7.x) It provides a function call that you can use to dynamically add a
type/class/define to the catalog.

It has some examples of (far from perfect) unit tests that are checking that
certain resources are added to a catalog

https://github.com/puppetlabs/puppetlabs-create_resources/

On Fri, Jul 15, 2011 at 3:59 PM, Tim Sharpe <[email protected]> wrote:

>  Hi Luke,
>
> Thanks, but this is not quite what I'm after.  Basically I want to test
> that a catalogue with just the baz resource generates certain resources, so
> I would like a way to generate the equivalent of "node 'foo' { bar { 'name':
> test => 'value' } }" by manipulating classes like Puppet::Node etc
>
> basically, my dream endpoint is to be able to do something like the
> following
>
> describe My::Define do
>   let(:facts) { {:ipaddress_eth0 => '192.168.0.1' }
>   let(:node) { 'testhost.example.com' }
>   let(:params) { :name => 'foo', :test => 'value' }
>   subject { <some magic here to get the catalogue for this test host
> applying just this resource> }
>
>   it { should.create_file "/etc/foo".with_mode('0444') }
> end
>
> It looks like Puppet::Node accepts a :classes attribute to apply classes to
> a node, but I'm not sure how to just apply a single resource to a node?
>
> Thanks,
> Tim
>
> On Saturday, 16 July 2011 at 10:01 AM, Luke Kanies wrote:
>
> On Jul 15, 2011, at 2:57 PM, Tim Sharpe wrote:
>
>  Hi,
>
> I'm looking at setting up some tests for the defined types in my manifests
> (probably using rspec) to test that node <foo> with facts <bar> applying
> define <baz> with params <gronk> creates a catalogue with the following
> resources...
>
> Basically, I want to generate the catalogue for the following and then
> inspect it's contents.
>
> node "foo" {
>   baz { 'name':
>     test => value,
>   }
> }
>
> Looking over Puppet::Node, I can see how to apply classes to a host, but
> nothing about individual resources.  If someone could point me in the right
> direction, that'd be greatly appreciated.
>
>
> Dan Bode has some interesting work here, I think - hopefully he can post
> it.
>
> In the meantime, I'd probably try something like this:
>
> before do
>   Puppet[:config] = "/path/to/my/config"
>   Puppet.settings.parse
>
>   @catalog = Puppet::Node::Catalog.indirection.find(Puppet[:certname])
> end
>
> it "should create a 'baz' resource" do
>   @catalog.resource(:baz, "name").should be_instance_of(Puppet::Resource)
> end
>
> Something like that, anyway.
>
>
> --
> This space intentionally has nothing but text explaining why this
> space has nothing but text explaining that this space would otherwise
> have been left blank, and would otherwise have been left blank.
>
> ---------------------------------------------------------------------
> Luke Kanies | http://puppetlabs.com | http://about.me/lak
>
> Join us in PDX for PuppetConf: http://bit.ly/puppetconfsig
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Puppet Developers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/puppet-dev?hl=en.
>
>
>   --
> You received this message because you are subscribed to the Google Groups
> "Puppet Developers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/puppet-dev?hl=en.
>



-- 
"Join us for PuppetConf <http://bit.ly/puppetconfsig>, September 22nd and
23rd in Portland, OR."

bit.ly/puppetconfsig

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to