docker_cli_exec_test.go 22 KB

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