| index 0b3a07a88..e27b6a680 100644 |
| --- a/internal/host/egress_startpaths_rootrunner_linux_test.go |
| +++ b/internal/host/egress_startpaths_rootrunner_linux_test.go |
| @@ -175,6 +175,28 @@ func egressTemplateDeny(t *testing.T) string { |
| // pair and the namespace. |
| func egressTargetNetns(t *testing.T) map[string]string { |
| t.Helper() |
| + addrs, _ := egressTargetNetnsClosable(t) |
| + return addrs |
| +} |
| + |
| +// egressTargetNetnsClosable is egressTargetNetns with its teardown, for a |
| +// test that must have the fixture gone before it goes on: `ip netns add` |
| +// bind-mounts the namespace at /run/netns, and the root generation census |
| +// (generationMounts) refuses any nsfs mount on purpose. Calling close, or |
| +// the test's end, tears it down once, last-made first. |
| +func egressTargetNetnsClosable(t *testing.T) (map[string]string, func()) { |
| + t.Helper() |
| + var steps []func() |
| + var once sync.Once |
| + closeAll := func() { |
| + once.Do(func() { |
| + for i := len(steps) - 1; i >= 0; i-- { |
| + steps[i]() |
| + } |
| + }) |
| + } |
| + t.Cleanup(closeAll) |
| + cleanup := func(f func()) { steps = append(steps, f) } |
| if os.Geteuid() != 0 { |
| t.Fatal("needs real root") |
| } |
| @@ -190,7 +212,7 @@ func egressTargetNetns(t *testing.T) map[string]string { |
| } |
| |
| egressIP(t, "netns", "add", egressNetns) |
| - t.Cleanup(func() { |
| + cleanup(func() { |
| if out, err := exec.Command("ip", "netns", "del", egressNetns).CombinedOutput(); err != nil { |
| t.Errorf("ip netns del %s: %v: %s", egressNetns, err, out) |
| } |
| @@ -199,7 +221,7 @@ func egressTargetNetns(t *testing.T) map[string]string { |
| } |
| }) |
| egressIP(t, "link", "add", egressHostLink, "type", "veth", "peer", "name", egressNsLink, "netns", egressNetns) |
| - t.Cleanup(func() { |
| + cleanup(func() { |
| // Deleting one end removes the pair. The namespace's own deletion |
| // would too, but only once nothing holds the namespace, so the |
| // pair goes first and explicitly. |
| @@ -240,7 +262,7 @@ func egressTargetNetns(t *testing.T) map[string]string { |
| if err != nil { |
| t.Fatalf("listen on %s in %s: %v", a, egressNetns, err) |
| } |
| - t.Cleanup(func() { ln.Close() }) |
| + cleanup(func() { ln.Close() }) |
| go func() { |
| for { |
| c, err := ln.Accept() |
| @@ -279,7 +301,7 @@ func egressTargetNetns(t *testing.T) map[string]string { |
| } |
| } |
| } |
| - return addrs |
| + return addrs, closeAll |
| } |
| |
| // egressListenIn opens a TCP listener inside the network namespace at |
| @@ -1140,9 +1162,9 @@ func TestPlan14EgressClaimLaunchRootRunner(t *testing.T) { |
| // dir) whose row carries 10.0.5.20 with an unexpired expiry. The registry |
| // is the one the claims asserter is built over (instanceControlClaimResources); |
| // under claims it only names slices. |
| -func plan14EgressClaimSetup(t *testing.T, host, dir string, uid, gid int) (targets map[string]string, reg *registry.Registry, data string) { |
| +func plan14EgressClaimSetup(t *testing.T, host, dir string, uid, gid int) (targets map[string]string, reg *registry.Registry, data string, closeTargets func()) { |
| t.Helper() |
| - targets = egressTargetNetns(t) |
| + targets, closeTargets = egressTargetNetnsClosable(t) |
| slice := instancectl.SliceName(host) |
| egressSliceProperties(t, slice) |
| data = filepath.Join(dir, "egress-data") |
| @@ -1167,7 +1189,7 @@ func plan14EgressClaimSetup(t *testing.T, host, dir string, uid, gid int) (targe |
| if got := egressShow(t, slice, "IPAddressAllow"); got != "10.0.5.20/32" { |
| t.Fatalf("hand grant: slice holds %q", got) |
| } |
| - return targets, reg, data |
| + return targets, reg, data, closeTargets |
| } |
| |
| // plan14EgressClaimPolicy puts, in the harness's store, a policy for the |
| @@ -1190,9 +1212,17 @@ func plan14EgressClaimPolicy(t *testing.T, store release.Store, priv ed25519.Pri |
| // control service's handler over the same asserter) reports under_claims |
| // with claimsEgressDetail, the agent's status probe publishes it for the |
| // row that carries the allow (REASON "egress: not yet supported under |
| -// claims"), and root wrote no policy of its own. |
| -func plan14EgressClaimChecks(t *testing.T, host string, tenant *http.Client, targets map[string]string, egress *instancectl.EgressAsserter, reg *registry.Registry, optDir string) { |
| +// claims"), and root wrote no policy of its own. Then it removes the |
| +// target fixture: the harness goes on to the generation census, which |
| +// refuses the namespace's nsfs mount (generationMounts), on purpose. |
| +func plan14EgressClaimChecks(t *testing.T, host string, tenant *http.Client, targets map[string]string, closeTargets func(), egress *instancectl.EgressAsserter, reg *registry.Registry, optDir string) { |
| t.Helper() |
| + defer func() { |
| + closeTargets() |
| + if _, err := os.Lstat(filepath.Join("/run/netns", egressNetns)); !errors.Is(err, fs.ErrNotExist) { |
| + t.Errorf("the target namespace is still mounted before the generation census: %v", err) |
| + } |
| + }() |
| slice := instancectl.SliceName(host) |
| if got := egressShow(t, slice, "IPAddressAllow"); got != "" { |
| t.Fatalf("under claims: slice holds %q after the claim launch, want nothing", got) |