-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'pr/35'
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |