docker_cli_exec_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package main
  2. import (
  3. "bufio"
  4. "os"
  5. "os/exec"
  6. "strings"
  7. "testing"
  8. "time"
  9. )
  10. func TestExec(t *testing.T) {
  11. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100")
  12. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  13. t.Fatal(out, err)
  14. }
  15. execCmd := exec.Command(dockerBinary, "exec", "testing", "cat", "/tmp/file")
  16. out, _, err := runCommandWithOutput(execCmd)
  17. if err != nil {
  18. t.Fatal(out, err)
  19. }
  20. out = strings.Trim(out, "\r\n")
  21. if expected := "test"; out != expected {
  22. t.Errorf("container exec should've printed %q but printed %q", expected, out)
  23. }
  24. deleteAllContainers()
  25. logDone("exec - basic test")
  26. }
  27. func TestExecInteractiveStdinClose(t *testing.T) {
  28. defer deleteAllContainers()
  29. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "busybox", "/bin/cat"))
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. contId := strings.TrimSpace(out)
  34. println(contId)
  35. returnchan := make(chan struct{})
  36. go func() {
  37. var err error
  38. cmd := exec.Command(dockerBinary, "exec", "-i", contId, "/bin/ls", "/")
  39. cmd.Stdin = os.Stdin
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. out, err := cmd.CombinedOutput()
  44. if err != nil {
  45. t.Fatal(err, out)
  46. }
  47. if string(out) == "" {
  48. t.Fatalf("Output was empty, likely blocked by standard input")
  49. }
  50. returnchan <- struct{}{}
  51. }()
  52. select {
  53. case <-returnchan:
  54. case <-time.After(10 * time.Second):
  55. t.Fatal("timed out running docker exec")
  56. }
  57. logDone("exec - interactive mode closes stdin after execution")
  58. }
  59. func TestExecInteractive(t *testing.T) {
  60. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100")
  61. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  62. t.Fatal(out, err)
  63. }
  64. execCmd := exec.Command(dockerBinary, "exec", "-i", "testing", "sh")
  65. stdin, err := execCmd.StdinPipe()
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. stdout, err := execCmd.StdoutPipe()
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. if err := execCmd.Start(); err != nil {
  74. t.Fatal(err)
  75. }
  76. if _, err := stdin.Write([]byte("cat /tmp/file\n")); err != nil {
  77. t.Fatal(err)
  78. }
  79. r := bufio.NewReader(stdout)
  80. line, err := r.ReadString('\n')
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. line = strings.TrimSpace(line)
  85. if line != "test" {
  86. t.Fatalf("Output should be 'test', got '%q'", line)
  87. }
  88. if err := stdin.Close(); err != nil {
  89. t.Fatal(err)
  90. }
  91. finish := make(chan struct{})
  92. go func() {
  93. if err := execCmd.Wait(); err != nil {
  94. t.Fatal(err)
  95. }
  96. close(finish)
  97. }()
  98. select {
  99. case <-finish:
  100. case <-time.After(1 * time.Second):
  101. t.Fatal("docker exec failed to exit on stdin close")
  102. }
  103. deleteAllContainers()
  104. logDone("exec - Interactive test")
  105. }
  106. func TestExecAfterContainerRestart(t *testing.T) {
  107. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  108. out, _, err := runCommandWithOutput(runCmd)
  109. if err != nil {
  110. t.Fatal(out, err)
  111. }
  112. cleanedContainerID := stripTrailingCharacters(out)
  113. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  114. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  115. t.Fatal(out, err)
  116. }
  117. runCmd = exec.Command(dockerBinary, "exec", cleanedContainerID, "echo", "hello")
  118. out, _, err = runCommandWithOutput(runCmd)
  119. if err != nil {
  120. t.Fatal(out, err)
  121. }
  122. outStr := strings.TrimSpace(out)
  123. if outStr != "hello" {
  124. t.Errorf("container should've printed hello, instead printed %q", outStr)
  125. }
  126. deleteAllContainers()
  127. logDone("exec - exec running container after container restart")
  128. }
  129. func TestExecAfterDaemonRestart(t *testing.T) {
  130. d := NewDaemon(t)
  131. if err := d.StartWithBusybox(); err != nil {
  132. t.Fatalf("Could not start daemon with busybox: %v", err)
  133. }
  134. defer d.Stop()
  135. if out, err := d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
  136. t.Fatalf("Could not run top: err=%v\n%s", err, out)
  137. }
  138. if err := d.Restart(); err != nil {
  139. t.Fatalf("Could not restart daemon: %v", err)
  140. }
  141. if out, err := d.Cmd("start", "top"); err != nil {
  142. t.Fatalf("Could not start top after daemon restart: err=%v\n%s", err, out)
  143. }
  144. out, err := d.Cmd("exec", "top", "echo", "hello")
  145. if err != nil {
  146. t.Fatalf("Could not exec on container top: err=%v\n%s", err, out)
  147. }
  148. outStr := strings.TrimSpace(string(out))
  149. if outStr != "hello" {
  150. t.Errorf("container should've printed hello, instead printed %q", outStr)
  151. }
  152. logDone("exec - exec running container after daemon restart")
  153. }