docker_cli_logs_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "os/exec"
  7. "regexp"
  8. "strings"
  9. "time"
  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", "5", id)
  88. lines := strings.Split(out, "\n")
  89. c.Assert(lines, checker.HasLen, 6)
  90. out, _ = dockerCmd(c, "logs", "--tail", "all", id)
  91. lines = strings.Split(out, "\n")
  92. c.Assert(lines, checker.HasLen, testLen+1)
  93. out, _, _ = dockerCmdWithStdoutStderr(c, "logs", "--tail", "random", id)
  94. lines = strings.Split(out, "\n")
  95. c.Assert(lines, checker.HasLen, testLen+1)
  96. }
  97. func (s *DockerSuite) TestLogsFollowStopped(c *check.C) {
  98. dockerCmd(c, "run", "--name=test", "busybox", "echo", "hello")
  99. id, err := getIDByName("test")
  100. c.Assert(err, check.IsNil)
  101. logsCmd := exec.Command(dockerBinary, "logs", "-f", id)
  102. c.Assert(logsCmd.Start(), checker.IsNil)
  103. errChan := make(chan error)
  104. go func() {
  105. errChan <- logsCmd.Wait()
  106. close(errChan)
  107. }()
  108. select {
  109. case err := <-errChan:
  110. c.Assert(err, checker.IsNil)
  111. case <-time.After(30 * time.Second):
  112. c.Fatal("Following logs is hanged")
  113. }
  114. }
  115. func (s *DockerSuite) TestLogsSince(c *check.C) {
  116. name := "testlogssince"
  117. dockerCmd(c, "run", "--name="+name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do sleep 2; echo log$i; done")
  118. out, _ := dockerCmd(c, "logs", "-t", name)
  119. log2Line := strings.Split(strings.Split(out, "\n")[1], " ")
  120. t, err := time.Parse(time.RFC3339Nano, log2Line[0]) // the timestamp log2 is written
  121. c.Assert(err, checker.IsNil)
  122. since := t.Unix() + 1 // add 1s so log1 & log2 doesn't show up
  123. out, _ = dockerCmd(c, "logs", "-t", fmt.Sprintf("--since=%v", since), name)
  124. // Skip 2 seconds
  125. unexpected := []string{"log1", "log2"}
  126. for _, v := range unexpected {
  127. c.Assert(out, checker.Not(checker.Contains), v, check.Commentf("unexpected log message returned, since=%v", since))
  128. }
  129. // Test to make sure a bad since format is caught by the client
  130. out, _, _ = dockerCmdWithError("logs", "-t", "--since=2006-01-02T15:04:0Z", name)
  131. c.Assert(out, checker.Contains, "cannot parse \"0Z\" as \"05\"", check.Commentf("bad since format passed to server"))
  132. // Test with default value specified and parameter omitted
  133. expected := []string{"log1", "log2", "log3"}
  134. for _, cmd := range []*exec.Cmd{
  135. exec.Command(dockerBinary, "logs", "-t", name),
  136. exec.Command(dockerBinary, "logs", "-t", "--since=0", name),
  137. } {
  138. out, _, err = runCommandWithOutput(cmd)
  139. c.Assert(err, checker.IsNil, check.Commentf("failed to log container: %s", out))
  140. for _, v := range expected {
  141. c.Assert(out, checker.Contains, v)
  142. }
  143. }
  144. }
  145. func (s *DockerSuite) TestLogsSinceFutureFollow(c *check.C) {
  146. // TODO Windows: Flakey on TP4. Enable for next technical preview.
  147. testRequires(c, DaemonIsLinux)
  148. name := "testlogssincefuturefollow"
  149. out, _ := dockerCmd(c, "run", "-d", "--name", name, "busybox", "/bin/sh", "-c", `for i in $(seq 1 5); do echo log$i; sleep 1; done`)
  150. // Extract one timestamp from the log file to give us a starting point for
  151. // our `--since` argument. Because the log producer runs in the background,
  152. // we need to check repeatedly for some output to be produced.
  153. var timestamp string
  154. for i := 0; i != 100 && timestamp == ""; i++ {
  155. if out, _ = dockerCmd(c, "logs", "-t", name); out == "" {
  156. time.Sleep(time.Millisecond * 100) // Retry
  157. } else {
  158. timestamp = strings.Split(strings.Split(out, "\n")[0], " ")[0]
  159. }
  160. }
  161. c.Assert(timestamp, checker.Not(checker.Equals), "")
  162. t, err := time.Parse(time.RFC3339Nano, timestamp)
  163. c.Assert(err, check.IsNil)
  164. since := t.Unix() + 2
  165. out, _ = dockerCmd(c, "logs", "-t", "-f", fmt.Sprintf("--since=%v", since), name)
  166. lines := strings.Split(strings.TrimSpace(out), "\n")
  167. c.Assert(lines, checker.Not(checker.HasLen), 0)
  168. for _, v := range lines {
  169. ts, err := time.Parse(time.RFC3339Nano, strings.Split(v, " ")[0])
  170. c.Assert(err, checker.IsNil, check.Commentf("cannot parse timestamp output from log: '%v'", v))
  171. c.Assert(ts.Unix() >= since, checker.Equals, true, check.Commentf("earlier log found. since=%v logdate=%v", since, ts))
  172. }
  173. }
  174. // Regression test for #8832
  175. func (s *DockerSuite) TestLogsFollowSlowStdoutConsumer(c *check.C) {
  176. // TODO Windows: Consider enabling post-TP4. Too expensive to run on TP4
  177. testRequires(c, DaemonIsLinux)
  178. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", `usleep 600000;yes X | head -c 200000`)
  179. id := strings.TrimSpace(out)
  180. stopSlowRead := make(chan bool)
  181. go func() {
  182. exec.Command(dockerBinary, "wait", id).Run()
  183. stopSlowRead <- true
  184. }()
  185. logCmd := exec.Command(dockerBinary, "logs", "-f", id)
  186. stdout, err := logCmd.StdoutPipe()
  187. c.Assert(err, checker.IsNil)
  188. c.Assert(logCmd.Start(), checker.IsNil)
  189. // First read slowly
  190. bytes1, err := consumeWithSpeed(stdout, 10, 50*time.Millisecond, stopSlowRead)
  191. c.Assert(err, checker.IsNil)
  192. // After the container has finished we can continue reading fast
  193. bytes2, err := consumeWithSpeed(stdout, 32*1024, 0, nil)
  194. c.Assert(err, checker.IsNil)
  195. actual := bytes1 + bytes2
  196. expected := 200000
  197. c.Assert(actual, checker.Equals, expected)
  198. }
  199. func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
  200. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 2; done")
  201. id := strings.TrimSpace(out)
  202. c.Assert(waitRun(id), checker.IsNil)
  203. type info struct {
  204. NGoroutines int
  205. }
  206. getNGoroutines := func() int {
  207. var i info
  208. status, b, err := sockRequest("GET", "/info", nil)
  209. c.Assert(err, checker.IsNil)
  210. c.Assert(status, checker.Equals, 200)
  211. c.Assert(json.Unmarshal(b, &i), checker.IsNil)
  212. return i.NGoroutines
  213. }
  214. nroutines := getNGoroutines()
  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. // NGoroutines is not updated right away, so we need to wait before failing
  229. t := time.After(30 * time.Second)
  230. for {
  231. select {
  232. case <-t:
  233. n := getNGoroutines()
  234. c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
  235. default:
  236. if n := getNGoroutines(); n <= nroutines {
  237. return
  238. }
  239. time.Sleep(200 * time.Millisecond)
  240. }
  241. }
  242. }
  243. func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *check.C) {
  244. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 2; done")
  245. id := strings.TrimSpace(out)
  246. c.Assert(waitRun(id), checker.IsNil)
  247. type info struct {
  248. NGoroutines int
  249. }
  250. getNGoroutines := func() int {
  251. var i info
  252. status, b, err := sockRequest("GET", "/info", nil)
  253. c.Assert(err, checker.IsNil)
  254. c.Assert(status, checker.Equals, 200)
  255. c.Assert(json.Unmarshal(b, &i), checker.IsNil)
  256. return i.NGoroutines
  257. }
  258. nroutines := getNGoroutines()
  259. cmd := exec.Command(dockerBinary, "logs", "-f", id)
  260. c.Assert(cmd.Start(), checker.IsNil)
  261. time.Sleep(200 * time.Millisecond)
  262. c.Assert(cmd.Process.Kill(), checker.IsNil)
  263. // NGoroutines is not updated right away, so we need to wait before failing
  264. t := time.After(30 * time.Second)
  265. for {
  266. select {
  267. case <-t:
  268. n := getNGoroutines()
  269. c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
  270. default:
  271. if n := getNGoroutines(); n <= nroutines {
  272. return
  273. }
  274. time.Sleep(200 * time.Millisecond)
  275. }
  276. }
  277. }
  278. func (s *DockerSuite) TestLogsCLIContainerNotFound(c *check.C) {
  279. name := "testlogsnocontainer"
  280. out, _, _ := dockerCmdWithError("logs", name)
  281. message := fmt.Sprintf("Error: No such container: %s\n", name)
  282. c.Assert(out, checker.Equals, message)
  283. }