[Pkg-puppet-devel] [facter] 151/180: (FACT-593) Remove dependency on route binary from dhcp_servers fact

Stig Sandbeck Mathisen ssm at debian.org
Mon Jun 30 15:06:42 UTC 2014


This is an automated email from the git hooks/post-receive script.

ssm pushed a commit to branch master
in repository facter.

commit 0a08f846fb0e90b1e391b69a4fddce88fdd5548c
Author: Will <whopper at puppetlabs.com>
Date:   Thu Jun 19 15:11:57 2014 -0700

    (FACT-593) Remove dependency on route binary from dhcp_servers fact
    
    Previously, the dhcp_servers fact assumed that the route binary
    was present, which it then tried to use to determine the primary
    interface. This commit updates that fact to instead use /proc/net/route
    to retrieve the same information.
---
 lib/facter/util/dhcp_servers.rb                 | 14 +++++++++++++-
 spec/fixtures/unit/dhcp_servers/route           |  7 +++----
 spec/fixtures/unit/dhcp_servers/route_nogw      |  5 +----
 spec/fixtures/unit/util/dhcp_servers/route      |  3 +++
 spec/fixtures/unit/util/dhcp_servers/route_nogw |  1 +
 spec/unit/dhcp_servers_spec.rb                  |  6 +++---
 spec/unit/util/dhcp_servers_spec.rb             | 13 +++++++++++++
 7 files changed, 37 insertions(+), 12 deletions(-)

diff --git a/lib/facter/util/dhcp_servers.rb b/lib/facter/util/dhcp_servers.rb
index 785064d..6091808 100644
--- a/lib/facter/util/dhcp_servers.rb
+++ b/lib/facter/util/dhcp_servers.rb
@@ -1,6 +1,18 @@
+require 'facter/util/file_read'
+
 module Facter::Util::DHCPServers
   def self.gateway_device
-    Facter::Core::Execution.exec("route -n").scan(/^0\.0\.0\.0.*?(\S+)$/).flatten.first
+    interface = nil
+    if routes = Facter::Util::FileRead.read('/proc/net/route')
+      routes.each_line do |line|
+        device, destination = line.split(' ')
+        if destination == '00000000'
+          interface = device
+          break
+        end
+      end
+    end
+    interface
   end
 
   def self.devices
diff --git a/spec/fixtures/unit/dhcp_servers/route b/spec/fixtures/unit/dhcp_servers/route
index 46ee6ae..e732bcd 100644
--- a/spec/fixtures/unit/dhcp_servers/route
+++ b/spec/fixtures/unit/dhcp_servers/route
@@ -1,4 +1,3 @@
-Kernel IP routing table
-Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
-0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 eth0
-192.168.2.0     0.0.0.0         255.255.255.0   U     9      0        0 wlan0
+Iface	Destination	Gateway 	Flags	RefCnt	Use	Metric	Mask		MTU	Window	IRTT
+eth0	00000000	0110100A	0003	0	0	0	00000000	0	0	0
+eth0	0010100A	00000000	0001	0	0	1	00FCFFFF	0	0	0
diff --git a/spec/fixtures/unit/dhcp_servers/route_nogw b/spec/fixtures/unit/dhcp_servers/route_nogw
index ae42372..e9b73f5 100644
--- a/spec/fixtures/unit/dhcp_servers/route_nogw
+++ b/spec/fixtures/unit/dhcp_servers/route_nogw
@@ -1,4 +1 @@
-Kernel IP routing table
-Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
-192.168.1.0     0.0.0.0         255.255.255.0   UG    0      0        0 eth0
-192.168.2.0     0.0.0.0         255.255.255.0   U     9      0        0 wlan0
+Iface	Destination	Gateway 	Flags	RefCnt	Use	Metric	Mask		MTU	Window	IRTT
diff --git a/spec/fixtures/unit/util/dhcp_servers/route b/spec/fixtures/unit/util/dhcp_servers/route
new file mode 100644
index 0000000..e732bcd
--- /dev/null
+++ b/spec/fixtures/unit/util/dhcp_servers/route
@@ -0,0 +1,3 @@
+Iface	Destination	Gateway 	Flags	RefCnt	Use	Metric	Mask		MTU	Window	IRTT
+eth0	00000000	0110100A	0003	0	0	0	00000000	0	0	0
+eth0	0010100A	00000000	0001	0	0	1	00FCFFFF	0	0	0
diff --git a/spec/fixtures/unit/util/dhcp_servers/route_nogw b/spec/fixtures/unit/util/dhcp_servers/route_nogw
new file mode 100644
index 0000000..e9b73f5
--- /dev/null
+++ b/spec/fixtures/unit/util/dhcp_servers/route_nogw
@@ -0,0 +1 @@
+Iface	Destination	Gateway 	Flags	RefCnt	Use	Metric	Mask		MTU	Window	IRTT
diff --git a/spec/unit/dhcp_servers_spec.rb b/spec/unit/dhcp_servers_spec.rb
index 3e99685..fc5334f 100644
--- a/spec/unit/dhcp_servers_spec.rb
+++ b/spec/unit/dhcp_servers_spec.rb
@@ -4,7 +4,7 @@ describe "DHCP server facts" do
   describe "on Linux OS's" do
     before :each do
       Facter.fact(:kernel).stubs(:value).returns 'Linux'
