Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra debug logging and error handling in plugin loading #808

Merged
merged 5 commits into from
Apr 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ohai/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ 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 plugin search path",
:proc => lambda { |path| Ohai::Config.platform_specific_path(path) }

option :log_level,
Expand Down
8 changes: 8 additions & 0 deletions lib/ohai/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class PluginFile < Struct.new(:path, :plugin_root)

# Finds all the *.rb files under the configured paths in :plugin_path
def self.find_all_in(plugin_dir)
unless Dir.exist?(plugin_dir)
Ohai::Log.warn("The plugin path #{plugin_dir} does not exist. Skipping...")
return []
end

Ohai::Log.debug("Searching for Ohai plugins in #{plugin_dir}")

# escape_glob_dir does not exist in 12.7 or below
if ChefConfig::PathHelper.respond_to?(:escape_glob_dir)
escaped = ChefConfig::PathHelper.escape_glob_dir(plugin_dir)
Expand Down Expand Up @@ -105,6 +112,7 @@ def load_plugin_class(plugin_path, plugin_dir_path = nil)
# Read the contents of the plugin to understand if it's a V6 or V7 plugin.
contents = ""
begin
Ohai::Log.debug("Loading plugin at #{plugin_path}")
contents << IO.read(plugin_path)
rescue IOError, Errno::ENOENT
Ohai::Log.warn("Unable to open or read plugin at #{plugin_path}")
Expand Down
2 changes: 2 additions & 0 deletions lib/ohai/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ def configure_ohai
!Ohai.config[:plugin_path].include?(Ohai.config[:directory])
Ohai.config[:plugin_path] << Ohai.config[:directory]
end

Ohai::Log.debug("Running Ohai with the following configuration: #{Ohai.config.configuration}")
end

def configure_logging
Expand Down
1 change: 1 addition & 0 deletions spec/unit/dsl/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@
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")
Expand Down
8 changes: 8 additions & 0 deletions spec/unit/loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@
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(:warn).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