docker_cli_stats_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package main
  2. import (
  3. "bufio"
  4. "os/exec"
  5. "regexp"
  6. "strings"
  7. "time"
  8. "github.com/docker/docker/pkg/integration/checker"
  9. "github.com/go-check/check"
  10. )
  11. func (s *DockerSuite) TestStatsNoStream(c *check.C) {
  12. // Windows does not support stats
  13. testRequires(c, DaemonIsLinux)
  14. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  15. id := strings.TrimSpace(out)
  16. c.Assert(waitRun(id), checker.IsNil)
  17. statsCmd := exec.Command(dockerBinary, "stats", "--no-stream", id)
  18. type output struct {
  19. out []byte
  20. err error
  21. }
  22. ch := make(chan output)
  23. go func() {
  24. out, err := statsCmd.Output()
  25. ch <- output{out, err}
  26. }()
  27. select {
  28. case outerr := <-ch:
  29. c.Assert(outerr.err, checker.IsNil, check.Commentf("Error running stats: %v", outerr.err))
  30. c.Assert(string(outerr.out), checker.Contains, id) //running container wasn't present in output
  31. case <-time.After(3 * time.Second):
  32. statsCmd.Process.Kill()
  33. c.Fatalf("stats did not return immediately when not streaming")
  34. }
  35. }
  36. func (s *DockerSuite) TestStatsContainerNotFound(c *check.C) {
  37. // Windows does not support stats
  38. testRequires(c, DaemonIsLinux)
  39. out, _, err := dockerCmdWithError("stats", "notfound")
  40. c.Assert(err, checker.NotNil)
  41. c.Assert(out, checker.Contains, "No such container: notfound", check.Commentf("Expected to fail on not found container stats, got %q instead", out))
  42. out, _, err = dockerCmdWithError("stats", "--no-stream", "notfound")
  43. c.Assert(err, checker.NotNil)
  44. c.Assert(out, checker.Contains, "No such container: notfound", check.Commentf("Expected to fail on not found container stats with --no-stream, got %q instead", out))
  45. }
  46. func (s *DockerSuite) TestStatsAllRunningNoStream(c *check.C) {
  47. // Windows does not support stats
  48. testRequires(c, DaemonIsLinux)
  49. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  50. id1 := strings.TrimSpace(out)[:12]
  51. c.Assert(waitRun(id1), check.IsNil)
  52. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  53. id2 := strings.TrimSpace(out)[:12]
  54. c.Assert(waitRun(id2), check.IsNil)
  55. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  56. id3 := strings.TrimSpace(out)[:12]
  57. c.Assert(waitRun(id3), check.IsNil)
  58. dockerCmd(c, "stop", id3)
  59. out, _ = dockerCmd(c, "stats", "--no-stream")
  60. if !strings.Contains(out, id1) || !strings.Contains(out, id2) {
  61. c.Fatalf("Expected stats output to contain both %s and %s, got %s", id1, id2, out)
  62. }
  63. if strings.Contains(out, id3) {
  64. c.Fatalf("Did not expect %s in stats, got %s", id3, out)
  65. }
  66. // check output contains real data, but not all zeros
  67. reg, _ := regexp.Compile("[1-9]+")
  68. // split output with "\n", outLines[1] is id2's output
  69. // outLines[2] is id1's output
  70. outLines := strings.Split(out, "\n")
  71. // check stat result of id2 contains real data
  72. realData := reg.Find([]byte(outLines[1][12:]))
  73. c.Assert(realData, checker.NotNil, check.Commentf("stat result are empty: %s", out))
  74. // check stat result of id1 contains real data
  75. realData = reg.Find([]byte(outLines[2][12:]))
  76. c.Assert(realData, checker.NotNil, check.Commentf("stat result are empty: %s", out))
  77. }
  78. func (s *DockerSuite) TestStatsAllNoStream(c *check.C) {
  79. // Windows does not support stats
  80. testRequires(c, DaemonIsLinux)
  81. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  82. id1 := strings.TrimSpace(out)[:12]
  83. c.Assert(waitRun(id1), check.IsNil)
  84. dockerCmd(c, "stop", id1)
  85. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  86. id2 := strings.TrimSpace(out)[:12]
  87. c.Assert(waitRun(id2), check.IsNil)
  88. out, _ = dockerCmd(c, "stats", "--all", "--no-stream")
  89. if !strings.Contains(out, id1) || !strings.Contains(out, id2) {
  90. c.Fatalf("Expected stats output to contain both %s and %s, got %s", id1, id2, out)
  91. }
  92. // check output contains real data, but not all zeros
  93. reg, _ := regexp.Compile("[1-9]+")
  94. // split output with "\n", outLines[1] is id2's output
  95. outLines := strings.Split(out, "\n")
  96. // check stat result of id2 contains real data
  97. realData := reg.Find([]byte(outLines[1][12:]))
  98. c.Assert(realData, checker.NotNil, check.Commentf("stat result of %s is empty: %s", id2, out))
  99. // check stat result of id1 contains all zero
  100. realData = reg.Find([]byte(outLines[2][12:]))
  101. c.Assert(realData, checker.IsNil, check.Commentf("stat result of %s should be empty : %s", id1, out))
  102. }
  103. func (s *DockerSuite) TestStatsAllNewContainersAdded(c *check.C) {
  104. // Windows does not support stats
  105. testRequires(c, DaemonIsLinux)
  106. id := make(chan string)
  107. addedChan := make(chan struct{})
  108. runSleepingContainer(c, "-d")
  109. statsCmd := exec.Command(dockerBinary, "stats")
  110. stdout, err := statsCmd.StdoutPipe()
  111. c.Assert(err, check.IsNil)
  112. c.Assert(statsCmd.Start(), check.IsNil)
  113. defer statsCmd.Process.Kill()
  114. go func() {
  115. containerID := <-id
  116. matchID := regexp.MustCompile(containerID)
  117. scanner := bufio.NewScanner(stdout)
  118. for scanner.Scan() {
  119. switch {
  120. case matchID.MatchString(scanner.Text()):
  121. close(addedChan)
  122. return
  123. }
  124. }
  125. }()
  126. out, _ := runSleepingContainer(c, "-d")
  127. c.Assert(waitRun(strings.TrimSpace(out)), check.IsNil)
  128. id <- strings.TrimSpace(out)[:12]
  129. select {
  130. case <-time.After(30 * time.Second):
  131. c.Fatal("failed to observe new container created added to stats")
  132. case <-addedChan:
  133. // ignore, done
  134. }
  135. }