docker_api_exec_resize_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // It's probably a panic of the daemon if io.ErrUnexpectedEOF is returned.
  64. if err == io.ErrUnexpectedEOF {
  65. return fmt.Errorf("The daemon might have crashed.")
  66. }
  67. if err == nil {
  68. rc.Close()
  69. }
  70. // We only interested in the io.ErrUnexpectedEOF error, so we return nil otherwise.
  71. return nil
  72. }
  73. // The panic happens when daemon.ContainerExecStart is called but the
  74. // container.Exec is not called.
  75. // Because the panic is not 100% reproducible, we send the requests concurrently
  76. // to increase the probability that the problem is triggered.
  77. var (
  78. n = 10
  79. ch = make(chan error, n)
  80. wg sync.WaitGroup
  81. )
  82. for i := 0; i < n; i++ {
  83. wg.Add(1)
  84. go func() {
  85. defer wg.Done()
  86. if err := testExecResize(); err != nil {
  87. ch <- err
  88. }
  89. }()
  90. }
  91. wg.Wait()
  92. select {
  93. case err := <-ch:
  94. c.Fatal(err.Error())
  95. default:
  96. }
  97. }