1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 type CgroupSuite struct{}
18 var _ = Suite(&CgroupSuite{})
20 func (s *CgroupSuite) TestFindCgroup(c *C) {
21 var testfiles []string
22 buf, err := exec.Command("find", "../crunchstat/testdata", "-name", "cgroup", "-type", "f").Output()
24 for _, testfile := range bytes.Split(buf, []byte{'\n'}) {
25 if len(testfile) > 0 {
26 testfiles = append(testfiles, string(testfile))
29 testfiles = append(testfiles, "/proc/self/cgroup")
32 err = os.MkdirAll(tmpdir+"/proc/self", 0777)
34 fsys := os.DirFS(tmpdir)
36 for _, trial := range []struct {
37 match string // if non-empty, only check testfiles containing this string
39 expect string // empty means "any" (we never actually expect empty string)
41 {"debian11", "blkio", "/user.slice/user-1000.slice/session-5424.scope"},
42 {"debian12", "cpuacct", "/user.slice/user-1000.slice/session-4.scope"},
43 {"debian12", "bogus-does-not-matter", "/user.slice/user-1000.slice/session-4.scope"},
44 {"ubuntu1804", "blkio", "/user.slice"},
45 {"ubuntu1804", "cpuacct", "/user.slice"},
49 {"", "bogus-does-not-matter", ""},
51 for _, testfile := range testfiles {
52 if !strings.Contains(testfile, trial.match) {
55 c.Logf("trial %+v testfile %s", trial, testfile)
57 // Copy cgroup file into our fake proc/self/ dir
58 buf, err := os.ReadFile(testfile)
60 err = os.WriteFile(tmpdir+"/proc/self/cgroup", buf, 0777)
63 cgroup, err := findCgroup(fsys, trial.subsys)
64 if !c.Check(err, IsNil) {
67 c.Logf("\tcgroup = %q", cgroup)
68 c.Check(cgroup, Not(Equals), "")
69 if trial.expect != "" {
70 c.Check(cgroup, Equals, trial.expect)