docker_api_exec_resize_test.go 3.0 KB

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