summaryrefslogtreecommitdiff
path: root/chef-config/spec/unit/workstation_config_loader_spec.rb
blob: 77e573c776c7d7574d9f464e82dd2fc9bcd22881 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#
# Author:: Daniel DeLeo (<dan@getchef.com>)
# Copyright:: Copyright (c) 2014 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"
require "tempfile"

require "chef-config/exceptions"
require "chef-config/windows"
require "chef-config/workstation_config_loader"

RSpec.describe ChefConfig::WorkstationConfigLoader do

  let(:explicit_config_location) { nil }

  let(:env) { {} }

  let(:config_loader) do
    described_class.new(explicit_config_location).tap do |c|
      allow(c).to receive(:env).and_return(env)
    end
  end

  # Test methods that do I/O or reference external state which are stubbed out
  # elsewhere.
  describe "external dependencies" do
    let(:config_loader) { described_class.new(nil) }

    it "delegates to ENV for env" do
      expect(config_loader.env).to equal(ENV)
    end

    it "tests a path's existence" do
      expect(config_loader.path_exists?("/nope/nope/nope/nope/frab/jab/nab")).to be(false)
      expect(config_loader.path_exists?(__FILE__)).to be(true)
    end

  end

  describe "locating the config file" do
    context "without an explicit config" do

      before do
        allow(config_loader).to receive(:path_exists?).with(an_instance_of(String)).and_return(false)
      end

      it "has no config if HOME is not set" do
        expect(config_loader.config_location).to be(nil)
        expect(config_loader.no_config_found?).to be(true)
      end

      context "when HOME is set and contains a knife.rb" do

        let(:home) { "/Users/example.user" }

        before do
          allow(ChefConfig::PathHelper).to receive(:home).with(".chef").and_yield(File.join(home, ".chef"))
          allow(config_loader).to receive(:path_exists?).with("#{home}/.chef/knife.rb").and_return(true)
        end

        it "uses the config in HOME/.chef/knife.rb" do
          expect(config_loader.config_location).to eq("#{home}/.chef/knife.rb")
        end

        context "and has a config.rb" do

          before do
            allow(config_loader).to receive(:path_exists?).with("#{home}/.chef/config.rb").and_return(true)
          end

          it "uses the config in HOME/.chef/config.rb" do
            expect(config_loader.config_location).to eq("#{home}/.chef/config.rb")
          end

          context "and/or a parent dir contains a .chef dir" do

            let(:env_pwd) { "/path/to/cwd" }

            before do
              if ChefConfig.windows?
                env["CD"] = env_pwd
              else
                env["PWD"] = env_pwd
              end

              allow(config_loader).to receive(:path_exists?).with("#{env_pwd}/.chef/knife.rb").and_return(true)
              allow(File).to receive(:exist?).with("#{env_pwd}/.chef").and_return(true)
              allow(File).to receive(:directory?).with("#{env_pwd}/.chef").and_return(true)
            end

            it "prefers the config from parent_dir/.chef" do
              expect(config_loader.config_location).to eq("#{env_pwd}/.chef/knife.rb")
            end

            context "and the parent dir's .chef dir has a config.rb" do

              before do
                allow(config_loader).to receive(:path_exists?).with("#{env_pwd}/.chef/config.rb").and_return(true)
              end

              it "prefers the config from parent_dir/.chef" do
                expect(config_loader.config_location).to eq("#{env_pwd}/.chef/config.rb")
              end

              context "and/or the current working directory contains a .chef dir" do

                let(:cwd) { Dir.pwd }

                before do
                  allow(config_loader).to receive(:path_exists?).with("#{cwd}/knife.rb").and_return(true)
                end

                it "prefers a knife.rb located in the cwd" do
                  expect(config_loader.config_location).to eq("#{cwd}/knife.rb")
                end

                context "and the CWD's .chef dir has a config.rb" do

                  before do
                    allow(config_loader).to receive(:path_exists?).with("#{cwd}/config.rb").and_return(true)
                  end

                  it "prefers a config located in the cwd" do
                    expect(config_loader.config_location).to eq("#{cwd}/config.rb")
                  end


                  context "and/or KNIFE_HOME is set" do

                    let(:knife_home) { "/path/to/knife/home" }

                    before do
                      env["KNIFE_HOME"] = knife_home
                      allow(config_loader).to receive(:path_exists?).with("#{knife_home}/knife.rb").and_return(true)
                    end

                    it "prefers a knife located in KNIFE_HOME" do
                      expect(config_loader.config_location).to eq("/path/to/knife/home/knife.rb")
                    end

                    context "and KNIFE_HOME contains a config.rb" do

                      before do
                        env["KNIFE_HOME"] = knife_home
                        allow(config_loader).to receive(:path_exists?).with("#{knife_home}/config.rb").and_return(true)
                      end

                      it "prefers a config.rb located in KNIFE_HOME" do
                        expect(config_loader.config_location).to eq("/path/to/knife/home/config.rb")
                      end

                    end

                  end
                end
              end
            end
          end
        end
      end

      context "when the current working dir is inside a symlinked directory" do
        before do
          # pwd according to your shell is /home/someuser/prod/chef-repo, but
          # chef-repo is a symlink to /home/someuser/codes/chef-repo
          env["CD"] = "/home/someuser/prod/chef-repo" # windows
          env["PWD"] = "/home/someuser/prod/chef-repo" # unix

          allow(Dir).to receive(:pwd).and_return("/home/someuser/codes/chef-repo")
        end

        it "loads the config from the non-dereferenced directory path" do
          expect(File).to receive(:exist?).with("/home/someuser/prod/chef-repo/.chef").and_return(false)
          expect(File).to receive(:exist?).with("/home/someuser/prod/.chef").and_return(true)
          expect(File).to receive(:directory?).with("/home/someuser/prod/.chef").and_return(true)

          expect(config_loader).to receive(:path_exists?).with("/home/someuser/prod/.chef/knife.rb").and_return(true)

          expect(config_loader.config_location).to eq("/home/someuser/prod/.chef/knife.rb")
        end
      end
    end

    context "when given an explicit config to load" do

      let(:explicit_config_location) { "/path/to/explicit/config.rb" }

      it "prefers the explicit config" do
        expect(config_loader.config_location).to eq(explicit_config_location)
      end

    end
  end


  describe "loading the config file" do

    context "when no explicit config is specifed and no implicit config is found" do

      before do
        allow(config_loader).to receive(:path_exists?).with(an_instance_of(String)).and_return(false)
      end

      it "skips loading" do
        expect(config_loader.config_location).to be(nil)
        expect(config_loader.load).to be(false)
      end

    end

    context "when an explicit config is given but it doesn't exist" do

      let(:explicit_config_location) { "/nope/nope/nope/frab/jab/nab" }

      it "raises a configuration error" do
        expect { config_loader.load }.to raise_error(ChefConfig::ConfigurationError)
      end

    end

    context "when the config file exists" do

      let(:config_content) { "" }

      # We need to keep a reference to the tempfile because while #close does
      # not unlink the file, the object being GC'd will.
      let(:tempfile) do
        Tempfile.new("Chef-WorkstationConfigLoader-rspec-test").tap do |t|
          t.print(config_content)
          t.close
        end
      end

      let(:explicit_config_location) do
        tempfile.path
      end

      after { File.unlink(explicit_config_location) if File.exist?(explicit_config_location) }

      context "and is valid" do

        let(:config_content) { "config_file_evaluated(true)" }

        it "loads the config" do
          expect(config_loader.load).to be(true)
          expect(ChefConfig::Config.config_file_evaluated).to be(true)
        end

        it "sets ChefConfig::Config.config_file" do
          config_loader.load
          expect(ChefConfig::Config.config_file).to eq(explicit_config_location)
        end
      end

      context "and has a syntax error" do

        let(:config_content) { "{{{{{:{{" }

        it "raises a ConfigurationError" do
          expect { config_loader.load }.to raise_error(ChefConfig::ConfigurationError)
        end
      end

      context "and raises a ruby exception during evaluation" do

        let(:config_content) { ":foo\n:bar\nraise 'oops'\n:baz\n" }

        it "raises a ConfigurationError" do
          expect { config_loader.load }.to raise_error(ChefConfig::ConfigurationError)
        end
      end

    end

  end

end