summaryrefslogtreecommitdiff
path: root/spec/unit/system_spec.rb
blob: fc1508e72d7f85f3be697b3e19518a14a57ecc51 (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
#
# Author:: Adam Jacob (<adam@chef.io>)
# Author:: Claire McQuin (<claire@chef.io>)
# Copyright:: Copyright (c) 2008-2017, 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/mixin/os"

describe "Ohai::System" do
  extend IntegrationSupport

  let(:ohai_external) {}
  let(:ohai) { Ohai::System.new({ invoked_from_cli: true }) }

  describe "#initialize" do
    it "returns an Ohai::System object" do
      expect(ohai).to be_a_kind_of(Ohai::System)
    end

    it "sets @attributes to a ProvidesMap" do
      expect(ohai.provides_map).to be_a_kind_of(Ohai::ProvidesMap)
    end

    it "merges provided configuration options into the ohai config context" do
      config = {
        disabled_plugins: %i{Foo Baz},
        directory: ["/some/extra/plugins"],
        critical_plugins: %i{Foo Bar},
      }
      Ohai::System.new(config)
      config.each do |option, value|
        expect(Ohai.config[option]).to eq(value)
      end
    end

    context "when a single directory is configured as a string" do
      let(:directory) { "/some/fantastic/plugins" }

      it "adds directory to plugin_path" do
        Ohai.config[:directory] = directory
        Ohai::System.new({ invoked_from_cli: true })
        expect(Ohai.config[:plugin_path]).to include("/some/fantastic/plugins")
      end
    end

    context "when multiple directories are configured" do
      let(:directory) { ["/some/fantastic/plugins", "/some/other/plugins"] }

      it "adds directories to plugin_path" do
        Ohai.config[:directory] = directory
        Ohai::System.new({ invoked_from_cli: true })
        expect(Ohai.config[:plugin_path]).to include("/some/fantastic/plugins")
        expect(Ohai.config[:plugin_path]).to include("/some/other/plugins")
      end
    end

    context "when testing the intializer that does way too much" do
      it "configures logging" do
        log_level = :debug
        Ohai.config[:log_level] = log_level
        expect(Ohai::Log).to receive(:level=).with(log_level)
        Ohai::System.new({ invoked_from_cli: true })
      end

      it "resolves log_level when set to :auto" do
        expect(Ohai::Log).to receive(:level=).with(:info)
        Ohai::System.new({ invoked_from_cli: true })
      end

      context "when called externally" do
        it "does not configure logging" do
          log_level = :debug
          Ohai.config[:log_level] = log_level
          expect(Ohai::Log).not_to receive(:level=).with(log_level)
          Ohai::System.new
        end

        it "does not resolve log_level when set to :auto" do
          expect(Ohai::Log).not_to receive(:level=).with(:info)
          Ohai::System.new
        end
      end
    end
  end

  when_plugins_directory "contains directories inside" do
    with_plugin("repo1/zoo.rb", <<~EOF)
      Ohai.plugin(:Zoo) do
        provides 'seals'
      end
    EOF

    with_plugin("repo1/lake.rb", <<~EOF)
      Ohai.plugin(:Nature) do
      provides 'fish'
      end
    EOF

    with_plugin("repo2/nature.rb", <<~EOF)
      Ohai.plugin(:Nature) do
        provides 'crabs'
      end
    EOF

    with_plugin("repo2/mountain.rb", <<~EOF)
      Ohai.plugin(:Nature) do
      provides 'bear'
      end
    EOF

    before do
      Ohai.config[:plugin_path] = [ path_to("repo1"), path_to("repo2") ]
    end

    it "load_plugins() should load all the plugins" do
      ohai.load_plugins
      expect(ohai.provides_map.map.keys).to include("seals")
      expect(ohai.provides_map.map.keys).to include("crabs")
      expect(Ohai::NamedPlugin.const_get(:Zoo)).to eq(Ohai::NamedPlugin::Zoo)
      expect(Ohai::NamedPlugin.const_get(:Nature)).to eq(Ohai::NamedPlugin::Nature)
    end

  end

  describe "when running plugins" do
    when_plugins_directory "contains a v7 plugins with :default and platform specific blocks" do
      with_plugin("message.rb", <<~EOF)
        Ohai.plugin(:Message) do
          provides 'message'

          collect_data(:default) do
            message("default")
          end

          collect_data(:#{Ohai::Mixin::OS.collect_os}) do
            message("platform_specific_message")
          end
        end
      EOF

      it "should collect platform specific" do
        Ohai.config[:plugin_path] = [ path_to(".") ]
        ohai.all_plugins
        expect(ohai.data[:message]).to eq("platform_specific_message")
      end
    end

    when_plugins_directory "contains v7 plugins only" do
      with_plugin("zoo.rb", <<~EOF)
        Ohai.plugin(:Zoo) do
          provides 'zoo'

          collect_data(:default) do
            zoo("animals")
          end
        end
      EOF

      with_plugin("park.rb", <<~EOF)
        Ohai.plugin(:Park) do
          provides 'park'
          collect_data(:default) do
            park("plants")
          end
        end
      EOF

      with_plugin("fails.rb", <<~EOF)
        Ohai.plugin(:Fails) do
          provides 'fails'
          collect_data(:default) do
            fail 'thing'
          end
        end
      EOF

      with_plugin("optional.rb", <<~EOF)
        Ohai.plugin(:Optional) do
          provides 'optional'
          optional true

          collect_data(:default) do
            optional("canteloupe")
          end
        end
      EOF

      it "should collect data from all the plugins" do
        Ohai.config[:plugin_path] = [ path_to(".") ]
        ohai.all_plugins
        expect(ohai.data[:zoo]).to eq("animals")
        expect(ohai.data[:park]).to eq("plants")
        expect(ohai.data[:zoo]).to be_frozen
        expect(ohai.data[:park]).to be_frozen
      end

      it "should write an error to Ohai::Log" do
        Ohai.config[:plugin_path] = [ path_to(".") ]
        # Make sure the stubbing of runner is not overriden with reset_system during test
        allow(ohai).to receive(:reset_system)
        allow(ohai.instance_variable_get("@runner")).to receive(:run_plugin).and_raise(Ohai::Exceptions::AttributeNotFound)
        expect(ohai.logger).to receive(:error).with(/Encountered error while running plugins/)
        expect { ohai.all_plugins }.to raise_error(Ohai::Exceptions::AttributeNotFound)
      end

      describe "when using :disabled_plugins" do
        before do
          Ohai.config[:disabled_plugins] = [ :Zoo ]
        end

        after do
          Ohai.config[:disabled_plugins] = [ ]
        end

        it "shouldn't run disabled plugins" do
          Ohai.config[:plugin_path] = [ path_to(".") ]
          ohai.all_plugins
          expect(ohai.data[:zoo]).to be_nil
          expect(ohai.data[:park]).to eq("plants")
        end
      end

      describe "when using :critical_plugins" do
        # if called from cli is true, we'll exit these tests
        let(:ohai) { Ohai::System.new }

        before do
          Ohai.config[:critical_plugins] = [ :Fails ]
        end

        after do
          Ohai.config[:critical_plugins] = []
        end

        it "should fail when critical plugins fail" do
          Ohai.config[:plugin_path] = [ path_to(".") ]
          expect { ohai.all_plugins }.to raise_error(Ohai::Exceptions::CriticalPluginFailure,
            "The following Ohai plugins marked as critical failed: [:Fails]. Failing Chef run.")
        end
      end

      describe "when using :optional_plugins" do
        it "should not run optional plugins by default" do
          Ohai.config[:plugin_path] = [ path_to(".") ]
          ohai.all_plugins
          expect(ohai.data[:optional]).to be_nil
        end

        it "should run optional plugins when specifically enabled" do
          Ohai.config[:optional_plugins] = [ :Optional ]
          Ohai.config[:plugin_path] = [ path_to(".") ]
          ohai.all_plugins
          expect(ohai.data[:optional]).to eq("canteloupe")
        end

        it "should run optional plugins when all plugins are enabled" do
          Ohai.config[:run_all_plugins] = true
          Ohai.config[:plugin_path] = [ path_to(".") ]
          ohai.all_plugins
          expect(ohai.data[:optional]).to eq("canteloupe")
        end
      end
    end
  end

  describe "when Chef OHAI resource executes :reload action" do

    when_plugins_directory "contains a random plugin" do
      with_plugin("random.rb", <<-E)
        Ohai.plugin(:Random) do
          provides 'random'

          collect_data do
            random rand(1 << 32)
          end
        end
      E

      before do
        Ohai.config[:plugin_path] = [ path_to(".") ]
      end

      it "should rerun the plugin providing the desired attributes" do
        ohai.all_plugins
        initial_value = ohai.data["random"]
        ohai.all_plugins
        updated_value = ohai.data["random"]
        expect(initial_value).not_to eq(updated_value)
      end

    end
  end

  describe "when running ohai for specific attributes" do
    when_plugins_directory "contains v7 plugins" do
      with_plugin("languages.rb", <<-E)
        Ohai.plugin(:Languages) do
          provides 'languages'

          collect_data do
            languages Mash.new
          end
        end
      E

      with_plugin("english.rb", <<-E)
        Ohai.plugin(:English) do
          provides 'languages/english'

          depends 'languages'

          collect_data do
            languages[:english] = Mash.new
            languages[:english][:version] = 2014
          end
        end
      E

      with_plugin("french.rb", <<-E)
        Ohai.plugin(:French) do
          provides 'languages/french'

          depends 'languages'

          collect_data do
            languages[:french] = Mash.new
            languages[:french][:version] = 2012
          end
        end
      E

      before do
        Ohai.config[:plugin_path] = [ path_to(".") ]
      end

      it "should run all the plugins when a top level attribute is specified" do
        ohai.all_plugins("languages")
        expect(ohai.data[:languages][:english][:version]).to eq(2014)
        expect(ohai.data[:languages][:french][:version]).to eq(2012)
      end

      it "should run the first parent when a non-existent child is specified" do
        ohai.all_plugins("languages/english/version")
        expect(ohai.data[:languages][:english][:version]).to eq(2014)
        expect(ohai.data[:languages][:french]).to be_nil
      end

      it "should be able to run multiple plugins" do
        ohai.all_plugins(["languages/english", "languages/french"])
        expect(ohai.data[:languages][:english][:version]).to eq(2014)
        expect(ohai.data[:languages][:french][:version]).to eq(2012)
      end

    end
  end

  describe "when loading a specific plugin path" do
    when_plugins_directory "contains v7 plugins" do
      with_plugin("my_cookbook/canteloupe.rb", <<-E)
        Ohai.plugin(:Canteloupe) do
          provides 'canteloupe'

          collect_data do
            canteloupe Mash.new
          end
        end
      E

      with_plugin("english/english.rb", <<-E)
        Ohai.plugin(:English) do
          provides 'canteloupe/english'

          depends 'canteloupe'

          collect_data do
            canteloupe[:english] = Mash.new
            canteloupe[:english][:version] = 2014
          end
        end
      E

      with_plugin("french/french.rb", <<-E)
        Ohai.plugin(:French) do
          provides 'canteloupe/french'

          depends 'canteloupe'

          collect_data do
            canteloupe[:french] = Mash.new
            canteloupe[:french][:version] = 2012
          end
        end
      E

      it "should run all the plugins" do
        ohai.run_additional_plugins(@plugins_directory)
        expect(ohai.data[:canteloupe][:english][:version]).to eq(2014)
        expect(ohai.data[:canteloupe][:french][:version]).to eq(2012)
      end
    end
  end
end