docker_api_exec_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // +build !test_no_exec
  2. package main
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "strings"
  9. "github.com/go-check/check"
  10. )
  11. // Regression test for #9414
  12. func (s *DockerSuite) TestExecApiCreateNoCmd(c *check.C) {
  13. testRequires(c, DaemonIsLinux)
  14. name := "exec_test"
  15. dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
  16. status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": nil})
  17. c.Assert(err, check.IsNil)
  18. c.Assert(status, check.Equals, http.StatusInternalServerError)
  19. if !bytes.Contains(body, []byte("No exec command specified")) {
  20. c.Fatalf("Expected message when creating exec command with no Cmd specified")
  21. }
  22. }
  23. func (s *DockerSuite) TestExecApiCreateNoValidContentType(c *check.C) {
  24. testRequires(c, DaemonIsLinux)
  25. name := "exec_test"
  26. dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
  27. jsonData := bytes.NewBuffer(nil)
  28. if err := json.NewEncoder(jsonData).Encode(map[string]interface{}{"Cmd": nil}); err != nil {
  29. c.Fatalf("Can not encode data to json %s", err)
  30. }
  31. res, body, err := sockRequestRaw("POST", fmt.Sprintf("/containers/%s/exec", name), jsonData, "text/plain")
  32. c.Assert(err, check.IsNil)
  33. c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError)
  34. b, err := readBody(body)
  35. c.Assert(err, check.IsNil)
  36. if !bytes.Contains(b, []byte("Content-Type specified")) {
  37. c.Fatalf("Expected message when creating exec command with invalid Content-Type specified")
  38. }
  39. }
  40. func (s *DockerSuite) TestExecAPIStart(c *check.C) {
  41. dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
  42. createExec := func() string {
  43. _, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", "test"), map[string]interface{}{"Cmd": []string{"true"}})
  44. c.Assert(err, check.IsNil, check.Commentf(string(b)))
  45. createResp := struct {
  46. ID string `json:"Id"`
  47. }{}
  48. c.Assert(json.Unmarshal(b, &createResp), check.IsNil, check.Commentf(string(b)))
  49. return createResp.ID
  50. }
  51. startExec := func(id string, code int) {
  52. resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json")
  53. c.Assert(err, check.IsNil)
  54. b, err := readBody(body)
  55. c.Assert(err, check.IsNil, check.Commentf(string(b)))
  56. c.Assert(resp.StatusCode, check.Equals, code, check.Commentf(string(b)))
  57. }
  58. startExec(createExec(), http.StatusOK)
  59. id := createExec()
  60. dockerCmd(c, "stop", "test")
  61. startExec(id, http.StatusNotFound)
  62. dockerCmd(c, "start", "test")
  63. startExec(id, http.StatusNotFound)
  64. // make sure exec is created before pausing
  65. id = createExec()
  66. dockerCmd(c, "pause", "test")
  67. startExec(id, http.StatusConflict)
  68. dockerCmd(c, "unpause", "test")
  69. startExec(id, http.StatusOK)
  70. }