Skip to content

Commit

Permalink
chore: improve lifecycle errors (#2708)
Browse files Browse the repository at this point in the history
Improve the lifecycle errors with wrapping so that its easier to
determine the cause of an error.
  • Loading branch information
stevenh authored Aug 9, 2024
1 parent 150a48c commit c88e776
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 34 deletions.
23 changes: 6 additions & 17 deletions container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"log"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -319,28 +318,18 @@ func Test_GetLogsFromFailedContainer(t *testing.T) {
ContainerRequest: req,
Started: true,
})

if err != nil && err.Error() != "failed to start container: container exited with code 0" {
t.Fatal(err)
} else if err == nil {
terminateContainerOnEnd(t, ctx, c)
t.Fatal("was expecting error starting container")
}
terminateContainerOnEnd(t, ctx, c)
require.Error(t, err)
require.Contains(t, err.Error(), "container exited with code 0")

logs, logErr := c.Logs(ctx)
if logErr != nil {
t.Fatal(logErr)
}
require.NoError(t, logErr)

b, err := io.ReadAll(logs)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

log := string(b)
if strings.Contains(log, "I was not expecting this") == false {
t.Fatalf("could not find expected log in %s", log)
}
require.Contains(t, log, "I was not expecting this")
}

// dockerImageSubstitutor {
Expand Down
8 changes: 4 additions & 4 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,24 +221,24 @@ func (c *DockerContainer) SessionID() string {
func (c *DockerContainer) Start(ctx context.Context) error {
err := c.startingHook(ctx)
if err != nil {
return err
return fmt.Errorf("starting hook: %w", err)
}

if err := c.provider.client.ContainerStart(ctx, c.ID, container.StartOptions{}); err != nil {
return err
return fmt.Errorf("container start: %w", err)
}
defer c.provider.Close()

err = c.startedHook(ctx)
if err != nil {
return err
return fmt.Errorf("started hook: %w", err)
}

c.isRunning = true

err = c.readiedHook(ctx)
if err != nil {
return err
return fmt.Errorf("readied hook: %w", err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ var defaultReadinessHook = func() ContainerLifecycleHooks {
dockerContainer.ID[:12], dockerContainer.Image, dockerContainer.WaitingFor,
)
if err := dockerContainer.WaitingFor.WaitUntilReady(ctx, c); err != nil {
return err
return fmt.Errorf("wait until ready: %w", err)
}
}

Expand Down
17 changes: 6 additions & 11 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,14 @@ func TestWithLogConsumers(t *testing.T) {
err := testcontainers.WithLogConsumers(lc)(&req)
require.NoError(t, err)

c, err := testcontainers.GenericContainer(context.Background(), req)
ctx := context.Background()
c, err := testcontainers.GenericContainer(ctx, req)
terminateContainerOnEnd(t, ctx, c)
// we expect an error because the MySQL environment variables are not set
// but this is expected because we just want to test the log consumer
require.EqualError(t, err, "failed to start container: container exited with code 1")
// c might be not nil even on error
if c != nil {
defer func() {
err = c.Terminate(context.Background())
require.NoError(t, err)
}()
}

assert.NotEmpty(t, lc.msgs)
require.Error(t, err)
require.Contains(t, err.Error(), "container exited with code 1")
require.NotEmpty(t, lc.msgs)
}

func TestWithStartupCommand(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion wait/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type StrategyTarget interface {
func checkTarget(ctx context.Context, target StrategyTarget) error {
state, err := target.State(ctx)
if err != nil {
return err
return fmt.Errorf("get state: %w", err)
}

return checkState(state)
Expand Down

0 comments on commit c88e776

Please sign in to comment.