Merge pull request #6544 from mitchellh/sethvargo/race_test

Fix port collision in tests
This commit is contained in:
Seth Vargo 2015-11-18 18:50:44 -08:00
commit a0ab17f2e7
4 changed files with 30 additions and 5 deletions

View File

@ -39,7 +39,10 @@ describe VagrantPlugins::FTPPush::FTPAdapter do
include_context "unit"
before(:all) do
@server = FakeFtp::Server.new(21212, 21213)
@server = nil
with_random_port do |port1, port2|
@server = FakeFtp::Server.new(port1, port2)
end
@server.start
end

View File

@ -9,7 +9,7 @@ describe VagrantPlugins::FTPPush::Push do
let(:env) { isolated_environment }
let(:config) do
double("config",
host: "127.0.0.1:51234",
host: "127.0.0.1:#{@port}",
username: "sethvargo",
password: "bacon",
passive: false,
@ -34,7 +34,11 @@ describe VagrantPlugins::FTPPush::Push do
describe "#push" do
before(:all) do
@server = FakeFtp::Server.new(51234, 21213)
@server = nil
with_random_port do |port1, port2|
@port = port1
@server = FakeFtp::Server.new(port1, port2)
end
@server.start
@dir = Dir.mktmpdir

View File

@ -116,4 +116,18 @@ shared_context "unit" do
ENV[key] = value
end
end
# This helper provides a randomly available port(s) for each argument to the
# block.
def with_random_port(&block)
ports = []
block.arity.times do
server = TCPServer.new('127.0.0.1', 0)
ports << server.addr[1]
server.close
end
block.call(*ports)
end
end

View File

@ -42,8 +42,12 @@ describe Vagrant::Action::Builtin::BoxAdd do
tf = Tempfile.new("vagrant")
tf.close
port = 21212
server = FakeFtp::Server.new(port, port+1)
port = nil
server = nil
with_random_port do |port1, port2|
port = port1
server = FakeFtp::Server.new(port1, port2)
end
server.add_file(path.basename, path.read)
server.start
yield port