docker_cli_exec_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. // +build !test_no_exec
  2. package main
  3. import (
  4. "bufio"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "reflect"
  11. "sort"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/docker/docker/pkg/integration/checker"
  16. "github.com/go-check/check"
  17. )
  18. func (s *DockerSuite) TestExec(c *check.C) {
  19. testRequires(c, DaemonIsLinux)
  20. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && top")
  21. out, _ := dockerCmd(c, "exec", "testing", "cat", "/tmp/file")
  22. out = strings.Trim(out, "\r\n")
  23. if out != "test" {
  24. c.Errorf("container exec should've printed test but printed %q", out)
  25. }
  26. }
  27. func (s *DockerSuite) TestExecInteractive(c *check.C) {
  28. testRequires(c, DaemonIsLinux)
  29. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && top")
  30. execCmd := exec.Command(dockerBinary, "exec", "-i", "testing", "sh")
  31. stdin, err := execCmd.StdinPipe()
  32. if err != nil {
  33. c.Fatal(err)
  34. }
  35. stdout, err := execCmd.StdoutPipe()
  36. if err != nil {
  37. c.Fatal(err)
  38. }
  39. if err := execCmd.Start(); err != nil {
  40. c.Fatal(err)
  41. }
  42. if _, err := stdin.Write([]byte("cat /tmp/file\n")); err != nil {
  43. c.Fatal(err)
  44. }
  45. r := bufio.NewReader(stdout)
  46. line, err := r.ReadString('\n')
  47. if err != nil {
  48. c.Fatal(err)
  49. }
  50. line = strings.TrimSpace(line)
  51. if line != "test" {
  52. c.Fatalf("Output should be 'test', got '%q'", line)
  53. }
  54. if err := stdin.Close(); err != nil {
  55. c.Fatal(err)
  56. }
  57. errChan := make(chan error)
  58. go func() {
  59. errChan <- execCmd.Wait()
  60. close(errChan)
  61. }()
  62. select {
  63. case err := <-errChan:
  64. c.Assert(err, check.IsNil)
  65. case <-time.After(1 * time.Second):
  66. c.Fatal("docker exec failed to exit on stdin close")
  67. }
  68. }
  69. func (s *DockerSuite) TestExecAfterContainerRestart(c *check.C) {
  70. testRequires(c, DaemonIsLinux)
  71. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  72. cleanedContainerID := strings.TrimSpace(out)
  73. dockerCmd(c, "restart", cleanedContainerID)
  74. out, _ = dockerCmd(c, "exec", cleanedContainerID, "echo", "hello")
  75. outStr := strings.TrimSpace(out)
  76. if outStr != "hello" {
  77. c.Errorf("container should've printed hello, instead printed %q", outStr)
  78. }
  79. }
  80. func (s *DockerDaemonSuite) TestExecAfterDaemonRestart(c *check.C) {
  81. testRequires(c, DaemonIsLinux)
  82. testRequires(c, SameHostDaemon)
  83. if err := s.d.StartWithBusybox(); err != nil {
  84. c.Fatalf("Could not start daemon with busybox: %v", err)
  85. }
  86. if out, err := s.d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
  87. c.Fatalf("Could not run top: err=%v\n%s", err, out)
  88. }
  89. if err := s.d.Restart(); err != nil {
  90. c.Fatalf("Could not restart daemon: %v", err)
  91. }
  92. if out, err := s.d.Cmd("start", "top"); err != nil {
  93. c.Fatalf("Could not start top after daemon restart: err=%v\n%s", err, out)
  94. }
  95. out, err := s.d.Cmd("exec", "top", "echo", "hello")
  96. if err != nil {
  97. c.Fatalf("Could not exec on container top: err=%v\n%s", err, out)
  98. }
  99. outStr := strings.TrimSpace(string(out))
  100. if outStr != "hello" {
  101. c.Errorf("container should've printed hello, instead printed %q", outStr)
  102. }
  103. }
  104. // Regression test for #9155, #9044
  105. func (s *DockerSuite) TestExecEnv(c *check.C) {
  106. testRequires(c, DaemonIsLinux)
  107. dockerCmd(c, "run", "-e", "LALA=value1", "-e", "LALA=value2",
  108. "-d", "--name", "testing", "busybox", "top")
  109. out, _ := dockerCmd(c, "exec", "testing", "env")
  110. if strings.Contains(out, "LALA=value1") ||
  111. !strings.Contains(out, "LALA=value2") ||
  112. !strings.Contains(out, "HOME=/root") {
  113. c.Errorf("exec env(%q), expect %q, %q", out, "LALA=value2", "HOME=/root")
  114. }
  115. }
  116. func (s *DockerSuite) TestExecExitStatus(c *check.C) {
  117. testRequires(c, DaemonIsLinux)
  118. dockerCmd(c, "run", "-d", "--name", "top", "busybox", "top")
  119. // Test normal (non-detached) case first
  120. cmd := exec.Command(dockerBinary, "exec", "top", "sh", "-c", "exit 23")
  121. ec, _ := runCommand(cmd)
  122. if ec != 23 {
  123. c.Fatalf("Should have had an ExitCode of 23, not: %d", ec)
  124. }
  125. }
  126. func (s *DockerSuite) TestExecPausedContainer(c *check.C) {
  127. testRequires(c, DaemonIsLinux)
  128. defer unpauseAllContainers()
  129. out, _ := dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
  130. ContainerID := strings.TrimSpace(out)
  131. dockerCmd(c, "pause", "testing")
  132. out, _, err := dockerCmdWithError("exec", "-i", "-t", ContainerID, "echo", "hello")
  133. if err == nil {
  134. c.Fatal("container should fail to exec new command if it is paused")
  135. }
  136. expected := ContainerID + " is paused, unpause the container before exec"
  137. if !strings.Contains(out, expected) {
  138. c.Fatal("container should not exec new command if it is paused")
  139. }
  140. }
  141. // regression test for #9476
  142. func (s *DockerSuite) TestExecTtyCloseStdin(c *check.C) {
  143. testRequires(c, DaemonIsLinux)
  144. dockerCmd(c, "run", "-d", "-it", "--name", "exec_tty_stdin", "busybox")
  145. cmd := exec.Command(dockerBinary, "exec", "-i", "exec_tty_stdin", "cat")
  146. stdinRw, err := cmd.StdinPipe()
  147. if err != nil {
  148. c.Fatal(err)
  149. }
  150. stdinRw.Write([]byte("test"))
  151. stdinRw.Close()
  152. if out, _, err := runCommandWithOutput(cmd); err != nil {
  153. c.Fatal(out, err)
  154. }
  155. out, _ := dockerCmd(c, "top", "exec_tty_stdin")
  156. outArr := strings.Split(out, "\n")
  157. if len(outArr) > 3 || strings.Contains(out, "nsenter-exec") {
  158. c.Fatalf("exec process left running\n\t %s", out)
  159. }
  160. }
  161. func (s *DockerSuite) TestExecTtyWithoutStdin(c *check.C) {
  162. testRequires(c, DaemonIsLinux)
  163. out, _ := dockerCmd(c, "run", "-d", "-ti", "busybox")
  164. id := strings.TrimSpace(out)
  165. c.Assert(waitRun(id), check.IsNil)
  166. errChan := make(chan error)
  167. go func() {
  168. defer close(errChan)
  169. cmd := exec.Command(dockerBinary, "exec", "-ti", id, "true")
  170. if _, err := cmd.StdinPipe(); err != nil {
  171. errChan <- err
  172. return
  173. }
  174. expected := "cannot enable tty mode"
  175. if out, _, err := runCommandWithOutput(cmd); err == nil {
  176. errChan <- fmt.Errorf("exec should have failed")
  177. return
  178. } else if !strings.Contains(out, expected) {
  179. errChan <- fmt.Errorf("exec failed with error %q: expected %q", out, expected)
  180. return
  181. }
  182. }()
  183. select {
  184. case err := <-errChan:
  185. c.Assert(err, check.IsNil)
  186. case <-time.After(3 * time.Second):
  187. c.Fatal("exec is running but should have failed")
  188. }
  189. }
  190. func (s *DockerSuite) TestExecParseError(c *check.C) {
  191. testRequires(c, DaemonIsLinux)
  192. dockerCmd(c, "run", "-d", "--name", "top", "busybox", "top")
  193. // Test normal (non-detached) case first
  194. cmd := exec.Command(dockerBinary, "exec", "top")
  195. if _, stderr, code, err := runCommandWithStdoutStderr(cmd); err == nil || !strings.Contains(stderr, "See '"+dockerBinary+" exec --help'") || code == 0 {
  196. c.Fatalf("Should have thrown error & point to help: %s", stderr)
  197. }
  198. }
  199. func (s *DockerSuite) TestExecStopNotHanging(c *check.C) {
  200. testRequires(c, DaemonIsLinux)
  201. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
  202. if err := exec.Command(dockerBinary, "exec", "testing", "top").Start(); err != nil {
  203. c.Fatal(err)
  204. }
  205. type dstop struct {
  206. out []byte
  207. err error
  208. }
  209. ch := make(chan dstop)
  210. go func() {
  211. out, err := exec.Command(dockerBinary, "stop", "testing").CombinedOutput()
  212. ch <- dstop{out, err}
  213. close(ch)
  214. }()
  215. select {
  216. case <-time.After(3 * time.Second):
  217. c.Fatal("Container stop timed out")
  218. case s := <-ch:
  219. c.Assert(s.err, check.IsNil)
  220. }
  221. }
  222. func (s *DockerSuite) TestExecCgroup(c *check.C) {
  223. testRequires(c, DaemonIsLinux)
  224. dockerCmd(c, "run", "-d", "--name", "testing", "busybox", "top")
  225. out, _ := dockerCmd(c, "exec", "testing", "cat", "/proc/1/cgroup")
  226. containerCgroups := sort.StringSlice(strings.Split(out, "\n"))
  227. var wg sync.WaitGroup
  228. var mu sync.Mutex
  229. execCgroups := []sort.StringSlice{}
  230. errChan := make(chan error)
  231. // exec a few times concurrently to get consistent failure
  232. for i := 0; i < 5; i++ {
  233. wg.Add(1)
  234. go func() {
  235. out, _, err := dockerCmdWithError("exec", "testing", "cat", "/proc/self/cgroup")
  236. if err != nil {
  237. errChan <- err
  238. return
  239. }
  240. cg := sort.StringSlice(strings.Split(out, "\n"))
  241. mu.Lock()
  242. execCgroups = append(execCgroups, cg)
  243. mu.Unlock()
  244. wg.Done()
  245. }()
  246. }
  247. wg.Wait()
  248. close(errChan)
  249. for err := range errChan {
  250. c.Assert(err, check.IsNil)
  251. }
  252. for _, cg := range execCgroups {
  253. if !reflect.DeepEqual(cg, containerCgroups) {
  254. fmt.Println("exec cgroups:")
  255. for _, name := range cg {
  256. fmt.Printf(" %s\n", name)
  257. }
  258. fmt.Println("container cgroups:")
  259. for _, name := range containerCgroups {
  260. fmt.Printf(" %s\n", name)
  261. }
  262. c.Fatal("cgroups mismatched")
  263. }
  264. }
  265. }
  266. func (s *DockerSuite) TestInspectExecID(c *check.C) {
  267. testRequires(c, DaemonIsLinux)
  268. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  269. id := strings.TrimSuffix(out, "\n")
  270. out, err := inspectField(id, "ExecIDs")
  271. if err != nil {
  272. c.Fatalf("failed to inspect container: %s, %v", out, err)
  273. }
  274. if out != "[]" {
  275. c.Fatalf("ExecIDs should be empty, got: %s", out)
  276. }
  277. // Start an exec, have it block waiting so we can do some checking
  278. cmd := exec.Command(dockerBinary, "exec", id, "sh", "-c",
  279. "while ! test -e /tmp/execid1; do sleep 1; done")
  280. if err = cmd.Start(); err != nil {
  281. c.Fatalf("failed to start the exec cmd: %q", err)
  282. }
  283. // Give the exec 10 chances/seconds to start then give up and stop the test
  284. tries := 10
  285. for i := 0; i < tries; i++ {
  286. // Since its still running we should see exec as part of the container
  287. out, err = inspectField(id, "ExecIDs")
  288. if err != nil {
  289. c.Fatalf("failed to inspect container: %s, %v", out, err)
  290. }
  291. out = strings.TrimSuffix(out, "\n")
  292. if out != "[]" && out != "<no value>" {
  293. break
  294. }
  295. if i+1 == tries {
  296. c.Fatalf("ExecIDs should not be empty, got: %s", out)
  297. }
  298. time.Sleep(1 * time.Second)
  299. }
  300. // Save execID for later
  301. execID, err := inspectFilter(id, "index .ExecIDs 0")
  302. if err != nil {
  303. c.Fatalf("failed to get the exec id: %v", err)
  304. }
  305. // End the exec by creating the missing file
  306. err = exec.Command(dockerBinary, "exec", id,
  307. "sh", "-c", "touch /tmp/execid1").Run()
  308. if err != nil {
  309. c.Fatalf("failed to run the 2nd exec cmd: %q", err)
  310. }
  311. // Wait for 1st exec to complete
  312. cmd.Wait()
  313. // All execs for the container should be gone now
  314. out, err = inspectField(id, "ExecIDs")
  315. if err != nil {
  316. c.Fatalf("failed to inspect container: %s, %v", out, err)
  317. }
  318. out = strings.TrimSuffix(out, "\n")
  319. if out != "[]" && out != "<no value>" {
  320. c.Fatalf("ExecIDs should be empty, got: %s", out)
  321. }
  322. // But we should still be able to query the execID
  323. sc, body, err := sockRequest("GET", "/exec/"+execID+"/json", nil)
  324. if sc != http.StatusOK {
  325. c.Fatalf("received status != 200 OK: %d\n%s", sc, body)
  326. }
  327. // Now delete the container and then an 'inspect' on the exec should
  328. // result in a 404 (not 'container not running')
  329. out, ec := dockerCmd(c, "rm", "-f", id)
  330. if ec != 0 {
  331. c.Fatalf("error removing container: %s", out)
  332. }
  333. sc, body, err = sockRequest("GET", "/exec/"+execID+"/json", nil)
  334. if sc != http.StatusNotFound {
  335. c.Fatalf("received status != 404: %d\n%s", sc, body)
  336. }
  337. }
  338. func (s *DockerSuite) TestLinksPingLinkedContainersOnRename(c *check.C) {
  339. testRequires(c, DaemonIsLinux)
  340. var out string
  341. out, _ = dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  342. idA := strings.TrimSpace(out)
  343. if idA == "" {
  344. c.Fatal(out, "id should not be nil")
  345. }
  346. out, _ = dockerCmd(c, "run", "-d", "--link", "container1:alias1", "--name", "container2", "busybox", "top")
  347. idB := strings.TrimSpace(out)
  348. if idB == "" {
  349. c.Fatal(out, "id should not be nil")
  350. }
  351. dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  352. dockerCmd(c, "rename", "container1", "container_new")
  353. dockerCmd(c, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  354. }
  355. func (s *DockerSuite) TestRunExecDir(c *check.C) {
  356. testRequires(c, SameHostDaemon)
  357. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  358. id := strings.TrimSpace(out)
  359. execDir := filepath.Join(execDriverPath, id)
  360. stateFile := filepath.Join(execDir, "state.json")
  361. {
  362. fi, err := os.Stat(execDir)
  363. if err != nil {
  364. c.Fatal(err)
  365. }
  366. if !fi.IsDir() {
  367. c.Fatalf("%q must be a directory", execDir)
  368. }
  369. fi, err = os.Stat(stateFile)
  370. if err != nil {
  371. c.Fatal(err)
  372. }
  373. }
  374. dockerCmd(c, "stop", id)
  375. {
  376. _, err := os.Stat(execDir)
  377. if err == nil {
  378. c.Fatal(err)
  379. }
  380. if err == nil {
  381. c.Fatalf("Exec directory %q exists for removed container!", execDir)
  382. }
  383. if !os.IsNotExist(err) {
  384. c.Fatalf("Error should be about non-existing, got %s", err)
  385. }
  386. }
  387. dockerCmd(c, "start", id)
  388. {
  389. fi, err := os.Stat(execDir)
  390. if err != nil {
  391. c.Fatal(err)
  392. }
  393. if !fi.IsDir() {
  394. c.Fatalf("%q must be a directory", execDir)
  395. }
  396. fi, err = os.Stat(stateFile)
  397. if err != nil {
  398. c.Fatal(err)
  399. }
  400. }
  401. dockerCmd(c, "rm", "-f", id)
  402. {
  403. _, err := os.Stat(execDir)
  404. if err == nil {
  405. c.Fatal(err)
  406. }
  407. if err == nil {
  408. c.Fatalf("Exec directory %q is exists for removed container!", execDir)
  409. }
  410. if !os.IsNotExist(err) {
  411. c.Fatalf("Error should be about non-existing, got %s", err)
  412. }
  413. }
  414. }
  415. func (s *DockerSuite) TestRunMutableNetworkFiles(c *check.C) {
  416. testRequires(c, SameHostDaemon)
  417. for _, fn := range []string{"resolv.conf", "hosts"} {
  418. deleteAllContainers()
  419. content, err := runCommandAndReadContainerFile(fn, exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s && top", fn)))
  420. if err != nil {
  421. c.Fatal(err)
  422. }
  423. if strings.TrimSpace(string(content)) != "success" {
  424. c.Fatal("Content was not what was modified in the container", string(content))
  425. }
  426. out, _ := dockerCmd(c, "run", "-d", "--name", "c2", "busybox", "top")
  427. contID := strings.TrimSpace(out)
  428. netFilePath := containerStorageFile(contID, fn)
  429. f, err := os.OpenFile(netFilePath, os.O_WRONLY|os.O_SYNC|os.O_APPEND, 0644)
  430. if err != nil {
  431. c.Fatal(err)
  432. }
  433. if _, err := f.Seek(0, 0); err != nil {
  434. f.Close()
  435. c.Fatal(err)
  436. }
  437. if err := f.Truncate(0); err != nil {
  438. f.Close()
  439. c.Fatal(err)
  440. }
  441. if _, err := f.Write([]byte("success2\n")); err != nil {
  442. f.Close()
  443. c.Fatal(err)
  444. }
  445. f.Close()
  446. res, _ := dockerCmd(c, "exec", contID, "cat", "/etc/"+fn)
  447. if res != "success2\n" {
  448. c.Fatalf("Expected content of %s: %q, got: %q", fn, "success2\n", res)
  449. }
  450. }
  451. }
  452. func (s *DockerSuite) TestExecWithUser(c *check.C) {
  453. testRequires(c, DaemonIsLinux)
  454. dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
  455. out, _ := dockerCmd(c, "exec", "-u", "1", "parent", "id")
  456. if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
  457. c.Fatalf("exec with user by id expected daemon user got %s", out)
  458. }
  459. out, _ = dockerCmd(c, "exec", "-u", "root", "parent", "id")
  460. if !strings.Contains(out, "uid=0(root) gid=0(root)") {
  461. c.Fatalf("exec with user by root expected root user got %s", out)
  462. }
  463. }
  464. func (s *DockerSuite) TestExecWithPrivileged(c *check.C) {
  465. testRequires(c, DaemonIsLinux)
  466. // Start main loop which attempts mknod repeatedly
  467. 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`)
  468. // Check exec mknod doesn't work
  469. cmd := exec.Command(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdb b 8 16")
  470. out, _, err := runCommandWithOutput(cmd)
  471. if err == nil || !strings.Contains(out, "Operation not permitted") {
  472. c.Fatalf("exec mknod in --cap-drop=ALL container without --privileged should fail")
  473. }
  474. // Check exec mknod does work with --privileged
  475. cmd = exec.Command(dockerBinary, "exec", "--privileged", "parent", "sh", "-c", `echo "Running exec --privileged" > /exec_priv && mknod /tmp/sdb b 8 16 && usleep 50000 && echo "Finished exec --privileged" > /exec_priv && echo ok`)
  476. out, _, err = runCommandWithOutput(cmd)
  477. if err != nil {
  478. c.Fatal(err, out)
  479. }
  480. if actual := strings.TrimSpace(out); actual != "ok" {
  481. c.Fatalf("exec mknod in --cap-drop=ALL container with --privileged failed: %v, output: %q", err, out)
  482. }
  483. // Check subsequent unprivileged exec cannot mknod
  484. cmd = exec.Command(dockerBinary, "exec", "parent", "sh", "-c", "mknod /tmp/sdc b 8 32")
  485. out, _, err = runCommandWithOutput(cmd)
  486. if err == nil || !strings.Contains(out, "Operation not permitted") {
  487. c.Fatalf("repeating exec mknod in --cap-drop=ALL container after --privileged without --privileged should fail")
  488. }
  489. // Confirm at no point was mknod allowed
  490. logCmd := exec.Command(dockerBinary, "logs", "parent")
  491. if out, _, err := runCommandWithOutput(logCmd); err != nil || strings.Contains(out, "Success") {
  492. c.Fatal(out, err)
  493. }
  494. }
  495. func (s *DockerSuite) TestExecWithImageUser(c *check.C) {
  496. testRequires(c, DaemonIsLinux)
  497. name := "testbuilduser"
  498. _, err := buildImage(name,
  499. `FROM busybox
  500. RUN echo 'dockerio:x:1001:1001::/bin:/bin/false' >> /etc/passwd
  501. USER dockerio`,
  502. true)
  503. if err != nil {
  504. c.Fatalf("Could not build image %s: %v", name, err)
  505. }
  506. dockerCmd(c, "run", "-d", "--name", "dockerioexec", name, "top")
  507. out, _ := dockerCmd(c, "exec", "dockerioexec", "whoami")
  508. if !strings.Contains(out, "dockerio") {
  509. c.Fatalf("exec with user by id expected dockerio user got %s", out)
  510. }
  511. }
  512. func (s *DockerSuite) TestExecOnReadonlyContainer(c *check.C) {
  513. testRequires(c, DaemonIsLinux)
  514. dockerCmd(c, "run", "-d", "--read-only", "--name", "parent", "busybox", "top")
  515. if _, status := dockerCmd(c, "exec", "parent", "true"); status != 0 {
  516. c.Fatalf("exec into a read-only container failed with exit status %d", status)
  517. }
  518. }
  519. // #15750
  520. // TODO Fix this test on windows #16738
  521. func (s *DockerSuite) TestExecStartFails(c *check.C) {
  522. testRequires(c, DaemonIsLinux, SameHostDaemon)
  523. name := "exec-15750"
  524. dockerCmd(c, "run", "-d", "--name", name, "busybox", "top")
  525. c.Assert(waitRun(name), check.IsNil)
  526. out, _, err := dockerCmdWithError("exec", name, "no-such-cmd")
  527. c.Assert(err, check.NotNil, check.Commentf(out))
  528. c.Assert(out, checker.Contains, "executable file not found")
  529. }