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,38 +1,25 @@
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|
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
end

View File

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