summaryrefslogtreecommitdiff
path: root/spec/unit/application_spec.rb
blob: 210f875fbe9c14267cfb3299a1742c578fca05b5 (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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#
# Author:: AJ Christensen (<aj@junglist.gen.nz>)
# Author:: Mark Mzyk (mmzyk@opscode.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'

describe Chef::Application do
  before do
    @original_argv = ARGV.dup
    ARGV.clear
    Chef::Log.logger = Logger.new(StringIO.new)
    @app = Chef::Application.new
    allow(@app).to receive(:trap)
    allow(Dir).to receive(:chdir).and_return(0)
    allow(@app).to receive(:reconfigure)
    Chef::Log.init(STDERR)
  end

  after do
    ARGV.replace(@original_argv)
  end

  describe "reconfigure" do
    before do
      @app = Chef::Application.new
      allow(@app).to receive(:configure_chef).and_return(true)
      allow(@app).to receive(:configure_logging).and_return(true)
      allow(@app).to receive(:configure_proxy_environment_variables).and_return(true)
    end

    it "should configure chef" do
      expect(@app).to receive(:configure_chef).and_return(true)
      @app.reconfigure
    end

    it "should configure logging" do
      expect(@app).to receive(:configure_logging).and_return(true)
      @app.reconfigure
    end

    it "should configure environment variables" do
      expect(@app).to receive(:configure_proxy_environment_variables).and_return(true)
      @app.reconfigure
    end
  end

  describe Chef::Application do
    before do
      @app = Chef::Application.new
    end

    describe "run" do
      before do
        allow(@app).to receive(:setup_application).and_return(true)
        allow(@app).to receive(:run_application).and_return(true)
        allow(@app).to receive(:configure_chef).and_return(true)
        allow(@app).to receive(:configure_logging).and_return(true)
      end

      it "should reconfigure the application before running" do
        expect(@app).to receive(:reconfigure).and_return(true)
        @app.run
      end

      it "should setup the application before running it" do
        expect(@app).to receive(:setup_application).and_return(true)
        @app.run
      end

      it "should run the actual application" do
        expect(@app).to receive(:run_application).and_return(true)
        @app.run
      end
    end
  end

  describe "configure_chef" do
    before do
      # Silence warnings when no config file exists
      allow(Chef::Log).to receive(:warn)

      @app = Chef::Application.new
      #Chef::Config.stub(:merge!).and_return(true)
      allow(@app).to receive(:parse_options).and_return(true)
    end

    it "should parse the commandline options" do
      expect(@app).to receive(:parse_options).and_return(true)
      @app.config[:config_file] = "/etc/chef/default.rb" #have a config file set, to prevent triggering error block
      @app.configure_chef
    end

    describe "when a config_file is present" do
      let(:config_content) { "rspec_ran('true')" }
      let(:config_location) { "/etc/chef/default.rb" }

      let(:config_location_pathname) do
        p = Pathname.new(config_location)
        allow(p).to receive(:realpath).and_return(config_location)
        p
      end

      before do
        @app.config[:config_file] = config_location

        # force let binding to get evaluated or else we stub Pathname.new before we try to use it.
        config_location_pathname
        allow(Pathname).to receive(:new).with(config_location).and_return(config_location_pathname)
        expect(File).to receive(:read).
          with(config_location).
          and_return(config_content)
      end

      it "should configure chef::config from a file" do
        expect(Chef::Config).to receive(:from_string).with(config_content, config_location)
        @app.configure_chef
      end

      it "should merge the local config hash into chef::config" do
        #File.should_receive(:open).with("/etc/chef/default.rb").and_yield(@config_file)
        @app.configure_chef
        expect(Chef::Config.rspec_ran).to eq("true")
      end

    end

    describe "when there is no config_file defined" do
      before do
        @app.config[:config_file] = nil
      end

      it "should emit a warning" do
        expect(Chef::Config).not_to receive(:from_file).with("/etc/chef/default.rb")
        expect(Chef::Log).to receive(:warn).with("No config file found or specified on command line, using command line options.")
        @app.configure_chef
      end
    end

    describe "when the config file is set and not found" do
      before do
        @app.config[:config_file] = "/etc/chef/notfound"
      end
      it "should use the passed in command line options and defaults" do
        expect(Chef::Config).to receive(:merge!)
        @app.configure_chef
      end
    end
  end

  describe "when configuring the logger" do
    before do
      @app = Chef::Application.new
      allow(Chef::Log).to receive(:init)
    end

    it "should initialise the chef logger" do
      allow(Chef::Log).to receive(:level=)
      @monologger = double("Monologger")
      expect(MonoLogger).to receive(:new).with(Chef::Config[:log_location]).and_return(@monologger)
      expect(Chef::Log).to receive(:init).with(@monologger)
      @app.configure_logging
    end

    it "should raise fatals if log location is invalid" do
      Chef::Config[:log_location] = "/tmp/non-existing-dir/logfile"
      expect(Chef::Log).to receive(:fatal).at_least(:once)
      expect(Process).to receive(:exit)
      @app.configure_logging
    end

    shared_examples_for "log_level_is_auto" do
      context "when STDOUT is to a tty" do
        before do
          allow(STDOUT).to receive(:tty?).and_return(true)
        end

        it "configures the log level to :warn" do
          @app.configure_logging
          expect(Chef::Log.level).to eq(:warn)
        end

        context "when force_logger is configured" do
          before do
            Chef::Config[:force_logger] = true
          end

          it "configures the log level to info" do
            @app.configure_logging
            expect(Chef::Log.level).to eq(:info)
          end
        end
      end

      context "when STDOUT is not to a tty" do
        before do
          allow(STDOUT).to receive(:tty?).and_return(false)
        end

        it "configures the log level to :info" do
          @app.configure_logging
          expect(Chef::Log.level).to eq(:info)
        end

        context "when force_formatter is configured" do
          before do
            Chef::Config[:force_formatter] = true
          end
          it "sets the log level to :warn" do
            @app.configure_logging
            expect(Chef::Log.level).to eq(:warn)
          end
        end
      end
    end

    context "when log_level is not set" do
      it_behaves_like "log_level_is_auto"
    end

    context "when log_level is :auto" do
      before do
        Chef::Config[:log_level] = :auto
      end

      it_behaves_like "log_level_is_auto"
    end
  end

  describe "when configuring environment variables" do
    def configure_proxy_environment_variables_stubs
      allow(@app).to receive(:configure_http_proxy).and_return(true)
      allow(@app).to receive(:configure_https_proxy).and_return(true)
      allow(@app).to receive(:configure_ftp_proxy).and_return(true)
      allow(@app).to receive(:configure_no_proxy).and_return(true)
    end

    shared_examples_for "setting ENV['http_proxy']" do
      before do
        Chef::Config[:http_proxy] = http_proxy
      end

      it "should set ENV['http_proxy']" do
        @app.configure_proxy_environment_variables
        expect(@env['http_proxy']).to eq("#{scheme}://#{address}:#{port}")
      end

      it "should set ENV['HTTP_PROXY']" do
        @app.configure_proxy_environment_variables
        expect(@env['HTTP_PROXY']).to eq("#{scheme}://#{address}:#{port}")
      end

      describe "when Chef::Config[:http_proxy_user] is set" do
        before do
          Chef::Config[:http_proxy_user] = "username"
        end

        it "should set ENV['http_proxy'] with the username" do
          @app.configure_proxy_environment_variables
          expect(@env['http_proxy']).to eq("#{scheme}://username@#{address}:#{port}")
          expect(@env['HTTP_PROXY']).to eq("#{scheme}://username@#{address}:#{port}")
        end

        context "when :http_proxy_user contains '@' and/or ':'" do
          before do
            Chef::Config[:http_proxy_user] = "my:usern@me"
          end

          it "should set ENV['http_proxy'] with the escaped username" do
            @app.configure_proxy_environment_variables
            expect(@env['http_proxy']).to eq("#{scheme}://my%3Ausern%40me@#{address}:#{port}")
            expect(@env['HTTP_PROXY']).to eq("#{scheme}://my%3Ausern%40me@#{address}:#{port}")
          end
        end

        describe "when Chef::Config[:http_proxy_pass] is set" do
          before do
            Chef::Config[:http_proxy_pass] = "password"
          end

          it "should set ENV['http_proxy'] with the password" do
            @app.configure_proxy_environment_variables
            expect(@env['http_proxy']).to eq("#{scheme}://username:password@#{address}:#{port}")
            expect(@env['HTTP_PROXY']).to eq("#{scheme}://username:password@#{address}:#{port}")
          end

          context "when :http_proxy_pass contains '@' and/or ':'" do
            before do
              Chef::Config[:http_proxy_pass] = ":P@ssword101"
            end

            it "should set ENV['http_proxy'] with the escaped password" do
              @app.configure_proxy_environment_variables
              expect(@env['http_proxy']).to eq("#{scheme}://username:%3AP%40ssword101@#{address}:#{port}")
              expect(@env['HTTP_PROXY']).to eq("#{scheme}://username:%3AP%40ssword101@#{address}:#{port}")
            end
          end
        end
      end

      describe "when Chef::Config[:http_proxy_pass] is set (but not Chef::Config[:http_proxy_user])" do
        before do
          Chef::Config[:http_proxy_user] = nil
          Chef::Config[:http_proxy_pass] = "password"
        end

        it "should set ENV['http_proxy']" do
          @app.configure_proxy_environment_variables
          expect(@env['http_proxy']).to eq("#{scheme}://#{address}:#{port}")
          expect(@env['HTTP_PROXY']).to eq("#{scheme}://#{address}:#{port}")
        end
      end
    end

    describe "when configuring ENV['http_proxy']" do
      before do
        @env = {}
        allow(@app).to receive(:env).and_return(@env)

        allow(@app).to receive(:configure_https_proxy).and_return(true)
        allow(@app).to receive(:configure_ftp_proxy).and_return(true)
        allow(@app).to receive(:configure_no_proxy).and_return(true)
      end

      describe "when Chef::Config[:http_proxy] is not set" do
        before do
          Chef::Config[:http_proxy] = nil
        end

        it "should not set ENV['http_proxy']" do
          @app.configure_proxy_environment_variables
          expect(@env).to eq({})
        end
      end

      describe "when Chef::Config[:http_proxy] is set" do
        context "when given an FQDN" do
          let(:scheme) { "http" }
          let(:address) { "proxy.example.org" }
          let(:port) { 8080 }
          let(:http_proxy) { "#{scheme}://#{address}:#{port}" }

          it_should_behave_like "setting ENV['http_proxy']"
        end

        context "when given an HTTPS URL" do
          let(:scheme) { "https" }
          let(:address) { "proxy.example.org" }
          let(:port) { 8080 }
          let(:http_proxy) { "#{scheme}://#{address}:#{port}" }

          it_should_behave_like "setting ENV['http_proxy']"
        end

        context "when given an IP" do
          let(:scheme) { "http" }
          let(:address) { "127.0.0.1" }
          let(:port) { 22 }
          let(:http_proxy) { "#{scheme}://#{address}:#{port}" }

          it_should_behave_like "setting ENV['http_proxy']"
        end

        context "when given an IPv6" do
          let(:scheme) { "http" }
          let(:address) { "[2001:db8::1]" }
          let(:port) { 80 }
          let(:http_proxy) { "#{scheme}://#{address}:#{port}" }

          it_should_behave_like "setting ENV['http_proxy']"
        end

        context "when given without including http://" do
          let(:scheme) { "http" }
          let(:address) { "proxy.example.org" }
          let(:port) { 8181 }
          let(:http_proxy) { "#{address}:#{port}" }

          it_should_behave_like "setting ENV['http_proxy']"
        end

        context "when given the full proxy in :http_proxy only" do
          before do
            Chef::Config[:http_proxy] = "http://username:password@proxy.example.org:2222"
            Chef::Config[:http_proxy_user] = nil
            Chef::Config[:http_proxy_pass] = nil
          end

          it "should set ENV['http_proxy']" do
            @app.configure_proxy_environment_variables
            expect(@env['http_proxy']).to eq(Chef::Config[:http_proxy])
          end
        end

        context "when the config options aren't URI compliant" do
          it "raises Chef::Exceptions::BadProxyURI" do
            Chef::Config[:http_proxy] = "http://proxy.bad_example.org/:8080"
            expect { @app.configure_proxy_environment_variables }.to raise_error(Chef::Exceptions::BadProxyURI)
          end
        end
      end
    end
  end

  describe "class method: fatal!" do
    before do
      allow(STDERR).to receive(:puts).with("FATAL: blah").and_return(true)
      allow(Chef::Log).to receive(:fatal).and_return(true)
      allow(Process).to receive(:exit).and_return(true)
    end

    it "should log an error message to the logger" do
      expect(Chef::Log).to receive(:fatal).with("blah").and_return(true)
      Chef::Application.fatal! "blah"
    end

    describe "when an exit code is supplied" do
      it "should exit with the given exit code" do
        expect(Process).to receive(:exit).with(-100).and_return(true)
        Chef::Application.fatal! "blah", -100
      end
    end

    describe "when an exit code is not supplied" do
      it "should exit with the default exit code" do
        expect(Process).to receive(:exit).with(-1).and_return(true)
        Chef::Application.fatal! "blah"
      end
    end

  end

  describe "setup_application" do
    before do
      @app = Chef::Application.new
    end

    it "should raise an error" do
      expect { @app.setup_application }.to raise_error(Chef::Exceptions::Application)
    end
  end

  describe "run_application" do
    before do
      @app = Chef::Application.new
    end

    it "should raise an error" do
      expect { @app.run_application }.to raise_error(Chef::Exceptions::Application)
    end
  end

  context "when the config file is not available" do
    it "should warn for bad config file path" do
      @app.config[:config_file] = "/tmp/non-existing-dir/file"
      config_file_regexp = Regexp.new @app.config[:config_file]
      expect(Chef::Log).to receive(:warn).at_least(:once).with(config_file_regexp).and_return(true)
      allow(Chef::Log).to receive(:warn).and_return(true)
      @app.configure_chef
    end
  end

  describe "configuration errors" do
    before do
      expect(Process).to receive(:exit)
    end

    def raises_informative_fatals_on_configure_chef
      config_file_regexp = Regexp.new @app.config[:config_file]
      expect(Chef::Log).to receive(:fatal).
        with(/Configuration error/)
      expect(Chef::Log).to receive(:fatal).
        with(config_file_regexp).
        at_least(1).times
      @app.configure_chef
    end

    describe "when config file exists but contains errors" do
      def create_config_file(text)
        @config_file = Tempfile.new("rspec-chef-config")
        @config_file.write(text)
        @config_file.close
        @app.config[:config_file] = @config_file.path
      end

      after(:each) do
        @config_file.unlink
      end

      it "should raise informative fatals for badly written config" do
        create_config_file("text that should break the config parsing")
        raises_informative_fatals_on_configure_chef
      end
    end
  end
end