Test overriding ports works properly

This commit is contained in:
Mitchell Hashimoto 2012-01-08 23:29:40 -08:00
parent 29eb419c86
commit d430d62941
1 changed files with 36 additions and 16 deletions

View File

@ -20,6 +20,24 @@ describe "vagrant port forwarding" do
env.execute("vagrant", "box", "add", "base", box_path("default")).should succeed env.execute("vagrant", "box", "add", "base", box_path("default")).should succeed
end 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 it "forwards ports properly" do
initialize_environment initialize_environment
@ -36,25 +54,27 @@ VFILE
end end
assert_execute("vagrant", "up") assert_execute("vagrant", "up")
assert_port_forwarded_properly(guest_port, host_port)
end
thr = nil it "properly overrides port forwarding from the same port" do
begin initialize_environment
# 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 guest_port = 3000
# to the port. We should get a 200 response. We retry this a few times host_port = 5000
# as we wait for the HTTP server to come online.
retryable(:tries => 5, :sleep => 2) do environment.workdir.join("Vagrantfile").open("w+") do |f|
result = Net::HTTP.get_response(URI.parse("http://localhost:#{host_port}/")) f.puts(<<VFILE)
result.code.should == "200" Vagrant::Config.run do |config|
end config.vm.box = "base"
ensure config.vm.forward_port #{guest_port}, #{host_port - 1}
# The server needs to die. This is how. config.vm.forward_port #{guest_port}, #{host_port}
thr.kill if thr end
VFILE
end end
assert_execute("vagrant", "up")
assert_port_forwarded_properly(guest_port, host_port)
end end
it "detects and corrects port collisions" do it "detects and corrects port collisions" do