Skip to content

Commit

Permalink
cgroup2: io: add io.stats parsing test
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksa Sarai <[email protected]>
  • Loading branch information
cyphar committed May 31, 2021
1 parent 6285a4c commit 4512e52
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions libcontainer/cgroups/fs2/io_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package fs2

import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"testing"

"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/cgroups/fscommon"
)

const exampleIoStatData = `254:1 rbytes=6901432320 wbytes=14245535744 rios=263278 wios=248603 dbytes=0 dios=0
254:0 rbytes=2702336 wbytes=0 rios=97 wios=0 dbytes=0 dios=0
259:0 rbytes=6911345664 wbytes=14245536256 rios=264538 wios=244914 dbytes=530485248 dios=2`

var exampleIoStatsParsed = cgroups.BlkioStats{
IoServiceBytesRecursive: []cgroups.BlkioStatEntry{
{Major: 254, Minor: 1, Value: 6901432320, Op: "Read"},
{Major: 254, Minor: 1, Value: 14245535744, Op: "Write"},
{Major: 254, Minor: 0, Value: 2702336, Op: "Read"},
{Major: 254, Minor: 0, Value: 0, Op: "Write"},
{Major: 259, Minor: 0, Value: 6911345664, Op: "Read"},
{Major: 259, Minor: 0, Value: 14245536256, Op: "Write"},
},
IoServicedRecursive: []cgroups.BlkioStatEntry{
{Major: 254, Minor: 1, Value: 263278, Op: "Read"},
{Major: 254, Minor: 1, Value: 248603, Op: "Write"},
{Major: 254, Minor: 0, Value: 97, Op: "Read"},
{Major: 254, Minor: 0, Value: 0, Op: "Write"},
{Major: 259, Minor: 0, Value: 264538, Op: "Read"},
{Major: 259, Minor: 0, Value: 244914, Op: "Write"},
},
}

func lessBlkioStatEntry(a, b cgroups.BlkioStatEntry) bool {
return a.Major < b.Major && a.Minor < a.Minor && a.Op < b.Op && a.Value < b.Value
}

func sortBlkioStats(stats *cgroups.BlkioStats) {
for _, table := range []*[]cgroups.BlkioStatEntry{
&stats.IoServicedRecursive,
&stats.IoServiceBytesRecursive,
} {
sort.SliceStable(*table, func(i, j int) bool { return lessBlkioStatEntry((*table)[i], (*table)[j]) })
}
}

func TestStatIo(t *testing.T) {
// We're using a fake cgroupfs.
fscommon.TestMode = true

fakeCgroupDir, err := ioutil.TempDir("", "runc-stat-io-test.*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(fakeCgroupDir)
statPath := filepath.Join(fakeCgroupDir, "io.stat")

if err := os.WriteFile(statPath, []byte(exampleIoStatData), 0644); err != nil {
t.Fatal(err)
}

var gotStats cgroups.Stats
if err := statIo(fakeCgroupDir, &gotStats); err != nil {
t.Error(err)
}

// Sort the output since statIo uses a map internally.
sortBlkioStats(&gotStats.BlkioStats)
sortBlkioStats(&exampleIoStatsParsed)

if !reflect.DeepEqual(gotStats.BlkioStats, exampleIoStatsParsed) {
t.Errorf("parsed cgroupv2 io.stat doesn't match expected result: \ngot %#v\nexpected %#v\n", gotStats.BlkioStats, exampleIoStatsParsed)
}
}

0 comments on commit 4512e52

Please sign in to comment.