Skip to content
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
1 change: 1 addition & 0 deletions lib/linux_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
require 'linux_admin/volume_group'
require 'linux_admin/scap'
require 'linux_admin/time_date'
require 'linux_admin/ip_address'

module LinuxAdmin
extend Common
Expand Down
27 changes: 27 additions & 0 deletions lib/linux_admin/ip_address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module LinuxAdmin
class IpAddress
include Common

def address
address_list.detect { |ip| IPAddr.new(ip).ipv4? }
end

def address6
address_list.detect { |ip| IPAddr.new(ip).ipv6? }
end

private

def address_list
result = nil
# Added retry to account for slow DHCP not assigning an IP quickly at boot; specifically:
# https://github.com/ManageIQ/manageiq-appliance/commit/160d8ccbfbfd617bdb5445e56cdab66b9323b15b
5.times do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment or link to PR that explains why 5.times is necessary.

result = run(cmd("hostname"), :params => ["-I"])
break if result.success?
end

result.success? ? result.output.split(' ') : []
end
end
end
52 changes: 52 additions & 0 deletions spec/ip_address_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
describe LinuxAdmin::IpAddress do
let(:ip) { described_class.new }

SPAWN_ARGS = [
described_class.new.cmd("hostname"),
:params => ["-I"]
]

def result(output, exit_status)
AwesomeSpawn::CommandResult.new("", output, "", exit_status)
end

describe "#address" do
it "returns an address" do
ip_addr = "192.168.1.2"
expect(AwesomeSpawn).to receive(:run).with(*SPAWN_ARGS).and_return(result(ip_addr, 0))
expect(ip.address).to eq(ip_addr)
end

it "returns nil when no address is found" do
ip_addr = ""
expect(AwesomeSpawn).to receive(:run).at_least(5).times.with(*SPAWN_ARGS).and_return(result(ip_addr, 1))
expect(ip.address).to be_nil
end

it "returns only IPv4 addresses" do
ip_addr = "fd12:3456:789a:1::1 192.168.1.2"
expect(AwesomeSpawn).to receive(:run).with(*SPAWN_ARGS).and_return(result(ip_addr, 0))
expect(ip.address).to eq("192.168.1.2")
end
end

describe "#address6" do
it "returns an address" do
ip_addr = "fd12:3456:789a:1::1"
expect(AwesomeSpawn).to receive(:run).with(*SPAWN_ARGS).and_return(result(ip_addr, 0))
expect(ip.address6).to eq(ip_addr)
end

it "returns nil when no address is found" do
ip_addr = ""
expect(AwesomeSpawn).to receive(:run).at_least(5).times.with(*SPAWN_ARGS).and_return(result(ip_addr, 1))
expect(ip.address6).to be_nil
end

it "returns only IPv6 addresses" do
ip_addr = "192.168.1.2 fd12:3456:789a:1::1"
expect(AwesomeSpawn).to receive(:run).with(*SPAWN_ARGS).and_return(result(ip_addr, 0))
expect(ip.address6).to eq("fd12:3456:789a:1::1")
end
end
end