2014-12-30 17:22:31 -05:00
|
|
|
// +build !test_no_exec
|
|
|
|
|
2014-12-01 12:16:49 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-07-02 02:57:44 -04:00
|
|
|
"encoding/json"
|
2014-12-01 12:16:49 -05:00
|
|
|
"fmt"
|
2015-04-20 17:03:56 -04:00
|
|
|
"net/http"
|
2015-09-11 22:50:21 -04:00
|
|
|
"strings"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2015-11-20 02:03:34 -05:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-04-18 12:46:47 -04:00
|
|
|
"github.com/go-check/check"
|
2014-12-01 12:16:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Regression test for #9414
|
2015-04-18 12:46:47 -04:00
|
|
|
func (s *DockerSuite) TestExecApiCreateNoCmd(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-12-01 12:16:49 -05:00
|
|
|
name := "exec_test"
|
2015-07-14 02:35:36 -04:00
|
|
|
dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
|
2014-12-01 12:16:49 -05:00
|
|
|
|
2015-04-20 17:03:56 -04:00
|
|
|
status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": nil})
|
2015-11-20 02:03:34 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(status, checker.Equals, http.StatusInternalServerError)
|
2015-04-20 17:03:56 -04:00
|
|
|
|
2015-11-20 02:03:34 -05:00
|
|
|
comment := check.Commentf("Expected message when creating exec command with no Cmd specified")
|
|
|
|
c.Assert(string(body), checker.Contains, "No exec command specified", comment)
|
2014-12-01 12:16:49 -05:00
|
|
|
}
|
2015-07-02 02:57:44 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestExecApiCreateNoValidContentType(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-02 02:57:44 -04:00
|
|
|
name := "exec_test"
|
|
|
|
dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
|
|
|
|
|
|
|
|
jsonData := bytes.NewBuffer(nil)
|
|
|
|
if err := json.NewEncoder(jsonData).Encode(map[string]interface{}{"Cmd": nil}); err != nil {
|
|
|
|
c.Fatalf("Can not encode data to json %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, body, err := sockRequestRaw("POST", fmt.Sprintf("/containers/%s/exec", name), jsonData, "text/plain")
|
2015-11-20 02:03:34 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError)
|
2015-07-02 02:57:44 -04:00
|
|
|
|
|
|
|
b, err := readBody(body)
|
2015-11-20 02:03:34 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-07-02 02:57:44 -04:00
|
|
|
|
2015-11-20 02:03:34 -05:00
|
|
|
comment := check.Commentf("Expected message when creating exec command with invalid Content-Type specified")
|
|
|
|
c.Assert(string(b), checker.Contains, "Content-Type specified", comment)
|
2015-07-02 02:57:44 -04:00
|
|
|
}
|
2015-09-11 22:50:21 -04:00
|
|
|
|
2015-11-19 00:51:26 -05:00
|
|
|
func (s *DockerSuite) TestExecApiCreateContainerPaused(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
name := "exec_create_test"
|
|
|
|
dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
|
|
|
|
|
|
|
|
dockerCmd(c, "pause", name)
|
|
|
|
status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}})
|
2015-11-20 02:03:34 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(status, checker.Equals, http.StatusConflict)
|
2015-11-19 00:51:26 -05:00
|
|
|
|
2015-11-20 02:03:34 -05:00
|
|
|
comment := check.Commentf("Expected message when creating exec command with Container s% is paused", name)
|
|
|
|
c.Assert(string(body), checker.Contains, "Container "+name+" is paused, unpause the container before exec", comment)
|
2015-11-19 00:51:26 -05:00
|
|
|
}
|
|
|
|
|
2015-09-11 22:50:21 -04:00
|
|
|
func (s *DockerSuite) TestExecAPIStart(c *check.C) {
|
|
|
|
dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
|
|
|
|
|
|
|
|
startExec := func(id string, code int) {
|
|
|
|
resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json")
|
2015-11-20 02:03:34 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-09-11 22:50:21 -04:00
|
|
|
|
|
|
|
b, err := readBody(body)
|
2015-10-23 12:48:55 -04:00
|
|
|
comment := check.Commentf("response body: %s", b)
|
2015-11-20 02:03:34 -05:00
|
|
|
c.Assert(err, checker.IsNil, comment)
|
|
|
|
c.Assert(resp.StatusCode, checker.Equals, code, comment)
|
2015-09-11 22:50:21 -04:00
|
|
|
}
|
|
|
|
|
2015-10-23 12:48:55 -04:00
|
|
|
id := createExec(c, "test")
|
|
|
|
startExec(id, http.StatusOK)
|
2015-09-11 22:50:21 -04:00
|
|
|
|
2015-10-23 12:48:55 -04:00
|
|
|
id = createExec(c, "test")
|
2015-09-11 22:50:21 -04:00
|
|
|
dockerCmd(c, "stop", "test")
|
|
|
|
|
|
|
|
startExec(id, http.StatusNotFound)
|
|
|
|
|
|
|
|
dockerCmd(c, "start", "test")
|
|
|
|
startExec(id, http.StatusNotFound)
|
|
|
|
|
|
|
|
// make sure exec is created before pausing
|
2015-10-23 12:48:55 -04:00
|
|
|
id = createExec(c, "test")
|
2015-09-11 22:50:21 -04:00
|
|
|
dockerCmd(c, "pause", "test")
|
|
|
|
startExec(id, http.StatusConflict)
|
|
|
|
dockerCmd(c, "unpause", "test")
|
|
|
|
startExec(id, http.StatusOK)
|
|
|
|
}
|
2015-10-23 12:48:55 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestExecAPIStartBackwardsCompatible(c *check.C) {
|
|
|
|
dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
|
|
|
|
id := createExec(c, "test")
|
|
|
|
|
|
|
|
resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/v1.20/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "text/plain")
|
2015-11-20 02:03:34 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-23 12:48:55 -04:00
|
|
|
|
|
|
|
b, err := readBody(body)
|
|
|
|
comment := check.Commentf("response body: %s", b)
|
2015-11-20 02:03:34 -05:00
|
|
|
c.Assert(err, checker.IsNil, comment)
|
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK, comment)
|
2015-10-23 12:48:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func createExec(c *check.C, name string) string {
|
|
|
|
_, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}})
|
2015-11-20 02:03:34 -05:00
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(b)))
|
2015-10-23 12:48:55 -04:00
|
|
|
|
|
|
|
createResp := struct {
|
|
|
|
ID string `json:"Id"`
|
|
|
|
}{}
|
2015-11-20 02:03:34 -05:00
|
|
|
c.Assert(json.Unmarshal(b, &createResp), checker.IsNil, check.Commentf(string(b)))
|
2015-10-23 12:48:55 -04:00
|
|
|
return createResp.ID
|
|
|
|
}
|