| index 018bfe8e5..3207bca06 100644 |
| --- a/internal/instancectl/egress_restart_linux_test.go |
| +++ b/internal/instancectl/egress_restart_linux_test.go |
| @@ -10,6 +10,7 @@ package instancectl |
| |
| import ( |
| "context" |
| + "errors" |
| "os" |
| "path/filepath" |
| "slices" |
| @@ -185,3 +186,143 @@ func TestEgressRevokeToEmptyRestartsWithoutAParentAllow(t *testing.T) { |
| } |
| }) |
| } |
| + |
| +// restartBox is a slice holding 10.0.5.20/32 with its run unit running, |
| +// no parent allow, and an asserter whose systemctl the test can wrap. |
| +func restartBox(t *testing.T, host string) (*egressCtl, *EgressAsserter, string) { |
| + t.Helper() |
| + slice := SliceName(host) |
| + root := t.TempDir() |
| + cgroupSlice(t, root, slice, true) |
| + run := UnitName(RoleRun, host) |
| + cgroupService(t, root, slice, run, true) |
| + ctl := newEgressCtl() |
| + ctl.setHeld(slice, "10.0.5.20/32") |
| + ctl.active[run] = "active" |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil)) |
| + e.CgroupRoot = root |
| + return ctl, e, run |
| +} |
| + |
| +// Review finding 1: the restart a revoke to empty owes is recorded before |
| +// the reset is written, so an uncertain write (a read after it that |
| +// fails, or a write that applied but answered an error) still leaves it |
| +// due, and the next Assert, finding the slice empty, restarts. |
| +func TestEgressRevokeRestartSurvivesAnUncertainWrite(t *testing.T) { |
| + slice := SliceName(egressHost) |
| + for _, tc := range []struct { |
| + name string |
| + wrap func(next func(context.Context, ...string) (string, error)) func(context.Context, ...string) (string, error) |
| + }{ |
| + {"the read after the reset fails", func(next func(context.Context, ...string) (string, error)) func(context.Context, ...string) (string, error) { |
| + wrote, failed := false, false |
| + return func(ctx context.Context, args ...string) (string, error) { |
| + if len(args) > 0 && args[0] == "set-property" { |
| + wrote = true |
| + } else if wrote && !failed && len(args) == 5 && args[2] == "IPAddressAllow" && args[4] == slice { |
| + failed = true |
| + return "Failed to connect to bus", errors.New("exit status 1") |
| + } |
| + return next(ctx, args...) |
| + } |
| + }}, |
| + {"the reset applies but answers an error", func(next func(context.Context, ...string) (string, error)) func(context.Context, ...string) (string, error) { |
| + done := false |
| + return func(ctx context.Context, args ...string) (string, error) { |
| + out, err := next(ctx, args...) |
| + if len(args) > 0 && args[0] == "set-property" && !done { |
| + done = true |
| + return "Connection timed out", errors.New("exit status 1") |
| + } |
| + return out, err |
| + } |
| + }}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + ctl, e, run := restartBox(t, egressHost) |
| + e.Systemctl = tc.wrap(e.Systemctl) |
| + if err := e.Assert(context.Background(), egressHost); err == nil { |
| + t.Fatal("first Assert succeeded through an uncertain write") |
| + } |
| + if got := ctl.held[slice]; got != "" { |
| + t.Fatalf("the reset did not apply in the fixture: %q", got) |
| + } |
| + ctl.takeCalls() |
| + rep := mustAssert(t, e, egressHost) // the slice now reads back empty |
| + if !slices.Contains(ctl.takeCalls(), "try-restart "+run) || rep.Detail != egressRestarted { |
| + t.Fatalf("the next Assert did not restart the slice's running unit: %+v", rep) |
| + } |
| + }) |
| + } |
| + t.Run("a grant landing again discharges the restart", func(t *testing.T) { |
| + ctl, e, run := restartBox(t, egressHost) |
| + ctl.restartFail[run] = true |
| + if err := e.Assert(context.Background(), egressHost); err == nil { |
| + t.Fatal("Assert succeeded though the restart did not happen") |
| + } |
| + ctl.restartFail[run] = false |
| + e.Source = staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil) |
| + mustAssert(t, e, egressHost) |
| + e.Source = staticSource(EgressPolicy{}, nil) |
| + ctl.setHeld(slice, "") |
| + ctl.takeCalls() |
| + mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.takeCalls(), showCall(egressHost)) |
| + }) |
| +} |
| + |
| +// Review finding 2: the sweep's reset of a slice it cannot name keeps a |
| +// failed restart due, and a later sweep that finds the slice empty |
| +// discharges it. |
| +func TestEgressSweepRetriesAnUnnamedSlicesRestart(t *testing.T) { |
| + slice := SliceName(longEgressHost) // a hashed id: no name for it here |
| + root := t.TempDir() |
| + cgroupSlice(t, root, slice, true) |
| + unit := UnitName(RoleRun, longEgressHost) |
| + cgroupService(t, root, slice, unit, true) |
| + ctl := newEgressCtl() |
| + ctl.setHeld(slice, "10.0.5.20/32") |
| + ctl.active[unit] = "active" |
| + ctl.restartFail[unit] = true |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil)) |
| + e.CgroupRoot = root |
| + e.Sweep(context.Background()) |
| + if got := ctl.held[slice]; got != "" { |
| + t.Fatalf("the first sweep did not reset the unnamed slice: %q", got) |
| + } |
| + ctl.restartFail[unit] = false |
| + ctl.takeCalls() |
| + e.Sweep(context.Background()) |
| + if !slices.Contains(ctl.takeCalls(), "try-restart "+unit) { |
| + t.Fatal("the second sweep did not retry the unnamed slice's restart") |
| + } |
| + e.Sweep(context.Background()) |
| + if slices.Contains(ctl.takeCalls(), "try-restart "+unit) { |
| + t.Fatal("a discharged restart ran again") |
| + } |
| +} |
| + |
| +// Review finding 3: a restart counts as done only on readings that prove |
| +// it: both InvocationIDs read and different, or the unit read inactive or |
| +// failed. A failed read proves nothing. |
| +func TestEgressRestartNotProvenByAFailedRead(t *testing.T) { |
| + ctl, e, run := restartBox(t, egressHost) |
| + ctl.restartFail[run] = true // try-restart fails; the unit stays active on its invocation |
| + next := e.Systemctl |
| + reads := 0 |
| + e.Systemctl = func(ctx context.Context, args ...string) (string, error) { |
| + if len(args) == 5 && args[2] == "InvocationID" && args[4] == run { |
| + reads++ |
| + if reads > 1 { // the read after the try-restart |
| + return "", errors.New("exit status 1") |
| + } |
| + } |
| + return next(ctx, args...) |
| + } |
| + if err := e.Assert(context.Background(), egressHost); err == nil { |
| + t.Fatal("a failed InvocationID read counted as proof the old invocation is gone") |
| + } |
| + if rep := e.Reports()[egressHost]; rep.State != EgressFailing || !e.isRestartDue(SliceName(egressHost)) { |
| + t.Fatalf("report = %+v, due %v: want failing and the restart kept due", rep, e.isRestartDue(SliceName(egressHost))) |
| + } |
| +} |