\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef.upgrade/embedded/lib/ruby/gems/2.3.0/gems/ohai-8.24.0/spec/unit/

Viewing File: loader_spec.rb

#
# 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 express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require File.expand_path(File.dirname(__FILE__) + "/../spec_helper.rb")

describe Ohai::Loader do
  extend IntegrationSupport

  let(:loader) { Ohai::Loader.new(ohai) }
  let(:ohai) { double("Ohai::System", :data => Mash.new, :provides_map => provides_map) }
  let(:provides_map) { Ohai::ProvidesMap.new }

  describe "#initialize" do
    it "returns an Ohai::Loader object" do
      loader = Ohai::Loader.new(ohai)
      expect(loader).to be_a_kind_of(Ohai::Loader)
    end
  end

  when_plugins_directory "contains both V6 & V7 plugins" do
    with_plugin("zoo.rb", <<EOF)
Ohai.plugin(:Zoo) do
  provides 'seals'
end
EOF

    with_plugin("zoo_too.rb", <<EOF)
Ohai.plugin(:Zoo) do
  provides 'elephants'
end
EOF

    with_plugin("lake.rb", <<EOF)
provides 'fish'
EOF

    describe "load_plugin() method" do
      describe "when loading a v7 plugin" do
        let(:plugin) { loader.load_plugin(path_to("zoo.rb")) }

        it "saves the plugin according to its attribute" do
          plugin
          expect(provides_map.map.keys).to include("seals")
        end

        it "saves a single plugin source" do
          expect(plugin.source).to eql([path_to("zoo.rb")])
        end

        it "saves all plugin sources" do
          plugin
          loader.load_plugin(path_to("zoo_too.rb"))
          expect(plugin.source).to eql([path_to("zoo.rb"), path_to("zoo_too.rb")])
        end
      end

      describe "when loading a v6 plugin" do
        let(:plugin) { loader.load_plugin(path_to("lake.rb"), path_to(".")) }

        before(:each) do
          expect(Ohai::Log).to receive(:warn).with(/\[DEPRECATION\]/)
        end

        it "does not add this plugin's provided attributes to the provides map" do
          plugin
          expect(provides_map.map).to be_empty
        end

        it "saves the plugin's source" do
          expect(plugin.source).to eql(path_to("lake.rb"))
        end
      end

      it "logs a warning if a plugin doesn't exist" do
        expect(Ohai::Log).to receive(:warn).with(/Unable to open or read plugin/)
        loader.load_plugin(path_to("rainier.rb"), path_to("."))
        expect(provides_map.map).to be_empty
      end
    end
  end

  when_plugins_directory "contains invalid plugins" do
    with_plugin("extra_s.rb", <<EOF)
Ohai.plugins(:ExtraS) do
  provides "the_letter_s"
end
EOF

    with_plugin("no_method.rb", <<EOF)
Ohai.plugin(:NoMethod) do
  really_wants "this_attribute"
end
EOF

    with_plugin("illegal_def.rb", <<EOF)
Ohai.plugin(:Zoo) do
  collect_data(:darwin) do
  end
  collect_data(:darwin) do
  end
end
EOF

    with_plugin("unexpected_error.rb", <<EOF)
Ohai.plugin(:Zoo) do
  raise "You aren't expecting this."
end
EOF

    with_plugin("bad_symbol.rb", <<EOF)
Ohai.plugin(:1nval!d) do
  provides "not_a_symbol"
end
EOF

    with_plugin("no_end.rb", <<EOF)
Ohai.plugin(:NoEnd) do
  provides "fish_oil"
  collect_data do
end
EOF

    with_plugin("bad_name.rb", <<EOF)
Ohai.plugin(:you_give_plugins_a_bad_name) do
  provides "that/one/song"
end
EOF

    describe "load_plugin() method" do
      describe "when the plugin uses Ohai.plugin instead of Ohai.plugins" do
        it "logs an unsupported operation warning" do
          expect(Ohai::Log).to receive(:warn).with(/Plugin Method Error: <#{path_to("extra_s.rb")}>:/)
          loader.load_plugin(path_to("extra_s.rb"))
        end

        it "does not raise an error" do
          expect { loader.load_plugin(path_to("extra_s.rb")) }.not_to raise_error
        end
      end

      describe "when the plugin tries to call an unexisting method" do
        it "shoud log an unsupported operation warning" do
          expect(Ohai::Log).to receive(:warn).with(/Plugin Method Error: <#{path_to("no_method.rb")}>:/)
          loader.load_plugin(path_to("no_method.rb"))
        end

        it "does not raise an error" do
          expect { loader.load_plugin(path_to("no_method.rb")) }.not_to raise_error
        end
      end

      describe "when the plugin defines collect_data on the same platform more than once" do
        it "shoud log an illegal plugin definition warning" do
          expect(Ohai::Log).to receive(:warn).with(/Plugin Definition Error: <#{path_to("illegal_def.rb")}>:/)
          loader.load_plugin(path_to("illegal_def.rb"))
        end

        it "does not raise an error" do
          expect { loader.load_plugin(path_to("illegal_def.rb")) }.not_to raise_error
        end
      end

      describe "when an unexpected error is encountered" do
        it "logs a warning" do
          expect(Ohai::Log).to receive(:warn).with(/Plugin Error: <#{path_to("unexpected_error.rb")}>:/)
          loader.load_plugin(path_to("unexpected_error.rb"))
        end

        it "does not raise an error" do
          expect { loader.load_plugin(path_to("unexpected_error.rb")) }.not_to raise_error
        end
      end

      describe "when the plugin name symbol has bad syntax" do
        it "logs a syntax error warning" do
          expect(Ohai::Log).to receive(:warn).with(/Plugin Syntax Error: <#{path_to("bad_symbol.rb")}>:/)
          loader.load_plugin(path_to("bad_symbol.rb"))
        end

        it "does not raise an error" do
          expect { loader.load_plugin(path_to("bad_symbol.rb")) }.not_to raise_error
        end
      end

      describe "when the plugin forgets an 'end'" do
        it "logs a syntax error warning" do
          expect(Ohai::Log).to receive(:warn).with(/Plugin Syntax Error: <#{path_to("no_end.rb")}>:/)
          loader.load_plugin(path_to("no_end.rb"))
        end

        it "does not raise an error" do
          expect { loader.load_plugin(path_to("no_end.rb")) }.not_to raise_error
        end
      end

      describe "when the plugin has an invalid name" do
        it "logs an invalid plugin name warning" do
          expect(Ohai::Log).to receive(:warn).with(/Plugin Name Error: <#{path_to("bad_name.rb")}>:/)
          loader.load_plugin(path_to("bad_name.rb"))
        end

        it "does not raise an error" do
          expect { loader.load_plugin(path_to("bad_name.rb")) }.not_to raise_error
        end
      end

      describe "when plugin directory does not exist" do
        it "logs an invalid plugin path warning" do
          expect(Ohai::Log).to receive(:info).with(/The plugin path.*does not exist/)
          allow(Dir).to receive(:exist?).with("/bogus/dir").and_return(false)
          Ohai::Loader::PluginFile.find_all_in("/bogus/dir")
        end
      end
    end
  end
end