Posts in Category: rspec puppet

Tip: avoid redownloading after each rspec run

the rake’s spec command does is comprised of the following:

spec = spec_prep + spec_standalone + spec_clean

So we need to avoid running spec_clean. To do this simply run:

$ bundle exec rake spec_prep

After that, always run

$ bundle exec rake spec_standalone

Instead of the “spec” command

Puppet – Testing Puppet Modules using RSpec

RSpec is all about unit testing. That means we test the class without the need for changing the state of any puppet agents, i.e. it’s a bit like a dry run. When we run an rspec tests, all that happens is that the rspec test provides a set of dummy (facter/puppet-variables/hiera) data to the puppetmaster via the “let” statement. The puppetmaster then generates the catalog using these inputs and then it sends it to RSpec instead of puppet-agent. RSpec then reviews the catalog to check that it contains what it “should” contain.

If you use this approach then you can nearly write your entire puppet module without needing to run the module against a puppet agent, which is best to do much later on as part of

Puppet – What is RSpec

RSpec is a BDD unit testing framework that is designed for testing ruby code.

When we run a RSpec test scripts against our puppet code we are working on, the following takes place:

  1. RSpec temporarily generates it’s own site.pp file, and then instructs the puppetmaster to use make use of that site.pp file rather than the normal site.pp file.
  2. RSpec also generates the modules folder which houses the main puppet module (that is to be tested) along with any puppet modules that it depends on. It then instructs the puppetmaster to use the new modules folder rather than the old one
  3. RSpec masquarades as a puppet agent and submits a catalog request to the master.
  4. The puppetmaster then requests facter data from the puppet agent (ie, rspec)
  5. The puppetmaster then generates the

Puppet – Setting up RSpec

Install RVM and Bundler

RSpec tests needs to be run in a tightly controlled environment. This will help ensure that the RSpec tests that you write/run in on your machine will also run on other people’s machine too. The environment has to be controlled in the in the following context:

  • RSpec test needs to be run under a known version of Ruby. The exact version is not that important, as long as we know which version our tests are contained in. The RVM gem is specifically designed for this purpose.
  • We need to contain the exact gems (along with gem versions) that the Rspec tests has access to. The Bundler gem is specifically designed to meet this purpose.
  • The Rspec tests needs to be executed by a non-root user. This will help to keep

Puppet – Unit Testing with RSpec