Skip to content

Loading…

OC-10998: Test & Fix Chef ohai resource #274

Merged
merged 8 commits into from

4 participants

@sersut
Chef Software, Inc. member

This PR includes two changes:

1-) Clean the plugin information on a consecutive run of all_plugins in order to pick up any changes to the plugins. Required for the :reload action of ohai resource in Chef.
2-) Turn off / Fix the warnings displayed in the default OHAI run configuration.

sersut added some commits
@sersut sersut Reset the plugin information when all_plugins is called consecutively…
… in order to pick up any changes to the plugins that happened in the meantime.

This is needed for ohai resource of Chef which calls all_plugins during :reload action.
9502152
@sersut sersut Fix and/or Turn off the errors, warnings from ohai in the default run…
… condition.
eabfceb
@sersut sersut commented on the diff
lib/ohai/plugins/sigar/network.rb
@@ -113,10 +113,7 @@ def flags(flags)
f
end
- # From sigar: include/sigar.h sigar_net_route_t
- SIGAR_ROUTE_METHODS = [:destination, :gateway, :mask, :flags, :refcnt, :use, :metric, :mtu, :window, :irtt, :ifname]
-
- collect_data(:aix, :hpux) do
+ collect_data(:hpux) do
@sersut Chef Software, Inc. member
sersut added a note

@lamont-granquist has removed :sigar from the values that can be passed to collect_data() @mcquin.

I'm also seeing another collect_data() block right below this for :default. Am I missing something?

@mcquin
mcquin added a note

Nope... I just saw that other collect_data block for default... Tried to hide my shame.

Why did :default replace :sigar? Plugins that didn't originally have default modes in version 6 will now have them in version 7 (e.g., cpu, filesystem, and memory). Is this what we want? Will the require 'sigar' in default mess anyone up?

@sersut Chef Software, Inc. member
sersut added a note

We've removed :sigar because it's technically not a platform.

By default sigar gem doesn't come with chef. So any data it provides only is available when it's available. I believe we have :windows, :linux etc... available for the plugins you've listed so the default will not be executed really frequently.

However I agree that it's counterintuitive to have sigar in the :default.

Yeah i did some work to make sure that LoadError's thrown by require sigar don't hose the entire run. In some cases there are collect_data(:default) blocks that do an explicit rescue of the LoadError and then have different non-sigar fallback code. In other cases it just blows up because that's the only default behavior we have and you don't get your attributes in the default case without sigar installed. collect_data(:sigar) was actually not doing anything, though and would never be run other than accidentally if it was paired up with a :default or :hpux or :aix that was getting run...

@mcquin
mcquin added a note

Cool. Thanks for the explanation! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@mcquin mcquin commented on an outdated diff
lib/ohai/system.rb
@@ -178,5 +190,23 @@ def attributes_print(a)
end
end
+ private
+ def recursive_remove_constants(object)
+ if object.respond_to?(:constants)
+ object.constants.each do |const|
+ next unless strict_const_defined?(object, const)
+ recursive_remove_constants(object.const_get(const))
+ object.send(:remove_const, const)
+ end
+ end
+ end
+
+ def strict_const_defined?(object, const)
@mcquin
mcquin added a note

Ugh we have this method in so many places. Can we put it in its own module?

@sersut Chef Software, Inc. member
sersut added a note

Totally agreed... Sorry missed this comment @mcquin...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@danielsdeleo danielsdeleo commented on an outdated diff
spec/unit/system_spec.rb
@@ -524,6 +528,39 @@
end
end
+ describe "when Chef OHAI resource executes :reload action" do
+ when_plugins_directory "contains a random plugin" do
+ with_plugin("random.rb", <<-E)
+ Ohai.plugin(:Random) do
+ provides 'random'
+
+ collect_data do
+ random rand(100)
@danielsdeleo Chef Software, Inc. member

I'd either increase this to something like 128 bit (ruby: 1 << 128 ) or use a global variable $test_var ||= 0; $test_var +=1 to make sure the false positive rate is really really low.

@sersut Chef Software, Inc. member
sersut added a note

Got it...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@danielsdeleo danielsdeleo commented on the diff
spec/unit/runner_spec.rb
@@ -284,8 +284,7 @@
end
describe "when a cycle is detected" do
- let(:ohai) { Ohai::System.new }
- let(:runner) { Ohai::Runner.new(ohai, true) }
+ let(:runner) { Ohai::Runner.new(@ohai, true) }
@danielsdeleo Chef Software, Inc. member

