docker_cli_logs_test.go 11 KB

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