diff options
author | James Turnbull <james@lovedthanlost.net> | 2010-06-11 09:24:24 +1000 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2010-06-15 01:29:05 +1000 |
commit | 8106bc3f000d0e18969c4ec142ef84fbc780b30a (patch) | |
tree | e00415d7e9cc7cda337af80090e850b28e67b375 | |
parent | 83b3ea6abbd1d382a6738fa731f9f7409867e135 (diff) | |
download | facter-8106bc3f000d0e18969c4ec142ef84fbc780b30a.tar.gz facter-8106bc3f000d0e18969c4ec142ef84fbc780b30a.tar.xz facter-8106bc3f000d0e18969c4ec142ef84fbc780b30a.zip |
Adding HP-UX support to Facter's IP facts
Includes Rspec tests
-rw-r--r-- | lib/facter/util/ip.rb | 18 | ||||
-rw-r--r-- | spec/unit/data/hpux_ifconfig_single_interface | 3 | ||||
-rw-r--r-- | spec/unit/data/hpux_netstat_all_interfaces | 6 | ||||
-rwxr-xr-x[-rw-r--r--] | spec/unit/util/ip.rb | 61 |
4 files changed, 83 insertions, 5 deletions
diff --git a/lib/facter/util/ip.rb b/lib/facter/util/ip.rb index 25acf3a..366303c 100644 --- a/lib/facter/util/ip.rb +++ b/lib/facter/util/ip.rb @@ -16,9 +16,14 @@ module Facter::Util::IP :netmask => /netmask\s+0x(\w{8})/ }, :sunos => { - :ipaddress => /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/, + :ipaddress => /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/, :macaddress => /(?:ether|lladdr)\s+(\w?\w:\w?\w:\w?\w:\w?\w:\w?\w:\w?\w)/, :netmask => /netmask\s+(\w{8})/ + }, + :"hp-ux" => { + :ipaddress => /\s+inet (\S+)\s.*/, + :macaddress => /(\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/, + :netmask => /.*\s+netmask (\S+)\s.*/ } } @@ -28,7 +33,7 @@ module Facter::Util::IP end def self.convert_from_hex?(kernel) - kernels_to_convert = [:sunos, :openbsd, :netbsd, :freebsd, :darwin] + kernels_to_convert = [:sunos, :openbsd, :netbsd, :freebsd, :darwin, :"hp-ux"] kernels_to_convert.include?(kernel) end @@ -60,6 +65,8 @@ module Facter::Util::IP output = %x{/sbin/ifconfig -a} when 'SunOS' output = %x{/usr/sbin/ifconfig -a} + when 'HP-UX' + output = %x{/bin/netstat -i} end output end @@ -71,6 +78,12 @@ module Facter::Util::IP output = %x{/sbin/ifconfig #{interface}} when 'SunOS' output = %x{/usr/sbin/ifconfig #{interface}} + when 'HP-UX' + mac = "" + ifc = %x{/usr/sbin/ifconfig #{interface}} + %x{/usr/sbin/lanscan}.scan(/(\dx\S+).*UP\s+(\w+\d+)/).each {|i| mac = i[0] if i.include?(interface) } + mac = mac.sub(/0x(\S+)/,'\1').scan(/../).join(":") + output = ifc + "\n" + mac end output end @@ -101,7 +114,6 @@ module Facter::Util::IP device end - def self.get_interface_value(interface, label) tmp1 = [] diff --git a/spec/unit/data/hpux_ifconfig_single_interface b/spec/unit/data/hpux_ifconfig_single_interface new file mode 100644 index 0000000..ff54eb8 --- /dev/null +++ b/spec/unit/data/hpux_ifconfig_single_interface @@ -0,0 +1,3 @@ +lan0: flags=1843<UP,BROADCAST,RUNNING,MULTICAST,CKO> + inet 168.24.80.71 netmask ffffff00 broadcast 168.24.80.255 +00:13:21:BD:9C:B7 diff --git a/spec/unit/data/hpux_netstat_all_interfaces b/spec/unit/data/hpux_netstat_all_interfaces new file mode 100644 index 0000000..7745fa8 --- /dev/null +++ b/spec/unit/data/hpux_netstat_all_interfaces @@ -0,0 +1,6 @@ +Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Coll +lan1 1500 15.12.0.0 host1.default.com + 572527659 0 1129421249 0 0 +lan0 1500 172.54.85.0 host2.default.com + 519222647 0 329127145 0 0 +lo0 4136 loopback localhost 14281117 0 14281125 0 0 diff --git a/spec/unit/util/ip.rb b/spec/unit/util/ip.rb index 222ce9a..a9aae76 100644..100755 --- a/spec/unit/util/ip.rb +++ b/spec/unit/util/ip.rb @@ -5,7 +5,7 @@ require File.dirname(__FILE__) + '/../../spec_helper' require 'facter/util/ip' describe Facter::Util::IP do - [:freebsd, :linux, :netbsd, :openbsd, :sunos, :darwin].each do |platform| + [:freebsd, :linux, :netbsd, :openbsd, :sunos, :darwin, :"hp-ux"].each do |platform| it "should be supported on #{platform}" do Facter::Util::IP.supported_platforms.should be_include(platform) end @@ -41,6 +41,13 @@ describe Facter::Util::IP do Facter::Util::IP.get_interfaces().should == ["lo0", "e1000g0"] end + it "should return a list three interfaces on HP-UX with three interfaces multiply reporting" do + sample_output_file = File.dirname(__FILE__) + '/../data/hpux_netstat_all_interfaces' + hpux_netstat = File.new(sample_output_file).read() + Facter::Util::IP.stubs(:get_all_interface_output).returns(hpux_netstat) + Facter::Util::IP.get_interfaces().should == ["lan1", "lan0", "lo0"] + end + it "should return a value for a specific interface" do Facter::Util::IP.should respond_to(:get_interface_value) end @@ -80,6 +87,46 @@ describe Facter::Util::IP do Facter::Util::IP.get_network_value("e1000g0").should == "172.16.15.0" end + it "should return ipaddress information for HP-UX" do + sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" + hpux_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("HP-UX") + + Facter::Util::IP.get_interface_value("lan0", "ipaddress").should == "168.24.80.71" + end + + it "should return macaddress information for HP-UX" do + sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" + hpux_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("HP-UX") + + Facter::Util::IP.get_interface_value("lan0", "macaddress").should == "00:13:21:BD:9C:B7" + end + + it "should return netmask information for HP-UX" do + sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" + hpux_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("HP-UX") + + Facter::Util::IP.get_interface_value("lan0", "netmask").should == "255.255.255.0" + end + + it "should return calculated network information for HP-UX" do + sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" + hpux_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.stubs(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("HP-UX") + + Facter::Util::IP.get_network_value("lan0").should == "168.24.80.0" + end + it "should return interface information for FreeBSD supported via an alias" do sample_output_file = File.dirname(__FILE__) + "/../data/6.0-STABLE_FreeBSD_ifconfig" ifconfig_interface = File.new(sample_output_file).read() @@ -120,6 +167,16 @@ describe Facter::Util::IP do Facter::Util::IP.get_interface_value("e1000g0", "netmask").should == "255.255.255.0" end + it "should return a human readable netmask on HP-UX" do + sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" + hpux_ifconfig_interface = File.new(sample_output_file).read() + + Facter::Util::IP.expects(:get_single_interface_output).with("lan0").returns(hpux_ifconfig_interface) + Facter.stubs(:value).with(:kernel).returns("HP-UX") + + Facter::Util::IP.get_interface_value("lan0", "netmask").should == "255.255.255.0" + end + it "should return a human readable netmask on Darwin" do sample_output_file = File.dirname(__FILE__) + "/../data/darwin_ifconfig_single_interface" @@ -137,7 +194,7 @@ describe Facter::Util::IP do Facter::Util::IP.get_bonding_master("eth0:1").should be_nil end - [:freebsd, :netbsd, :openbsd, :sunos, :darwin].each do |platform| + [:freebsd, :netbsd, :openbsd, :sunos, :darwin, :"hp-ux"].each do |platform| it "should require conversion from hex on #{platform}" do Facter::Util::IP.convert_from_hex?(platform).should == true end |