2014-12-31 01:22:31 +03:00
|
|
|
// +build !test_no_exec
|
|
|
|
|
2014-12-01 20:16:49 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-07-02 09:57:44 +03:00
|
|
|
"encoding/json"
|
2014-12-01 20:16:49 +03:00
|
|
|
"fmt"
|
2015-04-21 00:03:56 +03:00
|
|
|
"net/http"
|
2015-09-12 05:50:21 +03:00
|
|
|
"strings"
|
2015-04-18 19:46:47 +03:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2014-12-01 20:16:49 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Regression test for #9414
|
2015-04-18 19:46:47 +03:00
|
|
|
func (s *DockerSuite) TestExecApiCreateNoCmd(c *check.C) {
|
2015-08-28 20:36:42 +03:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-12-01 20:16:49 +03:00
|
|
|
name := "exec_test"
|
2015-07-14 09:35:36 +03:00
|
|
|
dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
|
2014-12-01 20:16:49 +03:00
|
|
|
|
2015-04-21 00:03:56 +03:00
|
|
|
status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": nil})
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-07-23 14:24:14 +03:00
|
|
|
c.Assert(status, check.Equals, http.StatusInternalServerError)
|
2015-04-21 00:03:56 +03:00
|
|
|
|
|
|
|
if !bytes.Contains(body, []byte("No exec command specified")) {
|
|
|
|
c.Fatalf("Expected message when creating exec command with no Cmd specified")
|
2014-12-01 20:16:49 +03:00
|
|
|
}
|
|
|
|
}
|
2015-07-02 09:57:44 +03:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestExecApiCreateNoValidContentType(c *check.C) {
|
2015-08-28 20:36:42 +03:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-02 09:57:44 +03: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")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
|
|
|
|
|
|
|
|
b, err := readBody(body)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
if !bytes.Contains(b, []byte("Content-Type specified")) {
|
|
|
|
c.Fatalf("Expected message when creating exec command with invalid Content-Type specified")
|
|
|
|
}
|
|
|
|
}
|
2015-09-12 05:50:21 +03: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")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
b, err := readBody(body)
|
2015-10-23 19:48:55 +03:00
|
|
|
comment := check.Commentf("response body: %s", b)
|
|
|
|
c.Assert(err, check.IsNil, comment)
|
|
|
|
c.Assert(resp.StatusCode, check.Equals, code, comment)
|
2015-09-12 05:50:21 +03:00
|
|
|
}
|
|
|
|
|
2015-10-23 19:48:55 +03:00
|
|
|
id := createExec(c, "test")
|
|
|
|
startExec(id, http.StatusOK)
|
2015-09-12 05:50:21 +03:00
|
|
|
|
2015-10-23 19:48:55 +03:00
|
|
|
id = createExec(c, "test")
|
2015-09-12 05:50:21 +03: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 19:48:55 +03:00
|
|
|
id = createExec(c, "test")
|
2015-09-12 05:50:21 +03:00
|
|
|
dockerCmd(c, "pause", "test")
|
|
|
|
startExec(id, http.StatusConflict)
|
|
|
|
dockerCmd(c, "unpause", "test")
|
|
|
|
startExec(id, http.StatusOK)
|
|
|
|
}
|
2015-10-23 19:48:55 +03: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")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
b, err := readBody(body)
|
|
|
|
comment := check.Commentf("response body: %s", b)
|
|
|
|
c.Assert(err, check.IsNil, comment)
|
|
|
|
c.Assert(resp.StatusCode, check.Equals, http.StatusOK, comment)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createExec(c *check.C, name string) string {
|
|
|
|
_, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}})
|
|
|
|
c.Assert(err, check.IsNil, check.Commentf(string(b)))
|
|
|
|
|
|
|
|
createResp := struct {
|
|
|
|
ID string `json:"Id"`
|
|
|
|
}{}
|
|
|
|
c.Assert(json.Unmarshal(b, &createResp), check.IsNil, check.Commentf(string(b)))
|
|
|
|
return createResp.ID
|
|
|
|
}
|