github.com/lacework-dev/go-moby@v20.10.12+incompatible/integration/container/stats_test.go (about)

     1  package container // import "github.com/docker/docker/integration/container"
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"io"
     7  	"reflect"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/integration/internal/container"
    13  	"gotest.tools/v3/assert"
    14  	is "gotest.tools/v3/assert/cmp"
    15  	"gotest.tools/v3/poll"
    16  	"gotest.tools/v3/skip"
    17  )
    18  
    19  func TestStats(t *testing.T) {
    20  	skip.If(t, testEnv.DaemonInfo.CgroupDriver == "none")
    21  	skip.If(t, !testEnv.DaemonInfo.MemoryLimit)
    22  
    23  	defer setupTest(t)()
    24  	client := testEnv.APIClient()
    25  	ctx := context.Background()
    26  
    27  	info, err := client.Info(ctx)
    28  	assert.NilError(t, err)
    29  
    30  	cID := container.Run(ctx, t, client)
    31  
    32  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
    33  
    34  	resp, err := client.ContainerStats(ctx, cID, false)
    35  	assert.NilError(t, err)
    36  	defer resp.Body.Close()
    37  
    38  	var v types.Stats
    39  	err = json.NewDecoder(resp.Body).Decode(&v)
    40  	assert.NilError(t, err)
    41  	assert.Check(t, is.Equal(int64(v.MemoryStats.Limit), info.MemTotal))
    42  	assert.Check(t, !reflect.DeepEqual(v.PreCPUStats, types.CPUStats{}))
    43  	err = json.NewDecoder(resp.Body).Decode(&v)
    44  	assert.Assert(t, is.ErrorContains(err, ""), io.EOF)
    45  
    46  	resp, err = client.ContainerStatsOneShot(ctx, cID)
    47  	assert.NilError(t, err)
    48  	defer resp.Body.Close()
    49  
    50  	v = types.Stats{}
    51  	err = json.NewDecoder(resp.Body).Decode(&v)
    52  	assert.NilError(t, err)
    53  	assert.Check(t, is.Equal(int64(v.MemoryStats.Limit), info.MemTotal))
    54  	assert.Check(t, is.DeepEqual(v.PreCPUStats, types.CPUStats{}))
    55  	err = json.NewDecoder(resp.Body).Decode(&v)
    56  	assert.Assert(t, is.ErrorContains(err, ""), io.EOF)
    57  }