From 60708873e48c670f4013f857d7e05dd8e1e0a171 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 8 Jul 2011 00:20:15 -0700 Subject: [PATCH] Clear forwarded ports is now done in context of customize block --- .../action/vm/clear_forwarded_ports.rb | 29 +++-------- .../action/vm/clear_forwarded_ports_test.rb | 51 +++++-------------- 2 files changed, 21 insertions(+), 59 deletions(-) diff --git a/lib/vagrant/action/vm/clear_forwarded_ports.rb b/lib/vagrant/action/vm/clear_forwarded_ports.rb index 81ba48cd6..e3fe8fd88 100644 --- a/lib/vagrant/action/vm/clear_forwarded_ports.rb +++ b/lib/vagrant/action/vm/clear_forwarded_ports.rb @@ -1,37 +1,24 @@ -require File.expand_path("../forward_ports_helpers", __FILE__) - module Vagrant class Action module VM class ClearForwardedPorts - include ForwardPortsHelpers - def initialize(app, env) @app = app @env = env end def call(env) - @env = env - clear - @app.call(env) - end + env["config"].vm.customize do |vm| + env.ui.info I18n.t("vagrant.actions.vm.clear_forward_ports.deleting") - def clear - if used_ports.length > 0 - @env.ui.info I18n.t("vagrant.actions.vm.clear_forward_ports.deleting") - clear_ports - @env["vm"].reload! - end - end - - # Deletes existing forwarded ports. - def clear_ports - @env["vm"].vm.network_adapters.each do |na| - na.nat_driver.forwarded_ports.dup.each do |fp| - fp.destroy + vm.network_adapters.each do |na| + na.nat_driver.forwarded_ports.dup.each do |fp| + fp.destroy + end end end + + @app.call(env) end end end diff --git a/test/vagrant/action/vm/clear_forwarded_ports_test.rb b/test/vagrant/action/vm/clear_forwarded_ports_test.rb index 46e0fc27c..8b8f17c92 100644 --- a/test/vagrant/action/vm/clear_forwarded_ports_test.rb +++ b/test/vagrant/action/vm/clear_forwarded_ports_test.rb @@ -13,48 +13,12 @@ class ClearForwardedPortsVMActionTest < Test::Unit::TestCase end context "calling" do - should "call the proper methods and continue chain" do - seq = sequence('seq') - @instance.expects(:clear).in_sequence(seq) - @app.expects(:call).with(@env).in_sequence(seq) - @instance.call(@env) - end - end - - context "clearing forwarded ports" do - setup do - @instance.stubs(:used_ports).returns([:a]) - @instance.stubs(:clear_ports) - end - - should "call destroy on all forwarded ports" do - @instance.expects(:clear_ports).once - @vm.expects(:reload!) - @instance.clear - end - - should "do nothing if there are no forwarded ports" do - @instance.stubs(:used_ports).returns([]) - @vm.expects(:reload!).never - @instance.clear - end - end - - context "clearing ports" do def mock_fp fp = mock("fp") fp.expects(:destroy).once fp end - setup do - VirtualBox.stubs(:version).returns("3.2.8") - @adapters = [] - @internal_vm = mock("internal_vm") - @internal_vm.stubs(:network_adapters).returns(@adapters) - @vm.stubs(:vm).returns(@internal_vm) - end - def mock_adapter na = mock("adapter") engine = mock("engine") @@ -63,10 +27,21 @@ class ClearForwardedPortsVMActionTest < Test::Unit::TestCase na end - should "destroy each forwarded port" do + setup do + VirtualBox.stubs(:version).returns("3.2.8") + @adapters = [] + @internal_vm = mock("internal_vm") + @internal_vm.stubs(:network_adapters).returns(@adapters) + @vm.stubs(:vm).returns(@internal_vm) + end + + should "call the proper methods and continue chain" do + @env["config"].vm.expects(:customize).yields(@internal_vm) + @adapters << mock_adapter @adapters << mock_adapter - @instance.clear_ports + @app.expects(:call).with(@env) + @instance.call(@env) end end end