docker_cli_logs_test.go 13 KB

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