/puppet-reference

Reference modules for puppet best practices and patterns.

puppet-reference

A list of puppet modules, tools, testing, and architecture that are good references for current Puppet best practices.

Puppet 4

  • Puppet Tea - Custom Defined Types that can be used to shorten the parameter list definitions and/or when complex types are used in multiple places.

Tools

Editor support

Testing

The modules below each highlight one or more aspects of rspec-puppet testing.

Beaker

Types & Providers

Custom Facts

Mocking an Object like File.exists?

Mocking a puppet 4 function

If you have a puppet 4 function that is called and you don't want it to execute and want to create a mock/fake to be used in the real one's place you can use the puppet-rpsec precondition. This function definition will override will be added to the loader and the original function will not be attempted to be loaded from disk since there already exists the function, your mock!

# Puppet code that calls puppet 4 function
class profile::sqlserver {

  $iso_location = 'C:/SW_DVD9_SQL_Svr_Ent_Core_2012_English_MLF_X17-99682.IMG'
  googlecloudstorage::gcpsignedfile { $iso_location:
    ensure     => 'present',
    signed_url => googlecloudstorage::generate_signed_url('20m', '/etc/puppetlabs/puppetserver/ssh/Terraform-puppet-credentials.json', 'gs://puppet-provisioning-binaries/SQL Server 2012 - Enterprise Edition/SW_DVD9_SQL_Svr_Ent_Core_2012_English_MLF_X17-99682.IMG'),
    # signed_url => 'https://asdasdasd.com/file.iso',
    mounted    => true,
  }
}
# Puppet 4 function
# @param [String] duration
# Example use case
#   
# with s = seconds, m = minutes, h = hours, d = days
require 'open3'
Puppet::Functions.create_function(:'googlecloudstorage::generate_signed_url') do
  # Signatures
  dispatch :generate_signed_url do
    required_param 'Pattern[/(s)|(m)|(h)|(d)$/]', :duration
    required_param 'String', :private_key_file
    required_param 'String', :google_cloud_storage_url
    return_type 'String'
  end

  # Implementations
  def generate_signed_url(duration, private_key_file, google_cloud_storage_url)
    Puppet.info("duration: #{duration}")
    Puppet.info("private_key_file: #{private_key_file}")
    Puppet.info("google_cloud_storage_url: #{google_cloud_storage_url}")
    stdout, stderr, status = Open3.capture3("gsutil signurl -d #{duration} '#{private_key_file}' '#{google_cloud_storage_url}'")
    if not status.success?
      raise Puppet::ParseError, "gcloud failure: exitcode #{status.exitstatus} stdout #{stdout} stderr #{stderr}"  
    end
    return stdout.split(' ')[-1]
  end
end
# Test
require 'spec_helper'

describe 'profile::sqlserver' do
  context 'with default values for all parameters' do
    let(:pre_condition) {
      'function googlecloudstorage::generate_signed_url($x, $y, $z) { return \'https://asdasdasd.com/file.iso\' }'
    }

     it { should contain_googlecloudstorage__gcpsignedfile('C:/SW_DVD9_SQL_Svr_Ent_Core_2012_English_MLF_X17-99682.IMG').with({
      :ensure     => 'present',
      :signed_url => 'https://asdasdasd.com/file.iso',
    }) }
  end
end

Including dependent classes

Template Results

Templates are often fed values by class parameters. Test for portions of the content based on the values you expect to find with various parameter settings, rather than testing the entire contents.

Architecture

Control Repositories

A Control Repository is used to control the code deployed in Puppet environments. Puppet has two official reference repositories, and there are some public repositories that are a mix of reference architecture and practical usage.

Other