Skip to content

Commit

Permalink
Fix pull on ContainerStart wrapper used in tests (elastic#15511)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jsoriano authored Jan 14, 2020
1 parent 6196c59 commit dd553b3
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions libbeat/tests/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -41,21 +43,23 @@ 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,
Cmd: cmd,
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
Expand Down

0 comments on commit dd553b3

Please sign in to comment.