docker_cli_exec_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "os/exec"
  7. "reflect"
  8. "sort"
  9. "strings"
  10. "sync"
  11. "testing"
  12. "time"
  13. )
  14. func TestExec(t *testing.T) {
  15. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100")
  16. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  17. t.Fatal(out, err)
  18. }
  19. execCmd := exec.Command(dockerBinary, "exec", "testing", "cat", "/tmp/file")
  20. out, _, err := runCommandWithOutput(execCmd)
  21. if err != nil {
  22. t.Fatal(out, err)
  23. }
  24. out = strings.Trim(out, "\r\n")
  25. if expected := "test"; out != expected {
  26. t.Errorf("container exec should've printed %q but printed %q", expected, out)
  27. }
  28. deleteAllContainers()
  29. logDone("exec - basic test")
  30. }
  31. func TestExecInteractiveStdinClose(t *testing.T) {
  32. defer deleteAllContainers()
  33. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "busybox", "/bin/cat"))
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. contId := strings.TrimSpace(out)
  38. returnchan := make(chan struct{})
  39. go func() {
  40. var err error
  41. cmd := exec.Command(dockerBinary, "exec", "-i", contId, "/bin/ls", "/")
  42. cmd.Stdin = os.Stdin
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. out, err := cmd.CombinedOutput()
  47. if err != nil {
  48. t.Fatal(err, out)
  49. }
  50. if string(out) == "" {
  51. t.Fatalf("Output was empty, likely blocked by standard input")
  52. }
  53. returnchan <- struct{}{}
  54. }()
  55. select {
  56. case <-returnchan:
  57. case <-time.After(10 * time.Second):
  58. t.Fatal("timed out running docker exec")
  59. }
  60. logDone("exec - interactive mode closes stdin after execution")
  61. }
  62. func TestExecInteractive(t *testing.T) {
  63. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100")
  64. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  65. t.Fatal(out, err)
  66. }
  67. execCmd := exec.Command(dockerBinary, "exec", "-i", "testing", "sh")
  68. stdin, err := execCmd.StdinPipe()
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. stdout, err := execCmd.StdoutPipe()
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. if err := execCmd.Start(); err != nil {
  77. t.Fatal(err)
  78. }
  79. if _, err := stdin.Write([]byte("cat /tmp/file\n")); err != nil {
  80. t.Fatal(err)
  81. }
  82. r := bufio.NewReader(stdout)
  83. line, err := r.ReadString('\n')
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. line = strings.TrimSpace(line)
  88. if line != "test" {
  89. t.Fatalf("Output should be 'test', got '%q'", line)
  90. }
  91. if err := stdin.Close(); err != nil {
  92. t.Fatal(err)
  93. }
  94. finish := make(chan struct{})
  95. go func() {
  96. if err := execCmd.Wait(); err != nil {
  97. t.Fatal(err)
  98. }
  99. close(finish)
  100. }()
  101. select {
  102. case <-finish:
  103. case <-time.After(1 * time.Second):
  104. t.Fatal("docker exec failed to exit on stdin close")
  105. }
  106. deleteAllContainers()
  107. logDone("exec - Interactive test")
  108. }
  109. func TestExecAfterContainerRestart(t *testing.T) {
  110. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  111. out, _, err := runCommandWithOutput(runCmd)
  112. if err != nil {
  113. t.Fatal(out, err)
  114. }
  115. cleanedContainerID := stripTrailingCharacters(out)
  116. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  117. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  118. t.Fatal(out, err)
  119. }
  120. runCmd = exec.Command(dockerBinary, "exec", cleanedContainerID, "echo", "hello")
  121. out, _, err = runCommandWithOutput(runCmd)
  122. if err != nil {
  123. t.Fatal(out, err)
  124. }
  125. outStr := strings.TrimSpace(out)
  126. if outStr != "hello" {
  127. t.Errorf("container should've printed hello, instead printed %q", outStr)
  128. }
  129. deleteAllContainers()
  130. logDone("exec - exec running container after container restart")
  131. }
  132. func TestExecAfterDaemonRestart(t *testing.T) {
  133. d := NewDaemon(t)
  134. if err := d.StartWithBusybox(); err != nil {
  135. t.Fatalf("Could not start daemon with busybox: %v", err)
  136. }
  137. defer d.Stop()
  138. if out, err := d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
  139. t.Fatalf("Could not run top: err=%v\n%s", err, out)
  140. }
  141. if err := d.Restart(); err != nil {
  142. t.Fatalf("Could not restart daemon: %v", err)
  143. }
  144. if out, err := d.Cmd("start", "top"); err != nil {
  145. t.Fatalf("Could not start top after daemon restart: err=%v\n%s", err, out)
  146. }
  147. out, err := d.Cmd("exec", "top", "echo", "hello")
  148. if err != nil {
  149. t.Fatalf("Could not exec on container top: err=%v\n%s", err, out)
  150. }
  151. outStr := strings.TrimSpace(string(out))
  152. if outStr != "hello" {
  153. t.Errorf("container should've printed hello, instead printed %q", outStr)
  154. }
  155. logDone("exec - exec running container after daemon restart")
  156. }
  157. // Regresssion test for #9155, #9044
  158. func TestExecEnv(t *testing.T) {
  159. defer deleteAllContainers()
  160. runCmd := exec.Command(dockerBinary, "run",
  161. "-e", "LALA=value1",
  162. "-e", "LALA=value2",
  163. "-d", "--name", "testing", "busybox", "top")
  164. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  165. t.Fatal(out, err)
  166. }
  167. execCmd := exec.Command(dockerBinary, "exec", "testing", "env")
  168. out, _, err := runCommandWithOutput(execCmd)
  169. if err != nil {
  170. t.Fatal(out, err)
  171. }
  172. if strings.Contains(out, "LALA=value1") ||
  173. !strings.Contains(out, "LALA=value2") ||
  174. !strings.Contains(out, "HOME=/root") {
  175. t.Errorf("exec env(%q), expect %q, %q", out, "LALA=value2", "HOME=/root")
  176. }
  177. logDone("exec - exec inherits correct env")
  178. }
  179. func TestExecExitStatus(t *testing.T) {
  180. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
  181. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  182. t.Fatal(out, err)
  183. }
  184. // Test normal (non-detached) case first
  185. cmd := exec.Command(dockerBinary, "exec", "top", "sh", "-c", "exit 23")
  186. ec, _ := runCommand(cmd)
  187. if ec != 23 {
  188. t.Fatalf("Should have had an ExitCode of 23, not: %d", ec)
  189. }
  190. logDone("exec - exec non-zero ExitStatus")
  191. }
  192. func TestExecPausedContainer(t *testing.T) {
  193. defer deleteAllContainers()
  194. defer unpauseAllContainers()
  195. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
  196. out, _, err := runCommandWithOutput(runCmd)
  197. if err != nil {
  198. t.Fatal(out, err)
  199. }
  200. ContainerID := stripTrailingCharacters(out)
  201. pausedCmd := exec.Command(dockerBinary, "pause", "testing")
  202. out, _, _, err = runCommandWithStdoutStderr(pausedCmd)
  203. if err != nil {
  204. t.Fatal(out, err)
  205. }
  206. execCmd := exec.Command(dockerBinary, "exec", "-i", "-t", ContainerID, "echo", "hello")
  207. out, _, err = runCommandWithOutput(execCmd)
  208. if err == nil {
  209. t.Fatal("container should fail to exec new command if it is paused")
  210. }
  211. expected := ContainerID + " is paused, unpause the container before exec"
  212. if !strings.Contains(out, expected) {
  213. t.Fatal("container should not exec new command if it is paused")
  214. }
  215. logDone("exec - exec should not exec a pause container")
  216. }
  217. // regression test for #9476
  218. func TestExecTtyCloseStdin(t *testing.T) {
  219. defer deleteAllContainers()
  220. cmd := exec.Command(dockerBinary, "run", "-d", "-it", "--name", "exec_tty_stdin", "busybox")
  221. if out, _, err := runCommandWithOutput(cmd); err != nil {
  222. t.Fatal(out, err)
  223. }
  224. cmd = exec.Command(dockerBinary, "exec", "-i", "exec_tty_stdin", "cat")
  225. stdinRw, err := cmd.StdinPipe()
  226. if err != nil {
  227. t.Fatal(err)
  228. }
  229. stdinRw.Write([]byte("test"))
  230. stdinRw.Close()
  231. if out, _, err := runCommandWithOutput(cmd); err != nil {
  232. t.Fatal(out, err)
  233. }
  234. cmd = exec.Command(dockerBinary, "top", "exec_tty_stdin")
  235. out, _, err := runCommandWithOutput(cmd)
  236. if err != nil {
  237. t.Fatal(out, err)
  238. }
  239. outArr := strings.Split(out, "\n")
  240. if len(outArr) > 3 || strings.Contains(out, "nsenter-exec") {
  241. // This is the really bad part
  242. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "-f", "exec_tty_stdin")); err != nil {
  243. t.Fatal(out, err)
  244. }
  245. t.Fatalf("exec process left running\n\t %s", out)
  246. }
  247. logDone("exec - stdin is closed properly with tty enabled")
  248. }
  249. func TestExecTtyWithoutStdin(t *testing.T) {
  250. defer deleteAllContainers()
  251. cmd := exec.Command(dockerBinary, "run", "-d", "-ti", "busybox")
  252. out, _, err := runCommandWithOutput(cmd)
  253. if err != nil {
  254. t.Fatalf("failed to start container: %v (%v)", out, err)
  255. }
  256. id := strings.TrimSpace(out)
  257. if err := waitRun(id); err != nil {
  258. t.Fatal(err)
  259. }
  260. defer func() {
  261. cmd := exec.Command(dockerBinary, "kill", id)
  262. if out, _, err := runCommandWithOutput(cmd); err != nil {
  263. t.Fatalf("failed to kill container: %v (%v)", out, err)
  264. }
  265. }()
  266. done := make(chan struct{})
  267. go func() {
  268. defer close(done)
  269. cmd := exec.Command(dockerBinary, "exec", "-ti", id, "true")
  270. if _, err := cmd.StdinPipe(); err != nil {
  271. t.Fatal(err)
  272. }
  273. expected := "cannot enable tty mode"
  274. if out, _, err := runCommandWithOutput(cmd); err == nil {
  275. t.Fatal("exec should have failed")
  276. } else if !strings.Contains(out, expected) {
  277. t.Fatalf("exec failed with error %q: expected %q", out, expected)
  278. }
  279. }()
  280. select {
  281. case <-done:
  282. case <-time.After(3 * time.Second):
  283. t.Fatal("exec is running but should have failed")
  284. }
  285. logDone("exec - forbid piped stdin to tty enabled container")
  286. }
  287. func TestExecParseError(t *testing.T) {
  288. defer deleteAllContainers()
  289. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
  290. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  291. t.Fatal(out, err)
  292. }
  293. // Test normal (non-detached) case first
  294. cmd := exec.Command(dockerBinary, "exec", "top")
  295. if _, stderr, code, err := runCommandWithStdoutStderr(cmd); err == nil || !strings.Contains(stderr, "See '"+dockerBinary+" exec --help'") || code == 0 {
  296. t.Fatalf("Should have thrown error & point to help: %s", stderr)
  297. }
  298. logDone("exec - error on parseExec should point to help")
  299. }
  300. func TestExecStopNotHanging(t *testing.T) {
  301. defer deleteAllContainers()
  302. if out, err := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top").CombinedOutput(); err != nil {
  303. t.Fatal(out, err)
  304. }
  305. if err := exec.Command(dockerBinary, "exec", "testing", "top").Start(); err != nil {
  306. t.Fatal(err)
  307. }
  308. wait := make(chan struct{})
  309. go func() {
  310. if out, err := exec.Command(dockerBinary, "stop", "testing").CombinedOutput(); err != nil {
  311. t.Fatal(out, err)
  312. }
  313. close(wait)
  314. }()
  315. select {
  316. case <-time.After(3 * time.Second):
  317. t.Fatal("Container stop timed out")
  318. case <-wait:
  319. }
  320. logDone("exec - container with exec not hanging on stop")
  321. }
  322. func TestExecCgroup(t *testing.T) {
  323. defer deleteAllContainers()
  324. var cmd *exec.Cmd
  325. cmd = exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
  326. _, err := runCommand(cmd)
  327. if err != nil {
  328. t.Fatal(err)
  329. }
  330. cmd = exec.Command(dockerBinary, "exec", "testing", "cat", "/proc/1/cgroup")
  331. out, _, err := runCommandWithOutput(cmd)
  332. if err != nil {
  333. t.Fatal(out, err)
  334. }
  335. containerCgroups := sort.StringSlice(strings.Split(string(out), "\n"))
  336. var wg sync.WaitGroup
  337. var s sync.Mutex
  338. execCgroups := []sort.StringSlice{}
  339. // exec a few times concurrently to get consistent failure
  340. for i := 0; i < 5; i++ {
  341. wg.Add(1)
  342. go func() {
  343. cmd = exec.Command(dockerBinary, "exec", "testing", "cat", "/proc/self/cgroup")
  344. out, _, err := runCommandWithOutput(cmd)
  345. if err != nil {
  346. t.Fatal(out, err)
  347. }
  348. cg := sort.StringSlice(strings.Split(string(out), "\n"))
  349. s.Lock()
  350. execCgroups = append(execCgroups, cg)
  351. s.Unlock()
  352. wg.Done()
  353. }()
  354. }
  355. wg.Wait()
  356. for _, cg := range execCgroups {
  357. if !reflect.DeepEqual(cg, containerCgroups) {
  358. fmt.Println("exec cgroups:")
  359. for _, name := range cg {
  360. fmt.Printf(" %s\n", name)
  361. }
  362. fmt.Println("container cgroups:")
  363. for _, name := range containerCgroups {
  364. fmt.Printf(" %s\n", name)
  365. }
  366. t.Fatal("cgroups mismatched")
  367. }
  368. }
  369. logDone("exec - exec has the container cgroups")
  370. }