docker_cli_logs_test.go 10 KB

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