From 1aba2fab540dd1c09719aef14f7c73c10ef3e784 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Tue, 27 Feb 2024 14:01:22 -0500 Subject: [PATCH 1/2] Fix Socket.gethostbyname deprecation warning Fixes: ``` lib/miq_environment.rb:12: warning: Socket.gethostbyname is deprecated; use Addrinfo.getaddrinfo instead. ``` --- lib/miq_environment.rb | 5 ++++- spec/models/miq_server/environment_manager_spec.rb | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/miq_environment.rb b/lib/miq_environment.rb index 32cb8e16ea3..08b97f1b16e 100644 --- a/lib/miq_environment.rb +++ b/lib/miq_environment.rb @@ -9,7 +9,10 @@ def self.arch # Return the fully qualified hostname for the local host. # def self.fully_qualified_domain_name - Socket.gethostbyname(Socket.gethostname).first + hostname = Socket.gethostname + addrinfo = Addrinfo.getaddrinfo(hostname, nil).first + fqdn, _port = addrinfo.getnameinfo + fqdn end # Return the local IP v4 address of the local host diff --git a/spec/models/miq_server/environment_manager_spec.rb b/spec/models/miq_server/environment_manager_spec.rb index 231d0bce1d6..ada8245fc76 100644 --- a/spec/models/miq_server/environment_manager_spec.rb +++ b/spec/models/miq_server/environment_manager_spec.rb @@ -3,14 +3,14 @@ RSpec.describe "Server Environment Management" do context ".get_network_information" do let(:mac_address) { 'a:1:b:2:c:3:d:4' } - let(:hostname) { "localhost.localdomain" } + let(:hostname) { "localhost" } let(:ip_address) { "10.1.2.3" } before do require "uuidtools" allow(UUIDTools::UUID).to receive(:mac_address).and_return(mac_address) allow(Socket).to receive(:gethostname).and_return("localhost") - allow(Socket).to receive(:gethostbyname).with("localhost").and_return([hostname]) + allow(Addrinfo).to receive(:getaddrinfo).with("localhost", nil).and_return([Addrinfo.tcp("localhost", nil), Addrinfo.udp("localhost", nil)]) allow(Socket).to receive(:ip_address_list).and_return([Addrinfo.ip(ip_address)]) end From ee3babfbba4b959870ec444201c4f676df3a0fda Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Tue, 27 Feb 2024 17:19:28 -0500 Subject: [PATCH 2/2] Filter to TCP sockets only --- lib/miq_environment.rb | 2 +- spec/models/miq_server/environment_manager_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/miq_environment.rb b/lib/miq_environment.rb index 08b97f1b16e..d83b14270cd 100644 --- a/lib/miq_environment.rb +++ b/lib/miq_environment.rb @@ -10,7 +10,7 @@ def self.arch # def self.fully_qualified_domain_name hostname = Socket.gethostname - addrinfo = Addrinfo.getaddrinfo(hostname, nil).first + addrinfo = Addrinfo.getaddrinfo(hostname, nil, nil, :STREAM).first fqdn, _port = addrinfo.getnameinfo fqdn end diff --git a/spec/models/miq_server/environment_manager_spec.rb b/spec/models/miq_server/environment_manager_spec.rb index ada8245fc76..bc23e353419 100644 --- a/spec/models/miq_server/environment_manager_spec.rb +++ b/spec/models/miq_server/environment_manager_spec.rb @@ -10,7 +10,7 @@ require "uuidtools" allow(UUIDTools::UUID).to receive(:mac_address).and_return(mac_address) allow(Socket).to receive(:gethostname).and_return("localhost") - allow(Addrinfo).to receive(:getaddrinfo).with("localhost", nil).and_return([Addrinfo.tcp("localhost", nil), Addrinfo.udp("localhost", nil)]) + allow(Addrinfo).to receive(:getaddrinfo).with("localhost", nil, nil, :STREAM).and_return([Addrinfo.tcp("localhost", nil)]) allow(Socket).to receive(:ip_address_list).and_return([Addrinfo.ip(ip_address)]) end