Skip to content

[MODULES-10729] defined_with_params - unnamed type #1115

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
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
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