docker_api_exec_resize_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. status, _, err := request.SockRequest("POST", endpoint, nil, daemonHost())
  20. c.Assert(err, checker.IsNil)
  21. c.Assert(status, checker.Equals, http.StatusInternalServerError)
  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. status, body, err := request.SockRequest("POST", uri, data, daemonHost())
  34. if err != nil {
  35. return err
  36. }
  37. if status != http.StatusCreated {
  38. return fmt.Errorf("POST %s is expected to return %d, got %d", uri, http.StatusCreated, status)
  39. }
  40. out := map[string]string{}
  41. err = json.Unmarshal(body, &out)
  42. if err != nil {
  43. return fmt.Errorf("ExecCreate returned invalid json. Error: %q", err.Error())
  44. }
  45. execID := out["Id"]
  46. if len(execID) < 1 {
  47. return fmt.Errorf("ExecCreate got invalid execID")
  48. }
  49. payload := bytes.NewBufferString(`{"Tty":true}`)
  50. conn, _, err := request.SockRequestHijack("POST", fmt.Sprintf("/exec/%s/start", execID), payload, "application/json", daemonHost())
  51. if err != nil {
  52. return fmt.Errorf("Failed to start the exec: %q", err.Error())
  53. }
  54. defer conn.Close()
  55. _, rc, err := request.Post(daemonHost(), fmt.Sprintf("/exec/%s/resize?h=24&w=80", execID), request.ContentType("text/plain"))
  56. // It's probably a panic of the daemon if io.ErrUnexpectedEOF is returned.
  57. if err == io.ErrUnexpectedEOF {
  58. return fmt.Errorf("The daemon might have crashed.")
  59. }
  60. if err == nil {
  61. rc.Close()
  62. }
  63. // We only interested in the io.ErrUnexpectedEOF error, so we return nil otherwise.
  64. return nil
  65. }
  66. // The panic happens when daemon.ContainerExecStart is called but the
  67. // container.Exec is not called.
  68. // Because the panic is not 100% reproducible, we send the requests concurrently
  69. // to increase the probability that the problem is triggered.
  70. var (
  71. n = 10
  72. ch = make(chan error, n)
  73. wg sync.WaitGroup
  74. )
  75. for i := 0; i < n; i++ {
  76. wg.Add(1)
  77. go func() {
  78. defer wg.Done()
  79. if err := testExecResize(); err != nil {
  80. ch <- err
  81. }
  82. }()
  83. }
  84. wg.Wait()
  85. select {
  86. case err := <-ch:
  87. c.Fatal(err.Error())
  88. default:
  89. }
  90. }