Mocking resources/classes in rspec-puppet

1,720 views
Skip to first unread message

R. Tyler Croy

unread,
Mar 7, 2012, 1:53:35 AM3/7/12
to puppet...@googlegroups.com
My module now depends on the puppetlabs/puppet-apt module for supporting
Debian/Ubuntu hosts, doing this however broke all my debian/ubuntu related
rspec-puppet tests.

Is there a reasonable way to mock out resources (like apt::source, etc) to just
test that my classes are doing what they should be doing?


Cheers
- R. Tyler Croy
--------------------------------------
Code: http://github.com/rtyler
Chatter: http://twitter.com/agentdero
rty...@jabber.org

Jeff McCune

unread,
Mar 7, 2012, 11:22:53 AM3/7/12
to puppet...@googlegroups.com
On Tue, Mar 6, 2012 at 10:53 PM, R. Tyler Croy <ty...@monkeypox.org> wrote:
My module now depends on the puppetlabs/puppet-apt module for supporting
Debian/Ubuntu hosts, doing this however broke all my debian/ubuntu related
rspec-puppet tests.

Is there a reasonable way to mock out resources (like apt::source, etc) to just
test that my classes are doing what they should be doing?

Not that I know of.  rspec-puppet is testing the catalog itself and we don't have a clean way to put mock resources in the catalog.

What's the breakage look like?  I usually treat this sort of issue as a code smell.  Ideally, one module will simply require Class resources from other modules rather than specific resources inside the class.  In this situation all you need to do is declare a class with the same name in your spec tests to satisfy the dependency.

Make sense?

-Jeff

Tim Sharpe

unread,
Mar 8, 2012, 7:09:21 AM3/8/12
to puppet...@googlegroups.com
As Jeff said, ideally you wouldn't have any cross-module dependencies, but everyone still does it.

There's a couple of ways you can solve this issue at the moment.  The first (and my preferred way) is to setup a fixtures directory as described in http://bombasticmonkey.com/2012/03/02/automatically-test-your-puppet-modules-with-travis-ci/ and then drop your apt module into spec/fixtures/modules/apt so that it's available to rspec-puppet.  

The other option is to define a stub apt::source type like so

```
describe 'foo::bar' do
  let(:pre_condition) { "define apt::source($param1, $param2…) {}" }

  it { should contain_apt__source('baz') }
end
```

Cheers,
Tim
--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.

Reply all
Reply to author
Forward
0 new messages