From 903d94e10527df8a288138d223e07f816422fa2c Mon Sep 17 00:00:00 2001 From: Hailee Kenney Date: Fri, 2 Dec 2016 14:21:45 +0000 Subject: (MODULES-3829) Add tests for ensure_resources Prior to this commit, we didn't have tests for the ensure_resources function (only for the ensure_resource function). This meant that we weren't catching a bug in the ensure_resources function. In order to prevent this in the future, add some tests which test the specific functionality of ensure_resources. --- spec/fixtures/test/manifests/ensure_resources.pp | 4 ++++ spec/unit/ensure_resources_spec.rb | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 spec/fixtures/test/manifests/ensure_resources.pp create mode 100644 spec/unit/ensure_resources_spec.rb diff --git a/spec/fixtures/test/manifests/ensure_resources.pp b/spec/fixtures/test/manifests/ensure_resources.pp new file mode 100644 index 0000000..bd26002 --- /dev/null +++ b/spec/fixtures/test/manifests/ensure_resources.pp @@ -0,0 +1,4 @@ +# A helper class to test the ensure_resources function +class test::ensure_resources($resource_type, $title_hash, $attributes_hash) { + ensure_resources($resource_type, $title_hash, $attributes_hash) +} diff --git a/spec/unit/ensure_resources_spec.rb b/spec/unit/ensure_resources_spec.rb new file mode 100644 index 0000000..aea723e --- /dev/null +++ b/spec/unit/ensure_resources_spec.rb @@ -0,0 +1,22 @@ +require 'spec_helper' + +describe 'test::ensure_resources', type: :class do + let(:params) {{ resource_type: 'user', title_hash: title_param, attributes_hash: {'ensure' => 'present'} }} + + describe 'given a title hash of multiple resources' do + + let(:title_param) { {'dan' => { 'gid' => 'mygroup', 'uid' => '600' }, 'alex' => { 'gid' => 'mygroup', 'uid' => '700'}} } + + it { is_expected.to compile } + it { is_expected.to contain_user('dan').with({ 'gid' => 'mygroup', 'uid' => '600', 'ensure' => 'present'}) } + it { is_expected.to contain_user('alex').with({ 'gid' => 'mygroup', 'uid' => '700', 'ensure' => 'present'}) } + end + + describe 'given a title hash of a single resource' do + + let(:title_param) { {'dan' => { 'gid' => 'mygroup', 'uid' => '600' }} } + + it { is_expected.to compile } + it { is_expected.to contain_user('dan').with({ 'gid' => 'mygroup', 'uid' => '600', 'ensure' => 'present'}) } + end +end -- cgit v1.2.3 From 3312cc1f44d1acf25ce45701a74cecd647c50858 Mon Sep 17 00:00:00 2001 From: Hailee Kenney Date: Fri, 9 Dec 2016 13:17:48 +0000 Subject: (MODULES-3829) Make ensure_packages work with < 2.0 Prior to this commit, if a hash was passed in as an argument to the ensure_packages function a method of hash duplication would be used that is not supported with versions of ruby older than 2.0. In order to ensure the method is compatible with older ruby versions, switch to a different method of duplication. Additionally add tests to prevent further regressions. --- lib/puppet/parser/functions/ensure_packages.rb | 2 +- spec/functions/ensure_packages_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/puppet/parser/functions/ensure_packages.rb b/lib/puppet/parser/functions/ensure_packages.rb index 532b702..439af1e 100644 --- a/lib/puppet/parser/functions/ensure_packages.rb +++ b/lib/puppet/parser/functions/ensure_packages.rb @@ -25,7 +25,7 @@ third argument to the ensure_resource() function. end Puppet::Parser::Functions.function(:ensure_resources) - function_ensure_resources(['package', Hash(arguments[0]), defaults ]) + function_ensure_resources(['package', arguments[0].dup, defaults ]) else packages = Array(arguments[0]) diff --git a/spec/functions/ensure_packages_spec.rb b/spec/functions/ensure_packages_spec.rb index c824732..5d97684 100755 --- a/spec/functions/ensure_packages_spec.rb +++ b/spec/functions/ensure_packages_spec.rb @@ -33,4 +33,12 @@ describe 'ensure_packages' do it { expect(lambda { catalogue }).to contain_package('facter').with_ensure('present').with_provider("gem") } end end + + context 'given hash of packages' do + before { subject.call([{"foo" => { "provider" => "rpm" }, "bar" => { "provider" => "gem" }}, { "ensure" => "present"}]) } + + # this lambda is required due to strangeness within rspec-puppet's expectation handling + it { expect(lambda { catalogue }).to contain_package('foo').with({'provider' => 'rpm', 'ensure' => 'present'}) } + it { expect(lambda { catalogue }).to contain_package('bar').with({'provider' => 'gem', 'ensure' => 'present'}) } + end end -- cgit v1.2.3