integration-cli/docker_api_stats_test.go
96123a1f
 package main
 
 import (
7d62e40f
 	"context"
96123a1f
 	"encoding/json"
 	"fmt"
66be81b1
 	"net/http"
8b40e44c
 	"os/exec"
c1b52448
 	"runtime"
8b40e44c
 	"strconv"
4fde1cb6
 	"strings"
2f3b7f08
 	"sync"
e25352a4
 	"testing"
1cbf5a54
 	"time"
4fde1cb6
 
91e197d6
 	"github.com/docker/docker/api/types"
 	"github.com/docker/docker/api/types/versions"
0fd5a654
 	"github.com/docker/docker/client"
42f6fdf0
 	"github.com/docker/docker/internal/test/request"
6345208b
 	"gotest.tools/assert"
96123a1f
 )
 
8ceded6d
 var expectedNetworkInterfaceStats = strings.Split("rx_bytes rx_dropped rx_errors rx_packets tx_bytes tx_dropped tx_errors tx_packets", " ")
 
64a928a3
 func (s *DockerSuite) TestAPIStatsNoStreamGetCpu(c *testing.T) {
c141574d
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "while true;usleep 100; do echo 'Hello'; done")
96123a1f
 
 	id := strings.TrimSpace(out)
6345208b
 	assert.NilError(c, waitRun(id))
b11ba123
 	resp, body, err := request.Get(fmt.Sprintf("/containers/%s/stats?stream=false", id))
6345208b
 	assert.NilError(c, err)
 	assert.Equal(c, resp.StatusCode, http.StatusOK)
 	assert.Equal(c, resp.Header.Get("Content-Type"), "application/json")
 	assert.Equal(c, resp.Header.Get("Content-Type"), "application/json")
4fde1cb6
 
96123a1f
 	var v *types.Stats
4fde1cb6
 	err = json.NewDecoder(body).Decode(&v)
6345208b
 	assert.NilError(c, err)
18faf6f9
 	body.Close()
96123a1f
 
4fde1cb6
 	var cpuPercent = 0.0
340e5233
 
18a771a7
 	if testEnv.OSType != "windows" {
340e5233
 		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
 	} else {
 		// Max number of 100ns intervals between the previous time read and now
 		possIntervals := uint64(v.Read.Sub(v.PreRead).Nanoseconds()) // Start with number of ns intervals
 		possIntervals /= 100                                         // Convert to number of 100ns intervals
 		possIntervals *= uint64(v.NumProcs)                          // Multiple by the number of processors
 
 		// Intervals used
 		intervalsUsed := v.CPUStats.CPUUsage.TotalUsage - v.PreCPUStats.CPUUsage.TotalUsage
 
 		// Percentage avoiding divide-by-zero
 		if possIntervals > 0 {
 			cpuPercent = float64(intervalsUsed) / float64(possIntervals) * 100.0
 		}
 	}
710817a7
 
6345208b
 	assert.Assert(c, cpuPercent != 0.0, "docker stats with no-stream get cpu usage failed: was %v", cpuPercent)
96123a1f
 }
1cbf5a54
 
64a928a3
 func (s *DockerSuite) TestAPIStatsStoppedContainerInGoroutines(c *testing.T) {
1cbf5a54
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "echo 1")
 	id := strings.TrimSpace(out)
 
 	getGoRoutines := func() int {
b11ba123
 		_, body, err := request.Get(fmt.Sprintf("/info"))
6345208b
 		assert.NilError(c, err)
1cbf5a54
 		info := types.Info{}
 		err = json.NewDecoder(body).Decode(&info)
6345208b
 		assert.NilError(c, err)
1cbf5a54
 		body.Close()
 		return info.NGoroutines
 	}
 
 	// When the HTTP connection is closed, the number of goroutines should not increase.
 	routines := getGoRoutines()
b11ba123
 	_, body, err := request.Get(fmt.Sprintf("/containers/%s/stats", id))
6345208b
 	assert.NilError(c, err)
