package fs2 import ( "io/ioutil" "os" "path/filepath" "testing" ) func TestGetMemoryLimit(t *testing.T) { tmpCgroupDir, err := ioutil.TempDir("/tmp", "cgroup-") if err != nil { t.Fatal("Cannot create temporary file", err) } defer os.RemoveAll(tmpCgroupDir) tmpMemLimitFile := filepath.Join(tmpCgroupDir, "memory.max") DefaultMemLimit := float64(3 * 1024 * 1024 * 1024) type memTest struct { limitSet string limitGet float64 } memTests := []memTest{ {limitSet: "max", limitGet: DefaultMemLimit}, {limitSet: "1024000000", limitGet: float64(1024000000)}, {limitSet: "0", limitGet: DefaultMemLimit}, } for i, test := range memTests { err = ioutil.WriteFile(tmpMemLimitFile, []byte(test.limitSet), 0) if err != nil { t.Fatal(i, "Failed to write to temporary file", err) } memLimit := GetMemoryLimit(tmpCgroupDir, DefaultMemLimit) if memLimit != test.limitGet { t.Fatalf("%d memLimit(%f) != expected(%f)", i, memLimit, test.limitGet) } } } func TestGetMemoryUsage(t *testing.T) { usage, workingSet, err := GetMemoryUsage("../testdata/cg2") if err != nil { t.Error("err:", err) return } if usage != 102400001 { t.Errorf("unexpected usage %d, should be %d", usage, 102400000) return } if workingSet != 100000001 { t.Errorf("unexpected usage %d, should be %d", workingSet, 100000000) return } } func TestGetKernelMemoryUsage(t *testing.T) { _, err := GetKernelMemoryUsage("../testdata/cg2") if err == nil { t.Error("err should not be nil") return } } func TestGetKernelMemorySlabInfo(t *testing.T) { _, err := GetKernelMemorySlabInfo("../testdata/cg2") if err == nil { t.Error("err should not be nil") return } }