docker_cli_stats_test.go 5.9 KB

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