docker_cli_exec_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. returnchan := make(chan struct{})
  35. go func() {
  36. var err error
  37. cmd := exec.Command(dockerBinary, "exec", "-i", contId, "/bin/ls", "/")
  38. cmd.Stdin = os.Stdin
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. out, err := cmd.CombinedOutput()
  43. if err != nil {
  44. t.Fatal(err, out)
  45. }
  46. if string(out) == "" {
  47. t.Fatalf("Output was empty, likely blocked by standard input")
  48. }
  49. returnchan <- struct{}{}
  50. }()
  51. select {
  52. case <-returnchan:
  53. case <-time.After(10 * time.Second):
  54. t.Fatal("timed out running docker exec")
  55. }
  56. logDone("exec - interactive mode closes stdin after execution")
  57. }
  58. func TestExecInteractive(t *testing.T) {
  59. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100")
  60. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  61. t.Fatal(out, err)
  62. }
  63. execCmd := exec.Command(dockerBinary, "exec", "-i", "testing", "sh")
  64. stdin, err := execCmd.StdinPipe()
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. stdout, err := execCmd.StdoutPipe()
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. if err := execCmd.Start(); err != nil {
  73. t.Fatal(err)
  74. }
  75. if _, err := stdin.Write([]byte("cat /tmp/file\n")); err != nil {
  76. t.Fatal(err)
  77. }
  78. r := bufio.NewReader(stdout)
  79. line, err := r.ReadString('\n')
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. line = strings.TrimSpace(line)
  84. if line != "test" {
  85. t.Fatalf("Output should be 'test', got '%q'", line)
  86. }
  87. if err := stdin.Close(); err != nil {
  88. t.Fatal(err)
  89. }
  90. finish := make(chan struct{})
  91. go func() {
  92. if err := execCmd.Wait(); err != nil {
  93. t.Fatal(err)
  94. }
  95. close(finish)
  96. }()
  97. select {
  98. case <-finish:
  99. case <-time.After(1 * time.Second):
  100. t.Fatal("docker exec failed to exit on stdin close")
  101. }
  102. deleteAllContainers()
  103. logDone("exec - Interactive test")
  104. }
  105. func TestExecAfterContainerRestart(t *testing.T) {
  106. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  107. out, _, err := runCommandWithOutput(runCmd)
  108. if err != nil {
  109. t.Fatal(out, err)
  110. }
  111. cleanedContainerID := stripTrailingCharacters(out)
  112. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  113. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  114. t.Fatal(out, err)
  115. }
  116. runCmd = exec.Command(dockerBinary, "exec", cleanedContainerID, "echo", "hello")
  117. out, _, err = runCommandWithOutput(runCmd)
  118. if err != nil {
  119. t.Fatal(out, err)
  120. }
  121. outStr := strings.TrimSpace(out)
  122. if outStr != "hello" {
  123. t.Errorf("container should've printed hello, instead printed %q", outStr)
  124. }
  125. deleteAllContainers()
  126. logDone("exec - exec running container after container restart")
  127. }
  128. func TestExecAfterDaemonRestart(t *testing.T) {
  129. d := NewDaemon(t)
  130. if err := d.StartWithBusybox(); err != nil {
  131. t.Fatalf("Could not start daemon with busybox: %v", err)
  132. }
  133. defer d.Stop()
  134. if out, err := d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
  135. t.Fatalf("Could not run top: err=%v\n%s", err, out)
  136. }
  137. if err := d.Restart(); err != nil {
  138. t.Fatalf("Could not restart daemon: %v", err)
  139. }
  140. if out, err := d.Cmd("start", "top"); err != nil {
  141. t.Fatalf("Could not start top after daemon restart: err=%v\n%s", err, out)
  142. }
  143. out, err := d.Cmd("exec", "top", "echo", "hello")
  144. if err != nil {
  145. t.Fatalf("Could not exec on container top: err=%v\n%s", err, out)
  146. }
  147. outStr := strings.TrimSpace(string(out))
  148. if outStr != "hello" {
  149. t.Errorf("container should've printed hello, instead printed %q", outStr)
  150. }
  151. logDone("exec - exec running container after daemon restart")
  152. }
  153. // Regresssion test for #9155, #9044
  154. func TestExecEnv(t *testing.T) {
  155. defer deleteAllContainers()
  156. runCmd := exec.Command(dockerBinary, "run",
  157. "-e", "LALA=value1",
  158. "-e", "LALA=value2",
  159. "-d", "--name", "testing", "busybox", "top")
  160. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  161. t.Fatal(out, err)
  162. }
  163. execCmd := exec.Command(dockerBinary, "exec", "testing", "env")
  164. out, _, err := runCommandWithOutput(execCmd)
  165. if err != nil {
  166. t.Fatal(out, err)
  167. }
  168. if strings.Contains(out, "LALA=value1") ||
  169. !strings.Contains(out, "LALA=value2") ||
  170. !strings.Contains(out, "HOME=/root") {
  171. t.Errorf("exec env(%q), expect %q, %q", out, "LALA=value2", "HOME=/root")
  172. }
  173. logDone("exec - exec inherits correct env")
  174. }
  175. func TestExecExitStatus(t *testing.T) {
  176. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
  177. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  178. t.Fatal(out, err)
  179. }
  180. // Test normal (non-detached) case first
  181. cmd := exec.Command(dockerBinary, "exec", "top", "sh", "-c", "exit 23")
  182. ec, _ := runCommand(cmd)
  183. if ec != 23 {
  184. t.Fatalf("Should have had an ExitCode of 23, not: %d", ec)
  185. }
  186. logDone("exec - exec non-zero ExitStatus")
  187. }