Skip to content
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

[MODULES-10781] Fix defined type defined_with_params() #1122

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
13 changes: 10 additions & 3 deletions lib/puppet/parser/functions/defined_with_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,19 @@
[findresource(type, title)]
end

resources.compact.each do |resource|
resources.compact.each do |res|
# If you call this from within a defined type, it will find itself
next if res.to_s == resource.to_s

matches = params.map do |key, value|
# eql? avoids bugs caused by monkeypatching in puppet
resource_is_undef = resource[key].eql?(:undef) || resource[key].nil?
res_is_undef = res[key].eql?(:undef) || res[key].nil?
value_is_undef = value.eql?(:undef) || value.nil?
(resource_is_undef && value_is_undef) || (resource[key] == value)
found_match = (res_is_undef && value_is_undef) || (res[key] == value)

Puppet.debug("Matching resource is #{res}") if found_match

found_match
end
ret = params.empty? || !matches.include?(false)

Expand Down
20 changes: 20 additions & 0 deletions spec/functions/defined_with_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@
}
end

describe 'when called from within a defined type looking for a defined type of the same type' do
let :pre_condition do
<<-PRECOND
define test::deftype(
Optional $port = undef
) {
if defined_with_params(Test::Deftype, { 'port' => $port }) {
fail('Ruh Roh Shaggy')
}
}

test::deftype { 'foo': }
test::deftype { 'bar': port => 200 }
PRECOND
end

# Testing to make sure that the internal logic handles this case via the pre_condition
it { is_expected.to run.with_params('NoOp[noop]', {}).and_return(false) }
end

describe 'when passed a class' do
let :pre_condition do
'class test () { } class { "test": }'
Expand Down