diff --git a/plugins/guests/darwin/cap/choose_addressable_ip_addr.rb b/plugins/guests/darwin/cap/choose_addressable_ip_addr.rb index f5b578ebd..d7327d6f7 100644 --- a/plugins/guests/darwin/cap/choose_addressable_ip_addr.rb +++ b/plugins/guests/darwin/cap/choose_addressable_ip_addr.rb @@ -3,16 +3,16 @@ 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 -t1 #{ip}" - if comm.test(command) - return ip - end + comm = machine.communicate + + possible.each do |ip| + if comm.test("ping -c1 -t1 #{ip}") + return ip end end - nil + # If we got this far, there are no addressable IPs + return nil end end end diff --git a/test/unit/plugins/guests/darwin/cap/choose_addressable_ip_addr_test.rb b/test/unit/plugins/guests/darwin/cap/choose_addressable_ip_addr_test.rb new file mode 100644 index 000000000..4468db8e9 --- /dev/null +++ b/test/unit/plugins/guests/darwin/cap/choose_addressable_ip_addr_test.rb @@ -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