Skip to content

Commit

Permalink
Split out MiqSockUtil methods into their on Environment methods, and …
Browse files Browse the repository at this point in the history
…add corresponding specs.
  • Loading branch information
djberg96 committed Jul 9, 2020
1 parent 516f535 commit 7800f10
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/models/miq_server/environment_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def get_network_information
mac_address = eth0.mac_address
else
require 'socket'
ipaddr = Socket.ip_address_list.detect { |addr| addr.ipv4? && !addr.ipv4_private? }&.ip_address
hostname = Socket.gethostbyname(Socket.gethostname).first
ipaddr = MiqEnvironment.local_ip_address
hostname = MiqEnvironment.fully_qualified_domain_name
mac_address = UUIDTools::UUID.mac_address.dup
end
rescue
Expand Down
13 changes: 13 additions & 0 deletions lib/miq_environment.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
require 'sys-uname'
require 'socket'

module MiqEnvironment
# Return the fully qualified hostname for the local host.
#
def self.fully_qualified_domain_name
Socket.gethostbyname(Socket.gethostname).first
end

# Return the local IP v4 address of the local host, ignoring private addresses.
#
def self.local_ip_address
Socket.ip_address_list.detect { |addr| addr.ipv4? && !addr.ipv4_private? }&.ip_address
end

class Command
EVM_KNOWN_COMMANDS = %w[apachectl memcached memcached-tool nohup service systemctl].freeze

Expand Down
10 changes: 10 additions & 0 deletions spec/lib/miq_environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
silence_warnings { Sys::Platform::IMPL = @old_impl } if @old_impl
end

context "Host Info" do
example "fully_qualified_domain_name" do
expect(described_class.fully_qualified_domain_name).to eq(`hostname -f`.chomp)
end

example "local_ip_address" do
expect(described_class.local_ip_address).to eq(`hostname -i`.chomp)
end
end

context "Command" do
context ".supports_memcached?" do
it "should run once and cache the result" do
Expand Down
13 changes: 13 additions & 0 deletions spec/models/miq_server/environment_manager_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
require 'socket'

RSpec.describe "Server Environment Management" do
let(:mac_address) { 'a:1:b:2:c:3:d:4' }
let(:hostname) { Socket.gethostname }
let(:loopback) { '127.0.0.1' }

context ".get_network_information" do
it "when in non-production mode" do
allow(UUIDTools::UUID).to receive(:mac_address).and_return(mac_address)
expect(MiqServer.get_network_information).to eq([loopback, hostname, mac_address])
end
end

context ".spartan_mode" do
before { MiqServer.class_eval { instance_variable_set :@spartan_mode, nil } }
after { MiqServer.class_eval { instance_variable_set :@spartan_mode, nil } }
Expand Down

0 comments on commit 7800f10

Please sign in to comment.