Skip to content

Instantly share code, notes, and snippets.

@stefanozanella
Created December 2, 2012 20:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stefanozanella/4190920 to your computer and use it in GitHub Desktop.
Save stefanozanella/4190920 to your computer and use it in GitHub Desktop.
Puppet implementation patterns: Test with External Function

Test with External Function

When

You want to test a resource definition (class, defined type) that uses a function provided by a third-party module your module depends upon. It's supposed that you're implementing your tests with rspec-puppet.

How

Add the folder spec/fixtures/<third-party module>/lib to Ruby's $LOAD_PATH.

Notes

According to Brice Figureau (@masterzen):

The Puppet::Parser::Functions class in Puppet core has the task of loading all functions defined in any puppet/parser/functions directories it will be able to find in the whole ruby load path.

Relying on rspec-puppet telling Puppet where to look for modules seems not to be enough to also have functions at disposal. In fact, exercising code that uses a third-party function without the additional step described in this pattern results in a Puppet::Error: Unknown function exception.

require 'spec_helper'
describe 'test' do
let(:title) { 'test integer' }
let(:params) {{ :integer => 'clearly not an integer' }}
it 'fails if parameter is not an integer' do
expect { should raise_error(Puppet::Error, /not an integer/) }
end
end
require 'rspec-puppet'
# This will make stdlib functions available when test examples will run
$:.unshift File.join(File.dirname(__FILE__), 'fixtures', 'modules', 'stdlib', 'lib')
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')
end
class test (
$integer
) {
# The is_integer function comes from puppetlabs-stdlib module
if !is_integer($integer) {
fail "not an integer"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment