Prechádzať zdrojové kódy

Add Logs to ContainerAttachOptions

Signed-off-by: Andy Goldstein <agoldste@redhat.com>
Andy Goldstein 8 rokov pred
rodič
commit
fc8097f957

+ 1 - 0
api/types/client.go

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

+ 3 - 0
client/container_attach.go

@@ -28,6 +28,9 @@ func (cli *Client) ContainerAttach(ctx context.Context, container string, option
 	if options.DetachKeys != "" {
 	if options.DetachKeys != "" {
 		query.Set("detachKeys", options.DetachKeys)
 		query.Set("detachKeys", options.DetachKeys)
 	}
 	}
+	if options.Logs {
+		query.Set("logs", "1")
+	}
 
 
 	headers := map[string][]string{"Content-Type": {"text/plain"}}
 	headers := map[string][]string{"Content-Type": {"text/plain"}}
 	return cli.postHijacked(ctx, "/containers/"+container+"/attach", query, nil, headers)
 	return cli.postHijacked(ctx, "/containers/"+container+"/attach", query, nil, headers)

+ 39 - 0
integration-cli/docker_api_attach_test.go

@@ -2,13 +2,18 @@ package main
 
 
 import (
 import (
 	"bufio"
 	"bufio"
+	"bytes"
+	"context"
 	"io"
 	"io"
 	"net"
 	"net"
 	"net/http"
 	"net/http"
 	"strings"
 	"strings"
 	"time"
 	"time"
 
 
+	"github.com/docker/docker/api/types"
+	"github.com/docker/docker/client"
 	"github.com/docker/docker/pkg/integration/checker"
 	"github.com/docker/docker/pkg/integration/checker"
+	"github.com/docker/docker/pkg/stdcopy"
 	"github.com/go-check/check"
 	"github.com/go-check/check"
 	"golang.org/x/net/websocket"
 	"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
 	// 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.
 	// sent to the client as stdout when tty is enabled.
 	expectTimeout(conn, br, "stdout")
 	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"))
 }
 }