Skip to content

Commit

Permalink
chore: test cleanups (testcontainers#2608)
Browse files Browse the repository at this point in the history
* Assert the error is what we expect

* Avoid panicking on c==nil case

* Add missing error handling

* Discard read bytes to not waste RAM
  • Loading branch information
ash2k authored Jun 28, 2024
1 parent 401ab9b commit 865a70e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
7 changes: 4 additions & 3 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package testcontainers
import (
"archive/tar"
"bufio"
"bytes"
"context"
"encoding/base64"
"encoding/binary"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
13 changes: 8 additions & 5 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 865a70e

Please sign in to comment.