Skip to content

Commit

Permalink
[MODULES-10729] defined_with_params - unnamed type
Browse files Browse the repository at this point in the history
This fixes an issue where trying to match against any declared defined
type with a specific parameter would never return that there was a match
in the catalog.

This is relevant when trying to ensure that you don't have port
conflicts in services that can declare multiple instances of a service
through a given defined type, such as Apache.

This, previously valid call to the function now works as expected:

``defined_with_params(My::DefinedType, { 'port' => 1337 })``

MODULES-10729 #close
  • Loading branch information
trevor-vaughan committed Jul 8, 2020
1 parent 88ffa24 commit 733320e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
15 changes: 12 additions & 3 deletions lib/puppet/parser/functions/defined_with_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,25 @@
title = nil
end

resource = findresource(type, title)
if resource
resources = if title.empty?
catalog.resources.select { |r| r.type == type }
else
[findresource(type, title)]
end

resources.compact.each do |resource|
matches = params.map do |key, value|
# 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)

break if ret
end
Puppet.debug("Resource #{reference} was not determined to be defined")

Puppet.debug("Resource #{reference} was not determined to be defined") unless ret

ret
end
26 changes: 24 additions & 2 deletions spec/functions/defined_with_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,35 @@

describe 'when passed a defined type' do
let :pre_condition do
'define test::deftype() { } test::deftype { "foo": }'
<<-PRECOND
define test::deftype(
Optional $port = undef
) { }
test::deftype { "foo": }
test::deftype { "baz": port => 100 }
test::deftype { "adv": port => 200 }
test::deftype { "adv2": port => 200 }
# Unsure how to stub this out below properly
if defined_with_params(Test::Deftype, { 'port' => 200 }) {
notify { 'Duplicate found somewhere': }
}
if defined_with_params(Test::Deftype, { 'port' => 'nope' }) {
notify { 'Should not find me': }
}
PRECOND
end

it { is_expected.to run.with_params('Test::Deftype[foo]', {}).and_return(true) }
it { is_expected.to run.with_params('Test::Deftype[bar]', {}).and_return(false) }
it { is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[foo]'), {}).and_return(true) }
it { is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[bar]'), {}).and_return(false) }
it {
is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[bar]'), {}).and_return(false)

expect(catalogue.resource('Notify[Duplicate found somewhere]')).not_to be_nil
expect(catalogue.resource('Notify[Should not find me]')).to be_nil
}
end

describe 'when passed a class' do
Expand Down

0 comments on commit 733320e

Please sign in to comment.