docker_cli_run_unix_test.go 7.0 KB

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