Unit testing reference modules and best practices

181 views
Skip to first unread message

Ben Sullivan

unread,
Jul 10, 2014, 8:29:32 PM7/10/14
to puppet...@googlegroups.com
Hi

I'm looking for some reference modules to share with my team to illustrate how to best use rspec-puppet to unit test Puppet DSL code.

I'm going to go through the Puppet Enterprise supported modules as I figured that was a reasonable place to start: https://forge.puppetlabs.com/modules?supported=yes

The team doesn't understand the value of rspec-puppet at present and I need to articulate arguments to get some buy-in.  At the moment we rely on long-running end to end tests which won't scale for us as our automation codebase grows.

Any tips/recommendations would be appreciated.

Many thanks

Ben

Hunter Haugen

unread,
Jul 11, 2014, 3:11:15 PM7/11/14
to puppet-users
We try hard to use rspec-puppet in effective ways, but you may still see some over-testing in the supported modules nonetheless :).

We use rspec-puppet to test that:

1. Manifests compile and templates parse (catches a lot of problems)
2. Conditional logic in manifests follows the expected paths for given inputs (various facts of operatingsystems [4], or various expected parameter values)
3. Conditional logic in templates actually creates config files as expected (make sure to have "negative" tests that throw false/undef in for validation to avoid blank entries)
4. And recently started testing "future parser" and "strict variables" on our modules (as of puppetlabs_spec_helper 0.6.0).

But what does rspec-puppet not do?

rspec-puppet will obviously NOT indicate when a configuration file's syntax cannot be understood by the actual service (apache [2], haproxy, sudoers, etc), that your package names are really correct (mongodb just changed all of their upstream package names [3]), or when command output from the system changes with versions (git 1.7 vs 1.9 in vcsrepo) but for those cases a few beaker-rspec [1] tests should give you extra assurances after you get rspec-puppet doing the most critical parts. (beaker-rspec is much slower than rspec-puppet, so we try to limit its use).

[4] https://github.com/apenney/pe_fixtures is an example of a collection of facts to choose from



-Hunter


--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/36fe87b2-4430-4ab1-8f55-d4d93572942d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Haani Niyaz

unread,
Dec 2, 2015, 6:33:42 PM12/2/15
to Puppet Users
Hi Ben,

I am also looking for some reference material to better illustrate how to use rsepc puppet, a best practices of sorts to my team. Were you successful in finding some examples?

Garrett Honeycutt

unread,
Dec 3, 2015, 11:41:29 AM12/3/15
to puppet...@googlegroups.com
Hello,

Last year I spoke[1] at LISA about why TDD works for configuration
management. Unfortunately, those slides are all pictures.. I teach an
interactive class about testing and last year gave a 1/2 day tutorial on
testing with rspec-puppet at LOAD. Those slides[2] explain why testing
is important, what are all the bits needed to get rspec-puppet to work
and what to test. It references a Vagrant setup[3] to get you started.
It installs some older gems, so you might be better off cloning one of
the modules below and using `rvm use 2.1.0 && bundle install` to get all
the testing dependencies going.

My vim module is an example[4] of testing a simple class that works on
many platforms and the nscd module is an example[5] of a module with a
ton of parameters and also supporting many platforms.

If you're interested in best practices around testing, I recently
starting working with Rubocop after noticing that a puppet-community
module was using it in their automated testing. Rubocop enforces ruby
style. Here's an example commit[6] that shows how I added it to a
module, modified the Travis-ci testing setup and all the changes to the
code that resulted.

[1] -
http://www.slideshare.net/gh/20141114-why-test-driven-development-tdd-works-for-sysadmins-lisa14

[2] - http://www.slideshare.net/gh/20140406-loa-daystddwithpuppettutorial

[3] - https://github.com/ghoneycutt/learnpuppet-tdd-vagrant

[4] -
https://github.com/ghoneycutt/puppet-module-vim/blob/master/spec/classes/init_spec.rb

[5] -
https://github.com/ghoneycutt/puppet-module-nscd/blob/master/spec/classes/init_spec.rb

[6] -
https://github.com/ghoneycutt/puppet-module-nscd/commit/de95ca517ed7df0bf2c34b0c8decff551a697fde

Best regards,
-g

--
Garrett Honeycutt
@learnpuppet
Puppet Training with LearnPuppet.com
Mobile: +1.206.414.8658
Reply all
Reply to author
Forward
0 new messages