summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsersut <serdar@opscode.com>2013-12-13 10:39:13 -0800
committersersut <serdar@opscode.com>2013-12-13 11:02:52 -0800
commitb416c535f67ae7b50d823d0ba064aab1297430ba (patch)
treee011e7d867e512152eec0bcb9a0fe6eab8af696f
parentfb5d6325f5431e5337072b4385ece100e9dbf58f (diff)
downloadohai-b416c535f67ae7b50d823d0ba064aab1297430ba.tar.gz
Refactor hints to its own class.
-rw-r--r--lib/ohai/dsl/plugin.rb25
-rw-r--r--lib/ohai/hints.rb48
-rw-r--r--lib/ohai/system.rb12
-rw-r--r--spec/unit/hints_spec.rb65
-rw-r--r--spec/unit/loader_spec.rb2
-rw-r--r--spec/unit/system_spec.rb120
6 files changed, 199 insertions, 73 deletions
diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb
index 038ba444..6ebc3b31 100644
--- a/lib/ohai/dsl/plugin.rb
+++ b/lib/ohai/dsl/plugin.rb
@@ -21,6 +21,7 @@
require 'ohai/mixin/os'
require 'ohai/mixin/command'
require 'ohai/mixin/seconds_to_human'
+require 'ohai/hints'
module Ohai
@@ -92,10 +93,6 @@ module Ohai
@has_run
end
- def hints
- @controller.hints
- end
-
def [](key)
@data[key]
end
@@ -147,25 +144,7 @@ module Ohai
end
def hint?(name)
- @json_parser ||= Yajl::Parser.new
-
- return hints[name] if hints[name]
-
- Ohai::Config[:hints_path].each do |path|
- filename = File.join(path, "#{name}.json")
- if File.exist?(filename)
- begin
- hash = @json_parser.parse(File.read(filename))
- hints[name] = hash || Hash.new # hint
- # should exist because the file did, even if it didn't
- # contain anything
- rescue Yajl::ParseError => e
- Ohai::Log.error("Could not parse hint file at #{filename}: #{e.message}")
- end
- end
- end
-
- hints[name]
+ Ohai::Hints.hint?(name)
end
# emulates the old plugin loading behavior
diff --git a/lib/ohai/hints.rb b/lib/ohai/hints.rb
new file mode 100644
index 00000000..1bab22ea
--- /dev/null
+++ b/lib/ohai/hints.rb
@@ -0,0 +1,48 @@
+#
+# Author:: Serdar Sutay (<serdar@opscode.com>)
+# Copyright:: Copyright (c) 2013 Opscode, 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.
+#
+
+module Ohai
+ module Hints
+ def self.refresh_hints
+ @hints = Hash.new
+ end
+
+ def self.hint?(name)
+ @hints ||= Hash.new
+ return @hints[name] if @hints[name]
+
+ Ohai::Config[:hints_path].each do |path|
+ filename = File.join(path, "#{name}.json")
+ if File.exist?(filename)
+ begin
+ json_parser = Yajl::Parser.new
+ hash = json_parser.parse(File.read(filename))
+ @hints[name] = hash || Hash.new # hint
+ # should exist because the file did, even if it didn't
+ # contain anything
+ rescue Yajl::ParseError => e
+ Ohai::Log.error("Could not parse hint file at #{filename}: #{e.message}")
+ end
+ end
+ end
+
+ @hints[name]
+ end
+ end
+end
diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb
index afbb449e..09b4d62d 100644
--- a/lib/ohai/system.rb
+++ b/lib/ohai/system.rb
@@ -26,6 +26,7 @@ require 'ohai/mixin/command'
require 'ohai/mixin/os'
require 'ohai/mixin/string'
require 'ohai/provides_map'
+require 'ohai/hints'
require 'mixlib/shellout'
require 'yajl'
@@ -35,19 +36,19 @@ module Ohai
class System
attr_accessor :data
attr_reader :provides_map
- attr_reader :hints
attr_reader :v6_dependency_solver
def initialize
@data = Mash.new
@provides_map = ProvidesMap.new
- @hints = Hash.new
@v6_dependency_solver = Hash.new
@plugin_path = ""
@loader = Ohai::Loader.new(self)
@runner = Ohai::Runner.new(self, true)
+
+ Ohai::Hints.refresh_hints()
end
def [](key)
@@ -71,7 +72,7 @@ module Ohai
if plugin && plugin.version == :version6
# Capture the plugin in @v6_dependency_solver if it is a V6 plugin
# to be able to resolve V6 dependencies later on.
- partial_path = Pathname.new(plugin_file_path).relative_path_from(path).to_s
+ partial_path = Pathname.new(plugin_file_path).relative_path_from(Pathname.new(path)).to_s
dep_solver_key = nameify_v6_plugin(partial_path)
unless @v6_dependency_solver.has_key?(dep_solver_key)
@@ -174,6 +175,8 @@ module Ohai
# todo: fix for running w/new internals
# add updated function to v7?
def refresh_plugins(path = '/')
+ Ohai::Hints.refresh_hints()
+
parts = path.split('/')
if parts.length == 0
h = @metadata
@@ -189,9 +192,6 @@ module Ohai
refreshments = collect_plugins(h)
Ohai::Log.debug("Refreshing plugins: #{refreshments.join(", ")}")
- # remove the hints cache
- @hints = Hash.new
-
refreshments.each do |r|
@seen_plugins.delete(r) if @seen_plugins.has_key?(r)
end
diff --git a/spec/unit/hints_spec.rb b/spec/unit/hints_spec.rb
new file mode 100644
index 00000000..9a2c82c8
--- /dev/null
+++ b/spec/unit/hints_spec.rb
@@ -0,0 +1,65 @@
+#
+# Author:: Serdar Sutay (<serdar@opscode.com>)
+# Copyright:: Copyright (c) 2013 Opscode, 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::Hints" do
+ # We are using the plugins directory infrastructure to test hints
+ extend IntegrationSupport
+
+ before do
+ @original_hints = Ohai::Config[:hints_path]
+ end
+
+ after do
+ Ohai::Config[:hints_path] = @original_hints
+ end
+
+ when_plugins_directory "doesn't contain any hints" do
+ before do
+ Ohai::Config[:hints_path] = [ path_to(".") ]
+ end
+
+ it "hint? should return nil" do
+ Ohai::Hints.hint?("cloud").should be_nil
+ end
+ end
+
+ when_plugins_directory "contains empty and full hints" do
+ with_plugin("cloud.json", <<EOF)
+{"name":"circus"}
+EOF
+
+ with_plugin("cloud_empty.json", <<EOF)
+EOF
+
+ before do
+ Ohai::Config[:hints_path] = [ path_to(".") ]
+ end
+
+ it "hint? should return the data for full hints" do
+ Ohai::Hints.hint?("cloud").should == {"name" => "circus"}
+ end
+
+ it "hint? should return empty hash for empty hints" do
+ Ohai::Hints.hint?("cloud_empty").should == { }
+ end
+ end
+
+end
diff --git a/spec/unit/loader_spec.rb b/spec/unit/loader_spec.rb
index d90c0b66..5547f652 100644
--- a/spec/unit/loader_spec.rb
+++ b/spec/unit/loader_spec.rb
@@ -67,7 +67,7 @@ EOF
end
end
end
-
+
when_plugins_directory "contains invalid plugins" do
with_plugin("no_method.rb", <<EOF)
Ohai.plugin(:Zoo) do
diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb
index 4375abf7..94ec0c33 100644
--- a/spec/unit/system_spec.rb
+++ b/spec/unit/system_spec.rb
@@ -7,9 +7,9 @@
# 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.
@@ -18,9 +18,10 @@
#
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
-require 'pry'
describe "Ohai::System" do
+ extend IntegrationSupport
+
describe "#initialize" do
before(:each) do
@ohai = Ohai::System.new
@@ -39,46 +40,72 @@ describe "Ohai::System" do
end
end
- # describe "#load_plugins" do
- # before(:each) do
- # Ohai::Mixin::OS.stub(:collect_os).and_return("ubuntu")
-
- # loader = double('@loader')
- # Ohai::Loader.stub(:new) { loader }
-
- # @ohai = Ohai::System.new
- # klass = Ohai.plugin(:Test) { }
- # plugin = klass.new(@ohai.data)
- # loader.stub(:load_plugin).with("/tmp/plugins/empty.rb").and_return(plugin)
- # end
-
- # it "should load plugins when plugin_path has a trailing slash" do
- # Ohai::Config[:plugin_path] = ["/tmp/plugins/"]
- # Dir.should_receive(:[]).with("/tmp/plugins/*").and_return(["/tmp/plugins/empty.rb"])
- # Dir.should_receive(:[]).with("/tmp/plugins/ubuntu/**/*").and_return([])
- # File.stub(:expand_path).with("/tmp/plugins/").and_return("/tmp/plugins")
- # @ohai.load_plugins
- # end
-
- # it "should log debug message for already loaded plugin" do
- # Ohai::Config[:plugin_path] = ["/tmp/plugins","/tmp/plugins"]
- # Dir.should_receive(:[]).with("/tmp/plugins/*").twice.and_return(["/tmp/plugins/empty.rb"])
- # Dir.should_receive(:[]).with("/tmp/plugins/ubuntu/**/*").twice.and_return([])
- # File.stub(:expand_path).with("/tmp/plugins").and_return("/tmp/plugins")
- # Ohai::Log.should_receive(:debug).with(/Already loaded plugin at/)
- # @ohai.load_plugins
- # end
-
- # it "should add loaded plugins to @v6_dependency_solver" do
- # Ohai::Config[:plugin_path] = ["/tmp/plugins"]
- # Ohai::Mixin::OS.stub(:collect_os).and_return("ubuntu")
- # Dir.should_receive(:[]).with("/tmp/plugins/*").and_return(["/tmp/plugins/empty.rb"])
- # Dir.should_receive(:[]).with("/tmp/plugins/ubuntu/**/*").and_return([])
- # File.stub(:expand_path).with("/tmp/plugins").and_return("/tmp/plugins")
- # @ohai.load_plugins
- # @ohai.v6_dependency_solver.should have_key("empty")
- # end
- # end
+ when_plugins_directory "contains v6 and v7 plugins" do
+ with_plugin("zoo.rb", <<EOF)
+Ohai.plugin(:Zoo) do
+ provides 'seals'
+end
+EOF
+
+ with_plugin("lake.rb", <<EOF)
+provides 'fish'
+EOF
+
+ before do
+ @ohai = Ohai::System.new
+ @original_config = Ohai::Config[:plugin_path]
+ Ohai::Config[:plugin_path] = [ path_to(".") ]
+ end
+
+ it "load_plugins() should load all the plugins" do
+ @ohai.load_plugins
+ @ohai.provides_map.map.keys.should include("seals")
+ @ohai.v6_dependency_solver.keys.should include("lake")
+ Ohai::NamedPlugin.const_get(:Zoo).should == Ohai::NamedPlugin::Zoo
+ end
+ end
+
+ when_plugins_directory "contains directories inside" do
+ with_plugin("repo1/zoo.rb", <<EOF)
+Ohai.plugin(:Zoo) do
+ provides 'seals'
+end
+EOF
+
+ with_plugin("repo1/lake.rb", <<EOF)
+provides 'fish'
+EOF
+
+ with_plugin("repo2/nature.rb", <<EOF)
+Ohai.plugin(:Nature) do
+ provides 'crabs'
+end
+EOF
+
+ with_plugin("repo2/mountain.rb", <<EOF)
+provides 'bear'
+EOF
+
+ before do
+ @ohai = Ohai::System.new
+ @original_config = Ohai::Config[:plugin_path]
+ Ohai::Config[:plugin_path] = [ path_to("repo1"), path_to("repo2") ]
+ end
+
+ after do
+ Ohai::Config[:plugin_path] = @original_config
+ end
+
+ it "load_plugins() should load all the plugins" do
+ @ohai.load_plugins
+ @ohai.provides_map.map.keys.should include("seals")
+ @ohai.provides_map.map.keys.should include("crabs")
+ @ohai.v6_dependency_solver.keys.should include("lake")
+ @ohai.v6_dependency_solver.keys.should include("mountain")
+ Ohai::NamedPlugin.const_get(:Zoo).should == Ohai::NamedPlugin::Zoo
+ Ohai::NamedPlugin.const_get(:Nature).should == Ohai::NamedPlugin::Nature
+ end
+ end
describe "#run_plugins" do
describe "with v6 plugins only" do
@@ -181,6 +208,8 @@ describe "Ohai::System" do
before(:each) do
@ohai = Ohai::System.new
loader = Ohai::Loader.new(@ohai)
+ @original_config = Ohai::Config[:plugin_path]
+ Ohai::Config[:plugin_path] = [ "/tmp" ]
messages = <<EOF
require_plugin 'v6message'
@@ -212,12 +241,17 @@ EOF
[v7message, :V7message]
].each do |contents, name|
IO.stub(:read).with("/tmp/#{name.to_s.downcase}.rb").and_return(contents)
+ File.stub(:exists?).with("/tmp/#{name.to_s.downcase}.rb").and_return(true)
plugin = loader.load_plugin("/tmp/#{name.to_s.downcase}.rb")
@plugins << plugin
@ohai.v6_dependency_solver[name.to_s.downcase] = plugin if plugin.version == :version6
end
end
+ after(:each) do
+ Ohai::Config[:plugin_path] = @original_config
+ end
+
it "should run each plugin" do
@ohai.run_plugins(true)
@plugins.each { |plugin| plugin.has_run?.should be_true }