commands_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package docker
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/docker/docker/api/client"
  11. "github.com/docker/docker/daemon"
  12. "github.com/docker/docker/pkg/term"
  13. )
  14. func closeWrap(args ...io.Closer) error {
  15. e := false
  16. ret := fmt.Errorf("Error closing elements")
  17. for _, c := range args {
  18. if err := c.Close(); err != nil {
  19. e = true
  20. ret = fmt.Errorf("%s\n%s", ret, err)
  21. }
  22. }
  23. if e {
  24. return ret
  25. }
  26. return nil
  27. }
  28. func setRaw(t *testing.T, c *daemon.Container) *term.State {
  29. pty, err := c.GetPtyMaster()
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. state, err := term.MakeRaw(pty.Fd())
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. return state
  38. }
  39. func unsetRaw(t *testing.T, c *daemon.Container, state *term.State) {
  40. pty, err := c.GetPtyMaster()
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. term.RestoreTerminal(pty.Fd(), state)
  45. }
  46. func waitContainerStart(t *testing.T, timeout time.Duration) *daemon.Container {
  47. var container *daemon.Container
  48. setTimeout(t, "Waiting for the container to be started timed out", timeout, func() {
  49. for {
  50. l := globalDaemon.List()
  51. if len(l) == 1 && l[0].IsRunning() {
  52. container = l[0]
  53. break
  54. }
  55. time.Sleep(10 * time.Millisecond)
  56. }
  57. })
  58. if container == nil {
  59. t.Fatal("An error occured while waiting for the container to start")
  60. }
  61. return container
  62. }
  63. func setTimeout(t *testing.T, msg string, d time.Duration, f func()) {
  64. c := make(chan bool)
  65. // Make sure we are not too long
  66. go func() {
  67. time.Sleep(d)
  68. c <- true
  69. }()
  70. go func() {
  71. f()
  72. c <- false
  73. }()
  74. if <-c && msg != "" {
  75. t.Fatal(msg)
  76. }
  77. }
  78. func expectPipe(expected string, r io.Reader) error {
  79. o, err := bufio.NewReader(r).ReadString('\n')
  80. if err != nil {
  81. return err
  82. }
  83. if strings.Trim(o, " \r\n") != expected {
  84. return fmt.Errorf("Unexpected output. Expected [%s], received [%s]", expected, o)
  85. }
  86. return nil
  87. }
  88. func assertPipe(input, output string, r io.Reader, w io.Writer, count int) error {
  89. for i := 0; i < count; i++ {
  90. if _, err := w.Write([]byte(input)); err != nil {
  91. return err
  92. }
  93. if err := expectPipe(output, r); err != nil {
  94. return err
  95. }
  96. }
  97. return nil
  98. }
  99. // Expected behaviour: container gets deleted automatically after exit
  100. func TestRunAutoRemove(t *testing.T) {
  101. t.Skip("Fixme. Skipping test for now, race condition")
  102. stdout, stdoutPipe := io.Pipe()
  103. cli := client.NewDockerCli(nil, stdoutPipe, ioutil.Discard, "", testDaemonProto, testDaemonAddr, nil)
  104. defer cleanup(globalEngine, t)
  105. c := make(chan struct{})
  106. go func() {
  107. defer close(c)
  108. if err := cli.CmdRun("--rm", unitTestImageID, "hostname"); err != nil {
  109. t.Fatal(err)
  110. }
  111. }()
  112. var temporaryContainerID string
  113. setTimeout(t, "Reading command output time out", 2*time.Second, func() {
  114. cmdOutput, err := bufio.NewReader(stdout).ReadString('\n')
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. temporaryContainerID = cmdOutput
  119. if err := closeWrap(stdout, stdoutPipe); err != nil {
  120. t.Fatal(err)
  121. }
  122. })
  123. setTimeout(t, "CmdRun timed out", 10*time.Second, func() {
  124. <-c
  125. })
  126. time.Sleep(500 * time.Millisecond)
  127. if len(globalDaemon.List()) > 0 {
  128. t.Fatalf("failed to remove container automatically: container %s still exists", temporaryContainerID)
  129. }
  130. }