mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #26718 from ncdc/add-logs-to-container-attach-options
Add Logs to ContainerAttachOptions
This commit is contained in:
commit
4d2e258531
3 changed files with 43 additions and 0 deletions
|
@ -23,6 +23,7 @@ type ContainerAttachOptions struct {
|
|||
Stdout bool
|
||||
Stderr bool
|
||||
DetachKeys string
|
||||
Logs bool
|
||||
}
|
||||
|
||||
// ContainerCommitOptions holds parameters to commit changes into a container.
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"))
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue