diff --git a/lib/puppet/parser/functions/defined_with_params.rb b/lib/puppet/parser/functions/defined_with_params.rb index 138330808..fb0cd8c07 100644 --- a/lib/puppet/parser/functions/defined_with_params.rb +++ b/lib/puppet/parser/functions/defined_with_params.rb @@ -31,7 +31,7 @@ type_name, title = Puppet::Resource.type_and_title(reference, nil) type = Puppet::Pops::Evaluator::Runtime3ResourceSupport.find_resource_type_or_class(find_global_scope, type_name.downcase) elsif reference.is_a?(Puppet::Resource) - type = reference.resource_type + type = reference.type title = reference.title else raise(ArgumentError, "Reference is not understood: '#{reference.class}'") diff --git a/spec/acceptance/defined_with_params_spec.rb b/spec/acceptance/defined_with_params_spec.rb index b12927a25..d8e5c8c7f 100644 --- a/spec/acceptance/defined_with_params_spec.rb +++ b/spec/acceptance/defined_with_params_spec.rb @@ -2,7 +2,7 @@ describe 'defined_with_params function' do describe 'success' do - pp = <<-DOC + pp1 = <<-DOC user { 'dan': ensure => present, } @@ -11,10 +11,29 @@ notify { 'User defined with ensure=>present': } } DOC - it 'successfullies notify' do - apply_manifest(pp, :catch_failures => true) do |r| + it 'successfullies checks a type' do + apply_manifest(pp1, :catch_failures => true) do |r| expect(r.stdout).to match(%r{Notice: User defined with ensure=>present}) end end + + pp2 = <<-DOC + class foo ( + $bar, + ) {} + + class { 'foo': + bar => 'baz', + } + + if defined_with_params(Class[foo], { 'bar' => 'baz' }) { + notify { 'Class foo defined with bar=>baz': } + } + DOC + it 'successfullies checks a class' do + apply_manifest(pp2, :catch_failures => true) do |r| + expect(r.stdout).to match(%r{Notice: Class foo defined with bar=>baz}) + end + end end end diff --git a/spec/functions/defined_with_params_spec.rb b/spec/functions/defined_with_params_spec.rb index b78c7e360..74f9a7ccc 100644 --- a/spec/functions/defined_with_params_spec.rb +++ b/spec/functions/defined_with_params_spec.rb @@ -71,4 +71,17 @@ 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) } end + + describe 'when passed a class' do + let :pre_condition do + 'class test () { } class { "test": }' + end + + it { is_expected.to run.with_params('Class[test]', {}).and_return(true) } + it { is_expected.to run.with_params('Class["bar"]', {}).and_return(false) } + it { is_expected.to run.with_params('Class[bar]', {}).and_return(false) } + it { is_expected.to run.with_params(Puppet::Resource.new('class', 'test'), {}).and_return(true) } + it { is_expected.to run.with_params(Puppet::Resource.new('Class["bar"]'), {}).and_return(false) } + it { is_expected.to run.with_params(Puppet::Resource.new('Class[bar]'), {}).and_return(false) } + end end