github.com/a4a881d4/docker@v1.9.0-rc2/api/client/stats_unit_test.go (about)

     1  package client
     2  
     3  import (
     4  	"bytes"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/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  		mu:               sync.RWMutex{},
    23  	}
    24  	var b bytes.Buffer
    25  	if err := c.Display(&b); err != nil {
    26  		t.Fatalf("c.Display() gave error: %s", err)
    27  	}
    28  	got := b.String()
    29  	want := "app\t30.00%\t104.9 MB / 2.147 GB\t4.88%\t104.9 MB / 838.9 MB\t104.9 MB / 838.9 MB\n"
    30  	if got != want {
    31  		t.Fatalf("c.Display() = %q, want %q", got, want)
    32  	}
    33  }
    34  
    35  func TestCalculBlockIO(t *testing.T) {
    36  	blkio := types.BlkioStats{
    37  		IoServiceBytesRecursive: []types.BlkioStatEntry{{8, 0, "read", 1234}, {8, 1, "read", 4567}, {8, 0, "write", 123}, {8, 1, "write", 456}},
    38  	}
    39  	blkRead, blkWrite := calculateBlockIO(blkio)
    40  	if blkRead != 5801 {
    41  		t.Fatalf("blkRead = %d, want 5801", blkRead)
    42  	}
    43  	if blkWrite != 579 {
    44  		t.Fatalf("blkWrite = %d, want 579", blkWrite)
    45  	}
    46  }