Browse code

Fix exec start api with detach and AttachStdin at same time. fixes #20638

Signed-off-by: Lei Jitang <leijitang@huawei.com>

Lei Jitang authored on 2016/02/25 11:04:44
Showing 2 changed files
... ...
@@ -157,7 +157,7 @@ func (d *Daemon) ContainerExecStart(name string, stdin io.ReadCloser, stdout io.
157 157
 	logrus.Debugf("starting exec command %s in container %s", ec.ID, c.ID)
158 158
 	d.LogContainerEvent(c, "exec_start: "+ec.ProcessConfig.Entrypoint+" "+strings.Join(ec.ProcessConfig.Arguments, " "))
159 159
 
160
-	if ec.OpenStdin {
160
+	if ec.OpenStdin && stdin != nil {
161 161
 		r, w := io.Pipe()
162 162
 		go func() {
163 163
 			defer w.Close()
... ...
@@ -122,6 +122,36 @@ func (s *DockerSuite) TestExecApiStartMultipleTimesError(c *check.C) {
122 122
 	startExec(c, execID, http.StatusConflict)
123 123
 }
124 124
 
125
+// #20638
126
+func (s *DockerSuite) TestExecApiStartWithDetach(c *check.C) {
127
+	name := "foo"
128
+	dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "top")
129
+	data := map[string]interface{}{
130
+		"cmd":         []string{"true"},
131
+		"AttachStdin": true,
132
+	}
133
+	_, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), data)
134
+	c.Assert(err, checker.IsNil, check.Commentf(string(b)))
135
+
136
+	createResp := struct {
137
+		ID string `json:"Id"`
138
+	}{}
139
+	c.Assert(json.Unmarshal(b, &createResp), checker.IsNil, check.Commentf(string(b)))
140
+
141
+	_, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", createResp.ID), strings.NewReader(`{"Detach": true}`), "application/json")
142
+	c.Assert(err, checker.IsNil)
143
+
144
+	b, err = readBody(body)
145
+	comment := check.Commentf("response body: %s", b)
146
+	c.Assert(err, checker.IsNil, comment)
147
+
148
+	resp, _, err := sockRequestRaw("GET", "/_ping", nil, "")
149
+	c.Assert(err, checker.IsNil)
150
+	if resp.StatusCode != http.StatusOK {
151
+		c.Fatal("daemon is down, it should alive")
152
+	}
153
+}
154
+
125 155
 func createExec(c *check.C, name string) string {
126 156
 	_, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}})
127 157
 	c.Assert(err, checker.IsNil, check.Commentf(string(b)))