Skip to content

Commit

Permalink
Merge pull request #52 from wolfspyre/master
Browse files Browse the repository at this point in the history
Fix for issue #43
  • Loading branch information
adrienthebo committed Aug 18, 2013
2 parents 06930c3 + ea2f8c5 commit 1b2deba
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 11 deletions.
25 changes: 20 additions & 5 deletions lib/facter/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
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
my_gw
Expand All @@ -32,24 +34,37 @@
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

#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
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
end
75 changes: 69 additions & 6 deletions spec/unit/network_facts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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
Expand All @@ -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

0 comments on commit 1b2deba

Please sign in to comment.