rspec-puppet
repository·master·Indexed 18 days ago
https://github.com/rodjek/rspec-puppetA testing framework for Puppet manifests and modules using RSpec. It enables developers to verify Puppet code by compiling catalogues and inspecting resources, facts, and parameters. The framework includes matchers for verifying resource existence, parameters, resource counts, and relationships, as well as support for testing custom types and type aliases.
What's inside rspec-puppet
- The Bootstrap 3 Lightbox is a module designed for Bootstrap that provides lightbox functionality for images, YouTube videos, and galleries. It is built on top of the standard Bootstrap Modal plugin.
Use let(:params) to provide class parameters
masterWhen testing a Puppet class, use thelet(:params)helper to define the parameters that will be passed to the class during the test execution. This allows you to simulate different configuration states for the class.Understand the purpose of rspec-puppet tests
masterrspec-puppet is not designed to test the result of a manifest on a live system. Instead, it tests the behavior of Puppet when it compiles your manifests into a catalogue of Puppet resources.
Key testing principles:
- Test the Catalogue: Verify that the expected resources (files, packages, services, etc.) are present in the compiled catalogue.
- Test the First Level: When testing a high-level class (e.g., a
webserverrole), only test for the existence of the resources or types it directly includes (e.g.,apache::vhost). Do not test the internal resources of those sub-components; that is the responsibility of the tests for those specific components.
Test custom types and structured facts
masterAs of version 2.3.0,
rspec-puppetprovides support for:- Testing custom Puppet types (includes example groups and matchers).
- Using
:undefvalues in parameters. - Using structured facts with keys as either symbols or strings.
Test recursive resource dependencies
masterStarting from version 2.3.0,
rspec-puppetsupports recursive dependency checking for relationship matchers likebefore,require,subscribe, andnotify. This means if you have a dependency chainFile[a] -> File[b] -> File[c], you can match the relationship between the first and last element directly.Example:
contain_file('a').that_comes_before('File[c]')will now correctly match the chain.Use is_expected.to for class assertions
masterUse theit { is_expected.to ... }syntax within yourdescribeblock to assert that the class meets specific expectations (such as containing certain resources or having specific attributes) based on the provided parameters.Organize Puppet module tests using naming conventions
masterTo ensure tests are automatically placed in the correct groups and have access to custom matchers, follow this directory structure:
module/ ├── manifests/ ├── lib/ └── spec/ ├── spec_helper.rb ├── classes/ │ └── <class_name>_spec.rb ├── defines/ │ └── <define_name>_spec.rb ├── applications/ │ └── <application_name>_spec.rb ├── functions/ │ └── <function_name>_spec.rb ├── types/ │ └── <type_name>_spec.rb ├── type_aliases/ │ └── <type_alias_name>_spec.rb └── hosts/ └── <host_name>_spec.rbmodule/ ├── manifests/ ├── lib/ └── spec/ ├── spec_helper.rb ├── classes/ │ └── <class_name>_spec.rb ├── defines/ │ └── <define_name>_spec.rb ├── applications/ │ └── <application_name>_spec.rb ├── functions/ │ └── <function_name>_spec.rb ├── types/ │ └── <type_name>_spec.rb ├── type_aliases/ │ └── <type_alias_name>_spec.rb └── hosts/ └── <host_name>_spec.rbHow to test Puppet Applications
masterTesting Puppet Applications (available in Puppet >= 4.3.0) requires a specific test structure because applications act like defined types and require a
title.Key requirements for the test setup:
- Node Definition: You must define a
nodevariable so it can be referenced in thenodesparameter. - Title: You must define a
titlefor the application instance. - Node References: The
nodesparameter requires node reference mappings to resource mappings. You must use therefmethod to create these references; passing a standard string will not work. - Single-Node Constraint: Cross-node support is currently unavailable. Ensure your tests are modeled to be single-node to avoid errors.
describe '<application name>' do let(:node) { '<host name>' } let(:title) { '<application instance title>' } let(:params) do { 'nodes' => { ref('Node', node) => ref('<capitalised application name>', title), } # any additional app parameters } end # tests go here end- Node Definition: You must define a
Access the parser scope for complex function testing
masterSome complex functions require access to the current parser's scope (e.g., for stubbing
lookupvar). You can accessscopedirectly in your tests.Note: This approach does not work when testing manifests that use custom functions. For custom functions, you must define a replacement function directly using
Puppet::Parser::Functions.newfunctionwithin abefore(:each)block.# Stubbing scope before(:each) { scope.expects(:lookupvar).with('some_variable').returns('some_value') } # Replacing a custom function before(:each) do Puppet::Parser::Functions.newfunction(:custom_function, :type => :rvalue) { |args| raise ArgumentError, 'expected foobar' unless args[0] == 'foobar' 'expected value' } endGroup elements using the Data API
masterYou can group elements using the
data-match-height="group-name"attribute (or thedata-mhshorthand). All elements sharing the same group name will be set to the same height, regardless of their position in the DOM.Note: When using the Data API,
byRowis automatically enabled.<div data-mh="my-group">My text</div> <div data-mh="my-group">Some other text</div> <div data-mh="my-other-group">Even more text</div> <div data-mh="my-other-group">The last bit of text</div>Test Puppet hosts with rspec-puppet
masterTo test a specific host (node), create a spec file located under
spec/hosts/. The test runner will look for a node definition in yoursite.ppmatching the host name, compile the catalogue for that host, and execute the tests against the resulting catalogue.Ensure you require
spec_helperat the top of your spec file.require 'spec_helper' describe '<host name>' do # your tests go here endOrganize tests for Puppet Types
masterTests for Puppet types must be placed in the
spec/typesdirectory. The filename should follow the pattern<type_name>_spec.rb. For example, a type namedsudoers_entryshould have its tests located atspec/types/sudoers_entry_spec.rb.require 'spec_helper' describe '<type name>' do # tests go here end