github.com/elastic/gosigar@v0.14.3/cgroup/blkio_test.go (about)

     1  package cgroup
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  const blkioPath = "testdata/docker/sys/fs/cgroup/blkio/docker/b29faf21b7eff959f64b4192c34d5d67a707fe8561e9eaa608cb27693fba4242"
    11  
    12  func TestParseBlkioValueWithOp(t *testing.T) {
    13  	line := `253:1 Async 1638912`
    14  	opValue, err := parseBlkioValue(line)
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  
    19  	assert.Equal(t, uint64(253), opValue.Major)
    20  	assert.Equal(t, uint64(1), opValue.Minor)
    21  	assert.Equal(t, "async", opValue.Operation)
    22  	assert.Equal(t, uint64(1638912), opValue.Value)
    23  }
    24  
    25  func TestParseBlkioValueWithoutOp(t *testing.T) {
    26  	line := `1:2 10088`
    27  	opValue, err := parseBlkioValue(line)
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	assert.Equal(t, uint64(1), opValue.Major)
    33  	assert.Equal(t, uint64(2), opValue.Minor)
    34  	assert.Equal(t, "", opValue.Operation)
    35  	assert.Equal(t, uint64(10088), opValue.Value)
    36  }
    37  
    38  func TestBlkioThrottle(t *testing.T) {
    39  	blkio := BlockIOSubsystem{}
    40  	err := blkioThrottle(blkioPath, &blkio)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	assert.Equal(t, uint64(46), blkio.Throttle.TotalIOs)
    46  	assert.Equal(t, uint64(1648128), blkio.Throttle.TotalBytes)
    47  	assert.Len(t, blkio.Throttle.Devices, 3)
    48  
    49  	for _, device := range blkio.Throttle.Devices {
    50  		if device.DeviceID.Major == 7 && device.DeviceID.Minor == 0 {
    51  			assert.Equal(t, uint64(1000), device.ReadLimitBPS)
    52  			assert.Equal(t, uint64(2000), device.ReadLimitIOPS)
    53  			assert.Equal(t, uint64(3000), device.WriteLimitBPS)
    54  			assert.Equal(t, uint64(4000), device.WriteLimitIOPS)
    55  
    56  			assert.Equal(t, uint64(4608), device.Bytes.Read)
    57  			assert.Equal(t, uint64(0), device.Bytes.Write)
    58  			assert.Equal(t, uint64(4608), device.Bytes.Async)
    59  			assert.Equal(t, uint64(0), device.Bytes.Sync)
    60  
    61  			assert.Equal(t, uint64(2), device.IOs.Read)
    62  			assert.Equal(t, uint64(0), device.IOs.Write)
    63  			assert.Equal(t, uint64(2), device.IOs.Async)
    64  			assert.Equal(t, uint64(0), device.IOs.Sync)
    65  		}
    66  	}
    67  }
    68  
    69  func TestBlockIOSubsystemGet(t *testing.T) {
    70  	blkio := BlockIOSubsystem{}
    71  	if err := blkio.get(blkioPath); err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	assert.True(t, len(blkio.Throttle.Devices) > 0)
    76  }
    77  
    78  func TestBlockIOSubsystemJSON(t *testing.T) {
    79  	blkio := BlockIOSubsystem{}
    80  	if err := blkio.get(blkioPath); err != nil {
    81  		t.Fatal(err)
    82  	}
    83  
    84  	json, err := json.MarshalIndent(blkio, "", "  ")
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  
    89  	t.Log(string(json))
    90  }