docker_api_exec_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // +build !test_no_exec
  2. package main
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "strings"
  9. "time"
  10. "github.com/docker/docker/pkg/integration/checker"
  11. "github.com/go-check/check"
  12. )
  13. // Regression test for #9414
  14. func (s *DockerSuite) TestExecAPICreateNoCmd(c *check.C) {
  15. name := "exec_test"
  16. dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
  17. status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": nil})
  18. c.Assert(err, checker.IsNil)
  19. c.Assert(status, checker.Equals, http.StatusInternalServerError)
  20. comment := check.Commentf("Expected message when creating exec command with no Cmd specified")
  21. c.Assert(getErrorMessage(c, body), checker.Contains, "No exec command specified", comment)
  22. }
  23. func (s *DockerSuite) TestExecAPICreateNoValidContentType(c *check.C) {
  24. name := "exec_test"
  25. dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
  26. jsonData := bytes.NewBuffer(nil)
  27. if err := json.NewEncoder(jsonData).Encode(map[string]interface{}{"Cmd": nil}); err != nil {
  28. c.Fatalf("Can not encode data to json %s", err)
  29. }
  30. res, body, err := sockRequestRaw("POST", fmt.Sprintf("/containers/%s/exec", name), jsonData, "text/plain")
  31. c.Assert(err, checker.IsNil)
  32. c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError)
  33. b, err := readBody(body)
  34. c.Assert(err, checker.IsNil)
  35. comment := check.Commentf("Expected message when creating exec command with invalid Content-Type specified")
  36. c.Assert(getErrorMessage(c, b), checker.Contains, "Content-Type specified", comment)
  37. }
  38. func (s *DockerSuite) TestExecAPICreateContainerPaused(c *check.C) {
  39. // Not relevant on Windows as Windows containers cannot be paused
  40. testRequires(c, DaemonIsLinux)
  41. name := "exec_create_test"
  42. dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh")
  43. dockerCmd(c, "pause", name)
  44. status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}})
  45. c.Assert(err, checker.IsNil)
  46. c.Assert(status, checker.Equals, http.StatusConflict)
  47. comment := check.Commentf("Expected message when creating exec command with Container %s is paused", name)
  48. c.Assert(getErrorMessage(c, body), checker.Contains, "Container "+name+" is paused, unpause the container before exec", comment)
  49. }
  50. func (s *DockerSuite) TestExecAPIStart(c *check.C) {
  51. testRequires(c, DaemonIsLinux) // Uses pause/unpause but bits may be salvagable to Windows to Windows CI
  52. dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
  53. id := createExec(c, "test")
  54. startExec(c, id, http.StatusOK)
  55. var execJSON struct{ PID int }
  56. inspectExec(c, id, &execJSON)
  57. c.Assert(execJSON.PID, checker.GreaterThan, 1)
  58. id = createExec(c, "test")
  59. dockerCmd(c, "stop", "test")
  60. startExec(c, id, http.StatusNotFound)
  61. dockerCmd(c, "start", "test")
  62. startExec(c, id, http.StatusNotFound)
  63. // make sure exec is created before pausing
  64. id = createExec(c, "test")
  65. dockerCmd(c, "pause", "test")
  66. startExec(c, id, http.StatusConflict)
  67. dockerCmd(c, "unpause", "test")
  68. startExec(c, id, http.StatusOK)
  69. }
  70. func (s *DockerSuite) TestExecAPIStartEnsureHeaders(c *check.C) {
  71. testRequires(c, DaemonIsLinux)
  72. dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
  73. id := createExec(c, "test")
  74. resp, _, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json")
  75. c.Assert(err, checker.IsNil)
  76. c.Assert(resp.Header.Get("Server"), checker.Not(checker.Equals), "")
  77. }
  78. func (s *DockerSuite) TestExecAPIStartBackwardsCompatible(c *check.C) {
  79. testRequires(c, DaemonIsLinux) // Windows only supports 1.25 or later
  80. runSleepingContainer(c, "-d", "--name", "test")
  81. id := createExec(c, "test")
  82. resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/v1.20/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "text/plain")
  83. c.Assert(err, checker.IsNil)
  84. b, err := readBody(body)
  85. comment := check.Commentf("response body: %s", b)
  86. c.Assert(err, checker.IsNil, comment)
  87. c.Assert(resp.StatusCode, checker.Equals, http.StatusOK, comment)
  88. }
  89. // #19362
  90. func (s *DockerSuite) TestExecAPIStartMultipleTimesError(c *check.C) {
  91. runSleepingContainer(c, "-d", "--name", "test")
  92. execID := createExec(c, "test")
  93. startExec(c, execID, http.StatusOK)
  94. timeout := time.After(60 * time.Second)
  95. var execJSON struct{ Running bool }
  96. for {
  97. select {
  98. case <-timeout:
  99. c.Fatal("timeout waiting for exec to start")
  100. default:
  101. }
  102. inspectExec(c, execID, &execJSON)
  103. if !execJSON.Running {
  104. break
  105. }
  106. }
  107. startExec(c, execID, http.StatusConflict)
  108. }
  109. // #20638
  110. func (s *DockerSuite) TestExecAPIStartWithDetach(c *check.C) {
  111. name := "foo"
  112. runSleepingContainer(c, "-d", "-t", "--name", name)
  113. data := map[string]interface{}{
  114. "cmd": []string{"true"},
  115. "AttachStdin": true,
  116. }
  117. _, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), data)
  118. c.Assert(err, checker.IsNil, check.Commentf(string(b)))
  119. createResp := struct {
  120. ID string `json:"Id"`
  121. }{}
  122. c.Assert(json.Unmarshal(b, &createResp), checker.IsNil, check.Commentf(string(b)))
  123. _, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", createResp.ID), strings.NewReader(`{"Detach": true}`), "application/json")
  124. c.Assert(err, checker.IsNil)
  125. b, err = readBody(body)
  126. comment := check.Commentf("response body: %s", b)
  127. c.Assert(err, checker.IsNil, comment)
  128. resp, _, err := sockRequestRaw("GET", "/_ping", nil, "")
  129. c.Assert(err, checker.IsNil)
  130. if resp.StatusCode != http.StatusOK {
  131. c.Fatal("daemon is down, it should alive")
  132. }
  133. }
  134. func createExec(c *check.C, name string) string {
  135. _, b, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), map[string]interface{}{"Cmd": []string{"true"}})
  136. c.Assert(err, checker.IsNil, check.Commentf(string(b)))
  137. createResp := struct {
  138. ID string `json:"Id"`
  139. }{}
  140. c.Assert(json.Unmarshal(b, &createResp), checker.IsNil, check.Commentf(string(b)))
  141. return createResp.ID
  142. }
  143. func startExec(c *check.C, id string, code int) {
  144. resp, body, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/start", id), strings.NewReader(`{"Detach": true}`), "application/json")
  145. c.Assert(err, checker.IsNil)
  146. b, err := readBody(body)
  147. comment := check.Commentf("response body: %s", b)
  148. c.Assert(err, checker.IsNil, comment)
  149. c.Assert(resp.StatusCode, checker.Equals, code, comment)
  150. }
  151. func inspectExec(c *check.C, id string, out interface{}) {
  152. resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/exec/%s/json", id), nil, "")
  153. c.Assert(err, checker.IsNil)
  154. defer body.Close()
  155. c.Assert(resp.StatusCode, checker.Equals, http.StatusOK)
  156. err = json.NewDecoder(body).Decode(out)
  157. c.Assert(err, checker.IsNil)
  158. }