docker_cli_exec_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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. func (s *DockerSuite) TestExecParseError(c *check.C) {
  170. // TODO Windows CI: Requires some extra work. Consider copying the
  171. // runSleepingContainer helper to have an exec version.
  172. testRequires(c, DaemonIsLinux)
  173. dockerCmd(c, "run", "-d", "--name", "top", "busybox", "top")
  174. // Test normal (non-detached) case first
  175. cmd := exec.Command(dockerBinary, "exec", "top")
  176. _, stderr, _, err := runCommandWithStdoutStderr(cmd)
  177. c.Assert(err, checker.NotNil)
  178. c.Assert(stderr, checker.Contains, "See 'docker exec --help'")
  179. }
  180. func (s *DockerSuite) TestExecStopNotHanging(c *check.C) {
  181. // TODO Windows CI: Requires some extra work. Consider copying the
  182. // runSleepingContainer helper to have an exec version.
  183. testRequires(c, DaemonIsLinux)
  184. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
  185. err := exec.Command(dockerBinary, "exec", "testing", "top").Start()
  186. c.Assert(err, checker.IsNil)
  187. type dstop struct {
  188. out []byte
  189. err error
  190. }
  191. ch := make(chan dstop)
  192. go func() {
  193. out, err := exec.Command(dockerBinary, "stop", "testing").CombinedOutput()
  194. ch <- dstop{out, err}
  195. close(ch)
  196. }()
  197. select {
  198. case <-time.After(3 * time.Second):
  199. c.Fatal("Container stop timed out")
  200. case s := <-ch:
  201. c.Assert(s.err, check.IsNil)
  202. }
  203. }
  204. func (s *DockerSuite) TestExecCgroup(c *check.C) {
  205. // Not applicable on Windows - using Linux specific functionality
  206. testRequires(c, NotUserNamespace)
  207. testRequires(c, DaemonIsLinux)
  208. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
  209. out, _ := dockerCmd(c, "exec", "testing", "cat", "/proc/1/cgroup")
  210. containerCgroups := sort.StringSlice(strings.Split(out, "\n"))
  211. var wg sync.WaitGroup
  212. var mu sync.Mutex
  213. execCgroups := []sort.StringSlice{}
  214. errChan := make(chan error)
  215. // exec a few times concurrently to get consistent failure
  216. for i := 0; i < 5; i++ {
  217. wg.Add(1)
  218. go func() {
  219. out, _, err := dockerCmdWithError("exec", "testing", "cat", "/proc/self/cgroup")
  220. if err != nil {
  221. errChan <- err
  222. return
  223. }
  224. cg := sort.StringSlice(strings.Split(out, "\n"))
  225. mu.Lock()
  226. execCgroups = append(execCgroups, cg)
  227. mu.Unlock()
  228. wg.Done()
  229. }()
  230. }
  231. wg.Wait()
  232. close(errChan)
  233. for err := range errChan {
  234. c.Assert(err, checker.IsNil)
  235. }
  236. for _, cg := range execCgroups {
  237. if !reflect.DeepEqual(cg, containerCgroups) {
  238. fmt.Println("exec cgroups:")
  239. for _, name := range cg {
  240. fmt.Printf(" %s\n", name)
  241. }
  242. fmt.Println("container cgroups:")
  243. for _, name := range containerCgroups {
  244. fmt.Printf(" %s\n", name)
  245. }
  246. c.Fatal("cgroups mismatched")
  247. }
  248. }
  249. }
  250. func (s *DockerSuite) TestExecInspectID(c *check.C) {
  251. out, _ := runSleepingContainer(c, "-d")
  252. id := strings.TrimSuffix(out, "\n")
  253. out = inspectField(c, id, "ExecIDs")
  254. c.Assert(out, checker.Equals, "[]", check.Commentf("ExecIDs should be empty, got: %s", out))
  255. // Start an exec, have it block waiting so we can do some checking
  256. cmd := exec.Command(dockerBinary, "exec", id, "sh", "-c",
  257. "while ! test -e /execid1; do sleep 1; done")
  258. err := cmd.Start()
  259. c.Assert(err, checker.IsNil, check.Commentf("failed to start the exec cmd"))
  260. // Give the exec 10 chances/seconds to start then give up and stop the test
  261. tries := 10
  262. for i := 0; i < tries; i++ {
  263. // Since its still running we should see exec as part of the container
  264. out = strings.TrimSpace(inspectField(c, id, "ExecIDs"))
  265. if out != "[]" && out != "<no value>" {
  266. break
  267. }
  268. c.Assert(i+1, checker.Not(checker.Equals), tries, check.Commentf("ExecIDs still empty after 10 second"))
  269. time.Sleep(1 * time.Second)
  270. }
  271. // Save execID for later
  272. execID, err := inspectFilter(id, "index .ExecIDs 0")
  273. c.Assert(err, checker.IsNil, check.Commentf("failed to get the exec id"))
  274. // End the exec by creating the missing file
  275. err = exec.Command(dockerBinary, "exec", id,
  276. "sh", "-c", "touch /execid1").Run()
  277. c.Assert(err, checker.IsNil, check.Commentf("failed to run the 2nd exec cmd"))
  278. // Wait for 1st exec to complete
  279. cmd.Wait()
  280. // Give the exec 10 chances/seconds to stop then give up and stop the test
  281. for i := 0; i < tries; i++ {
  282. // Since its still running we should see exec as part of the container
  283. out = strings.TrimSpace(inspectField(c, id, "ExecIDs"))
  284. if out == "[]" {
  285. break
  286. }
  287. c.Assert(i+1, checker.Not(checker.Equals), tries, check.Commentf("ExecIDs still not empty after 10 second"))
  288. time.Sleep(1 * time.Second)
  289. }
  290. // But we should still be able to query the execID
  291. sc, body, err := sockRequest("GET", "/exec/"+execID+"/json", nil)
  292. c.Assert(sc, checker.Equals, http.StatusOK, check.Commentf("received status != 200 OK: %d\n%s", sc, body))
  293. // Now delete the container and then an 'inspect' on the exec should
  294. // result in a 404 (not 'container not running')
  295. out, ec := dockerCmd(c, "rm", "-f", id)
  296. c.Assert(ec, checker.Equals, 0, check.Commentf("error removing container: %s", out))
  297. sc, body, err = sockRequest("GET", "/exec/"+execID+"/json", nil)
  298. c.Assert(sc, checker.Equals, http.StatusNotFound, check.Commentf("received status != 404: %d\n%s", sc, body))
  299. }
  300. func (s *DockerSuite) TestLinksPingLinkedContainersOnRename(c *check.C) {
  301. // Problematic on Windows as Windows does not support links
  302. testRequires(c, DaemonIsLinux)
  303. var out string
  304. out, _ = dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  305. idA := strings.TrimSpace(out)
  306. c.Assert(idA, checker.Not(checker.Equals), "", check.Commentf("%s, id should not be nil", out))
  307. out, _ = dockerCmd(c, "run", "-d", "--link", "container1:alias1", "--name", "container2", "busybox", "top")
  308. idB := strings.TrimSpace(out)
  309. c.Assert(idB, checker.Not(checker.Equals), "", check.Commentf("%s, id should not be nil", out))
  310. dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  311. dockerCmd(c, "rename", "container1", "container_new")
  312. dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  313. }
  314. func (s *DockerSuite) TestRunMutableNetworkFiles(c *check.C) {
  315. // Not applicable on Windows to Windows CI.
  316. testRequires(c, SameHostDaemon, DaemonIsLinux)
  317. for _, fn := range []string{"resolv.conf", "hosts"} {
  318. deleteAllContainers(c)
  319. content, err := runCommandAndReadContainerFile(fn, exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s && top", fn)))
  320. c.Assert(err, checker.IsNil)
  321. c.Assert(strings.TrimSpace(string(content)), checker.Equals, "success", check.Commentf("Content was not what was modified in the container", string(content)))
  322. out, _ := dockerCmd(c, "run", "-d", "--name", "c2", "busybox", "top")
  323. contID := strings.TrimSpace(out)
  324. netFilePath := containerStorageFile(contID, fn)
  325. f, err := os.OpenFile(netFilePath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644)
  326. c.Assert(err, checker.IsNil)
  327. if _, err := f.Seek(0, 0); err != nil {
  328. f.Close()
  329. c.Fatal(err)
  330. }
  331. if err := f.Truncate(0); err != nil {
  332. f.Close()
  333. c.Fatal(err)
  334. }
  335. if _, err := f.Write([]byte("success2\n")); err != nil {
  336. f.Close()
  337. c.Fatal(err)
  338. }
  339. f.Close()
  340. res, _ := dockerCmd(c, "exec", contID, "cat", "/etc/"+fn)
  341. c.Assert(res, checker.Equals, "success2\n")
  342. }
  343. }
  344. func (s *DockerSuite) TestExecWithUser(c *check.C) {
  345. // TODO Windows CI: This may be fixable in the future once Windows
  346. // supports users
  347. testRequires(c, DaemonIsLinux)
  348. dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
  349. out, _ := dockerCmd(c, "exec", "-u", "1", "parent", "id")
  350. c.Assert(out, checker.Contains, "uid=1(daemon) gid=1(daemon)")
  351. out, _ = dockerCmd(c, "exec", "-u", "root", "parent", "id")
  352. c.Assert(out, checker.Contains, "uid=0(root) gid=0(root)", check.Commentf("exec with user by id expected daemon user got %s", out))
  353. }
  354. func (s *DockerSuite) TestExecWithPrivileged(c *check.C) {
  355. // Not applicable on Windows
  356. testRequires(c, DaemonIsLinux, NotUserNamespace)
  357. // Start main loop which attempts mknod repeatedly
  358. 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`)
  359. // Check exec mknod doesn't work
  360. cmd := exec.Command(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdb b 8 16")
  361. out, _, err := runCommandWithOutput(cmd)
  362. c.Assert(err, checker.NotNil, check.Commentf("exec mknod in --cap-drop=ALL container without --privileged should fail"))
  363. c.Assert(out, checker.Contains, "Operation not permitted", check.Commentf("exec mknod in --cap-drop=ALL container without --privileged should fail"))
  364. // Check exec mknod does work with --privileged
  365. 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`)
  366. out, _, err = runCommandWithOutput(cmd)
  367. c.Assert(err, checker.IsNil)
  368. actual := strings.TrimSpace(out)
  369. c.Assert(actual, checker.Equals, "ok", check.Commentf("exec mknod in --cap-drop=ALL container with --privileged failed, output: %q", out))
  370. // Check subsequent unprivileged exec cannot mknod
  371. cmd = exec.Command(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdc b 8 32")
  372. out, _, err = runCommandWithOutput(cmd)
  373. c.Assert(err, checker.NotNil, check.Commentf("repeating exec mknod in --cap-drop=ALL container after --privileged without --privileged should fail"))
  374. c.Assert(out, checker.Contains, "Operation not permitted", check.Commentf("repeating exec mknod in --cap-drop=ALL container after --privileged without --privileged should fail"))
  375. // Confirm at no point was mknod allowed
  376. logCmd := exec.Command(dockerBinary, "logs", "parent")
  377. out, _, err = runCommandWithOutput(logCmd)
  378. c.Assert(err, checker.IsNil)
  379. c.Assert(out, checker.Not(checker.Contains), "Success")
  380. }
  381. func (s *DockerSuite) TestExecWithImageUser(c *check.C) {
  382. // Not applicable on Windows
  383. testRequires(c, DaemonIsLinux)
  384. name := "testbuilduser"
  385. _, err := buildImage(name,
  386. `FROM busybox
  387. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  388. USER dockerio`,
  389. true)
  390. c.Assert(err, checker.IsNil)
  391. dockerCmd(c, "run", "-d", "--name", "dockerioexec", name, "top")
  392. out, _ := dockerCmd(c, "exec", "dockerioexec", "whoami")
  393. c.Assert(out, checker.Contains, "dockerio", check.Commentf("exec with user by id expected dockerio user got %s", out))
  394. }
  395. func (s *DockerSuite) TestExecOnReadonlyContainer(c *check.C) {
  396. // Windows does not support read-only
  397. // --read-only + userns has remount issues
  398. testRequires(c, DaemonIsLinux, NotUserNamespace)
  399. dockerCmd(c, "run", "-d", "--read-only", "--name", "parent", "busybox", "top")
  400. dockerCmd(c, "exec", "parent", "true")
  401. }
  402. func (s *DockerSuite) TestExecUlimits(c *check.C) {
  403. testRequires(c, DaemonIsLinux)
  404. name := "testexeculimits"
  405. runSleepingContainer(c, "-d", "--ulimit", "nproc=21", "--name", name)
  406. c.Assert(waitRun(name), checker.IsNil)
  407. out, _, err := dockerCmdWithError("exec", name, "sh", "-c", "ulimit -p")
  408. c.Assert(err, checker.IsNil)
  409. c.Assert(strings.TrimSpace(out), checker.Equals, "21")
  410. }
  411. // #15750
  412. func (s *DockerSuite) TestExecStartFails(c *check.C) {
  413. // TODO Windows CI. This test should be portable. Figure out why it fails
  414. // currently.
  415. testRequires(c, DaemonIsLinux)
  416. name := "exec-15750"
  417. runSleepingContainer(c, "-d", "--name", name)
  418. c.Assert(waitRun(name), checker.IsNil)
  419. out, _, err := dockerCmdWithError("exec", name, "no-such-cmd")
  420. c.Assert(err, checker.NotNil, check.Commentf(out))
  421. c.Assert(out, checker.Contains, "executable file not found")
  422. }
  423. // Fix regression in https://github.com/docker/docker/pull/26461#issuecomment-250287297
  424. func (s *DockerSuite) TestExecWindowsPathNotWiped(c *check.C) {
  425. testRequires(c, DaemonIsWindows)
  426. out, _ := dockerCmd(c, "run", "-d", "--name", "testing", minimalBaseImage(), "powershell", "start-sleep", "60")
  427. c.Assert(waitRun(strings.TrimSpace(out)), check.IsNil)
  428. out, _ = dockerCmd(c, "exec", "testing", "powershell", "write-host", "$env:PATH")
  429. out = strings.ToLower(strings.Trim(out, "\r\n"))
  430. c.Assert(out, checker.Contains, `windowspowershell\v1.0`)
  431. }
  432. func (s *DockerSuite) TestExecEnvLinksHost(c *check.C) {
  433. testRequires(c, DaemonIsLinux)
  434. runSleepingContainer(c, "-d", "--name", "foo")
  435. runSleepingContainer(c, "-d", "--link", "foo:db", "--hostname", "myhost", "--name", "bar")
  436. out, _ := dockerCmd(c, "exec", "bar", "env")
  437. c.Assert(out, checker.Contains, "HOSTNAME=myhost")
  438. c.Assert(out, checker.Contains, "DB_NAME=/bar/db")
  439. }
  440. func (s *DockerSuite) TestExecWindowsOpenHandles(c *check.C) {
  441. testRequires(c, DaemonIsWindows)
  442. runSleepingContainer(c, "-d", "--name", "test")
  443. exec := make(chan bool)
  444. go func() {
  445. dockerCmd(c, "exec", "test", "cmd", "/c", "start sleep 10")
  446. exec <- true
  447. }()
  448. for {
  449. top := make(chan string)
  450. var out string
  451. go func() {
  452. out, _ := dockerCmd(c, "top", "test")
  453. top <- out
  454. }()
  455. select {
  456. case <-time.After(time.Second * 5):
  457. c.Error("timed out waiting for top while exec is exiting")
  458. case out = <-top:
  459. break
  460. }
  461. if strings.Count(out, "busybox.exe") == 2 && !strings.Contains(out, "cmd.exe") {
  462. // The initial exec process (cmd.exe) has exited, and both sleeps are currently running
  463. break
  464. }
  465. time.Sleep(1 * time.Second)
  466. }
  467. inspect := make(chan bool)
  468. go func() {
  469. dockerCmd(c, "inspect", "test")
  470. inspect <- true
  471. }()
  472. select {
  473. case <-time.After(time.Second * 5):
  474. c.Error("timed out waiting for inspect while exec is exiting")
  475. case <-inspect:
  476. break
  477. }
  478. // Ensure the background sleep is still running
  479. out, _ := dockerCmd(c, "top", "test")
  480. c.Assert(strings.Count(out, "busybox.exe"), checker.Equals, 2)
  481. // The exec should exit when the background sleep exits
  482. select {
  483. case <-time.After(time.Second * 15):
  484. c.Error("timed out waiting for async exec to exit")
  485. case <-exec:
  486. // Ensure the background sleep has actually exited
  487. out, _ := dockerCmd(c, "top", "test")
  488. c.Assert(strings.Count(out, "busybox.exe"), checker.Equals, 1)
  489. break
  490. }
  491. }