I always knew there was a better way.

In a previous post I documented a procedure for compiling a catalog without logging onto a Puppet Master.  The procedure is useful but also complicated.

Some would ask, why can’t Rspec-puppet just dump the catalogs it compiles during unit testing?

It can; I just found a bit of the answer here, and the rest of it inside a debugger.

How to dump the catalog in Rspec-puppet

I assume we have Rspec-puppet set up already.  If not, try the Rspec-puppet tutorial.

Imagine we have a simple test as follows:

require 'spec_helper'

describe 'myclass' do
  it {
    is_expected.to compile.with_all_deps
  }
end

To instead just dump the catalog, change the code as follows:

require 'spec_helper'

describe 'myclass' do
  it {
    File.write(
      'myclass.json',
      PSON.pretty_generate(catalogue)
    )
    #is_expected.to compile.with_all_deps
  }
end

(Note carefully the use of UK spelling in ‘catalogue’, no doubt a consequence of Rspec-puppet’s esteemed author Tim Sharpe being an Australian!)

Otherwise, that’s it. Now run the spec tests again and you’ll have the catalog under test saved in myclass.json.

Update see also Rob Nelson’s related post here.