1cbf5a54
 	body.Close()
 
 	t := time.After(30 * time.Second)
 	for {
 		select {
 		case <-t:
6345208b
 			assert.Assert(c, getGoRoutines() <= routines)
1cbf5a54
 			return
 		default:
 			if n := getGoRoutines(); n <= routines {
 				return
 			}
 			time.Sleep(200 * time.Millisecond)
 		}
 	}
 }
8b40e44c
 
64a928a3
 func (s *DockerSuite) TestAPIStatsNetworkStats(c *testing.T) {
43b15e92
 	testRequires(c, testEnv.IsLocalDaemon)
695b7e8d
 
a899aa67
 	out := runSleepingContainer(c)
8b40e44c
 	id := strings.TrimSpace(out)
6345208b
 	assert.NilError(c, waitRun(id))
8b40e44c
 
 	// Retrieve the container address
340e5233
 	net := "bridge"
18a771a7
 	if testEnv.OSType == "windows" {
340e5233
 		net = "nat"
 	}
 	contIP := findContainerIP(c, id, net)
cfde8e78
 	numPings := 1
8b40e44c
 
d3379946
 	var preRxPackets uint64
 	var preTxPackets uint64
 	var postRxPackets uint64
 	var postTxPackets uint64
 
8b40e44c
 	// Get the container networking stats before and after pinging the container
 	nwStatsPre := getNetworkStats(c, id)
d3379946
 	for _, v := range nwStatsPre {
 		preRxPackets += v.RxPackets
 		preTxPackets += v.TxPackets
 	}
 
c1b52448
 	countParam := "-c"
 	if runtime.GOOS == "windows" {
 		countParam = "-n" // Ping count parameter is -n on Windows
 	}
94d05713
 	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
 		}
 	}
6345208b
 	assert.NilError(c, err)
94d05713
 	pingouts := string(pingout[:])
8b40e44c
 	nwStatsPost := getNetworkStats(c, id)
d3379946
 	for _, v := range nwStatsPost {
 		postRxPackets += v.RxPackets
 		postTxPackets += v.TxPackets
 	}
8b40e44c
 
340e5233
 	// Verify the stats contain at least the expected number of packets
 	// On Linux, account for ARP.
 	expRxPkts := preRxPackets + uint64(numPings)
 	expTxPkts := preTxPackets + uint64(numPings)
18a771a7
 	if testEnv.OSType != "windows" {
340e5233
 		expRxPkts++
 		expTxPkts++
 	}
6345208b
 	assert.Assert(c, postTxPackets >= expTxPkts, "Reported less TxPackets than expected. Expected >= %d. Found %d. %s", expTxPkts, postTxPackets, pingouts)
 	assert.Assert(c, postRxPackets >= expRxPkts, "Reported less RxPackets than expected. Expected >= %d. Found %d. %s", expRxPkts, postRxPackets, pingouts)
8b40e44c
 }
 
64a928a3
 func (s *DockerSuite) TestAPIStatsNetworkStatsVersioning(c *testing.T) {
f811d5b1
 	// Windows doesn't support API versions less than 1.25, so no point testing 1.17 .. 1.21
43b15e92
 	testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
695b7e8d
 
a899aa67
 	out := runSleepingContainer(c)
8ceded6d
 	id := strings.TrimSpace(out)
6345208b
 	assert.NilError(c, waitRun(id))
2f3b7f08
 	wg := sync.WaitGroup{}
8ceded6d
 
f811d5b1
 	for i := 17; i <= 21; i++ {
2f3b7f08
 		wg.Add(1)
825e3a66
 		go func(i int) {
2f3b7f08
 			defer wg.Done()
 			apiVersion := fmt.Sprintf("v1.%d", i)
 			statsJSONBlob := getVersionedStats(c, id, apiVersion)
 			if versions.LessThan(apiVersion, "v1.21") {
6345208b
 				assert.Assert(c, jsonBlobHasLTv121NetworkStats(statsJSONBlob), "Stats JSON blob from API %s %#v does not look like a <v1.21 API stats structure", apiVersion, statsJSONBlob)
2f3b7f08
 			} else {
6345208b
 				assert.Assert(c, jsonBlobHasGTE121NetworkStats(statsJSONBlob), "Stats JSON blob from API %s %#v does not look like a >=v1.21 API stats structure", apiVersion, statsJSONBlob)
2f3b7f08
 			}
825e3a66
 		}(i)
8ceded6d
 	}
2f3b7f08
 	wg.Wait()
8ceded6d
 }
 
