2015-05-30 17:25:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-09-17 16:20:25 +00:00
|
|
|
"net/http"
|
2015-07-01 16:54:26 +00:00
|
|
|
"os/exec"
|
2015-07-08 20:30:03 +00:00
|
|
|
"runtime"
|
2015-07-01 16:54:26 +00:00
|
|
|
"strconv"
|
2015-06-01 20:33:03 +00:00
|
|
|
"strings"
|
2016-08-03 20:24:55 +00:00
|
|
|
"sync"
|
2015-06-16 14:33:49 +00:00
|
|
|
"time"
|
2015-06-01 20:33:03 +00:00
|
|
|
|
2015-10-13 12:01:58 +00:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2016-01-05 00:05:26 +00:00
|
|
|
"github.com/docker/engine-api/types"
|
2016-04-19 14:56:54 +00:00
|
|
|
"github.com/docker/engine-api/types/versions"
|
2015-05-30 17:25:51 +00:00
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
2015-10-30 23:32:09 +00:00
|
|
|
var expectedNetworkInterfaceStats = strings.Split("rx_bytes rx_dropped rx_errors rx_packets tx_bytes tx_dropped tx_errors tx_packets", " ")
|
|
|
|
|
2015-09-17 16:20:25 +00:00
|
|
|
func (s *DockerSuite) TestApiStatsNoStreamGetCpu(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-06-12 15:27:39 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true;do echo 'Hello'; usleep 100000; done")
|
2015-05-30 17:25:51 +00:00
|
|
|
|
|
|
|
id := strings.TrimSpace(out)
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(waitRun(id), checker.IsNil)
|
2015-06-01 20:33:03 +00:00
|
|
|
|
2015-06-12 15:27:39 +00:00
|
|
|
resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2016-01-19 04:06:01 +00:00
|
|
|
c.Assert(resp.StatusCode, checker.Equals, http.StatusOK)
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(resp.Header.Get("Content-Type"), checker.Equals, "application/json")
|
2015-06-01 20:33:03 +00:00
|
|
|
|
2015-05-30 17:25:51 +00:00
|
|
|
var v *types.Stats
|
2015-06-01 20:33:03 +00:00
|
|
|
err = json.NewDecoder(body).Decode(&v)
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-07-23 11:24:14 +00:00
|
|
|
body.Close()
|
2015-05-30 17:25:51 +00:00
|
|
|
|
2015-06-01 20:33:03 +00:00
|
|
|
var cpuPercent = 0.0
|
2015-07-23 09:40:54 +00: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-10-13 12:01:58 +00:00
|
|
|
|
|
|
|
c.Assert(cpuPercent, check.Not(checker.Equals), 0.0, check.Commentf("docker stats with no-stream get cpu usage failed: was %v", cpuPercent))
|
2015-05-30 17:25:51 +00:00
|
|
|
}
|
2015-06-16 14:33:49 +00:00
|
|
|
|
2015-09-17 16:20:25 +00:00
|
|
|
func (s *DockerSuite) TestApiStatsStoppedContainerInGoroutines(c *check.C) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-06-16 14:33:49 +00: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, "")
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-06-16 14:33:49 +00:00
|
|
|
info := types.Info{}
|
|
|
|
err = json.NewDecoder(body).Decode(&info)
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-06-16 14:33:49 +00:00
|
|
|
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, "")
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-06-16 14:33:49 +00:00
|
|
|
body.Close()
|
|
|
|
|
|
|
|
t := time.After(30 * time.Second)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t:
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(getGoRoutines(), checker.LessOrEqualThan, routines)
|
2015-06-16 14:33:49 +00:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
if n := getGoRoutines(); n <= routines {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-01 16:54:26 +00:00
|
|
|
|
2015-09-17 16:20:25 +00:00
|
|
|
func (s *DockerSuite) TestApiStatsNetworkStats(c *check.C) {
|
2015-07-08 20:30:03 +00:00
|
|
|
testRequires(c, SameHostDaemon)
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2016-01-28 07:36:31 +00:00
|
|
|
|
|
|
|
out, _ := runSleepingContainer(c)
|
2015-07-01 16:54:26 +00:00
|
|
|
id := strings.TrimSpace(out)
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(waitRun(id), checker.IsNil)
|
2015-07-01 16:54:26 +00:00
|
|
|
|
|
|
|
// Retrieve the container address
|
2015-10-26 12:00:49 +00:00
|
|
|
contIP := findContainerIP(c, id, "bridge")
|
2016-08-03 20:30:34 +00:00
|
|
|
numPings := 1
|
2015-07-01 16:54:26 +00:00
|
|
|
|
2015-08-24 09:17:15 +00:00
|
|
|
var preRxPackets uint64
|
|
|
|
var preTxPackets uint64
|
|
|
|
var postRxPackets uint64
|
|
|
|
var postTxPackets uint64
|
|
|
|
|
2015-07-01 16:54:26 +00:00
|
|
|
// Get the container networking stats before and after pinging the container
|
|
|
|
nwStatsPre := getNetworkStats(c, id)
|
2015-08-24 09:17:15 +00:00
|
|
|
for _, v := range nwStatsPre {
|
|
|
|
preRxPackets += v.RxPackets
|
|
|
|
preTxPackets += v.TxPackets
|
|
|
|
}
|
|
|
|
|
2015-07-08 20:30:03 +00:00
|
|
|
countParam := "-c"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
countParam = "-n" // Ping count parameter is -n on Windows
|
|
|
|
}
|
2016-05-23 18:20:41 +00:00
|
|
|
pingout, err := exec.Command("ping", contIP, countParam, strconv.Itoa(numPings)).CombinedOutput()
|
|
|
|
if err != nil && runtime.GOOS == "linux" {
|
|
|
|
// If it fails then try a work-around, but just for linux.
|
|
|
|
// If this fails too then go back to the old error for reporting.
|
|
|
|
//
|
|
|
|
// The ping will sometimes fail due to an apparmor issue where it
|
|
|
|
// denies access to the libc.so.6 shared library - running it
|
|
|
|
// via /lib64/ld-linux-x86-64.so.2 seems to work around it.
|
|
|
|
pingout2, err2 := exec.Command("/lib64/ld-linux-x86-64.so.2", "/bin/ping", contIP, "-c", strconv.Itoa(numPings)).CombinedOutput()
|
|
|
|
if err2 == nil {
|
|
|
|
pingout = pingout2
|
|
|
|
err = err2
|
|
|
|
}
|
|
|
|
}
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2016-05-23 18:20:41 +00:00
|
|
|
pingouts := string(pingout[:])
|
2015-07-01 16:54:26 +00:00
|
|
|
nwStatsPost := getNetworkStats(c, id)
|
2015-08-24 09:17:15 +00:00
|
|
|
for _, v := range nwStatsPost {
|
|
|
|
postRxPackets += v.RxPackets
|
|
|
|
postTxPackets += v.TxPackets
|
|
|
|
}
|
2015-07-01 16:54:26 +00:00
|
|
|
|
|
|
|
// Verify the stats contain at least the expected number of packets (account for ARP)
|
2015-08-24 09:17:15 +00:00
|
|
|
expRxPkts := 1 + preRxPackets + uint64(numPings)
|
|
|
|
expTxPkts := 1 + preTxPackets + uint64(numPings)
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(postTxPackets, checker.GreaterOrEqualThan, expTxPkts,
|
2015-08-24 09:17:15 +00:00
|
|
|
check.Commentf("Reported less TxPackets than expected. Expected >= %d. Found %d. %s", expTxPkts, postTxPackets, pingouts))
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(postRxPackets, checker.GreaterOrEqualThan, expRxPkts,
|
2015-08-24 09:17:15 +00:00
|
|
|
check.Commentf("Reported less Txbytes than expected. Expected >= %d. Found %d. %s", expRxPkts, postRxPackets, pingouts))
|
2015-07-01 16:54:26 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 23:32:09 +00:00
|
|
|
func (s *DockerSuite) TestApiStatsNetworkStatsVersioning(c *check.C) {
|
|
|
|
testRequires(c, SameHostDaemon)
|
|
|
|
testRequires(c, DaemonIsLinux)
|
2016-01-28 07:36:31 +00:00
|
|
|
|
|
|
|
out, _ := runSleepingContainer(c)
|
2015-10-30 23:32:09 +00:00
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
c.Assert(waitRun(id), checker.IsNil)
|
2016-08-03 20:24:55 +00:00
|
|
|
wg := sync.WaitGroup{}
|
2015-10-30 23:32:09 +00:00
|
|
|
|
|
|
|
for i := 17; i <= 21; i++ {
|
2016-08-03 20:24:55 +00:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
apiVersion := fmt.Sprintf("v1.%d", i)
|
|
|
|
statsJSONBlob := getVersionedStats(c, id, apiVersion)
|
|
|
|
if versions.LessThan(apiVersion, "v1.21") {
|
|
|
|
c.Assert(jsonBlobHasLTv121NetworkStats(statsJSONBlob), checker.Equals, true,
|
|
|
|
check.Commentf("Stats JSON blob from API %s %#v does not look like a <v1.21 API stats structure", apiVersion, statsJSONBlob))
|
|
|
|
} else {
|
|
|
|
c.Assert(jsonBlobHasGTE121NetworkStats(statsJSONBlob), checker.Equals, true,
|
|
|
|
check.Commentf("Stats JSON blob from API %s %#v does not look like a >=v1.21 API stats structure", apiVersion, statsJSONBlob))
|
|
|
|
}
|
|
|
|
}()
|
2015-10-30 23:32:09 +00:00
|
|
|
}
|
2016-08-03 20:24:55 +00:00
|
|
|
wg.Wait()
|
2015-10-30 23:32:09 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 09:17:15 +00:00
|
|
|
func getNetworkStats(c *check.C, id string) map[string]types.NetworkStats {
|
|
|
|
var st *types.StatsJSON
|
2015-07-01 16:54:26 +00:00
|
|
|
|
|
|
|
_, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id), nil, "")
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-07-01 16:54:26 +00:00
|
|
|
|
|
|
|
err = json.NewDecoder(body).Decode(&st)
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-07-23 11:24:14 +00:00
|
|
|
body.Close()
|
2015-07-01 16:54:26 +00:00
|
|
|
|
2015-08-24 09:17:15 +00:00
|
|
|
return st.Networks
|
2015-07-01 16:54:26 +00:00
|
|
|
}
|
2015-09-17 16:20:25 +00:00
|
|
|
|
2016-01-28 07:36:31 +00:00
|
|
|
// getVersionedStats returns stats result for the
|
|
|
|
// container with id using an API call with version apiVersion. Since the
|
2015-10-30 23:32:09 +00:00
|
|
|
// stats result type differs between API versions, we simply return
|
2016-01-28 07:36:31 +00:00
|
|
|
// map[string]interface{}.
|
|
|
|
func getVersionedStats(c *check.C, id string, apiVersion string) map[string]interface{} {
|
|
|
|
stats := make(map[string]interface{})
|
2015-10-30 23:32:09 +00:00
|
|
|
|
2016-01-28 07:36:31 +00:00
|
|
|
_, body, err := sockRequestRaw("GET", fmt.Sprintf("/%s/containers/%s/stats?stream=false", apiVersion, id), nil, "")
|
2015-10-30 23:32:09 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
defer body.Close()
|
|
|
|
|
2016-01-28 07:36:31 +00:00
|
|
|
err = json.NewDecoder(body).Decode(&stats)
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("failed to decode stat: %s", err))
|
2015-10-30 23:32:09 +00:00
|
|
|
|
|
|
|
return stats
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonBlobHasLTv121NetworkStats(blob map[string]interface{}) bool {
|
|
|
|
networkStatsIntfc, ok := blob["network"]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
networkStats, ok := networkStatsIntfc.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, expectedKey := range expectedNetworkInterfaceStats {
|
|
|
|
if _, ok := networkStats[expectedKey]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonBlobHasGTE121NetworkStats(blob map[string]interface{}) bool {
|
|
|
|
networksStatsIntfc, ok := blob["networks"]
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
networksStats, ok := networksStatsIntfc.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, networkInterfaceStatsIntfc := range networksStats {
|
|
|
|
networkInterfaceStats, ok := networkInterfaceStatsIntfc.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, expectedKey := range expectedNetworkInterfaceStats {
|
|
|
|
if _, ok := networkInterfaceStats[expectedKey]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-09-17 16:20:25 +00:00
|
|
|
func (s *DockerSuite) TestApiStatsContainerNotFound(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
|
|
|
status, _, err := sockRequest("GET", "/containers/nonexistent/stats", nil)
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(status, checker.Equals, http.StatusNotFound)
|
2015-09-17 16:20:25 +00:00
|
|
|
|
|
|
|
status, _, err = sockRequest("GET", "/containers/nonexistent/stats?stream=0", nil)
|
2015-10-13 12:01:58 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(status, checker.Equals, http.StatusNotFound)
|
2015-09-17 16:20:25 +00:00
|
|
|
}
|
2016-04-08 02:09:07 +00:00
|
|
|
|
2016-04-09 06:57:04 +00:00
|
|
|
func (s *DockerSuite) TestApiStatsNoStreamConnectedContainers(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
|
|
|
out1, _ := runSleepingContainer(c)
|
|
|
|
id1 := strings.TrimSpace(out1)
|
|
|
|
c.Assert(waitRun(id1), checker.IsNil)
|
|
|
|
|
|
|
|
out2, _ := runSleepingContainer(c, "--net", "container:"+id1)
|
|
|
|
id2 := strings.TrimSpace(out2)
|
|
|
|
c.Assert(waitRun(id2), checker.IsNil)
|
|
|
|
|
|
|
|
ch := make(chan error)
|
|
|
|
go func() {
|
|
|
|
resp, body, err := sockRequestRaw("GET", fmt.Sprintf("/containers/%s/stats?stream=false", id2), nil, "")
|
|
|
|
defer body.Close()
|
|
|
|
if err != nil {
|
|
|
|
ch <- err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
ch <- fmt.Errorf("Invalid StatusCode %v", resp.StatusCode)
|
|
|
|
}
|
|
|
|
if resp.Header.Get("Content-Type") != "application/json" {
|
|
|
|
ch <- fmt.Errorf("Invalid 'Content-Type' %v", resp.Header.Get("Content-Type"))
|
|
|
|
}
|
|
|
|
var v *types.Stats
|
|
|
|
if err := json.NewDecoder(body).Decode(&v); err != nil {
|
|
|
|
ch <- err
|
|
|
|
}
|
|
|
|
ch <- nil
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-ch:
|
|
|
|
c.Assert(err, checker.IsNil, check.Commentf("Error in stats remote API: %v", err))
|
|
|
|
case <-time.After(15 * time.Second):
|
|
|
|
c.Fatalf("Stats did not return after timeout")
|
|
|
|
}
|
|
|
|
}
|