From de772a01ce997ec2cec9d052bbca9b289af75eff Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 5 Jul 2010 03:38:42 +0200 Subject: [PATCH] MAC address matching middleware --- lib/vagrant/action/builtin.rb | 1 + lib/vagrant/action/vm/match_mac_address.rb | 19 +++++++++++++ .../action/vm/match_mac_address_test.rb | 28 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 lib/vagrant/action/vm/match_mac_address.rb create mode 100644 test/vagrant/action/vm/match_mac_address_test.rb diff --git a/lib/vagrant/action/builtin.rb b/lib/vagrant/action/builtin.rb index dd5a3c5b1..b1b5aab99 100644 --- a/lib/vagrant/action/builtin.rb +++ b/lib/vagrant/action/builtin.rb @@ -8,6 +8,7 @@ module Vagrant up = Builder.new do use VM::Import use VM::Persist + use VM::MatchMACAddress use VM::Customize use VM::ForwardPorts use VM::ShareFolders diff --git a/lib/vagrant/action/vm/match_mac_address.rb b/lib/vagrant/action/vm/match_mac_address.rb new file mode 100644 index 000000000..93b27005d --- /dev/null +++ b/lib/vagrant/action/vm/match_mac_address.rb @@ -0,0 +1,19 @@ +module Vagrant + class Action + module VM + class MatchMACAddress + def initialize(app, env) + @app = app + end + + def call(env) + env.logger.info "Matching MAC addresses..." + env["vm"].vm.network_adapters.first.mac_address = env.env.config.vm.base_mac + env["vm"].vm.save + + @app.call(env) + end + end + end + end +end diff --git a/test/vagrant/action/vm/match_mac_address_test.rb b/test/vagrant/action/vm/match_mac_address_test.rb new file mode 100644 index 000000000..4bc5c5886 --- /dev/null +++ b/test/vagrant/action/vm/match_mac_address_test.rb @@ -0,0 +1,28 @@ +require File.join(File.dirname(__FILE__), '..', '..', '..', 'test_helper') + +class MatchMACAddressVMActionTest < Test::Unit::TestCase + setup do + @klass = Vagrant::Action::VM::MatchMACAddress + @app, @env = mock_action_data + + @vm = mock("vm") + @env["vm"] = @vm + + @internal_vm = mock("internal") + @vm.stubs(:vm).returns(@internal_vm) + + @instance = @klass.new(@app, @env) + end + + should "match the mac addresses" do + nic = mock("nic") + nic.expects(:mac_address=).once + + update_seq = sequence("update_seq") + @internal_vm.expects(:network_adapters).returns([nic]).once.in_sequence(update_seq) + @internal_vm.expects(:save).once.in_sequence(update_seq) + @app.expects(:call).with(@env).once.in_sequence(update_seq) + + @instance.call(@env) + end +end