Skip to content

Commit

Permalink
If the ryuk container has a health status, wait for a healthy contain…
Browse files Browse the repository at this point in the history
…er before returning.
  • Loading branch information
emetsger committed Apr 22, 2024
1 parent 539284c commit db38887
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docker.go
Expand Up @@ -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 .*")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1550,6 +1557,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
}

Expand Down
4 changes: 4 additions & 0 deletions reaper.go
Expand Up @@ -116,6 +116,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
Expand Down

0 comments on commit db38887

Please sign in to comment.