docker_cli_run_unix_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 getCgroupPaths(test string) map[string]string {
  93. cgroupPaths := map[string]string{}
  94. for _, line := range strings.Split(test, "\n") {
  95. line = strings.TrimSpace(line)
  96. if line == "" {
  97. continue
  98. }
  99. parts := strings.Split(line, ":")
  100. if len(parts) != 3 {
  101. fmt.Printf("unexpected file format for /proc/self/cgroup - %q\n", line)
  102. continue
  103. }
  104. cgroupPaths[parts[1]] = parts[2]
  105. }
  106. return cgroupPaths
  107. }
  108. func TestRunContainerWithCgroupParent(t *testing.T) {
  109. testRequires(t, NativeExecDriver)
  110. defer deleteAllContainers()
  111. cgroupParent := "test"
  112. data, err := ioutil.ReadFile("/proc/self/cgroup")
  113. if err != nil {
  114. t.Fatalf("failed to read '/proc/self/cgroup - %v", err)
  115. }
  116. selfCgroupPaths := getCgroupPaths(string(data))
  117. selfCpuCgroup, found := selfCgroupPaths["memory"]
  118. if !found {
  119. t.Fatalf("unable to find self cpu cgroup path. CgroupsPath: %v", selfCgroupPaths)
  120. }
  121. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
  122. if err != nil {
  123. t.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
  124. }
  125. cgroupPaths := getCgroupPaths(string(out))
  126. if len(cgroupPaths) == 0 {
  127. t.Fatalf("unexpected output - %q", string(out))
  128. }
  129. found = false
  130. expectedCgroupPrefix := path.Join(selfCpuCgroup, cgroupParent)
  131. for _, path := range cgroupPaths {
  132. if strings.HasPrefix(path, expectedCgroupPrefix) {
  133. found = true
  134. break
  135. }
  136. }
  137. if !found {
  138. t.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have prefix %q. Cgroup Paths: %v", expectedCgroupPrefix, cgroupPaths)
  139. }
  140. logDone("run - cgroup parent")
  141. }
  142. func TestRunContainerWithCgroupParentAbsPath(t *testing.T) {
  143. testRequires(t, NativeExecDriver)
  144. defer deleteAllContainers()
  145. cgroupParent := "/cgroup-parent/test"
  146. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
  147. if err != nil {
  148. t.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
  149. }
  150. cgroupPaths := getCgroupPaths(string(out))
  151. if len(cgroupPaths) == 0 {
  152. t.Fatalf("unexpected output - %q", string(out))
  153. }
  154. found := false
  155. for _, path := range cgroupPaths {
  156. if strings.HasPrefix(path, cgroupParent) {
  157. found = true
  158. break
  159. }
  160. }
  161. if !found {
  162. t.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have prefix %q. Cgroup Paths: %v", cgroupParent, cgroupPaths)
  163. }
  164. logDone("run - cgroup parent with absolute cgroup path")
  165. }