From 85dc0ebec9ebc8bf048163c006e1480249443076 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 7 Sep 2018 09:51:04 -0700 Subject: [PATCH] Allow automatic mac address assignment with virtuabox provider --- .../virtualbox/action/match_mac_address.rb | 13 +++--- .../virtualbox/driver/version_5_0.rb | 1 + templates/locales/en.yml | 1 + .../action/match_mac_address_test.rb | 44 +++++++++++++++++++ .../virtualbox/driver/version_5_0_test.rb | 20 +++++++++ 5 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 test/unit/plugins/providers/virtualbox/action/match_mac_address_test.rb diff --git a/plugins/providers/virtualbox/action/match_mac_address.rb b/plugins/providers/virtualbox/action/match_mac_address.rb index 10e998b2e..5b1f95ab1 100644 --- a/plugins/providers/virtualbox/action/match_mac_address.rb +++ b/plugins/providers/virtualbox/action/match_mac_address.rb @@ -10,11 +10,14 @@ module VagrantPlugins # If we cloned, we don't need a base mac, it is already set! return @app.call(env) if env[:machine].config.vm.clone - raise Vagrant::Errors::VMBaseMacNotSpecified if !env[:machine].config.vm.base_mac - - # Create the proc which we want to use to modify the virtual machine - env[:ui].info I18n.t("vagrant.actions.vm.match_mac.matching") - env[:machine].provider.driver.set_mac_address(env[:machine].config.vm.base_mac) + if env[:machine].config.vm.base_mac + # Create the proc which we want to use to modify the virtual machine + env[:ui].info I18n.t("vagrant.actions.vm.match_mac.matching") + env[:machine].provider.driver.set_mac_address(env[:machine].config.vm.base_mac) + else + env[:ui].info I18n.t("vagrant.actions.vm.match_mac.generating") + env[:machine].provider.driver.set_mac_address(nil) + end @app.call(env) end diff --git a/plugins/providers/virtualbox/driver/version_5_0.rb b/plugins/providers/virtualbox/driver/version_5_0.rb index 80c0d607e..1d6e5ec4f 100644 --- a/plugins/providers/virtualbox/driver/version_5_0.rb +++ b/plugins/providers/virtualbox/driver/version_5_0.rb @@ -635,6 +635,7 @@ module VagrantPlugins end def set_mac_address(mac) + mac = "auto" if !mac execute("modifyvm", @uuid, "--macaddress1", mac, retryable: true) end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 8cd72513e..774f92887 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -2177,6 +2177,7 @@ en: manually for more verbose error output. match_mac: matching: Matching MAC address for NAT networking... + generating: Generating MAC address for NAT networking... no_base_mac: |- No base MAC address was specified. This is required for the NAT networking to work properly (and hence port forwarding, SSH, etc.). Specifying this diff --git a/test/unit/plugins/providers/virtualbox/action/match_mac_address_test.rb b/test/unit/plugins/providers/virtualbox/action/match_mac_address_test.rb new file mode 100644 index 000000000..f6cfb980e --- /dev/null +++ b/test/unit/plugins/providers/virtualbox/action/match_mac_address_test.rb @@ -0,0 +1,44 @@ +require_relative "../base" + +describe VagrantPlugins::ProviderVirtualBox::Action::MatchMACAddress do + let(:ui) { double("ui") } + let(:machine) { double("machine", config: config, provider: double("provider", driver: driver)) } + let(:driver) { double("driver") } + let(:env) { + {machine: machine, ui: ui} + } + let(:app) { double("app") } + let(:config) { double("config", vm: vm) } + let(:vm) { double("vm", clone: clone, base_mac: base_mac) } + let(:clone) { false } + let(:base_mac) { "00:00:00:00:00:00" } + + let(:subject) { described_class.new(app, env) } + + before do + allow(ui).to receive(:info) + allow(app).to receive(:call) + end + + after { subject.call(env) } + + it "should set the mac address" do + expect(driver).to receive(:set_mac_address).with(base_mac) + end + + context "when clone is true" do + let(:clone) { true } + + it "should not set mac address" do + expect(driver).not_to receive(:set_mac_address) + end + end + + context "when base_mac is falsey" do + let(:base_mac) { nil } + + it "should set mac address" do + expect(driver).to receive(:set_mac_address).with(base_mac) + end + end +end diff --git a/test/unit/plugins/providers/virtualbox/driver/version_5_0_test.rb b/test/unit/plugins/providers/virtualbox/driver/version_5_0_test.rb index 49e6c0bc7..57e841864 100644 --- a/test/unit/plugins/providers/virtualbox/driver/version_5_0_test.rb +++ b/test/unit/plugins/providers/virtualbox/driver/version_5_0_test.rb @@ -40,4 +40,24 @@ describe VagrantPlugins::ProviderVirtualBox::Driver::Version_5_0 do end end + + describe "#set_mac_address" do + let(:mac) { "00:00:00:00:00:00" } + + after { subject.set_mac_address(mac) } + + it "should modify vm and set mac address" do + expect(subprocess).to receive(:execute).with("VBoxManage", "modifyvm", anything, "--macaddress1", mac, anything). + and_return(subprocess_result(exit_code: 0)) + end + + context "when mac address is falsey" do + let(:mac) { nil } + + it "should modify vm and set mac address to automatic value" do + expect(subprocess).to receive(:execute).with("VBoxManage", "modifyvm", anything, "--macaddress1", "auto", anything). + and_return(subprocess_result(exit_code: 0)) + end + end + end end