docker_api_exec_resize_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "strings"
  9. "github.com/go-check/check"
  10. )
  11. func (s *DockerSuite) TestExecResizeApiHeightWidthNoInt(c *check.C) {
  12. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  13. cleanedContainerID := strings.TrimSpace(out)
  14. endpoint := "/exec/" + cleanedContainerID + "/resize?h=foo&w=bar"
  15. status, _, err := sockRequest("POST", endpoint, nil)
  16. c.Assert(status, check.Equals, http.StatusInternalServerError)
  17. c.Assert(err, check.IsNil)
  18. }
  19. // Part of #14845
  20. func (s *DockerSuite) TestExecResizeImmediatelyAfterExecStart(c *check.C) {
  21. name := "exec_resize_test"
  22. dockerCmd(c, "run", "-d", "-i", "-t", "--name", name, "--restart", "always", "busybox", "/bin/sh")
  23. // The panic happens when daemon.ContainerExecStart is called but the
  24. // container.Exec is not called.
  25. // Because the panic is not 100% reproducible, we send the requests concurrently
  26. // to increase the probability that the problem is triggered.
  27. n := 10
  28. ch := make(chan struct{})
  29. for i := 0; i < n; i++ {
  30. go func() {
  31. defer func() {
  32. ch <- struct{}{}
  33. }()
  34. data := map[string]interface{}{
  35. "AttachStdin": true,
  36. "Cmd": []string{"/bin/sh"},
  37. }
  38. status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), data)
  39. c.Assert(err, check.IsNil)
  40. c.Assert(status, check.Equals, http.StatusCreated)
  41. out := map[string]string{}
  42. err = json.Unmarshal(body, &out)
  43. c.Assert(err, check.IsNil)
  44. execID := out["Id"]
  45. if len(execID) < 1 {
  46. c.Fatal("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. c.Assert(err, check.IsNil)
  51. defer conn.Close()
  52. _, rc, err := sockRequestRaw("POST", fmt.Sprintf("/exec/%s/resize?h=24&w=80", execID), nil, "text/plain")
  53. // It's probably a panic of the daemon if io.ErrUnexpectedEOF is returned.
  54. if err == io.ErrUnexpectedEOF {
  55. c.Fatal("The daemon might have crashed.")
  56. }
  57. if err == nil {
  58. rc.Close()
  59. }
  60. }()
  61. }
  62. for i := 0; i < n; i++ {
  63. <-ch
  64. }
  65. }