docker_cli_exec_test.go 3.6 KB

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