github.com/intel/goresctrl@v0.5.0/pkg/cgroups/cgroupcontrol_test.go (about) 1 package cgroups 2 3 import ( 4 "io" 5 "syscall" 6 "testing" 7 8 "github.com/intel/goresctrl/pkg/testutils" 9 ) 10 11 var cpuacctMyGroupTasks string = "" 12 var testfiles fsiIface = NewFsiMock(map[string]mockFile{ 13 "/sys/fs/cgroup/blkio/kubepods/tasks": { 14 data: []byte("1\n23\n4567890\n"), 15 }, 16 "/sys/fs/cgroup/cpu/open/permission/denied/cgroup.procs": { 17 // simulate open permission denied 18 open: func(string) (fileIface, error) { 19 return nil, syscall.EACCES 20 }, 21 }, 22 "/sys/fs/cgroup/cpuacct/store/all/writes/tasks": { 23 // everything that is written can be read 24 // (no overwrite / truncate) 25 write: func(b []byte) (int, error) { 26 cpuacctMyGroupTasks = cpuacctMyGroupTasks + string(b) + "\n" 27 return len(b), nil 28 }, 29 read: func(b []byte) (int, error) { 30 if len(cpuacctMyGroupTasks) == 0 { 31 return 0, io.EOF 32 } 33 bytes := len(cpuacctMyGroupTasks) 34 copy(b, []byte(cpuacctMyGroupTasks)) 35 cpuacctMyGroupTasks = "" 36 return bytes, nil 37 }, 38 }, 39 "/sys/fs/cgroup/cpuset/read/io/error/tasks": { 40 // every read causes I/O error 41 read: func(b []byte) (int, error) { 42 return 0, syscall.EIO 43 }, 44 }, 45 "/sys/fs/cgroup/devices/write/io/error/cgroup.procs": { 46 // every write causes I/O error 47 write: func(b []byte) (int, error) { 48 return 0, syscall.EIO 49 }, 50 }, 51 }) 52 53 func TestGetTasks(t *testing.T) { 54 fsi = testfiles 55 tasks, err := Blkio.Group("kubepods").GetTasks() 56 testutils.VerifyNoError(t, err) 57 testutils.VerifyStringSlices(t, []string{"1", "23", "4567890"}, tasks) 58 } 59 60 func TestGetProcesses(t *testing.T) { 61 fsi = testfiles 62 _, err := Cpu.Group("open/permission/denied").GetProcesses() 63 testutils.VerifyError(t, err, 1, []string{"permission denied"}) 64 } 65 66 func TestAddTasks(t *testing.T) { 67 fsi = testfiles 68 if err := Cpuacct.Group("store/all/writes").AddTasks("0", "987654321"); !testutils.VerifyNoError(t, err) { 69 return 70 } 71 if err := Cpuacct.Group("store/all/writes").AddTasks(); !testutils.VerifyNoError(t, err) { 72 return 73 } 74 if err := Cpuacct.Group("store/all/writes").AddTasks("12"); !testutils.VerifyNoError(t, err) { 75 return 76 } 77 tasks, err := Cpuacct.Group("store/all/writes").GetTasks() 78 testutils.VerifyNoError(t, err) 79 testutils.VerifyStringSlices(t, []string{"0", "987654321", "12"}, tasks) 80 } 81 82 func TestAddProcesses(t *testing.T) { 83 fsi = testfiles 84 err := Devices.Group("write/io/error").AddProcesses("1") 85 testutils.VerifyError(t, err, 1, []string{"input/output error"}) 86 err = Freezer.Group("file/not/found").AddProcesses("1") 87 testutils.VerifyError(t, err, 1, []string{"file not found"}) 88 } 89 90 func TestAsGroup(t *testing.T) { 91 memGroupIn := Memory.Group("my/memory") 92 memGroupOut := AsGroup(string(memGroupIn)) 93 testutils.VerifyStrings(t, string(memGroupIn), string(memGroupOut)) 94 } 95 96 func TestGroupToController(t *testing.T) { 97 c := Hugetlb.Group("my/group").Controller() 98 testutils.VerifyStrings(t, "hugetlb", c.String()) 99 } 100 101 func TestRelPath(t *testing.T) { 102 relPath := NetCls.RelPath() 103 testutils.VerifyStrings(t, "net_cls", relPath) 104 }