From 0fa8a94f320d1afbd8581d59d065253a2dd9acaf Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 13 Feb 2010 12:17:59 -0800 Subject: [PATCH] Forwarding ports action --- lib/vagrant/actions/forward_ports.rb | 20 +++++++++++++++++ test/vagrant/actions/forward_ports_test.rb | 25 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 lib/vagrant/actions/forward_ports.rb create mode 100644 test/vagrant/actions/forward_ports_test.rb diff --git a/lib/vagrant/actions/forward_ports.rb b/lib/vagrant/actions/forward_ports.rb new file mode 100644 index 000000000..29b59f45b --- /dev/null +++ b/lib/vagrant/actions/forward_ports.rb @@ -0,0 +1,20 @@ +module Vagrant + module Actions + class ForwardPorts < Base + def execute! + logger.info "Forwarding ports..." + + Vagrant.config.vm.forwarded_ports.each do |name, options| + logger.info "Forwarding \"#{name}\": #{options[:guestport]} => #{options[:hostport]}" + port = VirtualBox::ForwardedPort.new + port.name = name + port.hostport = options[:hostport] + port.guestport = options[:guestport] + @vm.forwarded_ports << port + end + + @vm.save(true) + end + end + end +end diff --git a/test/vagrant/actions/forward_ports_test.rb b/test/vagrant/actions/forward_ports_test.rb new file mode 100644 index 000000000..7cbc55d1c --- /dev/null +++ b/test/vagrant/actions/forward_ports_test.rb @@ -0,0 +1,25 @@ +require File.join(File.dirname(__FILE__), '..', '..', 'test_helper') + +class ForwardPortsActionTest < Test::Unit::TestCase + setup do + @mock_vm, @vm, @action = mock_action(Vagrant::Actions::ForwardPorts) + 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 + end + + @mock_vm.expects(:forwarded_ports).returns(forwarded_ports) + @mock_vm.expects(:save).with(true).once + @action.execute! + end +end