docker_cli_logs_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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/jsonlog"
  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(jsonlog.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. dockerCmd(c, "run", "--name="+name, "busybox", "/bin/sh", "-c", "for i in $(seq 1 3); do sleep 2; echo log$i; done")
  125. out, _ := dockerCmd(c, "logs", "-t", name)
  126. log2Line := strings.Split(strings.Split(out, "\n")[1], " ")
  127. t, err := time.Parse(time.RFC3339Nano, log2Line[0]) // the timestamp log2 is written
  128. c.Assert(err, checker.IsNil)
  129. since := t.Unix() + 1 // add 1s so log1 & log2 doesn't show up
  130. out, _ = dockerCmd(c, "logs", "-t", fmt.Sprintf("--since=%v", since), name)
  131. // Skip 2 seconds
  132. unexpected := []string{"log1", "log2"}
  133. for _, v := range unexpected {
  134. c.Assert(out, checker.Not(checker.Contains), v, check.Commentf("unexpected log message returned, since=%v", since))
  135. }
  136. // Test to make sure a bad since format is caught by the client
  137. out, _, _ = dockerCmdWithError("logs", "-t", "--since=2006-01-02T15:04:0Z", name)
  138. c.Assert(out, checker.Contains, "cannot parse \"0Z\" as \"05\"", check.Commentf("bad since format passed to server"))
  139. // Test with default value specified and parameter omitted
  140. expected := []string{"log1", "log2", "log3"}
  141. for _, cmd := range []*exec.Cmd{
  142. exec.Command(dockerBinary, "logs", "-t", name),
  143. exec.Command(dockerBinary, "logs", "-t", "--since=0", name),
  144. } {
  145. out, _, err = runCommandWithOutput(cmd)
  146. c.Assert(err, checker.IsNil, check.Commentf("failed to log container: %s", out))
  147. for _, v := range expected {
  148. c.Assert(out, checker.Contains, v)
  149. }
  150. }
  151. }
  152. func (s *DockerSuite) TestLogsSinceFutureFollow(c *check.C) {
  153. testRequires(c, DaemonIsLinux)
  154. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", `for i in $(seq 1 5); do date +%s; sleep 1; done`)
  155. id := strings.TrimSpace(out)
  156. now := daemonTime(c).Unix()
  157. since := now + 2
  158. out, _ = dockerCmd(c, "logs", "-f", fmt.Sprintf("--since=%v", since), id)
  159. lines := strings.Split(strings.TrimSpace(out), "\n")
  160. c.Assert(lines, checker.Not(checker.HasLen), 0)
  161. for _, v := range lines {
  162. ts, err := strconv.ParseInt(v, 10, 64)
  163. c.Assert(err, checker.IsNil, check.Commentf("cannot parse timestamp output from log: '%v'\nout=%s", v, out))
  164. c.Assert(ts >= since, checker.Equals, true, check.Commentf("earlier log found. since=%v logdate=%v", since, ts))
  165. }
  166. }
  167. // Regression test for #8832
  168. func (s *DockerSuite) TestLogsFollowSlowStdoutConsumer(c *check.C) {
  169. testRequires(c, DaemonIsLinux)
  170. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", `usleep 600000;yes X | head -c 200000`)
  171. id := strings.TrimSpace(out)
  172. stopSlowRead := make(chan bool)
  173. go func() {
  174. exec.Command(dockerBinary, "wait", id).Run()
  175. stopSlowRead <- true
  176. }()
  177. logCmd := exec.Command(dockerBinary, "logs", "-f", id)
  178. stdout, err := logCmd.StdoutPipe()
  179. c.Assert(err, checker.IsNil)
  180. c.Assert(logCmd.Start(), checker.IsNil)
  181. // First read slowly
  182. bytes1, err := consumeWithSpeed(stdout, 10, 50*time.Millisecond, stopSlowRead)
  183. c.Assert(err, checker.IsNil)
  184. // After the container has finished we can continue reading fast
  185. bytes2, err := consumeWithSpeed(stdout, 32*1024, 0, nil)
  186. c.Assert(err, checker.IsNil)
  187. actual := bytes1 + bytes2
  188. expected := 200000
  189. c.Assert(actual, checker.Equals, expected)
  190. }
  191. func (s *DockerSuite) TestLogsFollowGoroutinesWithStdout(c *check.C) {
  192. testRequires(c, DaemonIsLinux)
  193. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do echo hello; sleep 2; done")
  194. id := strings.TrimSpace(out)
  195. c.Assert(waitRun(id), checker.IsNil)
  196. type info struct {
  197. NGoroutines int
  198. }
  199. getNGoroutines := func() int {
  200. var i info
  201. status, b, err := sockRequest("GET", "/info", nil)
  202. c.Assert(err, checker.IsNil)
  203. c.Assert(status, checker.Equals, 200)
  204. c.Assert(json.Unmarshal(b, &i), checker.IsNil)
  205. return i.NGoroutines
  206. }
  207. nroutines := getNGoroutines()
  208. cmd := exec.Command(dockerBinary, "logs", "-f", id)
  209. r, w := io.Pipe()
  210. cmd.Stdout = w
  211. c.Assert(cmd.Start(), checker.IsNil)
  212. // Make sure pipe is written to
  213. chErr := make(chan error)
  214. go func() {
  215. b := make([]byte, 1)
  216. _, err := r.Read(b)
  217. chErr <- err
  218. }()
  219. c.Assert(<-chErr, checker.IsNil)
  220. c.Assert(cmd.Process.Kill(), checker.IsNil)
  221. // NGoroutines is not updated right away, so we need to wait before failing
  222. t := time.After(30 * time.Second)
  223. for {
  224. select {
  225. case <-t:
  226. n := getNGoroutines()
  227. c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
  228. default:
  229. if n := getNGoroutines(); n <= nroutines {
  230. return
  231. }
  232. time.Sleep(200 * time.Millisecond)
  233. }
  234. }
  235. }
  236. func (s *DockerSuite) TestLogsFollowGoroutinesNoOutput(c *check.C) {
  237. testRequires(c, DaemonIsLinux)
  238. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true; do sleep 2; done")
  239. id := strings.TrimSpace(out)
  240. c.Assert(waitRun(id), checker.IsNil)
  241. type info struct {
  242. NGoroutines int
  243. }
  244. getNGoroutines := func() int {
  245. var i info
  246. status, b, err := sockRequest("GET", "/info", nil)
  247. c.Assert(err, checker.IsNil)
  248. c.Assert(status, checker.Equals, 200)
  249. c.Assert(json.Unmarshal(b, &i), checker.IsNil)
  250. return i.NGoroutines
  251. }
  252. nroutines := getNGoroutines()
  253. cmd := exec.Command(dockerBinary, "logs", "-f", id)
  254. c.Assert(cmd.Start(), checker.IsNil)
  255. time.Sleep(200 * time.Millisecond)
  256. c.Assert(cmd.Process.Kill(), checker.IsNil)
  257. // NGoroutines is not updated right away, so we need to wait before failing
  258. t := time.After(30 * time.Second)
  259. for {
  260. select {
  261. case <-t:
  262. n := getNGoroutines()
  263. c.Assert(n <= nroutines, checker.Equals, true, check.Commentf("leaked goroutines: expected less than or equal to %d, got: %d", nroutines, n))
  264. default:
  265. if n := getNGoroutines(); n <= nroutines {
  266. return
  267. }
  268. time.Sleep(200 * time.Millisecond)
  269. }
  270. }
  271. }
  272. func (s *DockerSuite) TestLogsCLIContainerNotFound(c *check.C) {
  273. name := "testlogsnocontainer"
  274. out, _, _ := dockerCmdWithError("logs", name)
  275. message := fmt.Sprintf("Error: No such container: %s\n", name)
  276. c.Assert(out, checker.Equals, message)
  277. }