From c2d81fc9a074f24d7fbaa6d3b765cf2d5d9c2537 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Tue, 14 Jan 2020 14:07:31 +0100 Subject: [PATCH] Fix pull on ContainerStart wrapper used in tests (#15511) ContainerStart wrapper used in tests was not waiting for the pull to finish, causing flakiness, e.g. in TestDockerStart. Pull can be waited to finish by calling to Close() in the response body. This is also something that has to be done at some moment to avoid leaks. Also add some more context on error messages. (cherry picked from commit dd553b37327d09de957c5c83fcf98879ab34c6cc) --- libbeat/tests/docker/docker.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/libbeat/tests/docker/docker.go b/libbeat/tests/docker/docker.go index ce137da641cd..afa18317eb9d 100644 --- a/libbeat/tests/docker/docker.go +++ b/libbeat/tests/docker/docker.go @@ -20,6 +20,8 @@ package docker import ( "context" + "github.com/pkg/errors" + "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" "github.com/docker/docker/client" @@ -41,9 +43,11 @@ func NewClient() (Client, error) { // ContainerStart pulls and starts the given container func (c Client) ContainerStart(image string, cmd []string, labels map[string]string) (string, error) { ctx := context.Background() - if _, err := c.cli.ImagePull(ctx, image, types.ImagePullOptions{}); err != nil { - return "", err + respBody, err := c.cli.ImagePull(ctx, image, types.ImagePullOptions{}) + if err != nil { + return "", errors.Wrapf(err, "pullling image %s", image) } + defer respBody.Close() resp, err := c.cli.ContainerCreate(ctx, &container.Config{ Image: image, @@ -51,11 +55,11 @@ func (c Client) ContainerStart(image string, cmd []string, labels map[string]str Labels: labels, }, nil, nil, "") if err != nil { - return "", err + return "", errors.Wrap(err, "creating container") } if err := c.cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil { - return "", err + return "", errors.Wrap(err, "starting container") } return resp.ID, nil