docker_cli_logs_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "os/exec"
  6. "regexp"
  7. "strings"
  8. "time"
  9. "github.com/docker/docker/pkg/integration/checker"
  10. "github.com/docker/docker/pkg/jsonlog"
  11. "github.com/go-check/check"
  12. )
  13. // This used to work, it test a log of PageSize-1 (gh#4851)
  14. func (s *DockerSuite) TestLogsContainerSmallerThanPage(c *check.C) {
  15. testLen := 32767
  16. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n = >> a.a; done; echo >> a.a; cat a.a", testLen))
  17. id := strings.TrimSpace(out)
  18. dockerCmd(c, "wait", id)
  19. out, _ = dockerCmd(c, "logs", id)
  20. c.Assert(out, checker.HasLen, testLen+1)
  21. }
  22. // Regression test: When going over the PageSize, it used to panic (gh#4851)
  23. func (s *DockerSuite) TestLogsContainerBiggerThanPage(c *check.C) {
  24. testLen := 32768
  25. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n = >> a.a; done; echo >> a.a; cat a.a", testLen))
  26. id := strings.TrimSpace(out)
  27. dockerCmd(c, "wait", id)
  28. out, _ = dockerCmd(c, "logs", id)
  29. c.Assert(out, checker.HasLen, testLen+1)
  30. }
  31. // Regression test: When going much over the PageSize, it used to block (gh#4851)
  32. func (s *DockerSuite) TestLogsContainerMuchBiggerThanPage(c *check.C) {
  33. testLen := 33000
  34. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo -n = >> a.a; done; echo >> a.a; cat a.a", testLen))
  35. id := strings.TrimSpace(out)
  36. dockerCmd(c, "wait", id)
  37. out, _ = dockerCmd(c, "logs", id)
  38. c.Assert(out, checker.HasLen, testLen+1)
  39. }
  40. func (s *DockerSuite) TestLogsTimestamps(c *check.C) {
  41. testLen := 100
  42. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo = >> a.a; done; cat a.a", testLen))
  43. id := strings.TrimSpace(out)
  44. dockerCmd(c, "wait", id)
  45. out, _ = dockerCmd(c, "logs", "-t", id)
  46. lines := strings.Split(out, "\n")
  47. c.Assert(lines, checker.HasLen, testLen+1)
  48. ts := regexp.MustCompile(`^.* `)
  49. for _, l := range lines {
  50. if l != "" {
  51. _, err := time.Parse(jsonlog.RFC3339NanoFixed+" ", ts.FindString(l))
  52. c.Assert(err, checker.IsNil, check.Commentf("Failed to parse timestamp from %v", l))
  53. // ensure we have padded 0's
  54. c.Assert(l[29], checker.Equals, uint8('Z'))
  55. }
  56. }
  57. }
  58. func (s *DockerSuite) TestLogsSeparateStderr(c *check.C) {
  59. msg := "stderr_log"
  60. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
  61. id := strings.TrimSpace(out)
  62. dockerCmd(c, "wait", id)
  63. stdout, stderr, _ := dockerCmdWithStdoutStderr(c, "logs", id)
  64. c.Assert(stdout, checker.Equals, "")
  65. stderr = strings.TrimSpace(stderr)
  66. c.Assert(stderr, checker.Equals, msg)
  67. }
  68. func (s *DockerSuite) TestLogsStderrInStdout(c *check.C) {
  69. // TODO Windows: Needs investigation why this fails. Obtained string includes
  70. // a bunch of ANSI escape sequences before the "stderr_log" message.
  71. testRequires(c, DaemonIsLinux)
  72. msg := "stderr_log"
  73. out, _ := dockerCmd(c, "run", "-d", "-t", "busybox", "sh", "-c", fmt.Sprintf("echo %s 1>&2", msg))
  74. id := strings.TrimSpace(out)
  75. dockerCmd(c, "wait", id)
  76. stdout, stderr, _ := dockerCmdWithStdoutStderr(c, "logs", id)
  77. c.Assert(stderr, checker.Equals, "")
  78. stdout = strings.TrimSpace(stdout)
  79. c.Assert(stdout, checker.Equals, msg)
  80. }
  81. func (s *DockerSuite) TestLogsTail(c *check.C) {
  82. testLen := 100
  83. out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", fmt.Sprintf("for i in $(seq 1 %d); do echo =; done;", testLen))
  84. id := strings.TrimSpace(out)
  85. dockerCmd(c, "wait", id)
  86. out, _ = dockerCmd(c, "logs", "--tail", "0", id)
  87. lines := strings.Split(out, "\n")
  88. c.Assert(lines, checker.HasLen, 1)
  89. out, _ = dockerCmd(c, "logs", "--tail", "5", id)
  90. lines = strings.Split(out, "\n")
  91. c.Assert(lines, checker.HasLen, 6)
  92. out, _ = dockerCmd(c, "logs", "--tail", "99", id)
  93. lines = strings.Split(out, "\n")
  94. c.Assert(lines, checker.HasLen, 100)
  95. out, _ = dockerCmd(c, "logs", "--tail", "all", id)
  96. lines = strings.Split(out, "\n")
  97. c.Assert(lines, checker.HasLen, testLen+1)
  98. out, _ = dockerCmd(c, "logs", "--tail", "-1", id)
  99. lines = strings.Split(out, "\n")
  100. c.Assert(lines, checker.HasLen, testLen+1)
  101. out, _, _ = dockerCmdWithStdoutStderr(c, "logs", "--tail", "random", id)
  102. lines = strings.Split(out, "\n")
  103. c.Assert(lines, checker.HasLen, testLen+1)
  104. }
  105. func (s *DockerSuite) TestLogsFollowStopped(c *check.C) {
  106. dockerCmd(c, "run", "--name=test", "busybox", "echo", "hello")
  107. id, err := getIDByName("test")
  108. c.Assert(err, check.IsNil)
  109. logsCmd := exec.Command(dockerBinary, "logs", "-f", id)
  110. c.Assert(logsCmd.Start(), checker.IsNil)
  111. errChan := make(chan error)
  112. go func() {
  113. errChan <- logsCmd.Wait()
  114. close(errChan)
  115. }()
  116. select {
  117. case err := <-errChan:
  118. c.Assert(err, checker.IsNil)
  119. case <-time.After(30 * time.Second):
  120. c.Fatal("Following logs is hanged")
  121. }
  122. }
  123. func (s *DockerSuite) TestLogsSince(c *check.C) {
  124. name := "testlogssince"
  125. dockerCmd(c, "run", "--name="+name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do sleep 2; echo log$i; done")
  126. out, _ := dockerCmd(c, "logs", "-t", name)
  127. log2Line := strings.Split(strings.Split(out, "\n")[1], " ")
  128. t, err := time.Parse(time.RFC3339Nano, log2Line[0]) // the timestamp log2 is written
  129. c.Assert(err, checker.IsNil)
  130. since := t.Unix() + 1 // add 1s so log1 & log2 doesn't show up
  131. out, _ = dockerCmd(c, "logs", "-t", fmt.Sprintf("--since=%v", since), name)
  132. // Skip 2 seconds
  133. unexpected := []string{"log1", "log2"}
  134. for _, v := range unexpected {
  135. c.Assert(out, checker.Not(checker.Contains), v, check.Commentf("unexpected log message returned, since=%v", since))
  136. }
  137. // Test to make sure a bad since format is caught by the client
  138. out, _, _ = dockerCmdWithError("logs", "-t", "--since=2006-01-02T15:04:0Z", name)
  139. c.Assert(out, checker.Contains, "cannot parse \"0Z\" as \"05\"", check.Commentf("bad since format passed to server"))
  140. // Test with default value specified and parameter omitted
  141. expected := []string{"log1", "log2", "log3"}
  142. for _, cmd := range []*exec.Cmd{
  143. exec.Command(dockerBinary, "logs", "-t", name),
  144. exec.Command(dockerBinary, "logs", "-t", "--since=0", name),
  145. } {
  146. out, _, err = runCommandWithOutput(cmd)
  147. c.Assert(err, checker.IsNil, check.Commentf("failed to log container: %s", out))
  148. for _, v := range expected {
  149. c.Assert(out, checker.Contains, v)
  150. }
  151. }
  152. }
  153. func (s *DockerSuite) TestLogsSinceFutureFollow(c *check.C) {
  154. // TODO Windows TP5 - Figure out why this test is so flakey. Disabled for now.
  155. testRequires(c, DaemonIsLinux)
  156. name := "testlogssincefuturefollow"
  157. out, _ := dockerCmd(c, "run", "-d", "--name", name, "busybox", "/bin/sh", "-c", `for i in $(seq 1 5); do echo log$i; sleep 1; done`)
  158. // Extract one timestamp from the log file to give us a starting point for
  159. // our `--since` argument. Because the log producer runs in the background,
  160. // we need to check repeatedly for some output to be produced.
  161. var timestamp string
  162. for i := 0; i != 100 && timestamp == ""; i++ {
  163. if out, _ = dockerCmd(c, "logs", "-t", name); out == "" {
  164. time.Sleep(time.Millisecond * 100) // Retry
  165. } else {
  166. timestamp = strings.Split(strings.Split(out, "\n")[0], " ")[0]
  167. }
  168. }
  169. c.Assert(timestamp, checker.Not(checker.Equals), "")
  170. t, err := time.Parse(time.RFC3339Nano, timestamp)
  171. c.Assert(err, check.IsNil)
  172. since := t.Unix() + 2
  173. out, _ = dockerCmd(c, "logs", "-t", "-f", fmt.Sprintf("--since=%v", since), name)
  174. c.Assert(out, checker.Not(checker.HasLen), 0, check.Commentf("cannot read from empty log"))
  175. lines := strings.Split(strings.TrimSpace(out), "\n")
  176. for _, v := range lines {
  177. ts, err := time.Parse(time.RFC3339Nano, strings.Split(v, " ")[0])
  178. c.Assert(err, checker.IsNil, check.Commentf("cannot parse timestamp output from log: '%v'", v))
  179. c.Assert(ts.Unix() >= since, checker.Equals, true, check.Commentf("earlier log found. since=%v logdate=%v", since, ts))
  180. }
  181. }
  182. // Regression test for #8832
  183. func (s *DockerSuite) TestLogsFollowSlowStdoutConsumer(c *check.C) {
  184. // TODO Windows: Fix this test for TP5.
  185. testRequires(c, DaemonIsLinux)
  186. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", `usleep 600000;yes X | head -c 200000`)
  187. id := strings.TrimSpace(out)
  188. stopSlowRead := make(chan bool)
  189. go func() {
  190. exec.Command(dockerBinary, "wait", id).Run()
  191. stopSlowRead <- true
  192. }()
  193. logCmd := exec.Command(dockerBinary, "logs", "-f", id)
  194. stdout, err := logCmd.StdoutPipe()
  195. c.Assert(err, checker.IsNil)
  196. c.Assert(logCmd.Start(), checker.IsNil)
  197. // First read slowly
  198. bytes1, err := consumeWithSpeed(stdout, 10, 50*time.Millisecond, stopSlowRead)
  199. c.Assert(err, checker.IsNil)
  200. // After the container has finished we can continue reading fast
  201. bytes2, err := consumeWithSpeed(stdout, 32*1024, 0, nil)
  202. c.Assert(err, checker.IsNil)
  203. actual := bytes1 + bytes2
  204. expected := 200000
  205. c.Assert(actual, checker.Equals, expected)
  206. }
  207. func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
  208. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 2; done")
  209. id := strings.TrimSpace(out)
  210. c.Assert(waitRun(id), checker.IsNil)
  211. nroutines, err := getGoroutineNumber()
  212. c.Assert(err, checker.IsNil)
  213. cmd := exec.Command(dockerBinary, "logs", "-f", id)
  214. r, w := io.Pipe()
  215. cmd.Stdout = w
  216. c.Assert(cmd.Start(), checker.IsNil)
  217. // Make sure pipe is written to
  218. chErr := make(chan error)
  219. go func() {
  220. b := make([]byte, 1)
  221. _, err := r.Read(b)
  222. chErr <- err
  223. }()
  224. c.Assert(<-chErr, checker.IsNil)
  225. c.Assert(cmd.Process.Kill(), checker.IsNil)
  226. // NGoroutines is not updated right away, so we need to wait before failing
  227. c.Assert(waitForGoroutines(nroutines), checker.IsNil)
  228. }
  229. func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *check.C) {
  230. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 2; done")
  231. id := strings.TrimSpace(out)
  232. c.Assert(waitRun(id), checker.IsNil)
  233. nroutines, err := getGoroutineNumber()
  234. c.Assert(err, checker.IsNil)
  235. cmd := exec.Command(dockerBinary, "logs", "-f", id)
  236. c.Assert(cmd.Start(), checker.IsNil)
  237. time.Sleep(200 * time.Millisecond)
  238. c.Assert(cmd.Process.Kill(), checker.IsNil)
  239. // NGoroutines is not updated right away, so we need to wait before failing
  240. c.Assert(waitForGoroutines(nroutines), checker.IsNil)
  241. }
  242. func (s *DockerSuite) TestLogsCLIContainerNotFound(c *check.C) {
  243. name := "testlogsnocontainer"
  244. out, _, _ := dockerCmdWithError("logs", name)
  245. message := fmt.Sprintf("Error: No such container: %s\n", name)
  246. c.Assert(out, checker.Equals, message)
  247. }
  248. func (s *DockerSuite) TestLogsWithDetails(c *check.C) {
  249. dockerCmd(c, "run", "--name=test", "--label", "foo=bar", "-e", "baz=qux", "--log-opt", "labels=foo", "--log-opt", "env=baz", "busybox", "echo", "hello")
  250. out, _ := dockerCmd(c, "logs", "--details", "--timestamps", "test")
  251. logFields := strings.Fields(strings.TrimSpace(out))
  252. c.Assert(len(logFields), checker.Equals, 3, check.Commentf(out))
  253. details := strings.Split(logFields[1], ",")
  254. c.Assert(details, checker.HasLen, 2)
  255. c.Assert(details[0], checker.Equals, "baz=qux")
  256. c.Assert(details[1], checker.Equals, "foo=bar")
  257. }