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

Facts for issue 29 #35

Merged
merged 3 commits into from
May 2, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
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
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