Merge branch '21366-subprocess-output-loading-bug' into main. Closes #21366
[arvados.git] / lib / crunchrun / cgroup_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package crunchrun
6
7 import (
8         "bytes"
9         "os"
10         "os/exec"
11         "strings"
12
13         . "gopkg.in/check.v1"
14 )
15
16 type CgroupSuite struct{}
17
18 var _ = Suite(&CgroupSuite{})
19
20 func (s *CgroupSuite) TestFindCgroup(c *C) {
21         var testfiles []string
22         buf, err := exec.Command("find", "../crunchstat/testdata", "-name", "cgroup", "-type", "f").Output()
23         c.Assert(err, IsNil)
24         for _, testfile := range bytes.Split(buf, []byte{'\n'}) {
25                 if len(testfile) > 0 {
26                         testfiles = append(testfiles, string(testfile))
27                 }
28         }
29         testfiles = append(testfiles, "/proc/self/cgroup")
30
31         tmpdir := c.MkDir()
32         err = os.MkdirAll(tmpdir+"/proc/self", 0777)
33         c.Assert(err, IsNil)
34         fsys := os.DirFS(tmpdir)
35
36         for _, trial := range []struct {
37                 match  string // if non-empty, only check testfiles containing this string
38                 subsys string
39                 expect string // empty means "any" (we never actually expect empty string)
40         }{
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"},
46                 {"", "cpu", ""},
47                 {"", "cpuset", ""},
48                 {"", "devices", ""},
49                 {"", "bogus-does-not-matter", ""},
50         } {
51                 for _, testfile := range testfiles {
52                         if !strings.Contains(testfile, trial.match) {
53                                 continue
54                         }
55                         c.Logf("trial %+v testfile %s", trial, testfile)
56
57                         // Copy cgroup file into our fake proc/self/ dir
58                         buf, err := os.ReadFile(testfile)
59                         c.Assert(err, IsNil)
60                         err = os.WriteFile(tmpdir+"/proc/self/cgroup", buf, 0777)
61                         c.Assert(err, IsNil)
62
63                         cgroup, err := findCgroup(fsys, trial.subsys)
64                         if !c.Check(err, IsNil) {
65                                 continue
66                         }
67                         c.Logf("\tcgroup = %q", cgroup)
68                         c.Check(cgroup, Not(Equals), "")
69                         if trial.expect != "" {
70                                 c.Check(cgroup, Equals, trial.expect)
71                         }
72                 }
73         }
74 }