diff --git a/test/unit/plugins/pushes/ftp/adapter_test.rb b/test/unit/plugins/pushes/ftp/adapter_test.rb index e929078cd..6ef07196a 100644 --- a/test/unit/plugins/pushes/ftp/adapter_test.rb +++ b/test/unit/plugins/pushes/ftp/adapter_test.rb @@ -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 diff --git a/test/unit/plugins/pushes/ftp/push_test.rb b/test/unit/plugins/pushes/ftp/push_test.rb index 1f6773e24..2430138bc 100644 --- a/test/unit/plugins/pushes/ftp/push_test.rb +++ b/test/unit/plugins/pushes/ftp/push_test.rb @@ -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 diff --git a/test/unit/support/shared/base_context.rb b/test/unit/support/shared/base_context.rb index bd7f3f517..295e83b66 100644 --- a/test/unit/support/shared/base_context.rb +++ b/test/unit/support/shared/base_context.rb @@ -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 diff --git a/test/unit/vagrant/action/builtin/box_add_test.rb b/test/unit/vagrant/action/builtin/box_add_test.rb index 44bcbca24..cc034df02 100644 --- a/test/unit/vagrant/action/builtin/box_add_test.rb +++ b/test/unit/vagrant/action/builtin/box_add_test.rb @@ -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