commands_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package docker
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "strings"
  7. "testing"
  8. "time"
  9. )
  10. func closeWrap(args ...io.Closer) error {
  11. e := false
  12. ret := fmt.Errorf("Error closing elements")
  13. for _, c := range args {
  14. if err := c.Close(); err != nil {
  15. e = true
  16. ret = fmt.Errorf("%s\n%s", ret, err)
  17. }
  18. }
  19. if e {
  20. return ret
  21. }
  22. return nil
  23. }
  24. func setTimeout(t *testing.T, msg string, d time.Duration, f func(chan bool)) {
  25. c := make(chan bool)
  26. // Make sure we are not too long
  27. go func() {
  28. time.Sleep(d)
  29. c <- true
  30. }()
  31. go f(c)
  32. if timeout := <-c; timeout {
  33. t.Fatalf("Timeout: %s", msg)
  34. }
  35. }
  36. func assertPipe(input, output string, r io.Reader, w io.Writer, count int) error {
  37. for i := 0; i < count; i++ {
  38. if _, err := w.Write([]byte(input)); err != nil {
  39. return err
  40. }
  41. o, err := bufio.NewReader(r).ReadString('\n')
  42. if err != nil {
  43. return err
  44. }
  45. if strings.Trim(o, " \r\n") != output {
  46. return fmt.Errorf("Unexpected output. Expected [%s], received [%s]", output, o)
  47. }
  48. }
  49. return nil
  50. }
  51. // Test the behavior of a client disconnection.
  52. // We expect a client disconnect to leave the stdin of the container open
  53. // Therefore a process will keep his stdin open when a client disconnects
  54. func TestReattachAfterDisconnect(t *testing.T) {
  55. runtime, err := newTestRuntime()
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. defer nuke(runtime)
  60. // FIXME: low down the timeout (after #230)
  61. setTimeout(t, "TestReattachAfterDisconnect", 12*time.Second, func(timeout chan bool) {
  62. srv := &Server{runtime: runtime}
  63. stdin, stdinPipe := io.Pipe()
  64. stdout, stdoutPipe := io.Pipe()
  65. c1 := make(chan struct{})
  66. go func() {
  67. if err := srv.CmdRun(stdin, stdoutPipe, "-i", GetTestImage(runtime).Id, "/bin/cat"); err == nil {
  68. t.Fatal("CmdRun should generate a read/write on closed pipe error. No error found.")
  69. }
  70. close(c1)
  71. }()
  72. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  73. t.Fatal(err)
  74. }
  75. // Close pipes (simulate disconnect)
  76. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  77. t.Fatal(err)
  78. }
  79. container := runtime.containers.Back().Value.(*Container)
  80. // Recreate the pipes
  81. stdin, stdinPipe = io.Pipe()
  82. stdout, stdoutPipe = io.Pipe()
  83. // Attach to it
  84. c2 := make(chan struct{})
  85. go func() {
  86. if err := srv.CmdAttach(stdin, stdoutPipe, container.Id); err != nil {
  87. t.Fatal(err)
  88. }
  89. close(c2)
  90. }()
  91. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  92. t.Fatal(err)
  93. }
  94. // Close pipes
  95. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  96. t.Fatal(err)
  97. }
  98. // FIXME: when #230 will be finished, send SIGINT instead of SIGTERM
  99. // we expect cat to stay alive so SIGTERM will have no effect
  100. // and Stop will timeout
  101. if err := container.Stop(); err != nil {
  102. t.Fatal(err)
  103. }
  104. // Wait for run and attach to finish
  105. <-c1
  106. <-c2
  107. // Finished, no timeout
  108. timeout <- false
  109. })
  110. }