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"
|
2016-01-15 11:57:23 -05:00
|
|
|
"time"
|
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) {
|
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) {
|
|
|
|
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) {
|
2016-01-28 19:26:06 -05:00
|
|
|
// Not relevant on Windows as Windows containers cannot be paused
|
2015-11-19 00:51:26 -05:00
|
|
|
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
|
|
|
|
2016-02-17 14:05:51 -05:00
|
|
|
comment := check.Commentf("Expected message when creating exec command with Container %s is paused", name)
|
2015-11-20 02:03:34 -05:00
|
|
|
c.Assert(string(body), checker.Contains, "Container "+name+" is paused, unpause the container before exec", comment)
|
2015-11-19 00:51:26 -05:00
|
|
|
}
|
|
|
|
|
2016-01-28 19:26:06 -05:00
|
|
|
func (s *DockerSuite) TestExecApiStart(c *check.C) {
|
2016-01-08 16:49:43 -05:00
|
|
|
testRequires(c, DaemonIsLinux) // Uses pause/unpause but bits may be salvagable to Windows to Windows CI
|
2015-09-11 22:50:21 -04:00
|
|
|
dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
|
|
|
|
|
2015-10-23 12:48:55 -04:00
|
|
|
id := createExec(c, "test")
|
2016-01-15 11:57:23 -05:00
|
|
|
startExec(c, 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")
|
|
|
|
|
2016-01-15 11:57:23 -05:00
|
|
|
startExec(c, id, http.StatusNotFound)
|
2015-09-11 22:50:21 -04:00
|
|
|
|
|
|
|
dockerCmd(c, "start", "test")
|
2016-01-15 11:57:23 -05:00
|
|
|
startExec(c, id, http.StatusNotFound)
|
2015-09-11 22:50:21 -04:00
|
|
|
|
|
|
|
// 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")
|
2016-01-15 11:57:23 -05:00
|
|
|
startExec(c, id, http.StatusConflict)
|
2015-09-11 22:50:21 -04:00
|
|
|
dockerCmd(c, "unpause", "test")
|
2016-01-15 11:57:23 -05:00
|
|
|
startExec(c, id, http.StatusOK)
|
2015-09-11 22:50:21 -04:00
|
|
|
}
|
2015-10-23 12:48:55 -04:00
|
|
|
|
2016-01-28 19:26:06 -05:00
|
|
|
func (s *DockerSuite) TestExecApiStartBackwardsCompatible(c *check.C) {
|
|
|
|
runSleepingContainer(c, "-d", "--name", "test")
|
2015-10-23 12:48:55 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-15 11:57:23 -05:00
|
|
|
// #19362
|
2016-01-28 19:26:06 -05:00
|
|
|
func (s *DockerSuite) TestExecApiStartMultipleTimesError(c *check.C) {
|
|
|
|
runSleepingContainer(c, "-d", "--name", "test")
|
2016-01-15 11:57:23 -05:00
|
|
|
execID := createExec(c, "test")
|
|
|
|
startExec(c, execID, http.StatusOK)
|
|
|
|
|
2016-01-28 19:26:06 -05:00
|
|
|
timeout := time.After(60 * time.Second)
|
2016-01-15 11:57:23 -05:00
|
|
|
var execJSON struct{ Running bool }
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
c.Fatal("timeout waiting for exec to start")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
inspectExec(c, execID, &execJSON)
|
|
|
|
if !execJSON.Running {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startExec(c, execID, http.StatusConflict)
|
|
|
|
}
|
|
|
|
|
2016-02-24 21:04:44 -05:00
|
|
|
// #20638
|
|
|
|
func (s *DockerSuite) TestExecApiStartWithDetach(c *check.C) {
|
|
|
|
name := "foo"
|
2016-02-25 08:27:22 -05:00
|
|
|
runSleepingContainer(c, "-d", "-t", "--name", name)
|
2016-02-24 21:04:44 -05:00
|
|
|
data := map[string]interface{}{
|
|
|
|
"cmd": []string{"true"},
|
|
|
|
"AttachStdin": true,
|
|
|
|
}
|
|
|
|
_, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), data)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf(string(b)))
|
|
|
|
|
|
|
|
createResp := struct {
|
|
|
|
ID string `json:"Id"`
|
|
|
|
}{}
|
|
|
|
c.Assert(json.Unmarshal(b, &createResp), checker.IsNil, check.Commentf(string(b)))
|
|
|
|
|
|
|
|
_, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", createResp.ID), strings.NewReader(`{"Detach": true}`), "application/json")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
b, err = readBody(body)
|
|
|
|
comment := check.Commentf("response body: %s", b)
|
|
|
|
c.Assert(err, checker.IsNil, comment)
|
|
|
|
|
|
|
|
resp, _, err := sockRequestRaw("GET", "/_ping", nil, "")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
c.Fatal("daemon is down, it should alive")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2016-01-15 11:57:23 -05:00
|
|
|
|
|
|
|
func startExec(c *check.C, id string, code int) {
|
|
|
|
resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
b, err := readBody(body)
|
|
|
|
comment := check.Commentf("response body: %s", b)
|
|
|
|
c.Assert(err, checker.IsNil, comment)
|
|
|
|
c.Assert(resp.StatusCode, checker.Equals, code, comment)
|
|
|
|
}
|
|
|
|
|
|
|
|
func inspectExec(c *check.C, id string, out interface{}) {
|
|
|
|
resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/exec/%s/json", id), nil, "")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer body.Close()
|
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK)
|
|
|
|
err = json.NewDecoder(body).Decode(out)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
}
|