From 8a8c09ed34f9917378cd8b7bcc798c21c2975dce Mon Sep 17 00:00:00 2001 From: Chad Metcalf Date: Tue, 18 Sep 2012 11:43:30 -0700 Subject: Add an ensure_packages function. Its often the case that modules need to install a handful of packages. In some cases its worth breaking these dependencies out into their own modules (e.g., Java). In others it makes more sense to keep them in the module. This can be problematic when multiple modules depend on common packages (git, python ruby, etc). ensure_resource was a good first step towards solving this problem. ensure_resource does not handle arrays and for 3 or more packages stamping out ensure_resource declarations is tedious. ensure_packages is a convenience function that takes an array of packages and wraps calls to ensure_resource. Currently users cannot specify package versions. But the function could be extended to use a hash if that functionality would be useful. --- lib/puppet/parser/functions/ensure_packages.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 lib/puppet/parser/functions/ensure_packages.rb diff --git a/lib/puppet/parser/functions/ensure_packages.rb b/lib/puppet/parser/functions/ensure_packages.rb new file mode 100644 index 0000000..450ea02 --- /dev/null +++ b/lib/puppet/parser/functions/ensure_packages.rb @@ -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 : -- cgit v1.2.3 From 616b4521f45edacbdaa5f979e4069f74a64d1356 Mon Sep 17 00:00:00 2001 From: Jeff McCune Date: Tue, 27 Nov 2012 16:15:32 -0800 Subject: Add example behaviors for ensure_packages() function Without this patch the ensure_packages() function has no rspec behavior examples. This patch fixes the problem by filling out a spec file with expected behaviors I could think of. --- spec/functions/ensure_packages_spec.rb | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 spec/functions/ensure_packages_spec.rb diff --git a/spec/functions/ensure_packages_spec.rb b/spec/functions/ensure_packages_spec.rb new file mode 100644 index 0000000..1c2a328 --- /dev/null +++ b/spec/functions/ensure_packages_spec.rb @@ -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 -- cgit v1.2.3 From dbe3c8ecf6379963f583ea11e51e403e5ad55002 Mon Sep 17 00:00:00 2001 From: Jeff McCune Date: Tue, 27 Nov 2012 16:18:07 -0800 Subject: (Maint) Add spec/functions to rake test task Without this patch the `test` rake task does not exercise the rspec-puppet behaviors located in spec/functions/ This is a self-evident problem. This patch fixes the problem by adding spec/functions to the list of directories scanned for spec tests. --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index 01b2a31..cbe8f28 100644 --- a/Rakefile +++ b/Rakefile @@ -5,7 +5,7 @@ task :default => [:test] desc 'Run RSpec' RSpec::Core::RakeTask.new(:test) do |t| - t.pattern = 'spec/{unit}/**/*.rb' + t.pattern = 'spec/{unit,functions}/**/*.rb' t.rspec_opts = ['--color'] end -- cgit v1.2.3