Browse code

Fix issue with exec TTY caused by 15446

The bool logic around setting up the TTY ended up getting flipped
accidentally.
Also added a test for exec with TTY.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>

Brian Goff authored on 2015/08/15 06:55:26
Showing 2 changed files
... ...
@@ -65,7 +65,7 @@ func (s *Server) postContainerExecStart(version version.Version, w http.Response
65 65
 	}
66 66
 	var (
67 67
 		execName                  = vars["name"]
68
-		stdin, inStream           io.ReadCloser
68
+		stdin                     io.ReadCloser
69 69
 		stdout, stderr, outStream io.Writer
70 70
 	)
71 71
 
... ...
@@ -77,7 +77,7 @@ func (s *Server) postContainerExecStart(version version.Version, w http.Response
77 77
 	if !execStartCheck.Detach {
78 78
 		var err error
79 79
 		// Setting up the streaming http interface.
80
-		inStream, outStream, err = hijackServer(w)
80
+		inStream, outStream, err := hijackServer(w)
81 81
 		if err != nil {
82 82
 			return err
83 83
 		}
... ...
@@ -89,11 +89,12 @@ func (s *Server) postContainerExecStart(version version.Version, w http.Response
89 89
 			fmt.Fprintf(outStream, "HTTP/1.1 200 OK\r\nContent-Type: application/vnd.docker.raw-stream\r\n\r\n")
90 90
 		}
91 91
 
92
+		stdin = inStream
93
+		stdout = outStream
92 94
 		if !execStartCheck.Tty {
93 95
 			stderr = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
94 96
 			stdout = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
95 97
 		}
96
-		stdin = inStream
97 98
 	}
98 99
 
99 100
 	// Now run the user process in container.
... ...
@@ -42,3 +42,31 @@ func (s *DockerSuite) TestExecInteractiveStdinClose(c *check.C) {
42 42
 		c.Fatal("timed out running docker exec")
43 43
 	}
44 44
 }
45
+
46
+func (s *DockerSuite) TestExecTTY(c *check.C) {
47
+	dockerCmd(c, "run", "-d", "--name=test", "busybox", "sh", "-c", "echo hello > /foo && top")
48
+
49
+	cmd := exec.Command(dockerBinary, "exec", "-it", "test", "sh")
50
+	p, err := pty.Start(cmd)
51
+	c.Assert(err, check.IsNil)
52
+	defer p.Close()
53
+
54
+	_, err = p.Write([]byte("cat /foo && exit\n"))
55
+	c.Assert(err, check.IsNil)
56
+
57
+	chErr := make(chan error)
58
+	go func() {
59
+		chErr <- cmd.Wait()
60
+	}()
61
+	select {
62
+	case err := <-chErr:
63
+		c.Assert(err, check.IsNil)
64
+	case <-time.After(3 * time.Second):
65
+		c.Fatal("timeout waiting for exec to exit")
66
+	}
67
+
68
+	buf := make([]byte, 256)
69
+	read, err := p.Read(buf)
70
+	c.Assert(err, check.IsNil)
71
+	c.Assert(bytes.Contains(buf, []byte("hello")), check.Equals, true, check.Commentf(string(buf[:read])))
72
+}