| new file mode 100644 |
| index 000000000..85628dde5 |
| --- /dev/null |
| +++ b/internal/host/size_rootrunner_linux_test.go |
| @@ -0,0 +1,573 @@ |
| +//go:build linux && rootrunner |
| + |
| +package host |
| + |
| +import ( |
| + "encoding/json" |
| + "errors" |
| + "fmt" |
| + "io" |
| + "io/fs" |
| + "net" |
| + "net/http" |
| + "os" |
| + "path/filepath" |
| + "strconv" |
| + "strings" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/cgroup" |
| + "github.com/carlosframework/platform/internal/instancectl" |
| + "github.com/carlosframework/platform/internal/provision" |
| +) |
| + |
| +// Production-wired size witnesses (#329, plan 3 Task 11). Every test here |
| +// drives the real control service (instancectl.Server) over its unix |
| +// socket, on the pre-claim box of the egress witnesses |
| +// (egress_fixtures_rootrunner_linux_test.go): the installed templates, real |
| +// units run by systemd as the carlos user, and the real cgroup v2 files. |
| +// They live in internal/host, not internal/instancectl, because the |
| +// templates (unitsFS), the box and its tenants are this package's. |
| +// |
| +// Network: as the egress witnesses, only their own target namespace (the |
| +// seed and cmd oneshots dial 10.0.5.20 there). |
| + |
| +// sizeRow is one row of the size table (spec §5.1) as `systemctl show` |
| +// prints it, verbatim from the brief: the witnesses pin the table, not |
| +// the code that derives it. |
| +type sizeRow struct { |
| + name string |
| + max, high string |
| + quota, cpuW, ioW string |
| + memoryMax int64 |
| +} |
| + |
| +var ( |
| + sizeSmall = sizeRow{name: "small", max: "268435456", high: "241591905", quota: "500ms", cpuW: "50", ioW: "50", memoryMax: 268435456} |
| + sizeStandard = sizeRow{name: "standard", max: "536870912", high: "483183819", quota: "1s", cpuW: "100", ioW: "100", memoryMax: 536870912} |
| + sizeLarge = sizeRow{name: "large", max: "1073741824", high: "966367638", quota: "2s", cpuW: "100", ioW: "100", memoryMax: 1073741824} |
| + sizeXLarge = sizeRow{name: "xlarge", max: "2147483648", high: "1932735276", quota: "4s", cpuW: "200", ioW: "200", memoryMax: 2147483648} |
| +) |
| + |
| +// sizeHoldMiB is what the allocating tenant holds: more than standard's |
| +// 512 MiB cap, less than large's MemoryHigh (about 921 MiB). |
| +const sizeHoldMiB = 700 |
| + |
| +// The size-tenant payload: a serving tenant on the unix socket named by |
| +// its one argument (never PLAN14_TENANT_SOCKET, which TestMain takes for |
| +// plan14-tenant). GET / answers "running"; ?alloc=N allocates N MiB more, |
| +// touches every page so it is resident, keeps it, and answers the MiB |
| +// held; ?held answers the MiB held. It runs before TestMain. |
| +var ( |
| + sizeTenantMu sync.Mutex |
| + sizeTenantHeld [][]byte |
| +) |
| + |
| +func init() { |
| + if len(os.Args) == 3 && os.Args[1] == "size-tenant" { |
| + sizeTenant(os.Args[2]) |
| + } |
| +} |
| + |
| +func sizeTenant(socket string) { |
| + if st, err := os.Lstat(socket); err == nil { |
| + if st.Mode()&os.ModeSocket == 0 || os.Remove(socket) != nil { |
| + os.Exit(1) |
| + } |
| + } |
| + ln, err := net.Listen("unix", socket) |
| + if err != nil { |
| + fmt.Fprintln(os.Stderr, "size-tenant:", err) |
| + os.Exit(1) |
| + } |
| + held := func() int { |
| + n := 0 |
| + for _, b := range sizeTenantHeld { |
| + n += len(b) >> 20 |
| + } |
| + return n |
| + } |
| + err = http.Serve(ln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + q := r.URL.Query() |
| + switch { |
| + case q.Has("alloc"): |
| + n, err := strconv.Atoi(q.Get("alloc")) |
| + if err != nil || n <= 0 || n > 4096 { |
| + http.Error(w, "bad alloc", http.StatusBadRequest) |
| + return |
| + } |
| + b := make([]byte, n<<20) |
| + page := os.Getpagesize() |
| + for i := 0; i < len(b); i += page { |
| + b[i] = 1 |
| + } |
| + sizeTenantMu.Lock() |
| + sizeTenantHeld = append(sizeTenantHeld, b) |
| + total := held() |
| + sizeTenantMu.Unlock() |
| + fmt.Fprint(w, total) |
| + case q.Has("held"): |
| + sizeTenantMu.Lock() |
| + total := held() |
| + sizeTenantMu.Unlock() |
| + fmt.Fprint(w, total) |
| + default: |
| + w.Write([]byte("running")) |
| + } |
| + })) |
| + fmt.Fprintln(os.Stderr, "size-tenant:", err) |
| + os.Exit(1) |
| +} |
| + |
| +// sizeBox is the egress witnesses' pre-claim box with the control |
| +// service's production ControlDropInDir. |
| +func newSizeBox(t *testing.T) *egressBox { |
| + t.Helper() |
| + return newEgressPreClaimBoxWith(t, egressBoxOptions{controlDropIns: true}) |
| +} |
| + |
| +// sizeAllocTenant makes generation egressGen's launch the size-tenant on |
| +// gen.sock, in place of the box's plan14-tenant. |
| +func sizeAllocTenant(t *testing.T, b *egressBox) { |
| + t.Helper() |
| + sock := filepath.Join(provision.InstanceRunDir(b.host), "gen.sock") |
| + raw, err := json.Marshal(instancectl.Launch{Schema: instancectl.LaunchSchema, Host: b.host, |
| + Argv: []string{filepath.Join(instancectl.LiveDir, b.host), "size-tenant", sock}, Env: []string{}}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(filepath.Join(provision.InstanceDir("/data", b.host), instancectl.GenLaunchFileName(egressGen)), raw, 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| +} |
| + |
| +// sizeTenantGet asks the generation's tenant for query and returns the |
| +// body of its 200. An allocation may take a while to touch its pages. |
| +func sizeTenantGet(t *testing.T, b *egressBox, query string) (string, error) { |
| + t.Helper() |
| + sock := filepath.Join(provision.InstanceRunDir(b.host), "gen.sock") |
| + c := egressTenantClient(sock) |
| + c.Timeout = 60 * time.Second |
| + resp, err := c.Get("http://tenant/?" + query) |
| + if err != nil { |
| + return "", err |
| + } |
| + defer resp.Body.Close() |
| + body, err := io.ReadAll(resp.Body) |
| + if err != nil { |
| + return "", err |
| + } |
| + if resp.StatusCode != http.StatusOK { |
| + return "", fmt.Errorf("%s: %s", resp.Status, body) |
| + } |
| + return strings.TrimSpace(string(body)), nil |
| +} |
| + |
| +// sizeShow reads props of unit from systemd as KEY=value lines. |
| +func sizeShow(t *testing.T, unit string, props ...string) map[string]string { |
| + t.Helper() |
| + args := []string{"show"} |
| + for _, p := range props { |
| + args = append(args, "-p", p) |
| + } |
| + out := egressMustCtl(t, append(args, unit)...) |
| + m := map[string]string{} |
| + for _, line := range strings.Split(out, "\n") { |
| + if k, v, ok := strings.Cut(line, "="); ok { |
| + m[k] = strings.TrimSpace(v) |
| + } |
| + } |
| + return m |
| +} |
| + |
| +// wantSliceRow checks the host slice's limits as systemd shows them |
| +// against the table row. |
| +func wantSliceRow(t *testing.T, step, slice string, row sizeRow) { |
| + t.Helper() |
| + got := sizeShow(t, slice, "MemoryMax", "MemoryHigh", "CPUQuotaPerSecUSec", "CPUWeight", "IOWeight") |
| + want := map[string]string{"MemoryMax": row.max, "MemoryHigh": row.high, "CPUQuotaPerSecUSec": row.quota, "CPUWeight": row.cpuW, "IOWeight": row.ioW} |
| + for k, v := range want { |
| + if got[k] != v { |
| + t.Errorf("%s: %s %s = %q, want %q (%s)", step, slice, k, got[k], v, row.name) |
| + } |
| + } |
| + if t.Failed() { |
| + t.FailNow() |
| + } |
| +} |
| + |
| +// sizeCgroupFile reads a cgroup v2 file of unit's own cgroup. |
| +func sizeCgroupFile(t *testing.T, unit, name string) string { |
| + t.Helper() |
| + cg := egressShow(t, unit, "ControlGroup") |
| + if cg == "" { |
| + t.Fatalf("%s has no control group", unit) |
| + } |
| + raw, err := os.ReadFile(filepath.Join("/sys/fs/cgroup", cg, name)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return strings.TrimSpace(string(raw)) |
| +} |
| + |
| +// sizeOOMKills is memory.events' oom_kill count for unit's cgroup. |
| +func sizeOOMKills(t *testing.T, unit string) string { |
| + t.Helper() |
| + for _, line := range strings.Split(sizeCgroupFile(t, unit, "memory.events"), "\n") { |
| + if v, ok := strings.CutPrefix(line, "oom_kill "); ok { |
| + return v |
| + } |
| + } |
| + t.Fatalf("%s: memory.events has no oom_kill line", unit) |
| + return "" |
| +} |
| + |
| +// wantGenMemory checks the generation unit's MemoryMax from systemd and |
| +// its cgroup's memory.max from the kernel. |
| +func wantGenMemory(t *testing.T, step, unit string, row sizeRow) { |
| + t.Helper() |
| + if got := egressShow(t, unit, "MemoryMax"); got != row.max { |
| + t.Fatalf("%s: %s MemoryMax = %q, want %q (%s)", step, unit, got, row.max, row.name) |
| + } |
| + if got := sizeCgroupFile(t, unit, "memory.max"); got != row.max { |
| + t.Fatalf("%s: %s cgroup memory.max = %q, want %q (%s)", step, unit, got, row.max, row.name) |
| + } |
| +} |
| + |
| +// sizeHolding allocates sizeHoldMiB in the generation's tenant and checks |
| +// the process survived holding it: the unit is active with the same |
| +// invocation and main PID, no OOM kill, the tenant answers holding it, and |
| +// the unit's cgroup has it resident. |
| +func sizeHolding(t *testing.T, b *egressBox, unit string) { |
| + t.Helper() |
| + inv, pid := egressShow(t, unit, "InvocationID"), egressShow(t, unit, "MainPID") |
| + got, err := sizeTenantGet(t, b, "alloc="+strconv.Itoa(sizeHoldMiB)) |
| + if err != nil { |
| + t.Fatalf("allocating %d MiB in %s: %v\nmemory.events: %s\n%s", sizeHoldMiB, unit, err, |
| + sizeCgroupFileOrErr(unit, "memory.events"), egressJournal(unit)) |
| + } |
| + if got != strconv.Itoa(sizeHoldMiB) { |
| + t.Fatalf("the tenant holds %s MiB, want %d", got, sizeHoldMiB) |
| + } |
| + sizeAlive(t, b, unit, "after the allocation", inv, pid) |
| +} |
| + |
| +// sizeAlive checks the generation's tenant is the same live process, |
| +// still holding sizeHoldMiB, with no OOM kill in its cgroup. |
| +func sizeAlive(t *testing.T, b *egressBox, unit, step, inv, pid string) { |
| + t.Helper() |
| + if st := egressShow(t, unit, "ActiveState"); st != "active" { |
| + t.Fatalf("%s: %s is %s\n%s", step, unit, st, egressJournal(unit)) |
| + } |
| + if got := egressShow(t, unit, "InvocationID"); got != inv { |
| + t.Fatalf("%s: %s invocation %s, want %s", step, unit, got, inv) |
| + } |
| + if got := egressShow(t, unit, "MainPID"); got != pid || pid == "0" { |
| + t.Fatalf("%s: %s main PID %s, want %s", step, unit, got, pid) |
| + } |
| + if n := sizeOOMKills(t, unit); n != "0" { |
| + t.Fatalf("%s: %s cgroup oom_kill = %s", step, unit, n) |
| + } |
| + got, err := sizeTenantGet(t, b, "held") |
| + if err != nil || got != strconv.Itoa(sizeHoldMiB) { |
| + t.Fatalf("%s: the tenant holds %q MiB (%v), want %d", step, got, err, sizeHoldMiB) |
| + } |
| + cur, err := strconv.ParseInt(sizeCgroupFile(t, unit, "memory.current"), 10, 64) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if cur < sizeHoldMiB<<20 { |
| + t.Fatalf("%s: %s memory.current = %d, below the %d MiB the tenant holds", step, unit, cur, sizeHoldMiB) |
| + } |
| + t.Logf("%s: %s alive (pid %s), memory.current %d", step, unit, pid, cur) |
| +} |
| + |
| +func sizeCgroupFileOrErr(unit, name string) string { |
| + cg, err := egressCtl("show", "-p", "ControlGroup", "--value", unit) |
| + if err != nil || cg == "" { |
| + return fmt.Sprintf("(no control group: %v)", err) |
| + } |
| + raw, err := os.ReadFile(filepath.Join("/sys/fs/cgroup", cg, name)) |
| + if err != nil { |
| + return err.Error() |
| + } |
| + return string(raw) |
| +} |
| + |
| +// sizeReadBackIs checks POST /size's read-back against the size applied |
| +// and the memory held. |
| +func sizeReadBackIs(t *testing.T, step string, got instancectl.SizeReadBack, shares sizeRow, memory int64, gen bool) { |
| + t.Helper() |
| + s, ok := cgroup.SizeByName(shares.name) |
| + if !ok { |
| + t.Fatal("no size", shares.name) |
| + } |
| + want := instancectl.SizeReadBack{SliceMemoryMax: memory, CPUQuotaPercent: s.Shares.CPUQuotaPercent, CPUWeight: s.Shares.CPUWeight, IOWeight: s.Shares.IOWeight} |
| + if gen { |
| + want.UnitMemoryMax = memory |
| + } |
| + if got != want { |
| + t.Fatalf("%s: read-back %+v, want %+v", step, got, want) |
| + } |
| +} |
| + |
| +// TestSizesThroughEveryStartPathRootRunner: for small and xlarge, each |
| +// legacy start path (run, generation, seed, sidecar, cmd) writes the size |
| +// table's row onto the host slice, as systemd shows it. The sizes |
| +// alternate, so each start's row differs from the one before it: every |
| +// check sees that start's own write. |
| +func TestSizesThroughEveryStartPathRootRunner(t *testing.T) { |
| + b := newSizeBox(t) |
| + ctx := t.Context() |
| + rows := []sizeRow{sizeSmall, sizeXLarge} |
| + |
| + for _, row := range rows { |
| + step := "run " + row.name |
| + unit := instancectl.UnitName(instancectl.RoleRun, b.host) |
| + if _, err := b.client.Start(ctx, b.host, instancectl.RoleRun, row.memoryMax); err != nil { |
| + t.Fatalf("%s: POST /start: %v\n%s", step, err, egressJournal(unit)) |
| + } |
| + b.tenant(t, unit, "run.sock") |
| + wantSliceRow(t, step, b.slice, row) |
| + if err := b.client.Stop(ctx, b.host, instancectl.RoleRun); err != nil { |
| + t.Fatal(step, "stop:", err) |
| + } |
| + } |
| + |
| + for _, row := range rows { |
| + step := "generation " + row.name |
| + unit := instancectl.GenUnitName(b.host, egressGen) |
| + if _, err := b.client.StartGen(ctx, b.host, egressGen, row.memoryMax); err != nil { |
| + t.Fatalf("%s: POST /start gen: %v\n%s", step, err, egressJournal(unit)) |
| + } |
| + b.tenant(t, unit, "gen.sock") |
| + wantSliceRow(t, step, b.slice, row) |
| + wantGenMemory(t, step, unit, row) |
| + if err := b.client.StopGen(ctx, b.host, egressGen, false); err != nil { |
| + t.Fatal(step, "stop:", err) |
| + } |
| + } |
| + |
| + for _, row := range rows { |
| + step := "seed " + row.name |
| + unit := instancectl.UnitName(instancectl.RoleSeed, b.host) |
| + res, err := b.client.Seed(ctx, b.host, row.memoryMax) |
| + if err != nil { |
| + t.Fatalf("%s: POST /seed: %v\n%s", step, err, egressJournal(unit)) |
| + } |
| + if res.Result != "success" || res.Status != "0" { |
| + t.Fatalf("%s: %+v, want success with status 0\n%s", step, res, egressJournal(unit)) |
| + } |
| + wantSliceRow(t, step, b.slice, row) |
| + } |
| + |
| + for _, row := range rows { |
| + step := "sidecar " + row.name |
| + unit := instancectl.UnitName(instancectl.RoleSidecar, b.host) |
| + if _, err := b.client.StartSidecarExe(ctx, b.host, row.memoryMax, b.staged); err != nil { |
| + t.Fatalf("%s: POST /start sidecar: %v\n%s", step, err, egressJournal(unit)) |
| + } |
| + b.tenant(t, unit, "sidecar.sock") |
| + wantSliceRow(t, step, b.slice, row) |
| + if err := b.client.Stop(ctx, b.host, instancectl.RoleSidecar); err != nil { |
| + t.Fatal(step, "stop:", err) |
| + } |
| + } |
| + |
| + for _, row := range rows { |
| + step := "cmd " + row.name |
| + unit := instancectl.UnitName(instancectl.RoleCmd, b.host) |
| + inv, err := b.client.Cmd(ctx, b.host, row.memoryMax) |
| + if err != nil { |
| + t.Fatalf("%s: POST /cmd: %v\n%s", step, err, egressJournal(unit)) |
| + } |
| + wantSliceRow(t, step, b.slice, row) |
| + var st instancectl.CmdStatus |
| + for deadline := time.Now().Add(30 * time.Second); ; { |
| + if st, err = b.client.CmdStatus(ctx, b.host, inv); err != nil { |
| + t.Fatal(step, "GET /cmd:", err) |
| + } |
| + if st.State != instancectl.CmdRunning { |
| + break |
| + } |
| + if time.Now().After(deadline) { |
| + t.Fatalf("%s: still running: %+v", step, st) |
| + } |
| + } |
| + if st.State != instancectl.CmdDone || st.Status != "0" { |
| + t.Fatalf("%s: %+v, want done with status 0\n%s", step, st, egressJournal(unit)) |
| + } |
| + if err := b.client.Stop(ctx, b.host, instancectl.RoleCmd); err != nil { |
| + t.Fatal(step, "stop:", err) |
| + } |
| + } |
| +} |
| + |
| +// TestLiveGrowthReachesTheServingGenerationsOwnCapRootRunner: a |
| +// generation started at standard is grown to large live (POST /size with |
| +// its gen). The unit's own MemoryMax and its cgroup's memory.max read |
| +// 1073741824, and a process in it then allocating 700 MiB, more than |
| +// standard's cap, survives holding it. |
| +func TestLiveGrowthReachesTheServingGenerationsOwnCapRootRunner(t *testing.T) { |
| + b := newSizeBox(t) |
| + ctx := t.Context() |
| + sizeAllocTenant(t, b) |
| + unit := instancectl.GenUnitName(b.host, egressGen) |
| + if _, err := b.client.StartGen(ctx, b.host, egressGen, sizeStandard.memoryMax); err != nil { |
| + t.Fatalf("POST /start gen: %v\n%s", err, egressJournal(unit)) |
| + } |
| + b.tenant(t, unit, "gen.sock") |
| + wantSliceRow(t, "start", b.slice, sizeStandard) |
| + wantGenMemory(t, "start", unit, sizeStandard) |
| + |
| + rb, err := b.client.Size(ctx, b.host, egressGen, "large", sizeLarge.memoryMax) |
| + if err != nil { |
| + t.Fatal("POST /size large:", err) |
| + } |
| + sizeReadBackIs(t, "growth", rb, sizeLarge, sizeLarge.memoryMax, true) |
| + wantSliceRow(t, "growth", b.slice, sizeLarge) |
| + wantGenMemory(t, "growth", unit, sizeLarge) |
| + if got, err := os.ReadFile(filepath.Join("/sys/fs/cgroup", egressShow(t, b.slice, "ControlGroup"), "memory.max")); err != nil || strings.TrimSpace(string(got)) != sizeLarge.max { |
| + t.Fatalf("growth: slice cgroup memory.max = %q (%v), want %s", got, err, sizeLarge.max) |
| + } |
| + sizeHolding(t, b, unit) |
| +} |
| + |
| +// TestMemoryShrinkLeavesARunningProcessAloneRootRunner: a generation at |
| +// large holds 700 MiB. POST /size small with memory_max=1073741824 (memory |
| +// held, CPU and IO shrunk) leaves the process alive; a POST with |
| +// memory_max=268435456 is refused with 409 and writes nothing; after a |
| +// stop, a cold start at small puts the slice at 268435456. |
| +func TestMemoryShrinkLeavesARunningProcessAloneRootRunner(t *testing.T) { |
| + b := newSizeBox(t) |
| + ctx := t.Context() |
| + sizeAllocTenant(t, b) |
| + unit := instancectl.GenUnitName(b.host, egressGen) |
| + if _, err := b.client.StartGen(ctx, b.host, egressGen, sizeLarge.memoryMax); err != nil { |
| + t.Fatalf("POST /start gen: %v\n%s", err, egressJournal(unit)) |
| + } |
| + b.tenant(t, unit, "gen.sock") |
| + wantSliceRow(t, "start", b.slice, sizeLarge) |
| + wantGenMemory(t, "start", unit, sizeLarge) |
| + sizeHolding(t, b, unit) |
| + inv, pid := egressShow(t, unit, "InvocationID"), egressShow(t, unit, "MainPID") |
| + |
| + rb, err := b.client.Size(ctx, b.host, egressGen, "small", sizeLarge.memoryMax) |
| + if err != nil { |
| + t.Fatal("POST /size small, memory held:", err) |
| + } |
| + sizeReadBackIs(t, "shrink, memory held", rb, sizeSmall, sizeLarge.memoryMax, true) |
| + held := sizeRow{name: "small with large's memory", max: sizeLarge.max, high: sizeLarge.high, quota: sizeSmall.quota, cpuW: sizeSmall.cpuW, ioW: sizeSmall.ioW} |
| + wantSliceRow(t, "shrink, memory held", b.slice, held) |
| + wantGenMemory(t, "shrink, memory held", unit, sizeLarge) |
| + sizeAlive(t, b, unit, "shrink, memory held", inv, pid) |
| + |
| + _, err = b.client.Size(ctx, b.host, egressGen, "small", sizeSmall.memoryMax) |
| + if !errors.Is(err, instancectl.ErrSizeRefused) || !strings.Contains(err.Error(), ": 409: ") { |
| + t.Fatalf("POST /size small with memory_max=%d = %v, want a 409 refusal", sizeSmall.memoryMax, err) |
| + } |
| + wantSliceRow(t, "refused shrink", b.slice, held) |
| + wantGenMemory(t, "refused shrink", unit, sizeLarge) |
| + sizeAlive(t, b, unit, "refused shrink", inv, pid) |
| + |
| + if err := b.client.StopGen(ctx, b.host, egressGen, false); err != nil { |
| + t.Fatal("stop gen:", err) |
| + } |
| + // The cold start goes through the run path, so a fault in a reused |
| + // generation number's start (the next witness) cannot mask this one. |
| + run := instancectl.UnitName(instancectl.RoleRun, b.host) |
| + if _, err := b.client.Start(ctx, b.host, instancectl.RoleRun, sizeSmall.memoryMax); err != nil { |
| + t.Fatalf("cold start at small: %v\n%s", err, egressJournal(run)) |
| + } |
| + b.tenant(t, run, "run.sock") |
| + wantSliceRow(t, "cold start", b.slice, sizeSmall) |
| + if err := b.client.Stop(ctx, b.host, instancectl.RoleRun); err != nil { |
| + t.Fatal("stop run:", err) |
| + } |
| +} |
| + |
| +// TestReusedGenerationNumberStartsAtItsOwnSizeRootRunner: generation 1 is |
| +// grown to xlarge live, which leaves a runtime set-property drop-in |
| +// (MemoryMax=2147483648) for its unit, and stopped; generation 1 started |
| +// again at standard runs at 536870912. Twice: once as the stop leaves it |
| +// (StopGen removes the drop-in), and once with that drop-in put back and |
| +// loaded by systemd, as a stop that could not remove it would leave it: |
| +// the start itself must clear it (Task 5, ruling C5 (d)). |
| +func TestReusedGenerationNumberStartsAtItsOwnSizeRootRunner(t *testing.T) { |
| + b := newSizeBox(t) |
| + ctx := t.Context() |
| + unit := instancectl.GenUnitName(b.host, egressGen) |
| + control := filepath.Join(instancectl.RuntimeControlDir, unit+".d") |
| + |
| + grow := func(step string) map[string][]byte { |
| + t.Helper() |
| + if _, err := b.client.StartGen(ctx, b.host, egressGen, sizeStandard.memoryMax); err != nil { |
| + t.Fatalf("%s: POST /start gen: %v\n%s", step, err, egressJournal(unit)) |
| + } |
| + b.tenant(t, unit, "gen.sock") |
| + wantGenMemory(t, step+": start", unit, sizeStandard) |
| + rb, err := b.client.Size(ctx, b.host, egressGen, "xlarge", sizeXLarge.memoryMax) |
| + if err != nil { |
| + t.Fatal(step, "POST /size xlarge:", err) |
| + } |
| + sizeReadBackIs(t, step+": growth", rb, sizeXLarge, sizeXLarge.memoryMax, true) |
| + wantGenMemory(t, step+": growth", unit, sizeXLarge) |
| + // What systemd wrote for the unit's live growth. |
| + files := map[string][]byte{} |
| + entries, err := os.ReadDir(control) |
| + if err != nil { |
| + t.Fatalf("%s: the live growth left no control drop-in for %s: %v", step, unit, err) |
| + } |
| + var stale bool |
| + for _, e := range entries { |
| + raw, err := os.ReadFile(filepath.Join(control, e.Name())) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + files[e.Name()] = raw |
| + stale = stale || strings.Contains(string(raw), "MemoryMax="+sizeXLarge.max) |
| + } |
| + if !stale { |
| + t.Fatalf("%s: %s holds no MemoryMax=%s: %v", step, control, sizeXLarge.max, files) |
| + } |
| + if err := b.client.StopGen(ctx, b.host, egressGen, false); err != nil { |
| + t.Fatal(step, "stop gen:", err) |
| + } |
| + if _, err := os.Lstat(control); !errors.Is(err, fs.ErrNotExist) { |
| + t.Fatalf("%s: %s left behind by the stop: %v", step, control, err) |
| + } |
| + return files |
| + } |
| + reuse := func(step string) { |
| + t.Helper() |
| + if _, err := b.client.StartGen(ctx, b.host, egressGen, sizeStandard.memoryMax); err != nil { |
| + t.Fatalf("%s: POST /start gen (reused): %v\n%s", step, err, egressJournal(unit)) |
| + } |
| + b.tenant(t, unit, "gen.sock") |
| + wantGenMemory(t, step, unit, sizeStandard) |
| + wantSliceRow(t, step, b.slice, sizeStandard) |
| + if err := b.client.StopGen(ctx, b.host, egressGen, false); err != nil { |
| + t.Fatal(step, "stop gen:", err) |
| + } |
| + } |
| + |
| + grow("first") |
| + reuse("reused after a clean stop") |
| + |
| + files := grow("second") |
| + if err := os.Mkdir(control, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + for name, raw := range files { |
| + if err := os.WriteFile(filepath.Join(control, name), raw, 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + egressMustCtl(t, "daemon-reload") |
| + if got := egressShow(t, unit, "MemoryMax"); got != sizeXLarge.max { |
| + t.Fatalf("the stale drop-in put back is not in force: %s MemoryMax = %q, want %s", unit, got, sizeXLarge.max) |
| + } |
| + reuse("reused over a stale drop-in") |
| +} |