From fc8097f957ec7ae990f84faf54bce85a399c96af Mon Sep 17 00:00:00 2001 From: Andy Goldstein Date: Mon, 19 Sep 2016 14:55:52 -0400 Subject: [PATCH] Add Logs to ContainerAttachOptions Signed-off-by: Andy Goldstein --- api/types/client.go | 1 + client/container_attach.go | 3 ++ integration-cli/docker_api_attach_test.go | 39 +++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/api/types/client.go b/api/types/client.go index b4d6a3c017..c226a91067 100644 --- a/api/types/client.go +++ b/api/types/client.go @@ -23,6 +23,7 @@ type ContainerAttachOptions struct { Stdout bool Stderr bool DetachKeys string + Logs bool } // ContainerCommitOptions holds parameters to commit changes into a container. diff --git a/client/container_attach.go b/client/container_attach.go index 7cfc860fcc..eea4682158 100644 --- a/client/container_attach.go +++ b/client/container_attach.go @@ -28,6 +28,9 @@ func (cli *Client) ContainerAttach(ctx context.Context, container string, option if options.DetachKeys != "" { query.Set("detachKeys", options.DetachKeys) } + if options.Logs { + query.Set("logs", "1") + } headers := map[string][]string{"Content-Type": {"text/plain"}} return cli.postHijacked(ctx, "/containers/"+container+"/attach", query, nil, headers) diff --git a/integration-cli/docker_api_attach_test.go b/integration-cli/docker_api_attach_test.go index 740ce6ecdd..d43bf3ab0e 100644 --- a/integration-cli/docker_api_attach_test.go +++ b/integration-cli/docker_api_attach_test.go @@ -2,13 +2,18 @@ package main import ( "bufio" + "bytes" + "context" "io" "net" "net/http" "strings" "time" + "github.com/docker/docker/api/types" + "github.com/docker/docker/client" "github.com/docker/docker/pkg/integration/checker" + "github.com/docker/docker/pkg/stdcopy" "github.com/go-check/check" "golang.org/x/net/websocket" ) @@ -168,4 +173,38 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) { // Nothing should be received because both the stdout and stderr of the container will be // sent to the client as stdout when tty is enabled. expectTimeout(conn, br, "stdout") + + // Test the client API + // Make sure we don't see "hello" if Logs is false + client, err := client.NewEnvClient() + c.Assert(err, checker.IsNil) + + cid, _ = dockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "echo hello; cat") + cid = strings.TrimSpace(cid) + + attachOpts := types.ContainerAttachOptions{ + Stream: true, + Stdin: true, + Stdout: true, + } + + resp, err := client.ContainerAttach(context.Background(), cid, attachOpts) + c.Assert(err, checker.IsNil) + expectSuccess(resp.Conn, resp.Reader, "stdout", false) + + // Make sure we do see "hello" if Logs is true + attachOpts.Logs = true + resp, err = client.ContainerAttach(context.Background(), cid, attachOpts) + c.Assert(err, checker.IsNil) + + defer resp.Conn.Close() + resp.Conn.SetReadDeadline(time.Now().Add(time.Second)) + + _, err = resp.Conn.Write([]byte("success")) + c.Assert(err, checker.IsNil) + + actualStdout := new(bytes.Buffer) + actualStderr := new(bytes.Buffer) + stdcopy.StdCopy(actualStdout, actualStderr, resp.Reader) + c.Assert(actualStdout.Bytes(), checker.DeepEquals, []byte("hello\nsuccess"), check.Commentf("Attach didn't return the expected data from stdout")) }