summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2009-01-27 17:50:21 -0600
committerLuke Kanies <luke@madstop.com>2009-01-27 18:06:00 -0600
commit1f1fa9bcecfb6d137219819c9aeefdc41f3f9dce (patch)
tree2e9a905fd427c82bf5a9963dda5f290461a763ca
parent5f202c9aaa24581b235d3d9613b3e6ec6ac91d34 (diff)
downloadfacter-1f1fa9bcecfb6d137219819c9aeefdc41f3f9dce.tar.gz
facter-1f1fa9bcecfb6d137219819c9aeefdc41f3f9dce.tar.xz
facter-1f1fa9bcecfb6d137219819c9aeefdc41f3f9dce.zip
Fixing #1838 - profiler failures don't throw exceptions
Also added tests to the macosx code; it's much cleaner and actually tested now. Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r--lib/facter/util/macosx.rb34
-rwxr-xr-xspec/unit/util/macosx.rb47
2 files changed, 71 insertions, 10 deletions
diff --git a/lib/facter/util/macosx.rb b/lib/facter/util/macosx.rb
index 2704a7c..d32d257 100644
--- a/lib/facter/util/macosx.rb
+++ b/lib/facter/util/macosx.rb
@@ -19,24 +19,38 @@
module Facter::Macosx
require 'thread'
require 'facter/util/plist'
+ require 'facter/util/resolution'
# JJM I'd really like to dynamically generate these methods
# by looking at the _name key of the _items dict for each _dataType
+ def self.profiler_xml(data_field)
+ Facter::Util::Resolution.exec("/usr/sbin/system_profiler -xml #{data_field}")
+ end
+
+ def self.intern_xml(xml)
+ return nil unless xml
+ Plist::parse_xml(xml)
+ end
+
+ # Return an xml result, modified as we need it.
+ def self.profiler_data(data_field)
+ begin
+ return nil unless parsed_xml = intern_xml(profiler_xml(data_field))
+ return nil unless data = parsed_xml[0]['_items'][0]
+ data.delete '_name'
+ data
+ rescue
+ return nil
+ end
+ end
+
def self.hardware_overview
- # JJM Perhaps we should cache the XML data in a "class" level object.
- top_level_plist = Plist::parse_xml %x{/usr/sbin/system_profiler -xml SPHardwareDataType}
- system_hardware = top_level_plist[0]['_items'][0]
- system_hardware.delete '_name'
- system_hardware
+ profiler_data("SPHardwareDataType")
end
- # SPSoftwareDataType
def self.os_overview
- top_level_plist = Plist::parse_xml %x{/usr/sbin/system_profiler -xml SPSoftwareDataType}
- os_stuff = top_level_plist[0]['_items'][0]
- os_stuff.delete '_name'
- os_stuff
+ profiler_data("SPSoftwareDataType")
end
def self.sw_vers
diff --git a/spec/unit/util/macosx.rb b/spec/unit/util/macosx.rb
new file mode 100755
index 0000000..01e862d
--- /dev/null
+++ b/spec/unit/util/macosx.rb
@@ -0,0 +1,47 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'facter/util/macosx'
+
+describe Facter::Macosx do
+ it "should be able to retrieve profiler data as xml for a given data field" do
+ Facter::Util::Resolution.expects(:exec).with("/usr/sbin/system_profiler -xml foo").returns "yay"
+ Facter::Macosx.profiler_xml("foo").should == "yay"
+ end
+
+ it "should use PList to convert xml to data structures" do
+ Plist.expects(:parse_xml).with("foo").returns "bar"
+
+ Facter::Macosx.intern_xml("foo").should == "bar"
+ end
+
+ describe "when collecting profiler data" do
+ it "should return the first value in the '_items' hash in the first value of the results of the system_profiler data, with the '_name' field removed, if the profiler returns data" do
+ @result = [
+ '_items' => [
+ {'_name' => "foo", "yay" => "bar"}
+ ]
+ ]
+ Facter::Macosx.expects(:profiler_xml).with("foo").returns "eh"
+ Facter::Macosx.expects(:intern_xml).with("eh").returns @result
+ Facter::Macosx.profiler_data("foo").should == {"yay" => "bar"}
+ end
+
+ it "should return nil if an exception is thrown during parsing of xml" do
+ Facter::Macosx.expects(:profiler_xml).with("foo").returns "eh"
+ Facter::Macosx.expects(:intern_xml).with("eh").raises "boo!"
+ Facter::Macosx.profiler_data("foo").should be_nil
+ end
+ end
+
+ it "should return the profiler data for 'SPHardwareDataType' as the hardware information" do
+ Facter::Macosx.expects(:profiler_data).with("SPHardwareDataType").returns "eh"
+ Facter::Macosx.hardware_overview.should == "eh"
+ end
+
+ it "should return the profiler data for 'SPSoftwareDataType' as the os information" do
+ Facter::Macosx.expects(:profiler_data).with("SPSoftwareDataType").returns "eh"
+ Facter::Macosx.os_overview.should == "eh"
+ end
+end