Add Logs to ContainerAttachOptions

Signed-off-by: Andy Goldstein <agoldste@redhat.com>
This commit is contained in:
Andy Goldstein 2016-09-19 14:55:52 -04:00
parent 15fb3fd9da
commit fc8097f957
3 changed files with 43 additions and 0 deletions

View File

@ -23,6 +23,7 @@ type ContainerAttachOptions struct {
Stdout bool
Stderr bool
DetachKeys string
Logs bool
}
// ContainerCommitOptions holds parameters to commit changes into a container.

View File

@ -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)

View File

@ -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"))
}