docker_cli_stats_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. }
  67. func (s *DockerSuite) TestStatsAllNoStream(c *check.C) {
  68. // Windows does not support stats
  69. testRequires(c, DaemonIsLinux)
  70. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  71. id1 := strings.TrimSpace(out)[:12]
  72. c.Assert(waitRun(id1), check.IsNil)
  73. dockerCmd(c, "stop", id1)
  74. out, _ = dockerCmd(c, "run", "-d", "busybox", "top")
  75. id2 := strings.TrimSpace(out)[:12]
  76. c.Assert(waitRun(id2), check.IsNil)
  77. out, _ = dockerCmd(c, "stats", "--all", "--no-stream")
  78. if !strings.Contains(out, id1) || !strings.Contains(out, id2) {
  79. c.Fatalf("Expected stats output to contain both %s and %s, got %s", id1, id2, out)
  80. }
  81. }
  82. func (s *DockerSuite) TestStatsAllNewContainersAdded(c *check.C) {
  83. // Windows does not support stats
  84. testRequires(c, DaemonIsLinux)
  85. id := make(chan string)
  86. addedChan := make(chan struct{})
  87. dockerCmd(c, "run", "-d", "busybox", "top")
  88. statsCmd := exec.Command(dockerBinary, "stats")
  89. stdout, err := statsCmd.StdoutPipe()
  90. c.Assert(err, check.IsNil)
  91. c.Assert(statsCmd.Start(), check.IsNil)
  92. defer statsCmd.Process.Kill()
  93. go func() {
  94. containerID := <-id
  95. matchID := regexp.MustCompile(containerID)
  96. scanner := bufio.NewScanner(stdout)
  97. for scanner.Scan() {
  98. switch {
  99. case matchID.MatchString(scanner.Text()):
  100. close(addedChan)
  101. }
  102. }
  103. }()
  104. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  105. c.Assert(waitRun(strings.TrimSpace(out)), check.IsNil)
  106. id <- strings.TrimSpace(out)[:12]
  107. select {
  108. case <-time.After(10 * time.Second):
  109. c.Fatal("failed to observe new container created added to stats")
  110. case <-addedChan:
  111. // ignore, done
  112. }
  113. }