docker_cli_exec_test.go 17 KB

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