From 1f1fa9bcecfb6d137219819c9aeefdc41f3f9dce Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Tue, 27 Jan 2009 17:50:21 -0600 Subject: 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 --- lib/facter/util/macosx.rb | 34 ++++++++++++++++++++++++---------- spec/unit/util/macosx.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 10 deletions(-) create mode 100755 spec/unit/util/macosx.rb 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 -- cgit