package fs2 import ( "io/ioutil" "os" "path/filepath" "testing" "linkfog.com/public/lib/cgroup/types" ) func TestSetIORWLimit(t *testing.T) { tmpCgroupDir, err := ioutil.TempDir("/tmp", "cgroup-") if err != nil { t.Fatal("Cannot create temporary file", err) } defer os.RemoveAll(tmpCgroupDir) tmpIOMaxFile := filepath.Join(tmpCgroupDir, "io.max") devInfos := []types.DevInfo{{Name: "test-sda", Major: "1", Minor: "8"}} type ioTest struct { limit uint64 expected string } // 实际格式 8:0 rbytes=0 wbytes=12288 rios=0 wios=3 dbytes=0 dios=0 // 因写文件无法模拟真实的cgroup设置,使用非真实的期望结果做判断 ioTests := []ioTest{ {limit: 10 * 1024 * 1024, expected: "1:8 rbps=104857601:8 wbps=10485760"}, {limit: 0, expected: "1:8 rbps=max1:8 wbps=max"}, } for i, test := range ioTests { // 清除测试环境 os.Remove(tmpIOMaxFile) devLmts, err := SetIORWLimit(tmpCgroupDir, devInfos, test.limit, test.limit) if err != nil { t.Fatal(i, err) } if devLmt, ok := devLmts["1:8"]; !ok { t.Fatal(i, "device 1:8 not exist") } else if !devLmt.RdLimit || !devLmt.WrLimit { t.Fatal(i, "device 1:8 rw limit error") } ioMaxLimitContent, err := ioutil.ReadFile(tmpIOMaxFile) if err != nil { t.Fatal(i, err) } if string(ioMaxLimitContent) != test.expected { t.Fatalf("%d io max limit error, content: %s", i, string(ioMaxLimitContent)) } } }