github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/api/client/stats_unit_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/docker/engine-api/types"
     9  )
    10  
    11  func TestDisplay(t *testing.T) {
    12  	c := &containerStats{
    13  		Name:             "app",
    14  		CPUPercentage:    30.0,
    15  		Memory:           100 * 1024 * 1024.0,
    16  		MemoryLimit:      2048 * 1024 * 1024.0,
    17  		MemoryPercentage: 100.0 / 2048.0 * 100.0,
    18  		NetworkRx:        100 * 1024 * 1024,
    19  		NetworkTx:        800 * 1024 * 1024,
    20  		BlockRead:        100 * 1024 * 1024,
    21  		BlockWrite:       800 * 1024 * 1024,
    22  		PidsCurrent:      1,
    23  		mu:               sync.RWMutex{},
    24  	}
    25  	var b bytes.Buffer
    26  	if err := c.Display(&b); err != nil {
    27  		t.Fatalf("c.Display() gave error: %s", err)
    28  	}
    29  	got := b.String()
    30  	want := "app\t30.00%\t100 MiB / 2 GiB\t4.88%\t104.9 MB / 838.9 MB\t104.9 MB / 838.9 MB\t1\n"
    31  	if got != want {
    32  		t.Fatalf("c.Display() = %q, want %q", got, want)
    33  	}
    34  }
    35  
    36  func TestCalculBlockIO(t *testing.T) {
    37  	blkio := types.BlkioStats{
    38  		IoServiceBytesRecursive: []types.BlkioStatEntry{{8, 0, "read", 1234}, {8, 1, "read", 4567}, {8, 0, "write", 123}, {8, 1, "write", 456}},
    39  	}
    40  	blkRead, blkWrite := calculateBlockIO(blkio)
    41  	if blkRead != 5801 {
    42  		t.Fatalf("blkRead = %d, want 5801", blkRead)
    43  	}
    44  	if blkWrite != 579 {
    45  		t.Fatalf("blkWrite = %d, want 579", blkWrite)
    46  	}
    47  }