This guide will help you start a test process for organizational module code and public shared code. Part of writing modules is writing and automating tests. We recommend that you plan and design module testing and (ultimately) automate deployment. If you’re a Forge contributor, having a plan to test code can also help you improve your quality score and get the approved tag.
We’ll give you a list of tools that you can use, and some concrete steps for how to start testing with them.
Tools to test Puppet code
The time to complete each type of test increases as you go down the list. The tests that are quickest to complete are at the top, and the tests that take the longest to complete are at the bottom.
puppet parser validate
When you start testing, keep it simple and build on your success. The order of these tests (like the list above) goes from less time consuming to more time-consuming and from less complex to more complex.
Syntax and linting
Start by using your organization’s version control tooling to validate syntax and lint code before or during code pushes. This helps ensure that code committed to the repo can be compiled.
- Instructions on how to set up and use Git hooks
- Using Git hooks with the Puppet Development Kit (PDK).
Use the Puppet Development Kit (PDK)
We recommend that you convert your modules to be compatible with PDK. Use PDK to validate syntax, check style, and do Rspec-Puppet unit tests on any compatible module to ensure that your code functions and meets structural guidelines.
Unit testing with Rspec-Puppet
Testing in Rspec-Puppet is not done on an agent or by applying code. Rspec-Puppet compiles a catalog and evaluates the catalog’s content against expected test results. When planning and preparing to write or extend unit test code with Rspec, use PDK. The
pdk test unit command compiles a catalog and runs all valid Rspec-Puppet unit tests against it.
This example file (
.gitlab-ci.yml), runs linting and syntax tests with the command
pdk validateand Rspec-Puppet unit tests with the command
pdk test unit in the Puppet PDK Docker image:
stages: - syntax - unit cache: paths: - vendor/bundle PDK validate: stage: syntax image: puppet/pdk:188.8.131.52 script: - pdk validate PDK test unit: stage: syntax image: puppet/pdk:184.108.40.206 script: - pdk test unit
More information on Rspec Puppet
Puppet System/Integration tests evaluate an enforced node to ensure that the node is configured correctly using a set of tests. We recommend that you use Puppet Litmus and/or Serverspec for system/integration testing. This testing requires infrastructure and communication with a container server or a virtual machine deployment API. It requires more research, access, and planning than the testing previously described in this article.
Litmus deploys a container or virtual machine, enforces the code, and runs the system/integration tests. If you’d like to configure and implement Litmus, learn more by reading a step by step guide for MoTD.
If you can’t use Litmus at your organization, you can use Serverspec. Serverspec is a testing framework. You can write tests and add them to a repo, but the automation to deploy a target node and enforce the code onto it must come from organizational infrastructure processes. Once a target node is deployed and configured, you can use Serverspec to test the target node configuration.
Learn more from this example of Serverspec testing with Vagrant.