Clear forwarded ports is now done in context of customize block

This commit is contained in:
Mitchell Hashimoto 2011-07-08 00:20:15 -07:00
parent 268f7f7bc2
commit 60708873e4
2 changed files with 21 additions and 59 deletions

View File

@ -1,37 +1,24 @@
require File.expand_path("../forward_ports_helpers", __FILE__)
module Vagrant module Vagrant
class Action class Action
module VM module VM
class ClearForwardedPorts class ClearForwardedPorts
include ForwardPortsHelpers
def initialize(app, env) def initialize(app, env)
@app = app @app = app
@env = env @env = env
end end
def call(env) def call(env)
@env = env env["config"].vm.customize do |vm|
clear env.ui.info I18n.t("vagrant.actions.vm.clear_forward_ports.deleting")
@app.call(env)
end
def clear vm.network_adapters.each do |na|
if used_ports.length > 0 na.nat_driver.forwarded_ports.dup.each do |fp|
@env.ui.info I18n.t("vagrant.actions.vm.clear_forward_ports.deleting") fp.destroy
clear_ports end
@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
end end
end end
@app.call(env)
end end
end end
end end

View File

@ -13,48 +13,12 @@ class ClearForwardedPortsVMActionTest < Test::Unit::TestCase
end end
context "calling" do 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 def mock_fp
fp = mock("fp") fp = mock("fp")
fp.expects(:destroy).once fp.expects(:destroy).once
fp fp
end 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 def mock_adapter
na = mock("adapter") na = mock("adapter")
engine = mock("engine") engine = mock("engine")
@ -63,10 +27,21 @@ class ClearForwardedPortsVMActionTest < Test::Unit::TestCase
na na
end 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
@adapters << mock_adapter @adapters << mock_adapter
@instance.clear_ports @app.expects(:call).with(@env)
@instance.call(@env)
end end
end end
end end