updated gethostname to avoid deprecation warning
warning: Socket.gethostbyname is deprecated; use Addrinfo.getaddrinfo instead.
MODIFY
lib/beetle.rb
lib/beetle.rb
@@ -39,12 +39,8 @@ module Beetle
# determine the fully qualified domainname of the host we're running on
def self.hostname
name = Socket.gethostname
- parts = name.split('.')
- if parts.size > 1
- name
- else
- Socket.gethostbyname(parts.first).first rescue name
- end
+ host = name.split('.').first
+ Addrinfo.getaddrinfo(host, nil, nil, :STREAM, nil, Socket::AI_CANONNAME).first.canonname rescue name
end
# use ruby's autoload mechanism for loading beetle classes

MODIFY
test/beetle/beetle_test.rb
test/beetle/beetle_test.rb
@@ -2,15 +2,18 @@ require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
module Beetle
class HostnameTest < Minitest::Test
- test "should use Socket.gethostname if returned name name is fully qualified" do
+ test "should use canonical name if possible " do
+ addr = mock("addr")
+ addr.expects(:canonname).returns("a.b.com")
Socket.expects(:gethostname).returns("a.b.com")
+ Addrinfo.expects(:getaddrinfo).with("a", nil, nil, :STREAM, nil, Socket::AI_CANONNAME).returns([addr])
assert_equal "a.b.com", Beetle.hostname
end
- test "should use Socket.gethosbyname if returned name name is not fully qualified" do
+ test "should use Socket.gethostbyname if Addrinfo raises" do
Socket.expects(:gethostname).returns("a")
- Socket.expects(:gethostbyname).with("a").returns(["a.b.com"])
- assert_equal "a.b.com", Beetle.hostname
+ Addrinfo.expects(:getaddrinfo).with("a", nil, nil, :STREAM, nil, Socket::AI_CANONNAME).raises("murks")
+ assert_equal "a", Beetle.hostname
end
end
end

updated gethostname to avoid deprecation warning

warning: Socket.gethostbyname is deprecated; use Addrinfo.getaddrinfo instead.