| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | // Only a successful evaluation's peak trains a project's estimate. One |
| 11 | // that failed or was killed stopped short of its high-water mark, so it |
| 12 | // makes the estimate unknown, and the project's next evaluation counts as |
| 13 | // the whole budget, until one succeeds; then the high-water marks the |
| 14 | // failures did not push out still count. The record survives a new pass. |
| 15 | func TestFailedEvaluationsMakeTheEstimateUnknown(t *testing.T) { |
| 16 | state := t.TempDir() |
| 17 | e, err := openEvaluations(state, 1000, 0) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | must := func(err error) { |
| 22 | t.Helper() |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | } |
| 27 | must(e.record("app", 600, "ok")) |
| 28 | must(e.record("site", 100, "ok")) |
| 29 | for range 5 { |
| 30 | must(e.record("app", 50, "killed")) |
| 31 | } |
| 32 | if got := e.estimate("app"); got != 1000 { |
| 33 | t.Fatalf("after killed evaluations the estimate is %d, want the whole budget", got) |
| 34 | } |
| 35 | if e, err = openEvaluations(state, 1000, 0); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if got := e.estimate("app"); got != 1000 { |
| 39 | t.Fatalf("a new pass estimates %d, want the whole budget", got) |
| 40 | } |
| 41 | must(e.record("app", 0, "ok")) |
| 42 | if got := e.estimate("app"); got != 1000 { |
| 43 | t.Fatalf("an ok evaluation that measured nothing made the estimate %d", got) |
| 44 | } |
| 45 | must(e.record("app", 300, "ok")) |
| 46 | if got := e.estimate("app"); got != 600 { |
| 47 | t.Fatalf("after a successful evaluation the estimate is %d, want the high-water mark 600", got) |
| 48 | } |
| 49 | must(e.record("app", 200, "failed")) |
| 50 | if got := e.estimate("app"); got != 1000 { |
| 51 | t.Fatalf("after a failed evaluation the estimate is %d, want the whole budget", got) |
| 52 | } |
| 53 | if got := e.estimate("site"); got != 100 { |
| 54 | t.Fatalf("another project's estimate is %d, want 100", got) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // The cgroup is found where mountinfo says the cgroup2 hierarchy is |
| 59 | // mounted, under the mount's root, and not by assuming /sys/fs/cgroup. |
| 60 | func TestCgroupDir(t *testing.T) { |
| 61 | const self = "0::/system.slice/valley-worker.service\n" |
| 62 | mount := func(root, point string) string { |
| 63 | return "25 1 0:22 / /proc rw - proc proc rw\n" + |
| 64 | "30 25 0:26 " + root + " " + point + " rw,nosuid shared:4 - cgroup2 cgroup2 rw,nsdelegate\n" |
| 65 | } |
| 66 | for _, c := range []struct { |
| 67 | why, cgroup, mountinfo, dir, top, err string |
| 68 | }{ |
| 69 | {"the usual mount", self, mount("/", "/sys/fs/cgroup"), "/sys/fs/cgroup/system.slice/valley-worker.service", "/sys/fs/cgroup", ""}, |
| 70 | {"another mount point, escaped", self, mount("/", `/run/cg\040two`), "/run/cg two/system.slice/valley-worker.service", "/run/cg two", ""}, |
| 71 | {"a mount of a subtree", self, mount("/system.slice", "/mnt/slice"), "/mnt/slice/valley-worker.service", "/mnt/slice", ""}, |
| 72 | {"a mount of another subtree", self, mount("/system", "/mnt/s"), "", "", "no cgroup2 mount"}, |
| 73 | {"no cgroup2 mount", self, "25 1 0:22 / /proc rw - proc proc rw\n", "", "", "no cgroup2 mount"}, |
| 74 | {"only cgroup v1", "1:memory:/user.slice\n", mount("/", "/sys/fs/cgroup"), "", "", "names no cgroup2 cgroup"}, |
| 75 | {"outside the namespace", "0::/../other\n", mount("/", "/sys/fs/cgroup"), "", "", "names no cgroup2 cgroup"}, |
| 76 | } { |
| 77 | dir, top, _, err := cgroupDir(c.cgroup, c.mountinfo) |
| 78 | if c.err != "" { |
| 79 | if err == nil || !strings.Contains(err.Error(), c.err) { |
| 80 | t.Errorf("%s: err %v, want one saying %q", c.why, err, c.err) |
| 81 | } |
| 82 | continue |
| 83 | } |
| 84 | if err != nil || dir != c.dir || top != c.top { |
| 85 | t.Errorf("%s: %q, %q, %v; want %q, %q", c.why, dir, top, err, c.dir, c.top) |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // The limit is the smallest memory.max from the cgroup up to the mount's |
| 91 | // root, and the host's memory when none is smaller. "max" and a missing |
| 92 | // file are no limit; a memory.max that cannot be read, or says something |
| 93 | // else, means the limit is not known. |
| 94 | func TestCgroupLimit(t *testing.T) { |
| 95 | top := t.TempDir() |
| 96 | dir := filepath.Join(top, "slice", "service") |
| 97 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | write := func(at, text string) { |
| 101 | t.Helper() |
| 102 | if err := os.WriteFile(filepath.Join(at, "memory.max"), []byte(text), 0o644); err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | } |
| 106 | write(dir, "max\n") |
| 107 | write(filepath.Join(top, "slice"), "8000\n") |
| 108 | limit, of, err := cgroupLimit(dir, top, "/slice/service", 10000) |
| 109 | if err != nil || limit != 8000 || of != "the memory.max of the cgroup /slice" { |
| 110 | t.Fatalf("limit %d from %q, %v; want 8000 from /slice", limit, of, err) |
| 111 | } |
| 112 | if limit, _, err = cgroupLimit(dir, top, "/slice/service", 5000); err != nil || limit != 5000 { |
| 113 | t.Fatalf("limit %d, %v; want the host's 5000", limit, err) |
| 114 | } |
| 115 | write(dir, "lots\n") |
| 116 | if _, _, err = cgroupLimit(dir, top, "/slice/service", 10000); err == nil { |
| 117 | t.Fatal("a memory.max that says neither a number nor max gave a limit") |
| 118 | } |
| 119 | os.Remove(filepath.Join(dir, "memory.max")) |
| 120 | if err := os.Mkdir(filepath.Join(dir, "memory.max"), 0o755); err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | if _, _, err = cgroupLimit(dir, top, "/slice/service", 10000); err == nil { |
| 124 | t.Fatal("a memory.max that cannot be read gave a limit") |
| 125 | } |
| 126 | } |