From e8e07d26f5602b3e5d20c50651c430933c8e0f79 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 16 Feb 2010 15:47:52 -0800 Subject: [PATCH] Forwarding ports now clears old ports first --- lib/vagrant/actions/forward_ports.rb | 10 +++++ test/vagrant/actions/forward_ports_test.rb | 51 ++++++++++++++++------ 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/lib/vagrant/actions/forward_ports.rb b/lib/vagrant/actions/forward_ports.rb index 767dac06d..39fb0486b 100644 --- a/lib/vagrant/actions/forward_ports.rb +++ b/lib/vagrant/actions/forward_ports.rb @@ -2,6 +2,16 @@ module Vagrant module Actions class ForwardPorts < Base def execute! + clear + forward_ports + end + + def clear + logger.info "Deleting any previously set forwarded ports..." + @vm.vm.forwarded_ports.collect { |p| p.destroy(true) } + end + + def forward_ports logger.info "Forwarding ports..." Vagrant.config.vm.forwarded_ports.each do |name, options| diff --git a/test/vagrant/actions/forward_ports_test.rb b/test/vagrant/actions/forward_ports_test.rb index 8239fa6ce..2ed09fc3a 100644 --- a/test/vagrant/actions/forward_ports_test.rb +++ b/test/vagrant/actions/forward_ports_test.rb @@ -6,20 +6,45 @@ class ForwardPortsActionTest < Test::Unit::TestCase mock_config end - should "create a port forwarding for the VM" do - forwarded_ports = mock("forwarded_ports") - - Vagrant.config.vm.forwarded_ports.each do |name, opts| - forwarded_ports.expects(:<<).with do |port| - assert_equal name, port.name - assert_equal opts[:hostport], port.hostport - assert_equal opts[:guestport], port.guestport - true - end + context "execution" do + should "clear all previous ports and forward new ports" do + exec_seq = sequence("exec_seq") + @action.expects(:clear).once.in_sequence(exec_seq) + @action.expects(:forward_ports).once.in_sequence(exec_seq) + @action.execute! end + end - @vm.expects(:forwarded_ports).returns(forwarded_ports) - @vm.expects(:save).with(true).once - @action.execute! + context "forwarding ports" do + should "create a port forwarding for the VM" do + forwarded_ports = mock("forwarded_ports") + + Vagrant.config.vm.forwarded_ports.each do |name, opts| + forwarded_ports.expects(:<<).with do |port| + assert_equal name, port.name + assert_equal opts[:hostport], port.hostport + assert_equal opts[:guestport], port.guestport + true + end + end + + @vm.expects(:forwarded_ports).returns(forwarded_ports) + @vm.expects(:save).with(true).once + @action.forward_ports + end + end + + context "clearing forwarded ports" do + should "call destroy on all forwarded ports" do + forwarded_ports = [] + 5.times do |i| + port = mock("port#{i}") + port.expects(:destroy).with(true).once + forwarded_ports << port + end + + @vm.expects(:forwarded_ports).returns(forwarded_ports) + @action.clear + end end end