docker_cli_exec_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. // +build !test_no_exec
  2. package main
  3. import (
  4. "bufio"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "os/exec"
  9. "reflect"
  10. "runtime"
  11. "sort"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/docker/docker/pkg/integration/checker"
  16. icmd "github.com/docker/docker/pkg/integration/cmd"
  17. "github.com/go-check/check"
  18. )
  19. func (s *DockerSuite) TestExec(c *check.C) {
  20. testRequires(c, DaemonIsLinux)
  21. out, _ := dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && top")
  22. c.Assert(waitRun(strings.TrimSpace(out)), check.IsNil)
  23. out, _ = dockerCmd(c, "exec", "testing", "cat", "/tmp/file")
  24. out = strings.Trim(out, "\r\n")
  25. c.Assert(out, checker.Equals, "test")
  26. }
  27. func (s *DockerSuite) TestExecInteractive(c *check.C) {
  28. testRequires(c, DaemonIsLinux)
  29. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && top")
  30. execCmd := exec.Command(dockerBinary, "exec", "-i", "testing", "sh")
  31. stdin, err := execCmd.StdinPipe()
  32. c.Assert(err, checker.IsNil)
  33. stdout, err := execCmd.StdoutPipe()
  34. c.Assert(err, checker.IsNil)
  35. err = execCmd.Start()
  36. c.Assert(err, checker.IsNil)
  37. _, err = stdin.Write([]byte("cat /tmp/file\n"))
  38. c.Assert(err, checker.IsNil)
  39. r := bufio.NewReader(stdout)
  40. line, err := r.ReadString('\n')
  41. c.Assert(err, checker.IsNil)
  42. line = strings.TrimSpace(line)
  43. c.Assert(line, checker.Equals, "test")
  44. err = stdin.Close()
  45. c.Assert(err, checker.IsNil)
  46. errChan := make(chan error)
  47. go func() {
  48. errChan <- execCmd.Wait()
  49. close(errChan)
  50. }()
  51. select {
  52. case err := <-errChan:
  53. c.Assert(err, checker.IsNil)
  54. case <-time.After(1 * time.Second):
  55. c.Fatal("docker exec failed to exit on stdin close")
  56. }
  57. }
  58. func (s *DockerSuite) TestExecAfterContainerRestart(c *check.C) {
  59. out, _ := runSleepingContainer(c)
  60. cleanedContainerID := strings.TrimSpace(out)
  61. c.Assert(waitRun(cleanedContainerID), check.IsNil)
  62. dockerCmd(c, "restart", cleanedContainerID)
  63. c.Assert(waitRun(cleanedContainerID), check.IsNil)
  64. out, _ = dockerCmd(c, "exec", cleanedContainerID, "echo", "hello")
  65. outStr := strings.TrimSpace(out)
  66. c.Assert(outStr, checker.Equals, "hello")
  67. }
  68. func (s *DockerDaemonSuite) TestExecAfterDaemonRestart(c *check.C) {
  69. // TODO Windows CI: Requires a little work to get this ported.
  70. testRequires(c, DaemonIsLinux, SameHostDaemon)
  71. s.d.StartWithBusybox(c)
  72. out, err := s.d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top")
  73. c.Assert(err, checker.IsNil, check.Commentf("Could not run top: %s", out))
  74. s.d.Restart(c)
  75. out, err = s.d.Cmd("start", "top")
  76. c.Assert(err, checker.IsNil, check.Commentf("Could not start top after daemon restart: %s", out))
  77. out, err = s.d.Cmd("exec", "top", "echo", "hello")
  78. c.Assert(err, checker.IsNil, check.Commentf("Could not exec on container top: %s", out))
  79. outStr := strings.TrimSpace(string(out))
  80. c.Assert(outStr, checker.Equals, "hello")
  81. }
  82. // Regression test for #9155, #9044
  83. func (s *DockerSuite) TestExecEnv(c *check.C) {
  84. // TODO Windows CI: This one is interesting and may just end up being a feature
  85. // difference between Windows and Linux. On Windows, the environment is passed
  86. // into the process that is launched, not into the machine environment. Hence
  87. // a subsequent exec will not have LALA set/
  88. testRequires(c, DaemonIsLinux)
  89. runSleepingContainer(c, "-e", "LALA=value1", "-e", "LALA=value2", "-d", "--name", "testing")
  90. c.Assert(waitRun("testing"), check.IsNil)
  91. out, _ := dockerCmd(c, "exec", "testing", "env")
  92. c.Assert(out, checker.Not(checker.Contains), "LALA=value1")
  93. c.Assert(out, checker.Contains, "LALA=value2")
  94. c.Assert(out, checker.Contains, "HOME=/root")
  95. }
  96. func (s *DockerSuite) TestExecSetEnv(c *check.C) {
  97. testRequires(c, DaemonIsLinux)
  98. runSleepingContainer(c, "-e", "HOME=/root", "-d", "--name", "testing")
  99. c.Assert(waitRun("testing"), check.IsNil)
  100. out, _ := dockerCmd(c, "exec", "-e", "HOME=/another", "-e", "ABC=xyz", "testing", "env")
  101. c.Assert(out, checker.Not(checker.Contains), "HOME=/root")
  102. c.Assert(out, checker.Contains, "HOME=/another")
  103. c.Assert(out, checker.Contains, "ABC=xyz")
  104. }
  105. func (s *DockerSuite) TestExecExitStatus(c *check.C) {
  106. runSleepingContainer(c, "-d", "--name", "top")
  107. result := icmd.RunCommand(dockerBinary, "exec", "top", "sh", "-c", "exit 23")
  108. c.Assert(result, icmd.Matches, icmd.Expected{ExitCode: 23, Error: "exit status 23"})
  109. }
  110. func (s *DockerSuite) TestExecPausedContainer(c *check.C) {
  111. testRequires(c, IsPausable)
  112. defer unpauseAllContainers(c)
  113. out, _ := runSleepingContainer(c, "-d", "--name", "testing")
  114. ContainerID := strings.TrimSpace(out)
  115. dockerCmd(c, "pause", "testing")
  116. out, _, err := dockerCmdWithError("exec", "-i", "-t", ContainerID, "echo", "hello")
  117. c.Assert(err, checker.NotNil, check.Commentf("container should fail to exec new conmmand if it is paused"))
  118. expected := ContainerID + " is paused, unpause the container before exec"
  119. c.Assert(out, checker.Contains, expected, check.Commentf("container should not exec new command if it is paused"))
  120. }
  121. // regression test for #9476
  122. func (s *DockerSuite) TestExecTTYCloseStdin(c *check.C) {
  123. // TODO Windows CI: This requires some work to port to Windows.
  124. testRequires(c, DaemonIsLinux)
  125. dockerCmd(c, "run", "-d", "-it", "--name", "exec_tty_stdin", "busybox")
  126. cmd := exec.Command(dockerBinary, "exec", "-i", "exec_tty_stdin", "cat")
  127. stdinRw, err := cmd.StdinPipe()
  128. c.Assert(err, checker.IsNil)
  129. stdinRw.Write([]byte("test"))
  130. stdinRw.Close()
  131. out, _, err := runCommandWithOutput(cmd)
  132. c.Assert(err, checker.IsNil, check.Commentf(out))
  133. out, _ = dockerCmd(c, "top", "exec_tty_stdin")
  134. outArr := strings.Split(out, "\n")
  135. c.Assert(len(outArr), checker.LessOrEqualThan, 3, check.Commentf("exec process left running"))
  136. c.Assert(out, checker.Not(checker.Contains), "nsenter-exec")
  137. }
  138. func (s *DockerSuite) TestExecTTYWithoutStdin(c *check.C) {
  139. out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
  140. id := strings.TrimSpace(out)
  141. c.Assert(waitRun(id), checker.IsNil)
  142. errChan := make(chan error)
  143. go func() {
  144. defer close(errChan)
  145. cmd := exec.Command(dockerBinary, "exec", "-ti", id, "true")
  146. if _, err := cmd.StdinPipe(); err != nil {
  147. errChan <- err
  148. return
  149. }
  150. expected := "the input device is not a TTY"
  151. if runtime.GOOS == "windows" {
  152. expected += ". If you are using mintty, try prefixing the command with 'winpty'"
  153. }
  154. if out, _, err := runCommandWithOutput(cmd); err == nil {
  155. errChan <- fmt.Errorf("exec should have failed")
  156. return
  157. } else if !strings.Contains(out, expected) {
  158. errChan <- fmt.Errorf("exec failed with error %q: expected %q", out, expected)
  159. return
  160. }
  161. }()
  162. select {
  163. case err := <-errChan:
  164. c.Assert(err, check.IsNil)
  165. case <-time.After(3 * time.Second):
  166. c.Fatal("exec is running but should have failed")
  167. }
  168. }
  169. // FIXME(vdemeester) this should be a unit tests on cli/command/container package
  170. func (s *DockerSuite) TestExecParseError(c *check.C) {
  171. // TODO Windows CI: Requires some extra work. Consider copying the
  172. // runSleepingContainer helper to have an exec version.
  173. testRequires(c, DaemonIsLinux)
  174. dockerCmd(c, "run", "-d", "--name", "top", "busybox", "top")
  175. // Test normal (non-detached) case first
  176. icmd.RunCommand(dockerBinary, "exec", "top").Assert(c, icmd.Expected{
  177. ExitCode: 1,
  178. Error: "exit status 1",
  179. Err: "See 'docker exec --help'",
  180. })
  181. }
  182. func (s *DockerSuite) TestExecStopNotHanging(c *check.C) {
  183. // TODO Windows CI: Requires some extra work. Consider copying the
  184. // runSleepingContainer helper to have an exec version.
  185. testRequires(c, DaemonIsLinux)
  186. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
  187. err := exec.Command(dockerBinary, "exec", "testing", "top").Start()
  188. c.Assert(err, checker.IsNil)
  189. type dstop struct {
  190. out []byte
  191. err error
  192. }
  193. ch := make(chan dstop)
  194. go func() {
  195. out, err := exec.Command(dockerBinary, "stop", "testing").CombinedOutput()
  196. ch <- dstop{out, err}
  197. close(ch)
  198. }()
  199. select {
  200. case <-time.After(3 * time.Second):
  201. c.Fatal("Container stop timed out")
  202. case s := <-ch:
  203. c.Assert(s.err, check.IsNil)
  204. }
  205. }
  206. func (s *DockerSuite) TestExecCgroup(c *check.C) {
  207. // Not applicable on Windows - using Linux specific functionality
  208. testRequires(c, NotUserNamespace)
  209. testRequires(c, DaemonIsLinux)
  210. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
  211. out, _ := dockerCmd(c, "exec", "testing", "cat", "/proc/1/cgroup")
  212. containerCgroups := sort.StringSlice(strings.Split(out, "\n"))
  213. var wg sync.WaitGroup
  214. var mu sync.Mutex
  215. execCgroups := []sort.StringSlice{}
  216. errChan := make(chan error)
  217. // exec a few times concurrently to get consistent failure
  218. for i := 0; i < 5; i++ {
  219. wg.Add(1)
  220. go func() {
  221. out, _, err := dockerCmdWithError("exec", "testing", "cat", "/proc/self/cgroup")
  222. if err != nil {
  223. errChan <- err
  224. return
  225. }
  226. cg := sort.StringSlice(strings.Split(out, "\n"))
  227. mu.Lock()
  228. execCgroups = append(execCgroups, cg)
  229. mu.Unlock()
  230. wg.Done()
  231. }()
  232. }
  233. wg.Wait()
  234. close(errChan)
  235. for err := range errChan {
  236. c.Assert(err, checker.IsNil)
  237. }
  238. for _, cg := range execCgroups {
  239. if !reflect.DeepEqual(cg, containerCgroups) {
  240. fmt.Println("exec cgroups:")
  241. for _, name := range cg {
  242. fmt.Printf(" %s\n", name)
  243. }
  244. fmt.Println("container cgroups:")
  245. for _, name := range containerCgroups {
  246. fmt.Printf(" %s\n", name)
  247. }
  248. c.Fatal("cgroups mismatched")
  249. }
  250. }
  251. }
  252. func (s *DockerSuite) TestExecInspectID(c *check.C) {
  253. out, _ := runSleepingContainer(c, "-d")
  254. id := strings.TrimSuffix(out, "\n")
  255. out = inspectField(c, id, "ExecIDs")
  256. c.Assert(out, checker.Equals, "[]", check.Commentf("ExecIDs should be empty, got: %s", out))
  257. // Start an exec, have it block waiting so we can do some checking
  258. cmd := exec.Command(dockerBinary, "exec", id, "sh", "-c",
  259. "while ! test -e /execid1; do sleep 1; done")
  260. err := cmd.Start()
  261. c.Assert(err, checker.IsNil, check.Commentf("failed to start the exec cmd"))
  262. // Give the exec 10 chances/seconds to start then give up and stop the test
  263. tries := 10
  264. for i := 0; i < tries; i++ {
  265. // Since its still running we should see exec as part of the container
  266. out = strings.TrimSpace(inspectField(c, id, "ExecIDs"))
  267. if out != "[]" && out != "<no value>" {
  268. break
  269. }
  270. c.Assert(i+1, checker.Not(checker.Equals), tries, check.Commentf("ExecIDs still empty after 10 second"))
  271. time.Sleep(1 * time.Second)
  272. }
  273. // Save execID for later
  274. execID, err := inspectFilter(id, "index .ExecIDs 0")
  275. c.Assert(err, checker.IsNil, check.Commentf("failed to get the exec id"))
  276. // End the exec by creating the missing file
  277. err = exec.Command(dockerBinary, "exec", id,
  278. "sh", "-c", "touch /execid1").Run()
  279. c.Assert(err, checker.IsNil, check.Commentf("failed to run the 2nd exec cmd"))
  280. // Wait for 1st exec to complete
  281. cmd.Wait()
  282. // Give the exec 10 chances/seconds to stop then give up and stop the test
  283. for i := 0; i < tries; i++ {
  284. // Since its still running we should see exec as part of the container
  285. out = strings.TrimSpace(inspectField(c, id, "ExecIDs"))
  286. if out == "[]" {
  287. break
  288. }
  289. c.Assert(i+1, checker.Not(checker.Equals), tries, check.Commentf("ExecIDs still not empty after 10 second"))
  290. time.Sleep(1 * time.Second)
  291. }
  292. // But we should still be able to query the execID
  293. sc, body, err := sockRequest("GET", "/exec/"+execID+"/json", nil)
  294. c.Assert(sc, checker.Equals, http.StatusOK, check.Commentf("received status != 200 OK: %d\n%s", sc, body))
  295. // Now delete the container and then an 'inspect' on the exec should
  296. // result in a 404 (not 'container not running')
  297. out, ec := dockerCmd(c, "rm", "-f", id)
  298. c.Assert(ec, checker.Equals, 0, check.Commentf("error removing container: %s", out))
  299. sc, body, err = sockRequest("GET", "/exec/"+execID+"/json", nil)
  300. c.Assert(sc, checker.Equals, http.StatusNotFound, check.Commentf("received status != 404: %d\n%s", sc, body))
  301. }
  302. func (s *DockerSuite) TestLinksPingLinkedContainersOnRename(c *check.C) {
  303. // Problematic on Windows as Windows does not support links
  304. testRequires(c, DaemonIsLinux)
  305. var out string
  306. out, _ = dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  307. idA := strings.TrimSpace(out)
  308. c.Assert(idA, checker.Not(checker.Equals), "", check.Commentf("%s, id should not be nil", out))
  309. out, _ = dockerCmd(c, "run", "-d", "--link", "container1:alias1", "--name", "container2", "busybox", "top")
  310. idB := strings.TrimSpace(out)
  311. c.Assert(idB, checker.Not(checker.Equals), "", check.Commentf("%s, id should not be nil", out))
  312. dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  313. dockerCmd(c, "rename", "container1", "container_new")
  314. dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  315. }
  316. func (s *DockerSuite) TestRunMutableNetworkFiles(c *check.C) {
  317. // Not applicable on Windows to Windows CI.
  318. testRequires(c, SameHostDaemon, DaemonIsLinux)
  319. for _, fn := range []string{"resolv.conf", "hosts"} {
  320. deleteAllContainers(c)
  321. content, err := runCommandAndReadContainerFile(fn, exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s && top", fn)))
  322. c.Assert(err, checker.IsNil)
  323. c.Assert(strings.TrimSpace(string(content)), checker.Equals, "success", check.Commentf("Content was not what was modified in the container", string(content)))
  324. out, _ := dockerCmd(c, "run", "-d", "--name", "c2", "busybox", "top")
  325. contID := strings.TrimSpace(out)
  326. netFilePath := containerStorageFile(contID, fn)
  327. f, err := os.OpenFile(netFilePath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644)
  328. c.Assert(err, checker.IsNil)
  329. if _, err := f.Seek(0, 0); err != nil {
  330. f.Close()
  331. c.Fatal(err)
  332. }
  333. if err := f.Truncate(0); err != nil {
  334. f.Close()
  335. c.Fatal(err)
  336. }
  337. if _, err := f.Write([]byte("success2\n")); err != nil {
  338. f.Close()
  339. c.Fatal(err)
  340. }
  341. f.Close()
  342. res, _ := dockerCmd(c, "exec", contID, "cat", "/etc/"+fn)
  343. c.Assert(res, checker.Equals, "success2\n")
  344. }
  345. }
  346. func (s *DockerSuite) TestExecWithUser(c *check.C) {
  347. // TODO Windows CI: This may be fixable in the future once Windows
  348. // supports users
  349. testRequires(c, DaemonIsLinux)
  350. dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
  351. out, _ := dockerCmd(c, "exec", "-u", "1", "parent", "id")
  352. c.Assert(out, checker.Contains, "uid=1(daemon) gid=1(daemon)")
  353. out, _ = dockerCmd(c, "exec", "-u", "root", "parent", "id")
  354. c.Assert(out, checker.Contains, "uid=0(root) gid=0(root)", check.Commentf("exec with user by id expected daemon user got %s", out))
  355. }
  356. func (s *DockerSuite) TestExecWithPrivileged(c *check.C) {
  357. // Not applicable on Windows
  358. testRequires(c, DaemonIsLinux, NotUserNamespace)
  359. // Start main loop which attempts mknod repeatedly
  360. dockerCmd(c, "run", "-d", "--name", "parent", "--cap-drop=ALL", "busybox", "sh", "-c", `while (true); do if [ -e /exec_priv ]; then cat /exec_priv && mknod /tmp/sda b 8 0 && echo "Success"; else echo "Privileged exec has not run yet"; fi; usleep 10000; done`)
  361. // Check exec mknod doesn't work
  362. cmd := exec.Command(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdb b 8 16")
  363. out, _, err := runCommandWithOutput(cmd)
  364. c.Assert(err, checker.NotNil, check.Commentf("exec mknod in --cap-drop=ALL container without --privileged should fail"))
  365. c.Assert(out, checker.Contains, "Operation not permitted", check.Commentf("exec mknod in --cap-drop=ALL container without --privileged should fail"))
  366. // Check exec mknod does work with --privileged
  367. cmd = exec.Command(dockerBinary, "exec", "--privileged", "parent", "sh", "-c", `echo "Running exec --privileged" > /exec_priv && mknod /tmp/sdb b 8 16 && usleep 50000 && echo "Finished exec --privileged" > /exec_priv && echo ok`)
  368. out, _, err = runCommandWithOutput(cmd)
  369. c.Assert(err, checker.IsNil)
  370. actual := strings.TrimSpace(out)
  371. c.Assert(actual, checker.Equals, "ok", check.Commentf("exec mknod in --cap-drop=ALL container with --privileged failed, output: %q", out))
  372. // Check subsequent unprivileged exec cannot mknod
  373. cmd = exec.Command(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdc b 8 32")
  374. out, _, err = runCommandWithOutput(cmd)
  375. c.Assert(err, checker.NotNil, check.Commentf("repeating exec mknod in --cap-drop=ALL container after --privileged without --privileged should fail"))
  376. c.Assert(out, checker.Contains, "Operation not permitted", check.Commentf("repeating exec mknod in --cap-drop=ALL container after --privileged without --privileged should fail"))
  377. // Confirm at no point was mknod allowed
  378. logCmd := exec.Command(dockerBinary, "logs", "parent")
  379. out, _, err = runCommandWithOutput(logCmd)
  380. c.Assert(err, checker.IsNil)
  381. c.Assert(out, checker.Not(checker.Contains), "Success")
  382. }
  383. func (s *DockerSuite) TestExecWithImageUser(c *check.C) {
  384. // Not applicable on Windows
  385. testRequires(c, DaemonIsLinux)
  386. name := "testbuilduser"
  387. _, err := buildImage(name,
  388. `FROM busybox
  389. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  390. USER dockerio`,
  391. true)
  392. c.Assert(err, checker.IsNil)
  393. dockerCmd(c, "run", "-d", "--name", "dockerioexec", name, "top")
  394. out, _ := dockerCmd(c, "exec", "dockerioexec", "whoami")
  395. c.Assert(out, checker.Contains, "dockerio", check.Commentf("exec with user by id expected dockerio user got %s", out))
  396. }
  397. func (s *DockerSuite) TestExecOnReadonlyContainer(c *check.C) {
  398. // Windows does not support read-only
  399. // --read-only + userns has remount issues
  400. testRequires(c, DaemonIsLinux, NotUserNamespace)
  401. dockerCmd(c, "run", "-d", "--read-only", "--name", "parent", "busybox", "top")
  402. dockerCmd(c, "exec", "parent", "true")
  403. }
  404. func (s *DockerSuite) TestExecUlimits(c *check.C) {
  405. testRequires(c, DaemonIsLinux)
  406. name := "testexeculimits"
  407. runSleepingContainer(c, "-d", "--ulimit", "nproc=21", "--name", name)
  408. c.Assert(waitRun(name), checker.IsNil)
  409. out, _, err := dockerCmdWithError("exec", name, "sh", "-c", "ulimit -p")
  410. c.Assert(err, checker.IsNil)
  411. c.Assert(strings.TrimSpace(out), checker.Equals, "21")
  412. }
  413. // #15750
  414. func (s *DockerSuite) TestExecStartFails(c *check.C) {
  415. // TODO Windows CI. This test should be portable. Figure out why it fails
  416. // currently.
  417. testRequires(c, DaemonIsLinux)
  418. name := "exec-15750"
  419. runSleepingContainer(c, "-d", "--name", name)
  420. c.Assert(waitRun(name), checker.IsNil)
  421. out, _, err := dockerCmdWithError("exec", name, "no-such-cmd")
  422. c.Assert(err, checker.NotNil, check.Commentf(out))
  423. c.Assert(out, checker.Contains, "executable file not found")
  424. }
  425. // Fix regression in https://github.com/docker/docker/pull/26461#issuecomment-250287297
  426. func (s *DockerSuite) TestExecWindowsPathNotWiped(c *check.C) {
  427. testRequires(c, DaemonIsWindows)
  428. out, _ := dockerCmd(c, "run", "-d", "--name", "testing", minimalBaseImage(), "powershell", "start-sleep", "60")
  429. c.Assert(waitRun(strings.TrimSpace(out)), check.IsNil)
  430. out, _ = dockerCmd(c, "exec", "testing", "powershell", "write-host", "$env:PATH")
  431. out = strings.ToLower(strings.Trim(out, "\r\n"))
  432. c.Assert(out, checker.Contains, `windowspowershell\v1.0`)
  433. }
  434. func (s *DockerSuite) TestExecEnvLinksHost(c *check.C) {
  435. testRequires(c, DaemonIsLinux)
  436. runSleepingContainer(c, "-d", "--name", "foo")
  437. runSleepingContainer(c, "-d", "--link", "foo:db", "--hostname", "myhost", "--name", "bar")
  438. out, _ := dockerCmd(c, "exec", "bar", "env")
  439. c.Assert(out, checker.Contains, "HOSTNAME=myhost")
  440. c.Assert(out, checker.Contains, "DB_NAME=/bar/db")
  441. }
  442. func (s *DockerSuite) TestExecWindowsOpenHandles(c *check.C) {
  443. testRequires(c, DaemonIsWindows)
  444. runSleepingContainer(c, "-d", "--name", "test")
  445. exec := make(chan bool)
  446. go func() {
  447. dockerCmd(c, "exec", "test", "cmd", "/c", "start sleep 10")
  448. exec <- true
  449. }()
  450. for {
  451. top := make(chan string)
  452. var out string
  453. go func() {
  454. out, _ := dockerCmd(c, "top", "test")
  455. top <- out
  456. }()
  457. select {
  458. case <-time.After(time.Second * 5):
  459. c.Error("timed out waiting for top while exec is exiting")
  460. case out = <-top:
  461. break
  462. }
  463. if strings.Count(out, "busybox.exe") == 2 && !strings.Contains(out, "cmd.exe") {
  464. // The initial exec process (cmd.exe) has exited, and both sleeps are currently running
  465. break
  466. }
  467. time.Sleep(1 * time.Second)
  468. }
  469. inspect := make(chan bool)
  470. go func() {
  471. dockerCmd(c, "inspect", "test")
  472. inspect <- true
  473. }()
  474. select {
  475. case <-time.After(time.Second * 5):
  476. c.Error("timed out waiting for inspect while exec is exiting")
  477. case <-inspect:
  478. break
  479. }
  480. // Ensure the background sleep is still running
  481. out, _ := dockerCmd(c, "top", "test")
  482. c.Assert(strings.Count(out, "busybox.exe"), checker.Equals, 2)
  483. // The exec should exit when the background sleep exits
  484. select {
  485. case <-time.After(time.Second * 15):
  486. c.Error("timed out waiting for async exec to exit")
  487. case <-exec:
  488. // Ensure the background sleep has actually exited
  489. out, _ := dockerCmd(c, "top", "test")
  490. c.Assert(strings.Count(out, "busybox.exe"), checker.Equals, 1)
  491. break
  492. }
  493. }