What's the benefit of switching to the instance variable style here?

@sersut Chef Software, Inc. member
sersut added a note

Consistency throughout the file.

@danielsdeleo Chef Software, Inc. member

Can we switch in the other direction then? Add a let binding at the top and gsub/@ohai/ohai/ ?

@sersut Chef Software, Inc. member
sersut added a note

:+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@sersut sersut merged commit e0c6d2d into master

1 check passed

Details default The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 14, 2014
  1. @sersut

    Reset the plugin information when all_plugins is called consecutively…

    sersut committed
    … in order to pick up any changes to the plugins that happened in the meantime.
    
    This is needed for ohai resource of Chef which calls all_plugins during :reload action.
  2. @sersut
  3. @sersut
  4. @sersut
Commits on Jan 15, 2014
  1. @sersut

    Fix network plugin specs.

    sersut committed
  2. @sersut
  3. @sersut
  4. @sersut
View
2 lib/ohai/dsl/plugin.rb
@@ -163,7 +163,7 @@ def safe_run
rescue Ohai::Exceptions::Error => e
raise e
rescue => e
- Ohai::Log.error("Plugin #{self.name} threw #{e.inspect}")
+ Ohai::Log.debug("Plugin #{self.name} threw #{e.inspect}")
e.backtrace.each { |line| Ohai::Log.debug( line )}
end
end
View
2 lib/ohai/loader.rb
@@ -165,7 +165,7 @@ def loaded_v6_plugin(plugin, plugin_file_path, plugin_dir_path)
end
def load_v7_plugin_class(contents, plugin_path)
- plugin_class = eval(contents, TOPLEVEL_BINDING)
+ plugin_class = eval(contents, TOPLEVEL_BINDING, plugin_path)
unless plugin_class.kind_of?(Class) and plugin_class < Ohai::DSL::Plugin
raise Ohai::Exceptions::IllegalPluginDefinition, "Plugin file cannot contain any statements after the plugin definition"
end
View
53 lib/ohai/mixin/constant_helper.rb
@@ -0,0 +1,53 @@
+#
+# Author:: Serdar Sutay (<serdar@opscode.com>)
+# Copyright:: Copyright (c) 2014 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 Mixin
+ module ConstantHelper
+
+ def remove_constants
+ new_object_constants = Object.constants - @object_pristine.constants
+ new_object_constants.each do |constant|
+ Object.send(:remove_const, constant) unless Object.const_get(constant).is_a?(Module)
+ end
+
+ recursive_remove_constants(Ohai::NamedPlugin)
+ end
+
+ def recursive_remove_constants(object)
+ if object.respond_to?(:constants)
+ object.constants.each do |const|
+ next unless strict_const_defined?(object, const)
+ recursive_remove_constants(object.const_get(const))
+ object.send(:remove_const, const)
+ end
+ end
+ end
+
+ def strict_const_defined?(object, const)
+ if object.method(:const_defined?).arity == 1
+ object.const_defined?(const)
+ else
+ object.const_defined?(const, false)
+ end
+ end
+
+ end
+ end
+end
View
34 lib/ohai/mixin/network_constants.rb
@@ -0,0 +1,34 @@
+#
+# Author:: Serdar Sutay (<serdar@opscode.com>)
+# Copyright:: Copyright (c) 2014 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 Mixin
+ module NetworkConstants
+ FAMILIES = {
+ "inet" => "default",
+ "inet6" => "default_inet6"
+ }
+
+ # From sigar: include/sigar.h sigar_net_route_t
+ SIGAR_ROUTE_METHODS = [:destination, :gateway, :mask, :flags,
+ :refcnt, :use, :metric, :mtu, :window,
+ :irtt, :ifname]
+ end
+ end
+end
View
2 lib/ohai/plugins/freebsd/network.rb
@@ -20,7 +20,7 @@
provides "network", "network/interfaces"
provides "counters/network", "counters/network/interfaces"
- collect_data do
+ collect_data(:freebsd) do
network Mash.new unless network
network[:interfaces] = Mash.new unless network[:interfaces]
counters Mash.new unless counters
View
14 lib/ohai/plugins/network.rb
@@ -17,17 +17,15 @@
#
require 'ipaddress'
+require 'ohai/mixin/network_constants'
Ohai.plugin(:NetworkAddresses) do
+ include Ohai::Mixin::NetworkConstants
+
provides "ipaddress", "ip6address", "macaddress"
depends "network/interfaces"
- FAMILIES = {
- "inet" => "default",
- "inet6" => "default_inet6"
- }
-
def sorted_ips(family = "inet")
raise "bad family #{family}" unless [ "inet", "inet6" ].include? family
@@ -65,8 +63,8 @@ def find_ip(family = "inet")
return [ nil, nil ] if ips.empty?
# shortcuts to access default #{family} interface and gateway
- int_attr = FAMILIES[family] +"_interface"
- gw_attr = FAMILIES[family] + "_gateway"
+ int_attr = Ohai::Mixin::NetworkConstants::FAMILIES[family] +"_interface"
+ gw_attr = Ohai::Mixin::NetworkConstants::FAMILIES[family] + "_gateway"
# If we have a default interface that has addresses,
# populate the short-cut attributes ipaddress, ip6address and macaddress
@@ -141,7 +139,7 @@ def network_contains_address(address_to_match, ipaddress, iface)
counters[:network] = Mash.new unless counters[:network]
# inet family is treated before inet6
- FAMILIES.keys.sort.each do |family|
+ Ohai::Mixin::NetworkConstants::FAMILIES.keys.sort.each do |family|
r = {}
( r["ip"], r["iface"] ) = find_ip(family)
r["mac"] = find_mac_from_iface(r["iface"]) unless r["iface"].nil?
View
2 lib/ohai/plugins/sigar/filesystem.rb
@@ -19,7 +19,7 @@
Ohai.plugin(:Filesystem) do
provides "filesystem"
- collect_data(:aix, :hpux, :default) do
+ collect_data(:hpux, :default) do
require "sigar"
sigar = Sigar.new
View
11 lib/ohai/plugins/sigar/network.rb
@@ -16,9 +16,11 @@
# limitations under the License.
#
-#http://github.com/mdkent/ohai/commit/92f51aa18b6add9682510a87dcf94835ea72b04d
+require 'ohai/mixin/network_constants'
Ohai.plugin(:Network) do
+ include Ohai::Mixin::NetworkConstants
+
provides "network", "network/interfaces"
provides "counters/network", "counters/network/interfaces"
@@ -113,10 +115,7 @@ def flags(flags)
f
end
- # From sigar: include/sigar.h sigar_net_route_t
- SIGAR_ROUTE_METHODS = [:destination, :gateway, :mask, :flags, :refcnt, :use, :metric, :mtu, :window, :irtt, :ifname]
-
- collect_data(:aix, :hpux) do
+ collect_data(:hpux) do
@sersut Chef Software, Inc. member
sersut added a note

