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