docker_cli_run_unix_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // +build !windows
  2. package main
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path"
  9. "path/filepath"
  10. "strings"
  11. "testing"
  12. "time"
  13. "github.com/docker/docker/pkg/mount"
  14. "github.com/kr/pty"
  15. )
  16. // #6509
  17. func TestRunRedirectStdout(t *testing.T) {
  18. defer deleteAllContainers()
  19. checkRedirect := func(command string) {
  20. _, tty, err := pty.Open()
  21. if err != nil {
  22. t.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. t.Fatalf("start err: %v", err)
  31. }
  32. go func() {
  33. if err := cmd.Wait(); err != nil {
  34. t.Fatalf("wait err=%v", err)
  35. }
  36. close(ch)
  37. }()
  38. select {
  39. case <-time.After(10 * time.Second):
  40. t.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. logDone("run - redirect stdout")
  47. }
  48. // Test recursive bind mount works by default
  49. func TestRunWithVolumesIsRecursive(t *testing.T) {
  50. defer deleteAllContainers()
  51. tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. defer os.RemoveAll(tmpDir)
  56. // Create a temporary tmpfs mount.
  57. tmpfsDir := filepath.Join(tmpDir, "tmpfs")
  58. if err := os.MkdirAll(tmpfsDir, 0777); err != nil {
  59. t.Fatalf("failed to mkdir at %s - %s", tmpfsDir, err)
  60. }
  61. if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
  62. t.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
  63. }
  64. f, err := ioutil.TempFile(tmpfsDir, "touch-me")
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. defer f.Close()
  69. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
  70. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  71. if err != nil && exitCode != 0 {
  72. t.Fatal(out, stderr, err)
  73. }
  74. if !strings.Contains(out, filepath.Base(f.Name())) {
  75. t.Fatal("Recursive bind mount test failed. Expected file not found")
  76. }
  77. logDone("run - volumes are bind mounted recursively")
  78. }
  79. func TestRunWithUlimits(t *testing.T) {
  80. testRequires(t, NativeExecDriver)
  81. defer deleteAllContainers()
  82. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name=testulimits", "--ulimit", "nofile=42", "busybox", "/bin/sh", "-c", "ulimit -n"))
  83. if err != nil {
  84. t.Fatal(err, out)
  85. }
  86. ul := strings.TrimSpace(out)
  87. if ul != "42" {
  88. t.Fatalf("expected `ulimit -n` to be 42, got %s", ul)
  89. }
  90. logDone("run - ulimits are set")
  91. }
  92. func TestRunContainerWithCgroupParent(t *testing.T) {
  93. testRequires(t, NativeExecDriver)
  94. defer deleteAllContainers()
  95. cgroupParent := "test"
  96. data, err := ioutil.ReadFile("/proc/self/cgroup")
  97. if err != nil {
  98. t.Fatalf("failed to read '/proc/self/cgroup - %v", err)
  99. }
  100. selfCgroupPaths := parseCgroupPaths(string(data))
  101. selfCpuCgroup, found := selfCgroupPaths["memory"]
  102. if !found {
  103. t.Fatalf("unable to find self cpu cgroup path. CgroupsPath: %v", selfCgroupPaths)
  104. }
  105. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
  106. if err != nil {
  107. t.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
  108. }
  109. cgroupPaths := parseCgroupPaths(string(out))
  110. if len(cgroupPaths) == 0 {
  111. t.Fatalf("unexpected output - %q", string(out))
  112. }
  113. found = false
  114. expectedCgroupPrefix := path.Join(selfCpuCgroup, cgroupParent)
  115. for _, path := range cgroupPaths {
  116. if strings.HasPrefix(path, expectedCgroupPrefix) {
  117. found = true
  118. break
  119. }
  120. }
  121. if !found {
  122. t.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have prefix %q. Cgroup Paths: %v", expectedCgroupPrefix, cgroupPaths)
  123. }
  124. logDone("run - cgroup parent")
  125. }
  126. func TestRunContainerWithCgroupParentAbsPath(t *testing.T) {
  127. testRequires(t, NativeExecDriver)
  128. defer deleteAllContainers()
  129. cgroupParent := "/cgroup-parent/test"
  130. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
  131. if err != nil {
  132. t.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
  133. }
  134. cgroupPaths := parseCgroupPaths(string(out))
  135. if len(cgroupPaths) == 0 {
  136. t.Fatalf("unexpected output - %q", string(out))
  137. }
  138. found := false
  139. for _, path := range cgroupPaths {
  140. if strings.HasPrefix(path, cgroupParent) {
  141. found = true
  142. break
  143. }
  144. }
  145. if !found {
  146. t.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have prefix %q. Cgroup Paths: %v", cgroupParent, cgroupPaths)
  147. }
  148. logDone("run - cgroup parent with absolute cgroup path")
  149. }
  150. func TestRunDeviceDirectory(t *testing.T) {
  151. testRequires(t, NativeExecDriver)
  152. defer deleteAllContainers()
  153. cmd := exec.Command(dockerBinary, "run", "--device", "/dev/snd:/dev/snd", "busybox", "sh", "-c", "ls /dev/snd/")
  154. out, _, err := runCommandWithOutput(cmd)
  155. if err != nil {
  156. t.Fatal(err, out)
  157. }
  158. if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "timer") {
  159. t.Fatalf("expected output /dev/snd/timer, received %s", actual)
  160. }
  161. cmd = exec.Command(dockerBinary, "run", "--device", "/dev/snd:/dev/othersnd", "busybox", "sh", "-c", "ls /dev/othersnd/")
  162. out, _, err = runCommandWithOutput(cmd)
  163. if err != nil {
  164. t.Fatal(err, out)
  165. }
  166. if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "seq") {
  167. t.Fatalf("expected output /dev/othersnd/timer, received %s", actual)
  168. }
  169. logDone("run - test --device directory mounts all internal devices")
  170. }