docker_api_exec_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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) TestExecApiCreateContainerPaused(c *check.C) {
  41. testRequires(c, DaemonIsLinux)
  42. name := "exec_create_test"
  43. dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
  44. dockerCmd(c, "pause", name)
  45. status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}})
  46. c.Assert(err, check.IsNil)
  47. c.Assert(status, check.Equals, http.StatusConflict)
  48. if !bytes.Contains(body, []byte("Container "+name+" is paused, unpause the container before exec")) {
  49. c.Fatalf("Expected message when creating exec command with Container %s is paused", name)
  50. }
  51. }
  52. func (s *DockerSuite) TestExecAPIStart(c *check.C) {
  53. dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
  54. startExec := func(id string, code int) {
  55. resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json")
  56. c.Assert(err, check.IsNil)
  57. b, err := readBody(body)
  58. comment := check.Commentf("response body: %s", b)
  59. c.Assert(err, check.IsNil, comment)
  60. c.Assert(resp.StatusCode, check.Equals, code, comment)
  61. }
  62. id := createExec(c, "test")
  63. startExec(id, http.StatusOK)
  64. id = createExec(c, "test")
  65. dockerCmd(c, "stop", "test")
  66. startExec(id, http.StatusNotFound)
  67. dockerCmd(c, "start", "test")
  68. startExec(id, http.StatusNotFound)
  69. // make sure exec is created before pausing
  70. id = createExec(c, "test")
  71. dockerCmd(c, "pause", "test")
  72. startExec(id, http.StatusConflict)
  73. dockerCmd(c, "unpause", "test")
  74. startExec(id, http.StatusOK)
  75. }
  76. func (s *DockerSuite) TestExecAPIStartBackwardsCompatible(c *check.C) {
  77. dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
  78. id := createExec(c, "test")
  79. resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/v1.20/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "text/plain")
  80. c.Assert(err, check.IsNil)
  81. b, err := readBody(body)
  82. comment := check.Commentf("response body: %s", b)
  83. c.Assert(err, check.IsNil, comment)
  84. c.Assert(resp.StatusCode, check.Equals, http.StatusOK, comment)
  85. }
  86. func createExec(c *check.C, name string) string {
  87. _, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}})
  88. c.Assert(err, check.IsNil, check.Commentf(string(b)))
  89. createResp := struct {
  90. ID string `json:"Id"`
  91. }{}
  92. c.Assert(json.Unmarshal(b, &createResp), check.IsNil, check.Commentf(string(b)))
  93. return createResp.ID
  94. }