docker_api_stats_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "os/exec"
  7. "runtime"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/pkg/integration/checker"
  13. "github.com/go-check/check"
  14. )
  15. func (s *DockerSuite) TestApiStatsNoStreamGetCpu(c *check.C) {
  16. testRequires(c, DaemonIsLinux)
  17. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true;do echo 'Hello'; usleep 100000; done")
  18. id := strings.TrimSpace(out)
  19. c.Assert(waitRun(id), checker.IsNil)
  20. resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
  21. c.Assert(err, checker.IsNil)
  22. c.Assert(resp.ContentLength, checker.GreaterThan, int64(0), check.Commentf("should not use chunked encoding"))
  23. c.Assert(resp.Header.Get("Content-Type"), checker.Equals, "application/json")
  24. var v *types.Stats
  25. err = json.NewDecoder(body).Decode(&v)
  26. c.Assert(err, checker.IsNil)
  27. body.Close()
  28. var cpuPercent = 0.0
  29. cpuDelta := float64(v.CPUStats.CPUUsage.TotalUsage - v.PreCPUStats.CPUUsage.TotalUsage)
  30. systemDelta := float64(v.CPUStats.SystemUsage - v.PreCPUStats.SystemUsage)
  31. cpuPercent = (cpuDelta / systemDelta) * float64(len(v.CPUStats.CPUUsage.PercpuUsage)) * 100.0
  32. c.Assert(cpuPercent, check.Not(checker.Equals), 0.0, check.Commentf("docker stats with no-stream get cpu usage failed: was %v", cpuPercent))
  33. }
  34. func (s *DockerSuite) TestApiStatsStoppedContainerInGoroutines(c *check.C) {
  35. testRequires(c, DaemonIsLinux)
  36. out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo 1")
  37. id := strings.TrimSpace(out)
  38. getGoRoutines := func() int {
  39. _, body, err := sockRequestRaw("GET", fmt.Sprintf("/info"), nil, "")
  40. c.Assert(err, checker.IsNil)
  41. info := types.Info{}
  42. err = json.NewDecoder(body).Decode(&info)
  43. c.Assert(err, checker.IsNil)
  44. body.Close()
  45. return info.NGoroutines
  46. }
  47. // When the HTTP connection is closed, the number of goroutines should not increase.
  48. routines := getGoRoutines()
  49. _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats", id), nil, "")
  50. c.Assert(err, checker.IsNil)
  51. body.Close()
  52. t := time.After(30 * time.Second)
  53. for {
  54. select {
  55. case <-t:
  56. c.Assert(getGoRoutines(), checker.LessOrEqualThan, routines)
  57. return
  58. default:
  59. if n := getGoRoutines(); n <= routines {
  60. return
  61. }
  62. time.Sleep(200 * time.Millisecond)
  63. }
  64. }
  65. }
  66. func (s *DockerSuite) TestApiStatsNetworkStats(c *check.C) {
  67. testRequires(c, SameHostDaemon)
  68. testRequires(c, DaemonIsLinux)
  69. // Run container for 30 secs
  70. out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
  71. id := strings.TrimSpace(out)
  72. c.Assert(waitRun(id), checker.IsNil)
  73. // Retrieve the container address
  74. contIP := findContainerIP(c, id, "bridge")
  75. numPings := 10
  76. var preRxPackets uint64
  77. var preTxPackets uint64
  78. var postRxPackets uint64
  79. var postTxPackets uint64
  80. // Get the container networking stats before and after pinging the container
  81. nwStatsPre := getNetworkStats(c, id)
  82. for _, v := range nwStatsPre {
  83. preRxPackets += v.RxPackets
  84. preTxPackets += v.TxPackets
  85. }
  86. countParam := "-c"
  87. if runtime.GOOS == "windows" {
  88. countParam = "-n" // Ping count parameter is -n on Windows
  89. }
  90. pingout, err := exec.Command("ping", contIP, countParam, strconv.Itoa(numPings)).Output()
  91. pingouts := string(pingout[:])
  92. c.Assert(err, checker.IsNil)
  93. nwStatsPost := getNetworkStats(c, id)
  94. for _, v := range nwStatsPost {
  95. postRxPackets += v.RxPackets
  96. postTxPackets += v.TxPackets
  97. }
  98. // Verify the stats contain at least the expected number of packets (account for ARP)
  99. expRxPkts := 1 + preRxPackets + uint64(numPings)
  100. expTxPkts := 1 + preTxPackets + uint64(numPings)
  101. c.Assert(postTxPackets, checker.GreaterOrEqualThan, expTxPkts,
  102. check.Commentf("Reported less TxPackets than expected. Expected >= %d. Found %d. %s", expTxPkts, postTxPackets, pingouts))
  103. c.Assert(postRxPackets, checker.GreaterOrEqualThan, expRxPkts,
  104. check.Commentf("Reported less Txbytes than expected. Expected >= %d. Found %d. %s", expRxPkts, postRxPackets, pingouts))
  105. }
  106. func getNetworkStats(c *check.C, id string) map[string]types.NetworkStats {
  107. var st *types.StatsJSON
  108. _, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
  109. c.Assert(err, checker.IsNil)
  110. err = json.NewDecoder(body).Decode(&st)
  111. c.Assert(err, checker.IsNil)
  112. body.Close()
  113. return st.Networks
  114. }
  115. func (s *DockerSuite) TestApiStatsContainerNotFound(c *check.C) {
  116. testRequires(c, DaemonIsLinux)
  117. status, _, err := sockRequest("GET", "/containers/nonexistent/stats", nil)
  118. c.Assert(err, checker.IsNil)
  119. c.Assert(status, checker.Equals, http.StatusNotFound)
  120. status, _, err = sockRequest("GET", "/containers/nonexistent/stats?stream=0", nil)
  121. c.Assert(err, checker.IsNil)
  122. c.Assert(status, checker.Equals, http.StatusNotFound)
  123. }