Test-driven development can bring a number of benefits. An approach where you write tests first
makes you think about what you need to develop. Subsequent refactoring generally leads to
designs that emerge in the code with high levels of test coverage.
Recently PuppetLabs introduced a new tool for acceptance testing
called Beaker.
A Puppet module is applied to a system under test (SUT) which in turn is
acceptance tested. These tests would normally complement a unit test-driven
approach to writing modules with rspec-puppet.
Acceptance Testing with Beaker
Beaker can manage the temporary creation of a SUT via a hypervisor.
The Puppet module under test is installed and then the tests are executed.
Install Beaker
Beaker can be installed via a Gemfile
Use bundler to install the gems
Beaker Configuration
Typically Beaker acceptance tests are held in a spec/acceptance subdirectory.
A spec/acceptance/nodesets subdirectory defines the machines that we want to test against.
Nodesets are yaml files that define the machine’s name, location and platform. The
hypervisor type is also specified. Examples for Vagrant can be found
here.
The spec_helper_acceptance.rb file provides the basic configuration. The module
under test is copied on to each host. UNSUPPORTED_PLATFORMS provides an
easy way to de-scope tests for a particular platform.
Acceptance Tests
The acceptance tests describe the expected behaviour and state of the SUT and are
written in RSpec. In this example a test context is defined for user (and group) creation.
This test context applies the manifest, expects it to execute without errors and then
validates that a user is created in the correct group.
Tests can be run with
Test Execution
Beaker will initially use the specified hypervisor to bring up a SUT.
Puppet will then be installed and the acceptance tests
within spec/acceptance will be performed.
Generally each test applies manifests within the module for a particular context.
The tests are then performed. Beaker resolves them to an underlying operating system
command in order to validate the assertion
The hypervisor will then shutdown and destroy the SUT.
Beaker reports on the results when all the tests have completed.
Summary
Beaker is a new acceptance testing tool from PuppetLabs.
It is designed to test a module by applying it to a SUT.
It is provided as a Ruby (1.8+) Gem.
It can manage a hypervisor to create a temporary SUT for testing.
Underlying operating system commands are used behind the scenes.