Skip to content

Commit

Permalink
Merge pull request #247 from opscode/OC-10161
Browse files Browse the repository at this point in the history
Make sure :disabled_plugins can disable both v6 and v7 plugins.
  • Loading branch information
Serdar Sutay committed Jan 9, 2014
2 parents a86fa97 + ab1eacf commit d906ae0
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 16 deletions.
8 changes: 5 additions & 3 deletions lib/ohai/dsl/plugin/versionvi.rb
Expand Up @@ -24,17 +24,19 @@ class VersionVI < Plugin
attr_reader :version
attr_reader :source

def initialize(controller, plugin_path)
def initialize(controller, plugin_path, plugin_dir_path)
super(controller.data)
@controller = controller
@version = :version6
@source = plugin_path
@plugin_dir_path = plugin_dir_path
end

def name
# Ohai V6 doesn't have any name specification for plugins.
# So we are using the full path to the plugin as the name of the plugin.
@source
# So we are using the partial path to infer the name of the plugin
partial_path = Pathname.new(@source).relative_path_from(Pathname.new(@plugin_dir_path)).to_s
partial_path.chomp(".rb").gsub("/", "::")
end

def self.version
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/dsl/plugin/versionvii.rb
Expand Up @@ -31,7 +31,7 @@ def initialize(data)
end

def name
self.class.name.split("Ohai::NamedPlugin::")[1]
self.class.name.split("Ohai::NamedPlugin::")[1].capitalize.to_sym
end

def self.version
Expand Down
13 changes: 7 additions & 6 deletions lib/ohai/loader.rb
Expand Up @@ -74,12 +74,13 @@ def load_all
# Load a specified file as an ohai plugin and creates an instance of it.
# Not used by ohai itself, but can be used to load a plugin for testing
# purposes.
def load_plugin(plugin_path)
plugin_class = load_plugin_class(plugin_path)
# plugin_dir_path is required when loading a v6 plugin.
def load_plugin(plugin_path, plugin_dir_path = nil)
plugin_class = load_plugin_class(plugin_path, plugin_dir_path)
return nil unless plugin_class.kind_of?(Class)
case
when plugin_class < Ohai::DSL::Plugin::VersionVI
load_v6_plugin(plugin_class, plugin_path)
load_v6_plugin(plugin_class, plugin_path, plugin_dir_path)
when plugin_class < Ohai::DSL::Plugin::VersionVII
load_v7_plugin(plugin_class)
else
Expand Down Expand Up @@ -129,7 +130,7 @@ def v6_dependency_solver

def collect_v6_plugins
@v6_plugin_classes.each do |plugin_spec|
plugin = load_v6_plugin(plugin_spec.plugin_class, plugin_spec.plugin_path)
plugin = load_v6_plugin(plugin_spec.plugin_class, plugin_spec.plugin_path, plugin_spec.plugin_dir_path)
loaded_v6_plugin(plugin, plugin_spec.plugin_path, plugin_spec.plugin_dir_path)
end
end
Expand All @@ -146,8 +147,8 @@ def load_v6_plugin_class(contents, plugin_path, plugin_dir_path)
plugin_class
end

def load_v6_plugin(plugin_class, plugin_path)
plugin_class.new(@controller, plugin_path)
def load_v6_plugin(plugin_class, plugin_path, plugin_dir_path)
plugin_class.new(@controller, plugin_path, plugin_dir_path)
end

# Capture the plugin in @v6_dependency_solver if it is a V6 plugin
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/dsl/plugin_spec.rb
Expand Up @@ -266,12 +266,12 @@

it "should log a debug message when provides is used" do
Ohai::Log.should_receive(:debug).with(/Skipping provides/)
plugin = Ohai::DSL::Plugin::VersionVI.new(@ohai, "some/plugin/path.rb")
plugin = Ohai::DSL::Plugin::VersionVI.new(@ohai, "/some/plugin/path.rb", "/some/plugin")
plugin.provides("attribute")
end

it "should not update the provides map for version 6 plugins." do
plugin = Ohai::DSL::Plugin::VersionVI.new(@ohai, "some/plugin/path.rb")
plugin = Ohai::DSL::Plugin::VersionVI.new(@ohai, "/some/plugin/path.rb", "/some/plugin")
plugin.provides("attribute")
@ohai.provides_map.map.should be_empty
end
Expand All @@ -280,7 +280,7 @@

