docker_cli_exec_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. }
  188. func TestExecPausedContainer(t *testing.T) {
  189. defer deleteAllContainers()
  190. defer unpauseAllContainers()
  191. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
  192. out, _, err := runCommandWithOutput(runCmd)
  193. if err != nil {
  194. t.Fatal(out, err)
  195. }
  196. ContainerID := stripTrailingCharacters(out)
  197. pausedCmd := exec.Command(dockerBinary, "pause", "testing")
  198. out, _, _, err = runCommandWithStdoutStderr(pausedCmd)
  199. if err != nil {
  200. t.Fatal(out, err)
  201. }
  202. execCmd := exec.Command(dockerBinary, "exec", "-i", "-t", ContainerID, "echo", "hello")
  203. out, _, err = runCommandWithOutput(execCmd)
  204. if err == nil {
  205. t.Fatal("container should fail to exec new command if it is paused")
  206. }
  207. expected := ContainerID + " is paused, unpause the container before exec"
  208. if !strings.Contains(out, expected) {
  209. t.Fatal("container should not exec new command if it is paused")
  210. }
  211. logDone("exec - exec should not exec a pause container")
  212. }
  213. // regression test for #9476
  214. func TestExecTtyCloseStdin(t *testing.T) {
  215. defer deleteAllContainers()
  216. cmd := exec.Command(dockerBinary, "run", "-d", "-it", "--name", "exec_tty_stdin", "busybox")
  217. if out, _, err := runCommandWithOutput(cmd); err != nil {
  218. t.Fatal(out, err)
  219. }
  220. cmd = exec.Command(dockerBinary, "exec", "-i", "exec_tty_stdin", "cat")
  221. stdinRw, err := cmd.StdinPipe()
  222. if err != nil {
  223. t.Fatal(err)
  224. }
  225. stdinRw.Write([]byte("test"))
  226. stdinRw.Close()
  227. if out, _, err := runCommandWithOutput(cmd); err != nil {
  228. t.Fatal(out, err)
  229. }
  230. cmd = exec.Command(dockerBinary, "top", "exec_tty_stdin")
  231. out, _, err := runCommandWithOutput(cmd)
  232. if err != nil {
  233. t.Fatal(out, err)
  234. }
  235. outArr := strings.Split(out, "\n")
  236. if len(outArr) > 3 || strings.Contains(out, "nsenter-exec") {
  237. // This is the really bad part
  238. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "-f", "exec_tty_stdin")); err != nil {
  239. t.Fatal(out, err)
  240. }
  241. t.Fatalf("exec process left running\n\t %s", out)
  242. }
  243. logDone("exec - stdin is closed properly with tty enabled")
  244. }
  245. func TestExecTtyWithoutStdin(t *testing.T) {
  246. defer deleteAllContainers()
  247. cmd := exec.Command(dockerBinary, "run", "-d", "-ti", "busybox")
  248. out, _, err := runCommandWithOutput(cmd)
  249. if err != nil {
  250. t.Fatalf("failed to start container: %v (%v)", out, err)
  251. }
  252. id := strings.TrimSpace(out)
  253. if err := waitRun(id); err != nil {
  254. t.Fatal(err)
  255. }
  256. defer func() {
  257. cmd := exec.Command(dockerBinary, "kill", id)
  258. if out, _, err := runCommandWithOutput(cmd); err != nil {
  259. t.Fatalf("failed to kill container: %v (%v)", out, err)
  260. }
  261. }()
  262. done := make(chan struct{})
  263. go func() {
  264. defer close(done)
  265. cmd := exec.Command(dockerBinary, "exec", "-ti", id, "true")
  266. if _, err := cmd.StdinPipe(); err != nil {
  267. t.Fatal(err)
  268. }
  269. expected := "cannot enable tty mode"
  270. if out, _, err := runCommandWithOutput(cmd); err == nil {
  271. t.Fatal("exec should have failed")
  272. } else if !strings.Contains(out, expected) {
  273. t.Fatalf("exec failed with error %q: expected %q", out, expected)
  274. }
  275. }()
  276. select {
  277. case <-done:
  278. case <-time.After(3 * time.Second):
  279. t.Fatal("exec is running but should have failed")
  280. }
  281. logDone("exec - forbid piped stdin to tty enabled container")
  282. }
  283. func TestExecParseError(t *testing.T) {
  284. defer deleteAllContainers()
  285. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
  286. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  287. t.Fatal(out, err)
  288. }
  289. // Test normal (non-detached) case first
  290. cmd := exec.Command(dockerBinary, "exec", "top")
  291. if _, stderr, code, err := runCommandWithStdoutStderr(cmd); err == nil || !strings.Contains(stderr, "See '"+dockerBinary+" exec --help'") || code == 0 {
  292. t.Fatalf("Should have thrown error & point to help: %s", stderr)
  293. }
  294. logDone("exec - error on parseExec should point to help")
  295. }
  296. func TestExecStopNotHanging(t *testing.T) {
  297. defer deleteAllContainers()
  298. if out, err := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top").CombinedOutput(); err != nil {
  299. t.Fatal(out, err)
  300. }
  301. if err := exec.Command(dockerBinary, "exec", "testing", "top").Start(); err != nil {
  302. t.Fatal(err)
  303. }
  304. wait := make(chan struct{})
  305. go func() {
  306. if out, err := exec.Command(dockerBinary, "stop", "testing").CombinedOutput(); err != nil {
  307. t.Fatal(out, err)
  308. }
  309. close(wait)
  310. }()
  311. select {
  312. case <-time.After(3 * time.Second):
  313. t.Fatal("Container stop timed out")
  314. case <-wait:
  315. }
  316. logDone("exec - container with exec not hanging on stop")
  317. }