docker_api_exec_resize_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/pkg/integration/checker"
  11. "github.com/go-check/check"
  12. )
  13. func (s *DockerSuite) TestExecResizeApiHeightWidthNoInt(c *check.C) {
  14. testRequires(c, DaemonIsLinux)
  15. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  16. cleanedContainerID := strings.TrimSpace(out)
  17. endpoint := "/exec/" + cleanedContainerID + "/resize?h=foo&w=bar"
  18. status, _, err := sockRequest("POST", endpoint, nil)
  19. c.Assert(err, checker.IsNil)
  20. c.Assert(status, checker.Equals, http.StatusInternalServerError)
  21. }
  22. // Part of #14845
  23. func (s *DockerSuite) TestExecResizeImmediatelyAfterExecStart(c *check.C) {
  24. testRequires(c, DaemonIsLinux)
  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 := sockRequest("POST", uri, data)
  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 := sockRequestHijack("POST", fmt.Sprintf("/exec/%s/start", execID), payload, "application/json")
  51. if err != nil {
  52. return fmt.Errorf("Failed to start the exec: %q", err.Error())
  53. }
  54. defer conn.Close()
  55. _, rc, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/resize?h=24&w=80", execID), nil, "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. }