Merge branch '4.x'

* 4.x:
  (Maint) Add spec/functions to rake test task
  Add example behaviors for ensure_packages() function
  Add an ensure_packages function.
This commit is contained in:
Jeff McCune 2012-11-27 16:22:26 -08:00
commit 4f91efa56e
2 changed files with 66 additions and 0 deletions

View file

@ -0,0 +1,24 @@
#
# ensure_packages.rb
#
require 'puppet/parser/functions'
module Puppet::Parser::Functions
newfunction(:ensure_packages, :type => :statement, :doc => <<-EOS
Takes a list of packages and only installs them if they don't already exist.
EOS
) do |arguments|
raise(Puppet::ParseError, "ensure_packages(): Wrong number of arguments " +
"given (#{arguments.size} for 1)") if arguments.size != 1
raise(Puppet::ParseError, "ensure_packages(): Requires array " +
"given (#{arguments[0].class})") if !arguments[0].kind_of?(Array)
Puppet::Parser::Functions.function(:ensure_resource)
arguments[0].each { |package_name|
function_ensure_resource(['package', package_name, {'ensure' => 'present' } ])
}
end
end
# vim: set ts=2 sw=2 et :

View file

@ -0,0 +1,42 @@
#! /usr/bin/env ruby
require 'spec_helper'
require 'rspec-puppet'
describe 'ensure_packages' do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
describe 'argument handling' do
it 'fails with no arguments' do
should run.with_params().and_raise_error(Puppet::ParseError)
end
it 'requires an array' do
lambda { scope.function_ensure_packages([['foo']]) }.should_not raise_error
end
it 'fails when given a string' do
should run.with_params('foo').and_raise_error(Puppet::ParseError)
end
end
context 'given a catalog containing Package[puppet]{ensure => absent}' do
let :pre_condition do
'package { puppet: ensure => absent }'
end
# NOTE: should run.with_params has the side effect of making the compiler
# available to the test harness.
it 'has no effect on Package[puppet]' do
should run.with_params(['puppet'])
rsrc = compiler.catalog.resource('Package[puppet]')
rsrc.to_hash.should == {:ensure => "absent"}
end
end
context 'given a clean catalog' do
it 'declares package resources with ensure => present' do
should run.with_params(['facter'])
rsrc = compiler.catalog.resource('Package[facter]')
rsrc.to_hash.should == {:name => "facter", :ensure => "present"}
end
end
end