diff options
-rw-r--r-- | lib/ohai/application.rb | 4 | ||||
-rw-r--r-- | lib/ohai/loader.rb | 7 | ||||
-rw-r--r-- | lib/ohai/system.rb | 2 | ||||
-rw-r--r-- | spec/functional/loader_spec.rb | 53 | ||||
-rw-r--r-- | spec/unit/loader_spec.rb | 104 |
5 files changed, 112 insertions, 58 deletions
diff --git a/lib/ohai/application.rb b/lib/ohai/application.rb index a4b34699..7ecc22de 100644 --- a/lib/ohai/application.rb +++ b/lib/ohai/application.rb @@ -15,6 +15,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +require 'chef-config/path_helper' require 'chef-config/workstation_config_loader' require 'ohai' require 'ohai/log' @@ -32,7 +33,8 @@ class Ohai::Application option :directory, :short => "-d DIRECTORY", :long => "--directory DIRECTORY", - :description => "A directory to add to the Ohai search path" + :description => "A directory to add to the Ohai search path", + :proc => lambda { |path| Ohai::Config.platform_specific_path(path) } option :log_level, :short => "-l LEVEL", diff --git a/lib/ohai/loader.rb b/lib/ohai/loader.rb index 1c7519df..3c023413 100644 --- a/lib/ohai/loader.rb +++ b/lib/ohai/loader.rb @@ -1,6 +1,6 @@ # -# Author:: Claire McQuin (<claire@opscode.com>) -# Copyright:: Copyright (c) 2013 Opscode, Inc. +# Author:: Claire McQuin (<claire@chef.io>) +# Copyright:: Copyright (c) 2013-2015 Chef Software, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,6 +16,7 @@ # limitations under the License. # +require 'chef-config/path_helper' require 'ohai/log' require 'ohai/mash' require 'ohai/dsl' @@ -38,7 +39,7 @@ module Ohai # Finds all the *.rb files under the configured paths in :plugin_path def self.find_all_in(plugin_dir) - Dir[File.join(plugin_dir, "**", "*.rb")].map do |file| + Dir[File.join(ChefConfig::PathHelper.escape_glob(plugin_dir), "**", "*.rb")].map do |file| new(file, plugin_dir) end end diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb index 6b06e2a8..05d0d94b 100644 --- a/lib/ohai/system.rb +++ b/lib/ohai/system.rb @@ -211,7 +211,7 @@ module Ohai Ohai.config.merge!(@config) if Ohai.config[:directory] && - !Ohai.config[:plugin_path].include?(Ohai::Config[:directory]) + !Ohai.config[:plugin_path].include?(Ohai.config[:directory]) Ohai.config[:plugin_path] << Ohai.config[:directory] end end diff --git a/spec/functional/loader_spec.rb b/spec/functional/loader_spec.rb new file mode 100644 index 00000000..34e3d7a1 --- /dev/null +++ b/spec/functional/loader_spec.rb @@ -0,0 +1,53 @@ +# +# 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" + +RSpec.describe "Ohai::Loader" do + let(:loader) { Ohai::Loader.new(Ohai::System.new) } + + describe "#load_all" do + context "when the plugin path contains backslash characters", :windows_only do + let(:plugin_directory) { Dir.mktmpdir("plugins") } + let(:plugin_path) { plugin_directory.gsub("/", "\\") } + + before(:each) do + Ohai.config[:plugin_path] = plugin_path + + plugin_content = <<-EOF +Ohai.plugin(:Foo) do + provides 'foo' +end +EOF + File.open(File.join(plugin_directory, "foo.rb"), 'w+') do |f| + f.write(plugin_content) + end + end + + after(:each) do + FileUtils.rm_rf(plugin_directory) + end + + it "loads all the plugins" do + loader.load_all + loaded_plugins = loader.instance_variable_get(:@v7_plugin_classes) + loaded_plugins_names = loaded_plugins.map { |plugin| plugin.name } + expect(loaded_plugins_names).to eq(["Ohai::NamedPlugin::Foo"]) + end + end + end +end diff --git a/spec/unit/loader_spec.rb b/spec/unit/loader_spec.rb index 38d9abf3..5d79b193 100644 --- a/spec/unit/loader_spec.rb +++ b/spec/unit/loader_spec.rb @@ -22,17 +22,13 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb') describe Ohai::Loader do extend IntegrationSupport - before(:each) do - @plugin_data = Mash.new - @provides_map = Ohai::ProvidesMap.new - - @ohai = double('Ohai::System', :data => @plugin_data, :provides_map => @provides_map) - @loader = Ohai::Loader.new(@ohai) - end + 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 "should return an Ohai::Loader object" do - loader = Ohai::Loader.new(@ohai) + it "returns an Ohai::Loader object" do + loader = Ohai::Loader.new(ohai) expect(loader).to be_a_kind_of(Ohai::Loader) end end @@ -56,43 +52,45 @@ EOF describe "load_plugin() method" do describe "when loading a v7 plugin" do - before(:each) do - @plugin = @loader.load_plugin(path_to("zoo.rb")) - end + let(:plugin) { loader.load_plugin(path_to("zoo.rb")) } - it "should save the plugin according to its attribute" do - expect(@provides_map.map.keys).to include("seals") + it "saves the plugin according to its attribute" do + plugin + expect(provides_map.map.keys).to include("seals") end - it "should save a single plugin source" do - expect(@plugin.source).to eql([path_to("zoo.rb")]) + it "saves a single plugin source" do + expect(plugin.source).to eql([path_to("zoo.rb")]) end - it "should save all plugin sources" do - @loader.load_plugin(path_to("zoo_too.rb")) - expect(@plugin.source).to eql([path_to("zoo.rb"), path_to("zoo_too.rb")]) + 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\]/) - @plugin = @loader.load_plugin(path_to("lake.rb"), path_to(".")) end - it "should not add this plugin's provided attributes to the provides map" do - expect(@provides_map.map).to be_empty + it "does not add this plugin's provided attributes to the provides map" do + plugin + expect(provides_map.map).to be_empty end - it "should save the plugin's source" do - expect(@plugin.source).to eql(path_to("lake.rb")) + it "saves the plugin's source" do + expect(plugin.source).to eql(path_to("lake.rb")) end end - it "should log a warning if a plugin doesn't exist" do + 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 + loader.load_plugin(path_to("rainier.rb"), path_to(".")) + expect(provides_map.map).to be_empty end end end @@ -146,79 +144,79 @@ EOF describe "load_plugin() method" do describe "when the plugin uses Ohai.plugin instead of Ohai.plugins" do - it "should log an unsupported operation warning" 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")) + loader.load_plugin(path_to("extra_s.rb")) end - it "should not raise an error" do - expect{ @loader.load_plugin(path_to("extra_s.rb")) }.not_to raise_error + 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")) + loader.load_plugin(path_to("no_method.rb")) end - it "should not raise an error" do - expect{ @loader.load_plugin(path_to("no_method.rb")) }.not_to raise_error + 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")) + loader.load_plugin(path_to("illegal_def.rb")) end - it "should not raise an error" do - expect{ @loader.load_plugin(path_to("illegal_def.rb")) }.not_to raise_error + 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 "should log a warning" 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")) + loader.load_plugin(path_to("unexpected_error.rb")) end - it "should not raise an error" do - expect{ @loader.load_plugin(path_to("unexpected_error.rb")) }.not_to raise_error + 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 "should log a syntax error warning" 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")) + loader.load_plugin(path_to("bad_symbol.rb")) end - it "should not raise an error" do - expect{ @loader.load_plugin(path_to("bad_symbol.rb")) }.not_to raise_error + 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 "should log a syntax error warning" 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")) + loader.load_plugin(path_to("no_end.rb")) end - it "should not raise an error" do - expect{ @loader.load_plugin(path_to("no_end.rb")) }.not_to raise_error + 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 "should log an invalid plugin name warning" 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")) + loader.load_plugin(path_to("bad_name.rb")) end - it "should not raise an error" do - expect{ @loader.load_plugin(path_to("bad_name.rb")) }.not_to raise_error + it "does not raise an error" do + expect{ loader.load_plugin(path_to("bad_name.rb")) }.not_to raise_error end end end |