guests/linux: Add tests for choosing addressable IP

This commit is contained in:
Seth Vargo 2016-06-05 14:46:43 -04:00
parent d07cb9d75e
commit d4159e4208
No known key found for this signature in database
GPG Key ID: 905A90C2949E8787
2 changed files with 44 additions and 6 deletions

View File

@ -3,12 +3,12 @@ module VagrantPlugins
module Cap
module ChooseAddressableIPAddr
def self.choose_addressable_ip_addr(machine, possible)
machine.communicate.tap do |comm|
possible.each do |ip|
command = "ping -c1 -w1 -W1 #{ip}"
if comm.test(command)
return ip
end
comm = machine.communicate
possible.each do |ip|
command = "ping -c1 -w1 -W1 #{ip}"
if comm.test(command)
return ip
end
end

View File

@ -0,0 +1,38 @@
require_relative "../../../../base"
describe "VagrantPlugins::GuestLinux::Cap::ChooseAddressableIPAddr" do
let(:caps) do
VagrantPlugins::GuestLinux::Plugin
.components
.guest_capabilities[:linux]
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(:cap) { caps.get(:choose_addressable_ip_addr) }
it "returns the first matching IP address" do
possible = ["1.2.3.4", "5.6.7.8"]
possible.each do |ip|
comm.stub_command("ping -c1 -w1 -W1 #{ip}", exit_code: 0)
end
result = cap.choose_addressable_ip_addr(machine, possible)
expect(result).to eq("1.2.3.4")
end
it "returns nil when there are no matches" do
result = cap.choose_addressable_ip_addr(machine, [])
expect(result).to be(nil)
end
end
end