summaryrefslogtreecommitdiff
path: root/spec/functional/application_spec.rb
blob: 5370c92c9300b8f376d116695eea9f2848fc2bee (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
#
# Author:: Claire McQuin <claire@chef.io>
# Copyright:: Copyright (c) 2015-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"
require "ohai/application"

RSpec.describe "Ohai::Application" do

  let(:app) { Ohai::Application.new }
  let(:argv) { [] }
  let(:stderr) { StringIO.new }

  before do
    @original_argv = ARGV.dup
    ARGV.replace(argv)
  end

  after do
    ARGV.replace(@original_argv)
  end

  describe "#configure_ohai" do

    let(:config_content) { "" }
    let(:config_dir) { Dir.mktmpdir(".chef") }
    let(:config_location) { File.join(config_dir, "config.rb") }

    before do
      File.open(config_location, "w+") do |f|
        f.write(config_content)
      end
    end

    after do
      FileUtils.rm_rf(config_dir)
    end

    context "when a configuration file is provided as a command line option" do

      let(:argv) { [ "-c", config_location + ".oops" ] }

      context "and the configuration file does not exist" do

        it "logs an error and terminates the application" do
          expect(STDERR).to receive(:puts).with(/FATAL:/)
          expect(Ohai::Log).to receive(:fatal)
            .with(/Specified config file #{argv[1]} does not exist/)
          expect { app.configure_ohai }.to raise_error(SystemExit)
        end
      end
    end

    context "when a workstation configuration file exists" do

      let(:config_content) { "ohai.disabled_plugins = [ :Foo, :Baz ]" }

      # env['KNIFE_HOME']/config.rb is the first config file the workstation
      # config loader looks for:
      # https://github.com/chef/chef/blob/master/chef-config/lib/chef-config/workstation_config_loader.rb#L102
      let(:env) { { "KNIFE_HOME" => config_dir } }

      before do
        allow_any_instance_of(ChefConfig::WorkstationConfigLoader)
          .to receive(:env).and_return(env)
      end

      it "loads the workstation configuration file" do
        app.configure_ohai
        expect(Ohai.config[:disabled_plugins]).to eq(%i{Foo Baz})
      end
    end

    context "when the configuration file has a syntax error" do
      # For the purpose of these tests it doesn't matter if the configuration
      # file was specified via command line or discovered on the local
      # workstation. It's easier if we pass the configuration file as a cli
      # argument (there's less to stub).

      let(:argv) { [ "-c", config_location ] }

      let(:config_content) { 'config_location "blaaaaa' }

      it "logs an error and terminates the application" do
        expect(STDERR).to receive(:puts).with(/FATAL:/)
        expect(Ohai::Log).to receive(:fatal)
          .with(/You have invalid ruby syntax in your config file/)
        expect { app.configure_ohai }.to raise_error(SystemExit)
      end
    end

  end
end