1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 "github.com/sirupsen/logrus"
17 type CgroupSuite struct{}
19 var _ = Suite(&CgroupSuite{})
21 func (s *CgroupSuite) TestFindCgroup(c *C) {
22 var testfiles []string
23 buf, err := exec.Command("find", "../crunchstat/testdata", "-name", "cgroup", "-type", "f").Output()
25 for _, testfile := range bytes.Split(buf, []byte{'\n'}) {
26 if len(testfile) > 0 {
27 testfiles = append(testfiles, string(testfile))
30 testfiles = append(testfiles, "/proc/self/cgroup")
33 err = os.MkdirAll(tmpdir+"/proc/self", 0777)
35 fsys := os.DirFS(tmpdir)
37 for _, trial := range []struct {
38 match string // if non-empty, only check testfiles containing this string
40 expect string // empty means "any" (we never actually expect empty string)
42 {"debian11", "blkio", "/user.slice/user-1000.slice/session-5424.scope"},
43 {"debian12", "cpuacct", "/user.slice/user-1000.slice/session-4.scope"},
44 {"debian12", "bogus-does-not-matter", "/user.slice/user-1000.slice/session-4.scope"},
45 {"ubuntu1804", "blkio", "/user.slice"},
46 {"ubuntu1804", "cpuacct", "/user.slice"},
50 {"", "bogus-does-not-matter", ""},
52 for _, testfile := range testfiles {
53 if !strings.Contains(testfile, trial.match) {
56 c.Logf("trial %+v testfile %s", trial, testfile)
58 // Copy cgroup file into our fake proc/self/ dir
59 buf, err := os.ReadFile(testfile)
61 err = os.WriteFile(tmpdir+"/proc/self/cgroup", buf, 0777)
64 cgroup, err := findCgroup(fsys, trial.subsys)
65 if !c.Check(err, IsNil) {
68 c.Logf("\tcgroup = %q", cgroup)
69 c.Check(cgroup, Not(Equals), "")
70 if trial.expect != "" {
71 c.Check(cgroup, Equals, trial.expect)
77 func (s *CgroupSuite) TestCgroupSupport(c *C) {
78 var logbuf bytes.Buffer
79 logger := logrus.New()
81 checkCgroupSupport(logger.Printf)
82 c.Check(logbuf.String(), Equals, "")
83 c.Check(cgroupSupport, NotNil)
84 c.Check(cgroupSupport["memory"], Equals, true)
85 c.Check(cgroupSupport["entropy"], Equals, false)
88 func (s *CgroupSuite) TestCgroup1Support(c *C) {
90 err := os.MkdirAll(tmpdir+"/proc/self", 0777)
92 err = os.WriteFile(tmpdir+"/proc/self/cgroup", []byte(`12:blkio:/user.slice
95 9:pids:/user.slice/user-1000.slice/session-5.scope
98 6:cpu,cpuacct:/user.slice
100 4:memory:/user.slice/user-1000.slice/session-5.scope
103 1:name=systemd:/user.slice/user-1000.slice/session-5.scope
104 0::/user.slice/user-1000.slice/session-5.scope
107 cgroupSupport = map[string]bool{}
108 ok := checkCgroup1Support(os.DirFS(tmpdir), c.Logf)
109 c.Check(ok, Equals, true)
110 c.Check(cgroupSupport, DeepEquals, map[string]bool{
119 "name=systemd": true,