File: resource_template_spec.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (57 lines) | stat: -rw-r--r-- 2,160 bytes parent folder | download | duplicates (2)
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
require 'spec_helper'

require 'puppet/util/resource_template'

describe Puppet::Util::ResourceTemplate do
  describe "when initializing" do
    it "should fail if the template does not exist" do
      expect(Puppet::FileSystem).to receive(:exist?).with("/my/template").and_return(false)
      expect { Puppet::Util::ResourceTemplate.new("/my/template", double('resource')) }.to raise_error(ArgumentError)
    end

    it "should not create the ERB template" do
      expect(ERB).not_to receive(:new)
      expect(Puppet::FileSystem).to receive(:exist?).with("/my/template").and_return(true)
      Puppet::Util::ResourceTemplate.new("/my/template", double('resource'))
    end
  end

  describe "when evaluating" do
    before do
      allow(Puppet::FileSystem).to receive(:exist?).and_return(true)
      allow(Puppet::FileSystem).to receive(:read).and_return("eh")

      @template = double('template', :result => nil)
      allow(ERB).to receive(:new).and_return(@template)

      @resource = double('resource')
      @wrapper = Puppet::Util::ResourceTemplate.new("/my/template", @resource)
    end

    it "should set all of the resource's parameters as instance variables" do
      expect(@resource).to receive(:to_hash).and_return(:one => "uno", :two => "dos")
      expect(@template).to receive(:result) do |bind|
        expect(eval("@one", bind)).to eq("uno")
        expect(eval("@two", bind)).to eq("dos")
      end
      @wrapper.evaluate
    end

    it "should create a template instance with the contents of the file" do
      expect(Puppet::FileSystem).to receive(:read).with("/my/template", {:encoding => 'utf-8'}).and_return("yay")
      expect(Puppet::Util).to receive(:create_erb).with("yay").and_return(@template)

      allow(@wrapper).to receive(:set_resource_variables)

      @wrapper.evaluate
    end

    it "should return the result of the template" do
      allow(@wrapper).to receive(:set_resource_variables)

      expect(@wrapper).to receive(:binding).and_return("mybinding")
      expect(@template).to receive(:result).with("mybinding").and_return("myresult")
      expect(@wrapper.evaluate).to eq("myresult")
    end
  end
end