Beaker is a test framework created by Puppetlabs to run tests against puppet modules on real servers (vm, containers whatever) and test that they do what they say they should do.
This is a quick tutorial on how to use this framework. At the time of this writing, Beaker is under heavy development, so this could all change.
The first thing you need to do is install beaker. Usually this is as simple as
adding it to your Gemfile and running
gem 'beaker' gem 'beaker-rspec'
Now install it:
Rspec and the Puppetlabs Helper
This tutorial assumes you already have the puppetlabs_spec_helper installed, rake, rspec, etc.
Folder For Tests
You need a place to put acceptance tests. They must go in
See puppetlabs-mysql for an example of what it looks like.
You must have at least a default.yml in the nodesets folder inside your acceptance folder. Here is an example:
1 2 3 4 5 6 7 8
You can have different yaml files for different platforms you wish to test against. The format is described in the Beaker wiki
Note: I use my own docker files for speed, as they come preinstalled with the the Beaker Host Requirements
Warning: If you use docker, you cannot test service things because there is no init running inside the container. For comprehensive testing against things like services, firewalls, etc, you must use a true hypervisor with Vagrant.
Acceptance Spec Helper
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
The spec helper does the tasks needed in order to prepare your SUT (system under test). This might include installing puppet, installing your puppet module dependencies, etc.
Example Acceptance Test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
The filename is important, it must end in _spec.rb in order for the test harness to detect it. You can see that there are many matchers you can use to run pretty much any kind of test you can think of.
See the puppetlabs-mysql collection again for some great examples.
bundle exec rake acceptance
This command will spin up your described servers in nodesets, install your puppet modules and dependencies, and test your assertions.
Acceptance tests should be used sparingly, they are the top of the testing testing pyramid.
They are slow, touch the disks and network, and depend on external resources. The example mysql acceptance tests literally install mysql, install and configure databases, and assert that they exist.
They will may be slow, but they can be very helpful, and potentially the only way to really test functionality of a puppet module in an end-to-end way.
Puppet is a system configuration management tool. Unit tests can only go so far to make sure the compiled catalog is “correct”. Puppet acceptance tests can help you go 100% and ensure that your module literally does what it says it does by running tests against actual systems, files, packages, and services.