docker_cli_logs_test.go 10 KB

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