docker_cli_exec_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. // +build !test_no_exec
  2. package main
  3. import (
  4. "bufio"
  5. "context"
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "reflect"
  10. "runtime"
  11. "sort"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. "time"
  16. "github.com/docker/docker/client"
  17. "github.com/docker/docker/integration-cli/cli"
  18. "github.com/docker/docker/integration-cli/cli/build"
  19. "github.com/docker/docker/pkg/parsers/kernel"
  20. "github.com/go-check/check"
  21. "gotest.tools/assert"
  22. is "gotest.tools/assert/cmp"
  23. "gotest.tools/icmd"
  24. )
  25. func (s *DockerSuite) TestExec(c *check.C) {
  26. testRequires(c, DaemonIsLinux)
  27. out, _ := dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && top")
  28. assert.NilError(c, waitRun(strings.TrimSpace(out)))
  29. out, _ = dockerCmd(c, "exec", "testing", "cat", "/tmp/file")
  30. assert.Equal(c, strings.Trim(out, "\r\n"), "test")
  31. }
  32. func (s *DockerSuite) TestExecInteractive(c *check.C) {
  33. testRequires(c, DaemonIsLinux)
  34. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && top")
  35. execCmd := exec.Command(dockerBinary, "exec", "-i", "testing", "sh")
  36. stdin, err := execCmd.StdinPipe()
  37. assert.NilError(c, err)
  38. stdout, err := execCmd.StdoutPipe()
  39. assert.NilError(c, err)
  40. err = execCmd.Start()
  41. assert.NilError(c, err)
  42. _, err = stdin.Write([]byte("cat /tmp/file\n"))
  43. assert.NilError(c, err)
  44. r := bufio.NewReader(stdout)
  45. line, err := r.ReadString('\n')
  46. assert.NilError(c, err)
  47. line = strings.TrimSpace(line)
  48. assert.Equal(c, line, "test")
  49. err = stdin.Close()
  50. assert.NilError(c, err)
  51. errChan := make(chan error)
  52. go func() {
  53. errChan <- execCmd.Wait()
  54. close(errChan)
  55. }()
  56. select {
  57. case err := <-errChan:
  58. assert.NilError(c, err)
  59. case <-time.After(1 * time.Second):
  60. c.Fatal("docker exec failed to exit on stdin close")
  61. }
  62. }
  63. func (s *DockerSuite) TestExecAfterContainerRestart(c *check.C) {
  64. out := runSleepingContainer(c)
  65. cleanedContainerID := strings.TrimSpace(out)
  66. assert.NilError(c, waitRun(cleanedContainerID))
  67. dockerCmd(c, "restart", cleanedContainerID)
  68. assert.NilError(c, waitRun(cleanedContainerID))
  69. out, _ = dockerCmd(c, "exec", cleanedContainerID, "echo", "hello")
  70. assert.Equal(c, strings.TrimSpace(out), "hello")
  71. }
  72. func (s *DockerDaemonSuite) TestExecAfterDaemonRestart(c *check.C) {
  73. // TODO Windows CI: Requires a little work to get this ported.
  74. testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon)
  75. s.d.StartWithBusybox(c)
  76. out, err := s.d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top")
  77. assert.NilError(c, err, "Could not run top: %s", out)
  78. s.d.Restart(c)
  79. out, err = s.d.Cmd("start", "top")
  80. assert.NilError(c, err, "Could not start top after daemon restart: %s", out)
  81. out, err = s.d.Cmd("exec", "top", "echo", "hello")
  82. assert.NilError(c, err, "Could not exec on container top: %s", out)
  83. assert.Equal(c, strings.TrimSpace(out), "hello")
  84. }
  85. // Regression test for #9155, #9044
  86. func (s *DockerSuite) TestExecEnv(c *check.C) {
  87. // TODO Windows CI: This one is interesting and may just end up being a feature
  88. // difference between Windows and Linux. On Windows, the environment is passed
  89. // into the process that is launched, not into the machine environment. Hence
  90. // a subsequent exec will not have LALA set/
  91. testRequires(c, DaemonIsLinux)
  92. runSleepingContainer(c, "-e", "LALA=value1", "-e", "LALA=value2", "-d", "--name", "testing")
  93. assert.NilError(c, waitRun("testing"))
  94. out, _ := dockerCmd(c, "exec", "testing", "env")
  95. assert.Check(c, !strings.Contains(out, "LALA=value1"))
  96. assert.Check(c, strings.Contains(out, "LALA=value2"))
  97. assert.Check(c, strings.Contains(out, "HOME=/root"))
  98. }
  99. func (s *DockerSuite) TestExecSetEnv(c *check.C) {
  100. testRequires(c, DaemonIsLinux)
  101. runSleepingContainer(c, "-e", "HOME=/root", "-d", "--name", "testing")
  102. assert.NilError(c, waitRun("testing"))
  103. out, _ := dockerCmd(c, "exec", "-e", "HOME=/another", "-e", "ABC=xyz", "testing", "env")
  104. assert.Check(c, !strings.Contains(out, "HOME=/root"))
  105. assert.Check(c, strings.Contains(out, "HOME=/another"))
  106. assert.Check(c, strings.Contains(out, "ABC=xyz"))
  107. }
  108. func (s *DockerSuite) TestExecExitStatus(c *check.C) {
  109. runSleepingContainer(c, "-d", "--name", "top")
  110. result := icmd.RunCommand(dockerBinary, "exec", "top", "sh", "-c", "exit 23")
  111. result.Assert(c, icmd.Expected{ExitCode: 23, Error: "exit status 23"})
  112. }
  113. func (s *DockerSuite) TestExecPausedContainer(c *check.C) {
  114. testRequires(c, IsPausable)
  115. out := runSleepingContainer(c, "-d", "--name", "testing")
  116. ContainerID := strings.TrimSpace(out)
  117. dockerCmd(c, "pause", "testing")
  118. out, _, err := dockerCmdWithError("exec", ContainerID, "echo", "hello")
  119. assert.ErrorContains(c, err, "", "container should fail to exec new command if it is paused")
  120. expected := ContainerID + " is paused, unpause the container before exec"
  121. assert.Assert(c, is.Contains(out, expected), "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. assert.NilError(c, err)
  131. stdinRw.Write([]byte("test"))
  132. stdinRw.Close()
  133. out, _, err := runCommandWithOutput(cmd)
  134. assert.NilError(c, err, out)
  135. out, _ = dockerCmd(c, "top", "exec_tty_stdin")
  136. outArr := strings.Split(out, "\n")
  137. assert.Assert(c, len(outArr) <= 3, "exec process left running")
  138. assert.Assert(c, !strings.Contains(out, "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. assert.NilError(c, waitRun(id))
  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. assert.NilError(c, err)
  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. result := icmd.StartCmd(icmd.Command(dockerBinary, "exec", "testing", "top"))
  190. result.Assert(c, icmd.Success)
  191. go icmd.WaitOnCmd(0, result)
  192. type dstop struct {
  193. out string
  194. err error
  195. }
  196. ch := make(chan dstop)
  197. go func() {
  198. result := icmd.RunCommand(dockerBinary, "stop", "testing")
  199. ch <- dstop{result.Combined(), result.Error}
  200. close(ch)
  201. }()
  202. select {
  203. case <-time.After(3 * time.Second):
  204. c.Fatal("Container stop timed out")
  205. case s := <-ch:
  206. assert.NilError(c, s.err)
  207. }
  208. }
  209. func (s *DockerSuite) TestExecCgroup(c *check.C) {
  210. // Not applicable on Windows - using Linux specific functionality
  211. testRequires(c, NotUserNamespace)
  212. testRequires(c, DaemonIsLinux)
  213. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
  214. out, _ := dockerCmd(c, "exec", "testing", "cat", "/proc/1/cgroup")
  215. containerCgroups := sort.StringSlice(strings.Split(out, "\n"))
  216. var wg sync.WaitGroup
  217. var mu sync.Mutex
  218. var execCgroups []sort.StringSlice
  219. errChan := make(chan error)
  220. // exec a few times concurrently to get consistent failure
  221. for i := 0; i < 5; i++ {
  222. wg.Add(1)
  223. go func() {
  224. out, _, err := dockerCmdWithError("exec", "testing", "cat", "/proc/self/cgroup")
  225. if err != nil {
  226. errChan <- err
  227. return
  228. }
  229. cg := sort.StringSlice(strings.Split(out, "\n"))
  230. mu.Lock()
  231. execCgroups = append(execCgroups, cg)
  232. mu.Unlock()
  233. wg.Done()
  234. }()
  235. }
  236. wg.Wait()
  237. close(errChan)
  238. for err := range errChan {
  239. assert.NilError(c, err)
  240. }
  241. for _, cg := range execCgroups {
  242. if !reflect.DeepEqual(cg, containerCgroups) {
  243. fmt.Println("exec cgroups:")
  244. for _, name := range cg {
  245. fmt.Printf(" %s\n", name)
  246. }
  247. fmt.Println("container cgroups:")
  248. for _, name := range containerCgroups {
  249. fmt.Printf(" %s\n", name)
  250. }
  251. c.Fatal("cgroups mismatched")
  252. }
  253. }
  254. }
  255. func (s *DockerSuite) TestExecInspectID(c *check.C) {
  256. out := runSleepingContainer(c, "-d")
  257. id := strings.TrimSuffix(out, "\n")
  258. out = inspectField(c, id, "ExecIDs")
  259. assert.Equal(c, out, "[]", "ExecIDs should be empty, got: %s", out)
  260. // Start an exec, have it block waiting so we can do some checking
  261. cmd := exec.Command(dockerBinary, "exec", id, "sh", "-c",
  262. "while ! test -e /execid1; do sleep 1; done")
  263. err := cmd.Start()
  264. assert.NilError(c, err, "failed to start the exec cmd")
  265. // Give the exec 10 chances/seconds to start then give up and stop the test
  266. tries := 10
  267. for i := 0; i < tries; i++ {
  268. // Since its still running we should see exec as part of the container
  269. out = strings.TrimSpace(inspectField(c, id, "ExecIDs"))
  270. if out != "[]" && out != "<no value>" {
  271. break
  272. }
  273. assert.Check(c, i+1 != tries, "ExecIDs still empty after 10 second")
  274. time.Sleep(1 * time.Second)
  275. }
  276. // Save execID for later
  277. execID, err := inspectFilter(id, "index .ExecIDs 0")
  278. assert.NilError(c, err, "failed to get the exec id")
  279. // End the exec by creating the missing file
  280. err = exec.Command(dockerBinary, "exec", id, "sh", "-c", "touch /execid1").Run()
  281. assert.NilError(c, err, "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. assert.Check(c, i+1 != tries, "ExecIDs still empty after 10 second")
  292. time.Sleep(1 * time.Second)
  293. }
  294. // But we should still be able to query the execID
  295. cli, err := client.NewClientWithOpts(client.FromEnv)
  296. assert.NilError(c, err)
  297. defer cli.Close()
  298. _, err = cli.ContainerExecInspect(context.Background(), execID)
  299. assert.NilError(c, err)
  300. // Now delete the container and then an 'inspect' on the exec should
  301. // result in a 404 (not 'container not running')
  302. out, ec := dockerCmd(c, "rm", "-f", id)
  303. assert.Equal(c, ec, 0, "error removing container: %s", out)
  304. _, err = cli.ContainerExecInspect(context.Background(), execID)
  305. assert.ErrorContains(c, err, "No such exec instance")
  306. }
  307. func (s *DockerSuite) TestLinksPingLinkedContainersOnRename(c *check.C) {
  308. // Problematic on Windows as Windows does not support links
  309. testRequires(c, DaemonIsLinux)
  310. var out string
  311. out, _ = dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  312. idA := strings.TrimSpace(out)
  313. assert.Assert(c, idA != "", "%s, id should not be nil", out)
  314. out, _ = dockerCmd(c, "run", "-d", "--link", "container1:alias1", "--name", "container2", "busybox", "top")
  315. idB := strings.TrimSpace(out)
  316. assert.Assert(c, idB != "", "%s, id should not be nil", out)
  317. dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  318. dockerCmd(c, "rename", "container1", "container_new")
  319. dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  320. }
  321. func (s *DockerSuite) TestRunMutableNetworkFiles(c *check.C) {
  322. // Not applicable on Windows to Windows CI.
  323. testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
  324. for _, fn := range []string{"resolv.conf", "hosts"} {
  325. containers := cli.DockerCmd(c, "ps", "-q", "-a").Combined()
  326. if containers != "" {
  327. cli.DockerCmd(c, append([]string{"rm", "-fv"}, strings.Split(strings.TrimSpace(containers), "\n")...)...)
  328. }
  329. content := runCommandAndReadContainerFile(c, fn, dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s && top", fn))
  330. assert.Equal(c, strings.TrimSpace(string(content)), "success", "Content was not what was modified in the container", string(content))
  331. out, _ := dockerCmd(c, "run", "-d", "--name", "c2", "busybox", "top")
  332. contID := strings.TrimSpace(out)
  333. netFilePath := containerStorageFile(contID, fn)
  334. f, err := os.OpenFile(netFilePath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644)
  335. assert.NilError(c, err)
  336. if _, err := f.Seek(0, 0); err != nil {
  337. f.Close()
  338. c.Fatal(err)
  339. }
  340. if err := f.Truncate(0); err != nil {
  341. f.Close()
  342. c.Fatal(err)
  343. }
  344. if _, err := f.Write([]byte("success2\n")); err != nil {
  345. f.Close()
  346. c.Fatal(err)
  347. }
  348. f.Close()
  349. res, _ := dockerCmd(c, "exec", contID, "cat", "/etc/"+fn)
  350. assert.Equal(c, res, "success2\n")
  351. }
  352. }
  353. func (s *DockerSuite) TestExecWithUser(c *check.C) {
  354. // TODO Windows CI: This may be fixable in the future once Windows
  355. // supports users
  356. testRequires(c, DaemonIsLinux)
  357. dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
  358. out, _ := dockerCmd(c, "exec", "-u", "1", "parent", "id")
  359. assert.Assert(c, strings.Contains(out, "uid=1(daemon) gid=1(daemon)"))
  360. out, _ = dockerCmd(c, "exec", "-u", "root", "parent", "id")
  361. assert.Assert(c, strings.Contains(out, "uid=0(root) gid=0(root)"), "exec with user by id expected daemon user got %s", out)
  362. }
  363. func (s *DockerSuite) TestExecWithPrivileged(c *check.C) {
  364. // Not applicable on Windows
  365. testRequires(c, DaemonIsLinux, NotUserNamespace)
  366. // Start main loop which attempts mknod repeatedly
  367. 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`)
  368. // Check exec mknod doesn't work
  369. icmd.RunCommand(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdb b 8 16").Assert(c, icmd.Expected{
  370. ExitCode: 1,
  371. Err: "Operation not permitted",
  372. })
  373. // Check exec mknod does work with --privileged
  374. 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`)
  375. result.Assert(c, icmd.Success)
  376. actual := strings.TrimSpace(result.Combined())
  377. assert.Equal(c, actual, "ok", "exec mknod in --cap-drop=ALL container with --privileged failed, output: %q", result.Combined())
  378. // Check subsequent unprivileged exec cannot mknod
  379. icmd.RunCommand(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdc b 8 32").Assert(c, icmd.Expected{
  380. ExitCode: 1,
  381. Err: "Operation not permitted",
  382. })
  383. // Confirm at no point was mknod allowed
  384. result = icmd.RunCommand(dockerBinary, "logs", "parent")
  385. result.Assert(c, icmd.Success)
  386. assert.Assert(c, !strings.Contains(result.Combined(), "Success"))
  387. }
  388. func (s *DockerSuite) TestExecWithImageUser(c *check.C) {
  389. // Not applicable on Windows
  390. testRequires(c, DaemonIsLinux)
  391. name := "testbuilduser"
  392. buildImageSuccessfully(c, name, build.WithDockerfile(`FROM busybox
  393. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  394. USER dockerio`))
  395. dockerCmd(c, "run", "-d", "--name", "dockerioexec", name, "top")
  396. out, _ := dockerCmd(c, "exec", "dockerioexec", "whoami")
  397. assert.Assert(c, strings.Contains(out, "dockerio"), "exec with user by id expected dockerio user got %s", out)
  398. }
  399. func (s *DockerSuite) TestExecOnReadonlyContainer(c *check.C) {
  400. // Windows does not support read-only
  401. // --read-only + userns has remount issues
  402. testRequires(c, DaemonIsLinux, NotUserNamespace)
  403. dockerCmd(c, "run", "-d", "--read-only", "--name", "parent", "busybox", "top")
  404. dockerCmd(c, "exec", "parent", "true")
  405. }
  406. func (s *DockerSuite) TestExecUlimits(c *check.C) {
  407. testRequires(c, DaemonIsLinux)
  408. name := "testexeculimits"
  409. runSleepingContainer(c, "-d", "--ulimit", "nofile=511:511", "--name", name)
  410. assert.NilError(c, waitRun(name))
  411. out, _, err := dockerCmdWithError("exec", name, "sh", "-c", "ulimit -n")
  412. assert.NilError(c, err)
  413. assert.Equal(c, strings.TrimSpace(out), "511")
  414. }
  415. // #15750
  416. func (s *DockerSuite) TestExecStartFails(c *check.C) {
  417. // TODO Windows CI. This test should be portable. Figure out why it fails
  418. // currently.
  419. testRequires(c, DaemonIsLinux)
  420. name := "exec-15750"
  421. runSleepingContainer(c, "-d", "--name", name)
  422. assert.NilError(c, waitRun(name))
  423. out, _, err := dockerCmdWithError("exec", name, "no-such-cmd")
  424. assert.ErrorContains(c, err, "", out)
  425. assert.Assert(c, strings.Contains(out, "executable file not found"))
  426. }
  427. // Fix regression in https://github.com/docker/docker/pull/26461#issuecomment-250287297
  428. func (s *DockerSuite) TestExecWindowsPathNotWiped(c *check.C) {
  429. testRequires(c, DaemonIsWindows)
  430. out, _ := dockerCmd(c, "run", "-d", "--name", "testing", minimalBaseImage(), "powershell", "start-sleep", "60")
  431. assert.NilError(c, waitRun(strings.TrimSpace(out)))
  432. out, _ = dockerCmd(c, "exec", "testing", "powershell", "write-host", "$env:PATH")
  433. out = strings.ToLower(strings.Trim(out, "\r\n"))
  434. assert.Assert(c, strings.Contains(out, `windowspowershell\v1.0`))
  435. }
  436. func (s *DockerSuite) TestExecEnvLinksHost(c *check.C) {
  437. testRequires(c, DaemonIsLinux)
  438. runSleepingContainer(c, "-d", "--name", "foo")
  439. runSleepingContainer(c, "-d", "--link", "foo:db", "--hostname", "myhost", "--name", "bar")
  440. out, _ := dockerCmd(c, "exec", "bar", "env")
  441. assert.Check(c, is.Contains(out, "HOSTNAME=myhost"))
  442. assert.Check(c, is.Contains(out, "DB_NAME=/bar/db"))
  443. }
  444. func (s *DockerSuite) TestExecWindowsOpenHandles(c *check.C) {
  445. testRequires(c, DaemonIsWindows)
  446. if runtime.GOOS == "windows" {
  447. v, err := kernel.GetKernelVersion()
  448. assert.NilError(c, err)
  449. build, _ := strconv.Atoi(strings.Split(strings.SplitN(v.String(), " ", 3)[2][1:], ".")[0])
  450. if build >= 17743 {
  451. c.Skip("Temporarily disabled on RS5 17743+ builds due to platform bug")
  452. // This is being tracked internally. @jhowardmsft. Summary of failure
  453. // from an email in early July 2018 below:
  454. //
  455. // Platform regression. In cmd.exe by the look of it. I can repro
  456. // it outside of CI. It fails the same on 17681, 17676 and even as
  457. // far back as 17663, over a month old. From investigating, I can see
  458. // what's happening in the container, but not the reason. The test
  459. // starts a long-running container based on the Windows busybox image.
  460. // It then adds another process (docker exec) to that container to
  461. // sleep. It loops waiting for two instances of busybox.exe running,
  462. // and cmd.exe to quit. What's actually happening is that the second
  463. // exec hangs indefinitely, and from docker top, I can see
  464. // "OpenWith.exe" running.
  465. //Manual repro would be
  466. //# Start the first long-running container
  467. //docker run --rm -d --name test busybox sleep 300
  468. //# In another window, docker top test. There should be a single instance of busybox.exe running
  469. //# In a third window, docker exec test cmd /c start sleep 10 NOTE THIS HANGS UNTIL 5 MIN TIMEOUT
  470. //# In the second window, run docker top test. Note that OpenWith.exe is running, one cmd.exe and only one busybox. I would expect no "OpenWith" and two busybox.exe's.
  471. }
  472. }
  473. runSleepingContainer(c, "-d", "--name", "test")
  474. exec := make(chan bool)
  475. go func() {
  476. dockerCmd(c, "exec", "test", "cmd", "/c", "start sleep 10")
  477. exec <- true
  478. }()
  479. count := 0
  480. for {
  481. top := make(chan string)
  482. var out string
  483. go func() {
  484. out, _ := dockerCmd(c, "top", "test")
  485. top <- out
  486. }()
  487. select {
  488. case <-time.After(time.Second * 5):
  489. c.Fatal("timed out waiting for top while exec is exiting")
  490. case out = <-top:
  491. break
  492. }
  493. if strings.Count(out, "busybox.exe") == 2 && !strings.Contains(out, "cmd.exe") {
  494. // The initial exec process (cmd.exe) has exited, and both sleeps are currently running
  495. break
  496. }
  497. count++
  498. if count >= 30 {
  499. c.Fatal("too many retries")
  500. }
  501. time.Sleep(1 * time.Second)
  502. }
  503. inspect := make(chan bool)
  504. go func() {
  505. dockerCmd(c, "inspect", "test")
  506. inspect <- true
  507. }()
  508. select {
  509. case <-time.After(time.Second * 5):
  510. c.Fatal("timed out waiting for inspect while exec is exiting")
  511. case <-inspect:
  512. break
  513. }
  514. // Ensure the background sleep is still running
  515. out, _ := dockerCmd(c, "top", "test")
  516. assert.Equal(c, strings.Count(out, "busybox.exe"), 2)
  517. // The exec should exit when the background sleep exits
  518. select {
  519. case <-time.After(time.Second * 15):
  520. c.Fatal("timed out waiting for async exec to exit")
  521. case <-exec:
  522. // Ensure the background sleep has actually exited
  523. out, _ := dockerCmd(c, "top", "test")
  524. assert.Equal(c, strings.Count(out, "busybox.exe"), 1)
  525. break
  526. }
  527. }