Skip to content

(MODULES-3543) Fixup defined_with_params to work on all puppet versions #615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/puppet/parser/functions/defined_with_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
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
Expand Down
4 changes: 2 additions & 2 deletions spec/functions/defined_with_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

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
30 changes: 28 additions & 2 deletions spec/functions/ensure_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@
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 }' }

Expand All @@ -28,8 +54,8 @@
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') }
Expand Down