You are browsing a read-only backup copy of Wikitech. The live site can be found at

Puppet coding/testing: Difference between revisions

From Wikitech-static
Jump to navigation Jump to search
No edit summary
(Dzahn moved page Puppet coding/testing to Puppet/testing: requested by Brett )
(12 intermediate revisions by 5 users not shown)
Line 1: Line 1:
#REDIRECT [[Puppet/testing]]
==== Running ====
Some modules have test suites using the ruby test runner rspec and a set of rake tasks to run linting check (valid manifests, puppet-lint, hiera yaml files, erb templates). The ruby dependencies are listed in /Gemfile and are installed via bundler. To install all dependencies and later run a command in that environment:
bundle install
bundle exec <some command>
Assuming a module has a Rakefile and tests defined in a spec sub directory, one can run all tests via:
bundle exec rake syntax
bundle exec rake puppet-lint
bundle exec rake spec
You can list the various rake tasks via <code>bundle exec rake -T</code> or for a tree of the task dependencies <code>bundle exec rake -P</code>.
==== Rake explained ====
The <code>/Gemfile</code> ask for the rubygem [ puppetlabs_spec_helper] ([ doc]) which contains several predefined rake tasks. Hence in a module one just have to create a Rakefile with:
<source lang=ruby>
require 'puppetlabs_spec_helper/rake_tasks'
<source lang=console>
$ cd modules/mymodule
$ bundle exec rake -T
rake beaker                # Run beaker acceptance tests
rake beaker_nodes          # List available beaker nodesets
rake build                # Build puppet module package
rake check:dot_underscore  # Fails if any ._ files are present in directory
rake check:git_ignore      # Fails if directories contain the files specified in .gitignore
rake check:symlinks        # Fails if symlinks are present in directory
rake check:test_file      # Fails if .pp files present in tests folder
rake clean                # Clean a built module package
rake compute_dev_version  # Print development version of module
rake coverage              # Generate code coverage information
rake help                  # Display the list of available rake tasks
rake lint                  # Run puppet-lint
rake release_checks        # Runs all nessesary checks on a module in preparation for a release
rake spec                  # Run spec tests in a clean fixtures directory
rake spec_clean            # Clean up the fixtures directory
rake spec_prep            # Create the fixtures directory
rake spec_standalone      # Run spec tests on an existing fixtures directory
rake syntax                # Syntax check Puppet manifests and templates
rake syntax:hiera          # Syntax check Hiera config files
rake syntax:manifests      # Syntax check Puppet manifests
rake syntax:templates      # Syntax check Puppet templates
rake validate              # Check syntax of Ruby files and call :syntax and :metadata_lint
The <code>syntax*</code> tasks come from the [ rubygem puppet-syntax].
The <code>spec*</code> tasks are helper to run rspec taking care of preparing test fixtures and tearing them down.
==== rspec-puppet ====
To tests puppet resources, we rely on [ rspec-puppet] an helper on top of the ruby test runner rspec. rspec-puppet provides utilities to setup puppet, compile a catalog and has built-in assert methods to run against the catalog.
A minimal case requires a Rakefile, an helper file to be read by each test, and a spec. At first the Rakefile reuse the puppetlabs_spec_helper rake tasks described in the previous section:
<source lang=ruby>
require 'puppetlabs_spec_helper/rake_tasks'
The tests are placed in sub directories of <code>spec/</code> based on the type of Puppet resource being tested. That convention lets rspec-puppet properly setup the environment. rspec find tests by crawling the hierachy looking for files with the suffix <code>_spec.rb</code>. The hierarchy is:
  ├── applications/
  ├── classes/
  ├── defines/
  ├── functions/
  ├── hosts/
  ├── types/
  └── types_aliases/
We also need common code to initialize Puppet and point it to a fixture directory, that is where rspec-puppet will create a dummy manifests/site.pp and inject required modules.
Create <code>spec/spec_helper.rb</code>:
<source lang=ruby>
require 'rspec-puppet'
fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
RSpec.configure do |c|
  c.module_path = File.join(fixture_path, 'modules')
  c.manifest_dir = File.join(fixture_path, 'manifests')
The file will be required by each of the specs.
Given a puppet module <code>mymodule</code> consisting of a single class in <code>manifests/init.pp</code>:
<source lang=ruby>
class mymodule {
We first have to instruct rspec-puppet to inject our module in the fixture directory. To do so create a <code>/.fixtures.yml</code>:
<source lang=yaml>
        mymodule: "#{source_dir}"
Since we will test a class, we create a <code>mymodule_spec.rb</code> file under <code>spec/classes/</code>:
<source lang=ruby>
require 'spec_helper'
# We will act on the resource "mymodule"
describe 'mymodule' do
  it { should.compile }
Finally some fancy configuration of rspec via <code>/.rspec</code>:
--format doc
And we can run the spec:
$ bundle exec rake spec
  should compile into a catalogue without dependency cycles
Finished in 0.07349 seconds (files took 0.4312 seconds to load)
1 example, 0 failures
==== Resources ====

Revision as of 18:02, 29 June 2022

Redirect to: