docker_api_exec_resize_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/integration-cli/checker"
  11. "github.com/docker/docker/integration-cli/request"
  12. "github.com/go-check/check"
  13. )
  14. func (s *DockerSuite) TestExecResizeAPIHeightWidthNoInt(c *check.C) {
  15. testRequires(c, DaemonIsLinux)
  16. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  17. cleanedContainerID := strings.TrimSpace(out)
  18. endpoint := "/exec/" + cleanedContainerID + "/resize?h=foo&w=bar"
  19. res, _, err := request.Post(endpoint)
  20. c.Assert(err, checker.IsNil)
  21. c.Assert(res.StatusCode, checker.Equals, http.StatusBadRequest)
  22. }
  23. // Part of #14845
  24. func (s *DockerSuite) TestExecResizeImmediatelyAfterExecStart(c *check.C) {
  25. name := "exec_resize_test"
  26. dockerCmd(c, "run", "-d", "-i", "-t", "--name", name, "--restart", "always", "busybox", "/bin/sh")
  27. testExecResize := func() error {
  28. data := map[string]interface{}{
  29. "AttachStdin": true,
  30. "Cmd": []string{"/bin/sh"},
  31. }
  32. uri := fmt.Sprintf("/containers/%s/exec", name)
  33. res, body, err := request.Post(uri, request.JSONBody(data))
  34. if err != nil {
  35. return err
  36. }
  37. if res.StatusCode != http.StatusCreated {
  38. return fmt.Errorf("POST %s is expected to return %d, got %d", uri, http.StatusCreated, res.StatusCode)
  39. }
  40. buf, err := request.ReadBody(body)
  41. c.Assert(err, checker.IsNil)
  42. out := map[string]string{}
  43. err = json.Unmarshal(buf, &out)
  44. if err != nil {
  45. return fmt.Errorf("ExecCreate returned invalid json. Error: %q", err.Error())
  46. }
  47. execID := out["Id"]
  48. if len(execID) < 1 {
  49. return fmt.Errorf("ExecCreate got invalid execID")
  50. }
  51. payload := bytes.NewBufferString(`{"Tty":true}`)
  52. conn, _, err := request.SockRequestHijack("POST", fmt.Sprintf("/exec/%s/start", execID), payload, "application/json", daemonHost())
  53. if err != nil {
  54. return fmt.Errorf("Failed to start the exec: %q", err.Error())
  55. }
  56. defer conn.Close()
  57. _, rc, err := request.Post(fmt.Sprintf("/exec/%s/resize?h=24&w=80", execID), request.ContentType("text/plain"))
  58. // It's probably a panic of the daemon if io.ErrUnexpectedEOF is returned.
  59. if err == io.ErrUnexpectedEOF {
  60. return fmt.Errorf("The daemon might have crashed.")
  61. }
  62. if err == nil {
  63. rc.Close()
  64. }
  65. // We only interested in the io.ErrUnexpectedEOF error, so we return nil otherwise.
  66. return nil
  67. }
  68. // The panic happens when daemon.ContainerExecStart is called but the
  69. // container.Exec is not called.
  70. // Because the panic is not 100% reproducible, we send the requests concurrently
  71. // to increase the probability that the problem is triggered.
  72. var (
  73. n = 10
  74. ch = make(chan error, n)
  75. wg sync.WaitGroup
  76. )
  77. for i := 0; i < n; i++ {
  78. wg.Add(1)
  79. go func() {
  80. defer wg.Done()
  81. if err := testExecResize(); err != nil {
  82. ch <- err
  83. }
  84. }()
  85. }
  86. wg.Wait()
  87. select {
  88. case err := <-ch:
  89. c.Fatal(err.Error())
  90. default:
  91. }
  92. }