docker_api_exec_resize_test.go 3.2 KB

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