From 61174960d7226c8fc37eaabf4b7aba58a75a6c97 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sat, 24 Oct 2026 00:55:23 -0600 Subject: [PATCH] refactor(e2e): share detached cleanup context helper --- e2e/framework/command.go | 16 ++++++++++++---- e2e/tests/up/microsandbox_session.go | 2 +- .../up/provider_microsandbox_compatibility.go | 4 ++-- .../up/provider_microsandbox_consumer_linux.go | 4 ++-- e2e/tests/up/provider_microsandbox_egress.go | 6 +++--- e2e/tests/up/provider_microsandbox_ephemeral.go | 12 ++++++------ e2e/tests/up/provider_microsandbox_migration.go | 4 ++-- e2e/tests/up/provider_microsandbox_mounts.go | 9 ++++----- e2e/tests/up/provider_microsandbox_recreate.go | 4 ++-- .../provider_microsandbox_remote_agent_linux.go | 9 ++++----- 10 files changed, 38 insertions(+), 32 deletions(-) diff --git a/e2e/framework/command.go b/e2e/framework/command.go index 6bb52a09e6..d351e9c77f 100644 --- a/e2e/framework/command.go +++ b/e2e/framework/command.go @@ -527,11 +527,19 @@ func (f *Framework) DevsyWorkspaceDelete( return f.ExecCommand(ctx, false, true, fmt.Sprintf("deleted workspace %s", workspace), baseArgs) } -// CleanupWorkspace gives cleanup a fresh, bounded context. Ginkgo cleanup may -// run after the spec context has expired, so inheriting its cancellation can -// silently skip the workspace deletion. +// DetachedCleanupContext bounds cleanup work to timeout without inheriting +// parent's cancellation. Ginkgo cleanup may run after the spec context has +// expired, and inheriting that cancellation silently skips the cleanup. +func DetachedCleanupContext( + parent context.Context, + timeout time.Duration, +) (context.Context, context.CancelFunc) { + return context.WithTimeout(context.WithoutCancel(parent), timeout) +} + +// CleanupWorkspace deletes workspace under a DetachedCleanupContext. func (f *Framework) CleanupWorkspace(ctx context.Context, workspace string) error { - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), time.Minute) + cleanupCtx, cancel := DetachedCleanupContext(ctx, time.Minute) defer cancel() err := f.DevsyWorkspaceDelete(cleanupCtx, workspace) if err != nil { diff --git a/e2e/tests/up/microsandbox_session.go b/e2e/tests/up/microsandbox_session.go index 8dc13ff7de..a2ae5b2766 100644 --- a/e2e/tests/up/microsandbox_session.go +++ b/e2e/tests/up/microsandbox_session.go @@ -117,7 +117,7 @@ func (s *microsandboxTestSession) copyWorkspace(source string) string { func (s *microsandboxTestSession) addProvider(ctx context.Context, source, name string) { framework.ExpectNoError(s.framework.DevsyProviderAdd(ctx, source, "--name", name)) ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), time.Minute) + cleanupCtx, cancel := framework.DetachedCleanupContext(cleanupCtx, time.Minute) defer cancel() return s.framework.DevsyProviderDelete(cleanupCtx, name) }) diff --git a/e2e/tests/up/provider_microsandbox_compatibility.go b/e2e/tests/up/provider_microsandbox_compatibility.go index eff0b6507d..c1c7047dd5 100644 --- a/e2e/tests/up/provider_microsandbox_compatibility.go +++ b/e2e/tests/up/provider_microsandbox_compatibility.go @@ -113,7 +113,7 @@ func (fixture *compatibilityFixture) registerCleanup() { // Faults are cleared independently so a failed workspace cleanup cannot block fallback. ginkgo.DeferCleanup(fixture.shim.restoreEnvironment) ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), time.Minute) + cleanupCtx, cancel := framework.DetachedCleanupContext(cleanupCtx, time.Minute) defer cancel() fixture.shim.clearVersionFault() out, err := microsandboxResourceCommand(cleanupCtx, "list", "--quiet") @@ -136,7 +136,7 @@ func (fixture *compatibilityFixture) registerCleanup() { return nil }) ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), time.Minute) + cleanupCtx, cancel := framework.DetachedCleanupContext(cleanupCtx, time.Minute) defer cancel() fixture.shim.clearVersionFault() return fixture.framework.CleanupWorkspace(cleanupCtx, fixture.hostPath) diff --git a/e2e/tests/up/provider_microsandbox_consumer_linux.go b/e2e/tests/up/provider_microsandbox_consumer_linux.go index cd1c9adb5a..86cf07607c 100644 --- a/e2e/tests/up/provider_microsandbox_consumer_linux.go +++ b/e2e/tests/up/provider_microsandbox_consumer_linux.go @@ -151,7 +151,7 @@ func runMicrosandboxConsumerAttempt(ctx context.Context, attempt microsandboxCon host := newMicrosandboxConsumerHost(ctx, providerName, workspaceID, consumerProvider) ginkgo.DeferCleanup(func(cleanupCtx ginkgo.SpecContext) error { - deleteCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), time.Minute) + deleteCtx, cancel := framework.DetachedCleanupContext(cleanupCtx, time.Minute) defer cancel() return host.DeleteDevContainer(deleteCtx, workspaceID) }) @@ -191,7 +191,7 @@ func newMicrosandboxConsumerRegistry(handler http.Handler) *registryfixture.Serv } func closeMicrosandboxConsumerRegistry(ctx context.Context, fixture *registryfixture.Server) error { - closeCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) + closeCtx, cancel := framework.DetachedCleanupContext(ctx, 30*time.Second) defer cancel() return fixture.Close(closeCtx) } diff --git a/e2e/tests/up/provider_microsandbox_egress.go b/e2e/tests/up/provider_microsandbox_egress.go index 274226043f..fd6ed95eb6 100644 --- a/e2e/tests/up/provider_microsandbox_egress.go +++ b/e2e/tests/up/provider_microsandbox_egress.go @@ -259,7 +259,7 @@ func newMicrosandboxEgressEndpoint(ctx context.Context) *microsandboxEgressEndpo finished := make(chan error, 1) go func() { finished <- server.Serve(listener) }() ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), 10*time.Second) + cleanupCtx, cancel := framework.DetachedCleanupContext(cleanupCtx, 10*time.Second) defer cancel() closeErr := server.Close() select { @@ -297,7 +297,7 @@ func createMicrosandboxEgressInterface(ctx context.Context, name, address string // v0.7.7 classifies this benchmark range as Public and dials non-gateway IPs directly. framework.ExpectNoError(microsandboxEgressIP(ctx, "link", "add", "name", name, "type", "dummy")) ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), time.Minute) + cleanupCtx, cancel := framework.DetachedCleanupContext(cleanupCtx, time.Minute) defer cancel() return microsandboxEgressIP(cleanupCtx, "link", "delete", "dev", name) }) @@ -425,7 +425,7 @@ func cleanupMicrosandboxEgressProvider( f *framework.Framework, provider string, ) error { - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), time.Minute) + cleanupCtx, cancel := framework.DetachedCleanupContext(ctx, time.Minute) defer cancel() return f.DevsyProviderDelete(cleanupCtx, provider) } diff --git a/e2e/tests/up/provider_microsandbox_ephemeral.go b/e2e/tests/up/provider_microsandbox_ephemeral.go index 4d398a2848..a6b8b2c334 100644 --- a/e2e/tests/up/provider_microsandbox_ephemeral.go +++ b/e2e/tests/up/provider_microsandbox_ephemeral.go @@ -43,8 +43,8 @@ var _ = ginkgo.Describe("microsandbox ephemeral root parity", f.DevsyProviderAdd(ctx, provider.source, "--name", provider.name), ) ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { - cleanupCtx, cancel := context.WithTimeout( - context.WithoutCancel(cleanupCtx), + cleanupCtx, cancel := framework.DetachedCleanupContext( + cleanupCtx, time.Minute, ) defer cancel() @@ -55,8 +55,8 @@ var _ = ginkgo.Describe("microsandbox ephemeral root parity", if ownedLabel == "" { return nil } - cleanupCtx, cancel := context.WithTimeout( - context.WithoutCancel(cleanupCtx), + cleanupCtx, cancel := framework.DetachedCleanupContext( + cleanupCtx, time.Minute, ) defer cancel() @@ -82,8 +82,8 @@ var _ = ginkgo.Describe("microsandbox ephemeral root parity", if !volumeOwned { return nil } - cleanupCtx, cancel := context.WithTimeout( - context.WithoutCancel(cleanupCtx), + cleanupCtx, cancel := framework.DetachedCleanupContext( + cleanupCtx, time.Minute, ) defer cancel() diff --git a/e2e/tests/up/provider_microsandbox_migration.go b/e2e/tests/up/provider_microsandbox_migration.go index 5ff2d6f7c5..fd8fb888b4 100644 --- a/e2e/tests/up/provider_microsandbox_migration.go +++ b/e2e/tests/up/provider_microsandbox_migration.go @@ -117,7 +117,7 @@ func (fixture *microsandboxMigrationFixture) registerCleanup() { if !fixture.volumeOwned { return nil } - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), time.Minute) + cleanupCtx, cancel := framework.DetachedCleanupContext(cleanupCtx, time.Minute) defer cancel() out, err := microsandboxResourceCommand(cleanupCtx, "volume", "rm", fixture.volume) if err != nil { @@ -126,7 +126,7 @@ func (fixture *microsandboxMigrationFixture) registerCleanup() { return nil }) ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), time.Minute) + cleanupCtx, cancel := framework.DetachedCleanupContext(cleanupCtx, time.Minute) defer cancel() out, err := microsandboxResourceCommand(cleanupCtx, "list", "--quiet") if err != nil { diff --git a/e2e/tests/up/provider_microsandbox_mounts.go b/e2e/tests/up/provider_microsandbox_mounts.go index 4634c5d894..eb90fbac2e 100644 --- a/e2e/tests/up/provider_microsandbox_mounts.go +++ b/e2e/tests/up/provider_microsandbox_mounts.go @@ -68,8 +68,9 @@ var _ = ginkgo.Describe("microsandbox mount parity", ) framework.ExpectNoError(err) ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { - cleanupCtx, cancel := context.WithTimeout( - context.WithoutCancel(cleanupCtx), time.Minute, + cleanupCtx, cancel := framework.DetachedCleanupContext( + cleanupCtx, + time.Minute, ) defer cancel() _, err := f.ExecCommandOutput( @@ -292,9 +293,7 @@ func storeMicrosandboxSecret( ) framework.ExpectNoError(err) ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { - cleanupCtx, cancel := context.WithTimeout( - context.WithoutCancel(cleanupCtx), time.Minute, - ) + cleanupCtx, cancel := framework.DetachedCleanupContext(cleanupCtx, time.Minute) defer cancel() _, err := f.ExecCommandOutput( cleanupCtx, diff --git a/e2e/tests/up/provider_microsandbox_recreate.go b/e2e/tests/up/provider_microsandbox_recreate.go index 4b6b6f232f..3b706f8790 100644 --- a/e2e/tests/up/provider_microsandbox_recreate.go +++ b/e2e/tests/up/provider_microsandbox_recreate.go @@ -148,7 +148,7 @@ func (f *safeRecreateFixture) registerCleanup() { if !f.volumeOwned { return nil } - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), time.Minute) + cleanupCtx, cancel := framework.DetachedCleanupContext(cleanupCtx, time.Minute) defer cancel() out, err := microsandboxResourceCommand(cleanupCtx, "volume", "rm", f.volume) if err != nil { @@ -157,7 +157,7 @@ func (f *safeRecreateFixture) registerCleanup() { return nil }) ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), time.Minute) + cleanupCtx, cancel := framework.DetachedCleanupContext(cleanupCtx, time.Minute) defer cancel() out, err := microsandboxResourceCommand(cleanupCtx, "list", "--quiet") if err != nil { diff --git a/e2e/tests/up/provider_microsandbox_remote_agent_linux.go b/e2e/tests/up/provider_microsandbox_remote_agent_linux.go index 43669e4373..30b711dce2 100644 --- a/e2e/tests/up/provider_microsandbox_remote_agent_linux.go +++ b/e2e/tests/up/provider_microsandbox_remote_agent_linux.go @@ -94,9 +94,7 @@ var _ = ginkgo.Describe("published MicroSandbox provider remote agent binaries", ) server := startRemoteAgentArtifactServer(runtimeBytes, agentPath) ginkgo.DeferCleanup(func(cleanupCtx ginkgo.SpecContext) error { - closeCtx, cancel := context.WithTimeout( - context.WithoutCancel(cleanupCtx), 10*time.Second, - ) + closeCtx, cancel := framework.DetachedCleanupContext(cleanupCtx, 10*time.Second) defer cancel() return server.close(closeCtx) }) @@ -122,8 +120,9 @@ var _ = ginkgo.Describe("published MicroSandbox provider remote agent binaries", workspace := session.newWorkspace() ginkgo.DeferCleanup(func(cleanupCtx ginkgo.SpecContext) error { - boundedCtx, cancel := context.WithTimeout( - context.WithoutCancel(cleanupCtx), 20*time.Second, + boundedCtx, cancel := framework.DetachedCleanupContext( + cleanupCtx, + 20*time.Second, ) defer cancel() return removeRemoteAgentContainer(boundedCtx, containerName)