summaryrefslogtreecommitdiff
path: root/spec/unit/provider/template/content_spec.rb
blob: 60ff18c123f9cd34b6b9c1f254d97c402dabaf81 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#
# Author:: Lamont Granquist (<lamont@chef.io>)
# Copyright:: Copyright 2013-2016, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "spec_helper"

describe Chef::Provider::Template::Content do

  let(:enclosing_directory) do
    canonicalize_path(Dir.mktmpdir)
  end

  let(:resource_path) do
    canonicalize_path(File.expand_path(File.join(enclosing_directory, "openldap_stuff.conf")))
  end

  let(:new_resource) do
    double("Chef::Resource::Template (new)",
      cookbook_name: "openldap",
      recipe_name: "default",
      source_line: "/Users/lamont/solo/cookbooks/openldap/recipes/default.rb:2:in `from_file'",
      source_line_file: "/Users/lamont/solo/cookbooks/openldap/recipes/default.rb",
      source_line_number: "2",
      source: "openldap_stuff.conf.erb",
      name: "openldap_stuff.conf",
      path: resource_path,
      local: false,
      cookbook: nil,
      variables: {},
      inline_helper_blocks: {},
      inline_helper_modules: [],
      helper_modules: [])
  end

  let(:rendered_file_locations) do
    [Dir.tmpdir + "/openldap_stuff.conf",
     enclosing_directory + "/openldap_stuff.conf"]
  end

  let(:run_context) do
    cookbook_repo = File.expand_path(File.join(CHEF_SPEC_DATA, "cookbooks"))
    Chef::Cookbook::FileVendor.fetch_from_disk(cookbook_repo)
    cl = Chef::CookbookLoader.new(cookbook_repo)
    cl.load_cookbooks
    cookbook_collection = Chef::CookbookCollection.new(cl)
    node = Chef::Node.new
    double("Chef::Resource::RunContext", node: node, cookbook_collection: cookbook_collection)
  end

  let(:content) do
    current_resource = double("Chef::Resource::Template (current)")
    Chef::Provider::Template::Content.new(new_resource, current_resource, run_context)
  end

  after do
    rendered_file_locations.each do |file|
      FileUtils.rm(file) if ::File.exist?(file)
    end
  end

  it "finds the template file in the cookbook cache if it isn't local" do
    expect(content.template_location).to eq(CHEF_SPEC_DATA + "/cookbooks/openldap/templates/default/openldap_stuff.conf.erb")
  end

  it "finds the template file locally if it is local" do
    allow(new_resource).to receive(:local).and_return(true)
    allow(new_resource).to receive(:source).and_return("/tmp/its_on_disk.erb")
    expect(content.template_location).to eq("/tmp/its_on_disk.erb")
  end

  it "should use the cookbook name if defined in the template resource" do
    allow(new_resource).to receive(:cookbook_name).and_return("apache2")
    allow(new_resource).to receive(:cookbook).and_return("openldap")
    allow(new_resource).to receive(:source).and_return("test.erb")
    expect(content.template_location).to eq(CHEF_SPEC_DATA + "/cookbooks/openldap/templates/default/test.erb")
  end

  it "returns a tempfile in the tempdir when :file_staging_uses_destdir is not set" do
    Chef::Config[:file_staging_uses_destdir] = false
    expect(content.tempfile.path.start_with?(Dir.tmpdir)).to be true
    expect(canonicalize_path(content.tempfile.path).start_with?(enclosing_directory)).to be false
  end

  it "returns a tempfile in the destdir when :file_staging_uses_destdir is set" do
    Chef::Config[:file_staging_uses_destdir] = true
    expect(canonicalize_path(content.tempfile.path).start_with?(enclosing_directory)).to be true
  end

  context "when creating a tempfile in destdir fails" do
    let(:enclosing_directory) do
      canonicalize_path("/nonexisting/path")
    end

    it "returns a tempfile in the tempdir when :file_deployment_uses_destdir is set to :auto" do
      Chef::Config[:file_staging_uses_destdir] = :auto
      expect(content.tempfile.path.start_with?(Dir.tmpdir)).to be true
      expect(canonicalize_path(content.tempfile.path).start_with?(enclosing_directory)).to be false
    end

    it "fails when :file_desployment_uses_destdir is set" do
      Chef::Config[:file_staging_uses_destdir] = true
      expect { content.tempfile }.to raise_error(Chef::Exceptions::FileContentStagingError)
    end

    it "returns a tempfile in the tempdir when :file_desployment_uses_destdir is not set" do
      expect(content.tempfile.path.start_with?(Dir.tmpdir)).to be true
      expect(canonicalize_path(content.tempfile.path).start_with?(enclosing_directory)).to be false
    end
  end

  it "creates the template with the rendered content" do
    run_context.node.normal[:slappiness] = "a warm gun"
    expect(IO.read(content.tempfile.path)).to eq("slappiness is a warm gun")
  end

  describe "when using location helpers" do
    let(:new_resource) do
      double("Chef::Resource::Template (new)",
        cookbook_name: "openldap",
        recipe_name: "default",
        source_line: CHEF_SPEC_DATA + "/cookbooks/openldap/recipes/default.rb:2:in `from_file'",
        source_line_file: CHEF_SPEC_DATA + "/cookbooks/openldap/recipes/default.rb",
        source_line_number: "2",
        source: "helpers.erb",
        name: "helpers.erb",
        path: CHEF_SPEC_DATA + "/cookbooks/openldap/templates/default/helpers.erb",
        local: false,
        cookbook: nil,
        variables: {},
        inline_helper_blocks: {},
        inline_helper_modules: [],
        helper_modules: [])
    end

    it "creates the template with the rendered content" do
      expect(IO.read(content.tempfile.path)).to eql <<~EOF
        openldap
        default
        #{CHEF_SPEC_DATA}/cookbooks/openldap/recipes/default.rb:2:in `from_file'
        #{CHEF_SPEC_DATA}/cookbooks/openldap/recipes/default.rb
        2
        helpers.erb
        #{CHEF_SPEC_DATA}/cookbooks/openldap/templates/default/helpers.erb
        openldap
        default
        #{CHEF_SPEC_DATA}/cookbooks/openldap/recipes/default.rb:2:in `from_file'
        #{CHEF_SPEC_DATA}/cookbooks/openldap/recipes/default.rb
        2
        helpers.erb
        #{CHEF_SPEC_DATA}/cookbooks/openldap/templates/default/helpers.erb
      EOF
    end

  end
end