package fs2 import ( "fmt" "io/ioutil" "os" "path/filepath" "testing" ) func TestGetCPULimit(t *testing.T) { tmpCgroupDir, err := ioutil.TempDir("/tmp", "cgroup-") if err != nil { t.Fatal("Cannot create temporary file", err) } defer os.RemoveAll(tmpCgroupDir) tmpCPUMaxFile := filepath.Join(tmpCgroupDir, "cpu.max") DefaultCPULimit := float64(2) type cpuTest struct { period string quota string limit float64 } cpuTests := []cpuTest{ {period: "100000", quota: "max", limit: DefaultCPULimit}, {period: "100000", quota: "200000", limit: float64(2)}, {period: "100000", quota: "0", limit: DefaultCPULimit}, {period: "0", quota: "200000", limit: DefaultCPULimit}, } for i, test := range cpuTests { err = ioutil.WriteFile(tmpCPUMaxFile, []byte(fmt.Sprintf("%s %s", test.quota, test.period)), 0) if err != nil { t.Fatal(i, "Failed to write to temporary file", err) } cpuLimit := GetCPULimit(tmpCgroupDir, DefaultCPULimit) if cpuLimit != test.limit { t.Fatalf("%d cpuLimit(%f) != expected(%f)", i, cpuLimit, test.limit) } } }