github.com/LazyboyChen7/engine@v17.12.1-ce-rc2+incompatible/integration-cli/docker_api_attach_test.go (about) 1 package main 2 3 import ( 4 "bufio" 5 "bytes" 6 "context" 7 "io" 8 "net" 9 "net/http" 10 "strings" 11 "time" 12 13 "github.com/docker/docker/api/types" 14 "github.com/docker/docker/client" 15 "github.com/docker/docker/integration-cli/checker" 16 "github.com/docker/docker/integration-cli/request" 17 "github.com/docker/docker/pkg/stdcopy" 18 "github.com/go-check/check" 19 "golang.org/x/net/websocket" 20 ) 21 22 func (s *DockerSuite) TestGetContainersAttachWebsocket(c *check.C) { 23 testRequires(c, DaemonIsLinux) 24 out, _ := dockerCmd(c, "run", "-dit", "busybox", "cat") 25 26 rwc, err := request.SockConn(time.Duration(10*time.Second), daemonHost()) 27 c.Assert(err, checker.IsNil) 28 29 cleanedContainerID := strings.TrimSpace(out) 30 config, err := websocket.NewConfig( 31 "/containers/"+cleanedContainerID+"/attach/ws?stream=1&stdin=1&stdout=1&stderr=1", 32 "http://localhost", 33 ) 34 c.Assert(err, checker.IsNil) 35 36 ws, err := websocket.NewClient(config, rwc) 37 c.Assert(err, checker.IsNil) 38 defer ws.Close() 39 40 expected := []byte("hello") 41 actual := make([]byte, len(expected)) 42 43 outChan := make(chan error) 44 go func() { 45 _, err := io.ReadFull(ws, actual) 46 outChan <- err 47 close(outChan) 48 }() 49 50 inChan := make(chan error) 51 go func() { 52 _, err := ws.Write(expected) 53 inChan <- err 54 close(inChan) 55 }() 56 57 select { 58 case err := <-inChan: 59 c.Assert(err, checker.IsNil) 60 case <-time.After(5 * time.Second): 61 c.Fatal("Timeout writing to ws") 62 } 63 64 select { 65 case err := <-outChan: 66 c.Assert(err, checker.IsNil) 67 case <-time.After(5 * time.Second): 68 c.Fatal("Timeout reading from ws") 69 } 70 71 c.Assert(actual, checker.DeepEquals, expected, check.Commentf("Websocket didn't return the expected data")) 72 } 73 74 // regression gh14320 75 func (s *DockerSuite) TestPostContainersAttachContainerNotFound(c *check.C) { 76 client, err := request.NewHTTPClient(daemonHost()) 77 c.Assert(err, checker.IsNil) 78 req, err := request.New(daemonHost(), "/containers/doesnotexist/attach", request.Method(http.MethodPost)) 79 resp, err := client.Do(req) 80 // connection will shutdown, err should be "persistent connection closed" 81 c.Assert(resp.StatusCode, checker.Equals, http.StatusNotFound) 82 content, err := request.ReadBody(resp.Body) 83 c.Assert(err, checker.IsNil) 84 expected := "No such container: doesnotexist\r\n" 85 c.Assert(string(content), checker.Equals, expected) 86 } 87 88 func (s *DockerSuite) TestGetContainersWsAttachContainerNotFound(c *check.C) { 89 res, body, err := request.Get("/containers/doesnotexist/attach/ws") 90 c.Assert(res.StatusCode, checker.Equals, http.StatusNotFound) 91 c.Assert(err, checker.IsNil) 92 b, err := request.ReadBody(body) 93 c.Assert(err, checker.IsNil) 94 expected := "No such container: doesnotexist" 95 c.Assert(getErrorMessage(c, b), checker.Contains, expected) 96 } 97 98 func (s *DockerSuite) TestPostContainersAttach(c *check.C) { 99 testRequires(c, DaemonIsLinux) 100 101 expectSuccess := func(conn net.Conn, br *bufio.Reader, stream string, tty bool) { 102 defer conn.Close() 103 expected := []byte("success") 104 _, err := conn.Write(expected) 105 c.Assert(err, checker.IsNil) 106 107 conn.SetReadDeadline(time.Now().Add(time.Second)) 108 lenHeader := 0 109 if !tty { 110 lenHeader = 8 111 } 112 actual := make([]byte, len(expected)+lenHeader) 113 _, err = io.ReadFull(br, actual) 114 c.Assert(err, checker.IsNil) 115 if !tty { 116 fdMap := map[string]byte{ 117 "stdin": 0, 118 "stdout": 1, 119 "stderr": 2, 120 } 121 c.Assert(actual[0], checker.Equals, fdMap[stream]) 122 } 123 c.Assert(actual[lenHeader:], checker.DeepEquals, expected, check.Commentf("Attach didn't return the expected data from %s", stream)) 124 } 125 126 expectTimeout := func(conn net.Conn, br *bufio.Reader, stream string) { 127 defer conn.Close() 128 _, err := conn.Write([]byte{'t'}) 129 c.Assert(err, checker.IsNil) 130 131 conn.SetReadDeadline(time.Now().Add(time.Second)) 132 actual := make([]byte, 1) 133 _, err = io.ReadFull(br, actual) 134 opErr, ok := err.(*net.OpError) 135 c.Assert(ok, checker.Equals, true, check.Commentf("Error is expected to be *net.OpError, got %v", err)) 136 c.Assert(opErr.Timeout(), checker.Equals, true, check.Commentf("Read from %s is expected to timeout", stream)) 137 } 138 139 // Create a container that only emits stdout. 140 cid, _ := dockerCmd(c, "run", "-di", "busybox", "cat") 141 cid = strings.TrimSpace(cid) 142 // Attach to the container's stdout stream. 143 conn, br, err := request.SockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", daemonHost()) 144 c.Assert(err, checker.IsNil) 145 // Check if the data from stdout can be received. 146 expectSuccess(conn, br, "stdout", false) 147 // Attach to the container's stderr stream. 148 conn, br, err = request.SockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", daemonHost()) 149 c.Assert(err, checker.IsNil) 150 // Since the container only emits stdout, attaching to stderr should return nothing. 151 expectTimeout(conn, br, "stdout") 152 153 // Test the similar functions of the stderr stream. 154 cid, _ = dockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "cat >&2") 155 cid = strings.TrimSpace(cid) 156 conn, br, err = request.SockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", daemonHost()) 157 c.Assert(err, checker.IsNil) 158 expectSuccess(conn, br, "stderr", false) 159 conn, br, err = request.SockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", daemonHost()) 160 c.Assert(err, checker.IsNil) 161 expectTimeout(conn, br, "stderr") 162 163 // Test with tty. 164 cid, _ = dockerCmd(c, "run", "-dit", "busybox", "/bin/sh", "-c", "cat >&2") 165 cid = strings.TrimSpace(cid) 166 // Attach to stdout only. 167 conn, br, err = request.SockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stdout=1", nil, "text/plain", daemonHost()) 168 c.Assert(err, checker.IsNil) 169 expectSuccess(conn, br, "stdout", true) 170 171 // Attach without stdout stream. 172 conn, br, err = request.SockRequestHijack("POST", "/containers/"+cid+"/attach?stream=1&stdin=1&stderr=1", nil, "text/plain", daemonHost()) 173 c.Assert(err, checker.IsNil) 174 // Nothing should be received because both the stdout and stderr of the container will be 175 // sent to the client as stdout when tty is enabled. 176 expectTimeout(conn, br, "stdout") 177 178 // Test the client API 179 // Make sure we don't see "hello" if Logs is false 180 client, err := client.NewEnvClient() 181 c.Assert(err, checker.IsNil) 182 defer client.Close() 183 184 cid, _ = dockerCmd(c, "run", "-di", "busybox", "/bin/sh", "-c", "echo hello; cat") 185 cid = strings.TrimSpace(cid) 186 187 attachOpts := types.ContainerAttachOptions{ 188 Stream: true, 189 Stdin: true, 190 Stdout: true, 191 } 192 193 resp, err := client.ContainerAttach(context.Background(), cid, attachOpts) 194 c.Assert(err, checker.IsNil) 195 expectSuccess(resp.Conn, resp.Reader, "stdout", false) 196 197 // Make sure we do see "hello" if Logs is true 198 attachOpts.Logs = true 199 resp, err = client.ContainerAttach(context.Background(), cid, attachOpts) 200 c.Assert(err, checker.IsNil) 201 202 defer resp.Conn.Close() 203 resp.Conn.SetReadDeadline(time.Now().Add(time.Second)) 204 205 _, err = resp.Conn.Write([]byte("success")) 206 c.Assert(err, checker.IsNil) 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")) 212 }