github.com/dlintw/docker@v1.5.0-rc4/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/filepath"
    11  	"strings"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/docker/docker/pkg/mount"
    16  	"github.com/kr/pty"
    17  )
    18  
    19  // #6509
    20  func TestRunRedirectStdout(t *testing.T) {
    21  
    22  	defer deleteAllContainers()
    23  
    24  	checkRedirect := func(command string) {
    25  		_, tty, err := pty.Open()
    26  		if err != nil {
    27  			t.Fatalf("Could not open pty: %v", err)
    28  		}
    29  		cmd := exec.Command("sh", "-c", command)
    30  		cmd.Stdin = tty
    31  		cmd.Stdout = tty
    32  		cmd.Stderr = tty
    33  		ch := make(chan struct{})
    34  		if err := cmd.Start(); err != nil {
    35  			t.Fatalf("start err: %v", err)
    36  		}
    37  		go func() {
    38  			if err := cmd.Wait(); err != nil {
    39  				t.Fatalf("wait err=%v", err)
    40  			}
    41  			close(ch)
    42  		}()
    43  
    44  		select {
    45  		case <-time.After(10 * time.Second):
    46  			t.Fatal("command timeout")
    47  		case <-ch:
    48  		}
    49  	}
    50  
    51  	checkRedirect(dockerBinary + " run -i busybox cat /etc/passwd | grep -q root")
    52  	checkRedirect(dockerBinary + " run busybox cat /etc/passwd | grep -q root")
    53  
    54  	logDone("run - redirect stdout")
    55  }
    56  
    57  // Test recursive bind mount works by default
    58  func TestRunWithVolumesIsRecursive(t *testing.T) {
    59  	tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	defer os.RemoveAll(tmpDir)
    65  
    66  	// Create a temporary tmpfs mount.
    67  	tmpfsDir := filepath.Join(tmpDir, "tmpfs")
    68  	if err := os.MkdirAll(tmpfsDir, 0777); err != nil {
    69  		t.Fatalf("failed to mkdir at %s - %s", tmpfsDir, err)
    70  	}
    71  	if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
    72  		t.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
    73  	}
    74  
    75  	f, err := ioutil.TempFile(tmpfsDir, "touch-me")
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	defer f.Close()
    80  
    81  	runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
    82  	out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
    83  	if err != nil && exitCode != 0 {
    84  		t.Fatal(out, stderr, err)
    85  	}
    86  	if !strings.Contains(out, filepath.Base(f.Name())) {
    87  		t.Fatal("Recursive bind mount test failed. Expected file not found")
    88  	}
    89  
    90  	deleteAllContainers()
    91  
    92  	logDone("run - volumes are bind mounted recursively")
    93  }