docker_api_exec_resize_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. testRequires(c, NativeExecDriver)
  22. name := "exec_resize_test"
  23. dockerCmd(c, "run", "-d", "-i", "-t", "--name", name, "--restart", "always", "busybox", "/bin/sh")
  24. // The panic happens when daemon.ContainerExecStart is called but the
  25. // container.Exec is not called.
  26. // Because the panic is not 100% reproducible, we send the requests concurrently
  27. // to increase the probability that the problem is triggered.
  28. n := 10
  29. ch := make(chan struct{})
  30. for i := 0; i < n; i++ {
  31. go func() {
  32. defer func() {
  33. ch <- struct{}{}
  34. }()
  35. data := map[string]interface{}{
  36. "AttachStdin": true,
  37. "Cmd": []string{"/bin/sh"},
  38. }
  39. status, body, err := sockRequest("POST", fmt.Sprintf("/containers/%s/exec", name), data)
  40. c.Assert(err, check.IsNil)
  41. c.Assert(status, check.Equals, http.StatusCreated)
  42. out := map[string]string{}
  43. err = json.Unmarshal(body, &out)
  44. c.Assert(err, check.IsNil)
  45. execID := out["Id"]
  46. if len(execID) < 1 {
  47. c.Fatal("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. c.Assert(err, check.IsNil)
  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. c.Fatal("The daemon might have crashed.")
  57. }
  58. if err == nil {
  59. rc.Close()
  60. }
  61. }()
  62. }
  63. for i := 0; i < n; i++ {
  64. <-ch
  65. }
  66. }