Signed-off-by: Shijiang Wei <mountkin@gmail.com>
| ... | ... |
@@ -2,13 +2,13 @@ package main |
| 2 | 2 |
|
| 3 | 3 |
import ( |
| 4 | 4 |
"bufio" |
| 5 |
- "bytes" |
|
| 6 | 5 |
"io" |
| 7 | 6 |
"net" |
| 8 | 7 |
"net/http" |
| 9 | 8 |
"strings" |
| 10 | 9 |
"time" |
| 11 | 10 |
|
| 11 |
+ "github.com/docker/docker/pkg/integration/checker" |
|
| 12 | 12 |
"github.com/go-check/check" |
| 13 | 13 |
"golang.org/x/net/websocket" |
| 14 | 14 |
) |
| ... | ... |
@@ -18,23 +18,17 @@ func (s *DockerSuite) TestGetContainersAttachWebsocket(c *check.C) {
|
| 18 | 18 |
out, _ := dockerCmd(c, "run", "-dit", "busybox", "cat") |
| 19 | 19 |
|
| 20 | 20 |
rwc, err := sockConn(time.Duration(10 * time.Second)) |
| 21 |
- if err != nil {
|
|
| 22 |
- c.Fatal(err) |
|
| 23 |
- } |
|
| 21 |
+ c.Assert(err, checker.IsNil) |
|
| 24 | 22 |
|
| 25 | 23 |
cleanedContainerID := strings.TrimSpace(out) |
| 26 | 24 |
config, err := websocket.NewConfig( |
| 27 | 25 |
"/containers/"+cleanedContainerID+"/attach/ws?stream=1&stdin=1&stdout=1&stderr=1", |
| 28 | 26 |
"http://localhost", |
| 29 | 27 |
) |
| 30 |
- if err != nil {
|
|
| 31 |
- c.Fatal(err) |
|
| 32 |
- } |
|
| 28 |
+ c.Assert(err, checker.IsNil) |
|
| 33 | 29 |
|
| 34 | 30 |
ws, err := websocket.NewClient(config, rwc) |
| 35 |
- if err != nil {
|
|
| 36 |
- c.Fatal(err) |
|
| 37 |
- } |
|
| 31 |
+ c.Assert(err, checker.IsNil) |
|
| 38 | 32 |
defer ws.Close() |
| 39 | 33 |
|
| 40 | 34 |
expected := []byte("hello")
|
| ... | ... |
@@ -56,46 +50,36 @@ func (s *DockerSuite) TestGetContainersAttachWebsocket(c *check.C) {
|
| 56 | 56 |
|
| 57 | 57 |
select {
|
| 58 | 58 |
case err := <-inChan: |
| 59 |
- if err != nil {
|
|
| 60 |
- c.Fatal(err) |
|
| 61 |
- } |
|
| 59 |
+ c.Assert(err, checker.IsNil) |
|
| 62 | 60 |
case <-time.After(5 * time.Second): |
| 63 | 61 |
c.Fatal("Timeout writing to ws")
|
| 64 | 62 |
} |
| 65 | 63 |
|
| 66 | 64 |
select {
|
| 67 | 65 |
case err := <-outChan: |
| 68 |
- if err != nil {
|
|
| 69 |
- c.Fatal(err) |
|
| 70 |
- } |
|
| 66 |
+ c.Assert(err, checker.IsNil) |
|
| 71 | 67 |
case <-time.After(5 * time.Second): |
| 72 | 68 |
c.Fatal("Timeout reading from ws")
|
| 73 | 69 |
} |
| 74 | 70 |
|
| 75 |
- if !bytes.Equal(expected, actual) {
|
|
| 76 |
- c.Fatal("Expected output on websocket to match input")
|
|
| 77 |
- } |
|
| 71 |
+ c.Assert(actual, checker.DeepEquals, expected, check.Commentf("Websocket didn't return the expected data"))
|
|
| 78 | 72 |
} |
| 79 | 73 |
|
| 80 | 74 |
// regression gh14320 |
| 81 | 75 |
func (s *DockerSuite) TestPostContainersAttachContainerNotFound(c *check.C) {
|
| 82 | 76 |
status, body, err := sockRequest("POST", "/containers/doesnotexist/attach", nil)
|
| 83 |
- c.Assert(status, check.Equals, http.StatusNotFound) |
|
| 84 |
- c.Assert(err, check.IsNil) |
|
| 77 |
+ c.Assert(status, checker.Equals, http.StatusNotFound) |
|
| 78 |
+ c.Assert(err, checker.IsNil) |
|
| 85 | 79 |
expected := "no such id: doesnotexist\n" |
| 86 |
- if !strings.Contains(string(body), expected) {
|
|
| 87 |
- c.Fatalf("Expected response body to contain %q", expected)
|
|
| 88 |
- } |
|
| 80 |
+ c.Assert(string(body), checker.Contains, expected) |
|
| 89 | 81 |
} |
| 90 | 82 |
|
| 91 | 83 |
func (s *DockerSuite) TestGetContainersWsAttachContainerNotFound(c *check.C) {
|
| 92 | 84 |
status, body, err := sockRequest("GET", "/containers/doesnotexist/attach/ws", nil)
|
| 93 |
- c.Assert(status, check.Equals, http.StatusNotFound) |
|
| 94 |
- c.Assert(err, check.IsNil) |
|
| 85 |
+ c.Assert(status, checker.Equals, http.StatusNotFound) |
|
| 86 |
+ c.Assert(err, checker.IsNil) |
|
| 95 | 87 |
expected := "no such id: doesnotexist\n" |
| 96 |
- if !strings.Contains(string(body), expected) {
|
|
| 97 |
- c.Fatalf("Expected response body to contain %q", expected)
|
|
| 98 |
- } |
|
| 88 |
+ c.Assert(string(body), checker.Contains, expected) |
|
| 99 | 89 |
} |
| 100 | 90 |
|
| 101 | 91 |
func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
| ... | ... |
@@ -105,7 +89,7 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
| 105 | 105 |
defer conn.Close() |
| 106 | 106 |
expected := []byte("success")
|
| 107 | 107 |
_, err := conn.Write(expected) |
| 108 |
- c.Assert(err, check.IsNil) |
|
| 108 |
+ c.Assert(err, checker.IsNil) |
|
| 109 | 109 |
|
| 110 | 110 |
conn.SetReadDeadline(time.Now().Add(time.Second)) |
| 111 | 111 |
lenHeader := 0 |
| ... | ... |
@@ -114,29 +98,29 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
| 114 | 114 |
} |
| 115 | 115 |
actual := make([]byte, len(expected)+lenHeader) |
| 116 | 116 |
_, err = io.ReadFull(br, actual) |
| 117 |
- c.Assert(err, check.IsNil) |
|
| 117 |
+ c.Assert(err, checker.IsNil) |
|
| 118 | 118 |
if !tty {
|
| 119 | 119 |
fdMap := map[string]byte{
|
| 120 | 120 |
"stdin": 0, |
| 121 | 121 |
"stdout": 1, |
| 122 | 122 |
"stderr": 2, |
| 123 | 123 |
} |
| 124 |
- c.Assert(actual[0], check.Equals, fdMap[stream]) |
|
| 124 |
+ c.Assert(actual[0], checker.Equals, fdMap[stream]) |
|
| 125 | 125 |
} |
| 126 |
- c.Assert(actual[lenHeader:], check.DeepEquals, expected, check.Commentf("Attach didn't return the expected data from %s", stream))
|
|
| 126 |
+ c.Assert(actual[lenHeader:], checker.DeepEquals, expected, check.Commentf("Attach didn't return the expected data from %s", stream))
|
|
| 127 | 127 |
} |
| 128 | 128 |
|
| 129 | 129 |
expectTimeout := func(conn net.Conn, br *bufio.Reader, stream string) {
|
| 130 | 130 |
defer conn.Close() |
| 131 | 131 |
_, err := conn.Write([]byte{'t'})
|
| 132 |
- c.Assert(err, check.IsNil) |
|
| 132 |
+ c.Assert(err, checker.IsNil) |
|
| 133 | 133 |
|
| 134 | 134 |
conn.SetReadDeadline(time.Now().Add(time.Second)) |
| 135 | 135 |
actual := make([]byte, 1) |
| 136 | 136 |
_, err = io.ReadFull(br, actual) |
| 137 | 137 |
opErr, ok := err.(*net.OpError) |
| 138 |
- c.Assert(ok, check.Equals, true, check.Commentf("Error is expected to be *net.OpError, got %v", err))
|
|
| 139 |
- c.Assert(opErr.Timeout(), check.Equals, true, check.Commentf("Read from %s is expected to timeout", stream))
|
|
| 138 |
+ c.Assert(ok, checker.Equals, true, check.Commentf("Error is expected to be *net.OpError, got %v", err))
|
|
| 139 |
+ c.Assert(opErr.Timeout(), checker.Equals, true, check.Commentf("Read from %s is expected to timeout", stream))
|
|
| 140 | 140 |
} |
| 141 | 141 |
|
| 142 | 142 |
// Create a container that only emits stdout. |
| ... | ... |
@@ -144,12 +128,12 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
| 144 | 144 |
cid = strings.TrimSpace(cid) |
| 145 | 145 |
// Attach to the container's stdout stream. |
| 146 | 146 |
conn, br, err := sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
|
| 147 |
- c.Assert(err, check.IsNil) |
|
| 147 |
+ c.Assert(err, checker.IsNil) |
|
| 148 | 148 |
// Check if the data from stdout can be received. |
| 149 | 149 |
expectSuccess(conn, br, "stdout", false) |
| 150 | 150 |
// Attach to the container's stderr stream. |
| 151 | 151 |
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
|
| 152 |
- c.Assert(err, check.IsNil) |
|
| 152 |
+ c.Assert(err, checker.IsNil) |
|
| 153 | 153 |
// Since the container only emits stdout, attaching to stderr should return nothing. |
| 154 | 154 |
expectTimeout(conn, br, "stdout") |
| 155 | 155 |
|
| ... | ... |
@@ -157,10 +141,10 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
| 157 | 157 |
cid, _ = dockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "cat >&2") |
| 158 | 158 |
cid = strings.TrimSpace(cid) |
| 159 | 159 |
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
|
| 160 |
- c.Assert(err, check.IsNil) |
|
| 160 |
+ c.Assert(err, checker.IsNil) |
|
| 161 | 161 |
expectSuccess(conn, br, "stderr", false) |
| 162 | 162 |
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
|
| 163 |
- c.Assert(err, check.IsNil) |
|
| 163 |
+ c.Assert(err, checker.IsNil) |
|
| 164 | 164 |
expectTimeout(conn, br, "stderr") |
| 165 | 165 |
|
| 166 | 166 |
// Test with tty. |
| ... | ... |
@@ -168,12 +152,12 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
|
| 168 | 168 |
cid = strings.TrimSpace(cid) |
| 169 | 169 |
// Attach to stdout only. |
| 170 | 170 |
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain")
|
| 171 |
- c.Assert(err, check.IsNil) |
|
| 171 |
+ c.Assert(err, checker.IsNil) |
|
| 172 | 172 |
expectSuccess(conn, br, "stdout", true) |
| 173 | 173 |
|
| 174 | 174 |
// Attach without stdout stream. |
| 175 | 175 |
conn, br, err = sockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain")
|
| 176 |
- c.Assert(err, check.IsNil) |
|
| 176 |
+ c.Assert(err, checker.IsNil) |
|
| 177 | 177 |
// Nothing should be received because both the stdout and stderr of the container will be |
| 178 | 178 |
// sent to the client as stdout when tty is enabled. |
| 179 | 179 |
expectTimeout(conn, br, "stdout") |