docker_cli_exec_test.go 20 KB

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