@lamont-granquist has removed :sigar from the values that can be passed to collect_data() @mcquin.

I'm also seeing another collect_data() block right below this for :default. Am I missing something?

@mcquin
mcquin added a note

Nope... I just saw that other collect_data block for default... Tried to hide my shame.

Why did :default replace :sigar? Plugins that didn't originally have default modes in version 6 will now have them in version 7 (e.g., cpu, filesystem, and memory). Is this what we want? Will the require 'sigar' in default mess anyone up?

@sersut Chef Software, Inc. member
sersut added a note

We've removed :sigar because it's technically not a platform.

By default sigar gem doesn't come with chef. So any data it provides only is available when it's available. I believe we have :windows, :linux etc... available for the plugins you've listed so the default will not be executed really frequently.

However I agree that it's counterintuitive to have sigar in the :default.

Yeah i did some work to make sure that LoadError's thrown by require sigar don't hose the entire run. In some cases there are collect_data(:default) blocks that do an explicit rescue of the LoadError and then have different non-sigar fallback code. In other cases it just blows up because that's the only default behavior we have and you don't get your attributes in the default case without sigar installed. collect_data(:sigar) was actually not doing anything, though and would never be run other than accidentally if it was paired up with a :default or :hpux or :aix that was getting run...

@mcquin
mcquin added a note

