Allow automatic mac address assignment with virtuabox provider

This commit is contained in:
Chris Roberts 2018-09-07 09:51:04 -07:00
parent 11b0d58fa0
commit 85dc0ebec9
5 changed files with 74 additions and 5 deletions

View File

@ -10,11 +10,14 @@ module VagrantPlugins
# If we cloned, we don't need a base mac, it is already set! # If we cloned, we don't need a base mac, it is already set!
return @app.call(env) if env[:machine].config.vm.clone return @app.call(env) if env[:machine].config.vm.clone
raise Vagrant::Errors::VMBaseMacNotSpecified if !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
# 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[:ui].info I18n.t("vagrant.actions.vm.match_mac.matching") env[:machine].provider.driver.set_mac_address(env[:machine].config.vm.base_mac)
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) @app.call(env)
end end

View File

@ -635,6 +635,7 @@ module VagrantPlugins
end end
def set_mac_address(mac) def set_mac_address(mac)
mac = "auto" if !mac
execute("modifyvm", @uuid, "--macaddress1", mac, retryable: true) execute("modifyvm", @uuid, "--macaddress1", mac, retryable: true)
end end

View File

@ -2177,6 +2177,7 @@ en:
manually for more verbose error output. manually for more verbose error output.
match_mac: match_mac:
matching: Matching MAC address for NAT networking... matching: Matching MAC address for NAT networking...
generating: Generating MAC address for NAT networking...
no_base_mac: |- no_base_mac: |-
No base MAC address was specified. This is required for the NAT networking No base MAC address was specified. This is required for the NAT networking
to work properly (and hence port forwarding, SSH, etc.). Specifying this to work properly (and hence port forwarding, SSH, etc.). Specifying this

View File

@ -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

View File

@ -40,4 +40,24 @@ describe VagrantPlugins::ProviderVirtualBox::Driver::Version_5_0 do
end end
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 end