Browse code

TestPostContainerAttach: minor improvements

When this test fails, the error looks like this:

> FAIL: docker_api_attach_test.go:98: DockerSuite.TestPostContainersAttach
> docker_api_attach_test.go:211:
> c.Assert(actualStdout.Bytes(), checker.DeepEquals, []byte("hello\nsuccess"), check.Commentf("Attach didn't return the expected data from stdout"))
> ... obtained []uint8 = []byte{0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73}
> ... expected []uint8 = []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xa, 0x73, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73}
> ... Attach didn't return the expected data from stdout

Let's use strings for comparisons to make the output more readable.

While at it,
- get the container's stderr as well, and make sure it's empty;
- check that stdcopy.StdCopy() did not return an error, except for
the timeout which is expected;
- move/remove comments, simplify var names.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>

Kir Kolyshkin authored on 2018/03/16 12:07:26
Showing 1 changed files
... ...
@@ -18,6 +18,7 @@ import (
18 18
 	"github.com/docker/docker/integration-cli/request"
19 19
 	"github.com/docker/docker/pkg/stdcopy"
20 20
 	"github.com/go-check/check"
21
+	"github.com/pkg/errors"
21 22
 	"golang.org/x/net/websocket"
22 23
 )
23 24
 
... ...
@@ -176,7 +177,6 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
176 176
 	expectTimeout(conn, br, "stdout")
177 177
 
178 178
 	// Test the client API
179
-	// Make sure we don't see "hello" if Logs is false
180 179
 	client, err := client.NewEnvClient()
181 180
 	c.Assert(err, checker.IsNil)
182 181
 	defer client.Close()
... ...
@@ -184,10 +184,13 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
184 184
 	cid, _ = dockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "echo hello; cat")
185 185
 	cid = strings.TrimSpace(cid)
186 186
 
187
+	// Make sure we don't see "hello" if Logs is false
187 188
 	attachOpts := types.ContainerAttachOptions{
188 189
 		Stream: true,
189 190
 		Stdin:  true,
190 191
 		Stdout: true,
192
+		Stderr: true,
193
+		Logs:   false,
191 194
 	}
192 195
 
193 196
 	resp, err := client.ContainerAttach(context.Background(), cid, attachOpts)
... ...
@@ -205,10 +208,15 @@ func (s *DockerSuite) TestPostContainersAttach(c *check.C) {
205 205
 	_, err = resp.Conn.Write([]byte("success"))
206 206
 	c.Assert(err, checker.IsNil)
207 207
 
208
-	actualStdout := new(bytes.Buffer)
209
-	actualStderr := new(bytes.Buffer)
210
-	stdcopy.StdCopy(actualStdout, actualStderr, resp.Reader)
211
-	c.Assert(actualStdout.Bytes(), checker.DeepEquals, []byte("hello\nsuccess"), check.Commentf("Attach didn't return the expected data from stdout"))
208
+	var outBuf, errBuf bytes.Buffer
209
+	_, err = stdcopy.StdCopy(&outBuf, &errBuf, resp.Reader)
210
+	if err != nil && errors.Cause(err).(net.Error).Timeout() {
211
+		// ignore the timeout error as it is expected
212
+		err = nil
213
+	}
214
+	c.Assert(err, checker.IsNil)
215
+	c.Assert(errBuf.String(), checker.Equals, "")
216
+	c.Assert(outBuf.String(), checker.Equals, "hello\nsuccess")
212 217
 }
213 218
 
214 219
 // SockRequestHijack creates a connection to specified host (with method, contenttype, …) and returns a hijacked connection