Skip to content

Instantly share code, notes, and snippets.

@eliasp
Last active December 19, 2015 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliasp/5897475 to your computer and use it in GitHub Desktop.
Save eliasp/5897475 to your computer and use it in GitHub Desktop.
Facter.add(:macaddress) do
confine :kernel => 'Linux'
has_weight 10 # about an order of magnitude faster
setcode do
begin
result = nil
Dir.glob('/sys/class/net/*').each do |path|
mac = File.read(path + "/address").chomp
unless /^(?:00:)+00$|^\s*/.match(mac)
result = mac
end
break unless result == nil
end
result
rescue Exception
nil
end
end
end
describe "when run on Linux" do
before :each do
Facter.fact(:kernel).stubs(:value).returns("Linux")
Facter.fact(:operatingsystem).stubs(:value).returns("Linux")
Facter::Util::IP.stubs(:get_ifconfig).returns("/sbin/ifconfig")
end
describe "with /sys available" do
before :each do
Dir.stubs(:glob).with('/sys/class/net/*').returns( [
'/sys/class/net/lo',
'/sys/class/net/eth0',
'/sys/class/net/eth1',
'/sys/class/net/sit0',
'/sys/class/net/ip6tnl0',
'/sys/class/net/tunvpn0',
'/sys/class/net/tunvpn1',
])
File.stubs(:read).with('/sys/class/net/lo/address').returns("00:00:00:00:00:00\n")
File.stubs(:read).with('/sys/class/net/eth0/address').returns("00:12:3f:be:22:01\n")
File.stubs(:read).with('/sys/class/net/eth1/address').returns("00:12:3f:be:22:02\n")
File.stubs(:read).with('/sys/class/net/sit0/address').returns("00:00:00:00")
File.stubs(:read).with('/sys/class/net/ip6tnl0/address').returns("00:00:00:00:00:00:00:00:00:00:00:00")
File.stubs(:read).with('/sys/class/net/tunvpn0/address').returns("")
File.stubs(:read).with('/sys/class/net/tunvpn1/address').returns(" \n")
end
it "should glob /sys/class/net" do
Dir.expects(:glob).with('/sys/class/net/*').returns([ '/sys/class/net/lo', '/sys/class/ne/eth0' ])
Facter.value(:macaddress)
end
it "should open the address file of the first interface" do
File.expects(:read).with('/sys/class/net/lo/address').returns( "00:00:00:00:00:00\n" )
Facter.value(:macaddress)
end
it "should open the address file of the second interface" do
File.expects(:read).with('/sys/class/net/eth0/address').returns( "00:12:3f:be:22:01\n" )
Facter.value(:macaddress)
end
it "should not open the address file of the third interface" do
File.expects(:read).with('/sys/class/net/eth1/address').returns( "00:12:3f:be:22:02\n" ).never
Facter.value(:macaddress)
end
it "should not return an empty address" do
File.expects(:read).with('/sys/class/net/tunvpn0/address').returns( "" )
Facter.value(:macaddress).should == nil
end
it "should not return a whitespace-only address" do
File.expects(:read).with('/sys/class/net/tunvpn1/address').returns( " \n")
Facter.value(:macaddress).should == nil
end
it "should not return a shortened zero-only address" do
File.expects(:read).with('/sys/class/net/sit0/address').returns( "00:00:00:00\n")
Facter.value(:macaddress).should == nil
end
it "should not return a double-length zero-only address" do
File.expects(:read).with('/sys/class/net/ip6tnl0/address').returns( "00:00:00:00:00:00:00:00:00:00:00:00\n" )
Facter.value(:macaddress).should == nil
end
it "should return the macaddress of the first interface with a proper macaddress" do
Facter.value(:macaddress).should == "00:12:3f:be:22:01"
end
# the local loopback interface (lo) doesn't have a macaddress/has its macaddress zero-filled
it "should return nil for zero-only macaddresses" do
Dir.stubs(:glob).returns( [ '/sys/class/net/lo' ])
File.stubs(:read).returns( "00:00:00:00:00:00\n" )
Facter.value(:macaddress).should == nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment