guests/darwin: Add tests for get_addressable_ip_addr

This commit is contained in:
Seth Vargo 2016-05-31 01:11:15 -04:00
parent 5683a3b8ca
commit dc883aa46f
No known key found for this signature in database
GPG Key ID: 905A90C2949E8787
2 changed files with 43 additions and 7 deletions

View File

@ -3,16 +3,16 @@ module VagrantPlugins
module Cap
module ChooseAddressableIPAddr
def self.choose_addressable_ip_addr(machine, possible)
machine.communicate.tap do |comm|
comm = machine.communicate
possible.each do |ip|
command = "ping -c1 -t1 #{ip}"
if comm.test(command)
if comm.test("ping -c1 -t1 #{ip}")
return ip
end
end
end
nil
# If we got this far, there are no addressable IPs
return nil
end
end
end

View File

@ -0,0 +1,36 @@
require_relative "../../../../base"
describe "VagrantPlugins::GuestDarwin::Cap::ChangeHostName" do
let(:described_class) do
VagrantPlugins::GuestDarwin::Plugin
.components
.guest_capabilities[:darwin]
.get(:choose_addressable_ip_addr)
end
let(:machine) { double("machine") }
let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) }
before do
allow(machine).to receive(:communicate).and_return(comm)
end
after do
comm.verify_expectations!
end
describe ".choose_addressable_ip_addr" do
let(:possible) { ["1.2.3.4", "5.6.7.8"] }
it "retrieves the value" do
comm.stub_command("ping -c1 -t1 5.6.7.8", exit_code: 0)
result = described_class.choose_addressable_ip_addr(machine, possible)
expect(result).to eq("5.6.7.8")
end
it "returns nil if no ips are found" do
result = described_class.choose_addressable_ip_addr(machine, [])
expect(result).to be(nil)
end
end
end