1 #! /usr/bin/env ruby -S rspec
4 require 'puppet_spec/compiler'
7 include PuppetSpec::Compiler
10 Puppet::Parser::Functions.autoloader.loadall
11 Puppet::Parser::Functions.function(:getparam)
14 let :node do Puppet::Node.new('localhost') end
15 let :compiler do Puppet::Parser::Compiler.new(node) end
16 if Puppet.version.to_f >= 3.0
17 let :scope do Puppet::Parser::Scope.new(compiler) end
20 newscope = Puppet::Parser::Scope.new
21 newscope.compiler = compiler
22 newscope.source = Puppet::Resource::Type.new(:node, :localhost)
28 expect(Puppet::Parser::Functions.function("getparam")).to eq("function_getparam")
31 describe 'when a resource is not specified' do
32 it { expect { scope.function_getparam([]) }.to raise_error }
33 it { expect { scope.function_getparam(['User[dan]']) }.to raise_error }
34 it { expect { scope.function_getparam(['User[dan]']) }.to raise_error }
35 it { expect { scope.function_getparam(['User[dan]', {}]) }.to raise_error }
36 # This seems to be OK because we just check for a string.
37 it { expect { scope.function_getparam(['User[dan]', '']) }.to_not raise_error }
40 describe 'when compared against a resource with no params' do
42 compile_to_catalog(<<-EOS
49 expect(scope.function_getparam(['User[dan]', 'shell'])).to eq('')
53 describe 'when compared against a resource with params' do
55 compile_to_catalog(<<-EOS
56 user { 'dan': ensure => present, shell => '/bin/sh', managehome => false}
57 $test = getparam(User[dan], 'shell')
63 resource = Puppet::Parser::Resource.new(:user, 'dan', {:scope => scope})
64 resource.set_parameter('ensure', 'present')
65 resource.set_parameter('shell', '/bin/sh')
66 resource.set_parameter('managehome', false)
67 compiler.add_resource(scope, resource)
69 expect(scope.function_getparam(['User[dan]', 'shell'])).to eq('/bin/sh')
70 expect(scope.function_getparam(['User[dan]', ''])).to eq('')
71 expect(scope.function_getparam(['User[dan]', 'ensure'])).to eq('present')
72 # TODO: Expected this to be false, figure out why we're getting '' back.
73 expect(scope.function_getparam(['User[dan]', 'managehome'])).to eq('')