Puppet Module Testing
Best practices to make your module awesome!
Prepared By: Bill Fraser
September 9th, 2014
© 2014 Pythian
About Me• Principal Consultant, Enterprise
Infrastructure Services• Open source enthusiast• DevOps, WebOps, SRE• IT automation and configuration
management• System, network and application
monitoring and performance trending
• Cloud and virtualization technologies
© 2014 Pythian
Why Test?• Does the software do what it was designed to do?• Are you confident code changes won’t break existing
functionality? Or introduce new bugs?• Are your troubleshooting, repair, rework and / or redesign
costs minimal?• Is your software used externally? Do you have name or
brand perception / reputation to uphold?• Can other team members and contributors understand and
support / maintain the code?
© 2014 Pythian
How (Not) To Test• Where do you test?
• On a passive, standby server?• On a ‘test’ server?• Locally, in a testing environment?
• What do you test?• Deploy the software and look for errors?• Follow a build / deployment checklist?• Have a ‘focus group’ use the software and file bugs?
© 2014 Pythian
How To TestSyntax / Style Tests to ensure the code is syntactically correct and / or
conforms to ‘style’ best practices for readability and maintainability
Unit Testing Tests that verify the functionality of a specific section of code (usually at the class level, can have multiple tests per class)
Integration Testing Tests that verify the interfaces between components. Progressively larger groups of components are integrated and tested until the software works as a system
System Testing “End-to-end” testing, tests a completely integrated system to verify it meets its requirements
Acceptance Testing The system is delivered to the users for acceptance testing
© 2014 Pythian
Puppet TestsSyntax / Style puppet parser, puppet-syntax, puppet-lint
Unit Tests rspec-puppet
Integration Tests rspec-puppet
System Tests beaker, beaker-rspec
Acceptance Tests beaker, beaker-rspec
© 2014 Pythian
Puppet Parser• You can interact directly with the Puppet parser from the
command line
puppet parser validate [ manifest] [manifest ...]
• Validates Puppet DSL syntax without compiling a catalog or syncing any resources
• Returns: nothing, or the first syntax error encountered
© 2014 Pythian
Puppet Syntax• Ruby gem puppet-syntax• Syntax check of manifests, templates, and Hiera YAML• Works with any version of
• Puppet >= 2.7 that provides the validate face• Ruby >= 1.8 with erb from stdlib
• Add the following to your Rakefilerequire 'puppet-syntax/tasks/puppet-syntax'
• Provides syntax, syntax:hiera, syntax:manifests, and syntax:templates Rake tasks
© 2014 Pythian
Puppet Lint• Ruby gem puppet-lint• Checks conformance of your manifest(s) with the Puppet
Style Guide• Requires Ruby 1.8.7 or higher• Can be run from command line or as a Rake task• Add the following to your Rakefile
require 'puppet-lint/tasks/puppet-lint'
• Also provided as a Rake task if using puppetlabs_spec_helper
• Individual checks can be disabled / ignored if desired
© 2014 Pythian
rspec-puppet• Ruby gem rspec-puppet• Allows you to write RSpec tests for your Puppet manifests• Supports a number of matchers, including:
• compile• contain_*• with_*, without_*• with, without• that_requires, that_comes_before• that_notifies, that_subscribes_to
• Rake tasks provided by puppetlabs_spec_helper
© 2014 Pythian
Beaker• Ruby gems beaker and beaker-rspec• System / acceptance testing harness with its own DSL• Can provision, validate, test, clean up, and report• Supports multiple hypervisors for provisioning, including:
• VMware (Fusion, vSphere, vCloud)• Amazon EC2• Google Compute Engine• Docker• Vagrant
• Run via command line or as RSpec tests
© 2014 Pythian
Beaker (cont)• RSpec tests rely on spec_helper_acceptance.rb• Systems Under Test (SUT) referred to as ‘nodesets’• SUT configuration in YAML in spec/acceptance/nodesets/• Can perform the following actions on SUTs:
• Install, upgrade Puppet• Check for, install or upgrade packages• Copy files in and out, create files, run scripts• Use Puppet, Facter and Hiera• Apply manifests
© 2014 Pythian
puppetlabs_spec_helper• A project (Ruby gem) that intends to provide:
• A bridge between external projects and multiple versions of Puppet, to simplify testing
• A common set of Rake tasks and convenience classes / methods such as common RSpec matchers
• For module testing, add the following to spec_helper.rbrequire 'puppetlabs_spec_helper/module_spec_helper'
• And add the following to your Rakefilerequire 'puppetlabs_spec_helper/rake_tasks'
© 2014 Pythian
Setting Up The Environment• You can do the following:
• Add puppet-lint, puppet-syntax, rspec-puppet and puppetlabs_spec_helper to your Gemfile
• Run bundle install• Add the following to your Rakefile
require 'puppetlabs_spec_helper/rake_tasks'require 'puppet-lint/tasks/puppet-lint'require 'puppet-syntax/tasks/puppet-syntax'
• Create spec/spec_helper.rb• Et cetera, or you can make life easy on yourself ...
© 2014 Pythian
puppet-module-skeleton• Use Gareth Rushgrove’s puppet-module-skeleton• All you need to do:
git clone https://github.com/garethr/puppet-module-skeletoncd puppet-module-skeletonbash install.sh
• Integrates with Puppet Module Tool, so you can do the following:
puppet module generate user-modulecd user-modulebundle installbundle exec rake -T
• Provides all of the Rake tasks mentioned in previous slides
© 2014 Pythian
References1. https://docs.puppetlabs.com/references/latest/man/parser.html2. https://github.com/gds-operations/puppet-syntax3. http://puppet-lint.com/4. http://rspec-puppet.com/5. https://github.com/puppetlabs/beaker6. https://github.com/puppetlabs/beaker-rspec7. https://docs.puppetlabs.com/guides/style_guide.html8. https://github.com/puppetlabs/puppetlabs_spec_helper9. https://github.com/garethr/puppet-module-skeleton
© 2014 Pythian
Thank You - Q&ATo contact us
[email protected], [email protected]
+1 (877) PYTHIAN
To follow ushttp://www.pythian.com/blog
@pythian, @wmjfraser
http://www.linkedin.com/company/pythian,
http://www.linkedin.com/in/wmjfraser
Top Related