docker_api_exec_resize_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "sync"
  10. "github.com/docker/docker/api/types/versions"
  11. "github.com/docker/docker/integration-cli/checker"
  12. "github.com/docker/docker/internal/test/request"
  13. "github.com/go-check/check"
  14. )
  15. func (s *DockerSuite) TestExecResizeAPIHeightWidthNoInt(c *check.C) {
  16. testRequires(c, DaemonIsLinux)
  17. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  18. cleanedContainerID := strings.TrimSpace(out)
  19. endpoint := "/exec/" + cleanedContainerID + "/resize?h=foo&w=bar"
  20. res, _, err := request.Post(endpoint)
  21. c.Assert(err, checker.IsNil)
  22. if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
  23. c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError)
  24. } else {
  25. c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
  26. }
  27. }
  28. // Part of #14845
  29. func (s *DockerSuite) TestExecResizeImmediatelyAfterExecStart(c *check.C) {
  30. name := "exec_resize_test"
  31. dockerCmd(c, "run", "-d", "-i", "-t", "--name", name, "--restart", "always", "busybox", "/bin/sh")
  32. testExecResize := func() error {
  33. data := map[string]interface{}{
  34. "AttachStdin": true,
  35. "Cmd": []string{"/bin/sh"},
  36. }
  37. uri := fmt.Sprintf("/containers/%s/exec", name)
  38. res, body, err := request.Post(uri, request.JSONBody(data))
  39. if err != nil {
  40. return err
  41. }
  42. if res.StatusCode != http.StatusCreated {
  43. return fmt.Errorf("POST %s is expected to return %d, got %d", uri, http.StatusCreated, res.StatusCode)
  44. }
  45. buf, err := request.ReadBody(body)
  46. c.Assert(err, checker.IsNil)
  47. out := map[string]string{}
  48. err = json.Unmarshal(buf, &out)
  49. if err != nil {
  50. return fmt.Errorf("ExecCreate returned invalid json. Error: %q", err.Error())
  51. }
  52. execID := out["Id"]
  53. if len(execID) < 1 {
  54. return fmt.Errorf("ExecCreate got invalid execID")
  55. }
  56. payload := bytes.NewBufferString(`{"Tty":true}`)
  57. conn, _, err := sockRequestHijack("POST", fmt.Sprintf("/exec/%s/start", execID), payload, "application/json", daemonHost())
  58. if err != nil {
  59. return fmt.Errorf("Failed to start the exec: %q", err.Error())
  60. }
  61. defer conn.Close()
  62. _, rc, err := request.Post(fmt.Sprintf("/exec/%s/resize?h=24&w=80", execID), request.ContentType("text/plain"))
  63. if err != nil {
  64. // It's probably a panic of the daemon if io.ErrUnexpectedEOF is returned.
  65. if err == io.ErrUnexpectedEOF {
  66. return fmt.Errorf("The daemon might have crashed.")
  67. }
  68. // Other error happened, should be reported.
  69. return fmt.Errorf("Fail to exec resize immediately after start. Error: %q", err.Error())
  70. }
  71. rc.Close()
  72. return nil
  73. }
  74. // The panic happens when daemon.ContainerExecStart is called but the
  75. // container.Exec is not called.
  76. // Because the panic is not 100% reproducible, we send the requests concurrently
  77. // to increase the probability that the problem is triggered.
  78. var (
  79. n = 10
  80. ch = make(chan error, n)
  81. wg sync.WaitGroup
  82. )
  83. for i := 0; i < n; i++ {
  84. wg.Add(1)
  85. go func() {
  86. defer wg.Done()
  87. if err := testExecResize(); err != nil {
  88. ch <- err
  89. }
  90. }()
  91. }
  92. wg.Wait()
  93. select {
  94. case err := <-ch:
  95. c.Fatal(err.Error())
  96. default:
  97. }
  98. }