docker_cli_logs_test.go 11 KB

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