docker_cli_exec_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // +build !test_no_exec
  2. package main
  3. import (
  4. "bufio"
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "reflect"
  10. "sort"
  11. "strings"
  12. "sync"
  13. "time"
  14. "github.com/go-check/check"
  15. )
  16. func (s *DockerSuite) TestExec(c *check.C) {
  17. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && top")
  18. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  19. c.Fatal(out, err)
  20. }
  21. execCmd := exec.Command(dockerBinary, "exec", "testing", "cat", "/tmp/file")
  22. out, _, err := runCommandWithOutput(execCmd)
  23. if err != nil {
  24. c.Fatal(out, err)
  25. }
  26. out = strings.Trim(out, "\r\n")
  27. if expected := "test"; out != expected {
  28. c.Errorf("container exec should've printed %q but printed %q", expected, out)
  29. }
  30. }
  31. func (s *DockerSuite) TestExecInteractive(c *check.C) {
  32. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && top")
  33. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  34. c.Fatal(out, err)
  35. }
  36. execCmd := exec.Command(dockerBinary, "exec", "-i", "testing", "sh")
  37. stdin, err := execCmd.StdinPipe()
  38. if err != nil {
  39. c.Fatal(err)
  40. }
  41. stdout, err := execCmd.StdoutPipe()
  42. if err != nil {
  43. c.Fatal(err)
  44. }
  45. if err := execCmd.Start(); err != nil {
  46. c.Fatal(err)
  47. }
  48. if _, err := stdin.Write([]byte("cat /tmp/file\n")); err != nil {
  49. c.Fatal(err)
  50. }
  51. r := bufio.NewReader(stdout)
  52. line, err := r.ReadString('\n')
  53. if err != nil {
  54. c.Fatal(err)
  55. }
  56. line = strings.TrimSpace(line)
  57. if line != "test" {
  58. c.Fatalf("Output should be 'test', got '%q'", line)
  59. }
  60. if err := stdin.Close(); err != nil {
  61. c.Fatal(err)
  62. }
  63. errChan := make(chan error)
  64. go func() {
  65. errChan <- execCmd.Wait()
  66. close(errChan)
  67. }()
  68. select {
  69. case err := <-errChan:
  70. c.Assert(err, check.IsNil)
  71. case <-time.After(1 * time.Second):
  72. c.Fatal("docker exec failed to exit on stdin close")
  73. }
  74. }
  75. func (s *DockerSuite) TestExecAfterContainerRestart(c *check.C) {
  76. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  77. out, _, err := runCommandWithOutput(runCmd)
  78. if err != nil {
  79. c.Fatal(out, err)
  80. }
  81. cleanedContainerID := strings.TrimSpace(out)
  82. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  83. if out, _, err = runCommandWithOutput(runCmd); err != nil {
  84. c.Fatal(out, err)
  85. }
  86. runCmd = exec.Command(dockerBinary, "exec", cleanedContainerID, "echo", "hello")
  87. out, _, err = runCommandWithOutput(runCmd)
  88. if err != nil {
  89. c.Fatal(out, err)
  90. }
  91. outStr := strings.TrimSpace(out)
  92. if outStr != "hello" {
  93. c.Errorf("container should've printed hello, instead printed %q", outStr)
  94. }
  95. }
  96. func (s *DockerDaemonSuite) TestExecAfterDaemonRestart(c *check.C) {
  97. testRequires(c, SameHostDaemon)
  98. if err := s.d.StartWithBusybox(); err != nil {
  99. c.Fatalf("Could not start daemon with busybox: %v", err)
  100. }
  101. if out, err := s.d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
  102. c.Fatalf("Could not run top: err=%v\n%s", err, out)
  103. }
  104. if err := s.d.Restart(); err != nil {
  105. c.Fatalf("Could not restart daemon: %v", err)
  106. }
  107. if out, err := s.d.Cmd("start", "top"); err != nil {
  108. c.Fatalf("Could not start top after daemon restart: err=%v\n%s", err, out)
  109. }
  110. out, err := s.d.Cmd("exec", "top", "echo", "hello")
  111. if err != nil {
  112. c.Fatalf("Could not exec on container top: err=%v\n%s", err, out)
  113. }
  114. outStr := strings.TrimSpace(string(out))
  115. if outStr != "hello" {
  116. c.Errorf("container should've printed hello, instead printed %q", outStr)
  117. }
  118. }
  119. // Regression test for #9155, #9044
  120. func (s *DockerSuite) TestExecEnv(c *check.C) {
  121. runCmd := exec.Command(dockerBinary, "run",
  122. "-e", "LALA=value1",
  123. "-e", "LALA=value2",
  124. "-d", "--name", "testing", "busybox", "top")
  125. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  126. c.Fatal(out, err)
  127. }
  128. execCmd := exec.Command(dockerBinary, "exec", "testing", "env")
  129. out, _, err := runCommandWithOutput(execCmd)
  130. if err != nil {
  131. c.Fatal(out, err)
  132. }
  133. if strings.Contains(out, "LALA=value1") ||
  134. !strings.Contains(out, "LALA=value2") ||
  135. !strings.Contains(out, "HOME=/root") {
  136. c.Errorf("exec env(%q), expect %q, %q", out, "LALA=value2", "HOME=/root")
  137. }
  138. }
  139. func (s *DockerSuite) TestExecExitStatus(c *check.C) {
  140. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
  141. if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
  142. c.Fatal(out, err)
  143. }
  144. // Test normal (non-detached) case first
  145. cmd := exec.Command(dockerBinary, "exec", "top", "sh", "-c", "exit 23")
  146. ec, _ := runCommand(cmd)
  147. if ec != 23 {
  148. c.Fatalf("Should have had an ExitCode of 23, not: %d", ec)
  149. }
  150. }
  151. func (s *DockerSuite) TestExecPausedContainer(c *check.C) {
  152. defer unpauseAllContainers()
  153. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
  154. out, _, err := runCommandWithOutput(runCmd)
  155. if err != nil {
  156. c.Fatal(out, err)
  157. }
  158. ContainerID := strings.TrimSpace(out)
  159. pausedCmd := exec.Command(dockerBinary, "pause", "testing")
  160. out, _, _, err = runCommandWithStdoutStderr(pausedCmd)
  161. if err != nil {
  162. c.Fatal(out, err)
  163. }
  164. execCmd := exec.Command(dockerBinary, "exec", "-i", "-t", ContainerID, "echo", "hello")
  165. out, _, err = runCommandWithOutput(execCmd)
  166. if err == nil {
  167. c.Fatal("container should fail to exec new command if it is paused")
  168. }
  169. expected := ContainerID + " is paused, unpause the container before exec"
  170. if !strings.Contains(out, expected) {
  171. c.Fatal("container should not exec new command if it is paused")
  172. }
  173. }
  174. // regression test for #9476
  175. func (s *DockerSuite) TestExecTtyCloseStdin(c *check.C) {
  176. cmd := exec.Command(dockerBinary, "run", "-d", "-it", "--name", "exec_tty_stdin", "busybox")
  177. if out, _, err := runCommandWithOutput(cmd); err != nil {
  178. c.Fatal(out, err)
  179. }
  180. cmd = exec.Command(dockerBinary, "exec", "-i", "exec_tty_stdin", "cat")
  181. stdinRw, err := cmd.StdinPipe()
  182. if err != nil {
  183. c.Fatal(err)
  184. }
  185. stdinRw.Write([]byte("test"))
  186. stdinRw.Close()
  187. if out, _, err := runCommandWithOutput(cmd); err != nil {
  188. c.Fatal(out, err)
  189. }
  190. cmd = exec.Command(dockerBinary, "top", "exec_tty_stdin")
  191. out, _, err := runCommandWithOutput(cmd)
  192. if err != nil {
  193. c.Fatal(out, err)
  194. }
  195. outArr := strings.Split(out, "\n")
  196. if len(outArr) > 3 || strings.Contains(out, "nsenter-exec") {
  197. // This is the really bad part
  198. if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "-f", "exec_tty_stdin")); err != nil {
  199. c.Fatal(out, err)
  200. }
  201. c.Fatalf("exec process left running\n\t %s", out)
  202. }
  203. }
  204. func (s *DockerSuite) TestExecTtyWithoutStdin(c *check.C) {
  205. cmd := exec.Command(dockerBinary, "run", "-d", "-ti", "busybox")
  206. out, _, err := runCommandWithOutput(cmd)
  207. if err != nil {
  208. c.Fatalf("failed to start container: %v (%v)", out, err)
  209. }
  210. id := strings.TrimSpace(out)
  211. if err := waitRun(id); err != nil {
  212. c.Fatal(err)
  213. }
  214. defer func() {
  215. cmd := exec.Command(dockerBinary, "kill", id)
  216. if out, _, err := runCommandWithOutput(cmd); err != nil {
  217. c.Fatalf("failed to kill container: %v (%v)", out, err)
  218. }
  219. }()
  220. errChan := make(chan error)
  221. go func() {
  222. defer close(errChan)
  223. cmd := exec.Command(dockerBinary, "exec", "-ti", id, "true")
  224. if _, err := cmd.StdinPipe(); err != nil {
  225. errChan <- err
  226. return
  227. }
  228. expected := "cannot enable tty mode"
  229. if out, _, err := runCommandWithOutput(cmd); err == nil {
  230. errChan <- fmt.Errorf("exec should have failed")
  231. return
  232. } else if !strings.Contains(out, expected) {
  233. errChan <- fmt.Errorf("exec failed with error %q: expected %q", out, expected)
  234. return
  235. }
  236. }()
  237. select {
  238. case err := <-errChan:
  239. c.Assert(err, check.IsNil)
  240. case <-time.After(3 * time.Second):
  241. c.Fatal("exec is running but should have failed")
  242. }
  243. }
  244. func (s *DockerSuite) TestExecParseError(c *check.C) {
  245. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
  246. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  247. c.Fatal(out, err)
  248. }
  249. // Test normal (non-detached) case first
  250. cmd := exec.Command(dockerBinary, "exec", "top")
  251. if _, stderr, code, err := runCommandWithStdoutStderr(cmd); err == nil || !strings.Contains(stderr, "See '"+dockerBinary+" exec --help'") || code == 0 {
  252. c.Fatalf("Should have thrown error & point to help: %s", stderr)
  253. }
  254. }
  255. func (s *DockerSuite) TestExecStopNotHanging(c *check.C) {
  256. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
  257. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  258. c.Fatal(out, err)
  259. }
  260. if err := exec.Command(dockerBinary, "exec", "testing", "top").Start(); err != nil {
  261. c.Fatal(err)
  262. }
  263. type dstop struct {
  264. out []byte
  265. err error
  266. }
  267. ch := make(chan dstop)
  268. go func() {
  269. out, err := exec.Command(dockerBinary, "stop", "testing").CombinedOutput()
  270. ch <- dstop{out, err}
  271. close(ch)
  272. }()
  273. select {
  274. case <-time.After(3 * time.Second):
  275. c.Fatal("Container stop timed out")
  276. case s := <-ch:
  277. c.Assert(s.err, check.IsNil)
  278. }
  279. }
  280. func (s *DockerSuite) TestExecCgroup(c *check.C) {
  281. var cmd *exec.Cmd
  282. cmd = exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "top")
  283. _, err := runCommand(cmd)
  284. if err != nil {
  285. c.Fatal(err)
  286. }
  287. cmd = exec.Command(dockerBinary, "exec", "testing", "cat", "/proc/1/cgroup")
  288. out, _, err := runCommandWithOutput(cmd)
  289. if err != nil {
  290. c.Fatal(out, err)
  291. }
  292. containerCgroups := sort.StringSlice(strings.Split(string(out), "\n"))
  293. var wg sync.WaitGroup
  294. var mu sync.Mutex
  295. execCgroups := []sort.StringSlice{}
  296. errChan := make(chan error)
  297. // exec a few times concurrently to get consistent failure
  298. for i := 0; i < 5; i++ {
  299. wg.Add(1)
  300. go func() {
  301. cmd := exec.Command(dockerBinary, "exec", "testing", "cat", "/proc/self/cgroup")
  302. out, _, err := runCommandWithOutput(cmd)
  303. if err != nil {
  304. errChan <- err
  305. return
  306. }
  307. cg := sort.StringSlice(strings.Split(string(out), "\n"))
  308. mu.Lock()
  309. execCgroups = append(execCgroups, cg)
  310. mu.Unlock()
  311. wg.Done()
  312. }()
  313. }
  314. wg.Wait()
  315. close(errChan)
  316. for err := range errChan {
  317. c.Assert(err, check.IsNil)
  318. }
  319. for _, cg := range execCgroups {
  320. if !reflect.DeepEqual(cg, containerCgroups) {
  321. fmt.Println("exec cgroups:")
  322. for _, name := range cg {
  323. fmt.Printf(" %s\n", name)
  324. }
  325. fmt.Println("container cgroups:")
  326. for _, name := range containerCgroups {
  327. fmt.Printf(" %s\n", name)
  328. }
  329. c.Fatal("cgroups mismatched")
  330. }
  331. }
  332. }
  333. func (s *DockerSuite) TestInspectExecID(c *check.C) {
  334. out, exitCode, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "busybox", "top"))
  335. if exitCode != 0 || err != nil {
  336. c.Fatalf("failed to run container: %s, %v", out, err)
  337. }
  338. id := strings.TrimSuffix(out, "\n")
  339. out, err = inspectField(id, "ExecIDs")
  340. if err != nil {
  341. c.Fatalf("failed to inspect container: %s, %v", out, err)
  342. }
  343. if out != "[]" {
  344. c.Fatalf("ExecIDs should be empty, got: %s", out)
  345. }
  346. exitCode, err = runCommand(exec.Command(dockerBinary, "exec", "-d", id, "ls", "/"))
  347. if exitCode != 0 || err != nil {
  348. c.Fatalf("failed to exec in container: %s, %v", out, err)
  349. }
  350. out, err = inspectField(id, "ExecIDs")
  351. if err != nil {
  352. c.Fatalf("failed to inspect container: %s, %v", out, err)
  353. }
  354. out = strings.TrimSuffix(out, "\n")
  355. if out == "[]" || out == "<no value>" {
  356. c.Fatalf("ExecIDs should not be empty, got: %s", out)
  357. }
  358. }
  359. func (s *DockerSuite) TestLinksPingLinkedContainersOnRename(c *check.C) {
  360. var out string
  361. out, _ = dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  362. idA := strings.TrimSpace(out)
  363. if idA == "" {
  364. c.Fatal(out, "id should not be nil")
  365. }
  366. out, _ = dockerCmd(c, "run", "-d", "--link", "container1:alias1", "--name", "container2", "busybox", "top")
  367. idB := strings.TrimSpace(out)
  368. if idB == "" {
  369. c.Fatal(out, "id should not be nil")
  370. }
  371. execCmd := exec.Command(dockerBinary, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  372. out, _, err := runCommandWithOutput(execCmd)
  373. if err != nil {
  374. c.Fatal(out, err)
  375. }
  376. dockerCmd(c, "rename", "container1", "container_new")
  377. execCmd = exec.Command(dockerBinary, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  378. out, _, err = runCommandWithOutput(execCmd)
  379. if err != nil {
  380. c.Fatal(out, err)
  381. }
  382. }
  383. func (s *DockerSuite) TestRunExecDir(c *check.C) {
  384. testRequires(c, SameHostDaemon)
  385. cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  386. out, _, err := runCommandWithOutput(cmd)
  387. if err != nil {
  388. c.Fatal(err, out)
  389. }
  390. id := strings.TrimSpace(out)
  391. execDir := filepath.Join(execDriverPath, id)
  392. stateFile := filepath.Join(execDir, "state.json")
  393. {
  394. fi, err := os.Stat(execDir)
  395. if err != nil {
  396. c.Fatal(err)
  397. }
  398. if !fi.IsDir() {
  399. c.Fatalf("%q must be a directory", execDir)
  400. }
  401. fi, err = os.Stat(stateFile)
  402. if err != nil {
  403. c.Fatal(err)
  404. }
  405. }
  406. stopCmd := exec.Command(dockerBinary, "stop", id)
  407. out, _, err = runCommandWithOutput(stopCmd)
  408. if err != nil {
  409. c.Fatal(err, out)
  410. }
  411. {
  412. _, err := os.Stat(execDir)
  413. if err == nil {
  414. c.Fatal(err)
  415. }
  416. if err == nil {
  417. c.Fatalf("Exec directory %q exists for removed container!", execDir)
  418. }
  419. if !os.IsNotExist(err) {
  420. c.Fatalf("Error should be about non-existing, got %s", err)
  421. }
  422. }
  423. startCmd := exec.Command(dockerBinary, "start", id)
  424. out, _, err = runCommandWithOutput(startCmd)
  425. if err != nil {
  426. c.Fatal(err, out)
  427. }
  428. {
  429. fi, err := os.Stat(execDir)
  430. if err != nil {
  431. c.Fatal(err)
  432. }
  433. if !fi.IsDir() {
  434. c.Fatalf("%q must be a directory", execDir)
  435. }
  436. fi, err = os.Stat(stateFile)
  437. if err != nil {
  438. c.Fatal(err)
  439. }
  440. }
  441. rmCmd := exec.Command(dockerBinary, "rm", "-f", id)
  442. out, _, err = runCommandWithOutput(rmCmd)
  443. if err != nil {
  444. c.Fatal(err, out)
  445. }
  446. {
  447. _, err := os.Stat(execDir)
  448. if err == nil {
  449. c.Fatal(err)
  450. }
  451. if err == nil {
  452. c.Fatalf("Exec directory %q is exists for removed container!", execDir)
  453. }
  454. if !os.IsNotExist(err) {
  455. c.Fatalf("Error should be about non-existing, got %s", err)
  456. }
  457. }
  458. }
  459. func (s *DockerSuite) TestRunMutableNetworkFiles(c *check.C) {
  460. testRequires(c, SameHostDaemon)
  461. for _, fn := range []string{"resolv.conf", "hosts"} {
  462. deleteAllContainers()
  463. content, err := runCommandAndReadContainerFile(fn, exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s && top", fn)))
  464. if err != nil {
  465. c.Fatal(err)
  466. }
  467. if strings.TrimSpace(string(content)) != "success" {
  468. c.Fatal("Content was not what was modified in the container", string(content))
  469. }
  470. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--name", "c2", "busybox", "top"))
  471. if err != nil {
  472. c.Fatal(err)
  473. }
  474. contID := strings.TrimSpace(out)
  475. netFilePath := containerStorageFile(contID, fn)
  476. f, err := os.OpenFile(netFilePath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644)
  477. if err != nil {
  478. c.Fatal(err)
  479. }
  480. if _, err := f.Seek(0, 0); err != nil {
  481. f.Close()
  482. c.Fatal(err)
  483. }
  484. if err := f.Truncate(0); err != nil {
  485. f.Close()
  486. c.Fatal(err)
  487. }
  488. if _, err := f.Write([]byte("success2\n")); err != nil {
  489. f.Close()
  490. c.Fatal(err)
  491. }
  492. f.Close()
  493. res, err := exec.Command(dockerBinary, "exec", contID, "cat", "/etc/"+fn).CombinedOutput()
  494. if err != nil {
  495. c.Fatalf("Output: %s, error: %s", res, err)
  496. }
  497. if string(res) != "success2\n" {
  498. c.Fatalf("Expected content of %s: %q, got: %q", fn, "success2\n", res)
  499. }
  500. }
  501. }
  502. func (s *DockerSuite) TestExecWithUser(c *check.C) {
  503. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top")
  504. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  505. c.Fatal(out, err)
  506. }
  507. cmd := exec.Command(dockerBinary, "exec", "-u", "1", "parent", "id")
  508. out, _, err := runCommandWithOutput(cmd)
  509. if err != nil {
  510. c.Fatal(err, out)
  511. }
  512. if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
  513. c.Fatalf("exec with user by id expected daemon user got %s", out)
  514. }
  515. cmd = exec.Command(dockerBinary, "exec", "-u", "root", "parent", "id")
  516. out, _, err = runCommandWithOutput(cmd)
  517. if err != nil {
  518. c.Fatal(err, out)
  519. }
  520. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  521. c.Fatalf("exec with user by root expected root user got %s", out)
  522. }
  523. }