package fs import ( "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) tmpCPUPeriodFile := filepath.Join(tmpCgroupDir, "cpu.cfs_period_us") tmpCPUQuotaFile := filepath.Join(tmpCgroupDir, "cpu.cfs_quota_us") DefaultCPULimit := float64(2) type cpuTest struct { period string quota string limit float64 } cpuTests := []cpuTest{ {period: "100000", quota: defaultCPU, 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(tmpCPUPeriodFile, []byte(test.period), 0) if err != nil { t.Fatal(i, "Failed to write to temporary file", err) } err = ioutil.WriteFile(tmpCPUQuotaFile, []byte(test.quota), 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) } } }