diff --git a/plugins/providers/virtualbox/cap/public_address.rb b/plugins/providers/virtualbox/cap/public_address.rb new file mode 100644 index 000000000..09dd5408e --- /dev/null +++ b/plugins/providers/virtualbox/cap/public_address.rb @@ -0,0 +1,15 @@ +module VagrantPlugins + module ProviderVirtualBox + module Cap + module PublicAddress + def self.public_address(machine) + return nil if machine.state.id != :running + + ssh_info = machine.ssh_info + return nil if !ssh_info + ssh_info[:host] + end + end + end + end +end diff --git a/plugins/providers/virtualbox/plugin.rb b/plugins/providers/virtualbox/plugin.rb index 84f86ba51..bf355bb6c 100644 --- a/plugins/providers/virtualbox/plugin.rb +++ b/plugins/providers/virtualbox/plugin.rb @@ -34,6 +34,11 @@ module VagrantPlugins Cap end + provider_capability(:virtualbox, :public_address) do + require_relative "cap/public_address" + Cap::PublicAddress + end + provider_capability(:virtualbox, :snapshot_list) do require_relative "cap" Cap diff --git a/test/unit/plugins/providers/virtualbox/cap/public_address_test.rb b/test/unit/plugins/providers/virtualbox/cap/public_address_test.rb new file mode 100644 index 000000000..d9a581ed2 --- /dev/null +++ b/test/unit/plugins/providers/virtualbox/cap/public_address_test.rb @@ -0,0 +1,43 @@ +require_relative "../base" + +require Vagrant.source_root.join("plugins/providers/virtualbox/cap/public_address") + +describe VagrantPlugins::ProviderVirtualBox::Cap::PublicAddress do + include_context "unit" + + let(:iso_env) do + # We have to create a Vagrantfile so there is a root path + env = isolated_environment + env.vagrantfile("") + env.create_vagrant_env + end + + let(:machine) do + iso_env.machine(iso_env.machine_names[0], :dummy).tap do |m| + allow(m).to receive(:state).and_return(state) + end + end + + let(:state) do + double(:state) + end + + describe "#public_address" do + it "returns nil when the machine is not running" do + allow(state).to receive(:id).and_return(:not_created) + expect(described_class.public_address(machine)).to be(nil) + end + + it "returns nil when there is no ssh info" do + allow(state).to receive(:id).and_return(:not_created) + allow(machine).to receive(:ssh_info).and_return(nil) + expect(described_class.public_address(machine)).to be(nil) + end + + it "returns the host" do + allow(state).to receive(:id).and_return(:running) + allow(machine).to receive(:ssh_info).and_return(host: "test") + expect(described_class.public_address(machine)).to eq("test") + end + end +end