summaryrefslogtreecommitdiff
path: root/spec/unit/config_spec.rb
blob: 41411669e6f0a574519a13e6f727dfee6fd76f5d (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#
# Author:: Adam Jacob (<adam@opscode.com>)
# Author:: Kyle Goodwin (<kgoodwin@primerevenue.com>)
# Copyright:: Copyright (c) 2008 Opscode, 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 'chef/exceptions'
require 'chef/util/path_helper'

describe Chef::Config do
  describe "config attribute writer: chef_server_url" do
    before do
      Chef::Config.chef_server_url = "https://junglist.gen.nz"
    end

    it "sets the server url" do
      Chef::Config.chef_server_url.should == "https://junglist.gen.nz"
    end

    context "when the url has a leading space" do
      before do
        Chef::Config.chef_server_url = " https://junglist.gen.nz"
      end

      it "strips the space from the url when setting" do
        Chef::Config.chef_server_url.should == "https://junglist.gen.nz"
      end

    end

    context "when the url is a frozen string" do
      before do
        Chef::Config.chef_server_url = " https://junglist.gen.nz".freeze
      end

      it "strips the space from the url when setting without raising an error" do
        Chef::Config.chef_server_url.should == "https://junglist.gen.nz"
      end
    end

  end

  describe "when configuring formatters" do
      # if TTY and not(force-logger)
      #   formatter = configured formatter or default formatter
      #   formatter goes to STDOUT/ERR
      #   if log file is writeable
      #     log level is configured level or info
      #     log location is file
      #   else
      #     log level is warn
      #     log location is STDERR
      #    end
      # elsif not(TTY) and force formatter
      #   formatter = configured formatter or default formatter
      #   if log_location specified
      #     formatter goes to log_location
      #   else
      #     formatter goes to STDOUT/ERR
      #   end
      # else
      #   formatter = "null"
      #   log_location = configured-value or defualt
      #   log_level = info or defualt
      # end
      #
    it "has an empty list of formatters by default" do
      Chef::Config.formatters.should == []
    end

    it "configures a formatter with a short name" do
      Chef::Config.add_formatter(:doc)
      Chef::Config.formatters.should == [[:doc, nil]]
    end

    it "configures a formatter with a file output" do
      Chef::Config.add_formatter(:doc, "/var/log/formatter.log")
      Chef::Config.formatters.should == [[:doc, "/var/log/formatter.log"]]
    end

  end

  describe "class method: manage_secret_key" do
    before do
      Chef::FileCache.stub(:load).and_return(true)
      Chef::FileCache.stub(:has_key?).with("chef_server_cookie_id").and_return(false)
    end

    it "should generate and store a chef server cookie id" do
      Chef::FileCache.should_receive(:store).with("chef_server_cookie_id", /\w{40}/).and_return(true)
      Chef::Config.manage_secret_key
    end

    describe "when the filecache has a chef server cookie id key" do
      before do
        Chef::FileCache.stub(:has_key?).with("chef_server_cookie_id").and_return(true)
      end

      it "should not generate and store a chef server cookie id" do
        Chef::FileCache.should_not_receive(:store).with("chef_server_cookie_id", /\w{40}/)
        Chef::Config.manage_secret_key
      end
    end

  end

  [ false, true ].each do |is_windows|

    context "On #{is_windows ? 'Windows' : 'Unix'}" do
      def to_platform(*args)
        Chef::Config.platform_specific_path(*args)
      end

      before :each do
        Chef::Platform.stub(:windows?).and_return(is_windows)
      end

      describe "class method: platform_specific_path" do
        if is_windows
          it "should return a windows path on windows systems" do
            path = "/etc/chef/cookbooks"
            Chef::Config.stub(:env).and_return({ 'SYSTEMDRIVE' => 'C:' })
            # match on a regex that looks for the base path with an optional
            # system drive at the beginning (c:)
            # system drive is not hardcoded b/c it can change and b/c it is not present on linux systems
            Chef::Config.platform_specific_path(path).should == "C:\\chef\\cookbooks"
          end
        else
          it "should return given path on non-windows systems" do
            path = "/etc/chef/cookbooks"
            Chef::Config.platform_specific_path(path).should == "/etc/chef/cookbooks"
          end
        end
      end

      describe "default values" do
        let :primary_cache_path do
          if is_windows
            "#{Chef::Config.env['SYSTEMDRIVE']}\\chef"
          else
            "/var/chef"
          end
        end

        let :secondary_cache_path do
          if is_windows
            "#{Chef::Config[:user_home]}\\.chef"
          else
            "#{Chef::Config[:user_home]}/.chef"
          end
        end

        before do
          if is_windows
            Chef::Config.stub(:env).and_return({ 'SYSTEMDRIVE' => 'C:' })
            Chef::Config[:user_home] = 'C:\Users\charlie'
          else
            Chef::Config[:user_home] = '/Users/charlie'
          end

          Chef::Config.stub(:path_accessible?).and_return(false)
        end

        describe "Chef::Config[:cache_path]" do
          context "when /var/chef exists and is accessible" do
            it "defaults to /var/chef" do
              Chef::Config.stub(:path_accessible?).with(to_platform("/var/chef")).and_return(true)
              Chef::Config[:cache_path].should == primary_cache_path
            end
          end

          context "when /var/chef does not exist and /var is accessible" do
            it "defaults to /var/chef" do
              File.stub(:exists?).with(to_platform("/var/chef")).and_return(false)
              Chef::Config.stub(:path_accessible?).with(to_platform("/var")).and_return(true)
              Chef::Config[:cache_path].should == primary_cache_path
            end
          end

          context "when /var/chef does not exist and /var is not accessible" do
            it "defaults to $HOME/.chef" do
              File.stub(:exists?).with(to_platform("/var/chef")).and_return(false)
              Chef::Config.stub(:path_accessible?).with(to_platform("/var")).and_return(false)
              Chef::Config[:cache_path].should == secondary_cache_path
            end
          end

          context "when /var/chef exists and is not accessible" do
            it "defaults to $HOME/.chef" do
              File.stub(:exists?).with(to_platform("/var/chef")).and_return(true)
              File.stub(:readable?).with(to_platform("/var/chef")).and_return(true)
              File.stub(:writable?).with(to_platform("/var/chef")).and_return(false)

              Chef::Config[:cache_path].should == secondary_cache_path
            end
          end

          context "when chef is running in local mode" do
            before do
              Chef::Config.local_mode = true
            end

            context "and config_dir is /a/b/c" do
              before do
                Chef::Config.config_dir to_platform('/a/b/c')
              end

              it "cache_path is /a/b/c/local-mode-cache" do
                Chef::Config.cache_path.should == to_platform('/a/b/c/local-mode-cache')
              end
            end

            context "and config_dir is /a/b/c/" do
              before do
                Chef::Config.config_dir to_platform('/a/b/c/')
              end

              it "cache_path is /a/b/c/local-mode-cache" do
                Chef::Config.cache_path.should == to_platform('/a/b/c/local-mode-cache')
              end
            end
          end
        end

        it "Chef::Config[:file_backup_path] defaults to /var/chef/backup" do
          Chef::Config.stub(:cache_path).and_return(primary_cache_path)
          backup_path = is_windows ? "#{primary_cache_path}\\backup" : "#{primary_cache_path}/backup"
          Chef::Config[:file_backup_path].should == backup_path
        end

        it "Chef::Config[:ssl_verify_mode] defaults to :verify_peer" do
          Chef::Config[:ssl_verify_mode].should == :verify_peer
        end

        it "Chef::Config[:ssl_ca_path] defaults to nil" do
          Chef::Config[:ssl_ca_path].should be_nil
        end

        # TODO can this be removed?
        if !is_windows
          it "Chef::Config[:ssl_ca_file] defaults to nil" do
            Chef::Config[:ssl_ca_file].should be_nil
          end
        end

        it "Chef::Config[:data_bag_path] defaults to /var/chef/data_bags" do
          Chef::Config.stub(:cache_path).and_return(primary_cache_path)
          data_bag_path = is_windows ? "#{primary_cache_path}\\data_bags" : "#{primary_cache_path}/data_bags"
          Chef::Config[:data_bag_path].should == data_bag_path
        end

        it "Chef::Config[:environment_path] defaults to /var/chef/environments" do
          Chef::Config.stub(:cache_path).and_return(primary_cache_path)
          environment_path = is_windows ? "#{primary_cache_path}\\environments" : "#{primary_cache_path}/environments"
          Chef::Config[:environment_path].should == environment_path
        end

        describe "setting the config dir" do

          context "when the config file is /etc/chef/client.rb" do

            before do
              Chef::Config.config_file = to_platform("/etc/chef/client.rb")
            end

            it "config_dir is /etc/chef" do
              Chef::Config.config_dir.should == to_platform("/etc/chef")
            end

            context "and chef is running in local mode" do
              before do
                Chef::Config.local_mode = true
              end

              it "config_dir is /etc/chef" do
                Chef::Config.config_dir.should == to_platform("/etc/chef")
              end
            end

            context "when config_dir is set to /other/config/dir/" do
              before do
                Chef::Config.config_dir = to_platform("/other/config/dir/")
              end

              it "yields the explicit value" do
                Chef::Config.config_dir.should == to_platform("/other/config/dir/")
              end
            end

          end

          context "when the user's home dir is /home/charlie/" do
            before do
              Chef::Config.user_home = to_platform("/home/charlie")
            end

            it "config_dir is /home/charlie/.chef/" do
              Chef::Config.config_dir.should == Chef::Util::PathHelper.join(to_platform("/home/charlie/.chef"), '')
            end

            context "and chef is running in local mode" do
              before do
                Chef::Config.local_mode = true
              end

              it "config_dir is /home/charlie/.chef/" do
                Chef::Config.config_dir.should == Chef::Util::PathHelper.join(to_platform("/home/charlie/.chef"), '')
              end
            end
          end

        end

        if is_windows
          describe "finding the windows embedded dir" do
            let(:default_config_location) { "c:/opscode/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.6.0/lib/chef/config.rb" }
            let(:alternate_install_location) { "c:/my/alternate/install/place/chef/embedded/lib/ruby/gems/1.9.1/gems/chef-11.6.0/lib/chef/config.rb" }
            let(:non_omnibus_location) { "c:/my/dev/stuff/lib/ruby/gems/1.9.1/gems/chef-11.6.0/lib/chef/config.rb" }

            let(:default_ca_file) { "c:/opscode/chef/embedded/ssl/certs/cacert.pem" }

            it "finds the embedded dir in the default location" do
              Chef::Config.stub(:_this_file).and_return(default_config_location)
              Chef::Config.embedded_dir.should == "c:/opscode/chef/embedded"
            end

            it "finds the embedded dir in a custom install location" do
              Chef::Config.stub(:_this_file).and_return(alternate_install_location)
              Chef::Config.embedded_dir.should == "c:/my/alternate/install/place/chef/embedded"
            end

            it "doesn't error when not in an omnibus install" do
              Chef::Config.stub(:_this_file).and_return(non_omnibus_location)
              Chef::Config.embedded_dir.should be_nil
            end

            it "sets the ssl_ca_cert path if the cert file is available" do
              Chef::Config.stub(:_this_file).and_return(default_config_location)
              File.stub(:exist?).with(default_ca_file).and_return(true)
              Chef::Config.ssl_ca_file.should == default_ca_file
            end
          end
        end
      end

      describe "Chef::Config[:user_home]" do
        it "should set when HOME is provided" do
          expected = to_platform("/home/kitten")
          Chef::Config.stub(:env).and_return({ 'HOME' => expected })
          Chef::Config[:user_home].should == expected
        end

        it "should be set when only USERPROFILE is provided" do
          expected = to_platform("/users/kitten")
          Chef::Config.stub(:env).and_return({ 'USERPROFILE' => expected })
          Chef::Config[:user_home].should == expected
        end

        it "falls back to the current working directory when HOME and USERPROFILE is not set" do
          Chef::Config.stub(:env).and_return({})
          Chef::Config[:user_home].should == Dir.pwd
        end
      end

      describe "Chef::Config[:encrypted_data_bag_secret]" do
        let(:db_secret_default_path){ to_platform("/etc/chef/encrypted_data_bag_secret") }

        before do
          File.stub(:exist?).with(db_secret_default_path).and_return(secret_exists)
        end

        context "/etc/chef/encrypted_data_bag_secret exists" do
          let(:secret_exists) { true }
          it "sets the value to /etc/chef/encrypted_data_bag_secret" do
            Chef::Config[:encrypted_data_bag_secret].should eq db_secret_default_path
          end
        end

        context "/etc/chef/encrypted_data_bag_secret does not exist" do
          let(:secret_exists) { false }
          it "sets the value to nil" do
            Chef::Config[:encrypted_data_bag_secret].should be_nil
          end
        end
      end

      describe "Chef::Config[:event_handlers]" do
        it "sets a event_handlers to an empty array by default" do
          Chef::Config[:event_handlers].should eq([])
        end
        it "should be able to add custom handlers" do
          o = Object.new
          Chef::Config[:event_handlers] << o
          Chef::Config[:event_handlers].should be_include(o)
        end
      end

      describe "Chef::Config[:user_valid_regex]" do
        context "on a platform that is not Windows" do
          it "allows one letter usernames" do
            any_match = Chef::Config[:user_valid_regex].any? { |regex| regex.match('a') }
            expect(any_match).to be_true
          end
        end
      end
    end
  end
end