18992: Fix abrupt exit on cgroup test failure.
authorTom Clegg <tom@curii.com>
Thu, 14 Apr 2022 06:32:09 +0000 (02:32 -0400)
committerTom Clegg <tom@curii.com>
Thu, 14 Apr 2022 06:32:09 +0000 (02:32 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

lib/crunchrun/cgroup.go
lib/crunchrun/cgroup_test.go
lib/crunchrun/crunchrun.go

index 0b254f5bd7870dcaa188df80ad9552fa1eda83ed..48ec93b8768c0a117f0af60d8433bb30d8f4467c 100644 (file)
@@ -6,16 +6,16 @@ package crunchrun
 
 import (
        "bytes"
+       "fmt"
        "io/ioutil"
-       "log"
 )
 
 // Return the current process's cgroup for the given subsystem.
-func findCgroup(subsystem string) string {
+func findCgroup(subsystem string) (string, error) {
        subsys := []byte(subsystem)
        cgroups, err := ioutil.ReadFile("/proc/self/cgroup")
        if err != nil {
-               log.Fatal(err)
+               return "", err
        }
        for _, line := range bytes.Split(cgroups, []byte("\n")) {
                toks := bytes.SplitN(line, []byte(":"), 4)
@@ -24,10 +24,9 @@ func findCgroup(subsystem string) string {
                }
                for _, s := range bytes.Split(toks[1], []byte(",")) {
                        if bytes.Compare(s, subsys) == 0 {
-                               return string(toks[2])
+                               return string(toks[2]), nil
                        }
                }
        }
-       log.Fatalf("subsystem %q not found in /proc/self/cgroup", subsystem)
-       return ""
+       return "", fmt.Errorf("subsystem %q not found in /proc/self/cgroup", subsystem)
 }
index b43479a3b4ae02379cff1f60d33c532a510a7f46..eb87456d14b0d1e0e60245460009d75cfe4a01b2 100644 (file)
@@ -14,8 +14,10 @@ var _ = Suite(&CgroupSuite{})
 
 func (s *CgroupSuite) TestFindCgroup(c *C) {
        for _, s := range []string{"devices", "cpu", "cpuset"} {
-               g := findCgroup(s)
-               c.Check(g, Not(Equals), "")
+               g, err := findCgroup(s)
+               if c.Check(err, IsNil) {
+                       c.Check(g, Not(Equals), "", Commentf("subsys %q", s))
+               }
                c.Logf("cgroup(%q) == %q", s, g)
        }
 }
index 84b153554ae5e1a13b6c07024c75f9de95fec703..219ed3b98d3cac6aa171d063f1ef85cd4fbf0b3f 100644 (file)
@@ -1926,7 +1926,11 @@ func (command) RunCommand(prog string, args []string, stdin io.Reader, stdout, s
        cr.enableNetwork = *enableNetwork
        cr.networkMode = *networkMode
        if *cgroupParentSubsystem != "" {
-               p := findCgroup(*cgroupParentSubsystem)
+               p, err := findCgroup(*cgroupParentSubsystem)
+               if err != nil {
+                       log.Printf("fatal: cgroup parent subsystem: %s", err)
+                       return 1
+               }
                cr.setCgroupParent = p
                cr.expectCgroupParent = p
        }