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