Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #43 #52

Merged
merged 2 commits into from
Aug 18, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/facter/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whitespace damage

my_gw
end
end
Expand All @@ -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
Expand All @@ -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
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