Hi Dan,

Yeah, I'm focusing mainly on getting a clean RSpec API at the moment. What it 
looks like currently is

require 'rspec-puppet'

describe 'sysctl', :type => :define do
let(:module_path) { '/path/to/my/modules' }
let(:name) { 'baz' }
let(:params) { { :value => 'foo' } }

it { should create_exec('sysctl/reload').with_command("/sbin/sysctl -p 
/etc/sysctl.conf") }
end


This generates the catalogue for "sysctl { 'baz': value => 'foo' }" and checks 
that Exec['sysctl/reload'] exists with the specified command.

The code still has a few hardcoded values - that I need to get rid of - that 
prevent it from being usable by anyone else, but you can check out the code at 
https://github.com/rodjek/rspec-puppet. I'd love to hear any suggestions that 
you have!

Thanks,
Tim

On Sunday, 17 July 2011 at 4:32 PM, Dan Bode wrote:

> 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] 
> (mailto:[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 (http://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] 
> > > (mailto:[email protected]).
> > >  To unsubscribe from this group, send email to 
> > > [email protected] 
> > > (mailto:[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] 
> > (mailto:[email protected]).
> >  To unsubscribe from this group, send email to 
> > [email protected] 
> > (mailto:puppet-dev%[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 (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] 
> (mailto:[email protected]).
>  To unsubscribe from this group, send email to 
> [email protected] 
> (mailto:[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.

Reply via email to