summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerdar Sutay <serdar@opscode.com>2013-12-16 09:56:11 -0800
committerSerdar Sutay <serdar@opscode.com>2013-12-16 09:56:11 -0800
commite5b54391d6cdfa8cdb05c4afa09b1994879544b7 (patch)
treeff4bcb01437ea8694c07b63a23f83dbd136fa680
parent94302c37409227f8f70ab2661d4b54daf325deec (diff)
parent794c1601e8705061d2ec3050179f174928199bfa (diff)
downloadohai-e5b54391d6cdfa8cdb05c4afa09b1994879544b7.tar.gz
Merge pull request #239 from opscode/OC-9924
OC-9924 - Recursively search plugin_path directories for plugins
-rw-r--r--Gemfile4
-rw-r--r--lib/ohai/dsl.rb22
-rw-r--r--lib/ohai/dsl/plugin.rb191
-rw-r--r--lib/ohai/dsl/plugin/versionvi.rb59
-rw-r--r--lib/ohai/dsl/plugin/versionvii.rb100
-rw-r--r--lib/ohai/hints.rb48
-rw-r--r--lib/ohai/loader.rb67
-rw-r--r--lib/ohai/provides_map.rb2
-rw-r--r--lib/ohai/runner.rb2
-rw-r--r--lib/ohai/system.rb47
-rw-r--r--spec/spec_helper.rb2
-rw-r--r--spec/support/integration_helper.rb44
-rw-r--r--spec/unit/dsl/plugin_spec.rb (renamed from spec/ohai/dsl/plugin_spec.rb)69
-rw-r--r--spec/unit/hints_spec.rb65
-rw-r--r--spec/unit/loader_spec.rb134
-rw-r--r--spec/unit/provides_map_spec.rb8
-rw-r--r--spec/unit/runner_spec.rb18
-rw-r--r--spec/unit/system_spec.rb164
18 files changed, 594 insertions, 452 deletions
diff --git a/Gemfile b/Gemfile
index 9b991f32..2b17af9c 100644
--- a/Gemfile
+++ b/Gemfile
@@ -11,8 +11,8 @@ group :development do
gem "sigar", :platform => "ruby"
gem 'plist'
- #gem 'pry'
- #gem 'pry-debugger'
+
+ # gem 'pry-debugger'
# gem 'pry-stack_explorer'
end
diff --git a/lib/ohai/dsl.rb b/lib/ohai/dsl.rb
new file mode 100644
index 00000000..8ebde9de
--- /dev/null
+++ b/lib/ohai/dsl.rb
@@ -0,0 +1,22 @@
+#
+# 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 'ohai/dsl/plugin'
+require 'ohai/dsl/plugin/versionvi'
+require 'ohai/dsl/plugin/versionvii'
diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb
index 2b15cae3..6ebc3b31 100644
--- a/lib/ohai/dsl/plugin.rb
+++ b/lib/ohai/dsl/plugin.rb
@@ -21,10 +21,11 @@
require 'ohai/mixin/os'
require 'ohai/mixin/command'
require 'ohai/mixin/seconds_to_human'
+require 'ohai/hints'
module Ohai
- # for plugin namespacing
+ # For plugin namespacing
module NamedPlugin
# dealing with ruby 1.8
if Module.method(:const_defined?).arity == 1
@@ -40,49 +41,19 @@ module Ohai
def self.plugin(name, &block)
plugin = nil
+
if NamedPlugin.strict_const_defined?(name)
plugin = NamedPlugin.const_get(name)
- if plugin.version.eql?(:version6)
- Ohai::Log.warn("Already loaded version 6 plugin #{name}")
- else
- plugin.class_eval(&block)
- end
+ plugin.class_eval(&block)
else
klass = Class.new(DSL::Plugin::VersionVII, &block)
plugin = NamedPlugin.const_set(name, klass)
end
- plugin
- end
- def self.v6plugin(name_str, &block)
- plugin = nil
- name = nameify(name_str)
- if NamedPlugin.strict_const_defined?(name)
- # log @ debug-level mimics OHAI-6
- Ohai::Log.debug("Already loaded plugin #{name}")
- plugin = NamedPlugin.const_get(name)
- else
- klass = Class.new(DSL::Plugin::VersionVI, &block)
- plugin = NamedPlugin.const_set(name, klass)
- end
plugin
end
- def self.nameify(name_str)
- return name_str if name_str.is_a?(Symbol)
-
- parts = name_str.split(/[^a-zA-Z0-9]/)
- name = ""
- parts.each do |part|
- next if part.eql?("")
- name << part.capitalize
- end
-
- raise ArgumentError, "Invalid plugin name: #{name_str}" if name.eql?("")
- name.to_sym
- end
-
- # cross platform /dev/null
+ # Cross platform /dev/null to support testability
def self.dev_null
if RUBY_PLATFORM =~ /mswin|mingw|windows/
"NUL"
@@ -91,7 +62,8 @@ module Ohai
end
end
- # this methods gets overridden at test time, to force the shell to check
+ # Extracted abs_path to support testability:
+ # This method gets overridden at test time, to force the shell to check
# ohai/spec/unit/path/original/absolute/path/to/exe
def self.abs_path( abs_path )
abs_path
@@ -107,17 +79,11 @@ module Ohai
attr_reader :data
attr_reader :source
- def initialize(controller, source)
- @controller = controller
- @data = controller.data
- @source = source
+ def initialize(data)
+ @data = data
@has_run = false
end
- def provides_map
- @controller.provides_map
- end
-
def run
@has_run = true
run_plugin
@@ -127,125 +93,6 @@ module Ohai
@has_run
end
- #=====================================================
- # version 7 plugin class
- #=====================================================
- class VersionVII < Plugin
- attr_reader :version
-
- def initialize(controller, source)
- super(controller, source)
- @version = :version7
- end
-
- def name
- self.class.name.split("Ohai::NamedPlugin::")[1]
- end
-
- def self.version
- :version7
- end
-
- def self.provides_attrs
- @provides_attrs ||= []
- end
-
- def self.depends_attrs
- @depends_attrs ||= []
- end
-
- def self.data_collector
- @data_collector ||= Mash.new
- end
-
- def self.provides(*attrs)
- attrs.each do |attr|
- provides_attrs << attr
- end
- end
-
- def self.depends(*attrs)
- attrs.each do |attr|
- depends_attrs << attr
- end
- end
-
- def self.collect_data(platform = :default, *other_platforms, &block)
- [platform, other_platforms].flatten.each do |plat|
- if data_collector.has_key?(plat)
- raise Ohai::Exceptions::IllegalPluginDefinition, "collect_data already defined on platform #{plat}"
- else
- data_collector[plat] = block
- end
- end
- end
-
- def dependencies
- self.class.depends_attrs
- end
-
- def run_plugin
- collector = self.class.data_collector
- platform = collect_os
-
- if collector.has_key?(platform)
- self.instance_eval(&collector[platform])
- elsif collector.has_key?(:default)
- self.instance_eval(&collector[:default])
- else
- Ohai::Log.debug("No data to collect for plugin #{self.name}. Continuing...")
- end
- end
-
- def provides(*paths)
- Ohai::Log.warn("[UNSUPPORTED OPERATION] \'provides\' is no longer supported in a \'collect_data\' context. Please specify \'provides\' before collecting plugin data. Ignoring command \'provides #{paths.join(", ")}")
- end
-
- def require_plugin(*args)
- Ohai::Log.warn("[UNSUPPORTED OPERATION] \'require_plugin\' is no longer supported. Please use \'depends\' instead.\nIgnoring plugin(s) #{args.join(", ")}")
- end
- end
-
- #=====================================================
- # version 6 plugin class
- #=====================================================
- class VersionVI < Plugin
- attr_reader :version
-
- def initialize(controller, source)
- super(controller, source)
- @version = :version6
- end
-
- def name
- self.class.name.split("Ohai::NamedPlugin::")[1]
- end
-
- def self.version
- :version6
- end
-
- def self.collect_contents(contents)
- define_method(:run_plugin) { self.instance_eval(contents) }
- end
-
- def provides(*paths)
- provides_map.set_providers_for(self, paths)
- end
-
- def require_plugin(*args)
- @controller.require_plugin(*args)
- end
-
- end
-
- #=====================================================
- # plugin DSL methods
- #=====================================================
- def hints
- @controller.hints
- end
-
def [](key)
@data[key]
end
@@ -297,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/dsl/plugin/versionvi.rb b/lib/ohai/dsl/plugin/versionvi.rb
new file mode 100644
index 00000000..c59fa518
--- /dev/null
+++ b/lib/ohai/dsl/plugin/versionvi.rb
@@ -0,0 +1,59 @@
+#
+# 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 DSL
+ class Plugin
+ class VersionVI < Plugin
+ attr_reader :version
+
+ def initialize(controller, plugin_path)
+ super(controller.data)
+ @controller = controller
+ @version = :version6
+ @plugin_path = plugin_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.
+ @plugin_path
+ end
+
+ def self.version
+ :version6
+ end
+
+ def self.collect_contents(contents)
+ define_method(:run_plugin) { self.instance_eval(contents) }
+ end
+
+ def provides(*paths)
+ Ohai::Log.debug("Skipping provides '#{paths.join(",")}' for plugin '#{name}'")
+ end
+
+ def require_plugin(plugin_ref)
+ @controller.require_plugin(plugin_ref)
+ end
+
+ end
+ end
+ end
+end
+
diff --git a/lib/ohai/dsl/plugin/versionvii.rb b/lib/ohai/dsl/plugin/versionvii.rb
new file mode 100644
index 00000000..4a9713aa
--- /dev/null
+++ b/lib/ohai/dsl/plugin/versionvii.rb
@@ -0,0 +1,100 @@
+#
+# 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 DSL
+ class Plugin
+ class VersionVII < Plugin
+ attr_reader :version
+
+ def initialize(data)
+ super(data)
+ @version = :version7
+ end
+
+ def name
+ self.class.name.split("Ohai::NamedPlugin::")[1]
+ end
+
+ def self.version
+ :version7
+ end
+
+ def self.provides_attrs
+ @provides_attrs ||= []
+ end
+
+ def self.depends_attrs
+ @depends_attrs ||= []
+ end
+
+ def self.data_collector
+ @data_collector ||= Mash.new
+ end
+
+ def self.provides(*attrs)
+ attrs.each do |attr|
+ provides_attrs << attr
+ end
+ end
+
+ def self.depends(*attrs)
+ attrs.each do |attr|
+ depends_attrs << attr
+ end
+ end
+
+ def self.collect_data(platform = :default, *other_platforms, &block)
+ [platform, other_platforms].flatten.each do |plat|
+ if data_collector.has_key?(plat)
+ raise Ohai::Exceptions::IllegalPluginDefinition, "collect_data already defined on platform #{plat}"
+ else
+ data_collector[plat] = block
+ end
+ end
+ end
+
+ def dependencies
+ self.class.depends_attrs
+ end
+
+ def run_plugin
+ collector = self.class.data_collector
+ platform = collect_os
+
+ if collector.has_key?(platform)
+ self.instance_eval(&collector[platform])
+ elsif collector.has_key?(:default)
+ self.instance_eval(&collector[:default])
+ else
+ Ohai::Log.debug("No data to collect for plugin #{self.name}. Continuing...")
+ end
+ end
+
+ def provides(*paths)
+ Ohai::Log.warn("[UNSUPPORTED OPERATION] \'provides\' is no longer supported in a \'collect_data\' context. Please specify \'provides\' before collecting plugin data. Ignoring command \'provides #{paths.join(", ")}")
+ end
+
+ def require_plugin(*args)
+ Ohai::Log.warn("[UNSUPPORTED OPERATION] \'require_plugin\' is no longer supported. Please use \'depends\' instead.\nIgnoring plugin(s) #{args.join(", ")}")
+ end
+ end
+ end
+ end
+end
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/loader.rb b/lib/ohai/loader.rb
index bd698fcc..12fd8087 100644
--- a/lib/ohai/loader.rb
+++ b/lib/ohai/loader.rb
@@ -18,7 +18,7 @@
require 'ohai/log'
require 'ohai/mash'
-require 'ohai/dsl/plugin'
+require 'ohai/dsl'
module Ohai
class Loader
@@ -27,15 +27,10 @@ module Ohai
@controller = controller
end
- def provides_map
- @controller.provides_map
- end
-
- # @note: plugin_name is used only by version 6 plugins and is the
- # unique part of the file name from Ohai::Config[:plugin_path]
- def load_plugin(plugin_path, plugin_name=nil)
+ def load_plugin(plugin_path)
plugin = nil
+ # Read the contents of the plugin to understand if it's a V6 or V7 plugin.
contents = ""
begin
contents << IO.read(plugin_path)
@@ -44,34 +39,52 @@ module Ohai
return plugin
end
+ # We assume that a plugin is a V7 plugin if it contains Ohai.plugin in its contents.
if contents.include?("Ohai.plugin")
- begin
- klass = eval(contents, TOPLEVEL_BINDING)
- plugin = klass.new(@controller, plugin_path) unless klass.nil?
- rescue SystemExit, Interrupt
- raise
- rescue Ohai::Exceptions::IllegalPluginDefinition => e
- Ohai::Log.warn("Plugin at #{plugin_path} is not properly defined: #{e.inspect}")
- rescue NoMethodError => e
- Ohai::Log.warn("[UNSUPPORTED OPERATION] Plugin at #{plugin_path} used unsupported operation \'#{e.name.to_s}\'")
- rescue Exception, Errno::ENOENT => e
- Ohai::Log.warn("Plugin at #{plugin_path} threw exception #{e.inspect} #{e.backtrace.join("\n")}")
- end
-
- return plugin if plugin.nil?
- collect_provides(plugin)
+ plugin = load_v7_plugin(contents, plugin_path)
else
- Ohai::Log.warn("[DEPRECATION] Plugin at #{plugin_path} is a version 6 plugin. Version 6 plugins will not be supported in future releases of Ohai. Please upgrage your plugin to version 7 plugin syntax. For more information visit here: docs.opscode.com/ohai_custom.html")
- klass = Ohai.v6plugin(plugin_name) { collect_contents(contents) }
- plugin = klass.new(@controller, plugin_path)
+ Ohai::Log.warn("[DEPRECATION] Plugin at #{plugin_path} is a version 6 plugin. \
+Version 6 plugins will not be supported in future releases of Ohai. \
+Please upgrade your plugin to version 7 plugin syntax. \
+For more information visit here: docs.opscode.com/ohai_custom.html")
+
+ plugin = load_v6_plugin(contents, plugin_path)
end
plugin
end
+ private
+
def collect_provides(plugin)
plugin_provides = plugin.class.provides_attrs
- provides_map.set_providers_for(plugin, plugin_provides)
+ @controller.provides_map.set_providers_for(plugin, plugin_provides)
+ end
+
+ def load_v6_plugin(contents, plugin_path)
+ klass = Class.new(Ohai::DSL::Plugin::VersionVI) { collect_contents(contents) }
+ klass.new(@controller, plugin_path)
+ end
+
+ def load_v7_plugin(contents, plugin_path)
+ plugin = nil
+
+ begin
+ klass = eval(contents, TOPLEVEL_BINDING)
+ plugin = klass.new(@controller.data) unless klass.nil?
+ rescue SystemExit, Interrupt
+ raise
+ rescue Ohai::Exceptions::IllegalPluginDefinition => e
+ Ohai::Log.warn("Plugin at #{plugin_path} is not properly defined: #{e.inspect}")
+ rescue NoMethodError => e
+ Ohai::Log.warn("[UNSUPPORTED OPERATION] Plugin at #{plugin_path} used unsupported operation \'#{e.name.to_s}\'")
+ rescue Exception, Errno::ENOENT => e
+ Ohai::Log.warn("Plugin at #{plugin_path} threw exception #{e.inspect} #{e.backtrace.join("\n")}")
+ end
+
+ collect_provides(plugin) unless plugin.nil?
+
+ plugin
end
end
diff --git a/lib/ohai/provides_map.rb b/lib/ohai/provides_map.rb
index 2f61f550..d251e7dd 100644
--- a/lib/ohai/provides_map.rb
+++ b/lib/ohai/provides_map.rb
@@ -20,7 +20,7 @@
require 'ohai/mash'
require 'ohai/exception'
require 'ohai/mixin/os'
-require 'ohai/dsl/plugin'
+require 'ohai/dsl'
module Ohai
class ProvidesMap
diff --git a/lib/ohai/runner.rb b/lib/ohai/runner.rb
index 0ead9718..e4650319 100644
--- a/lib/ohai/runner.rb
+++ b/lib/ohai/runner.rb
@@ -17,7 +17,7 @@
# limitations under the License
#
-require 'ohai/dsl/plugin'
+require 'ohai/dsl'
module Ohai
class Runner
diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb
index a5029c29..09b4d62d 100644
--- a/lib/ohai/system.rb
+++ b/lib/ohai/system.rb
@@ -20,12 +20,13 @@ require 'ohai/loader'
require 'ohai/log'
require 'ohai/mash'
require 'ohai/runner'
-require 'ohai/dsl/plugin'
+require 'ohai/dsl'
require 'ohai/mixin/from_file'
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)
@@ -64,26 +65,26 @@ module Ohai
def load_plugins
Ohai::Config[:plugin_path].each do |path|
- [
- Dir[File.join(path, '*')],
- Dir[File.join(path, Ohai::Mixin::OS.collect_os, '**', '*')]
- ].flatten.each do |file|
- file_regex = Regexp.new("#{File.expand_path(path)}#{File::SEPARATOR}(.+).rb$")
- md = file_regex.match(file)
- if md
- plugin_name = md[1].gsub(File::SEPARATOR, "::")
- unless @v6_dependency_solver.has_key?(plugin_name)
- plugin = @loader.load_plugin(file, plugin_name)
- @v6_dependency_solver[plugin_name] = plugin unless plugin.nil?
+ Dir[File.join(path, '**', '*.rb')].each do |plugin_file_path|
+ # Load all the *.rb files under the configured paths in :plugin_path
+ plugin = @loader.load_plugin(plugin_file_path)
+
+ 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(Pathname.new(path)).to_s
+ dep_solver_key = nameify_v6_plugin(partial_path)
+
+ unless @v6_dependency_solver.has_key?(dep_solver_key)
+ @v6_dependency_solver[dep_solver_key] = plugin
else
- Ohai::Log.debug("Already loaded plugin at #{file}")
+ Ohai::Log.debug("Plugin '#{plugin_file_path}' is already loaded.")
end
end
end
end
- true
end
-
+
def run_plugins(safe = false, force = false)
# collect and run version 6 plugins
v6plugins = []
@@ -161,7 +162,7 @@ module Ohai
Ohai::Config[:plugin_path].each do |path|
check_path = File.expand_path(File.join(path, filename))
if File.exist?(check_path)
- plugin = @loader.load_plugin(check_path, plugin_name)
+ plugin = @loader.load_plugin(check_path)
@v6_dependency_solver[plugin_name] = plugin
break
else
@@ -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
@@ -188,9 +191,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)
@@ -233,5 +233,10 @@ module Ohai
end
end
+ def nameify_v6_plugin(partial_path)
+ md = Regexp.new("(.+).rb$").match(partial_path)
+ md[1].gsub(File::SEPARATOR, "::")
+ end
+
end
end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 1b245a89..374fcab2 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -1,6 +1,5 @@
require 'rspec'
-# require 'pry'
# require 'pry-debugger'
# require 'pry-stack_explorer'
@@ -8,6 +7,7 @@ $:.unshift(File.expand_path("../..", __FILE__))
$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'spec/support/platform_helpers'
+require 'spec/support/integration_helper'
require 'ohai'
Ohai::Config[:log_level] = :error
diff --git a/spec/support/integration_helper.rb b/spec/support/integration_helper.rb
new file mode 100644
index 00000000..56f179d2
--- /dev/null
+++ b/spec/support/integration_helper.rb
@@ -0,0 +1,44 @@
+require 'tmpdir'
+
+module IntegrationSupport
+ def when_plugins_directory(description, &block)
+ context "When the plugins directory #{description}" do
+
+ before(:each) do
+ @plugins_directory = Dir.mktmpdir('ohai-plugins')
+ end
+
+ after(:each) do
+ if @plugins_directory
+ begin
+ FileUtils.remove_entry_secure(@plugins_directory)
+ ensure
+ @plugins_directory = nil
+ end
+ end
+ end
+
+ def with_plugin(plugin_path, contents)
+ filename = path_to(plugin_path)
+ dir = File.dirname(filename)
+ FileUtils.mkdir_p(dir) unless dir == '.'
+ File.open(filename, 'w') do |file|
+ file.write(contents)
+ end
+ end
+
+ def path_to(plugin_path)
+ File.expand_path(plugin_path, @plugins_directory)
+ end
+
+ def self.with_plugin(plugin_path, contents)
+ before :each do
+ with_plugin(plugin_path, contents)
+ end
+ end
+
+ instance_eval(&block)
+ end
+ end
+
+end
diff --git a/spec/ohai/dsl/plugin_spec.rb b/spec/unit/dsl/plugin_spec.rb
index 072e293a..62a8fcc2 100644
--- a/spec/ohai/dsl/plugin_spec.rb
+++ b/spec/unit/dsl/plugin_spec.rb
@@ -21,10 +21,6 @@ require File.expand_path("../../../spec_helper.rb", __FILE__)
shared_examples "Ohai::DSL::Plugin" do
context "#initialize" do
- it "should save the plugin source file" do
- plugin.source.should eql(source)
- end
-
it "should set has_run? to false" do
plugin.has_run?.should be_false
end
@@ -95,13 +91,6 @@ describe Ohai::DSL::Plugin::VersionVII do
@name = :Test
end
- it "should log a warning when a version 6 plugin with the same name exists" do
- name_str = @name.to_s.downcase
- Ohai.v6plugin(name_str) { }
- Ohai::Log.should_receive(:warn).with(/Already loaded version 6 plugin #{@name}/)
- Ohai.plugin(@name) { }
- end
-
describe "#version" do
it "should save the plugin version as :version7" do
plugin = Ohai.plugin(@name) { }
@@ -221,7 +210,7 @@ describe Ohai::DSL::Plugin::VersionVII do
describe "#provides (deprecated)" do
it "should log a warning" do
- plugin = Ohai::DSL::Plugin::VersionVII.new(Ohai::System.new, "")
+ plugin = Ohai::DSL::Plugin::VersionVII.new(Mash.new)
Ohai::Log.should_receive(:warn).with(/\[UNSUPPORTED OPERATION\]/)
plugin.provides("attribute")
end
@@ -229,7 +218,7 @@ describe Ohai::DSL::Plugin::VersionVII do
describe "#require_plugin (deprecated)" do
it "should log a warning" do
- plugin = Ohai::DSL::Plugin::VersionVII.new(Ohai::System.new, "")
+ plugin = Ohai::DSL::Plugin::VersionVII.new(Mash.new)
Ohai::Log.should_receive(:warn).with(/\[UNSUPPORTED OPERATION\]/)
plugin.require_plugin("plugin")
end
@@ -237,27 +226,15 @@ describe Ohai::DSL::Plugin::VersionVII do
it_behaves_like "Ohai::DSL::Plugin" do
let(:ohai) { Ohai::System.new }
- let(:source) { "path/plugin.rb" }
- let(:plugin) { Ohai::DSL::Plugin::VersionVII.new(ohai, source) }
+ let(:plugin) { Ohai::DSL::Plugin::VersionVII.new(ohai.data) }
let(:version) { :version7 }
end
end
describe Ohai::DSL::Plugin::VersionVI do
- before(:each) do
- @name = "test"
- @name_sym = :Test
- end
-
- it "should log to debug if a plugin with the same name has been defined" do
- Ohai.plugin(@name_sym) { }
- Ohai::Log.should_receive(:debug).with(/Already loaded plugin #{@name_sym}/)
- Ohai.v6plugin(@name) { }
- end
-
describe "#version" do
it "should save the plugin version as :version6" do
- plugin = Ohai.v6plugin(@name) { }
+ plugin = Class.new(Ohai::DSL::Plugin::VersionVI) { }
plugin.version.should eql(:version6)
end
end
@@ -267,45 +244,23 @@ describe Ohai::DSL::Plugin::VersionVI do
@ohai = Ohai::System.new
end
- it "should collect a single attribute" do
- plugin = Ohai::DSL::Plugin::VersionVI.new(@ohai, "")
+ 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.provides("attribute")
-
- @ohai.provides_map.find_providers_for(["attribute"]).should eq([plugin])
end
- it "should collect a list of attributes" do
- plugin = Ohai::DSL::Plugin::VersionVI.new(@ohai, "")
- plugin.provides("attr1", "attr2", "attr3")
-
- %w[attr1 attr2 attr3].each do |attr|
- @ohai.provides_map.find_providers_for([attr]).should eq([plugin])
- end
- end
-
- it "should collect subattributes of an attribute" do
- plugin = Ohai::DSL::Plugin::VersionVI.new(@ohai, "")
- plugin.provides("attr/subattr")
-
- @ohai.provides_map.find_providers_for(["attr/subattr"]).should eq([plugin])
+ it "should not update the provides map for version 6 plugins." do
+ plugin = Ohai::DSL::Plugin::VersionVI.new(@ohai, "some/plugin/path.rb")
+ plugin.provides("attribute")
+ @ohai.provides_map.map.should be_empty
end
- it "should collect all unique providers for an attribute" do
- plugins = []
- 3.times do
- p = Ohai::DSL::Plugin::VersionVI.new(@ohai, "")
- p.provides("attribute")
- plugins << p
- end
-
- @ohai.provides_map.find_providers_for(["attribute"]).should =~ plugins
- end
end
it_behaves_like "Ohai::DSL::Plugin" do
let(:ohai) { Ohai::System.new }
- let(:source) { "path/plugin.rb" }
- let(:plugin) { Ohai::DSL::Plugin::VersionVI.new(ohai, source) }
+ let(:plugin) { Ohai::DSL::Plugin::VersionVI.new(ohai, "some/plugin/path.rb") }
let(:version) { :version6 }
end
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 8587d721..5547f652 100644
--- a/spec/unit/loader_spec.rb
+++ b/spec/unit/loader_spec.rb
@@ -20,8 +20,14 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
describe Ohai::Loader do
+ extend IntegrationSupport
+
before(:each) do
- @ohai = Ohai::System.new
+ @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
describe "#initialize" do
@@ -31,101 +37,79 @@ describe Ohai::Loader do
end
end
- describe "#load_plugin" do
- before(:each) do
- @name = :Test
- @v6name = "test"
- @path = "test.rb"
+ when_plugins_directory "contains both V6 & V7 plugins" do
+ with_plugin("zoo.rb", <<EOF)
+Ohai.plugin(:Zoo) do
+ provides 'seals'
+end
+EOF
- @loader = Ohai::Loader.new(@ohai)
- @loader.stub(:collect_provides).and_return({})
- end
+ with_plugin("lake.rb", <<EOF)
+provides 'fish'
+EOF
- it "should log a warning if a plugin cannot be loaded" do
- Ohai::Log.should_receive(:warn).with(/Unable to open or read plugin/)
- IO.stub(:read).with(anything()).and_raise(IOError)
- @loader.load_plugin("")
- end
+ describe "load_plugin() method" do
+ it "should load the v7 plugin correctly" do
+ @loader.load_plugin(path_to("zoo.rb"))
+ @provides_map.map.keys.should include("seals")
+ end
- it "should detect a version 6 plugin and emit deprecation message" do
- contents = <<EOF
-provides "test"
-test Mash.new
-EOF
- IO.stub(:read).with(@path).and_return(contents)
- Ohai::Log.should_receive(:warn).with(/\[DEPRECATION\]/)
- plugin = @loader.load_plugin(@path, @v6name)
- plugin.version.should eql(:version6)
+ it "should load the v6 plugin correctly with a depreceation message" do
+ Ohai::Log.should_receive(:warn).with(/\[DEPRECATION\]/)
+ @loader.load_plugin(path_to("lake.rb"))
+ @provides_map.map.should be_empty
+ end
+
+ 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"))
+ @provides_map.map.should be_empty
+ end
end
+ end
+
+ when_plugins_directory "contains invalid plugins" do
+ with_plugin("no_method.rb", <<EOF)
+Ohai.plugin(:Zoo) do
+ provides 'seals'
+end
- it "should detect a version 7 plugin" do
- contents = <<EOF
-Ohai.plugin(:#{@name}) do
+Ohai.blah(:Nasty) do
+ provides 'seals'
end
EOF
- IO.stub(:read).with(@path).and_return(contents)
- plugin = @loader.load_plugin(@path)
- plugin.version.should eql(:version7)
- end
- it "should log a warning when plugin poorly defined" do
- contents = <<EOF
-Ohai.plugin(:#{@name}) do
+ with_plugin("illegal_def.rb", <<EOF)
+Ohai.plugin(:Zoo) do
collect_data(:darwin) do
end
collect_data(:darwin) do
end
end
EOF
- IO.stub(:read).with(@path).and_return(contents)
- Ohai::Log.should_receive(:warn).with(/collect_data already defined on platform/)
- @loader.load_plugin(@path)
- end
- it "should log a warning from NoMethodError when plugin uses a non dsl command" do
- contents = <<EOF
-Ohai.plugin(:#{@name}) do
- requires "test"
+ with_plugin("unexpected_error.rb", <<EOF)
+Ohai.plugin(:Zoo) do
+ raise "You aren't expecting this."
end
EOF
- IO.stub(:read).with(@path).and_return(contents)
- Ohai::Log.should_receive(:warn).with(/\[UNSUPPORTED OPERATION\]/)
- @loader.load_plugin(@path)
- end
- end
-
- describe "#collect_provides" do
- before(:each) do
- @name = :Test
- @path = "test.rb"
- @loader = Ohai::Loader.new(@ohai)
- end
- it "should add provided attributes to Ohai" do
- klass = Ohai.plugin(@name) { provides("attr") }
- plugin = klass.new(@ohai, @path)
- @loader.collect_provides(plugin)
- @ohai.provides_map.find_providers_for(["attr"]).should eq([plugin])
- end
-
- it "should add provided subattributes to Ohai" do
- klass = Ohai.plugin(@name) { provides("attr/sub") }
- plugin = klass.new(@ohai, @plath)
- @loader.collect_provides(plugin)
- @ohai.provides_map.find_providers_for([ "attr/sub" ]).should include(plugin)
- end
-
- it "should collect the unique providers for an attribute" do
- n = 3
- klass = Ohai.plugin(@name) { provides("attr") }
+ describe "load_plugin() method" do
+ it "should log a warning when plugin tries to call an unexisting method" do
+ Ohai::Log.should_receive(:warn).with(/used unsupported operation/)
+ lambda { @loader.load_plugin(path_to("no_method.rb")) }.should_not raise_error
+ end
- plugins = []
- n.times do
- plugins << klass.new(@ohai, @path)
+ it "should log a warning for illegal plugins" do
+ Ohai::Log.should_receive(:warn).with(/not properly defined/)
+ lambda { @loader.load_plugin(path_to("illegal_def.rb")) }.should_not raise_error
end
- plugins.each { |plugin| @loader.collect_provides(plugin) }
- @ohai.provides_map.find_providers_for(["attr"]).should =~ plugins
+ it "should not raise an error during an unexpected exception" do
+ Ohai::Log.should_receive(:warn).with(/threw exception/)
+ lambda { @loader.load_plugin(path_to("unexpected_error.rb")) }.should_not raise_error
+ end
end
end
+
end
diff --git a/spec/unit/provides_map_spec.rb b/spec/unit/provides_map_spec.rb
index 5675335d..b5d905f5 100644
--- a/spec/unit/provides_map_spec.rb
+++ b/spec/unit/provides_map_spec.rb
@@ -23,10 +23,10 @@ describe Ohai::ProvidesMap do
let(:ohai_system) { Ohai::System.new }
let(:provides_map) { Ohai::ProvidesMap.new }
- let(:plugin_1) { Ohai::DSL::Plugin.new(ohai_system, "") }
- let(:plugin_2) { Ohai::DSL::Plugin.new(ohai_system, "") }
- let(:plugin_3) { Ohai::DSL::Plugin.new(ohai_system, "") }
- let(:plugin_4) { Ohai::DSL::Plugin.new(ohai_system, "") }
+ let(:plugin_1) { Ohai::DSL::Plugin.new(ohai_system.data) }
+ let(:plugin_2) { Ohai::DSL::Plugin.new(ohai_system.data) }
+ let(:plugin_3) { Ohai::DSL::Plugin.new(ohai_system.data) }
+ let(:plugin_4) { Ohai::DSL::Plugin.new(ohai_system.data) }
describe "when looking up providing plugins for a single attribute" do
describe "when only one plugin provides the attribute" do
diff --git a/spec/unit/runner_spec.rb b/spec/unit/runner_spec.rb
index 02ac92eb..b2695604 100644
--- a/spec/unit/runner_spec.rb
+++ b/spec/unit/runner_spec.rb
@@ -31,7 +31,7 @@ describe Ohai::Runner, "run_plugin" do
thing(Mash.new)
}
}
- @plugin = klass.new(@ohai, "/tmp/plugins/thing.rb")
+ @plugin = klass.new(@ohai.data)
end
it "should not find dependencies" do
@@ -66,7 +66,7 @@ describe Ohai::Runner, "run_plugin" do
thing(other_thing)
}
}
- @plugin = klass.new(@ohai, "/tmp/plugins/thing.rb")
+ @plugin = klass.new(@ohai.data)
end
it "should raise Ohai::Excpetions::AttributeNotFound" do
@@ -97,7 +97,7 @@ describe Ohai::Runner, "run_plugin" do
@plugins = []
[klass1, klass2].each do |klass|
- @plugins << klass.new(@ohai, "/tmp/plugins/source_dont_matter.rb")
+ @plugins << klass.new(@ohai.data)
end
@plugin1, @plugin2 = @plugins
@@ -130,7 +130,7 @@ describe Ohai::Runner, "run_plugin" do
@plugins = []
[klass1, klass1, klass2].each do |klass|
- @plugins << klass.new(@ohai, "/tmp/plugins/whateva.rb")
+ @plugins << klass.new(@ohai.data)
end
@plugin1, @plugin2, @plugin3 = @plugins
@@ -174,7 +174,7 @@ describe Ohai::Runner, "run_plugin" do
@plugins = []
[klass1, klass2, klass3].each do |klass|
- @plugins << klass.new(@ohai, "/tmp/plugins/number.rb")
+ @plugins << klass.new(@ohai.data)
end
@plugin1, @plugin2, @plugin3 = @plugins
@ohai.provides_map.set_providers_for(@plugin1, ["one", "two"])
@@ -211,7 +211,7 @@ describe Ohai::Runner, "run_plugin" do
@plugins = []
[klass1, klass2].each_with_index do |klass, idx|
- @plugins << klass.new(@ohai, "/tmp/plugins/plugin#{idx}.rb")
+ @plugins << klass.new(@ohai.data)
end
@plugin1, @plugin2 = @plugins
end
@@ -245,7 +245,7 @@ describe Ohai::Runner, "run_plugin" do
@plugins = []
[klassA, klassB, klassC].each do |klass|
- @plugins << klass.new(@ohai, "")
+ @plugins << klass.new(@ohai.data)
end
@pluginA, @pluginB, @pluginC = @plugins
end
@@ -285,7 +285,7 @@ describe Ohai::Runner, "fetch_plugins" do
end
it "should collect the provider" do
- plugin = Ohai::DSL::Plugin.new(@ohai, "")
+ plugin = Ohai::DSL::Plugin.new(@ohai.data)
@ohai.provides_map.set_providers_for(plugin, ["top/middle/bottom"])
dependency_providers = @runner.fetch_plugins(["top/middle/bottom"])
@@ -322,7 +322,7 @@ describe Ohai::Runner, "#get_cycle" do
plugins = []
[klass1, klass2, klass3].each_with_index do |klass, idx|
- plugins << klass.new(@ohai, "/tmp/plugins/plugin#{idx}.rb")
+ plugins << klass.new(@ohai.data)
end
@plugin1, @plugin2, @plugin3 = plugins
end
diff --git a/spec/unit/system_spec.rb b/spec/unit/system_spec.rb
index c4b640b8..53eb5cb6 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.
@@ -20,6 +20,8 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
describe "Ohai::System" do
+ extend IntegrationSupport
+
describe "#initialize" do
before(:each) do
@ohai = Ohai::System.new
@@ -38,44 +40,70 @@ describe "Ohai::System" do
end
end
- describe "#load_plugins" do
- before(:each) do
- Ohai::Mixin::OS.stub(:collect_os).and_return("ubuntu")
+ when_plugins_directory "contains v6 and v7 plugins" do
+ with_plugin("zoo.rb", <<EOF)
+Ohai.plugin(:Zoo) do
+ provides 'seals'
+end
+EOF
- loader = double('@loader')
- Ohai::Loader.stub(:new) { loader }
+ with_plugin("lake.rb", <<EOF)
+provides 'fish'
+EOF
+ before do
@ohai = Ohai::System.new
- klass = Ohai.plugin(:Test) { }
- plugin = klass.new(@ohai, "/tmp/plugins/empty.rb")
- loader.stub(:load_plugin).with("/tmp/plugins/empty.rb", anything()).and_return(plugin)
+ @original_config = Ohai::Config[:plugin_path]
+ Ohai::Config[:plugin_path] = [ path_to(".") ]
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")
+ 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
- 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
+ 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
- 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")
+ after do
+ Ohai::Config[:plugin_path] = @original_config
+ end
+
+ it "load_plugins() should load all the plugins" do
@ohai.load_plugins
- @ohai.v6_dependency_solver.should have_key("empty")
+ @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
@@ -87,10 +115,10 @@ describe "Ohai::System" do
@plugins = []
@names = ['one', 'two', 'three', 'four', 'five']
@names.each do |name|
- k = Ohai.v6plugin(name) {
+ k = Class.new(Ohai::DSL::Plugin::VersionVI) {
collect_contents("")
}
- p = k.new(@ohai, "/tmp/plugins/#{name}.rb")
+ p = k.new(@ohai, "some/plugin/path.rb")
@ohai.v6_dependency_solver[name] = p
@plugins << p
end
@@ -127,7 +155,7 @@ describe "Ohai::System" do
@ohai = Ohai::System.new
klass = Ohai.plugin(:Empty) { }
- plugin = klass.new(@ohai, "/tmp/plugins/empty.rb")
+ plugin = klass.new(@ohai.data)
@ohai.provides_map.should_receive(:all_plugins).and_return([plugin])
end
@@ -162,7 +190,7 @@ describe "Ohai::System" do
@plugins = []
klasses.each do |klass|
- @plugins << klass.new(@ohai, "")
+ @plugins << klass.new(@ohai.data)
end
@ohai.provides_map.should_receive(:all_plugins).and_return(@plugins)
@@ -176,12 +204,8 @@ describe "Ohai::System" do
end
end
- describe "with v6 plugins that depend on v7 plugins" do
- before(:each) do
- @ohai = Ohai::System.new
- loader = Ohai::Loader.new(@ohai)
-
- messages = <<EOF
+ when_plugins_directory "contains v6 plugins that depend on v7 plugins" do
+ with_plugin("messages.rb", <<EOF)
require_plugin 'v6message'
require_plugin 'v7message'
@@ -191,42 +215,36 @@ messages Mash.new
messages[:v6message] = v6message
messages[:v7message] = v7message
EOF
- v6message = <<EOF
+
+ with_plugin("v6message.rb", <<EOF)
provides 'v6message'
v6message "update me!"
EOF
- v7message = <<EOF
+
+ with_plugin("v7message.rb", <<EOF)
Ohai.plugin(:V7message) do
provides 'v7message'
collect_data(:default) do
+ puts "I'm running now."
v7message "v7 plugins are awesome!"
end
end
EOF
- @names = [:Messages, :V6message, :V7message]
- @plugins = []
- [
- [messages, :Messages],
- [v6message, :V6message],
- [v7message, :V7message]
- ].each do |contents, name|
- IO.stub(:read).with("tmp/#{name.to_s.downcase}.rb").and_return(contents)
- @plugins << loader.load_plugin("tmp/#{name.to_s.downcase}.rb", name)
- end
- @plugins.each do |plugin|
- @ohai.v6_dependency_solver[File.basename(plugin.source, '.rb')] = plugin
- end
+ before do
+ @ohai = Ohai::System.new
+ @original_config = Ohai::Config[:plugin_path]
+ Ohai::Config[:plugin_path] = [ path_to(".") ]
end
- it "should run each plugin" do
- @ohai.run_plugins(true)
- @plugins.each { |plugin| plugin.has_run?.should be_true }
+ after do
+ Ohai::Config[:plugin_path] = @original_config
end
it "should collect all data" do
- @ohai.run_plugins(true)
+ pending("Requires some changes to require_plugin() which will be changed in a seperate PR as a next step.")
+ @ohai.all_plugins
[:v6message, :v7message, :messages].each do |attribute|
@ohai.data.should have_key(attribute)
end
@@ -249,7 +267,7 @@ EOF
@plugins = []
@names.each do |name|
k = Ohai.plugin(name) { }
- @plugins << k.new(@ohai, "")
+ @plugins << k.new(@ohai.data)
end
end
@@ -274,8 +292,8 @@ EOF
Ohai::Config[:plugin_path] = ["/tmp/plugins"]
@ohai = Ohai::System.new
- klass = Ohai.v6plugin("empty") { }
- @plugin = klass.new(@ohai, "/tmp/plugins/empty.rb")
+ klass = Class.new(Ohai::DSL::Plugin::VersionVI) { }
+ @plugin = klass.new(@ohai, "some/plugin/path.rb")
@ohai.stub(:plugin_for).with("empty").and_return(@plugin)
end
@@ -339,13 +357,13 @@ provides 'v6attr'
require_plugin 'v7plugin'
v6attr message
EOF
- v6klass = Ohai.v6plugin('v6plugin') { collect_contents(v6string) }
+ v6klass = Class.new(Ohai::DSL::Plugin::VersionVI) { collect_contents(v6string) }
v7klass = Ohai.plugin(:V7plugin) {
provides("message")
collect_data { message("hey.") }
}
- @v6plugin = v6klass.new(@ohai, "/tmp/plugins/v6plugin.rb")
- @v7plugin = v7klass.new(@ohai, "/tmp/plugins/v7plugin.rb")
+ @v6plugin = v6klass.new(@ohai, "some/plugin/path.rb")
+ @v7plugin = v7klass.new(@ohai.data)
@ohai.v6_dependency_solver['v6plugin'] = @v6plugin
@ohai.v6_dependency_solver['v7plugin'] = @v7plugin
@@ -374,7 +392,7 @@ provides 'v6attr'
require_plugin 'v7plugin'
v6attr message
EOF
- v6klass = Ohai.v6plugin('v6plugin') { collect_contents(v6string) }
+ v6klass = Class.new(Ohai::DSL::Plugin::VersionVI) { collect_contents(v6string) }
v7klass = Ohai.plugin(:V7plugin) {
provides("message")
depends("other")
@@ -385,9 +403,9 @@ EOF
collect_data{ other("o hai") }
}
- @v6plugin = v6klass.new(@ohai, "/tmp/plugin/v6plugin.rb")
- @v7plugin = v7klass.new(@ohai, "/tmp/plugins/v7plugin.rb")
- @other = otherklass.new(@ohai, "/tmp/plugins/other.rb")
+ @v6plugin = v6klass.new(@ohai, "some/plugin/path.rb")
+ @v7plugin = v7klass.new(@ohai.data)
+ @other = otherklass.new(@ohai.data)
vds = @ohai.v6_dependency_solver
vds['v6plugin'] = @v6plugin
@@ -429,7 +447,7 @@ EOF
Ohai::Loader.stub(:new) { @loader }
@ohai = Ohai::System.new
- @klass = Ohai.v6plugin('empty') { }
+ @klass = Class.new(Ohai::DSL::Plugin::VersionVI) { }
end
it "should find a plugin with a simple name" do
@@ -437,18 +455,18 @@ EOF
File.stub(:join).with("/tmp/plugins", "empty.rb").and_return("/tmp/plugins/empty.rb")
File.stub(:expand_path).with("/tmp/plugins/empty.rb").and_return("/tmp/plugins/empty.rb")
File.stub(:exist?).with("/tmp/plugins/empty.rb").and_return(true)
- @loader.stub(:load_plugin).with("/tmp/plugins/empty.rb", "empty").and_return(plugin)
+ @loader.stub(:load_plugin).with("/tmp/plugins/empty.rb").and_return(plugin)
found_plugin = @ohai.plugin_for("empty")
found_plugin.should eql(plugin)
end
it "should find a plugin with a complex name" do
- plugin = @klass.new(@ohai, "/tmp/plugins/ubuntu/empty.rb")
+ plugin = @klass.new(@ohai, "/tmp/plugins/empty.rb")
File.stub(:join).with("/tmp/plugins", "ubuntu/empty.rb").and_return("/tmp/plugins/ubuntu/empty.rb")
File.stub(:expand_path).with("/tmp/plugins/ubuntu/empty.rb").and_return("/tmp/plugins/ubuntu/empty.rb")
File.stub(:exist?).with("/tmp/plugins/ubuntu/empty.rb").and_return(true)
- @loader.stub(:load_plugin).with("/tmp/plugins/ubuntu/empty.rb", "ubuntu::empty").and_return(plugin)
+ @loader.stub(:load_plugin).with("/tmp/plugins/ubuntu/empty.rb").and_return(plugin)
found_plugin = @ohai.plugin_for("ubuntu::empty")
found_plugin.should eql(plugin)
@@ -467,7 +485,7 @@ EOF
File.stub(:join).with("/tmp/plugins", "empty.rb").and_return("/tmp/plugins/empty.rb")
File.stub(:expand_path).with("/tmp/plugins/empty.rb").and_return("/tmp/plugins/empty.rb")
File.stub(:exist?).with("/tmp/plugins/empty.rb").and_return(true)
- @loader.stub(:load_plugin).with("/tmp/plugins/empty.rb", "empty").and_return(plugin)
+ @loader.stub(:load_plugin).with("/tmp/plugins/empty.rb").and_return(plugin)
@ohai.plugin_for("empty")
@ohai.v6_dependency_solver.should have_key('empty')