docker_cli_exec_test.go 17 KB

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