Skip to content

Commit

Permalink
Remove whitespace in DMI facts (#3008, #3011)
Browse files Browse the repository at this point in the history
In addition to the stripping of the output of these facts this patchset:

Refactor - Extracted function to enable easier testing.
Tests - data driven tests for the dmidecode/smbios fact.

Paul
  • Loading branch information
Paul Nasrat committed Jan 10, 2010
1 parent feecd39 commit 356cf15
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/facter/util/manufacturer.rb
Expand Up @@ -2,8 +2,8 @@
# Support methods for manufacturer specific facts

module Facter::Manufacturer
def self.dmi_find_system_info(name)
splitstr="Handle"

def self.get_dmi_table()
case Facter.value(:kernel)
when 'Linux'
return nil unless FileTest.exists?("/usr/sbin/dmidecode")
Expand All @@ -19,18 +19,23 @@ def self.dmi_find_system_info(name)
output=%x{/usr/pkg/sbin/dmidecode 2>/dev/null}
when 'SunOS'
return nil unless FileTest.exists?("/usr/sbin/smbios")
splitstr="ID SIZE TYPE"
output=%x{/usr/sbin/smbios 2>/dev/null}

output=%x{/usr/sbin/smbios 2>/dev/null}
else
return
output=nil
end
return output
end

def self.dmi_find_system_info(name)
splitstr= Facter.value(:kernel) == 'SunOS' ? "ID SIZE TYPE" : "Handle"
output = self.get_dmi_table()
name.each_pair do |key,v|
v.each do |v2|
v2.each_pair do |value,facterkey|
output.split(splitstr).each do |line|
if line =~ /#{key}/ and ( line =~ /#{value} 0x\d+ \(([-\w].*)\)\n*./ or line =~ /#{value} ([-\w].*)\n*./ )
result = $1
result = $1.strip
Facter.add(facterkey) do
confine :kernel => [ :linux, :freebsd, :netbsd, :sunos ]
setcode do
Expand Down
60 changes: 60 additions & 0 deletions spec/unit/data/linux_dmidecode_with_spaces
@@ -0,0 +1,60 @@
# dmidecode 2.2
SMBIOS 2.3 present.
32 structures occupying 994 bytes.
Table at 0x000F0800.
Handle 0x0000
DMI type 0, 20 bytes.
BIOS Information
Vendor: Award Software International, Inc.
Version: 6.00 PG
Release Date: 01/03/2003
Address: 0xE0000
Runtime Size: 128 kB
ROM Size: 256 kB
Characteristics:
ISA is supported
PCI is supported
PNP is supported
APM is supported
BIOS is upgradeable
BIOS shadowing is allowed
ESCD support is available
Boot from CD is supported
Selectable boot is supported
BIOS ROM is socketed
EDD is supported
5.25"/360 KB floppy services are supported (int 13h)
5.25"/1.2 MB floppy services are supported (int 13h)
3.5"/720 KB floppy services are supported (int 13h)
3.5"/2.88 MB floppy services are supported (int 13h)
Print screen service is supported (int 5h)
8042 keyboard services are supported (int 9h)
Serial services are supported (int 14h)
Printer services are supported (int 17h)
CGA/mono video services are supported (int 10h)
ACPI is supported
USB legacy is supported
AGP is supported
LS-120 boot is supported
ATAPI Zip drive boot is supported
Handle 0x0001
DMI type 1, 25 bytes.
System Information
Manufacturer: MICRO-STAR INTERNATIONAL CO., LTD
Product Name: MS-6754
Version:
Serial Number:
UUID: Not Present
Wake-up Type: Power Switch
Handle 0x0002
DMI type 2, 8 bytes.
Base Board Information
Manufacturer: MICRO-STAR INTERNATIONAL CO., LTD
Product Name: MS-6754
Version:
Serial Number:

Handle 0x001F
DMI type 127, 4 bytes.
End Of Table

26 changes: 26 additions & 0 deletions spec/unit/util/manufacturer.rb
@@ -0,0 +1,26 @@
require File.dirname(__FILE__) + '/../../spec_helper'

require 'facter/util/manufacturer'

describe Facter::Manufacturer do
it "should return the system DMI table" do
Facter::Manufacturer.should respond_to(:get_dmi_table)
end

it "should return nil on non-supported operating systems" do
Facter.stubs(:value).with(:kernel).returns("SomeThing")
Facter::Manufacturer.get_dmi_table().should be_nil
end

it "should strip white space on dmi output with spaces" do
sample_output_file = File.dirname(__FILE__) + "/../data/linux_dmidecode_with_spaces"
dmidecode_output = File.new(sample_output_file).read()
Facter::Manufacturer.expects(:get_dmi_table).returns(dmidecode_output)
Facter.fact(:kernel).stubs(:value).returns("Linux")

query = { '[Ss]ystem [Ii]nformation' => [ { 'Product(?: Name)?:' => 'productname' } ] }

Facter::Manufacturer.dmi_find_system_info(query)
Facter.value(:productname).should == "MS-6754"
end
end

0 comments on commit 356cf15

Please sign in to comment.