-      Facter::Core::Execution.stubs(:exec).with("route -n").returns(my_fixture_read("route"))
+      Facter::Util::FileRead.stubs(:read).with('/proc/net/route').returns(my_fixture_read('route'))
     end
 
     describe "with nmcli version <= 0.9.8 available" do
@@ -39,7 +39,7 @@ describe "DHCP server facts" do
 
       describe "with no default gateway" do
         before :each do
-          Facter::Core::Execution.stubs(:exec).with("route -n").returns(my_fixture_read("route_nogw"))
+          Facter::Util::FileRead.stubs(:read).with('/proc/net/route').returns(my_fixture_read('route_nogw'))
           Facter::Core::Execution.stubs(:exec).with("nmcli d").returns(my_fixture_read("nmcli_devices"))
           Facter::Core::Execution.stubs(:exec).with("nmcli -f all d list iface eth0").returns(my_fixture_read("nmcli_eth0_dhcp"))
           Facter::Core::Execution.stubs(:exec).with("nmcli -f all d list iface wlan0").returns(my_fixture_read("nmcli_wlan0_dhcp"))
@@ -105,7 +105,7 @@ describe "DHCP server facts" do
 
       describe "with no default gateway" do
         before :each do
-          Facter::Core::Execution.stubs(:exec).with("route -n").returns(my_fixture_read("route_nogw"))
+          Facter::Util::FileRead.stubs(:read).with('/proc/net/route').returns(my_fixture_read('route_nogw'))
           Facter::Core::Execution.stubs(:exec).with("nmcli d").returns(my_fixture_read("nmcli_devices"))
           Facter::Core::Execution.stubs(:exec).with("nmcli -f all d show eth0").returns(my_fixture_read("nmcli_eth0_dhcp"))
           Facter::Core::Execution.stubs(:exec).with("nmcli -f all d show wlan0").returns(my_fixture_read("nmcli_wlan0_dhcp"))
diff --git a/spec/unit/util/dhcp_servers_spec.rb b/spec/unit/util/dhcp_servers_spec.rb
index ec7a646..b922ccc 100644
--- a/spec/unit/util/dhcp_servers_spec.rb
+++ b/spec/unit/util/dhcp_servers_spec.rb
@@ -4,6 +4,19 @@ require 'spec_helper'
 require 'facter/util/dhcp_servers'
 
 describe Facter::Util::DHCPServers do
+
+  describe "retrieving the gateway device" do
+    it "returns nil when there are no default routes" do
+      Facter::Util::FileRead.stubs(:read).with('/proc/net/route').returns(my_fixture_read('route_nogw'))
+      described_class.gateway_device.should be_nil
+    end
+
+    it "returns the interface associated with the first default route" do
+      Facter::Util::FileRead.stubs(:read).with('/proc/net/route').returns(my_fixture_read('route'))
+      described_class.gateway_device.should eq "eth0"
+    end
+  end
+
   describe "nmcli_version" do
     {
       'nmcli tool, version 0.9.8.0' => [0, 9, 8, 0],

-- 
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