2015-05-30 13:25:51 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-09-17 12:20:25 -04:00
|
|
|
"net/http"
|
2015-07-01 12:54:26 -04:00
|
|
|
"os/exec"
|
2015-07-08 16:30:03 -04:00
|
|
|
"runtime"
|
2015-07-01 12:54:26 -04:00
|
|
|
"strconv"
|
2015-06-01 16:33:03 -04:00
|
|
|
"strings"
|
2015-06-16 10:33:49 -04:00
|
|
|
"time"
|
2015-06-01 16:33:03 -04:00
|
|
|
|
2015-05-30 13:25:51 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
2015-09-17 12:20:25 -04:00
|
|
|
func (s *DockerSuite) TestApiStatsNoStreamGetCpu(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-06-12 11:27:39 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true;do echo 'Hello'; usleep 100000; done")
|
2015-05-30 13:25:51 -04:00
|
|
|
|
|
|
|
id := strings.TrimSpace(out)
|
2015-08-11 03:41:11 -04:00
|
|
|
c.Assert(waitRun(id), check.IsNil)
|
2015-06-01 16:33:03 -04:00
|
|
|
|
2015-06-12 11:27:39 -04:00
|
|
|
resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
|
2015-06-01 16:33:03 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
2015-06-12 11:27:39 -04:00
|
|
|
c.Assert(resp.ContentLength > 0, check.Equals, true, check.Commentf("should not use chunked encoding"))
|
|
|
|
c.Assert(resp.Header.Get("Content-Type"), check.Equals, "application/json")
|
2015-06-01 16:33:03 -04:00
|
|
|
|
2015-05-30 13:25:51 -04:00
|
|
|
var v *types.Stats
|
2015-06-01 16:33:03 -04:00
|
|
|
err = json.NewDecoder(body).Decode(&v)
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-07-23 07:24:14 -04:00
|
|
|
body.Close()
|
2015-05-30 13:25:51 -04:00
|
|
|
|
2015-06-01 16:33:03 -04:00
|
|
|
var cpuPercent = 0.0
|
2015-07-23 05:40:54 -04:00
|
|
|
cpuDelta := float64(v.CPUStats.CPUUsage.TotalUsage - v.PreCPUStats.CPUUsage.TotalUsage)
|
|
|
|
systemDelta := float64(v.CPUStats.SystemUsage - v.PreCPUStats.SystemUsage)
|
|
|
|
cpuPercent = (cpuDelta / systemDelta) * float64(len(v.CPUStats.CPUUsage.PercpuUsage)) * 100.0
|
2015-06-01 16:33:03 -04:00
|
|
|
if cpuPercent == 0 {
|
|
|
|
c.Fatalf("docker stats with no-stream get cpu usage failed: was %v", cpuPercent)
|
2015-05-30 13:25:51 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-16 10:33:49 -04:00
|
|
|
|
2015-09-17 12:20:25 -04:00
|
|
|
func (s *DockerSuite) TestApiStatsStoppedContainerInGoroutines(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-06-16 10:33:49 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo 1")
|
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
|
|
|
|
getGoRoutines := func() int {
|
|
|
|
_, body, err := sockRequestRaw("GET", fmt.Sprintf("/info"), nil, "")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
info := types.Info{}
|
|
|
|
err = json.NewDecoder(body).Decode(&info)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
body.Close()
|
|
|
|
return info.NGoroutines
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the HTTP connection is closed, the number of goroutines should not increase.
|
|
|
|
routines := getGoRoutines()
|
|
|
|
_, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats", id), nil, "")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
body.Close()
|
|
|
|
|
|
|
|
t := time.After(30 * time.Second)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t:
|
|
|
|
c.Assert(getGoRoutines() <= routines, check.Equals, true)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
if n := getGoRoutines(); n <= routines {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-01 12:54:26 -04:00
|
|
|
|
2015-09-17 12:20:25 -04:00
|
|
|
func (s *DockerSuite) TestApiStatsNetworkStats(c *check.C) {
|
2015-07-08 16:30:03 -04:00
|
|
|
testRequires(c, SameHostDaemon)
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-07-01 12:54:26 -04:00
|
|
|
// Run container for 30 secs
|
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
|
|
|
|
id := strings.TrimSpace(out)
|
2015-08-11 03:41:11 -04:00
|
|
|
c.Assert(waitRun(id), check.IsNil)
|
2015-07-01 12:54:26 -04:00
|
|
|
|
|
|
|
// Retrieve the container address
|
|
|
|
contIP := findContainerIP(c, id)
|
|
|
|
numPings := 10
|
|
|
|
|
2015-08-24 05:17:15 -04:00
|
|
|
var preRxPackets uint64
|
|
|
|
var preTxPackets uint64
|
|
|
|
var postRxPackets uint64
|
|
|
|
var postTxPackets uint64
|
|
|
|
|
2015-07-01 12:54:26 -04:00
|
|
|
// Get the container networking stats before and after pinging the container
|
|
|
|
nwStatsPre := getNetworkStats(c, id)
|
2015-08-24 05:17:15 -04:00
|
|
|
for _, v := range nwStatsPre {
|
|
|
|
preRxPackets += v.RxPackets
|
|
|
|
preTxPackets += v.TxPackets
|
|
|
|
}
|
|
|
|
|
2015-07-08 16:30:03 -04:00
|
|
|
countParam := "-c"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
countParam = "-n" // Ping count parameter is -n on Windows
|
|
|
|
}
|
|
|
|
pingout, err := exec.Command("ping", contIP, countParam, strconv.Itoa(numPings)).Output()
|
|
|
|
pingouts := string(pingout[:])
|
2015-07-01 12:54:26 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
nwStatsPost := getNetworkStats(c, id)
|
2015-08-24 05:17:15 -04:00
|
|
|
for _, v := range nwStatsPost {
|
|
|
|
postRxPackets += v.RxPackets
|
|
|
|
postTxPackets += v.TxPackets
|
|
|
|
}
|
2015-07-01 12:54:26 -04:00
|
|
|
|
|
|
|
// Verify the stats contain at least the expected number of packets (account for ARP)
|
2015-08-24 05:17:15 -04:00
|
|
|
expRxPkts := 1 + preRxPackets + uint64(numPings)
|
|
|
|
expTxPkts := 1 + preTxPackets + uint64(numPings)
|
|
|
|
c.Assert(postTxPackets >= expTxPkts, check.Equals, true,
|
|
|
|
check.Commentf("Reported less TxPackets than expected. Expected >= %d. Found %d. %s", expTxPkts, postTxPackets, pingouts))
|
|
|
|
c.Assert(postRxPackets >= expRxPkts, check.Equals, true,
|
|
|
|
check.Commentf("Reported less Txbytes than expected. Expected >= %d. Found %d. %s", expRxPkts, postRxPackets, pingouts))
|
2015-07-01 12:54:26 -04:00
|
|
|
}
|
|
|
|
|
2015-08-24 05:17:15 -04:00
|
|
|
func getNetworkStats(c *check.C, id string) map[string]types.NetworkStats {
|
|
|
|
var st *types.StatsJSON
|
2015-07-01 12:54:26 -04:00
|
|
|
|
|
|
|
_, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
err = json.NewDecoder(body).Decode(&st)
|
|
|
|
c.Assert(err, check.IsNil)
|
2015-07-23 07:24:14 -04:00
|
|
|
body.Close()
|
2015-07-01 12:54:26 -04:00
|
|
|
|
2015-08-24 05:17:15 -04:00
|
|
|
return st.Networks
|
2015-07-01 12:54:26 -04:00
|
|
|
}
|
2015-09-17 12:20:25 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestApiStatsContainerNotFound(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
|
|
|
status, _, err := sockRequest("GET", "/containers/nonexistent/stats", nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(status, check.Equals, http.StatusNotFound)
|
|
|
|
|
|
|
|
status, _, err = sockRequest("GET", "/containers/nonexistent/stats?stream=0", nil)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(status, check.Equals, http.StatusNotFound)
|
|
|
|
}
|