docker_cli_logs_test.go 14 KB

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