docker_api_exec_resize_test.go 3.1 KB

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