We have been using puppet for a few years now and have a legacy of modules some of which are common and others that are quite specific. I want to make some changes to one of the common modules. When I have done this in the past I have just changed the module and rolled the changes out to a couple of servers using a dev branch before moving it live but this time I want something a bit more reliable.
These are some really simple notes on how I got started. This is not going to be a tutorial but should point to a some of the parts need to get started. A good place for more detail is the puppet labs post on next generation puppet module testing
Firstly install the requirements.
Install rvm to get a ruby you can play with.
Install bundler gem install bundler
Create the bundler Gemfile, (Puppetlabs recommend the filename .gemfile but I think Gemfile is clearer)
source :rubygems puppetversion = ENV.key?('PUPPET_VERSION') ? "= #{ENV['PUPPET_VERSION']}" : ['>= 2.7'] gem 'puppet', puppetversion gem 'puppetlabs_spec_helper', '>= 0.1.0' gem 'puppet-lint'
Then install the bundle of stuff bundle
. You should now have all
the bits required to get going.
Next up we need to create some fixtures. These are helpers that the test code
can setup so that your module runs. In this example it just makes sure you have
access to all the relevant modules that you require. So create a file called
.fixtures.yml
(note the stating dot)
fixtures: #repositories: #stdlib: git://github.com/puppetlabs/puppetlabs-stdlib.git #apt: git://github.com/puppetlabs/puppetlabs-apt.git symlinks: tomcat: "#{source_dir}" authbind: "#{source_dir}/../authbind" stdlib: "#{source_dir}/../stdlib" apt: "#{source_dir}/../apt"
I have chosen to just use the modules locally as we have them all in our tree,
but you can use the commented out part to pull the modules in directly. Rspec
will use this file to fill the spec/fixtures/
directory when it
builds up a copy of the code for testing.
Next up is a Rakefile (much like a Makefile but for ruby), this allows us to
use rake to run the tests and other commands simply. This gives use access to
commands like rake spec
and rake help
.
require 'rubygems' require 'puppetlabs_spec_helper/rake_tasks' # rake lint require 'puppet-lint/tasks/puppet-lint'
I added an extra lone so that you can run lint against the module as well. rake lint
Now we just need a bit of ruby to help the tests run. spec/spec_helper.rb
require 'rubygems' require 'puppetlabs_spec_helper/module_spec_helper'
Now you can create your tests and run them with rake spec
. I am
not going to talk about the tests here as I have only written one so far but
the puppetlabs link gives some examples. This is just to get the basics in
place.
I think there is also a puppet-rspec-init somewhere that would allow all this to be built automatically in the future.
Update: I also added a .gitignore
file in the module root so that
the fixtures dir does not get checked in. It just has
spec/fixtures
in it at the moment.