File: template_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (97 lines) | stat: -rw-r--r-- 3,495 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'spec_helper'

describe "the template function" do
  before :all do
    Puppet::Parser::Functions.autoloader.loadall
  end

  let :node     do Puppet::Node.new('localhost') end
  let :compiler do Puppet::Parser::Compiler.new(node) end
  let :scope    do Puppet::Parser::Scope.new(compiler) end

  it "concatenates outputs for multiple templates" do
    tw1 = double("template_wrapper1")
    tw2 = double("template_wrapper2")
    allow(Puppet::Parser::TemplateWrapper).to receive(:new).and_return(tw1,tw2)
    allow(tw1).to receive(:file=).with("1")
    allow(tw2).to receive(:file=).with("2")
    allow(tw1).to receive(:result).and_return("result1")
    allow(tw2).to receive(:result).and_return("result2")

    expect(scope.function_template(["1","2"])).to eq("result1result2")
  end

  it "raises an error if the template raises an error" do
    tw = double('template_wrapper')
    allow(tw).to receive(:file=).with("1")
    allow(Puppet::Parser::TemplateWrapper).to receive(:new).and_return(tw)
    allow(tw).to receive(:result).and_raise

    expect {
      scope.function_template(["1"])
    }.to raise_error(Puppet::ParseError, /Failed to parse template/)
  end

  context "when accessing scope variables via method calls (deprecated)" do
    it "raises an error when accessing an undefined variable" do
      expect {
        eval_template("template <%= deprecated %>")
      }.to raise_error(Puppet::ParseError, /undefined local variable or method `deprecated'/)
    end

    it "looks up the value from the scope" do
      scope["deprecated"] = "deprecated value"
      expect { eval_template("template <%= deprecated %>")}.to raise_error(/undefined local variable or method `deprecated'/)
    end

    it "still has access to Kernel methods" do
      expect { eval_template("<%= binding %>") }.to_not raise_error
    end
  end

  context "when accessing scope variables as instance variables" do
    it "has access to values" do
      scope['scope_var'] = "value"
      expect(eval_template("<%= @scope_var %>")).to eq("value")
    end

    it "get nil accessing a variable that does not exist" do
      expect(eval_template("<%= @not_defined.nil? %>")).to eq("true")
    end

    it "get nil accessing a variable that is undef" do
      scope['undef_var'] = :undef
      expect(eval_template("<%= @undef_var.nil? %>")).to eq("true")
    end
  end

  it "is not interfered with by having a variable named 'string' (#14093)" do
    scope['string'] = "this output should not be seen"
    expect(eval_template("some text that is static")).to eq("some text that is static")
  end

  it "has access to a variable named 'string' (#14093)" do
    scope['string'] = "the string value"
    expect(eval_template("string was: <%= @string %>")).to eq("string was: the string value")
  end

  it "does not have direct access to Scope#lookupvar" do
    expect {
      eval_template("<%= lookupvar('myvar') %>")
    }.to raise_error(Puppet::ParseError, /undefined method `lookupvar'/)
  end

  it 'is not available when --tasks is on' do
    Puppet[:tasks] = true
    expect {
      eval_template("<%= lookupvar('myvar') %>")
    }.to raise_error(Puppet::ParseError, /is only available when compiling a catalog/)

  end

  def eval_template(content)
    allow(Puppet::FileSystem).to receive(:read_preserve_line_endings).with("template").and_return(content)
    allow(Puppet::Parser::Files).to receive(:find_template).and_return("template")
    scope.function_template(['template'])
  end
end