Skip to content

Commit 6bab96e

Browse files
authored
Merge pull request #615 from DavidS/modules-3543-update-tests-and-function
(MODULES-3543) Fixup defined_with_params to work on all puppet versions
2 parents 70d543e + 3f86e3a commit 6bab96e

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

lib/puppet/parser/functions/defined_with_params.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
ret = false
2727
if resource = findresource(reference.to_s)
2828
matches = params.collect do |key, value|
29-
resource[key] == (value.eql?(:undef) ? nil : value) # eql? avoids bugs caused by monkeypatching in puppet
29+
# eql? avoids bugs caused by monkeypatching in puppet
30+
resource_is_undef = resource[key].eql?(:undef) || resource[key].nil?
31+
value_is_undef = value.eql?(:undef) || value.nil?
32+
(resource_is_undef && value_is_undef) || (resource[key] == value)
3033
end
3134
ret = params.empty? || !matches.include?(false)
3235
end

spec/functions/defined_with_params_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626

2727
describe 'when passing undef values' do
2828
let :pre_condition do
29-
'file { "/tmp/a": }'
29+
'file { "/tmp/a": ensure => present }'
3030
end
3131

3232
it { is_expected.to run.with_params('File[/tmp/a]', {}).and_return(true) }
33-
it { is_expected.to run.with_params('File[/tmp/a]', { 'owner' => :undef }).and_return(true) }
33+
it { is_expected.to run.with_params('File[/tmp/a]', { 'ensure' => 'present', 'owner' => :undef }).and_return(true) }
3434
end
3535
end

spec/functions/ensure_resource_spec.rb

+28-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,32 @@
1010
is_expected.to run.with_params(1,2,3).and_raise_error(Puppet::ParseError)
1111
}
1212

13+
context 'given an empty catalog' do
14+
describe 'after running ensure_resource("user", "username1", {})' do
15+
before { subject.call(['User', 'username1', {}]) }
16+
17+
# this lambda is required due to strangeness within rspec-puppet's expectation handling
18+
it { expect(lambda { catalogue }).to contain_user('username1').without_ensure }
19+
end
20+
21+
describe 'after running ensure_resource("user", "username1", { gid => undef })' do
22+
before { subject.call(['User', 'username1', { 'gid' => :undef }]) }
23+
24+
# this lambda is required due to strangeness within rspec-puppet's expectation handling
25+
it { expect(lambda { catalogue }).to contain_user('username1').without_ensure }
26+
it { expect(lambda { catalogue }).to contain_user('username1').without_gid }
27+
end
28+
29+
describe 'after running ensure_resource("user", "username1", { ensure => present, gid => undef })' do
30+
before { subject.call(['User', 'username1', { 'ensure' => 'present', 'gid' => :undef }]) }
31+
32+
# this lambda is required due to strangeness within rspec-puppet's expectation handling
33+
it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }
34+
it { expect(lambda { catalogue }).to contain_user('username1').without_gid }
35+
end
36+
37+
end
38+
1339
context 'given a catalog with "user { username1: ensure => present }"' do
1440
let(:pre_condition) { 'user { username1: ensure => present }' }
1541

@@ -28,8 +54,8 @@
2854
it { expect(lambda { catalogue }).to contain_user('username2').without_ensure }
2955
end
3056

31-
describe 'after running ensure_resource("user", "username1", { "gid" => undef })' do
32-
before { subject.call(['User', 'username1', { "gid" => :undef }]) }
57+
describe 'after running ensure_resource("user", "username1", { gid => undef })' do
58+
before { subject.call(['User', 'username1', { 'gid' => :undef }]) }
3359

3460
# this lambda is required due to strangeness within rspec-puppet's expectation handling
3561
it { expect(lambda { catalogue }).to contain_user('username1').with_ensure('present') }

0 commit comments

Comments
 (0)