[Pkg-puppet-devel] [facter] 12/18: Revert "(FACT-380) Use the interface of the default route for network facts"
Stig Sandbeck Mathisen
ssm at debian.org
Wed Jan 27 21:12:53 UTC 2016
This is an automated email from the git hooks/post-receive script.
ssm pushed a commit to branch master
in repository facter.
commit 3043635848153bf9540b8e430b5f694b9ccd1893
Author: Branan Riley <branan at puppetlabs.com>
Date: Mon Jan 25 14:24:55 2016 -0800
Revert "(FACT-380) Use the interface of the default route for network facts"
This update was more breaking than we anticipated (see comments on
FACT-380, but also FACT-1316 which was filed almost immediately)
For users that really need the new behavior, Facter 3 provides the new
behavior.
This reverts commit ec73549be0c26aab40a8968330726b369ab080f7.
---
lib/facter/ipaddress.rb | 19 ++++++++++++-----
lib/facter/ipaddress6.rb | 5 ++---
lib/facter/macaddress.rb | 11 +++++++---
lib/facter/netmask.rb | 10 +--------
lib/facter/util/ip.rb | 17 ---------------
lib/facter/util/netmask.rb | 6 ++++++
.../net_route_all_with_multiple_interfaces | 2 --
.../fixtures/ifconfig/net_route_net_tools_1.60.txt | 3 ---
.../ipaddress/net_route_multiple_127_addresses.txt | 3 ---
.../unit/ipaddress/net_route_net_tools_1.60.txt | 3 ---
.../ipaddress/net_route_non_english_locale.txt | 3 ---
.../unit/ipaddress/net_route_ubuntu_1204.txt | 3 ---
...fconfig_darwin_10_8_5.txt => darwin_10_8_5.txt} | 0
.../netmask/net_route_multiple_127_addresses.txt | 3 ---
.../unit/netmask/net_route_net_tools_1.60.txt | 3 ---
.../unit/netmask/net_route_non_english_locale.txt | 3 ---
.../unit/netmask/net_route_ubuntu_1204.txt | 3 ---
spec/unit/ipaddress6_spec.rb | 20 ++++--------------
spec/unit/ipaddress_spec.rb | 24 +++++++++++-----------
spec/unit/macaddress_spec.rb | 7 +------
spec/unit/netmask_spec.rb | 21 ++++---------------
21 files changed, 52 insertions(+), 117 deletions(-)
diff --git a/lib/facter/ipaddress.rb b/lib/facter/ipaddress.rb
index 96b10db..6179a4d 100644
--- a/lib/facter/ipaddress.rb
+++ b/lib/facter/ipaddress.rb
@@ -3,9 +3,7 @@
# Purpose: Return the main IP address for a host.
#
# Resolution:
-# On Linux and AIX, it examines the routing table and uses the IP address
-# of the default interface.
-# On other Unixes does an ifconfig, and returns the first non 127.0.0.0/8
+# On the Unixes does an ifconfig, and returns the first non 127.0.0.0/8
# subnetted IP it finds.
# On Windows, it attempts to use the socket library and resolve the machine's
# hostname via DNS.
@@ -29,8 +27,19 @@ require 'facter/util/ip'
Facter.add(:ipaddress) do
confine :kernel => :linux
setcode do
- iface = Facter::Util::IP.linux_default_iface
- Facter.value("ipaddress_#{iface}")
+ ip = nil
+ output = Facter::Util::IP.exec_ifconfig(["2>/dev/null"])
+ if output
+ regexp = /inet (?:addr:)?([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
+ output.split("\n").each do |line|
+ match = regexp.match(line)
+ if match and not /^127\./.match(match[1])
+ ip = match[1]
+ break
+ end
+ end
+ end
+ ip
end
end
diff --git a/lib/facter/ipaddress6.rb b/lib/facter/ipaddress6.rb
index 68a2771..128f3cd 100644
--- a/lib/facter/ipaddress6.rb
+++ b/lib/facter/ipaddress6.rb
@@ -37,12 +37,11 @@ def get_address_after_token(output, token, return_first=false)
ip
end
-
Facter.add(:ipaddress6) do
confine :kernel => :linux
setcode do
- iface = Facter::Util::IP.linux_default_iface
- Facter.value("ipaddress6_#{iface}")
+ output = Facter::Util::IP.exec_ifconfig(["2>/dev/null"])
+ get_address_after_token(output, 'inet6(?: addr:)?')
end
end
diff --git a/lib/facter/macaddress.rb b/lib/facter/macaddress.rb
index a950435..c2cbe65 100644
--- a/lib/facter/macaddress.rb
+++ b/lib/facter/macaddress.rb
@@ -12,10 +12,15 @@ require 'facter/util/macaddress'
require 'facter/util/ip'
Facter.add(:macaddress) do
- confine :kernel => :linux
+ confine :kernel => 'Linux'
setcode do
- iface = Facter::Util::IP.linux_default_iface
- Facter.value("macaddress_#{iface}")
+ ether = []
+ output = Facter::Util::IP.exec_ifconfig(["-a","2>/dev/null"])
+
+ String(output).each_line do |s|
+ ether.push($1) if s =~ /(?:ether|HWaddr) ((\w{1,2}:){5,}\w{1,2})/
+ end
+ Facter::Util::Macaddress.standardize(ether[0])
end
end
diff --git a/lib/facter/netmask.rb b/lib/facter/netmask.rb
index 185d23e..7156714 100644
--- a/lib/facter/netmask.rb
+++ b/lib/facter/netmask.rb
@@ -16,16 +16,8 @@
#
require 'facter/util/netmask'
-Facter.add(:netmask) do
- confine :kernel => :linux
- setcode do
- iface = Facter::Util::IP.linux_default_iface
- Facter.value("netmask_#{iface}")
- end
-end
-
Facter.add("netmask") do
- confine :kernel => [ :sunos, :freebsd, :openbsd, :netbsd, :darwin, :"gnu/kfreebsd", :dragonfly, :AIX ]
+ confine :kernel => [ :sunos, :linux, :freebsd, :openbsd, :netbsd, :darwin, :"gnu/kfreebsd", :dragonfly, :AIX ]
setcode do
Facter::NetMask.get_netmask
end
diff --git a/lib/facter/util/ip.rb b/lib/facter/util/ip.rb
index 887c8d2..62df8a4 100644
--- a/lib/facter/util/ip.rb
+++ b/lib/facter/util/ip.rb
@@ -337,21 +337,4 @@ module Facter::Util::IP
network = ip.mask(subnet.to_s).to_s
end
end
-
- def self.read_proc_net_route
- File.read("/proc/net/route")
- end
-
- def self.linux_default_iface
- routes = read_proc_net_route
- iface = nil
- routes.split("\n").each do |line|
- parts = line.split
- if parts[1] == "00000000"
- iface = alphafy(parts[0])
- break
- end
- end
- iface
- end
end
diff --git a/lib/facter/util/netmask.rb b/lib/facter/util/netmask.rb
index e06ca52..4c4cf01 100644
--- a/lib/facter/util/netmask.rb
+++ b/lib/facter/util/netmask.rb
@@ -5,6 +5,12 @@ module Facter::NetMask
ops = nil
case Facter.value(:kernel)
+ when 'Linux'
+ ops = {
+ :ifconfig_opts => ['2>/dev/null'],
+ :regex => %r{#{Facter.value(:ipaddress)}.*?(?:Mask:|netmask)\s*(#{ipregex})}x,
+ :munge => nil,
+ }
when 'SunOS'
ops = {
:ifconfig_opts => ['-a'],
diff --git a/spec/fixtures/ifconfig/net_route_all_with_multiple_interfaces b/spec/fixtures/ifconfig/net_route_all_with_multiple_interfaces
deleted file mode 100644
index bd427d2..0000000
--- a/spec/fixtures/ifconfig/net_route_all_with_multiple_interfaces
+++ /dev/null
@@ -1,2 +0,0 @@
-eth0 00000000 01D1FC83 0003 0 0 100 00000000 0 0 0
-eth0 01D1FC83 00000000 0001 0 0 100 00FFFFFF 0 0 0
diff --git a/spec/fixtures/ifconfig/net_route_net_tools_1.60.txt b/spec/fixtures/ifconfig/net_route_net_tools_1.60.txt
deleted file mode 100644
index 15ae788..0000000
--- a/spec/fixtures/ifconfig/net_route_net_tools_1.60.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
-em1 00000000 99D1FC83 0003 0 0 100 00000000 0 0 0
-em1 99D1FC83 99D1FC83 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/ipaddress/net_route_multiple_127_addresses.txt b/spec/fixtures/unit/ipaddress/net_route_multiple_127_addresses.txt
deleted file mode 100644
index ed3edb3..0000000
--- a/spec/fixtures/unit/ipaddress/net_route_multiple_127_addresses.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
-venet0:1 00000000 1400DE0A 0003 0 0 100 00000000 0 0 0
-venet0:1 1400DE0A 1400DE0A 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/ipaddress/net_route_net_tools_1.60.txt b/spec/fixtures/unit/ipaddress/net_route_net_tools_1.60.txt
deleted file mode 100644
index 15ae788..0000000
--- a/spec/fixtures/unit/ipaddress/net_route_net_tools_1.60.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
-em1 00000000 99D1FC83 0003 0 0 100 00000000 0 0 0
-em1 99D1FC83 99D1FC83 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/ipaddress/net_route_non_english_locale.txt b/spec/fixtures/unit/ipaddress/net_route_non_english_locale.txt
deleted file mode 100644
index 0a6508a..0000000
--- a/spec/fixtures/unit/ipaddress/net_route_non_english_locale.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
-wlan0 00000000 5301A8C0 0003 0 0 100 00000000 0 0 0
-wlan0 5301A8C0 5301A8C0 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/ipaddress/net_route_ubuntu_1204.txt b/spec/fixtures/unit/ipaddress/net_route_ubuntu_1204.txt
deleted file mode 100644
index 007e3d4..0000000
--- a/spec/fixtures/unit/ipaddress/net_route_ubuntu_1204.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
-eth0 00000000 6E50570A 0003 0 0 100 00000000 0 0 0
-eth0 6E50570A 6E50570A 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/netmask/ifconfig_darwin_10_8_5.txt b/spec/fixtures/unit/netmask/darwin_10_8_5.txt
similarity index 100%
rename from spec/fixtures/unit/netmask/ifconfig_darwin_10_8_5.txt
rename to spec/fixtures/unit/netmask/darwin_10_8_5.txt
diff --git a/spec/fixtures/unit/netmask/net_route_multiple_127_addresses.txt b/spec/fixtures/unit/netmask/net_route_multiple_127_addresses.txt
deleted file mode 100644
index ed3edb3..0000000
--- a/spec/fixtures/unit/netmask/net_route_multiple_127_addresses.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
-venet0:1 00000000 1400DE0A 0003 0 0 100 00000000 0 0 0
-venet0:1 1400DE0A 1400DE0A 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/netmask/net_route_net_tools_1.60.txt b/spec/fixtures/unit/netmask/net_route_net_tools_1.60.txt
deleted file mode 100644
index 15ae788..0000000
--- a/spec/fixtures/unit/netmask/net_route_net_tools_1.60.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
-em1 00000000 99D1FC83 0003 0 0 100 00000000 0 0 0
-em1 99D1FC83 99D1FC83 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/netmask/net_route_non_english_locale.txt b/spec/fixtures/unit/netmask/net_route_non_english_locale.txt
deleted file mode 100644
index 0a6508a..0000000
--- a/spec/fixtures/unit/netmask/net_route_non_english_locale.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
-wlan0 00000000 5301A8C0 0003 0 0 100 00000000 0 0 0
-wlan0 5301A8C0 5301A8C0 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/netmask/net_route_ubuntu_1204.txt b/spec/fixtures/unit/netmask/net_route_ubuntu_1204.txt
deleted file mode 100644
index 007e3d4..0000000
--- a/spec/fixtures/unit/netmask/net_route_ubuntu_1204.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
-eth0 00000000 6E50570A 0003 0 0 100 00000000 0 0 0
-eth0 6E50570A 6E50570A 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/unit/ipaddress6_spec.rb b/spec/unit/ipaddress6_spec.rb
index b95d4f8..96650fe 100755
--- a/spec/unit/ipaddress6_spec.rb
+++ b/spec/unit/ipaddress6_spec.rb
@@ -26,11 +26,8 @@ describe "The IPv6 address fact" do
it "should return ipaddress6 information for Linux" do
Facter::Core::Execution.stubs(:exec).with('uname -s').returns('Linux')
Facter::Util::IP.stubs(:get_ifconfig).returns("/sbin/ifconfig")
- Facter::Util::IP.stubs(:exec_ifconfig).
+ Facter::Util::IP.stubs(:exec_ifconfig).with(["2>/dev/null"]).
returns(ifconfig_fixture('linux_ifconfig_all_with_multiple_interfaces'))
- routes = ifconfig_fixture('net_route_all_with_multiple_interfaces')
- Facter::Util::IP.stubs(:read_proc_net_route).returns(routes)
- Facter.collection.internal_loader.load(:interfaces)
Facter.value(:ipaddress6).should == "2610:10:20:209:212:3fff:febe:2201"
end
@@ -38,11 +35,8 @@ describe "The IPv6 address fact" do
it "should return ipaddress6 information for Linux with recent net-tools" do
Facter::Core::Execution.stubs(:exec).with('uname -s').returns('Linux')
Facter::Util::IP.stubs(:get_ifconfig).returns("/sbin/ifconfig")
- Facter::Util::IP.stubs(:exec_ifconfig).
+ Facter::Util::IP.stubs(:exec_ifconfig).with(["2>/dev/null"]).
returns(ifconfig_fixture('ifconfig_net_tools_1.60.txt'))
- routes = ifconfig_fixture('net_route_net_tools_1.60.txt')
- Facter::Util::IP.stubs(:read_proc_net_route).returns(routes)
- Facter.collection.internal_loader.load(:interfaces)
Facter.value(:ipaddress6).should == "2610:10:20:209:212:3fff:febe:2201"
end
@@ -50,11 +44,8 @@ describe "The IPv6 address fact" do
it "should return ipaddress6 with fe80 in any other octet than the first for Linux" do
Facter::Core::Execution.stubs(:exec).with('uname -s').returns('Linux')
Facter::Util::IP.stubs(:get_ifconfig).returns("/sbin/ifconfig")
- Facter::Util::IP.stubs(:exec_ifconfig).
+ Facter::Util::IP.stubs(:exec_ifconfig).with(["2>/dev/null"]).
returns(ifconfig_fixture('linux_ifconfig_all_with_multiple_interfaces_and_fe80'))
- routes = ifconfig_fixture('net_route_all_with_multiple_interfaces')
- Facter::Util::IP.stubs(:read_proc_net_route).returns(routes)
- Facter.collection.internal_loader.load(:interfaces)
Facter.value(:ipaddress6).should == "2610:10:20:209:212:3fff:fe80:2201"
end
@@ -62,11 +53,8 @@ describe "The IPv6 address fact" do
it "should not return ipaddress6 link-local address for Linux" do
Facter::Core::Execution.stubs(:exec).with('uname -s').returns('Linux')
Facter::Util::IP.stubs(:get_ifconfig).returns("/sbin/ifconfig")
- Facter::Util::IP.stubs(:exec_ifconfig).
+ Facter::Util::IP.stubs(:exec_ifconfig).with(["2>/dev/null"]).
returns(ifconfig_fixture('linux_ifconfig_all_with_multiple_interfaces_and_no_public_ipv6'))
- routes = ifconfig_fixture('net_route_all_with_multiple_interfaces')
- Facter::Util::IP.stubs(:read_proc_net_route).returns(routes)
- Facter.collection.internal_loader.load(:interfaces)
Facter.value(:ipaddress6).should be_false
end
diff --git a/spec/unit/ipaddress_spec.rb b/spec/unit/ipaddress_spec.rb
index a1387f6..78b0c96 100755
--- a/spec/unit/ipaddress_spec.rb
+++ b/spec/unit/ipaddress_spec.rb
@@ -4,6 +4,10 @@ require 'spec_helper'
require 'facter/util/ip'
describe "ipaddress fact" do
+ before do
+ Facter.collection.internal_loader.load(:ipaddress)
+ end
+
context 'using `ifconfig`' do
before :each do
Facter.fact(:hostname).stubs(:value)
@@ -14,29 +18,25 @@ describe "ipaddress fact" do
Facter.fact(:kernel).stubs(:value).returns("Linux")
end
- after :each do
- Facter.collection.flush
- end
-
def expect_ifconfig_parse(address, fixture)
- Facter::Util::IP.stubs(:exec_ifconfig).returns(my_fixture_read("ifconfig_#{fixture}"))
- routes = my_fixture_read("net_route_#{fixture}")
- Facter::Util::IP.stubs(:read_proc_net_route).returns(routes)
- Facter.collection.internal_loader.load(:interfaces)
- Facter.collection.internal_loader.load(:ipaddress)
+ Facter::Util::IP.stubs(:exec_ifconfig).returns(my_fixture_read(fixture))
Facter.fact(:ipaddress).value.should == address
end
it "parses correctly on Ubuntu 12.04" do
- expect_ifconfig_parse "10.87.80.110", "ubuntu_1204.txt"
+ expect_ifconfig_parse "10.87.80.110", "ifconfig_ubuntu_1204.txt"
end
it "parses correctly on Fedora 17" do
- expect_ifconfig_parse "131.252.209.153", "net_tools_1.60.txt"
+ expect_ifconfig_parse "131.252.209.153", "ifconfig_net_tools_1.60.txt"
+ end
+
+ it "parses a real address over multiple loopback addresses" do
+ expect_ifconfig_parse "10.0.222.20", "ifconfig_multiple_127_addresses.txt"
end
it "parses nothing with a non-english locale" do
- expect_ifconfig_parse nil, "non_english_locale.txt"
+ expect_ifconfig_parse nil, "ifconfig_non_english_locale.txt"
end
end
end
diff --git a/spec/unit/macaddress_spec.rb b/spec/unit/macaddress_spec.rb
index b3ac6a2..ffc4e06 100755
--- a/spec/unit/macaddress_spec.rb
+++ b/spec/unit/macaddress_spec.rb
@@ -23,11 +23,8 @@ describe "macaddress fact" do
end
it "should return the macaddress of the first interface" do
- Facter::Util::IP.stubs(:exec_ifconfig).
+ Facter::Util::IP.stubs(:exec_ifconfig).with(["-a","2>/dev/null"]).
returns(ifconfig_fixture('linux_ifconfig_all_with_multiple_interfaces'))
- routes = ifconfig_fixture("net_route_all_with_multiple_interfaces")
- Facter::Util::IP.stubs(:read_proc_net_route).returns(routes)
- Facter.collection.internal_loader.load(:interfaces)
Facter.value(:macaddress).should == "00:12:3f:be:22:01"
end
@@ -35,7 +32,6 @@ describe "macaddress fact" do
it "should return nil when no macaddress can be found" do
Facter::Util::IP.stubs(:exec_ifconfig).with(["-a","2>/dev/null"]).
returns(ifconfig_fixture('linux_ifconfig_no_mac'))
- Facter::Util::IP.stubs(:read_proc_net_route).returns("")
proc { Facter.value(:macaddress) }.should_not raise_error
Facter.value(:macaddress).should be_nil
@@ -45,7 +41,6 @@ describe "macaddress fact" do
it "should return nil when no interface has a real macaddress" do
Facter::Util::IP.stubs(:exec_ifconfig).with(["-a","2>/dev/null"]).
returns(ifconfig_fixture('linux_ifconfig_venet'))
- Facter::Util::IP.stubs(:read_proc_net_route).returns("")
proc { Facter.value(:macaddress) }.should_not raise_error
Facter.value(:macaddress).should be_nil
diff --git a/spec/unit/netmask_spec.rb b/spec/unit/netmask_spec.rb
index ead885c..226fa11 100755
--- a/spec/unit/netmask_spec.rb
+++ b/spec/unit/netmask_spec.rb
@@ -6,21 +6,8 @@ require 'facter/util/ip'
shared_examples_for "netmask from ifconfig output" do |platform, address, fixture|
it "correctly on #{platform}" do
- Facter::Util::IP.stubs(:exec_ifconfig).returns(my_fixture_read("ifconfig_#{fixture}"))
+ Facter::Util::IP.stubs(:exec_ifconfig).returns(my_fixture_read(fixture))
Facter.collection.internal_loader.load(:netmask)
- begin
- routes = my_fixture_read("net_route_#{fixture}")
- Facter::Util::IP.stubs(:read_proc_net_route).returns(routes)
- rescue RuntimeError
- # We want to try to load proc/net/route fixtures, but skip if
- # they don't exist for non-linux platforms. Ideally we'd get an
- # IOError here, but the fixture machinery here is dumb and
- # converts this to a RuntimeError. Hopefully anything that would
- # error here would also cause the actual test to fail, so I'm
- # not going to worry too hard.
- end
- Facter.collection.internal_loader.load(:interfaces)
- Facter.collection.internal_loader.load(:ipaddress)
Facter.fact(:netmask).value.should eq(address)
end
@@ -34,10 +21,10 @@ describe "The netmask fact" do
example_behavior_for "netmask from ifconfig output",
"Archlinux (net-tools 1.60)", "255.255.255.0",
- "net_tools_1.60.txt"
+ "ifconfig_net_tools_1.60.txt"
example_behavior_for "netmask from ifconfig output",
"Ubuntu 12.04", "255.255.255.255",
- "ubuntu_1204.txt"
+ "ifconfig_ubuntu_1204.txt"
end
context "on AIX" do
@@ -47,7 +34,7 @@ describe "The netmask fact" do
example_behavior_for "netmask from ifconfig output",
"AIX 7", "255.255.255.0",
- "aix_7.txt"
+ "ifconfig_aix_7.txt"
end
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git
More information about the Pkg-puppet-devel
mailing list