From 5dc5c1d5a2e261a6f014eade62924e37148e5873 Mon Sep 17 00:00:00 2001 From: esm Date: Sun, 21 Apr 2024 22:14:10 -0400 Subject: [PATCH] If the ryuk container has a health status, wait for a healthy container before returning. --- docker.go | 12 ++++++++++++ reaper.go | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/docker.go b/docker.go index e188c25df22..6e99165716f 100644 --- a/docker.go +++ b/docker.go @@ -49,6 +49,11 @@ const ( packagePath = "github.com/testcontainers/testcontainers-go" logStoppedForOutOfSyncMessage = "Stopping log consumer: Headers out of sync" + + healthStatusNone = "" // default status for a container with no healthcheck + healthStatusHealthy = "healthy" // healthy container + healthStatusStarting = "starting" // starting container + healthStatusUnhealthy = "unhealthy" // unhealthy container ) var createContainerFailDueToNameConflictRegex = regexp.MustCompile("Conflict. The container name .* is already in use by container .*") @@ -86,6 +91,8 @@ type DockerContainer struct { logProductionTimeout *time.Duration logger Logging lifecycleHooks []ContainerLifecycleHooks + + healthStatus string // container health status, will default to healthStatusNone if no healthcheck is present } // SetLogger sets the logger for the container @@ -1590,6 +1597,11 @@ func containerFromDockerResponse(ctx context.Context, response types.Container) return nil, err } + // the health status of the container, if any + if health := container.raw.State.Health; health != nil { + container.healthStatus = health.Status + } + return &container, nil } diff --git a/reaper.go b/reaper.go index 54feb90cbe1..87205d0ff1f 100644 --- a/reaper.go +++ b/reaper.go @@ -117,6 +117,10 @@ func lookUpReaperContainer(ctx context.Context, sessionID string) (*DockerContai return err } + if r.healthStatus != healthStatusHealthy && r.healthStatus != healthStatusNone { + return fmt.Errorf("container %s is not healthy, wanted status=%s, got status=%s", resp[0].ID[:8], healthStatusHealthy, r.healthStatus) + } + reaperContainer = r return nil