docker_cli_logs_test.go 11 KB

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