Skip to content

Commit

Permalink
Wait for Docker container to start, to avoid test flake
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbrictson committed Jun 21, 2024
1 parent a87b263 commit 6b5f18d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ def flush_connections
class FunctionalTest < Minitest::Test
def setup
require_relative "support/docker_wrapper"
DockerWrapper.start unless DockerWrapper.running?
return if DockerWrapper.running?

DockerWrapper.start
DockerWrapper.wait_for_ssh_server
end
end

Expand Down
14 changes: 13 additions & 1 deletion test/support/docker_wrapper.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
require "socket"

Minitest.after_run do
DockerWrapper.stop if DockerWrapper.running?
end

module DockerWrapper
SSH_SERVER_PORT = 2122

class << self
def host
SSHKit::Host.new(
user: "deployer",
hostname: "localhost",
port: "2122",
port: SSH_SERVER_PORT,
password: "topsecret",
ssh_options: host_verify_options
)
Expand All @@ -27,6 +31,14 @@ def stop
run_compose_command("down")
end

def wait_for_ssh_server(retries=3)
Socket.tcp("localhost", SSH_SERVER_PORT, connect_timeout: 1).close
rescue Errno::ECONNREFUSED, Errno::ETIMEDOUT
retries -= 1
sleep(2) && retry if retries.positive?
raise
end

private

def run_compose_command(command, echo=true)
Expand Down

0 comments on commit 6b5f18d

Please sign in to comment.