Add more cgroup unit tests

PiperOrigin-RevId: 339380431
This commit is contained in:
Fabricio Voznika
2020-10-27 19:46:51 -07:00
committed by gVisor bot
parent 035b1c8272
commit 93d2d37a93
2 changed files with 90 additions and 1 deletions
+10 -1
View File
@@ -21,6 +21,7 @@ import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
@@ -198,8 +199,13 @@ func LoadPaths(pid string) (map[string]string, error) {
}
defer f.Close()
return loadPathsHelper(f)
}
func loadPathsHelper(cgroup io.Reader) (map[string]string, error) {
paths := make(map[string]string)
scanner := bufio.NewScanner(f)
scanner := bufio.NewScanner(cgroup)
for scanner.Scan() {
// Format: ID:[name=]controller1,controller2:path
// Example: 2:cpu,cpuacct:/user.slice
@@ -207,6 +213,9 @@ func LoadPaths(pid string) (map[string]string, error) {
if len(tokens) != 3 {
return nil, fmt.Errorf("invalid cgroups file, line: %q", scanner.Text())
}
if len(tokens[1]) == 0 {
continue
}
for _, ctrlr := range strings.Split(tokens[1], ",") {
// Remove prefix for cgroups with no controller, eg. systemd.
ctrlr = strings.TrimPrefix(ctrlr, "name=")
+80
View File
@@ -647,3 +647,83 @@ func TestPids(t *testing.T) {
})
}
}
func TestLoadPaths(t *testing.T) {
for _, tc := range []struct {
name string
cgroups string
want map[string]string
err string
}{
{
name: "abs-path",
cgroups: "0:ctr:/path",
want: map[string]string{"ctr": "/path"},
},
{
name: "rel-path",
cgroups: "0:ctr:rel-path",
want: map[string]string{"ctr": "rel-path"},
},
{
name: "non-controller",
cgroups: "0:name=systemd:/path",
want: map[string]string{"systemd": "/path"},
},
{
name: "empty",
},
{
name: "multiple",
cgroups: "0:ctr0:/path0\n" +
"1:ctr1:/path1\n" +
"2::/empty\n",
want: map[string]string{
"ctr0": "/path0",
"ctr1": "/path1",
},
},
{
name: "missing-field",
cgroups: "0:nopath\n",
err: "invalid cgroups file",
},
{
name: "too-many-fields",
cgroups: "0:ctr:/path:extra\n",
err: "invalid cgroups file",
},
{
name: "multiple-malformed",
cgroups: "0:ctr0:/path0\n" +
"1:ctr1:/path1\n" +
"2:\n",
err: "invalid cgroups file",
},
} {
t.Run(tc.name, func(t *testing.T) {
r := strings.NewReader(tc.cgroups)
got, err := loadPathsHelper(r)
if len(tc.err) == 0 {
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
} else if !strings.Contains(err.Error(), tc.err) {
t.Fatalf("Wrong error message, want: *%s*, got: %v", tc.err, err)
}
for key, vWant := range tc.want {
vGot, ok := got[key]
if !ok {
t.Errorf("Missing controller %q", key)
}
if vWant != vGot {
t.Errorf("Wrong controller %q value, want: %q, got: %q", key, vWant, vGot)
}
delete(got, key)
}
for k, v := range got {
t.Errorf("Unexpected controller %q: %q", k, v)
}
})
}
}