docker_cli_exec_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package main
  2. import (
  3. "bufio"
  4. "os/exec"
  5. "strings"
  6. "testing"
  7. "time"
  8. )
  9. func TestExec(t *testing.T) {
  10. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100")
  11. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  12. errorOut(err, t, out)
  13. execCmd := exec.Command(dockerBinary, "exec", "testing", "cat", "/tmp/file")
  14. out, _, err = runCommandWithOutput(execCmd)
  15. errorOut(err, t, out)
  16. out = strings.Trim(out, "\r\n")
  17. if expected := "test"; out != expected {
  18. t.Errorf("container exec should've printed %q but printed %q", expected, out)
  19. }
  20. deleteAllContainers()
  21. logDone("exec - basic test")
  22. }
  23. func TestExecInteractive(t *testing.T) {
  24. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100")
  25. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  26. errorOut(err, t, out)
  27. execCmd := exec.Command(dockerBinary, "exec", "-i", "testing", "sh")
  28. stdin, err := execCmd.StdinPipe()
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. stdout, err := execCmd.StdoutPipe()
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. if err := execCmd.Start(); err != nil {
  37. t.Fatal(err)
  38. }
  39. if _, err := stdin.Write([]byte("cat /tmp/file\n")); err != nil {
  40. t.Fatal(err)
  41. }
  42. r := bufio.NewReader(stdout)
  43. line, err := r.ReadString('\n')
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. line = strings.TrimSpace(line)
  48. if line != "test" {
  49. t.Fatalf("Output should be 'test', got '%q'", line)
  50. }
  51. if err := stdin.Close(); err != nil {
  52. t.Fatal(err)
  53. }
  54. finish := make(chan struct{})
  55. go func() {
  56. if err := execCmd.Wait(); err != nil {
  57. t.Fatal(err)
  58. }
  59. close(finish)
  60. }()
  61. select {
  62. case <-finish:
  63. case <-time.After(1 * time.Second):
  64. t.Fatal("docker exec failed to exit on stdin close")
  65. }
  66. deleteAllContainers()
  67. logDone("exec - Interactive test")
  68. }
  69. func TestExecAfterContainerRestart(t *testing.T) {
  70. runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
  71. out, _, err := runCommandWithOutput(runCmd)
  72. errorOut(err, t, out)
  73. cleanedContainerID := stripTrailingCharacters(out)
  74. runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
  75. out, _, err = runCommandWithOutput(runCmd)
  76. errorOut(err, t, out)
  77. runCmd = exec.Command(dockerBinary, "exec", cleanedContainerID, "echo", "hello")
  78. out, _, err = runCommandWithOutput(runCmd)
  79. errorOut(err, t, out)
  80. outStr := strings.TrimSpace(out)
  81. if outStr != "hello" {
  82. t.Errorf("container should've printed hello, instead printed %q", outStr)
  83. }
  84. deleteAllContainers()
  85. logDone("exec - exec running container after container restart")
  86. }
  87. func TestExecAfterDaemonRestart(t *testing.T) {
  88. d := NewDaemon(t)
  89. if err := d.StartWithBusybox(); err != nil {
  90. t.Fatalf("Could not start daemon with busybox: %v", err)
  91. }
  92. defer d.Stop()
  93. if out, err := d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
  94. t.Fatalf("Could not run top: err=%v\n%s", err, out)
  95. }
  96. if err := d.Restart(); err != nil {
  97. t.Fatalf("Could not restart daemon: %v", err)
  98. }
  99. if out, err := d.Cmd("start", "top"); err != nil {
  100. t.Fatalf("Could not start top after daemon restart: err=%v\n%s", err, out)
  101. }
  102. out, err := d.Cmd("exec", "top", "echo", "hello")
  103. if err != nil {
  104. t.Fatalf("Could not exec on container top: err=%v\n%s", err, out)
  105. }
  106. outStr := strings.TrimSpace(string(out))
  107. if outStr != "hello" {
  108. t.Errorf("container should've printed hello, instead printed %q", outStr)
  109. }
  110. logDone("exec - exec running container after daemon restart")
  111. }