From f264932430245998153360c3378303af3b9d011d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 10 Jan 2012 20:47:26 -0800 Subject: [PATCH] Move network tests out into a module so other tests can use it --- test/acceptance/port_forward_test.rb | 27 ++++------------------ test/acceptance/support/network_tests.rb | 29 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 test/acceptance/support/network_tests.rb diff --git a/test/acceptance/port_forward_test.rb b/test/acceptance/port_forward_test.rb index 67952cdd8..99678c540 100644 --- a/test/acceptance/port_forward_test.rb +++ b/test/acceptance/port_forward_test.rb @@ -3,13 +3,12 @@ require File.expand_path("../base", __FILE__) require "net/http" require "uri" -require "vagrant/util/retryable" - +require "acceptance/support/network_tests" require "acceptance/support/shared/command_examples" require "support/tempdir" describe "vagrant port forwarding" do - include Vagrant::Util::Retryable + include Acceptance::NetworkTests include_context "acceptance" @@ -20,24 +19,6 @@ describe "vagrant port forwarding" do env.execute("vagrant", "box", "add", "base", box_path("default")).should succeed end - def assert_port_forwarded_properly(guest_port, host_port) - # Start up a web server in another thread by SSHing into the VM. - thr = Thread.new do - assert_execute("vagrant", "ssh", "-c", "python -m SimpleHTTPServer #{guest_port}") - end - - # Verify that port forwarding works by making a simple HTTP request - # to the port. We should get a 200 response. We retry this a few times - # as we wait for the HTTP server to come online. - retryable(:tries => 5, :sleep => 2) do - result = Net::HTTP.get_response(URI.parse("http://localhost:#{host_port}/")) - result.code.should == "200" - end - ensure - # The server needs to die. This is how. - thr.kill if thr - end - it "forwards ports properly" do initialize_environment @@ -54,7 +35,7 @@ VFILE end assert_execute("vagrant", "up") - assert_port_forwarded_properly(guest_port, host_port) + assert_host_to_vm_network("http://localhost:#{host_port}/", guest_port) end it "properly overrides port forwarding from the same port" do @@ -74,7 +55,7 @@ VFILE end assert_execute("vagrant", "up") - assert_port_forwarded_properly(guest_port, host_port) + assert_host_to_vm_network("http://localhost:#{host_port}/", guest_port) end it "detects and corrects port collisions" do diff --git a/test/acceptance/support/network_tests.rb b/test/acceptance/support/network_tests.rb new file mode 100644 index 000000000..ee554e0f3 --- /dev/null +++ b/test/acceptance/support/network_tests.rb @@ -0,0 +1,29 @@ +require "vagrant/util/retryable" + +module Acceptance + module NetworkTests + include Vagrant::Util::Retryable + + # Tests that the host can access the VM through the network. + # + # @param [String] url URL to request from the host. + # @param [Integer] guest_port Port to run a web server on the guest. + def assert_host_to_vm_network(url, guest_port) + # Start up a web server in another thread by SSHing into the VM. + thr = Thread.new do + assert_execute("vagrant", "ssh", "-c", "python -m SimpleHTTPServer #{guest_port}") + end + + # Verify that port forwarding works by making a simple HTTP request + # to the port. We should get a 200 response. We retry this a few times + # as we wait for the HTTP server to come online. + retryable(:tries => 5, :sleep => 2) do + result = Net::HTTP.get_response(URI.parse(url)) + result.code.should == "200" + end + ensure + # The server needs to die. This is how. + thr.kill if thr + end + end +end