Merge pull request #16920 from mountkin/fix-attach-test

fix the flaws in the test of the attach API
This commit is contained in:
Alexander Morozov 2015-11-13 12:05:22 -08:00
commit 4302c14a64
1 changed files with 85 additions and 147 deletions

View File

@ -1,13 +1,14 @@
package main package main
import ( import (
"bytes" "bufio"
"io" "io"
"net"
"net/http" "net/http"
"net/http/httputil"
"strings" "strings"
"time" "time"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check" "github.com/go-check/check"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
) )
@ -17,23 +18,17 @@ func (s *DockerSuite) TestGetContainersAttachWebsocket(c *check.C) {
out, _ := dockerCmd(c, "run", "-dit", "busybox", "cat") out, _ := dockerCmd(c, "run", "-dit", "busybox", "cat")
rwc, err := sockConn(time.Duration(10 * time.Second)) rwc, err := sockConn(time.Duration(10 * time.Second))
if err != nil { c.Assert(err, checker.IsNil)
c.Fatal(err)
}
cleanedContainerID := strings.TrimSpace(out) cleanedContainerID := strings.TrimSpace(out)
config, err := websocket.NewConfig( config, err := websocket.NewConfig(
"/containers/"+cleanedContainerID+"/attach/ws?stream=1&stdin=1&stdout=1&stderr=1", "/containers/"+cleanedContainerID+"/attach/ws?stream=1&stdin=1&stdout=1&stderr=1",
"http://localhost", "http://localhost",
) )
if err != nil { c.Assert(err, checker.IsNil)
c.Fatal(err)
}
ws, err := websocket.NewClient(config, rwc) ws, err := websocket.NewClient(config, rwc)
if err != nil { c.Assert(err, checker.IsNil)
c.Fatal(err)
}
defer ws.Close() defer ws.Close()
expected := []byte("hello") expected := []byte("hello")
@ -55,172 +50,115 @@ func (s *DockerSuite) TestGetContainersAttachWebsocket(c *check.C) {
select { select {
case err := <-inChan: case err := <-inChan:
if err != nil { c.Assert(err, checker.IsNil)
c.Fatal(err)
}
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
c.Fatal("Timeout writing to ws") c.Fatal("Timeout writing to ws")
} }
select { select {
case err := <-outChan: case err := <-outChan:
if err != nil { c.Assert(err, checker.IsNil)
c.Fatal(err)
}
case <-time.After(5 * time.Second): case <-time.After(5 * time.Second):
c.Fatal("Timeout reading from ws") c.Fatal("Timeout reading from ws")
} }
if !bytes.Equal(expected, actual) { c.Assert(actual, checker.DeepEquals, expected, check.Commentf("Websocket didn't return the expected data"))
c.Fatal("Expected output on websocket to match input")
}
} }
// regression gh14320 // regression gh14320
func (s *DockerSuite) TestPostContainersAttachContainerNotFound(c *check.C) { func (s *DockerSuite) TestPostContainersAttachContainerNotFound(c *check.C) {
status, body, err := sockRequest("POST", "/containers/doesnotexist/attach", nil) status, body, err := sockRequest("POST", "/containers/doesnotexist/attach", nil)
c.Assert(status, check.Equals, http.StatusNotFound) c.Assert(status, checker.Equals, http.StatusNotFound)
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
expected := "no such id: doesnotexist\n" expected := "no such id: doesnotexist\n"
if !strings.Contains(string(body), expected) { c.Assert(string(body), checker.Contains, expected)
c.Fatalf("Expected response body to contain %q", expected)
}
} }
func (s *DockerSuite) TestGetContainersWsAttachContainerNotFound(c *check.C) { func (s *DockerSuite) TestGetContainersWsAttachContainerNotFound(c *check.C) {
status, body, err := sockRequest("GET", "/containers/doesnotexist/attach/ws", nil) status, body, err := sockRequest("GET", "/containers/doesnotexist/attach/ws", nil)
c.Assert(status, check.Equals, http.StatusNotFound) c.Assert(status, checker.Equals, http.StatusNotFound)
c.Assert(err, check.IsNil) c.Assert(err, checker.IsNil)
expected := "no such id: doesnotexist\n" expected := "no such id: doesnotexist\n"
if !strings.Contains(string(body), expected) { c.Assert(string(body), checker.Contains, expected)
c.Fatalf("Expected response body to contain %q", expected)
}
} }
func (s *DockerSuite) TestPostContainersAttach(c *check.C) { func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
testRequires(c, DaemonIsLinux) testRequires(c, DaemonIsLinux)
out, _ := dockerCmd(c, "run", "-dit", "busybox", "cat")
r, w := io.Pipe() expectSuccess := func(conn net.Conn, br *bufio.Reader, stream string, tty bool) {
defer r.Close() defer conn.Close()
defer w.Close() expected := []byte("success")
_, err := conn.Write(expected)
c.Assert(err, checker.IsNil)
conn, err := sockConn(time.Duration(10 * time.Second)) conn.SetReadDeadline(time.Now().Add(time.Second))
c.Assert(err, check.IsNil) lenHeader := 0
if !tty {
containerID := strings.TrimSpace(out) lenHeader = 8
}
req, err := http.NewRequest("POST", "/containers/"+containerID+"/attach?stream=1&stdin=1&stdout=1&stderr=1", bytes.NewReader([]byte{})) actual := make([]byte, len(expected)+lenHeader)
c.Assert(err, check.IsNil) _, err = io.ReadFull(br, actual)
c.Assert(err, checker.IsNil)
client := httputil.NewClientConn(conn, nil) if !tty {
defer client.Close() fdMap := map[string]byte{
"stdin": 0,
// Do POST attach request "stdout": 1,
resp, err := client.Do(req) "stderr": 2,
c.Assert(resp.StatusCode, check.Equals, http.StatusOK) }
// If we check the err, we get a ErrPersistEOF = &http.ProtocolError{ErrorString: "persistent connection closed"} c.Assert(actual[0], checker.Equals, fdMap[stream])
// This means that the remote requested this be the last request serviced, is this okay? }
c.Assert(actual[lenHeader:], checker.DeepEquals, expected, check.Commentf("Attach didn't return the expected data from %s", stream))
// Test read and write to the attached container
expected := []byte("hello")
actual := make([]byte, len(expected))
outChan := make(chan error)
go func() {
_, err := r.Read(actual)
outChan <- err
close(outChan)
}()
inChan := make(chan error)
go func() {
_, err := w.Write(expected)
inChan <- err
close(inChan)
}()
select {
case err := <-inChan:
c.Assert(err, check.IsNil)
case <-time.After(5 * time.Second):
c.Fatal("Timeout writing to stdout")
} }
select { expectTimeout := func(conn net.Conn, br *bufio.Reader, stream string) {
case err := <-outChan: defer conn.Close()
c.Assert(err, check.IsNil) _, err := conn.Write([]byte{'t'})
case <-time.After(5 * time.Second): c.Assert(err, checker.IsNil)
c.Fatal("Timeout reading from stdin")
conn.SetReadDeadline(time.Now().Add(time.Second))
actual := make([]byte, 1)
_, err = io.ReadFull(br, actual)
opErr, ok := err.(*net.OpError)
c.Assert(ok, checker.Equals, true, check.Commentf("Error is expected to be *net.OpError, got %v", err))
c.Assert(opErr.Timeout(), checker.Equals, true, check.Commentf("Read from %s is expected to timeout", stream))
} }
if !bytes.Equal(expected, actual) { // Create a container that only emits stdout.
c.Fatal("Expected output to match input") cid, _ := dockerCmd(c, "run", "-di", "busybox", "cat")
} cid = strings.TrimSpace(cid)
// Attach to the container's stdout stream.
conn, br, err := sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
c.Assert(err, checker.IsNil)
// Check if the data from stdout can be received.
expectSuccess(conn, br, "stdout", false)
// Attach to the container's stderr stream.
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
c.Assert(err, checker.IsNil)
// Since the container only emits stdout, attaching to stderr should return nothing.
expectTimeout(conn, br, "stdout")
resp.Body.Close() // Test the simlar functions of the stderr stream.
} cid, _ = dockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "cat >&2")
cid = strings.TrimSpace(cid)
func (s *DockerSuite) TestPostContainersAttachStderr(c *check.C) { conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
testRequires(c, DaemonIsLinux) c.Assert(err, checker.IsNil)
out, _ := dockerCmd(c, "run", "-dit", "busybox", "/bin/sh", "-c", "cat >&2") expectSuccess(conn, br, "stderr", false)
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
r, w := io.Pipe() c.Assert(err, checker.IsNil)
defer r.Close() expectTimeout(conn, br, "stderr")
defer w.Close()
// Test with tty.
conn, err := sockConn(time.Duration(10 * time.Second)) cid, _ = dockerCmd(c, "run", "-dit", "busybox", "/bin/sh", "-c", "cat >&2")
c.Assert(err, check.IsNil) cid = strings.TrimSpace(cid)
// Attach to stdout only.
containerID := strings.TrimSpace(out) conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
c.Assert(err, checker.IsNil)
req, err := http.NewRequest("POST", "/containers/"+containerID+"/attach?stream=1&stdin=1&stdout=1&stderr=1", bytes.NewReader([]byte{})) expectSuccess(conn, br, "stdout", true)
c.Assert(err, check.IsNil)
// Attach without stdout stream.
client := httputil.NewClientConn(conn, nil) conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
defer client.Close() c.Assert(err, checker.IsNil)
// Nothing should be received because both the stdout and stderr of the container will be
// Do POST attach request // sent to the client as stdout when tty is enabled.
resp, err := client.Do(req) expectTimeout(conn, br, "stdout")
c.Assert(resp.StatusCode, check.Equals, http.StatusOK)
// If we check the err, we get a ErrPersistEOF = &http.ProtocolError{ErrorString: "persistent connection closed"}
// This means that the remote requested this be the last request serviced, is this okay?
// Test read and write to the attached container
expected := []byte("hello")
actual := make([]byte, len(expected))
outChan := make(chan error)
go func() {
_, err := r.Read(actual)
outChan <- err
close(outChan)
}()
inChan := make(chan error)
go func() {
_, err := w.Write(expected)
inChan <- err
close(inChan)
}()
select {
case err := <-inChan:
c.Assert(err, check.IsNil)
case <-time.After(5 * time.Second):
c.Fatal("Timeout writing to stdout")
}
select {
case err := <-outChan:
c.Assert(err, check.IsNil)
case <-time.After(5 * time.Second):
c.Fatal("Timeout reading from stdin")
}
if !bytes.Equal(expected, actual) {
c.Fatal("Expected output to match input")
}
resp.Body.Close()
} }