it_behaves_like "Ohai::DSL::Plugin" do
let(:ohai) { Ohai::System.new }
let(:plugin) { Ohai::DSL::Plugin::VersionVI.new(ohai, "some/plugin/path.rb") }
let(:plugin) { Ohai::DSL::Plugin::VersionVI.new(ohai, "/some/plugin/path.rb", "/some/plugin") }
let(:version) { :version6 }
end
end
4 changes: 2 additions & 2 deletions spec/unit/loader_spec.rb
Expand Up @@ -77,7 +77,7 @@
describe "when loading a v6 plugin" do
before(:each) do
Ohai::Log.should_receive(:warn).with(/\[DEPRECATION\]/)
@plugin = @loader.load_plugin(path_to("lake.rb"))
@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
Expand All @@ -91,7 +91,7 @@

it "should log a warning if a plugin doesn't exist" do
Ohai::Log.should_receive(:warn).with(/Unable to open or read plugin/)
@loader.load_plugin(path_to("rainier.rb"))
@loader.load_plugin(path_to("rainier.rb"), path_to("."))
@provides_map.map.should be_empty
end
end
Expand Down
88 changes: 87 additions & 1 deletion spec/unit/system_spec.rb
Expand Up @@ -106,6 +106,7 @@
Ohai::NamedPlugin.const_get(:Zoo).should == Ohai::NamedPlugin::Zoo
Ohai::NamedPlugin.const_get(:Nature).should == Ohai::NamedPlugin::Nature
end

end

describe "when running plugins" do
Expand Down Expand Up @@ -139,6 +140,23 @@
@ohai.data[:park].should == "plants"
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 version 6 plugins" do
Ohai::Config[:plugin_path] = [ path_to(".") ]
@ohai.all_plugins
@ohai.data[:zoo].should be_nil
@ohai.data[:park].should == "plants"
end
end

describe "when running in whitelist mode" do
let(:ohai_system) { Ohai::System.new }

Expand Down Expand Up @@ -180,7 +198,7 @@
let(:dependency_plugin_one) { dependency_plugin_one_class.new(ohai_system) }
let(:dependency_plugin_two) { dependency_plugin_two_class.new(ohai_system) }
let(:unrelated_plugin) { unrelated_plugin_class.new(ohai_system) }
let(:v6_plugin) { v6_plugin_class.new(ohai_system, "/v6_plugin.rb") }
let(:v6_plugin) { v6_plugin_class.new(ohai_system, "/v6_plugin.rb", "/") }

before do
ohai_system.stub(:load_plugins) # TODO: temporary hack - don't run unrelated plugins...
Expand Down Expand Up @@ -272,6 +290,74 @@
Ohai::Log.should_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
@ohai.data[:zoo].should be_nil
@ohai.data[:park].should == "plants"
end
end
end

when_plugins_directory "contains v6 & v7 plugins in different directories" do
with_plugin("my_plugins/zoo.rb", <<EOF)
Ohai.plugin(:Zoo) do
provides 'zoo'
collect_data(:default) do
zoo("animals")
end
end
EOF

with_plugin("my_plugins/nature.rb", <<EOF)
Ohai.plugin(:Nature) do
provides 'nature'
collect_data(:default) do
nature("cougars")
end
end
EOF

with_plugin("my_plugins/park.rb", <<EOF)
provides 'park'
park("plants")
EOF

with_plugin("my_plugins/home.rb", <<EOF)
provides 'home'
home("dog")
EOF

describe "when using :disabled_plugins" do
before do
Ohai::Config[:disabled_plugins] = [ :Zoo, 'my_plugins::park' ]
end

after do
Ohai::Config[:disabled_plugins] = [ ]
end

it "shouldn't run disabled plugins" do
Ohai::Config[:plugin_path] = [ path_to(".") ]
@ohai.all_plugins
@ohai.data[:zoo].should be_nil
@ohai.data[:nature].should == "cougars"
@ohai.data[:park].should be_nil
@ohai.data[:home].should == "dog"
end
end
end

when_plugins_directory "contains v6 plugins that depend on v7 plugins" do
Expand Down

0 comments on commit d906ae0

Please sign in to comment.