docker_cli_exec_test.go 17 KB

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