docker_api_exec_resize_test.go 3.0 KB

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