]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/spec/functions/getparam_spec.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / spec / functions / getparam_spec.rb
1 #! /usr/bin/env ruby -S rspec
2 require 'spec_helper'
3 require 'rspec-puppet'
4 require 'puppet_spec/compiler'
5
6 describe 'getparam' do
7   include PuppetSpec::Compiler
8
9   before :each do
10     Puppet::Parser::Functions.autoloader.loadall
11     Puppet::Parser::Functions.function(:getparam)
12   end
13
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
18   else
19     let :scope    do
20       newscope = Puppet::Parser::Scope.new
21       newscope.compiler = compiler
22       newscope.source   = Puppet::Resource::Type.new(:node, :localhost)
23       newscope
24     end
25   end
26
27   it "should exist" do
28     expect(Puppet::Parser::Functions.function("getparam")).to eq("function_getparam")
29   end
30
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 }
38   end
39
40   describe 'when compared against a resource with no params' do
41     let :catalog do
42       compile_to_catalog(<<-EOS
43         user { "dan": }
44       EOS
45       )
46     end
47
48     it do
49       expect(scope.function_getparam(['User[dan]', 'shell'])).to eq('')
50     end
51   end
52
53   describe 'when compared against a resource with params' do
54     let :catalog do
55       compile_to_catalog(<<-EOS
56         user { 'dan': ensure => present, shell => '/bin/sh', managehome => false}
57         $test = getparam(User[dan], 'shell')
58       EOS
59       )
60     end
61
62     it do
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)
68
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('')
74     end
75   end
76 end