docker_cli_run_unix_test.go 2.1 KB

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