docker_cli_exec_test.go 17 KB

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