Skip to content

Commit 05376de

Browse files
Merge pull request puppetlabs#1123 from puppetlabs/master
(maint) - Merge PRs mistakenly merged into master into main
2 parents 7a6e346 + 7c1ae25 commit 05376de

2 files changed

Lines changed: 65 additions & 7 deletions

File tree

lib/puppet/parser/functions/defined_with_params.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,32 @@
4848
title = nil
4949
end
5050

51-
resource = findresource(type, title)
52-
if resource
51+
resources = if title.empty?
52+
catalog.resources.select { |r| r.type == type }
53+
else
54+
[findresource(type, title)]
55+
end
56+
57+
resources.compact.each do |res|
58+
# If you call this from within a defined type, it will find itself
59+
next if res.to_s == resource.to_s
60+
5361
matches = params.map do |key, value|
5462
# eql? avoids bugs caused by monkeypatching in puppet
55-
resource_is_undef = resource[key].eql?(:undef) || resource[key].nil?
63+
res_is_undef = res[key].eql?(:undef) || res[key].nil?
5664
value_is_undef = value.eql?(:undef) || value.nil?
57-
(resource_is_undef && value_is_undef) || (resource[key] == value)
65+
found_match = (res_is_undef && value_is_undef) || (res[key] == value)
66+
67+
Puppet.debug("Matching resource is #{res}") if found_match
68+
69+
found_match
5870
end
5971
ret = params.empty? || !matches.include?(false)
72+
73+
break if ret
6074
end
61-
Puppet.debug("Resource #{reference} was not determined to be defined")
75+
76+
Puppet.debug("Resource #{reference} was not determined to be defined") unless ret
77+
6278
ret
6379
end

spec/functions/defined_with_params_spec.rb

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,55 @@
6363

6464
describe 'when passed a defined type' do
6565
let :pre_condition do
66-
'define test::deftype() { } test::deftype { "foo": }'
66+
<<-PRECOND
67+
define test::deftype(
68+
Optional $port = undef
69+
) { }
70+
71+
test::deftype { "foo": }
72+
test::deftype { "baz": port => 100 }
73+
test::deftype { "adv": port => 200 }
74+
test::deftype { "adv2": port => 200 }
75+
76+
# Unsure how to stub this out below properly
77+
if defined_with_params(Test::Deftype, { 'port' => 200 }) {
78+
notify { 'Duplicate found somewhere': }
79+
}
80+
if defined_with_params(Test::Deftype, { 'port' => 'nope' }) {
81+
notify { 'Should not find me': }
82+
}
83+
PRECOND
6784
end
6885

6986
it { is_expected.to run.with_params('Test::Deftype[foo]', {}).and_return(true) }
7087
it { is_expected.to run.with_params('Test::Deftype[bar]', {}).and_return(false) }
7188
it { is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[foo]'), {}).and_return(true) }
72-
it { is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[bar]'), {}).and_return(false) }
89+
it {
90+
is_expected.to run.with_params(Puppet::Resource.new('Test::Deftype[bar]'), {}).and_return(false)
91+
92+
expect(catalogue.resource('Notify[Duplicate found somewhere]')).not_to be_nil
93+
expect(catalogue.resource('Notify[Should not find me]')).to be_nil
94+
}
95+
end
96+
97+
describe 'when called from within a defined type looking for a defined type of the same type' do
98+
let :pre_condition do
99+
<<-PRECOND
100+
define test::deftype(
101+
Optional $port = undef
102+
) {
103+
if defined_with_params(Test::Deftype, { 'port' => $port }) {
104+
fail('Ruh Roh Shaggy')
105+
}
106+
}
107+
108+
test::deftype { 'foo': }
109+
test::deftype { 'bar': port => 200 }
110+
PRECOND
111+
end
112+
113+
# Testing to make sure that the internal logic handles this case via the pre_condition
114+
it { is_expected.to run.with_params('NoOp[noop]', {}).and_return(false) }
73115
end
74116

75117
describe 'when passed a class' do

0 commit comments

Comments
 (0)