docker_cli_stats_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. testRequires(c, DaemonIsLinux)
  13. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  14. id := strings.TrimSpace(out)
  15. c.Assert(waitRun(id), checker.IsNil)
  16. statsCmd := exec.Command(dockerBinary, "stats", "--no-stream", id)
  17. type output struct {
  18. out []byte
  19. err error
  20. }
  21. ch := make(chan output)
  22. go func() {
  23. out, err := statsCmd.Output()
  24. ch <- output{out, err}
  25. }()
  26. select {
  27. case outerr := <-ch:
  28. c.Assert(outerr.err, checker.IsNil, check.Commentf("Error running stats: %v", outerr.err))
  29. c.Assert(string(outerr.out), checker.Contains, id) //running container wasn't present in output
  30. case <-time.After(3 * time.Second):
  31. statsCmd.Process.Kill()
  32. c.Fatalf("stats did not return immediately when not streaming")
  33. }
  34. }
  35. func (s *DockerSuite) TestStatsContainerNotFound(c *check.C) {
  36. testRequires(c, DaemonIsLinux)
  37. out, _, err := dockerCmdWithError("stats", "notfound")
  38. c.Assert(err, checker.NotNil)
  39. c.Assert(out, checker.Contains, "No such container: notfound", check.Commentf("Expected to fail on not found container stats, got %q instead", out))
  40. out, _, err = dockerCmdWithError("stats", "--no-stream", "notfound")
  41. c.Assert(err, checker.NotNil)
  42. 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))
  43. }
  44. func (s *DockerSuite) TestStatsAllRunningNoStream(c *check.C) {
  45. testRequires(c, DaemonIsLinux)
  46. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  47. id1 := strings.TrimSpace(out)[:12]
  48. c.Assert(waitRun(id1), check.IsNil)
  49. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  50. id2 := strings.TrimSpace(out)[:12]
  51. c.Assert(waitRun(id2), check.IsNil)
  52. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  53. id3 := strings.TrimSpace(out)[:12]
  54. c.Assert(waitRun(id3), check.IsNil)
  55. dockerCmd(c, "stop", id3)
  56. out, _ = dockerCmd(c, "stats", "--no-stream")
  57. if !strings.Contains(out, id1) || !strings.Contains(out, id2) {
  58. c.Fatalf("Expected stats output to contain both %s and %s, got %s", id1, id2, out)
  59. }
  60. if strings.Contains(out, id3) {
  61. c.Fatalf("Did not expect %s in stats, got %s", id3, out)
  62. }
  63. }
  64. func (s *DockerSuite) TestStatsAllNoStream(c *check.C) {
  65. testRequires(c, DaemonIsLinux)
  66. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  67. id1 := strings.TrimSpace(out)[:12]
  68. c.Assert(waitRun(id1), check.IsNil)
  69. dockerCmd(c, "stop", id1)
  70. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  71. id2 := strings.TrimSpace(out)[:12]
  72. c.Assert(waitRun(id2), check.IsNil)
  73. out, _ = dockerCmd(c, "stats", "--all", "--no-stream")
  74. if !strings.Contains(out, id1) || !strings.Contains(out, id2) {
  75. c.Fatalf("Expected stats output to contain both %s and %s, got %s", id1, id2, out)
  76. }
  77. }
  78. func (s *DockerSuite) TestStatsAllNewContainersAdded(c *check.C) {
  79. testRequires(c, DaemonIsLinux)
  80. id := make(chan string)
  81. addedChan := make(chan struct{})
  82. dockerCmd(c, "run", "-d", "busybox", "top")
  83. statsCmd := exec.Command(dockerBinary, "stats")
  84. stdout, err := statsCmd.StdoutPipe()
  85. c.Assert(err, check.IsNil)
  86. c.Assert(statsCmd.Start(), check.IsNil)
  87. defer statsCmd.Process.Kill()
  88. go func() {
  89. containerID := <-id
  90. matchID := regexp.MustCompile(containerID)
  91. scanner := bufio.NewScanner(stdout)
  92. for scanner.Scan() {
  93. switch {
  94. case matchID.MatchString(scanner.Text()):
  95. close(addedChan)
  96. }
  97. }
  98. }()
  99. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  100. c.Assert(waitRun(strings.TrimSpace(out)), check.IsNil)
  101. id <- strings.TrimSpace(out)[:12]
  102. select {
  103. case <-time.After(10 * time.Second):
  104. c.Fatal("failed to observe new container created added to stats")
  105. case <-addedChan:
  106. // ignore, done
  107. }
  108. }