Cool. Thanks for the explanation! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
require "sigar"
sigar = Sigar.new
@@ -156,7 +155,7 @@ def flags(flags)
# should never happen
network[:interfaces][route.ifname][:route] = Mash.new unless network[:interfaces][route.ifname][:route]
route_data={}
- SIGAR_ROUTE_METHODS.each do |m|
+ Ohai::Mixin::NetworkConstants::SIGAR_ROUTE_METHODS.each do |m|
if(m == :flags)
route_data[m]=flags(route.send(m))
else
View
14 lib/ohai/plugins/sigar/network_route.rb
@@ -16,10 +16,13 @@
# limitations under the License.
#
-require "sigar"
+require 'ohai/mixin/network_constants'
-Ohai.plugin do
- provides "network"
+Ohai.plugin(:NetworkRoutes) do
+ include Ohai::Mixin::NetworkConstants
+
+ provides "network/interfaces/adapters/route"
+ depends "network/interfaces"
def flags(flags)
f = ""
@@ -35,9 +38,6 @@ def flags(flags)
f
end
- # From sigar: include/sigar.h sigar_net_route_t
- SIGAR_ROUTE_METHODS = [:destination, :gateway, :mask, :flags, :refcnt, :use, :metric, :mtu, :window, :irtt, :ifname]
-
collect_data(:default) do
require "sigar"
sigar = Sigar.new
@@ -46,7 +46,7 @@ def flags(flags)
next unless network[:interfaces][route.ifname] # this should never happen
network[:interfaces][route.ifname][:route] = Mash.new unless network[:interfaces][route.ifname][:route]
route_data={}
- SIGAR_ROUTE_METHODS.each do |m|
+ Ohai::Mixin::NetworkConstants::SIGAR_ROUTE_METHODS.each do |m|
if(m == :flags)
route_data[m]=flags(route.send(m))
else
View
2 lib/ohai/plugins/sigar/platform.rb
@@ -19,7 +19,7 @@
Ohai.plugin(:Platform) do
provides "platform", "platform_version", "platform_family"
- collect_data(:hpux, :default) do
+ collect_data(:hpux) do
require "sigar"
sys = Sigar.new.sys_info
View
23 lib/ohai/system.rb
@@ -24,6 +24,7 @@
require 'ohai/mixin/command'
require 'ohai/mixin/os'
require 'ohai/mixin/string'
+require 'ohai/mixin/constant_helper'
require 'ohai/provides_map'
require 'ohai/hints'
require 'mixlib/shellout'
@@ -31,23 +32,31 @@
require 'yajl'
module Ohai
-
class System
+ include Ohai::Mixin::ConstantHelper
+
attr_accessor :data
attr_reader :provides_map
attr_reader :v6_dependency_solver
def initialize
+ @plugin_path = ""
+ reset_system
+ end
+
+ def reset_system
@data = Mash.new
@provides_map = ProvidesMap.new
@v6_dependency_solver = Hash.new
- @plugin_path = ""
@loader = Ohai::Loader.new(self)
@runner = Ohai::Runner.new(self, true)
Ohai::Hints.refresh_hints()
+
+ # Remove the previously defined plugins
+ recursive_remove_constants(Ohai::NamedPlugin)
end
def [](key)
@@ -55,6 +64,11 @@ def [](key)
end
def all_plugins(attribute_filter=nil)
+ # Reset the system when all_plugins is called since this function
+ # can be run multiple times in order to pick up any changes in the
+ # config or plugins with Chef.
+ reset_system
+
load_plugins
run_plugins(true, attribute_filter)
end
@@ -138,10 +152,10 @@ def require_plugin(plugin_ref, force=false)
# of them whenever called.
def refresh_plugins(attribute_filter=nil)
Ohai::Hints.refresh_hints()
- @provides_map.all_plugins(Array(attribute_filter)).each do |plugin|
+ @provides_map.all_plugins(attribute_filter).each do |plugin|
plugin.reset!
end
- run_plugins(true, Array(attribute_filter))
+ run_plugins(true, attribute_filter)
end
#
@@ -177,6 +191,5 @@ def attributes_print(a)
raise ArgumentError, "I can only generate JSON for Hashes, Mashes, Arrays and Strings. You fed me a #{data.class}!"
end
end
-
end
end
View
27 spec/spec_helper.rb
@@ -19,32 +19,7 @@
config.after(:each) { remove_constants }
end
-def remove_constants
- new_object_constants = Object.constants - @object_pristine.constants
- new_object_constants.each do |constant|
- Object.send(:remove_const, constant) unless Object.const_get(constant).is_a?(Module)
- end
-
- recursive_remove_constants(Ohai::NamedPlugin)
-end
-
-def recursive_remove_constants(object)
- if object.respond_to?(:constants)
- object.constants.each do |const|
- next unless strict_const_defined?(object, const)
- recursive_remove_constants(object.const_get(const))
- object.send(:remove_const, const)
- end
- end
-end
-
-def strict_const_defined?(object, const)
- if object.method(:const_defined?).arity == 1
- object.const_defined?(const)
- else
- object.const_defined?(const, false)
- end
-end
+include Ohai::Mixin::ConstantHelper
if Ohai::Mixin::OS.collect_os == /mswin|mingw32|windows/
ENV["PATH"] = ""
View
2 spec/unit/plugins/root_group_spec.rb
@@ -18,8 +18,6 @@
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper.rb')
-ORIGINAL_CONFIG_HOST_OS = ::RbConfig::CONFIG['host_os']
-
describe Ohai::System, 'root_group' do
before(:each) do
@plugin = get_plugin("root_group")
View
10 spec/unit/runner_spec.rb
@@ -284,8 +284,7 @@
end
describe "when a cycle is detected" do
- let(:ohai) { Ohai::System.new }
- let(:runner) { Ohai::Runner.new(ohai, true) }
+ let(:runner) { Ohai::Runner.new(@ohai, true) }
@danielsdeleo Chef Software, Inc. member

