commands_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package docker
  2. import (
  3. "bufio"
  4. "bytes"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. func closeWrap(args ...io.Closer) error {
  13. e := false
  14. ret := fmt.Errorf("Error closing elements")
  15. for _, c := range args {
  16. if err := c.Close(); err != nil {
  17. e = true
  18. ret = fmt.Errorf("%s\n%s", ret, err)
  19. }
  20. }
  21. if e {
  22. return ret
  23. }
  24. return nil
  25. }
  26. func setTimeout(t *testing.T, msg string, d time.Duration, f func()) {
  27. c := make(chan bool)
  28. // Make sure we are not too long
  29. go func() {
  30. time.Sleep(d)
  31. c <- true
  32. }()
  33. go func() {
  34. f()
  35. c <- false
  36. }()
  37. if <-c {
  38. t.Fatal(msg)
  39. }
  40. }
  41. func assertPipe(input, output string, r io.Reader, w io.Writer, count int) error {
  42. for i := 0; i < count; i++ {
  43. if _, err := w.Write([]byte(input)); err != nil {
  44. return err
  45. }
  46. o, err := bufio.NewReader(r).ReadString('\n')
  47. if err != nil {
  48. return err
  49. }
  50. if strings.Trim(o, " \r\n") != output {
  51. return fmt.Errorf("Unexpected output. Expected [%s], received [%s]", output, o)
  52. }
  53. }
  54. return nil
  55. }
  56. // TestRunHostname checks that 'docker run -h' correctly sets a custom hostname
  57. func TestRunHostname(t *testing.T) {
  58. runtime, err := newTestRuntime()
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. defer nuke(runtime)
  63. srv := &Server{runtime: runtime}
  64. var stdin, stdout bytes.Buffer
  65. setTimeout(t, "CmdRun timed out", 2*time.Second, func() {
  66. if err := srv.CmdRun(ioutil.NopCloser(&stdin), &nopWriteCloser{&stdout}, "-h", "foobar", GetTestImage(runtime).Id, "hostname"); err != nil {
  67. t.Fatal(err)
  68. }
  69. })
  70. if output := string(stdout.Bytes()); output != "foobar\n" {
  71. t.Fatalf("'hostname' should display '%s', not '%s'", "foobar\n", output)
  72. }
  73. }
  74. // Expected behaviour: the process dies when the client disconnects
  75. func TestRunDisconnect(t *testing.T) {
  76. runtime, err := newTestRuntime()
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. defer nuke(runtime)
  81. srv := &Server{runtime: runtime}
  82. stdin, stdinPipe := io.Pipe()
  83. stdout, stdoutPipe := io.Pipe()
  84. c1 := make(chan struct{})
  85. go func() {
  86. if err := srv.CmdRun(stdin, stdoutPipe, "-i", GetTestImage(runtime).Id, "/bin/cat"); err != nil {
  87. t.Fatal(err)
  88. }
  89. close(c1)
  90. }()
  91. setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
  92. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  93. t.Fatal(err)
  94. }
  95. })
  96. // Close pipes (simulate disconnect)
  97. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  98. t.Fatal(err)
  99. }
  100. // as the pipes are close, we expect the process to die,
  101. // therefore CmdRun to unblock. Wait for CmdRun
  102. setTimeout(t, "Waiting for CmdRun timed out", 2*time.Second, func() {
  103. <-c1
  104. })
  105. // Check the status of the container
  106. container := runtime.containers.Back().Value.(*Container)
  107. if container.State.Running {
  108. t.Fatalf("/bin/cat is still running after closing stdin")
  109. }
  110. }
  111. // Expected behaviour, the process stays alive when the client disconnects
  112. func TestAttachDisconnect(t *testing.T) {
  113. runtime, err := newTestRuntime()
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. defer nuke(runtime)
  118. srv := &Server{runtime: runtime}
  119. container, err := runtime.Create(
  120. &Config{
  121. Image: GetTestImage(runtime).Id,
  122. Memory: 33554432,
  123. Cmd: []string{"/bin/cat"},
  124. OpenStdin: true,
  125. },
  126. )
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. defer runtime.Destroy(container)
  131. // Start the process
  132. if err := container.Start(); err != nil {
  133. t.Fatal(err)
  134. }
  135. stdin, stdinPipe := io.Pipe()
  136. stdout, stdoutPipe := io.Pipe()
  137. // Attach to it
  138. c1 := make(chan struct{})
  139. go func() {
  140. if err := srv.CmdAttach(stdin, stdoutPipe, container.Id); err != nil {
  141. t.Fatal(err)
  142. }
  143. close(c1)
  144. }()
  145. setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
  146. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
  147. t.Fatal(err)
  148. }
  149. })
  150. // Close pipes (client disconnects)
  151. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  152. t.Fatal(err)
  153. }
  154. // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  155. setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
  156. <-c1
  157. })
  158. // We closed stdin, expect /bin/cat to still be running
  159. // Wait a little bit to make sure container.monitor() did his thing
  160. err = container.WaitTimeout(500 * time.Millisecond)
  161. if err == nil || !container.State.Running {
  162. t.Fatalf("/bin/cat is not running after closing stdin")
  163. }
  164. // Try to avoid the timeoout in destroy. Best effort, don't check error
  165. cStdin, _ := container.StdinPipe()
  166. cStdin.Close()
  167. }