summaryrefslogtreecommitdiff
path: root/spec/unit/dsl/plugin_spec.rb
blob: c37b6fd21a4d823b967aa17892abf43d60b60c41 (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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#
# Author:: Claire McQuin (<claire@chef.io>)
# Copyright:: Copyright (c) 2013-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 expressed or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License
#

require File.expand_path("../../../spec_helper.rb", __FILE__)

shared_examples "Ohai::DSL::Plugin" do
  context "#initialize" do
    it "sets has_run? to false" do
      expect(plugin.has_run?).to be false
    end

    it "sets the correct plugin version" do
      expect(plugin.version).to eql(version)
    end
  end

  context "#run" do
    before do
      allow(plugin).to receive(:run_plugin).and_return(true)
      allow(plugin).to receive(:name).and_return(:TestPlugin)
    end

    describe "when plugin is enabled" do
      before do
        allow(Ohai.config).to receive(:[]).with(:disabled_plugins).and_return([ ])
      end

      it "runs the plugin" do
        expect(plugin).to receive(:run_plugin)
        plugin.run
      end

      it "sets has_run? to true" do
        plugin.run
        expect(plugin.has_run?).to be true
      end
    end

    describe "if the plugin is disabled" do
      before do
        allow(Ohai.config).to receive(:[]).with(:disabled_plugins).and_return([ :TestPlugin ])
      end

      it "does not run the plugin" do
        expect(plugin).not_to receive(:run_plugin)
        plugin.run
      end

      it "logs a message to debug" do
        expect(Ohai::Log).to receive(:debug).with(/Skipping disabled plugin TestPlugin/)
        plugin.run
      end

      it "sets has_run? to true" do
        plugin.run
        expect(plugin.has_run?).to be true
      end
    end
  end

  context "when accessing data via method_missing" do
    it "takes a missing method and store the method name as a key, with its arguments as values" do
      plugin.guns_n_roses("chinese democracy")
      expect(plugin.data["guns_n_roses"]).to eql("chinese democracy")
    end

    it "returns the current value of the method name" do
      expect(plugin.guns_n_roses("chinese democracy")).to eql("chinese democracy")
    end

    it "allows you to get the value of a key by calling method_missing with no arguments" do
      plugin.guns_n_roses("chinese democracy")
      expect(plugin.guns_n_roses).to eql("chinese democracy")
    end
  end

  describe "set_attribute" do
    it "raises an ArgumentError when given one argument" do
      expect { plugin.set_attribute(:tea) }.to raise_error(ArgumentError)
    end

    it "lets you set an attribute" do
      plugin.set_attribute(:tea, "is soothing")
      expect(plugin.data["tea"]).to eql("is soothing")
    end

    it "lets you set an attribute to an empty array" do
      plugin.set_attribute(:tea, [])
      expect(plugin.data["tea"]).to eql([])
    end

    it "lets you set an attribute to an empty Mash" do
      plugin.set_attribute(:tea, {})
      expect(plugin.data["tea"]).to eql({})
    end

    it "lets you modify an attribute" do
      plugin.data["tea"] = "is soothing"
      plugin.set_attribute(:tea, "tastes like plants")
      expect(plugin.data["tea"]).to eql("tastes like plants")
    end

    it "lets you set a subattribute" do
      plugin.data["tea"] = Mash.new
      plugin.set_attribute(:tea, :green, "tastes like green")
      expect(plugin.data["tea"]["green"]).to eql("tastes like green")
    end

    it "lets you set a subattribute to an empty array" do
      plugin.set_attribute(:tea, :green, [])
      expect(plugin.data["tea"]["green"]).to eql([])
    end

    it "lets you set a subattribute to an empty Mash" do
      plugin.set_attribute(:tea, :green, {})
      expect(plugin.data["tea"]["green"]).to eql({})
    end

    it "lets you modify a subattribute" do
      plugin.data["tea"] = Mash.new
      plugin.data["tea"]["green"] = "tastes like green"
      plugin.set_attribute(:tea, :green, "made from leaves")
      expect(plugin.data["tea"]["green"]).to eql("made from leaves")
    end

    it "raises a TypeError when modifying an attribute that is not a Mash" do
      plugin.data["tea"] = Mash.new
      plugin.data["tea"] = "is soothing"
      expect { plugin.set_attribute(:tea, :green, "tastes like green") }.to raise_error(TypeError)
    end

    it "lets you modify a subattribute with an array" do
      green = { flavor: "tastes like green" }
      black = { flavor: "tastes like black" }
      plugin.data[:tea] = [green, black]
      plugin.set_attribute(:tea, 0, :source, "made from leaves")
      expect(plugin.data[:tea][0][:source]).to eq("made from leaves")
    end
  end

  context "when getting attributes" do
    before(:each) do
      plugin.set_attribute(:tea, "is soothing")
    end

    it "lets you get an attribute" do
      expect(plugin.get_attribute("tea")).to eql("is soothing")
    end
  end

  describe "get_attribute" do
    it "requires at least one argument" do
      expect { plugin.get_attribute }.to raise_error(ArgumentError)
    end

    describe "a top-level attribute" do
      before(:each) do
        plugin.set_attribute(:tea, "is soothing")
      end

      describe "as a string" do
        it "returns nil when the attribute does not exist" do
          expect(plugin.get_attribute("coffee")).to be nil
        end

        it "returns the attribute when it exists" do
          expect(plugin.get_attribute("tea")).to eql("is soothing")
        end
      end

      describe "as a symbol" do
        it "returns false when the attribute does not exist" do
          expect(plugin.get_attribute(:coffee)).to be nil
        end

        it "returns true when the attribute exists" do
          expect(plugin.get_attribute(:tea)).to eql("is soothing")
        end
      end
    end

    describe "a nested attribute" do
      before(:each) do
        plugin.set_attribute(:the_monarch, { arch_rival: "dr_venture" })
      end

      describe "as a list" do
        describe "of strings" do
          it "returns true when the attribute exists" do
            expect(plugin.get_attribute("the_monarch", "arch_rival")).
              to eql("dr_venture")
          end

          describe "when the attribute does not exist" do
            describe "and the subkey is missing" do
              it "returns nil" do
                expect(
                  plugin.get_attribute("the_monarch", "henchmen")
                ).to be nil
              end
            end

            describe "and an intermediate key is missing" do
              it "returns nil" do
                expect(
                  plugin.get_attribute("the_monarch", "henchmen",
                                       "corky_knightrider")
                ).to be nil
              end
            end

            describe "and an intermediate key is not a hash" do
              it "raises a TypeError" do
                expect {
                  plugin.get_attribute("the_monarch", "arch_rival",
                                       "dr_venture", "since")
                }.to raise_error(TypeError,
                                 "Expected Hash but got String.")
              end
            end
          end
        end

        describe "of symbols" do
          it "returns true when the attribute exists" do
            expect(plugin.get_attribute(:the_monarch, :arch_rival)).
              to eql("dr_venture")
          end

          describe "when the attribute does not exist" do
            describe "and the subkey is missing" do
              it "returns nil" do
                expect(plugin.get_attribute(:the_monarch, :henchmen)).to be nil
              end
            end

            describe "and an intermediate key is missing" do
              it "returns nil" do
                expect(
                  plugin.get_attribute(:the_monarch, :henchmen,
                                       :corky_knightrider)
                ).to be nil
              end
            end

            describe "and an intermediate key is not a hash" do
              it "raises a TypeError" do
                expect {
                  plugin.get_attribute(:the_monarch, :arch_rival,
                                       :dr_venture, :since)
                }.to raise_error(TypeError,
                                 "Expected Hash but got String.")
              end
            end
          end
        end
      end
    end
  end

  describe "attribute?" do
    it "requires at least one argument" do
      expect { plugin.attribute? }.to raise_error(ArgumentError)
    end

    describe "a top-level attribute" do
      describe "as a string" do
        it "returns false when the attribute does not exist" do
          expect(plugin.attribute?("alice in chains")).to eql(false)
        end

        it "returns true if an attribute exists with the given name" do
          plugin.metallica("death magnetic")
          expect(plugin.attribute?("metallica")).to eql(true)
        end
      end

      describe "as a symbol" do
        it "returns false when the attribute does not exist" do
          expect(plugin.attribute?(:sparkle_dream)).to be false
        end

        it "returns true when the attribute exists" do
          plugin.set_attribute("sparkle_dream", { version: 256 })
          expect(plugin.attribute?(:sparkle_dream)).to be true
        end
      end
    end

    describe "a nested attribute" do
      before(:each) do
        plugin.set_attribute(:the_monarch, { arch_rival: "dr_venture" })
      end

      describe "as a list" do
        describe "of strings" do
          it "returns true when the attribute exists" do
            expect(plugin.attribute?("the_monarch", "arch_rival")).to be true
          end

          describe "when the attribute does not exist" do
            describe "and the subkey is missing" do
              it "returns false" do
                expect(
                  plugin.attribute?("the_monarch", "henchmen")
                ).to be false
              end
            end

            describe "and an intermediate key is missing" do
              it "returns false" do
                expect(
                  plugin.attribute?("the_monarch", "henchmen",
                                    "corky_knightrider")
                ).to be false
              end
            end

            describe "and an intermediate key is not a hash" do
              it "raises a TypeError" do
                expect {
                  plugin.attribute?("the_monarch", "arch_rival",
                                    "dr_venture", "since")
                }.to raise_error(TypeError,
                                 "Expected Hash but got String.")
              end
            end
          end
        end

        describe "of symbols" do
          it "returns true when the attribute exists" do
            expect(plugin.attribute?(:the_monarch, :arch_rival)).to be true
          end

          describe "when the attribute does not exist" do
            describe "and the subkey is missing" do
              it "returns false" do
                expect(plugin.attribute?(:the_monarch, :henchmen)).to be false
              end
            end

            describe "and an intermediate key is missing" do
              it "returns false" do
                expect(
                  plugin.attribute?(:the_monarch, :henchmen,
                                    :corky_knightrider)
                ).to be false
              end
            end

            describe "and an intermediate key is not a hash" do
              it "raises a TypeError" do
                expect {
                  plugin.attribute?(:the_monarch, :arch_rival,
                                    :dr_venture, :since)
                }.to raise_error(TypeError,
                                 "Expected Hash but got String.")
              end
            end
          end
        end
      end
    end
  end
end

describe Ohai::DSL::Plugin::VersionVII do
  it "does not modify the plugin name when the plugin is named correctly" do
    plugin = Ohai.plugin(:FunkyVALIDpluginName) {}.new({})
    expect(plugin.name).to eql(:FunkyVALIDpluginName)
  end

  describe "when the plugin is named incorrectly" do
    context "because the plugin name doesn't start with a capital letter" do
      it "raises an Ohai::Exceptions::InvalidPluginName exception" do
        expect { Ohai.plugin(:badName) {} }.to raise_error(Ohai::Exceptions::InvalidPluginName, /badName is not a valid plugin name/)
      end
    end

    context "because the plugin name contains an underscore" do
      it "raises an Ohai::Exceptions::InvalidPluginName exception" do
        expect { Ohai.plugin(:Bad_Name) {} }.to raise_error(Ohai::Exceptions::InvalidPluginName, /Bad_Name is not a valid plugin name/)
      end
    end

    context "because the plugin name isn't a symbol" do
      it "raises an Ohai::Exceptions::InvalidPluginName exception" do
        expect { Ohai.plugin(1138) {} }.to raise_error(Ohai::Exceptions::InvalidPluginName, /1138 is not a valid plugin name/)
      end
    end
  end

  describe "#version" do
    it "saves the plugin version as :version7" do
      plugin = Ohai.plugin(:Test) {}
      expect(plugin.version).to eql(:version7)
    end
  end

  describe "#provides" do
    it "collects a single attribute" do
      plugin = Ohai.plugin(:Test) { provides("one") }
      expect(plugin.provides_attrs).to eql(["one"])
    end

    it "collects a list of attributes" do
      plugin = Ohai.plugin(:Test) { provides("one", "two", "three") }
      expect(plugin.provides_attrs).to eql(%w{one two three})
    end

    it "collects from multiple provides statements" do
      plugin = Ohai.plugin(:Test) {
        provides("one")
        provides("two", "three")
        provides("four")
      }
      expect(plugin.provides_attrs).to eql(%w{one two three four})
    end

    it "collects attributes across multiple plugin files" do
      plugin = Ohai.plugin(:Test) { provides("one") }
      plugin = Ohai.plugin(:Test) { provides("two", "three") }
      expect(plugin.provides_attrs).to eql(%w{one two three})
    end

    it "collects unique attributes" do
      plugin = Ohai.plugin(:Test) { provides("one") }
      plugin = Ohai.plugin(:Test) { provides("one", "two") }
      expect(plugin.provides_attrs).to eql(%w{one two})
    end
  end

  describe "#depends" do
    it "collects a single dependency" do
      plugin = Ohai.plugin(:Test) { depends("one") }
      expect(plugin.depends_attrs).to eql(["one"])
    end

    it "collects a list of dependencies" do
      plugin = Ohai.plugin(:Test) { depends("one", "two", "three") }
      expect(plugin.depends_attrs).to eql(%w{one two three})
    end

    it "collects from multiple depends statements" do
      plugin = Ohai.plugin(:Test) {
        depends("one")
        depends("two", "three")
        depends("four")
      }
      expect(plugin.depends_attrs).to eql(%w{one two three four})
    end

    it "collects dependencies across multiple plugin files" do
      plugin = Ohai.plugin(:Test) { depends("one") }
      plugin = Ohai.plugin(:Test) { depends("two", "three") }
      expect(plugin.depends_attrs).to eql(%w{one two three})
    end

    it "collects unique attributes" do
      plugin = Ohai.plugin(:Test) { depends("one") }
      plugin = Ohai.plugin(:Test) { depends("one", "two") }
      expect(plugin.depends_attrs).to eql(%w{one two})
    end
  end

  describe "#collect_data" do
    it "saves as :default if no platform is given" do
      plugin = Ohai.plugin(:Test) { collect_data {} }
      expect(plugin.data_collector).to have_key(:default)
    end

    it "saves a single given platform" do
      plugin = Ohai.plugin(:Test) { collect_data(:ubuntu) {} }
      expect(plugin.data_collector).to have_key(:ubuntu)
    end

    it "saves a list of platforms" do
      plugin = Ohai.plugin(:Test) { collect_data(:freebsd, :netbsd, :openbsd) {} }
      [:freebsd, :netbsd, :openbsd].each do |platform|
        expect(plugin.data_collector).to have_key(platform)
      end
    end

    it "saves multiple collect_data blocks" do
      plugin = Ohai.plugin(:Test) {
        collect_data {}
        collect_data(:windows) {}
        collect_data(:darwin) {}
      }
      [:darwin, :default, :windows].each do |platform|
        expect(plugin.data_collector).to have_key(platform)
      end
    end

    it "saves platforms across multiple plugins" do
      plugin = Ohai.plugin(:Test) { collect_data {} }
      plugin = Ohai.plugin(:Test) { collect_data(:aix, :sigar) {} }
      [:aix, :default, :sigar].each do |platform|
        expect(plugin.data_collector).to have_key(platform)
      end
    end

    it "fails a platform has already been defined in the same plugin" do
      expect {
        Ohai.plugin(:Test) {
          collect_data {}
          collect_data {}
        }
      }.to raise_error(Ohai::Exceptions::IllegalPluginDefinition, /collect_data already defined/)
    end

    it "fails if a platform has already been defined in another plugin file" do
      Ohai.plugin(:Test) { collect_data {} }
      expect {
        Ohai.plugin(:Test) {
          collect_data {}
        }
      }.to raise_error(Ohai::Exceptions::IllegalPluginDefinition, /collect_data already defined/)
    end
  end

  describe "#provides (deprecated)" do
    it "logs a warning" do
      plugin = Ohai::DSL::Plugin::VersionVII.new(Mash.new)
      expect(Ohai::Log).to receive(:warn).with(/\[UNSUPPORTED OPERATION\]/)
      plugin.provides("attribute")
    end
  end

  describe "#require_plugin (deprecated)" do
    it "logs a warning" do
      plugin = Ohai::DSL::Plugin::VersionVII.new(Mash.new)
      expect(Ohai::Log).to receive(:warn).with(/\[UNSUPPORTED OPERATION\]/)
      plugin.require_plugin("plugin")
    end
  end

  describe "#configuration" do
    let(:plugin) do
      klass = Ohai.plugin(camel_name) {}
      klass.new({})
    end

    shared_examples_for "plugin config lookup" do
      it "returns the configuration option value" do
        Ohai.config[:plugin][snake_name][:foo] = true
        expect(plugin.configuration(:foo)).to eq(true)
      end
    end

    describe "a plugin named Abc" do
      let(:camel_name) { :Abc }
      let(:snake_name) { :abc }

      it "returns nil when the plugin is not configured" do
        expect(plugin.configuration(:foo)).to eq(nil)
      end

      it "does not auto-vivify an un-configured plugin" do
        plugin.configuration(:foo)
        expect(Ohai.config[:plugin]).to_not have_key(:test)
      end

      it "returns nil when the option is not configured" do
        Ohai.config[:plugin][snake_name][:foo] = true
        expect(plugin.configuration(:bar)).to eq(nil)
      end

      it "returns nil when the suboption is not configured" do
        Ohai.config[:plugin][snake_name][:foo] = {}
        expect(plugin.configuration(:foo, :bar)).to eq(nil)
      end

      include_examples "plugin config lookup"

      it "returns the configuration sub-option value" do
        Ohai.config[:plugin][snake_name][:foo] = { :bar => true }
        expect(plugin.configuration(:foo, :bar)).to eq(true)
      end
    end

    describe "a plugin named ABC" do
      let(:camel_name) { :ABC }
      let(:snake_name) { :abc }

      include_examples "plugin config lookup"
    end

    describe "a plugin named Abc2" do
      let(:camel_name) { :Abc2 }
      let(:snake_name) { :abc_2 }

      include_examples "plugin config lookup"
    end

    describe "a plugin named AbcAbc" do
      let(:camel_name) { :AbcXyz }
      let(:snake_name) { :abc_xyz }

      include_examples "plugin config lookup"
    end

    describe "a plugin named ABCLmnoXyz" do
      let(:camel_name) { :ABCLmnoXyz }
      let(:snake_name) { :abc_lmno_xyz }

      include_examples "plugin config lookup"
    end
  end

  it_behaves_like "Ohai::DSL::Plugin" do
    let(:ohai) { Ohai::System.new }
    let(:plugin) { Ohai::DSL::Plugin::VersionVII.new(ohai.data) }
    let(:version) { :version7 }
  end
end

describe Ohai::DSL::Plugin::VersionVI do
  describe "#version" do
    it "saves the plugin version as :version6" do
      plugin = Class.new(Ohai::DSL::Plugin::VersionVI) {}
      expect(plugin.version).to eql(:version6)
    end
  end

  describe "#provides" do
    let(:ohai) { Ohai::System.new }

    it "logs a debug message when provides is used" do
      allow(Ohai::Log).to receive(:debug)
      expect(Ohai::Log).to receive(:debug).with(/Skipping provides/)
      plugin = Ohai::DSL::Plugin::VersionVI.new(ohai, "/some/plugin/path.rb", "/some/plugin")
      plugin.provides("attribute")
    end

    it "does not update the provides map for version 6 plugins." do
      plugin = Ohai::DSL::Plugin::VersionVI.new(ohai, "/some/plugin/path.rb", "/some/plugin")
      plugin.provides("attribute")
      expect(ohai.provides_map.map).to be_empty
    end

  end

  it_behaves_like "Ohai::DSL::Plugin" do
    let(:ohai) { Ohai::System.new }
    let(:plugin) { Ohai::DSL::Plugin::VersionVI.new(ohai, "/some/plugin/path.rb", "/some/plugin") }
    let(:version) { :version6 }
  end
end