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"
|
2017-02-28 11:12:30 -05:00
|
|
|
"io/ioutil"
|
2015-04-20 17:03:56 -04:00
|
|
|
"net/http"
|
2016-01-15 11:57:23 -05:00
|
|
|
"time"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2016-12-30 04:49:36 -05:00
|
|
|
"github.com/docker/docker/integration-cli/request"
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/pkg/testutil"
|
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
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-27 21:50:12 -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
|
|
|
|
2016-12-30 04:49:36 -05:00
|
|
|
status, body, err := request.SockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": nil}, daemonHost())
|
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")
|
2016-05-21 07:56:04 -04:00
|
|
|
c.Assert(getErrorMessage(c, body), checker.Contains, "No exec command specified", comment)
|
2014-12-01 12:16:49 -05:00
|
|
|
}
|
2015-07-02 02:57:44 -04:00
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-27 21:50:12 -04:00
|
|
|
func (s *DockerSuite) TestExecAPICreateNoValidContentType(c *check.C) {
|
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)
|
|
|
|
}
|
|
|
|
|
2017-03-06 10:35:27 -05:00
|
|
|
res, body, err := request.Post(fmt.Sprintf("/containers/%s/exec", name), request.RawContent(ioutil.NopCloser(jsonData)), request.ContentType("test/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
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
b, err := testutil.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")
|
2016-05-21 07:56:04 -04:00
|
|
|
c.Assert(getErrorMessage(c, b), checker.Contains, "Content-Type specified", comment)
|
2015-07-02 02:57:44 -04:00
|
|
|
}
|
2015-09-11 22:50:21 -04:00
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-27 21:50:12 -04: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)
|
2016-12-30 04:49:36 -05:00
|
|
|
status, body, err := request.SockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}}, daemonHost())
|
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)
|
2016-05-21 07:56:04 -04:00
|
|
|
c.Assert(getErrorMessage(c, body), checker.Contains, "Container "+name+" is paused, unpause the container before exec", comment)
|
2015-11-19 00:51:26 -05:00
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-27 21:50:12 -04:00
|
|
|
func (s *DockerSuite) TestExecAPIStart(c *check.C) {
|
2017-01-16 23:45:27 -05:00
|
|
|
testRequires(c, DaemonIsLinux) // Uses pause/unpause but bits may be salvageable 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
|
|
|
|
2016-10-17 13:49:36 -04:00
|
|
|
var execJSON struct{ PID int }
|
|
|
|
inspectExec(c, id, &execJSON)
|
|
|
|
c.Assert(execJSON.PID, checker.GreaterThan, 1)
|
|
|
|
|
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-12-05 10:58:53 -05:00
|
|
|
func (s *DockerSuite) TestExecAPIStartEnsureHeaders(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
|
|
|
|
|
|
|
|
id := createExec(c, "test")
|
2017-03-06 10:35:27 -05:00
|
|
|
resp, _, err := request.Post(fmt.Sprintf("/exec/%s/start", id), request.RawString(`{"Detach": true}`), request.JSON)
|
2016-12-05 10:58:53 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(resp.Header.Get("Server"), checker.Not(checker.Equals), "")
|
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-27 21:50:12 -04:00
|
|
|
func (s *DockerSuite) TestExecAPIStartBackwardsCompatible(c *check.C) {
|
2016-10-31 13:15:43 -04:00
|
|
|
testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later
|
2016-01-28 19:26:06 -05:00
|
|
|
runSleepingContainer(c, "-d", "--name", "test")
|
2015-10-23 12:48:55 -04:00
|
|
|
id := createExec(c, "test")
|
|
|
|
|
2017-03-06 10:35:27 -05:00
|
|
|
resp, body, err := request.Post(fmt.Sprintf("/v1.20/exec/%s/start", id), request.RawString(`{"Detach": true}`), request.ContentType("text/plain"))
|
2015-11-20 02:03:34 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-10-23 12:48:55 -04:00
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
b, err := testutil.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, http.StatusOK, comment)
|
2015-10-23 12:48:55 -04:00
|
|
|
}
|
|
|
|
|
2016-01-15 11:57:23 -05:00
|
|
|
// #19362
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-27 21:50:12 -04:00
|
|
|
func (s *DockerSuite) TestExecAPIStartMultipleTimesError(c *check.C) {
|
2016-01-28 19:26:06 -05:00
|
|
|
runSleepingContainer(c, "-d", "--name", "test")
|
2016-01-15 11:57:23 -05:00
|
|
|
execID := createExec(c, "test")
|
|
|
|
startExec(c, execID, http.StatusOK)
|
2017-01-21 06:35:54 -05:00
|
|
|
waitForExec(c, execID)
|
2016-01-15 11:57:23 -05:00
|
|
|
|
|
|
|
startExec(c, execID, http.StatusConflict)
|
|
|
|
}
|
|
|
|
|
2016-02-24 21:04:44 -05:00
|
|
|
// #20638
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-27 21:50:12 -04:00
|
|
|
func (s *DockerSuite) TestExecAPIStartWithDetach(c *check.C) {
|
2016-02-24 21:04:44 -05:00
|
|
|
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,
|
|
|
|
}
|
2016-12-30 04:49:36 -05:00
|
|
|
_, b, err := request.SockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), data, daemonHost())
|
2016-02-24 21:04:44 -05:00
|
|
|
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)))
|
|
|
|
|
2017-03-06 10:35:27 -05:00
|
|
|
_, body, err := request.Post(fmt.Sprintf("/exec/%s/start", createResp.ID), request.RawString(`{"Detach": true}`), request.JSON)
|
2016-02-24 21:04:44 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
b, err = testutil.ReadBody(body)
|
2016-02-24 21:04:44 -05:00
|
|
|
comment := check.Commentf("response body: %s", b)
|
|
|
|
c.Assert(err, checker.IsNil, comment)
|
|
|
|
|
2017-03-06 10:35:27 -05:00
|
|
|
resp, _, err := request.Get("/_ping")
|
2016-02-24 21:04:44 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
c.Fatal("daemon is down, it should alive")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 06:35:54 -05:00
|
|
|
// #30311
|
|
|
|
func (s *DockerSuite) TestExecAPIStartValidCommand(c *check.C) {
|
|
|
|
name := "exec_test"
|
|
|
|
dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
|
|
|
|
|
|
|
|
id := createExecCmd(c, name, "true")
|
|
|
|
startExec(c, id, http.StatusOK)
|
|
|
|
|
|
|
|
waitForExec(c, id)
|
|
|
|
|
|
|
|
var inspectJSON struct{ ExecIDs []string }
|
|
|
|
inspectContainer(c, name, &inspectJSON)
|
|
|
|
|
|
|
|
c.Assert(inspectJSON.ExecIDs, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// #30311
|
|
|
|
func (s *DockerSuite) TestExecAPIStartInvalidCommand(c *check.C) {
|
|
|
|
name := "exec_test"
|
|
|
|
dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
|
|
|
|
|
|
|
|
id := createExecCmd(c, name, "invalid")
|
|
|
|
startExec(c, id, http.StatusNotFound)
|
|
|
|
waitForExec(c, id)
|
|
|
|
|
|
|
|
var inspectJSON struct{ ExecIDs []string }
|
|
|
|
inspectContainer(c, name, &inspectJSON)
|
|
|
|
|
|
|
|
c.Assert(inspectJSON.ExecIDs, checker.IsNil)
|
|
|
|
}
|
|
|
|
|
2015-10-23 12:48:55 -04:00
|
|
|
func createExec(c *check.C, name string) string {
|
2017-01-21 06:35:54 -05:00
|
|
|
return createExecCmd(c, name, "true")
|
|
|
|
}
|
|
|
|
|
|
|
|
func createExecCmd(c *check.C, name string, cmd string) string {
|
2017-03-06 10:35:27 -05:00
|
|
|
_, reader, err := request.Post(fmt.Sprintf("/containers/%s/exec", name), request.JSONBody(map[string]interface{}{"Cmd": []string{cmd}}))
|
2017-02-28 11:12:30 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
b, err := ioutil.ReadAll(reader)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer reader.Close()
|
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) {
|
2017-03-06 10:35:27 -05:00
|
|
|
resp, body, err := request.Post(fmt.Sprintf("/exec/%s/start", id), request.RawString(`{"Detach": true}`), request.JSON)
|
2016-01-15 11:57:23 -05:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
b, err := testutil.ReadBody(body)
|
2016-01-15 11:57:23 -05:00
|
|
|
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{}) {
|
2017-03-06 10:35:27 -05:00
|
|
|
resp, body, err := request.Get(fmt.Sprintf("/exec/%s/json", id))
|
2016-01-15 11:57:23 -05:00
|
|
|
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)
|
|
|
|
}
|
2017-01-21 06:35:54 -05:00
|
|
|
|
|
|
|
func waitForExec(c *check.C, id string) {
|
|
|
|
timeout := time.After(60 * time.Second)
|
|
|
|
var execJSON struct{ Running bool }
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
c.Fatal("timeout waiting for exec to start")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
inspectExec(c, id, &execJSON)
|
|
|
|
if !execJSON.Running {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func inspectContainer(c *check.C, id string, out interface{}) {
|
2017-03-06 10:35:27 -05:00
|
|
|
resp, body, err := request.Get(fmt.Sprintf("/containers/%s/json", id))
|
2017-01-21 06:35:54 -05:00
|
|
|
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)
|
|
|
|
}
|