docker_api_exec_resize_test.go 2.7 KB

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