summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Schmitt <david@black.co.at>2016-06-30 11:06:56 +0100
committerDavid Schmitt <david@black.co.at>2016-06-30 11:10:24 +0100
commit3f86e3a731b9e1be943a55fa12bd5e32716a20ef (patch)
tree63b7caabab0694c85221c2e8f951e00665d3f5f5
parent70d543e66a9a0fdc670004784f4864e7a04dc4dc (diff)
(MODULES-3543) Fixup defined_with_params to work on all puppet versions
-rw-r--r--lib/puppet/parser/functions/defined_with_params.rb5
-rwxr-xr-xspec/functions/defined_with_params_spec.rb4
-rwxr-xr-xspec/functions/ensure_resource_spec.rb30
3 files changed, 34 insertions, 5 deletions
diff --git a/lib/puppet/parser/functions/defined_with_params.rb b/lib/puppet/parser/functions/defined_with_params.rb
index ffc7241..99687ae 100644
--- a/lib/puppet/parser/functions/defined_with_params.rb
+++ b/lib/puppet/parser/functions/defined_with_params.rb
@@ -26,7 +26,10 @@ ENDOFDOC
ret = false
if resource = findresource(reference.to_s)
matches = params.collect do |key, value|
- resource[key] == (value.eql?(:undef) ? nil : value) # eql? avoids bugs caused by monkeypatching in puppet
+ # eql? avoids bugs caused by monkeypatching in puppet
+ resource_is_undef = resource[key].eql?(:undef) || resource[key].nil?
+ value_is_undef = value.eql?(:undef) || value.nil?
+ (resource_is_undef && value_is_undef) || (resource[key] == value)
end
ret = params.empty? || !matches.include?(false)
end
diff --git a/spec/functions/defined_with_params_spec.rb b/spec/functions/defined_with_params_spec.rb
index e00c423..e2f3abe 100755
--- a/spec/functions/defined_with_params_spec.rb
+++ b/spec/functions/defined_with_params_spec.rb
@@ -26,10 +26,10 @@ describe 'defined_with_params' do
describe 'when passing undef values' do
let :pre_condition do
- 'file { "/tmp/a": }'
+ 'file { "/tmp/a": ensure => present }'
end
it { is_expected.to run.with_params('File[/tmp/a]', {}).and_return(true) }
- it { is_expected.to run.with_params('File[/tmp/a]', { 'owner' => :undef }).and_return(true) }
+ it { is_expected.to run.with_params('File[/tmp/a]', { 'ensure' => 'present', 'owner' => :undef }).and_return(true) }
end
end
diff --git a/spec/functions/ensure_resource_spec.rb b/spec/functions/ensure_resource_spec.rb
index 57b5123..9a17f5b 100755
--- a/spec/functions/ensure_resource_spec.rb
+++ b/spec/functions/ensure_resource_spec.rb
@@ -10,6 +10,32 @@ describe 'ensure_resource' do
is_expected.to run.with_params(1,2,3).and_raise_error(Puppet::ParseError)
}
+ context 'given an empty catalog' do
+ describe 'after running ensure_resource("user", "username1", {})' do
+ before { subject.call(['User', 'username1', {}]) }
+
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_user('username1').without_ensure }
+ end
+
+ describe 'after running ensure_resource("user", "username1", { gid => undef })' do
+ before { subject.call(['User', 'username1', { 'gid' => :undef }]) }
+
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_user('username1').without_ensure }
+ it { expect(lambda { catalogue }).to contain_user('username1').without_gid }
+ end
+
+ describe 'after running ensure_resource("user", "username1", { ensure => present, gid => undef })' do
+ before { subject.call(['User', 'username1', { 'ensure' => 'present', 'gid' => :undef }]) }
+
+ # this lambda is required due to strangeness within rspec-puppet's expectation handling
+ it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
+ it { expect(lambda { catalogue }).to contain_user('username1').without_gid }
+ end
+
+ end
+
context 'given a catalog with "user { username1: ensure => present }"' do
let(:pre_condition) { 'user { username1: ensure => present }' }
@@ -28,8 +54,8 @@ describe 'ensure_resource' do
it { expect(lambda { catalogue }).to contain_user('username2').without_ensure }
end
- describe 'after running ensure_resource("user", "username1", { "gid" => undef })' do
- before { subject.call(['User', 'username1', { "gid" => :undef }]) }
+ describe 'after running ensure_resource("user", "username1", { gid => undef })' do
+ before { subject.call(['User', 'username1', { 'gid' => :undef }]) }
# this lambda is required due to strangeness within rspec-puppet's expectation handling
it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }