From 865a70e7caeab592095e7dae99c08bfbee8b41dd Mon Sep 17 00:00:00 2001 From: Mikhail Mazurskiy <126021+ash2k@users.noreply.github.com> Date: Fri, 28 Jun 2024 19:50:31 +1000 Subject: [PATCH] chore: test cleanups (#2608) * Assert the error is what we expect * Avoid panicking on c==nil case * Add missing error handling * Discard read bytes to not waste RAM --- docker.go | 7 ++++--- options_test.go | 13 ++++++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/docker.go b/docker.go index 616c76c752..c808c6fcb9 100644 --- a/docker.go +++ b/docker.go @@ -3,7 +3,6 @@ package testcontainers import ( "archive/tar" "bufio" - "bytes" "context" "encoding/base64" "encoding/binary" @@ -894,6 +893,9 @@ var _ ContainerProvider = (*DockerProvider)(nil) // BuildImage will build and image from context and Dockerfile, then return the tag func (p *DockerProvider) BuildImage(ctx context.Context, img ImageBuildInfo) (string, error) { buildOptions, err := img.BuildOptions() + if err != nil { + return "", err + } var buildError error var resp types.ImageBuildResponse @@ -925,8 +927,7 @@ func (p *DockerProvider) BuildImage(ctx context.Context, img ImageBuildInfo) (st // need to read the response from Docker, I think otherwise the image // might not finish building before continuing to execute here - buf := new(bytes.Buffer) - _, err = buf.ReadFrom(resp.Body) + _, err = io.Copy(io.Discard, resp.Body) if err != nil { return "", err } diff --git a/options_test.go b/options_test.go index 94d01adc59..13717f1da0 100644 --- a/options_test.go +++ b/options_test.go @@ -94,11 +94,14 @@ func TestWithLogConsumers(t *testing.T) { c, err := testcontainers.GenericContainer(context.Background(), req) // 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.Error(t, err) - defer func() { - err = c.Terminate(context.Background()) - require.NoError(t, err) - }() + 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) }