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