| index d1284bb7f..b21d1e0f0 100644 |
| --- a/internal/instancectl/egress_test.go |
| +++ b/internal/instancectl/egress_test.go |
| @@ -369,40 +369,130 @@ func TestEgressReportsIsACopy(t *testing.T) { |
| } |
| } |
| |
| -func TestEgressReadBackErrorStillWrites(t *testing.T) { |
| - ctl := newEgressCtl() |
| - ctl.setHeld(SliceName(egressHost), "10.0.5.21/32") |
| - e := newEgressAsserter(ctl, staticSource(EgressPolicy{}, nil)) |
| - first := true |
| - e.Systemctl = func(ctx context.Context, args ...string) (string, error) { |
| +// failingShow wraps ctl so that the first n `show` calls fail (each still |
| +// recorded); everything else is ctl's. |
| +func failingShow(ctl *egressCtl, n int) func(context.Context, ...string) (string, error) { |
| + return func(ctx context.Context, args ...string) (string, error) { |
| out, err := ctl.run(ctx, args...) |
| - if args[0] == "show" && first { |
| - first = false |
| + if args[0] == "show" && n > 0 { |
| + n-- |
| return "Failed to get properties", errors.New("exit status 1") |
| } |
| return out, err |
| } |
| +} |
| + |
| +// Fix round 1, item 2: a read that fails with nothing wanted establishes |
| +// no difference, so nothing is written; the next assert whose read works |
| +// lands the revoke. |
| +func TestEgressReadErrorWithNothingWantedWritesNothing(t *testing.T) { |
| + ctl := newEgressCtl() |
| + ctl.setHeld(SliceName(egressHost), "10.0.5.20/32") |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Closed: EgressUnderClaims}, nil)) |
| + e.Systemctl = failingShow(ctl, 1) |
| + if err := e.Assert(context.Background(), egressHost); err == nil { |
| + t.Fatal("Assert = nil, want the read error") |
| + } |
| + wantCalls(t, ctl.takeCalls(), showCall(egressHost)) |
| + if rep := e.Reports()[egressHost]; rep.State != EgressFailing || !strings.Contains(rep.Detail, "Failed to get properties") || rep.Allowed != nil { |
| + t.Fatalf("report = %+v, want state %q naming the read error", rep, EgressFailing) |
| + } |
| + |
| rep := mustAssert(t, e, egressHost) |
| - wantCalls(t, ctl.sets(), setCall(egressHost)) |
| - if !reflect.DeepEqual(rep, EgressReport{}) { |
| - t.Fatalf("report = %+v, want {}", rep) |
| + wantCalls(t, ctl.takeCalls(), showCall(egressHost), setCall(egressHost), showCall(egressHost)) |
| + if rep.State != EgressUnderClaims || rep.Allowed != nil { |
| + t.Fatalf("report = %+v, want state %q and nothing allowed", rep, EgressUnderClaims) |
| + } |
| + |
| + // No policy at all: the same, never a blind reset. |
| + ctl2 := newEgressCtl() |
| + e2 := newEgressAsserter(ctl2, staticSource(EgressPolicy{}, nil)) |
| + e2.Systemctl = failingShow(ctl2, 1) |
| + if err := e2.Assert(context.Background(), egressHost); err == nil { |
| + t.Fatal("Assert with no policy = nil, want the read error") |
| } |
| + wantCalls(t, ctl2.takeCalls(), showCall(egressHost)) |
| } |
| |
| -func TestEgressWriteErrorIsFailing(t *testing.T) { |
| +// Fix round 1, item 2: with an allow wanted, a failed first read still |
| +// writes it, and the read after the write decides. |
| +func TestEgressReadErrorWithAnAllowWantedWrites(t *testing.T) { |
| ctl := newEgressCtl() |
| e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20"}}, nil)) |
| - e.Systemctl = func(ctx context.Context, args ...string) (string, error) { |
| - if args[0] == "set-property" { |
| - return "Failed to set unit properties", errors.New("exit status 1") |
| - } |
| - return ctl.run(ctx, args...) |
| - } |
| - if err := e.Assert(context.Background(), egressHost); err == nil { |
| - t.Fatal("Assert = nil, want the set-property error") |
| + e.Systemctl = failingShow(ctl, 1) |
| + rep := mustAssert(t, e, egressHost) |
| + wantCalls(t, ctl.takeCalls(), showCall(egressHost), setCall(egressHost, "10.0.5.20/32"), showCall(egressHost)) |
| + if want := (EgressReport{Allowed: []string{"10.0.5.20"}}); !reflect.DeepEqual(rep, want) { |
| + t.Fatalf("report = %+v, want %+v", rep, want) |
| } |
| - if rep := e.Reports()[egressHost]; rep.State != EgressFailing || !strings.Contains(rep.Detail, "Failed to set unit properties") { |
| - t.Fatalf("report = %+v, want state %q naming systemctl's output", rep, EgressFailing) |
| +} |
| + |
| +// Fix round 1, item 1: a failed write never hides an allow the slice was |
| +// seen to hold. Each case revokes a held 10.0.5.20/32. |
| +func TestEgressWriteErrorIsFailing(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + setFails bool // set-property returns an error |
| + setApplies bool // ... after changing the slice anyway (a partial change) |
| + failShowsAt int // 1-based show call that fails (0: none) |
| + wantAllowed []string |
| + wantDetail []string |
| + }{ |
| + {name: "reset fails, slice unchanged", setFails: true, |
| + wantAllowed: []string{"10.0.5.20"}, wantDetail: []string{"Failed to set unit properties", "the slice now holds 10.0.5.20"}}, |
| + {name: "reset fails, slice changed anyway", setFails: true, setApplies: true, |
| + wantAllowed: nil, wantDetail: []string{"Failed to set unit properties", "the slice now holds nothing"}}, |
| + {name: "reset fails, re-read fails", setFails: true, failShowsAt: 2, |
| + wantAllowed: []string{"10.0.5.20"}, wantDetail: []string{"Failed to set unit properties", "as read before the write and may have changed"}}, |
| + {name: "reset written, read after it fails", setApplies: true, failShowsAt: 2, |
| + wantAllowed: []string{"10.0.5.20"}, wantDetail: []string{"read back after the write", "as read before the write and may have changed"}}, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + ctl := newEgressCtl() |
| + ctl.setHeld(SliceName(egressHost), "10.0.5.20/32") |
| + e := newEgressAsserter(ctl, staticSource(EgressPolicy{Closed: EgressExpired}, nil)) |
| + shows := 0 |
| + e.Systemctl = func(ctx context.Context, args ...string) (string, error) { |
| + switch args[0] { |
| + case "set-property": |
| + if !tc.setApplies { |
| + ctl.mu.Lock() |
| + ctl.calls = append(ctl.calls, strings.Join(args, " ")) |
| + ctl.mu.Unlock() |
| + } else if _, err := ctl.run(ctx, args...); err != nil { |
| + return "", err |
| + } |
| + if tc.setFails { |
| + return "Failed to set unit properties", errors.New("exit status 1") |
| + } |
| + return "", nil |
| + case "show": |
| + shows++ |
| + out, err := ctl.run(ctx, args...) |
| + if shows == tc.failShowsAt { |
| + return "Failed to get properties", errors.New("exit status 1") |
| + } |
| + return out, err |
| + } |
| + return ctl.run(ctx, args...) |
| + } |
| + if err := e.Assert(context.Background(), egressHost); err == nil { |
| + t.Fatal("Assert = nil, want an error") |
| + } |
| + wantCalls(t, ctl.sets(), setCall(egressHost)) |
| + rep := e.Reports()[egressHost] |
| + if rep.State != EgressFailing { |
| + t.Fatalf("State = %q, want %q", rep.State, EgressFailing) |
| + } |
| + if !reflect.DeepEqual(rep.Allowed, tc.wantAllowed) { |
| + t.Fatalf("Allowed = %q, want %q", rep.Allowed, tc.wantAllowed) |
| + } |
| + for _, d := range tc.wantDetail { |
| + if !strings.Contains(rep.Detail, d) { |
| + t.Fatalf("Detail = %q, want it to contain %q", rep.Detail, d) |
| + } |
| + } |
| + }) |
| } |
| } |
| |