X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/a3c7d9e03062e3246b0857fbae05f45d22e39169..defaae08f226bb9efbb76a0e15d392f46e929479:/lib/crunchrun/cgroup_test.go diff --git a/lib/crunchrun/cgroup_test.go b/lib/crunchrun/cgroup_test.go index b43479a3b4..a1acb6fb92 100644 --- a/lib/crunchrun/cgroup_test.go +++ b/lib/crunchrun/cgroup_test.go @@ -5,6 +5,11 @@ package crunchrun import ( + "bytes" + "os" + "os/exec" + "strings" + . "gopkg.in/check.v1" ) @@ -13,9 +18,57 @@ type CgroupSuite struct{} var _ = Suite(&CgroupSuite{}) func (s *CgroupSuite) TestFindCgroup(c *C) { - for _, s := range []string{"devices", "cpu", "cpuset"} { - g := findCgroup(s) - c.Check(g, Not(Equals), "") - c.Logf("cgroup(%q) == %q", s, g) + var testfiles []string + buf, err := exec.Command("find", "../crunchstat/testdata", "-name", "cgroup", "-type", "f").Output() + c.Assert(err, IsNil) + for _, testfile := range bytes.Split(buf, []byte{'\n'}) { + if len(testfile) > 0 { + testfiles = append(testfiles, string(testfile)) + } + } + testfiles = append(testfiles, "/proc/self/cgroup") + + tmpdir := c.MkDir() + err = os.MkdirAll(tmpdir+"/proc/self", 0777) + c.Assert(err, IsNil) + fsys := os.DirFS(tmpdir) + + for _, trial := range []struct { + match string // if non-empty, only check testfiles containing this string + subsys string + expect string // empty means "any" (we never actually expect empty string) + }{ + {"debian11", "blkio", "/user.slice/user-1000.slice/session-5424.scope"}, + {"debian12", "cpuacct", "/user.slice/user-1000.slice/session-4.scope"}, + {"debian12", "bogus-does-not-matter", "/user.slice/user-1000.slice/session-4.scope"}, + {"ubuntu1804", "blkio", "/user.slice"}, + {"ubuntu1804", "cpuacct", "/user.slice"}, + {"", "cpu", ""}, + {"", "cpuset", ""}, + {"", "devices", ""}, + {"", "bogus-does-not-matter", ""}, + } { + for _, testfile := range testfiles { + if !strings.Contains(testfile, trial.match) { + continue + } + c.Logf("trial %+v testfile %s", trial, testfile) + + // Copy cgroup file into our fake proc/self/ dir + buf, err := os.ReadFile(testfile) + c.Assert(err, IsNil) + err = os.WriteFile(tmpdir+"/proc/self/cgroup", buf, 0777) + c.Assert(err, IsNil) + + cgroup, err := findCgroup(fsys, trial.subsys) + if !c.Check(err, IsNil) { + continue + } + c.Logf("\tcgroup = %q", cgroup) + c.Check(cgroup, Not(Equals), "") + if trial.expect != "" { + c.Check(cgroup, Equals, trial.expect) + } + } } }