carlos / platform Public

instancectl: a revoke's restart finds the slice's running services in cgroupfs, and never reports done on finding none (#328)

Run 20261025T165624-7042d3: the restart fallback restarted nothing on real
systemd. `systemctl show -p ConsistsOf <slice>` does not list a slice's
services (membership is each service's Slice=), so the list came back
empty and closeByRestart reported success with the address still open.

- What runs in the slice is now read from cgroupfs, as the sweep already
  reads it (sliceRunningServices, linux, with a !linux stub): the slice's
  cgroup.events, then each child cgroup named *.service whose own
  cgroup.events says "populated 1" (one leading "_", systemd's cgroup
  escape, dropped). A slice with no cgroup, or not populated, has nothing
  to restart (a start path's reset before its start).
- No silent success: a populated slice in which no service is found, an
  unreadable cgroup root, or no root configured, is an error; the report
  is failing and the restart stays due for the next Assert.
- The fakes answer as real systemd does: egressCtl no longer answers
  ConsistsOf, and the decision tests (now egress_restart_linux_test.go)
  build a fixture cgroup tree; the host sweep tests' fixture slices get the
  unit cgroup their processes live in.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev ded0af2541ecf8d961688569c80bd4228b442607 parent 36bdc1b
7 files changed, +312 −178
  • internal/host/egress_claim_wiring_test.go +0 −9
  • internal/host/egress_source_sweep_linux_test.go +18 −14
  • internal/instancectl/egress.go +40 −32
  • internal/instancectl/egress_cgroup_linux.go +46 −0
  • internal/instancectl/egress_cgroup_other.go +5 −0
  • internal/instancectl/egress_restart_linux_test.go +187 −0
  • internal/instancectl/egress_test.go +16 −123
diff --git a/internal/host/egress_claim_wiring_test.go b/internal/host/egress_claim_wiring_test.go
index 4696172af..86b061fc2 100644
--- a/internal/host/egress_claim_wiring_test.go
+++ b/internal/host/egress_claim_wiring_test.go
@@ -70,15 +70,6 @@ func (c *claimEgressCtl) run(_ context.Context, args ...string) (string, error)
switch {
case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "IPAddressAllow" && args[3] == "--value":
return c.allow[args[4]], nil
- case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "ConsistsOf" && args[3] == "--value":
- // The slice's one member: the ordinary run unit (closeByRestart
- // lists it after a revoke empties the slice with no parent allow).
- return instancectl.UnitName(instancectl.RoleRun, claimEgressHost) + "\n", nil
- case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "ActiveState" && args[3] == "--value":
- if c.active {
- return "active\n", nil
- }
- return "inactive\n", nil
case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[4] == "--all":
var out strings.Builder
for _, key := range strings.Split(args[2], ",") {
diff --git a/internal/host/egress_source_sweep_linux_test.go b/internal/host/egress_source_sweep_linux_test.go
index 02dd7e790..082ccc6d6 100644
--- a/internal/host/egress_source_sweep_linux_test.go
+++ b/internal/host/egress_source_sweep_linux_test.go
@@ -52,6 +52,22 @@ func (c *sliceCtl) run(_ context.Context, args ...string) (string, error) {
return "", nil
}
+// populatedHostCgroup lays out, under a fixture cgroup root, host's slice
+// running its run unit, as cgroupfs shows it: processes live in the unit's
+// cgroup, so both read "populated 1".
+func populatedHostCgroup(t *testing.T, root, host string) {
+ t.Helper()
+ slice := filepath.Join(root, "carlos.slice", "carlos-instances.slice", instancectl.SliceName(host))
+ for _, dir := range []string{slice, filepath.Join(slice, instancectl.UnitName(instancectl.RoleRun, host))} {
+ if err := os.MkdirAll(dir, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(dir, "cgroup.events"), []byte("populated 1\nfrozen 0\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ }
+}
+
// held is what host's slice holds.
func (c *sliceCtl) held(host string) string {
c.mu.Lock()
@@ -73,13 +89,7 @@ func TestRegistryEgressSourceReadsOncePerSweep(t *testing.T) {
for _, h := range hosts {
upsertRows(t, reg, nestedRow(dd, h))
setAllow(t, reg, h, "10.0.5.20", unixText(clock.Now().Add(time.Hour)))
- dir := filepath.Join(cgroup, "carlos.slice", "carlos-instances.slice", instancectl.SliceName(h))
- if err := os.MkdirAll(dir, 0o755); err != nil {
- t.Fatal(err)
- }
- if err := os.WriteFile(filepath.Join(dir, "cgroup.events"), []byte("populated 1\nfrozen 0\n"), 0o644); err != nil {
- t.Fatal(err)
- }
+ populatedHostCgroup(t, cgroup, h)
}
upsertRows(t, reg, registry.Route{Host: "console.example.com", Kind: "instance", App: "console", Addr: "127.0.0.1:8090",
Backing: registry.BackingUnit, Unit: "console.service"})
@@ -147,13 +157,7 @@ func TestRegistryEgressSweepCannotReopenARevoke(t *testing.T) {
for _, h := range []string{a, b} {
upsertRows(t, reg, nestedRow(dd, h))
setAllow(t, reg, h, "10.0.5.20", unixText(clock.Now().Add(time.Hour)))
- dir := filepath.Join(cgroup, "carlos.slice", "carlos-instances.slice", instancectl.SliceName(h))
- if err := os.MkdirAll(dir, 0o755); err != nil {
- t.Fatal(err)
- }
- if err := os.WriteFile(filepath.Join(dir, "cgroup.events"), []byte("populated 1\nfrozen 0\n"), 0o644); err != nil {
- t.Fatal(err)
- }
+ populatedHostCgroup(t, cgroup, h)
}
src := registryEgressSource(dd, clock.Now)
ctl := &sliceCtl{allow: map[string]string{}, paused: make(chan struct{}), resume: make(chan struct{})}
diff --git a/internal/instancectl/egress.go b/internal/instancectl/egress.go
index 9edecbba9..43461e1db 100644
--- a/internal/instancectl/egress.go
+++ b/internal/instancectl/egress.go
@@ -243,12 +243,20 @@ func (e *EgressAsserter) Assert(ctx context.Context, host string) error {
// closeByRestart finishes a revoke that emptied slice. With an allow on
// carlos-instances.slice the revoke is live (Task 1's kernel proof) and
// nothing more is done. With none, systemd may leave a running unit's
-// filter as it was (run 20261025T164418-7a14fe), so each unit of the slice
-// that is running is restarted with try-restart, and the report's Detail
-// says so. A parent that cannot be read counts as holding nothing: a
-// restart is the safe side. The unit names are the slice's members as
-// systemd lists them; nothing from a request reaches systemctl. A failure
-// keeps the restart due for the next Assert of the slice.
+// filter as it was (run 20261025T164418-7a14fe), so each service running
+// in the slice is restarted with try-restart, and the report's Detail says
+// so. A parent that cannot be read counts as holding nothing: a restart is
+// the safe side.
+//
+// What runs in the slice is read from cgroupfs (sliceRunningServices):
+// systemd's ConsistsOf does not list a slice's services (run
+// 20261025T165624-7042d3, where it found none and nothing restarted). The
+// unit names are the slice's child cgroups as the kernel lists them;
+// nothing from a request reaches systemctl. A populated slice in which no
+// service can be found, or a restart that leaves the old invocation
+// running, is an error and keeps the restart due for the next Assert of
+// the slice: a revoke is never reported done while something may still
+// run under the old filter.
func (e *EgressAsserter) closeByRestart(ctx context.Context, slice string, rep *EgressReport) error {
parent, err := e.readBack(ctx, InstancesSlice)
if err == nil && !parent.equal(nil) {
@@ -258,26 +266,30 @@ func (e *EgressAsserter) closeByRestart(ctx context.Context, slice string, rep *
if err != nil {
e.logf("egress: %s: %s: read back: %v; restarting the slice's units to close its revoke", slice, InstancesSlice, err)
}
- out, err := e.Systemctl(ctx, "show", "-p", "ConsistsOf", "--value", slice)
+ keepDue := func(err error) error {
+ e.setRestartDue(slice, true)
+ return err
+ }
+ if e.CgroupRoot == "" {
+ return keepDue(errors.New("revoked, but with no cgroup root the slice's running units cannot be found to restart them"))
+ }
+ populated, units, err := sliceRunningServices(e.CgroupRoot, slice)
if err != nil {
- return fmt.Errorf("revoked, but the slice's units could not be listed to restart them: %s: %v", strings.TrimSpace(out), err)
+ return keepDue(fmt.Errorf("revoked, but the slice's running units could not be read to restart them: %v", err))
+ }
+ if !populated {
+ e.setRestartDue(slice, false)
+ return nil // nothing runs under the old filter
+ }
+ if len(units) == 0 {
+ return keepDue(errors.New("revoked, but the slice runs something that is not a service, so it cannot be restarted"))
}
var failed []string
- restarted := 0
- for _, unit := range strings.Fields(out) {
- if !strings.HasSuffix(unit, ".service") || strings.HasPrefix(unit, "-") {
- continue
- }
- state, err := e.Systemctl(ctx, "show", "-p", "ActiveState", "--value", unit)
- if err != nil {
- failed = append(failed, fmt.Sprintf("%s: state: %v", unit, err))
+ for _, unit := range units {
+ if strings.HasPrefix(unit, "-") {
+ failed = append(failed, fmt.Sprintf("%q: not a unit name", unit))
continue
}
- switch strings.TrimSpace(state) {
- case "active", "activating", "reloading", "refreshing", "deactivating":
- default:
- continue // nothing of it runs under the old filter
- }
inv, _ := e.Systemctl(ctx, "show", "-p", "InvocationID", "--value", unit)
if out, err := e.Systemctl(ctx, "try-restart", unit); err != nil {
// A restart whose start fails (a claim unit's start without a
@@ -291,22 +303,18 @@ func (e *EgressAsserter) closeByRestart(ctx context.Context, slice string, rep *
continue
}
e.logf("egress: %s: restarted %s to close its revoke; its start failed: %s: %v", slice, unit, strings.TrimSpace(out), err)
- } else {
- e.logf("egress: %s: restarted %s to close its revoke (no allow on %s)", slice, unit, InstancesSlice)
+ continue
}
- restarted++
+ e.logf("egress: %s: restarted %s to close its revoke (no allow on %s)", slice, unit, InstancesSlice)
}
if len(failed) > 0 {
- e.setRestartDue(slice, true)
- return fmt.Errorf("revoked, but restarting the slice's units failed: %s", strings.Join(failed, "; "))
+ return keepDue(fmt.Errorf("revoked, but restarting the slice's units failed: %s", strings.Join(failed, "; ")))
}
e.setRestartDue(slice, false)
- if restarted > 0 {
- if rep.Detail == "" {
- rep.Detail = egressRestarted
- } else {
- rep.Detail += "; " + egressRestarted
- }
+ if rep.Detail == "" {
+ rep.Detail = egressRestarted
+ } else {
+ rep.Detail += "; " + egressRestarted
}
return nil
}
diff --git a/internal/instancectl/egress_cgroup_linux.go b/internal/instancectl/egress_cgroup_linux.go
index ebd13a7e2..a68a065ae 100644
--- a/internal/instancectl/egress_cgroup_linux.go
+++ b/internal/instancectl/egress_cgroup_linux.go
@@ -42,3 +42,49 @@ func populatedHostSlices(cgroupRoot string) ([]string, error) {
}
return out, nil
}
+
+// sliceRunningServices is what runs in one host slice now, as the kernel
+// sees it: whether the slice's cgroup.events says "populated 1", and the
+// services among its child cgroups whose own cgroup.events says so. A unit
+// in a slice is a child cgroup named for it (systemd prefixes "_" to a
+// name that would clash with a cgroupfs file, so one leading "_" is
+// dropped); systemd's own dependency lists do not carry slice membership.
+// A slice with no cgroup is not populated. The root itself must exist, so
+// a wrong root is an error, never "nothing runs".
+func sliceRunningServices(cgroupRoot, slice string) (populated bool, units []string, err error) {
+ if _, err := os.Stat(cgroupRoot); err != nil {
+ return false, nil, err
+ }
+ dir := filepath.Join(cgroupRoot, parentSlices, slice)
+ b, err := os.ReadFile(filepath.Join(dir, "cgroup.events"))
+ if errors.Is(err, os.ErrNotExist) {
+ return false, nil, nil
+ }
+ if err != nil {
+ return false, nil, err
+ }
+ if !slices.Contains(strings.Split(string(b), "\n"), "populated 1") {
+ return false, nil, nil
+ }
+ entries, err := os.ReadDir(dir)
+ if err != nil {
+ return true, nil, err
+ }
+ for _, ent := range entries {
+ name := ent.Name()
+ if !ent.IsDir() || !strings.HasSuffix(name, ".service") {
+ continue
+ }
+ ev, err := os.ReadFile(filepath.Join(dir, name, "cgroup.events"))
+ if errors.Is(err, os.ErrNotExist) {
+ continue // it emptied and went between the list and the read
+ }
+ if err != nil {
+ return true, nil, fmt.Errorf("%s: %w", name, err)
+ }
+ if slices.Contains(strings.Split(string(ev), "\n"), "populated 1") {
+ units = append(units, strings.TrimPrefix(name, "_"))
+ }
+ }
+ return true, units, nil
+}
diff --git a/internal/instancectl/egress_cgroup_other.go b/internal/instancectl/egress_cgroup_other.go
index b744eb93f..92779d013 100644
--- a/internal/instancectl/egress_cgroup_other.go
+++ b/internal/instancectl/egress_cgroup_other.go
@@ -9,3 +9,8 @@ import "errors"
func populatedHostSlices(string) ([]string, error) {
return nil, errors.New("per-host slices are a linux cgroup feature")
}
+
+// sliceRunningServices reads cgroupfs, which only linux has.
+func sliceRunningServices(string, string) (bool, []string, error) {
+ return false, nil, errors.New("per-host slices are a linux cgroup feature")
+}
diff --git a/internal/instancectl/egress_restart_linux_test.go b/internal/instancectl/egress_restart_linux_test.go
new file mode 100644
index 000000000..018bfe8e5
--- /dev/null
+++ b/internal/instancectl/egress_restart_linux_test.go
@@ -0,0 +1,187 @@
+//go:build linux
+
+package instancectl
+
+// The revoke-by-restart ruling's tests (run 20261025T164418-7a14fe): what
+// runs in a slice comes from a fixture cgroup tree laid out as cgroupfs is
+// (the linux sliceRunningServices reads it), never from systemd's
+// dependency lists, which on a real box do not list a slice's services
+// (run 20261025T165624-7042d3).
+
+import (
+ "context"
+ "os"
+ "path/filepath"
+ "slices"
+ "strings"
+ "testing"
+)
+
+// cgroupService writes a child cgroup of a host slice, as systemd names a
+// unit's: <root>/carlos.slice/carlos-instances.slice/<slice>/<name>.
+func cgroupService(t *testing.T, root, slice, name string, populated bool) {
+ t.Helper()
+ dir := filepath.Join(root, "carlos.slice", "carlos-instances.slice", slice, name)
+ if err := os.MkdirAll(dir, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ ev := "populated 0\nfrozen 0\n"
+ if populated {
+ ev = "populated 1\nfrozen 0\n"
+ }
+ if err := os.WriteFile(filepath.Join(dir, "cgroup.events"), []byte(ev), 0o644); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// TestEgressRevokeToEmptyRestartsWithoutAParentAllow pins the ruling: a
+// revoke that empties a slice while carlos-instances.slice holds no allow
+// restarts the services running in the slice; every other revoke is live;
+// and a revoke is never reported done while something may still run
+// under the old filter.
+func TestEgressRevokeToEmptyRestartsWithoutAParentAllow(t *testing.T) {
+ slice := SliceName(egressHost)
+ run, seed := UnitName(RoleRun, egressHost), UnitName(RoleSeed, egressHost)
+ restarts := func(ctl *egressCtl) []string {
+ var out []string
+ for _, c := range ctl.takeCalls() {
+ if strings.HasPrefix(c, "try-restart ") || c == "show -p IPAddressAllow --value "+InstancesSlice {
+ out = append(out, c)
+ }
+ }
+ return out
+ }
+ // box: the slice holds held, the parent holds parent, and in the
+ // cgroup tree the slice runs the run unit (populated), the seed unit's
+ // cgroup is left empty, and a scope runs beside them.
+ box := func(t *testing.T, held, parent string) (*egressCtl, string) {
+ root := t.TempDir()
+ cgroupSlice(t, root, slice, true)
+ cgroupService(t, root, slice, run, true)
+ cgroupService(t, root, slice, seed, false)
+ cgroupService(t, root, slice, "init.scope", true) // not a .service: skipped
+ ctl := newEgressCtl()
+ ctl.setHeld(slice, held)
+ ctl.setHeld(InstancesSlice, parent)
+ ctl.active[run] = "active"
+ return ctl, root
+ }
+ asserter := func(ctl *egressCtl, root string, p EgressPolicy) *EgressAsserter {
+ e := newEgressAsserter(ctl, staticSource(p, nil))
+ e.CgroupRoot = root
+ return e
+ }
+
+ t.Run("parent empty and target empty: running services restart", func(t *testing.T) {
+ ctl, root := box(t, "10.0.5.20/32", "")
+ rep := mustAssert(t, asserter(ctl, root, EgressPolicy{}), egressHost)
+ wantCalls(t, restarts(ctl), "show -p IPAddressAllow --value "+InstancesSlice, "try-restart "+run)
+ if rep.State != "" || rep.Detail != egressRestarted || len(rep.Allowed) != 0 {
+ t.Fatalf("report = %+v, want no state and detail %q", rep, egressRestarted)
+ }
+ })
+ t.Run("a unit cgroup systemd prefixed with _ is restarted by its unit name", func(t *testing.T) {
+ ctl, root := box(t, "10.0.5.20/32", "")
+ cgroupService(t, root, slice, run, false)
+ cgroupService(t, root, slice, "_"+seed, true)
+ mustAssert(t, asserter(ctl, root, EgressPolicy{}), egressHost)
+ wantCalls(t, restarts(ctl), "show -p IPAddressAllow --value "+InstancesSlice, "try-restart "+seed)
+ })
+ t.Run("parent holds an allow: the revoke is live, nothing restarts", func(t *testing.T) {
+ ctl, root := box(t, "10.0.5.20/32", "10.0.5.53/32")
+ rep := mustAssert(t, asserter(ctl, root, EgressPolicy{}), egressHost)
+ wantCalls(t, restarts(ctl), "show -p IPAddressAllow --value "+InstancesSlice)
+ if rep.Detail != "" {
+ t.Fatalf("report = %+v, want no detail", rep)
+ }
+ })
+ t.Run("target not empty: the narrowing is live, nothing restarts", func(t *testing.T) {
+ ctl, root := box(t, "10.0.5.20/32 10.0.5.21/32", "")
+ rep := mustAssert(t, asserter(ctl, root, EgressPolicy{Allow: []string{"10.0.5.21"}}), egressHost)
+ wantCalls(t, restarts(ctl))
+ if rep.Detail != "" || !slices.Equal(rep.Allowed, []string{"10.0.5.21"}) {
+ t.Fatalf("report = %+v", rep)
+ }
+ })
+ t.Run("nothing held, nothing wanted: no write, nothing restarts", func(t *testing.T) {
+ ctl, root := box(t, "", "")
+ mustAssert(t, asserter(ctl, root, EgressPolicy{}), egressHost)
+ wantCalls(t, ctl.takeCalls(), showCall(egressHost))
+ })
+ t.Run("nothing runs in the slice (a start path's reset): nothing to restart", func(t *testing.T) {
+ ctl := newEgressCtl()
+ ctl.setHeld(slice, "10.0.5.20/32")
+ rep := mustAssert(t, asserter(ctl, t.TempDir(), EgressPolicy{}), egressHost)
+ wantCalls(t, restarts(ctl), "show -p IPAddressAllow --value "+InstancesSlice)
+ if rep.State != "" || rep.Detail != "" {
+ t.Fatalf("report = %+v, want no state and no detail", rep)
+ }
+ })
+ t.Run("under claims the reset of a stale allow restarts too, the detail kept", func(t *testing.T) {
+ ctl, root := box(t, "10.0.5.20/32", "")
+ rep := mustAssert(t, asserter(ctl, root, EgressPolicy{Closed: EgressUnderClaims, Detail: "not yet supported under claims"}), egressHost)
+ wantCalls(t, restarts(ctl), "show -p IPAddressAllow --value "+InstancesSlice, "try-restart "+run)
+ if rep.State != EgressUnderClaims || rep.Detail != "not yet supported under claims; "+egressRestarted {
+ t.Fatalf("report = %+v", rep)
+ }
+ })
+ t.Run("a restart whose start fails still ended the old invocation", func(t *testing.T) {
+ ctl, root := box(t, "10.0.5.20/32", "")
+ ctl.restartFail[run] = true
+ ctl.active[run] = "failed" // the unit after its failed start
+ rep := mustAssert(t, asserter(ctl, root, EgressPolicy{}), egressHost)
+ if rep.Detail != egressRestarted {
+ t.Fatalf("report = %+v, want detail %q", rep, egressRestarted)
+ }
+ })
+ t.Run("a restart that does not happen stays due and is retried", func(t *testing.T) {
+ ctl, root := box(t, "10.0.5.20/32", "")
+ ctl.restartFail[run] = true
+ e := asserter(ctl, root, EgressPolicy{})
+ if err := e.Assert(context.Background(), egressHost); err == nil {
+ t.Fatal("Assert succeeded though the running unit kept its invocation")
+ }
+ if rep := e.Reports()[egressHost]; rep.State != EgressFailing || !strings.Contains(rep.Detail, "try-restart") {
+ t.Fatalf("report = %+v, want failing naming the try-restart", rep)
+ }
+ ctl.takeCalls()
+ ctl.restartFail[run] = false
+ rep := mustAssert(t, e, egressHost) // the slice already reads back empty
+ if rep.State != "" || rep.Detail != egressRestarted {
+ t.Fatalf("retry: report = %+v, want detail %q", rep, egressRestarted)
+ }
+ if !slices.Contains(ctl.takeCalls(), "try-restart "+run) {
+ t.Fatal("retry did not restart the unit")
+ }
+ mustAssert(t, e, egressHost)
+ wantCalls(t, ctl.takeCalls(), showCall(egressHost))
+ })
+ t.Run("a populated slice with no service in it is not reported done", func(t *testing.T) {
+ root := t.TempDir()
+ cgroupSlice(t, root, slice, true)
+ cgroupService(t, root, slice, "init.scope", true)
+ ctl := newEgressCtl()
+ ctl.setHeld(slice, "10.0.5.20/32")
+ e := asserter(ctl, root, EgressPolicy{})
+ if err := e.Assert(context.Background(), egressHost); err == nil {
+ t.Fatal("Assert reported a revoke done with something running under the old filter")
+ }
+ if rep := e.Reports()[egressHost]; rep.State != EgressFailing || !strings.Contains(rep.Detail, "not a service") {
+ t.Fatalf("report = %+v, want failing", rep)
+ }
+ if !e.isRestartDue(slice) {
+ t.Fatal("the restart is not kept due")
+ }
+ })
+ t.Run("an unreadable cgroup root is not reported done", func(t *testing.T) {
+ ctl := newEgressCtl()
+ ctl.setHeld(slice, "10.0.5.20/32")
+ e := asserter(ctl, filepath.Join(t.TempDir(), "absent"), EgressPolicy{})
+ if err := e.Assert(context.Background(), egressHost); err == nil {
+ t.Fatal("Assert reported a revoke done without reading what runs in the slice")
+ }
+ if rep := e.Reports()[egressHost]; rep.State != EgressFailing {
+ t.Fatalf("report = %+v, want failing", rep)
+ }
+ })
+}
diff --git a/internal/instancectl/egress_test.go b/internal/instancectl/egress_test.go
index 752293d82..447ff0e66 100644
--- a/internal/instancectl/egress_test.go
+++ b/internal/instancectl/egress_test.go
@@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/netip"
+ "os"
"reflect"
"slices"
"strconv"
@@ -28,17 +29,18 @@ type egressCtl struct {
calls []string
held map[string]string // slice -> show's answer
ignore bool // accept set-property but change nothing
- // For a revoke closed by restart (closeByRestart): a slice's members,
- // each unit's ActiveState and InvocationID (a try-restart moves it on),
- // and units whose try-restart fails.
- members map[string]string
+ // For a revoke closed by restart (closeByRestart): each unit's
+ // ActiveState and InvocationID (a try-restart moves it on), and units
+ // whose try-restart fails. What runs in a slice is the fixture cgroup
+ // tree's to say, as on a real box; systemd's ConsistsOf never lists a
+ // slice's services, so this fake does not answer it either.
active map[string]string
invocations map[string]int
restartFail map[string]bool
}
func newEgressCtl() *egressCtl {
- return &egressCtl{held: map[string]string{}, members: map[string]string{}, active: map[string]string{}, invocations: map[string]int{}, restartFail: map[string]bool{}}
+ return &egressCtl{held: map[string]string{}, active: map[string]string{}, invocations: map[string]int{}, restartFail: map[string]bool{}}
}
func (f *egressCtl) run(_ context.Context, args ...string) (string, error) {
@@ -48,8 +50,6 @@ func (f *egressCtl) run(_ context.Context, args ...string) (string, error) {
switch {
case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "IPAddressAllow" && args[3] == "--value":
return f.held[args[4]] + "\n", nil
- case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "ConsistsOf" && args[3] == "--value":
- return f.members[args[4]] + "\n", nil
case len(args) == 5 && args[0] == "show" && args[1] == "-p" && args[2] == "ActiveState" && args[3] == "--value":
if st, ok := f.active[args[4]]; ok {
return st + "\n", nil
@@ -117,17 +117,21 @@ func newEgressAsserter(ctl *egressCtl, src EgressSource) *EgressAsserter {
Systemctl: ctl.run,
Source: src,
LocalAddrs: func() ([]netip.Prefix, error) { return []netip.Prefix{netip.MustParsePrefix("127.0.0.0/8")}, nil },
+ // A root that exists and holds no carlos.slice: no host slice is
+ // populated. Tests of what runs set their own fixture tree.
+ CgroupRoot: os.TempDir(),
Logf: func(string, ...any) {},
}
}
func showCall(host string) string { return "show -p IPAddressAllow --value " + SliceName(host) }
-// emptiedCalls are closeByRestart's reads after a revoke empties slice
-// while carlos-instances.slice holds nothing: the parent's allow, then the
-// slice's members (none running here, so nothing is restarted).
-func emptiedCalls(slice string) []string {
- return []string{"show -p IPAddressAllow --value " + InstancesSlice, "show -p ConsistsOf --value " + slice}
+// emptiedCalls are closeByRestart's systemctl calls after a revoke
+// empties a slice while carlos-instances.slice holds nothing: the parent's
+// allow. Nothing runs in the slice here (its cgroup is absent under the
+// asserter's root), so nothing is restarted.
+func emptiedCalls(string) []string {
+ return []string{"show -p IPAddressAllow --value " + InstancesSlice}
}
func setCall(host string, list ...string) string {
@@ -656,114 +660,3 @@ func TestEgressRoutesServedWithClaimsOn(t *testing.T) {
t.Fatalf("the legacy start reached systemctl: %s", ctl.joined())
}
}
-
-// TestEgressRevokeToEmptyRestartsWithoutAParentAllow pins the ruling on
-// run 20261025T164418-7a14fe: a revoke that empties a slice while
-// carlos-instances.slice holds no allow restarts the slice's running
-// units; every other revoke stays live.
-func TestEgressRevokeToEmptyRestartsWithoutAParentAllow(t *testing.T) {
- slice := SliceName(egressHost)
- run, seed := UnitName(RoleRun, egressHost), UnitName(RoleSeed, egressHost)
- restarts := func(ctl *egressCtl) []string {
- var out []string
- for _, c := range ctl.takeCalls() {
- if strings.HasPrefix(c, "try-restart ") || strings.HasPrefix(c, "show -p ConsistsOf ") || c == "show -p IPAddressAllow --value "+InstancesSlice {
- out = append(out, c)
- }
- }
- return out
- }
- box := func(held, parent string) *egressCtl {
- ctl := newEgressCtl()
- ctl.setHeld(slice, held)
- ctl.setHeld(InstancesSlice, parent)
- ctl.members[slice] = run + " " + seed + " carlos-instances-x.slice"
- ctl.active[run] = "active"
- ctl.active[seed] = "inactive"
- return ctl
- }
-
- t.Run("parent empty and target empty: running units restart", func(t *testing.T) {
- ctl := box("10.0.5.20/32", "")
- rep := mustAssert(t, newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil)), egressHost)
- wantCalls(t, restarts(ctl), "show -p IPAddressAllow --value "+InstancesSlice, "show -p ConsistsOf --value "+slice, "try-restart "+run)
- if rep.State != "" || rep.Detail != egressRestarted || len(rep.Allowed) != 0 {
- t.Fatalf("report = %+v, want no state and detail %q", rep, egressRestarted)
- }
- })
- t.Run("parent holds an allow: the revoke is live, nothing restarts", func(t *testing.T) {
- ctl := box("10.0.5.20/32", "10.0.5.53/32")
- rep := mustAssert(t, newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil)), egressHost)
- wantCalls(t, restarts(ctl), "show -p IPAddressAllow --value "+InstancesSlice)
- if rep.Detail != "" {
- t.Fatalf("report = %+v, want no detail", rep)
- }
- })
- t.Run("target not empty: the narrowing is live, nothing restarts", func(t *testing.T) {
- ctl := box("10.0.5.20/32 10.0.5.21/32", "")
- rep := mustAssert(t, newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.21"}}, nil)), egressHost)
- wantCalls(t, restarts(ctl))
- if rep.Detail != "" || !slices.Equal(rep.Allowed, []string{"10.0.5.21"}) {
- t.Fatalf("report = %+v", rep)
- }
- })
- t.Run("nothing held, nothing wanted: no write, nothing restarts", func(t *testing.T) {
- ctl := box("", "")
- mustAssert(t, newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil)), egressHost)
- wantCalls(t, ctl.takeCalls(), showCall(egressHost))
- })
- t.Run("under claims the reset of a stale allow restarts too, the detail kept", func(t *testing.T) {
- ctl := box("10.0.5.20/32", "")
- rep := mustAssert(t, newEgressAsserter(ctl, staticSource(EgressPolicy{Closed: EgressUnderClaims, Detail: "not yet supported under claims"}, nil)), egressHost)
- wantCalls(t, restarts(ctl), "show -p IPAddressAllow --value "+InstancesSlice, "show -p ConsistsOf --value "+slice, "try-restart "+run)
- if rep.State != EgressUnderClaims || rep.Detail != "not yet supported under claims; "+egressRestarted {
- t.Fatalf("report = %+v", rep)
- }
- })
- t.Run("a restart whose start fails still ended the old invocation", func(t *testing.T) {
- ctl := box("10.0.5.20/32", "")
- ctl.restartFail[run] = true
- e := newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil))
- // The fake's failing try-restart leaves the invocation as it was;
- // the unit reading failed after it (its first read, the listing,
- // sees it active) is what counts as closed.
- orig := e.Systemctl
- calls := 0
- e.Systemctl = func(ctx context.Context, args ...string) (string, error) {
- if len(args) == 5 && args[2] == "ActiveState" && args[4] == run {
- calls++
- if calls > 1 {
- return "failed\n", nil
- }
- }
- return orig(ctx, args...)
- }
- rep := mustAssert(t, e, egressHost)
- if rep.Detail != egressRestarted {
- t.Fatalf("report = %+v, want detail %q", rep, egressRestarted)
- }
- })
- t.Run("a restart that does not happen stays due and is retried", func(t *testing.T) {
- ctl := box("10.0.5.20/32", "")
- ctl.restartFail[run] = true
- e := newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil))
- if err := e.Assert(context.Background(), egressHost); err == nil {
- t.Fatal("Assert succeeded though the running unit kept its invocation")
- }
- if rep := e.Reports()[egressHost]; rep.State != EgressFailing || !strings.Contains(rep.Detail, "try-restart") {
- t.Fatalf("report = %+v, want failing naming the try-restart", rep)
- }
- ctl.takeCalls()
- ctl.restartFail[run] = false
- rep := mustAssert(t, e, egressHost) // the slice already reads back empty
- if rep.State != "" || rep.Detail != egressRestarted {
- t.Fatalf("retry: report = %+v, want detail %q", rep, egressRestarted)
- }
- if !slices.Contains(ctl.takeCalls(), "try-restart "+run) {
- t.Fatal("retry did not restart the unit")
- }
- ctl.takeCalls()
- mustAssert(t, e, egressHost)
- wantCalls(t, ctl.takeCalls(), showCall(egressHost))
- })
-}