From dfa3183af166b01b685d386062a45e1f62a1666a Mon Sep 17 00:00:00 2001 From: Wolf Noble Date: Mon, 22 Jul 2013 17:48:24 -0500 Subject: [PATCH 1/2] wip fix for issue #43 --- lib/facter/network.rb | 23 ++++++++-- spec/unit/network_facts_spec.rb | 75 ++++++++++++++++++++++++++++++--- 2 files changed, 88 insertions(+), 10 deletions(-) diff --git a/lib/facter/network.rb b/lib/facter/network.rb index 6b226529..3aaa4ff1 100644 --- a/lib/facter/network.rb +++ b/lib/facter/network.rb @@ -19,9 +19,11 @@ confine :kernel => :linux setcode do gw_address = Facter::Util::Resolution.exec('/sbin/ip route show 0/0') - if gw_address + #not all network configurations will have a nexthop. + #the ip tool expresses the presence of a nexthop with the word 'via' + if gw_address.include? ' via ' my_gw = gw_address.split(/\s+/)[2].match(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/).to_s - end + end my_gw end end @@ -32,10 +34,18 @@ confine :kernel => :linux setcode do gw_address = Facter::Util::Resolution.exec('/sbin/ip route show 0/0') - if gw_address + #not all network configurations will have a nexthop. + #the ip tool expresses the presence of a nexthop with the word 'via' + if gw_address.include? ' via ' my_gw = gw_address.split(/\s+/)[2].match(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/).to_s fun = Facter::Util::Resolution.exec("/sbin/ip route get #{my_gw}").split("\n")[0] fun.split(/\s+/)[2].to_s + #some network configurations simply have a link that all interactions are abstracted through + elsif gw_address.include? 'scope link' + #since we have no default route ip to determine where to send 'traffic not otherwise explicitly routed' + #lets just use 8.8.8.8 as far as a route goes. + fun = Facter::Util::Resolution.exec("/sbin/ip route get 8.8.8.8").split("\n")[0] + fun.split(/\s+/)[2].to_s end end end @@ -46,10 +56,15 @@ confine :kernel => :linux setcode do gw_address = Facter::Util::Resolution.exec('/sbin/ip route show 0/0') - if gw_address + if gw_address.include? ' via ' my_gw = gw_address.split(/\s+/)[2].match(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/).to_s fun = Facter::Util::Resolution.exec("/sbin/ip route get #{my_gw}").split("\n")[0] fun.split(/\s+/)[4].to_s + elsif gw_address.include? 'scope link' + #since we have no default route ip to determine where to send 'traffic not otherwise explicitly routed' + #lets just use 8.8.8.8 as far as a route goes and grab our IP from there. + fun = Facter::Util::Resolution.exec("/sbin/ip route get 8.8.8.8").split("\n")[0] + fun.split(/\s+/)[4].to_s end end end \ No newline at end of file diff --git a/spec/unit/network_facts_spec.rb b/spec/unit/network_facts_spec.rb index c4de8afd..ad03f2c4 100644 --- a/spec/unit/network_facts_spec.rb +++ b/spec/unit/network_facts_spec.rb @@ -12,9 +12,9 @@ end end describe 'network_nexthop_ip' do - before do - Facter.fact(:kernel).stubs(:value).returns('linux') - end + before do + Facter.fact(:kernel).stubs(:value).returns('linux') + end context 'on a Linux host' do before do Facter::Util::Resolution.stubs(:exec).with('/sbin/ip route show 0/0').returns('default via 1.2.3.4 dev eth0') @@ -23,11 +23,38 @@ Facter.fact(:network_nexthop_ip).value.should == '1.2.3.4' end end - end - describe 'network_primary_interface' do - before do + context 'on an OpenVZ VM' do + before :each do + Facter.clear Facter.fact(:kernel).stubs(:value).returns('linux') + Facter.fact(:virtual).stubs(:value).returns('openvz') + Facter::Util::Resolution.stubs(:exec) + end + context 'which has the default route via a veth device' do + before do + Facter.fact(:macaddress).stubs(:value).returns(nil) + Facter::Util::Resolution.stubs(:exec).with('/sbin/ip route show 0/0').returns('default via 1.2.3.4 dev eth0') + end + it 'should exec ip and determine the next hop' do + Facter.fact(:network_nexthop_ip).value.should == '1.2.3.4' + end + end + context 'with only venet interfaces' do + before do + Facter::Util::Resolution.stubs(:exec).with('/sbin/ip route show 0/0').returns('default dev venet0 scope link') + end + it 'should not exist' do + Facter.fact(:network_nexthop_ip).value.should == nil + end end + end + + end + describe 'network_primary_interface' do + before do + Facter.fact(:kernel).stubs(:value).returns('linux') + Facter::Util::Resolution.stubs(:exec).with('/sbin/ip route show 0/0').returns('default via 1.2.3.4 dev eth0') + end context 'on a Linux host' do before do Facter::Util::Resolution.stubs(:exec).with('/sbin/ip route show 0/0').returns('default via 1.2.3.4 dev eth0') @@ -38,6 +65,24 @@ Facter.fact(:network_primary_interface).value.should == 'eth0' end end + context 'on an OpenVZ VM' do + before :each do + Facter.clear + Facter.fact(:kernel).stubs(:value).returns('linux') + Facter.fact(:virtual).stubs(:value).returns('openvz') + Facter::Util::Resolution.stubs(:exec) + end + context 'with only venet devices' do + before do + Facter::Util::Resolution.stubs(:exec).with('/sbin/ip route show 0/0').returns('default dev venet0 scope link') + Facter::Util::Resolution.stubs(:exec).with('/sbin/ip route get 8.8.8.8').returns('8.8.8.8 dev venet0 src 1.2.3.99\n + cache mtu 1500 advmss 1460 hoplimit 64') + end + it 'should exec ip and determine the primary interface' do + Facter.fact(:network_primary_interface).value.should == 'venet0' + end + end + end end describe 'network_primary_ip' do before do @@ -53,4 +98,22 @@ Facter.fact(:network_primary_ip).value.should == '1.2.3.99' end end + context 'on an OpenVZ VM' do + before :each do + Facter.clear + Facter.fact(:kernel).stubs(:value).returns('linux') + Facter.fact(:virtual).stubs(:value).returns('openvz') + Facter::Util::Resolution.stubs(:exec) + end + context 'with only venet devices' do + before do + Facter::Util::Resolution.stubs(:exec).with('/sbin/ip route show 0/0').returns('default dev venet0 scope link') + Facter::Util::Resolution.stubs(:exec).with('/sbin/ip route get 8.8.8.8').returns("8.8.8.8 dev venet0 src 1.2.3.99\n + cache mtu 1500 advmss 1460 hoplimit 64") + end + it 'should exec ip and determine the primary interface' do + Facter.fact(:network_primary_ip).value.should == '1.2.3.99' + end + end + end end \ No newline at end of file From ea2f8c5dff192bbc814300aecc06832963f7d31f Mon Sep 17 00:00:00 2001 From: Wolf Noble Date: Sat, 10 Aug 2013 12:27:54 -0500 Subject: [PATCH 2/2] fix whitespace damage, and a small typo --- lib/facter/network.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/facter/network.rb b/lib/facter/network.rb index 3aaa4ff1..787d4b77 100644 --- a/lib/facter/network.rb +++ b/lib/facter/network.rb @@ -23,7 +23,7 @@ #the ip tool expresses the presence of a nexthop with the word 'via' if gw_address.include? ' via ' my_gw = gw_address.split(/\s+/)[2].match(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/).to_s - end + end my_gw end end @@ -51,7 +51,7 @@ end #Primary IP -# Expected output: The ipaddress confugred on the interface that communicates with the nexthop +# Expected output: The ipaddress configred on the interface that communicates with the nexthop Facter.add("network_primary_ip") do confine :kernel => :linux setcode do @@ -67,4 +67,4 @@ fun.split(/\s+/)[4].to_s end end -end \ No newline at end of file +end