Wait for healthy containers in integration test

Rather than waiting 5 seconds and hoping for the best after we boot
docker compose, add docker healthchecks and wait for all the containers
to be healthy.
This commit is contained in:
Donal McBreen
2023-04-25 15:32:48 +01:00
parent 9ec3895dab
commit 52ca5b846a
9 changed files with 61 additions and 17 deletions

View File

@@ -1,10 +1,11 @@
require "net/http"
require "test_helper"
class DeployTest < ActiveSupport::TestCase
setup do
docker_compose "up --build --force-recreate -d"
sleep 5
wait_for_healthy
end
teardown do
@@ -20,12 +21,29 @@ class DeployTest < ActiveSupport::TestCase
end
private
def docker_compose(*commands)
system("cd test/integration && docker compose #{commands.join(" ")}")
def docker_compose(*commands, capture: false)
command = "docker compose #{commands.join(" ")}"
succeeded = false
if capture
result = stdouted { succeeded = system("cd test/integration && #{command}") }
else
succeeded = system("cd test/integration && #{command}")
end
raise "Command `#{command}` failed with error code `#{$?}`" unless succeeded
result
end
def mrsk(*commands)
docker_compose("exec deployer mrsk #{commands.join(" ")}")
def deployer_exec(*commands, capture: false)
if capture
stdouted { docker_compose("exec deployer #{commands.join(" ")}") }
else
docker_compose("exec deployer #{commands.join(" ")}", capture: capture)
end
end
def mrsk(*commands, capture: false)
deployer_exec(:mrsk, *commands, capture: capture)
end
def assert_app_is_down
@@ -39,4 +57,12 @@ class DeployTest < ActiveSupport::TestCase
def app_response
Net::HTTP.get_response(URI.parse("http://localhost:12345"))
end
def wait_for_healthy(timeout: 20)
timeout_at = Time.now + timeout
while docker_compose("ps -a | tail -n +2 | grep -v '(healthy)' | wc -l", capture: true) != "0"
raise "Container not healthy after #{timeout} seconds" if timeout_at < Time.now
sleep 0.1
end
end
end