mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
refacter attach API tests to use checkers
Signed-off-by: Shijiang Wei <mountkin@gmail.com>
This commit is contained in:
parent
13364cd431
commit
4f6f46a11d
1 changed files with 26 additions and 42 deletions
|
@ -2,13 +2,13 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
@ -18,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")
|
||||||
|
@ -56,46 +50,36 @@ 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) {
|
||||||
|
@ -105,7 +89,7 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
expected := []byte("success")
|
expected := []byte("success")
|
||||||
_, err := conn.Write(expected)
|
_, err := conn.Write(expected)
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, checker.IsNil)
|
||||||
|
|
||||||
conn.SetReadDeadline(time.Now().Add(time.Second))
|
conn.SetReadDeadline(time.Now().Add(time.Second))
|
||||||
lenHeader := 0
|
lenHeader := 0
|
||||||
|
@ -114,29 +98,29 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
||||||
}
|
}
|
||||||
actual := make([]byte, len(expected)+lenHeader)
|
actual := make([]byte, len(expected)+lenHeader)
|
||||||
_, err = io.ReadFull(br, actual)
|
_, err = io.ReadFull(br, actual)
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, checker.IsNil)
|
||||||
if !tty {
|
if !tty {
|
||||||
fdMap := map[string]byte{
|
fdMap := map[string]byte{
|
||||||
"stdin": 0,
|
"stdin": 0,
|
||||||
"stdout": 1,
|
"stdout": 1,
|
||||||
"stderr": 2,
|
"stderr": 2,
|
||||||
}
|
}
|
||||||
c.Assert(actual[0], check.Equals, fdMap[stream])
|
c.Assert(actual[0], checker.Equals, fdMap[stream])
|
||||||
}
|
}
|
||||||
c.Assert(actual[lenHeader:], check.DeepEquals, expected, check.Commentf("Attach didn't return the expected data from %s", stream))
|
c.Assert(actual[lenHeader:], checker.DeepEquals, expected, check.Commentf("Attach didn't return the expected data from %s", stream))
|
||||||
}
|
}
|
||||||
|
|
||||||
expectTimeout := func(conn net.Conn, br *bufio.Reader, stream string) {
|
expectTimeout := func(conn net.Conn, br *bufio.Reader, stream string) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
_, err := conn.Write([]byte{'t'})
|
_, err := conn.Write([]byte{'t'})
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, checker.IsNil)
|
||||||
|
|
||||||
conn.SetReadDeadline(time.Now().Add(time.Second))
|
conn.SetReadDeadline(time.Now().Add(time.Second))
|
||||||
actual := make([]byte, 1)
|
actual := make([]byte, 1)
|
||||||
_, err = io.ReadFull(br, actual)
|
_, err = io.ReadFull(br, actual)
|
||||||
opErr, ok := err.(*net.OpError)
|
opErr, ok := err.(*net.OpError)
|
||||||
c.Assert(ok, check.Equals, true, check.Commentf("Error is expected to be *net.OpError, got %v", err))
|
c.Assert(ok, checker.Equals, true, check.Commentf("Error is expected to be *net.OpError, got %v", err))
|
||||||
c.Assert(opErr.Timeout(), check.Equals, true, check.Commentf("Read from %s is expected to timeout", stream))
|
c.Assert(opErr.Timeout(), checker.Equals, true, check.Commentf("Read from %s is expected to timeout", stream))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a container that only emits stdout.
|
// Create a container that only emits stdout.
|
||||||
|
@ -144,12 +128,12 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
||||||
cid = strings.TrimSpace(cid)
|
cid = strings.TrimSpace(cid)
|
||||||
// Attach to the container's stdout stream.
|
// Attach to the container's stdout stream.
|
||||||
conn, br, err := sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
|
conn, br, err := sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, checker.IsNil)
|
||||||
// Check if the data from stdout can be received.
|
// Check if the data from stdout can be received.
|
||||||
expectSuccess(conn, br, "stdout", false)
|
expectSuccess(conn, br, "stdout", false)
|
||||||
// Attach to the container's stderr stream.
|
// Attach to the container's stderr stream.
|
||||||
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
|
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, checker.IsNil)
|
||||||
// Since the container only emits stdout, attaching to stderr should return nothing.
|
// Since the container only emits stdout, attaching to stderr should return nothing.
|
||||||
expectTimeout(conn, br, "stdout")
|
expectTimeout(conn, br, "stdout")
|
||||||
|
|
||||||
|
@ -157,10 +141,10 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
||||||
cid, _ = dockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "cat >&2")
|
cid, _ = dockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "cat >&2")
|
||||||
cid = strings.TrimSpace(cid)
|
cid = strings.TrimSpace(cid)
|
||||||
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
|
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, checker.IsNil)
|
||||||
expectSuccess(conn, br, "stderr", false)
|
expectSuccess(conn, br, "stderr", false)
|
||||||
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
|
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, checker.IsNil)
|
||||||
expectTimeout(conn, br, "stderr")
|
expectTimeout(conn, br, "stderr")
|
||||||
|
|
||||||
// Test with tty.
|
// Test with tty.
|
||||||
|
@ -168,12 +152,12 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
||||||
cid = strings.TrimSpace(cid)
|
cid = strings.TrimSpace(cid)
|
||||||
// Attach to stdout only.
|
// Attach to stdout only.
|
||||||
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
|
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, checker.IsNil)
|
||||||
expectSuccess(conn, br, "stdout", true)
|
expectSuccess(conn, br, "stdout", true)
|
||||||
|
|
||||||
// Attach without stdout stream.
|
// Attach without stdout stream.
|
||||||
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
|
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
|
||||||
c.Assert(err, check.IsNil)
|
c.Assert(err, checker.IsNil)
|
||||||
// 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")
|
||||||
|
|
Loading…
Reference in a new issue