docker_cli_run_unix_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. defer deleteAllContainers()
  50. tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. defer os.RemoveAll(tmpDir)
  55. // Create a temporary tmpfs mount.
  56. tmpfsDir := filepath.Join(tmpDir, "tmpfs")
  57. if err := os.MkdirAll(tmpfsDir, 0777); err != nil {
  58. t.Fatalf("failed to mkdir at %s - %s", tmpfsDir, err)
  59. }
  60. if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
  61. t.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
  62. }
  63. f, err := ioutil.TempFile(tmpfsDir, "touch-me")
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. defer f.Close()
  68. runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
  69. out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
  70. if err != nil && exitCode != 0 {
  71. t.Fatal(out, stderr, err)
  72. }
  73. if !strings.Contains(out, filepath.Base(f.Name())) {
  74. t.Fatal("Recursive bind mount test failed. Expected file not found")
  75. }
  76. logDone("run - volumes are bind mounted recursively")
  77. }
  78. func TestRunWithUlimits(t *testing.T) {
  79. testRequires(t, NativeExecDriver)
  80. defer deleteAllContainers()
  81. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name=testulimits", "--ulimit", "nofile=42", "busybox", "/bin/sh", "-c", "ulimit -n"))
  82. if err != nil {
  83. t.Fatal(err, out)
  84. }
  85. ul := strings.TrimSpace(out)
  86. if ul != "42" {
  87. t.Fatalf("expected `ulimit -n` to be 42, got %s", ul)
  88. }
  89. logDone("run - ulimits are set")
  90. }