What's the benefit of switching to the instance variable style here?

@sersut Chef Software, Inc. member
sersut added a note

Consistency throughout the file.

@danielsdeleo Chef Software, Inc. member

Can we switch in the other direction then? Add a let binding at the top and gsub/@ohai/ohai/ ?

@sersut Chef Software, Inc. member
sersut added a note

:+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
context "when there are no edges in the cycle (A->A)" do
let(:plugin_class) do
@@ -297,10 +296,10 @@
end
end
end
- let(:plugin) { plugin_class.new(ohai.data) }
+ let(:plugin) { plugin_class.new(@ohai.data) }
it "ignores the cycle" do
- ohai.provides_map.set_providers_for(plugin, ["thing"])
+ @ohai.provides_map.set_providers_for(plugin, ["thing"])
expected_error_string = "Dependency cycle detected. Please refer to the following plugins: Thing, Other"
runner.run_plugin(plugin) # should not raise
@@ -327,8 +326,9 @@
@plugins = []
[klass1, klass2].each_with_index do |klass, idx|
- @plugins << klass.new(ohai.data)
+ @plugins << klass.new(@ohai.data)
end
+
@plugin1, @plugin2 = @plugins
end
View
169 spec/unit/system_spec.rb
@@ -23,21 +23,19 @@
describe "Ohai::System" do
extend IntegrationSupport
- describe "#initialize" do
- before(:each) do
- @ohai = Ohai::System.new
- end
+ let(:ohai) { Ohai::System.new }
+ describe "#initialize" do
it "should return an Ohai::System object" do
- @ohai.should be_a_kind_of(Ohai::System)
+ ohai.should be_a_kind_of(Ohai::System)
end
it "should set @attributes to a ProvidesMap" do
- @ohai.provides_map.should be_a_kind_of(Ohai::ProvidesMap)
+ ohai.provides_map.should be_a_kind_of(Ohai::ProvidesMap)
end
it "should set @v6_dependency_solver to a Hash" do
- @ohai.v6_dependency_solver.should be_a_kind_of(Hash)
+ ohai.v6_dependency_solver.should be_a_kind_of(Hash)
end
end
@@ -53,15 +51,14 @@
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.rb")
+ ohai.load_plugins
+ ohai.provides_map.map.keys.should include("seals")
+ ohai.v6_dependency_solver.keys.should include("lake.rb")
Ohai::NamedPlugin.const_get(:Zoo).should == Ohai::NamedPlugin::Zoo
end
end
@@ -88,7 +85,6 @@
EOF
before do
- @ohai = Ohai::System.new
@original_config = Ohai::Config[:plugin_path]
Ohai::Config[:plugin_path] = [ path_to("repo1"), path_to("repo2") ]
end
@@ -98,11 +94,11 @@
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.rb")
- @ohai.v6_dependency_solver.keys.should include("mountain.rb")
+ 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.rb")
+ ohai.v6_dependency_solver.keys.should include("mountain.rb")
Ohai::NamedPlugin.const_get(:Zoo).should == Ohai::NamedPlugin::Zoo
Ohai::NamedPlugin.const_get(:Nature).should == Ohai::NamedPlugin::Nature
end
@@ -118,10 +114,6 @@
Ohai::Config[:plugin_path] = @original_config
end
- before(:each) do
- @ohai = Ohai::System.new
- end
-
when_plugins_directory "contains v6 plugins only" do
with_plugin("zoo.rb", <<EOF)
provides 'zoo'
@@ -135,9 +127,9 @@
it "should collect data from all the plugins" do
Ohai::Config[:plugin_path] = [ path_to(".") ]
- @ohai.all_plugins
- @ohai.data[:zoo].should == "animals"
- @ohai.data[:park].should == "plants"
+ ohai.all_plugins
+ ohai.data[:zoo].should == "animals"
+ ohai.data[:park].should == "plants"
end
describe "when using :disabled_plugins" do
@@ -151,9 +143,9 @@
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"
+ ohai.all_plugins
+ ohai.data[:zoo].should be_nil
+ ohai.data[:park].should == "plants"
end
end
@@ -209,7 +201,9 @@
ohai_system.v6_dependency_solver["v6_plugin"] = v6_plugin
- ohai_system.all_plugins("primary")
+ # Instead of calling all plugins we call load and run directly so that the information we setup is not cleared by all_plugins
+ ohai_system.load_plugins
+ ohai_system.run_plugins(true, "primary")
end
# This behavior choice is somewhat arbitrary, based on what creates the
@@ -252,8 +246,8 @@
it "should collect platform specific" do
Ohai::Config[:plugin_path] = [ path_to(".") ]
- @ohai.all_plugins
- @ohai.data[:message].should == "platform_specific_message"
+ ohai.all_plugins
+ ohai.data[:message].should == "platform_specific_message"
end
end
@@ -279,16 +273,18 @@
it "should collect data from all the plugins" do
Ohai::Config[:plugin_path] = [ path_to(".") ]
- @ohai.all_plugins
- @ohai.data[:zoo].should == "animals"
- @ohai.data[:park].should == "plants"
+ ohai.all_plugins
+ ohai.data[:zoo].should == "animals"
+ ohai.data[:park].should == "plants"
end
it "should write an error to Ohai::Log" do
Ohai::Config[:plugin_path] = [ path_to(".") ]
- @ohai.instance_variable_get("@runner").stub(:run_plugin).and_raise(Ohai::Exceptions::AttributeNotFound)
+ # Make sure the stubbing of runner is not overriden with reset_system during test
+ ohai.stub(:reset_system)
+ ohai.instance_variable_get("@runner").stub(:run_plugin).and_raise(Ohai::Exceptions::AttributeNotFound)
Ohai::Log.should_receive(:error).with(/Encountered error while running plugins/)
- expect { @ohai.all_plugins }.to raise_error(Ohai::Exceptions::AttributeNotFound)
+ expect { ohai.all_plugins }.to raise_error(Ohai::Exceptions::AttributeNotFound)
end
describe "when using :disabled_plugins" do
@@ -302,9 +298,9 @@
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"
+ ohai.all_plugins
+ ohai.data[:zoo].should be_nil
+ ohai.data[:park].should == "plants"
end
end
end
@@ -351,11 +347,11 @@
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"
+ 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
@@ -388,7 +384,6 @@
EOF
before do
- @ohai = Ohai::System.new
@original_config = Ohai::Config[:plugin_path]
Ohai::Config[:plugin_path] = [ path_to(".") ]
end
@@ -398,16 +393,16 @@
end
it "should collect all data" do
- @ohai.all_plugins
+ ohai.all_plugins
[:v6message, :v7message, :messages].each do |attribute|
- @ohai.data.should have_key(attribute)
+ ohai.data.should have_key(attribute)
end
- @ohai.data[:v6message].should eql("update me!")
- @ohai.data[:v7message].should eql("v7 plugins are awesome!")
+ ohai.data[:v6message].should eql("update me!")
+ ohai.data[:v7message].should eql("v7 plugins are awesome!")
[:v6message, :v7message].each do |subattr|
- @ohai.data[:messages].should have_key(subattr)
- @ohai.data[:messages][subattr].should eql(@ohai.data[subattr])
+ ohai.data[:messages].should have_key(subattr)
+ ohai.data[:messages][subattr].should eql(ohai.data[subattr])
end
end
end
@@ -432,7 +427,6 @@
EOF
before do
- @ohai = Ohai::System.new
@original_config = Ohai::Config[:plugin_path]
Ohai::Config[:plugin_path] = [ path_to(".") ]
end
@@ -442,9 +436,9 @@
end
it "version 6 should run" do
- @ohai.load_plugins
- @ohai.require_plugin("message")
- @ohai.data[:message].should eql("From Version 6")
+ ohai.load_plugins
+ ohai.require_plugin("message")
+ ohai.data[:message].should eql("From Version 6")
end
end
@@ -481,7 +475,6 @@
EOF
before do
- @ohai = Ohai::System.new
@original_config = Ohai::Config[:plugin_path]
Ohai::Config[:plugin_path] = [ path_to(".") ]
end
@@ -491,11 +484,11 @@
end
it "should collect all the data properly" do
- @ohai.all_plugins
- @ohai.data[:v7message].should == "Hellos from 7: animals"
- @ohai.data[:zoo].should == "animals"
- @ohai.data[:message][:v6message].should == "Hellos from 6"
- @ohai.data[:message][:copy_message].should == "Hellos from 7: animals"
+ ohai.all_plugins
+ ohai.data[:v7message].should == "Hellos from 7: animals"
+ ohai.data[:zoo].should == "animals"
+ ohai.data[:message][:v6message].should == "Hellos from 6"
+ ohai.data[:message][:copy_message].should == "Hellos from 7: animals"
end
end
@@ -509,7 +502,6 @@
EOF
before do
- @ohai = Ohai::System.new
@original_config = Ohai::Config[:plugin_path]
Ohai::Config[:plugin_path] = [ path_to(".") ]
end
@@ -519,8 +511,40 @@
end
it "should raise DependencyNotFound" do
- lambda { @ohai.all_plugins }.should raise_error(Ohai::Exceptions::DependencyNotFound)
+ lambda { ohai.all_plugins }.should raise_error(Ohai::Exceptions::DependencyNotFound)
+ end
+ end
+ end
+
+ describe "when Chef OHAI resource executes :reload action" do
+ when_plugins_directory "contains a random plugin" do
+ with_plugin("random.rb", <<-E)
+ Ohai.plugin(:Random) do
+ provides 'random'
+
+ collect_data do
+ random rand(1 << 32)
+ end
+ end
+ E
+
+ before do
+ @original_config = Ohai::Config[:plugin_path]
+ Ohai::Config[:plugin_path] = [ path_to(".") ]
end
+
+ after do
+ Ohai::Config[:plugin_path] = @original_config
+ end
+
+ it "should rerun the plugin providing the desired attributes" do
+ ohai.all_plugins
+ initial_value = ohai.data["random"]
+ ohai.all_plugins
+ updated_value = ohai.data["random"]
+ initial_value.should_not == updated_value
+ end
+
end
end
@@ -564,12 +588,11 @@
E
before do
- @ohai = Ohai::System.new
@original_config = Ohai::Config[:plugin_path]
Ohai::Config[:plugin_path] = [ path_to(".") ]
Ohai::Log.init(STDOUT)
Ohai::Log.level = :debug
- @ohai.all_plugins
+ ohai.all_plugins
end
after do
@@ -577,21 +600,21 @@
end
it "should rerun the plugin providing the desired attributes" do
- @ohai.data[:desired_attr_count].should == 1
- @ohai.refresh_plugins("desired_attr")
- @ohai.data[:desired_attr_count].should == 2
+ ohai.data[:desired_attr_count].should == 1
+ ohai.refresh_plugins("desired_attr")
+ ohai.data[:desired_attr_count].should == 2
end
it "should not re-run dependencies of the plugin providing the desired attributes" do
- @ohai.data[:depended_attr_count].should == 1
- @ohai.refresh_plugins("desired_attr")
- @ohai.data[:depended_attr_count].should == 1
+ ohai.data[:depended_attr_count].should == 1
+ ohai.refresh_plugins("desired_attr")
+ ohai.data[:depended_attr_count].should == 1
end
it "should not re-run plugins unrelated to the plugin providing the desired attributes" do
- @ohai.data[:other_attr_count].should == 1
- @ohai.refresh_plugins("desired_attr")
- @ohai.data[:other_attr_count].should == 1
+ ohai.data[:other_attr_count].should == 1
+ ohai.refresh_plugins("desired_attr")
+ ohai.data[:other_attr_count].should == 1
end
end
Something went wrong with that request. Please try again.