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