diff --git a/container_test.go b/container_test.go index 0da1b967f8..d2a070f2de 100644 --- a/container_test.go +++ b/container_test.go @@ -8,7 +8,6 @@ import ( "fmt" "io" "log" - "strings" "testing" "time" @@ -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 { diff --git a/docker.go b/docker.go index c3dfcb8da2..bc99cbc2dd 100644 --- a/docker.go +++ b/docker.go @@ -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 diff --git a/lifecycle.go b/lifecycle.go index a0c6b99b88..ce46a77916 100644 --- a/lifecycle.go +++ b/lifecycle.go @@ -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) } } diff --git a/options_test.go b/options_test.go index 13717f1da0..e19ecde96e 100644 --- a/options_test.go +++ b/options_test.go @@ -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) { diff --git a/wait/wait.go b/wait/wait.go index c9bc0606f6..ccfd4735e2 100644 --- a/wait/wait.go +++ b/wait/wait.go @@ -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)