Skip to content

Commit

Permalink
chore: better container logs on errors during startup
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Sep 11, 2023
1 parent b5d3749 commit 0e28881
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testcontainers

import (
"context"
"fmt"
"io"
"strings"

Expand Down Expand Up @@ -131,8 +132,7 @@ func (c *DockerContainer) startingHook(ctx context.Context) error {
for _, lifecycleHooks := range c.lifecycleHooks {
err := containerHookFn(ctx, lifecycleHooks.PreStarts)(c)
if err != nil {
c.printLogs(ctx)
return err
return printLogs(ctx, c, err)
}
}

Expand All @@ -144,30 +144,27 @@ func (c *DockerContainer) startedHook(ctx context.Context) error {
for _, lifecycleHooks := range c.lifecycleHooks {
err := containerHookFn(ctx, lifecycleHooks.PostStarts)(c)
if err != nil {
c.printLogs(ctx)
return err
return printLogs(ctx, c, err)
}
}

return nil
}

// printLogs is a helper function that will print the logs of a Docker container
// printLogs is a helper function that will print the logs of a Docker container after an error occurs
// We are going to use this helper function to inform the user of the logs when an error occurs
func (c *DockerContainer) printLogs(ctx context.Context) {
func printLogs(ctx context.Context, c *DockerContainer, rootCause error) error {
reader, err := c.Logs(ctx)
if err != nil {
c.logger.Printf("failed accessing container logs: %w\n", err)
return
return fmt.Errorf("failed accessing container logs: %w", err)
}

b, err := io.ReadAll(reader)
if err != nil {
c.logger.Printf("failed reading container logs: %w\n", err)
return
return fmt.Errorf("failed reading container logs: %w", err)
}

c.logger.Printf("container logs:\n%s", b)
return fmt.Errorf("%s: %w", b, rootCause)
}

// stoppingHook is a hook that will be called before a container is stopped
Expand Down

0 comments on commit 0e28881

Please sign in to comment.