123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- // +build !windows
- package main
- import (
- "bufio"
- "fmt"
- "io/ioutil"
- "os"
- "os/exec"
- "path"
- "path/filepath"
- "strings"
- "time"
- "github.com/docker/docker/pkg/mount"
- "github.com/go-check/check"
- "github.com/kr/pty"
- )
- // #6509
- func (s *DockerSuite) TestRunRedirectStdout(c *check.C) {
- checkRedirect := func(command string) {
- _, tty, err := pty.Open()
- if err != nil {
- c.Fatalf("Could not open pty: %v", err)
- }
- cmd := exec.Command("sh", "-c", command)
- cmd.Stdin = tty
- cmd.Stdout = tty
- cmd.Stderr = tty
- ch := make(chan struct{})
- if err := cmd.Start(); err != nil {
- c.Fatalf("start err: %v", err)
- }
- go func() {
- if err := cmd.Wait(); err != nil {
- c.Fatalf("wait err=%v", err)
- }
- close(ch)
- }()
- select {
- case <-time.After(10 * time.Second):
- c.Fatal("command timeout")
- case <-ch:
- }
- }
- checkRedirect(dockerBinary + " run -i busybox cat /etc/passwd | grep -q root")
- checkRedirect(dockerBinary + " run busybox cat /etc/passwd | grep -q root")
- }
- // Test recursive bind mount works by default
- func (s *DockerSuite) TestRunWithVolumesIsRecursive(c *check.C) {
- tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
- if err != nil {
- c.Fatal(err)
- }
- defer os.RemoveAll(tmpDir)
- // Create a temporary tmpfs mount.
- tmpfsDir := filepath.Join(tmpDir, "tmpfs")
- if err := os.MkdirAll(tmpfsDir, 0777); err != nil {
- c.Fatalf("failed to mkdir at %s - %s", tmpfsDir, err)
- }
- if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
- c.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
- }
- f, err := ioutil.TempFile(tmpfsDir, "touch-me")
- if err != nil {
- c.Fatal(err)
- }
- defer f.Close()
- runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
- out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
- if err != nil && exitCode != 0 {
- c.Fatal(out, stderr, err)
- }
- if !strings.Contains(out, filepath.Base(f.Name())) {
- c.Fatal("Recursive bind mount test failed. Expected file not found")
- }
- }
- func (s *DockerSuite) TestRunWithUlimits(c *check.C) {
- testRequires(c, NativeExecDriver)
- out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name=testulimits", "--ulimit", "nofile=42", "busybox", "/bin/sh", "-c", "ulimit -n"))
- if err != nil {
- c.Fatal(err, out)
- }
- ul := strings.TrimSpace(out)
- if ul != "42" {
- c.Fatalf("expected `ulimit -n` to be 42, got %s", ul)
- }
- }
- func (s *DockerSuite) TestRunContainerWithCgroupParent(c *check.C) {
- testRequires(c, NativeExecDriver)
- cgroupParent := "test"
- data, err := ioutil.ReadFile("/proc/self/cgroup")
- if err != nil {
- c.Fatalf("failed to read '/proc/self/cgroup - %v", err)
- }
- selfCgroupPaths := parseCgroupPaths(string(data))
- selfCpuCgroup, found := selfCgroupPaths["memory"]
- if !found {
- c.Fatalf("unable to find self cpu cgroup path. CgroupsPath: %v", selfCgroupPaths)
- }
- out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
- if err != nil {
- c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
- }
- cgroupPaths := parseCgroupPaths(string(out))
- if len(cgroupPaths) == 0 {
- c.Fatalf("unexpected output - %q", string(out))
- }
- found = false
- expectedCgroupPrefix := path.Join(selfCpuCgroup, cgroupParent)
- for _, path := range cgroupPaths {
- if strings.HasPrefix(path, expectedCgroupPrefix) {
- found = true
- break
- }
- }
- if !found {
- c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have prefix %q. Cgroup Paths: %v", expectedCgroupPrefix, cgroupPaths)
- }
- }
- func (s *DockerSuite) TestRunContainerWithCgroupParentAbsPath(c *check.C) {
- testRequires(c, NativeExecDriver)
- cgroupParent := "/cgroup-parent/test"
- out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
- if err != nil {
- c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
- }
- cgroupPaths := parseCgroupPaths(string(out))
- if len(cgroupPaths) == 0 {
- c.Fatalf("unexpected output - %q", string(out))
- }
- found := false
- for _, path := range cgroupPaths {
- if strings.HasPrefix(path, cgroupParent) {
- found = true
- break
- }
- }
- if !found {
- c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have prefix %q. Cgroup Paths: %v", cgroupParent, cgroupPaths)
- }
- }
- func (s *DockerSuite) TestRunDeviceDirectory(c *check.C) {
- testRequires(c, NativeExecDriver)
- cmd := exec.Command(dockerBinary, "run", "--device", "/dev/snd:/dev/snd", "busybox", "sh", "-c", "ls /dev/snd/")
- out, _, err := runCommandWithOutput(cmd)
- if err != nil {
- c.Fatal(err, out)
- }
- if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "timer") {
- c.Fatalf("expected output /dev/snd/timer, received %s", actual)
- }
- cmd = exec.Command(dockerBinary, "run", "--device", "/dev/snd:/dev/othersnd", "busybox", "sh", "-c", "ls /dev/othersnd/")
- out, _, err = runCommandWithOutput(cmd)
- if err != nil {
- c.Fatal(err, out)
- }
- if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "seq") {
- c.Fatalf("expected output /dev/othersnd/seq, received %s", actual)
- }
- }
- // TestRunDetach checks attaching and detaching with the escape sequence.
- func (s *DockerSuite) TestRunAttachDetach(c *check.C) {
- name := "attach-detach"
- cmd := exec.Command(dockerBinary, "run", "--name", name, "-it", "busybox", "cat")
- stdout, err := cmd.StdoutPipe()
- if err != nil {
- c.Fatal(err)
- }
- cpty, tty, err := pty.Open()
- if err != nil {
- c.Fatal(err)
- }
- defer cpty.Close()
- cmd.Stdin = tty
- if err := cmd.Start(); err != nil {
- c.Fatal(err)
- }
- if err := waitRun(name); err != nil {
- c.Fatal(err)
- }
- if _, err := cpty.Write([]byte("hello\n")); err != nil {
- c.Fatal(err)
- }
- out, err := bufio.NewReader(stdout).ReadString('\n')
- if err != nil {
- c.Fatal(err)
- }
- if strings.TrimSpace(out) != "hello" {
- c.Fatalf("exepected 'hello', got %q", out)
- }
- // escape sequence
- if _, err := cpty.Write([]byte{16}); err != nil {
- c.Fatal(err)
- }
- time.Sleep(100 * time.Millisecond)
- if _, err := cpty.Write([]byte{17}); err != nil {
- c.Fatal(err)
- }
- ch := make(chan struct{})
- go func() {
- cmd.Wait()
- ch <- struct{}{}
- }()
- running, err := inspectField(name, "State.Running")
- if err != nil {
- c.Fatal(err)
- }
- if running != "true" {
- c.Fatal("exepected container to still be running")
- }
- go func() {
- exec.Command(dockerBinary, "kill", name).Run()
- }()
- select {
- case <-ch:
- case <-time.After(10 * time.Millisecond):
- c.Fatal("timed out waiting for container to exit")
- }
- }
|