docker_cli_run_unix_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // +build !windows
  2. package main
  3. import (
  4. "bufio"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "path"
  10. "path/filepath"
  11. "strings"
  12. "time"
  13. "github.com/docker/docker/pkg/mount"
  14. "github.com/go-check/check"
  15. "github.com/kr/pty"
  16. )
  17. // #6509
  18. func (s *DockerSuite) TestRunRedirectStdout(c *check.C) {
  19. checkRedirect := func(command string) {
  20. _, tty, err := pty.Open()
  21. if err != nil {
  22. c.Fatalf("Could not open pty: %v", err)
  23. }
  24. cmd := exec.Command("sh", "-c", command)
  25. cmd.Stdin = tty
  26. cmd.Stdout = tty
  27. cmd.Stderr = tty
  28. ch := make(chan struct{})
  29. if err := cmd.Start(); err != nil {
  30. c.Fatalf("start err: %v", err)
  31. }
  32. go func() {
  33. if err := cmd.Wait(); err != nil {
  34. c.Fatalf("wait err=%v", err)
  35. }
  36. close(ch)
  37. }()
  38. select {
  39. case <-time.After(10 * time.Second):
  40. c.Fatal("command timeout")
  41. case <-ch:
  42. }
  43. }
  44. checkRedirect(dockerBinary + " run -i busybox cat /etc/passwd | grep -q root")
  45. checkRedirect(dockerBinary + " run busybox cat /etc/passwd | grep -q root")
  46. }
  47. // Test recursive bind mount works by default
  48. func (s *DockerSuite) TestRunWithVolumesIsRecursive(c *check.C) {
  49. tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
  50. if err != nil {
  51. c.Fatal(err)
  52. }
  53. defer os.RemoveAll(tmpDir)
  54. // Create a temporary tmpfs mount.
  55. tmpfsDir := filepath.Join(tmpDir, "tmpfs")
  56. if err := os.MkdirAll(tmpfsDir, 0777); err != nil {
  57. c.Fatalf("failed to mkdir at %s - %s", tmpfsDir, err)
  58. }
  59. if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
  60. c.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
  61. }
  62. f, err := ioutil.TempFile(tmpfsDir, "touch-me")
  63. if err != nil {
  64. c.Fatal(err)
  65. }
  66. defer f.Close()
  67. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
  68. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  69. if err != nil && exitCode != 0 {
  70. c.Fatal(out, stderr, err)
  71. }
  72. if !strings.Contains(out, filepath.Base(f.Name())) {
  73. c.Fatal("Recursive bind mount test failed. Expected file not found")
  74. }
  75. }
  76. func (s *DockerSuite) TestRunWithUlimits(c *check.C) {
  77. testRequires(c, NativeExecDriver)
  78. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name=testulimits", "--ulimit", "nofile=42", "busybox", "/bin/sh", "-c", "ulimit -n"))
  79. if err != nil {
  80. c.Fatal(err, out)
  81. }
  82. ul := strings.TrimSpace(out)
  83. if ul != "42" {
  84. c.Fatalf("expected `ulimit -n` to be 42, got %s", ul)
  85. }
  86. }
  87. func (s *DockerSuite) TestRunContainerWithCgroupParent(c *check.C) {
  88. testRequires(c, NativeExecDriver)
  89. cgroupParent := "test"
  90. data, err := ioutil.ReadFile("/proc/self/cgroup")
  91. if err != nil {
  92. c.Fatalf("failed to read '/proc/self/cgroup - %v", err)
  93. }
  94. selfCgroupPaths := parseCgroupPaths(string(data))
  95. selfCpuCgroup, found := selfCgroupPaths["memory"]
  96. if !found {
  97. c.Fatalf("unable to find self cpu cgroup path. CgroupsPath: %v", selfCgroupPaths)
  98. }
  99. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
  100. if err != nil {
  101. c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
  102. }
  103. cgroupPaths := parseCgroupPaths(string(out))
  104. if len(cgroupPaths) == 0 {
  105. c.Fatalf("unexpected output - %q", string(out))
  106. }
  107. found = false
  108. expectedCgroupPrefix := path.Join(selfCpuCgroup, cgroupParent)
  109. for _, path := range cgroupPaths {
  110. if strings.HasPrefix(path, expectedCgroupPrefix) {
  111. found = true
  112. break
  113. }
  114. }
  115. if !found {
  116. c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have prefix %q. Cgroup Paths: %v", expectedCgroupPrefix, cgroupPaths)
  117. }
  118. }
  119. func (s *DockerSuite) TestRunContainerWithCgroupParentAbsPath(c *check.C) {
  120. testRequires(c, NativeExecDriver)
  121. cgroupParent := "/cgroup-parent/test"
  122. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
  123. if err != nil {
  124. c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
  125. }
  126. cgroupPaths := parseCgroupPaths(string(out))
  127. if len(cgroupPaths) == 0 {
  128. c.Fatalf("unexpected output - %q", string(out))
  129. }
  130. found := false
  131. for _, path := range cgroupPaths {
  132. if strings.HasPrefix(path, cgroupParent) {
  133. found = true
  134. break
  135. }
  136. }
  137. if !found {
  138. c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have prefix %q. Cgroup Paths: %v", cgroupParent, cgroupPaths)
  139. }
  140. }
  141. func (s *DockerSuite) TestRunDeviceDirectory(c *check.C) {
  142. testRequires(c, NativeExecDriver)
  143. cmd := exec.Command(dockerBinary, "run", "--device", "/dev/snd:/dev/snd", "busybox", "sh", "-c", "ls /dev/snd/")
  144. out, _, err := runCommandWithOutput(cmd)
  145. if err != nil {
  146. c.Fatal(err, out)
  147. }
  148. if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "timer") {
  149. c.Fatalf("expected output /dev/snd/timer, received %s", actual)
  150. }
  151. cmd = exec.Command(dockerBinary, "run", "--device", "/dev/snd:/dev/othersnd", "busybox", "sh", "-c", "ls /dev/othersnd/")
  152. out, _, err = runCommandWithOutput(cmd)
  153. if err != nil {
  154. c.Fatal(err, out)
  155. }
  156. if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "seq") {
  157. c.Fatalf("expected output /dev/othersnd/seq, received %s", actual)
  158. }
  159. }
  160. // TestRunDetach checks attaching and detaching with the escape sequence.
  161. func (s *DockerSuite) TestRunAttachDetach(c *check.C) {
  162. name := "attach-detach"
  163. cmd := exec.Command(dockerBinary, "run", "--name", name, "-it", "busybox", "cat")
  164. stdout, err := cmd.StdoutPipe()
  165. if err != nil {
  166. c.Fatal(err)
  167. }
  168. cpty, tty, err := pty.Open()
  169. if err != nil {
  170. c.Fatal(err)
  171. }
  172. defer cpty.Close()
  173. cmd.Stdin = tty
  174. if err := cmd.Start(); err != nil {
  175. c.Fatal(err)
  176. }
  177. if err := waitRun(name); err != nil {
  178. c.Fatal(err)
  179. }
  180. if _, err := cpty.Write([]byte("hello\n")); err != nil {
  181. c.Fatal(err)
  182. }
  183. out, err := bufio.NewReader(stdout).ReadString('\n')
  184. if err != nil {
  185. c.Fatal(err)
  186. }
  187. if strings.TrimSpace(out) != "hello" {
  188. c.Fatalf("exepected 'hello', got %q", out)
  189. }
  190. // escape sequence
  191. if _, err := cpty.Write([]byte{16}); err != nil {
  192. c.Fatal(err)
  193. }
  194. time.Sleep(100 * time.Millisecond)
  195. if _, err := cpty.Write([]byte{17}); err != nil {
  196. c.Fatal(err)
  197. }
  198. ch := make(chan struct{})
  199. go func() {
  200. cmd.Wait()
  201. ch <- struct{}{}
  202. }()
  203. running, err := inspectField(name, "State.Running")
  204. if err != nil {
  205. c.Fatal(err)
  206. }
  207. if running != "true" {
  208. c.Fatal("exepected container to still be running")
  209. }
  210. go func() {
  211. exec.Command(dockerBinary, "kill", name).Run()
  212. }()
  213. select {
  214. case <-ch:
  215. case <-time.After(10 * time.Millisecond):
  216. c.Fatal("timed out waiting for container to exit")
  217. }
  218. }