64a928a3
 func getNetworkStats(c *testing.T, id string) map[string]types.NetworkStats {
d3379946
 	var st *types.StatsJSON
8b40e44c
 
b11ba123
 	_, body, err := request.Get(fmt.Sprintf("/containers/%s/stats?stream=false", id))
6345208b
 	assert.NilError(c, err)
8b40e44c
 
 	err = json.NewDecoder(body).Decode(&st)
6345208b
 	assert.NilError(c, err)
18faf6f9
 	body.Close()
8b40e44c
 
d3379946
 	return st.Networks
8b40e44c
 }
66be81b1
 
695b7e8d
 // getVersionedStats returns stats result for the
 // container with id using an API call with version apiVersion. Since the
8ceded6d
 // stats result type differs between API versions, we simply return
695b7e8d
 // map[string]interface{}.
64a928a3
 func getVersionedStats(c *testing.T, id string, apiVersion string) map[string]interface{} {
695b7e8d
 	stats := make(map[string]interface{})
8ceded6d
 
b11ba123
 	_, body, err := request.Get(fmt.Sprintf("/%s/containers/%s/stats?stream=false", apiVersion, id))
6345208b
 	assert.NilError(c, err)
8ceded6d
 	defer body.Close()
 
695b7e8d
 	err = json.NewDecoder(body).Decode(&stats)
6345208b
 	assert.NilError(c, err, "failed to decode stat: %s", err)
8ceded6d
 
 	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
 }
 
64a928a3
 func (s *DockerSuite) TestAPIStatsContainerNotFound(c *testing.T) {
66be81b1
 	testRequires(c, DaemonIsLinux)
c8ff5ecc
 	cli, err := client.NewClientWithOpts(client.FromEnv)
6345208b
 	assert.NilError(c, err)
0fd5a654
 	defer cli.Close()
66be81b1
 
0fd5a654
 	expected := "No such container: nonexistent"
 
 	_, err = cli.ContainerStats(context.Background(), "nonexistent", true)
6345208b
 	assert.ErrorContains(c, err, expected)
0fd5a654
 	_, err = cli.ContainerStats(context.Background(), "nonexistent", false)
6345208b
 	assert.ErrorContains(c, err, expected)
66be81b1
 }
a0a6d031
 
64a928a3
 func (s *DockerSuite) TestAPIStatsNoStreamConnectedContainers(c *testing.T) {
faf2b6f7
 	testRequires(c, DaemonIsLinux)
 
a899aa67
 	out1 := runSleepingContainer(c)
faf2b6f7
 	id1 := strings.TrimSpace(out1)
6345208b
 	assert.NilError(c, waitRun(id1))
faf2b6f7
 
a899aa67
 	out2 := runSleepingContainer(c, "--net", "container:"+id1)
faf2b6f7
 	id2 := strings.TrimSpace(out2)
6345208b
 	assert.NilError(c, waitRun(id2))
faf2b6f7
 
ddae20c0
 	ch := make(chan error, 1)
faf2b6f7
 	go func() {
b11ba123
 		resp, body, err := request.Get(fmt.Sprintf("/containers/%s/stats?stream=false", id2))
faf2b6f7
 		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:
6345208b
 		assert.NilError(c, err, "Error in stats Engine API: %v", err)
faf2b6f7
 	case <-time.After(15 * time.Second):
 		c.Fatalf("Stats did not return after timeout")
 	}
 }