Skip to content

Commit

Permalink
Merge remote-tracking branch 'pr/35'
Browse files Browse the repository at this point in the history
This closes GH-29 and GH-35
  • Loading branch information
adrienthebo committed May 2, 2013
2 parents d2924b9 + ff63f54 commit 07ace0f
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
55 changes: 55 additions & 0 deletions lib/facter/network.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'facter'
require 'open-uri'
require 'timeout'

#Public IP
# Expected output: The public ipaddress of this node.
Facter.add("network_public_ip") do
setcode do
Timeout::timeout(2) do
open('http://ip-echo.appspot.com', 'User-Agent' => 'Ruby/Facter').read.match(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/).to_s
end
end
end

#Gateway
# Expected output: The ip address of the nexthop/default router
Facter.add("network_nexthop_ip") do
my_gw = nil
confine :kernel => :linux
setcode do
gw_address = Facter::Util::Resolution.exec('/sbin/ip route show 0/0')
if gw_address
my_gw = gw_address.split(/\s+/)[2].match(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/).to_s
end
my_gw
end
end

#Primary interface
# Expected output: The specific interface name that the node uses to communicate with the nexthop
Facter.add("network_primary_interface") do
confine :kernel => :linux
setcode do
gw_address = Facter::Util::Resolution.exec('/sbin/ip route show 0/0')
if gw_address
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
end
end
end

#Primary IP
# Expected output: The ipaddress confugred 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
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
end
end
end
56 changes: 56 additions & 0 deletions spec/unit/network_facts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env rspec
require 'spec_helper'
require 'open-uri'
describe 'network_facts' do
describe 'network_public_ip fact' do
before do
Facter::Util::Resolution.any_instance.stubs(:open).returns(stub(:read => '1.1.1.1'))
end
it "should be our public ip" do
Facter.fact(:network_public_ip).value.should == '1.1.1.1'
end
end
end
describe 'network_nexthop_ip' do
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')
end
it 'should exec ip and determine the next hop' do
Facter.fact(:network_nexthop_ip).value.should == '1.2.3.4'
end
end
end
describe 'network_primary_interface' do
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')
Facter::Util::Resolution.stubs(:exec).with('/sbin/ip route get 1.2.3.4').returns('1.2.3.4 dev eth0 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 == 'eth0'
end
end
end
describe 'network_primary_ip' do
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')
Facter::Util::Resolution.stubs(:exec).with('/sbin/ip route get 1.2.3.4').returns("1.2.3.4 dev eth0 src 1.2.3.99\n
cache mtu 1500 advmss 1460 hoplimit 64")
end
it 'should exec ip and determine the primary ip address' do
Facter.fact(:network_primary_ip).value.should == '1.2.3.99'
end
end
end

0 comments on commit 07ace0f

Please sign in to comment.