summaryrefslogtreecommitdiff
path: root/spec/functional/application_spec.rb
blob: 33992f5dd57fef9891138d19e26dcc38db058f26 (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
#
# Author:: Claire McQuin <claire@chef.io>
# Copyright:: Copyright (c) 2015 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_relative '../spec_helper'

require 'ohai/application'

RSpec.describe 'Ohai::Application' do

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

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

  after(:each) 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(:each) do
      File.open(config_location, 'w+') do |f|
        f.write(config_content)
      end
    end

    after(:each) 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(:each) 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([ :Foo, :Baz ])
      end
    end

    context 'when the configuration file contains deprecated config options' 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) do
        <<-CONFIG
log_location "#{log_location}"
log_level    :#{log_level}
Ohai::Config[:disabled_plugins] = #{disabled_plugins}
Ohai::Config[:plugin_path] << "#{plugin_path}"
CONFIG
      end

      # config settings
      let(:disabled_plugins) { [ :Foo, :Baz ] }

      let(:log_level) { :debug }

      let(:log_location) { 'path/to/log' }

      let(:plugin_path) { '/path/to/plugins' }

      it 'logs warnings for deprecated top-level options' do
        # deprecation warnings for options need to be stubbed in the order
        # they are received, in this case it's the order they appear in the
        # configuration file.
        options = [ :log_location, :log_level, :disabled_plugins ]
        options.each do |option|
          expect(Ohai::Log).to receive(:warn).
            with(/Ohai::Config\[:#{option}\] is deprecated/)
        end
        app.configure_ohai
      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