docker_cli_logs_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "os/exec"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/docker/docker/pkg/timeutils"
  12. "github.com/go-check/check"
  13. )
  14. // This used to work, it test a log of PageSize-1 (gh#4851)
  15. func (s *DockerSuite) TestLogsContainerSmallerThanPage(c *check.C) {
  16. testLen := 32767
  17. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", testLen))
  18. cleanedContainerID := strings.TrimSpace(out)
  19. dockerCmd(c, "wait", cleanedContainerID)
  20. out, _ = dockerCmd(c, "logs", cleanedContainerID)
  21. if len(out) != testLen+1 {
  22. c.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
  23. }
  24. }
  25. // Regression test: When going over the PageSize, it used to panic (gh#4851)
  26. func (s *DockerSuite) TestLogsContainerBiggerThanPage(c *check.C) {
  27. testLen := 32768
  28. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", testLen))
  29. cleanedContainerID := strings.TrimSpace(out)
  30. dockerCmd(c, "wait", cleanedContainerID)
  31. out, _ = dockerCmd(c, "logs", cleanedContainerID)
  32. if len(out) != testLen+1 {
  33. c.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
  34. }
  35. }
  36. // Regression test: When going much over the PageSize, it used to block (gh#4851)
  37. func (s *DockerSuite) TestLogsContainerMuchBiggerThanPage(c *check.C) {
  38. testLen := 33000
  39. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n =; done; echo", testLen))
  40. cleanedContainerID := strings.TrimSpace(out)
  41. dockerCmd(c, "wait", cleanedContainerID)
  42. out, _ = dockerCmd(c, "logs", cleanedContainerID)
  43. if len(out) != testLen+1 {
  44. c.Fatalf("Expected log length of %d, received %d\n", testLen+1, len(out))
  45. }
  46. }
  47. func (s *DockerSuite) TestLogsTimestamps(c *check.C) {
  48. testLen := 100
  49. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen))
  50. cleanedContainerID := strings.TrimSpace(out)
  51. dockerCmd(c, "wait", cleanedContainerID)
  52. out, _ = dockerCmd(c, "logs", "-t", cleanedContainerID)
  53. lines := strings.Split(out, "\n")
  54. if len(lines) != testLen+1 {
  55. c.Fatalf("Expected log %d lines, received %d\n", testLen+1, len(lines))
  56. }
  57. ts := regexp.MustCompile(`^.* `)
  58. for _, l := range lines {
  59. if l != "" {
  60. _, err := time.Parse(timeutils.RFC3339NanoFixed+" ", ts.FindString(l))
  61. if err != nil {
  62. c.Fatalf("Failed to parse timestamp from %v: %v", l, err)
  63. }
  64. if l[29] != 'Z' { // ensure we have padded 0's
  65. c.Fatalf("Timestamp isn't padded properly: %s", l)
  66. }
  67. }
  68. }
  69. }
  70. func (s *DockerSuite) TestLogsSeparateStderr(c *check.C) {
  71. msg := "stderr_log"
  72. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
  73. cleanedContainerID := strings.TrimSpace(out)
  74. dockerCmd(c, "wait", cleanedContainerID)
  75. stdout, stderr, _ := dockerCmdWithStdoutStderr(c, "logs", cleanedContainerID)
  76. if stdout != "" {
  77. c.Fatalf("Expected empty stdout stream, got %v", stdout)
  78. }
  79. stderr = strings.TrimSpace(stderr)
  80. if stderr != msg {
  81. c.Fatalf("Expected %v in stderr stream, got %v", msg, stderr)
  82. }
  83. }
  84. func (s *DockerSuite) TestLogsStderrInStdout(c *check.C) {
  85. msg := "stderr_log"
  86. out, _ := dockerCmd(c, "run", "-d", "-t", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
  87. cleanedContainerID := strings.TrimSpace(out)
  88. dockerCmd(c, "wait", cleanedContainerID)
  89. stdout, stderr, _ := dockerCmdWithStdoutStderr(c, "logs", cleanedContainerID)
  90. if stderr != "" {
  91. c.Fatalf("Expected empty stderr stream, got %v", stderr)
  92. }
  93. stdout = strings.TrimSpace(stdout)
  94. if stdout != msg {
  95. c.Fatalf("Expected %v in stdout stream, got %v", msg, stdout)
  96. }
  97. }
  98. func (s *DockerSuite) TestLogsTail(c *check.C) {
  99. testLen := 100
  100. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen))
  101. cleanedContainerID := strings.TrimSpace(out)
  102. dockerCmd(c, "wait", cleanedContainerID)
  103. out, _ = dockerCmd(c, "logs", "--tail", "5", cleanedContainerID)
  104. lines := strings.Split(out, "\n")
  105. if len(lines) != 6 {
  106. c.Fatalf("Expected log %d lines, received %d\n", 6, len(lines))
  107. }
  108. out, _ = dockerCmd(c, "logs", "--tail", "all", cleanedContainerID)
  109. lines = strings.Split(out, "\n")
  110. if len(lines) != testLen+1 {
  111. c.Fatalf("Expected log %d lines, received %d\n", testLen+1, len(lines))
  112. }
  113. out, _, _ = dockerCmdWithStdoutStderr(c, "logs", "--tail", "random", cleanedContainerID)
  114. lines = strings.Split(out, "\n")
  115. if len(lines) != testLen+1 {
  116. c.Fatalf("Expected log %d lines, received %d\n", testLen+1, len(lines))
  117. }
  118. }
  119. func (s *DockerSuite) TestLogsFollowStopped(c *check.C) {
  120. out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "hello")
  121. cleanedContainerID := strings.TrimSpace(out)
  122. dockerCmd(c, "wait", cleanedContainerID)
  123. logsCmd := exec.Command(dockerBinary, "logs", "-f", cleanedContainerID)
  124. if err := logsCmd.Start(); err != nil {
  125. c.Fatal(err)
  126. }
  127. errChan := make(chan error)
  128. go func() {
  129. errChan <- logsCmd.Wait()
  130. close(errChan)
  131. }()
  132. select {
  133. case err := <-errChan:
  134. c.Assert(err, check.IsNil)
  135. case <-time.After(1 * time.Second):
  136. c.Fatal("Following logs is hanged")
  137. }
  138. }
  139. func (s *DockerSuite) TestLogsSince(c *check.C) {
  140. name := "testlogssince"
  141. out, _ := dockerCmd(c, "run", "--name="+name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do sleep 2; echo `date +%s` log$i; done")
  142. log2Line := strings.Split(strings.Split(out, "\n")[1], " ")
  143. t, err := strconv.ParseInt(log2Line[0], 10, 64) // the timestamp log2 is writen
  144. c.Assert(err, check.IsNil)
  145. since := t + 1 // add 1s so log1 & log2 doesn't show up
  146. out, _ = dockerCmd(c, "logs", "-t", fmt.Sprintf("--since=%v", since), name)
  147. // Skip 2 seconds
  148. unexpected := []string{"log1", "log2"}
  149. for _, v := range unexpected {
  150. if strings.Contains(out, v) {
  151. c.Fatalf("unexpected log message returned=%v, since=%v\nout=%v", v, since, out)
  152. }
  153. }
  154. // Test with default value specified and parameter omitted
  155. expected := []string{"log1", "log2", "log3"}
  156. for _, cmd := range []*exec.Cmd{
  157. exec.Command(dockerBinary, "logs", "-t", name),
  158. exec.Command(dockerBinary, "logs", "-t", "--since=0", name),
  159. } {
  160. out, _, err = runCommandWithOutput(cmd)
  161. if err != nil {
  162. c.Fatalf("failed to log container: %s, %v", out, err)
  163. }
  164. for _, v := range expected {
  165. if !strings.Contains(out, v) {
  166. c.Fatalf("'%v' does not contain=%v\nout=%s", cmd.Args, v, out)
  167. }
  168. }
  169. }
  170. }
  171. func (s *DockerSuite) TestLogsSinceFutureFollow(c *check.C) {
  172. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", `for i in $(seq 1 5); do date +%s; sleep 1; done`)
  173. cleanedContainerID := strings.TrimSpace(out)
  174. now := daemonTime(c).Unix()
  175. since := now + 2
  176. out, _ = dockerCmd(c, "logs", "-f", fmt.Sprintf("--since=%v", since), cleanedContainerID)
  177. lines := strings.Split(strings.TrimSpace(out), "\n")
  178. if len(lines) == 0 {
  179. c.Fatal("got no log lines")
  180. }
  181. for _, v := range lines {
  182. ts, err := strconv.ParseInt(v, 10, 64)
  183. if err != nil {
  184. c.Fatalf("cannot parse timestamp output from log: '%v'\nout=%s", v, out)
  185. }
  186. if ts < since {
  187. c.Fatalf("earlier log found. since=%v logdate=%v", since, ts)
  188. }
  189. }
  190. }
  191. // Regression test for #8832
  192. func (s *DockerSuite) TestLogsFollowSlowStdoutConsumer(c *check.C) {
  193. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", `usleep 200000;yes X | head -c 200000`)
  194. cleanedContainerID := strings.TrimSpace(out)
  195. stopSlowRead := make(chan bool)
  196. go func() {
  197. exec.Command(dockerBinary, "wait", cleanedContainerID).Run()
  198. stopSlowRead <- true
  199. }()
  200. logCmd := exec.Command(dockerBinary, "logs", "-f", cleanedContainerID)
  201. stdout, err := logCmd.StdoutPipe()
  202. c.Assert(err, check.IsNil)
  203. c.Assert(logCmd.Start(), check.IsNil)
  204. // First read slowly
  205. bytes1, err := consumeWithSpeed(stdout, 10, 50*time.Millisecond, stopSlowRead)
  206. c.Assert(err, check.IsNil)
  207. // After the container has finished we can continue reading fast
  208. bytes2, err := consumeWithSpeed(stdout, 32*1024, 0, nil)
  209. c.Assert(err, check.IsNil)
  210. actual := bytes1 + bytes2
  211. expected := 200000
  212. if actual != expected {
  213. c.Fatalf("Invalid bytes read: %d, expected %d", actual, expected)
  214. }
  215. }
  216. func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
  217. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 2; done")
  218. id := strings.TrimSpace(out)
  219. c.Assert(waitRun(id), check.IsNil)
  220. type info struct {
  221. NGoroutines int
  222. }
  223. getNGoroutines := func() int {
  224. var i info
  225. status, b, err := sockRequest("GET", "/info", nil)
  226. c.Assert(err, check.IsNil)
  227. c.Assert(status, check.Equals, 200)
  228. c.Assert(json.Unmarshal(b, &i), check.IsNil)
  229. return i.NGoroutines
  230. }
  231. nroutines := getNGoroutines()
  232. cmd := exec.Command(dockerBinary, "logs", "-f", id)
  233. r, w := io.Pipe()
  234. cmd.Stdout = w
  235. c.Assert(cmd.Start(), check.IsNil)
  236. // Make sure pipe is written to
  237. chErr := make(chan error)
  238. go func() {
  239. b := make([]byte, 1)
  240. _, err := r.Read(b)
  241. chErr <- err
  242. }()
  243. c.Assert(<-chErr, check.IsNil)
  244. c.Assert(cmd.Process.Kill(), check.IsNil)
  245. // NGoroutines is not updated right away, so we need to wait before failing
  246. t := time.After(30 * time.Second)
  247. for {
  248. select {
  249. case <-t:
  250. if n := getNGoroutines(); n > nroutines {
  251. c.Fatalf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n)
  252. }
  253. default:
  254. if n := getNGoroutines(); n <= nroutines {
  255. return
  256. }
  257. time.Sleep(200 * time.Millisecond)
  258. }
  259. }
  260. }
  261. func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *check.C) {
  262. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 2; done")
  263. id := strings.TrimSpace(out)
  264. c.Assert(waitRun(id), check.IsNil)
  265. type info struct {
  266. NGoroutines int
  267. }
  268. getNGoroutines := func() int {
  269. var i info
  270. status, b, err := sockRequest("GET", "/info", nil)
  271. c.Assert(err, check.IsNil)
  272. c.Assert(status, check.Equals, 200)
  273. c.Assert(json.Unmarshal(b, &i), check.IsNil)
  274. return i.NGoroutines
  275. }
  276. nroutines := getNGoroutines()
  277. cmd := exec.Command(dockerBinary, "logs", "-f", id)
  278. c.Assert(cmd.Start(), check.IsNil)
  279. time.Sleep(200 * time.Millisecond)
  280. c.Assert(cmd.Process.Kill(), check.IsNil)
  281. // NGoroutines is not updated right away, so we need to wait before failing
  282. t := time.After(30 * time.Second)
  283. for {
  284. select {
  285. case <-t:
  286. if n := getNGoroutines(); n > nroutines {
  287. c.Fatalf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n)
  288. }
  289. default:
  290. if n := getNGoroutines(); n <= nroutines {
  291. return
  292. }
  293. time.Sleep(200 * time.Millisecond)
  294. }
  295. }
  296. }