From 9312d2b0790acd1fa02589168bff45adf7cdb0b2 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 13 Oct 2026 00:23:20 -0600 Subject: [PATCH 1/7] fix(runtime): validate MicroSandbox replacements before teardown --- .github/workflows/pr-ci.yml | 9 + THIRD_PARTY_LICENSES.md | 2 +- .../up/provider_microsandbox_recreate.go | 398 ++++++++++++++++++ go.mod | 2 +- go.sum | 4 +- pkg/driver/external/capabilities.go | 8 +- pkg/driver/external/host.go | 2 +- .../external/internal/testfixture/main.go | 5 +- pkg/driver/external/recreate_test.go | 46 ++ pkg/driver/external/run_image.go | 1 + pkg/driver/external/run_image_test.go | 14 + pkg/driver/microsandbox/cliclient.go | 3 + pkg/driver/microsandbox/client.go | 31 +- .../microsandbox/image_accounts_test.go | 1 + pkg/driver/microsandbox/microsandbox.go | 11 +- pkg/driver/microsandbox/microsandbox_test.go | 5 +- pkg/driver/microsandbox/recreate.go | 35 ++ pkg/driver/microsandbox/recreate_cli_test.go | 138 ++++++ .../recreate_preservation_test.go | 72 ++++ .../microsandbox/workspace_owner_test.go | 1 + .../developing-providers/runtime-protocol.mdx | 14 +- 21 files changed, 770 insertions(+), 32 deletions(-) create mode 100644 e2e/tests/up/provider_microsandbox_recreate.go create mode 100644 pkg/driver/external/recreate_test.go create mode 100644 pkg/driver/microsandbox/recreate.go create mode 100644 pkg/driver/microsandbox/recreate_cli_test.go create mode 100644 pkg/driver/microsandbox/recreate_preservation_test.go diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index a9439b3ca3..63c9de1b83 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -723,6 +723,15 @@ jobs: test-timeout: 1200s job-timeout-minutes: 25 + - label: up-provider-microsandbox-recreate + runner: ubuntu-latest + free-disk-space: false + install-kind: false + requires-secret: false + install-microsandbox: true + test-timeout: 1200s + job-timeout-minutes: 25 + # Snapshot tests - label: snapshot diff --git a/THIRD_PARTY_LICENSES.md b/THIRD_PARTY_LICENSES.md index c9f63bd7e5..967aa10fb3 100644 --- a/THIRD_PARTY_LICENSES.md +++ b/THIRD_PARTY_LICENSES.md @@ -125,7 +125,7 @@ To regenerate this file after changing dependencies, run `task cli:licenses`. | [github.com/devsy-org/agentapi](https://github.com/devsy-org/agentapi) | `v1.0.1` | MPL-2.0 | | [github.com/devsy-org/api](https://github.com/devsy-org/api) | `v1.1.0` | MPL-2.0 | | [github.com/devsy-org/apiserver](https://github.com/devsy-org/apiserver) | `v1.5.4` | Apache-2.0 | -| [github.com/devsy-org/devsy-runtime-sdk](https://github.com/devsy-org/devsy-runtime-sdk) | `v1.5.2` | MPL-2.0 | +| [github.com/devsy-org/devsy-runtime-sdk](https://github.com/devsy-org/devsy-runtime-sdk) | `v1.6.0` | MPL-2.0 | | [github.com/devsy-org/ssh](https://github.com/devsy-org/ssh) | `v1.2.9` | BSD-3-Clause | | [github.com/distribution/reference](https://github.com/distribution/reference) | `v0.6.0` | Apache-2.0 | | [github.com/docker/cli](https://github.com/docker/cli) | `v29.8.0+incompatible` | Apache-2.0 | diff --git a/e2e/tests/up/provider_microsandbox_recreate.go b/e2e/tests/up/provider_microsandbox_recreate.go new file mode 100644 index 0000000000..ce4602041e --- /dev/null +++ b/e2e/tests/up/provider_microsandbox_recreate.go @@ -0,0 +1,398 @@ +package up + +import ( + "context" + "encoding/json" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + "time" + + "github.com/devsy-org/devsy/e2e/framework" + devconfig "github.com/devsy-org/devsy/pkg/devcontainer/config" + "github.com/devsy-org/devsy/pkg/encoding" + providerpkg "github.com/devsy-org/devsy/pkg/provider" + sshconfig "github.com/devsy-org/devsy/pkg/ssh" + "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" +) + +const ( + safeRecreateRootPath = "/root/devsy-safe-recreate-marker" + safeRecreateVolumeDir = "/safe-recreate-volume" + safeRecreateMarker = "safe-recreate-preserved" + safeRecreateSource = "safe-recreate-source" + safeRecreateConfig = ".devcontainer.json" + safeRecreateMemoryErr = "max_memory 1536 MiB must be greater than or equal to memory 2048 MiB" +) + +// These request-invalid cases are independent of the separate managed-policy +// override gate; they do not establish that broader effective-policy guarantee. +var _ = ginkgo.Describe("MicroSandbox rejected replacement preservation", + ginkgo.Label("up-provider-microsandbox-recreate"), func() { + ginkgo.DescribeTable( + "keeps the original generation until valid explicit replacement", + func(ctx context.Context, source, name string, stopped bool) { + fixture := newSafeRecreateFixture(ctx, source, name) + fixture.initialize(ctx, stopped) + fixture.rejectInvalidReplacement(ctx) + fixture.inspectOriginalFiles(ctx, stopped) + fixture.replaceSuccessfully(ctx) + }, + ginkgo.Entry( + "builtin running", + microsandboxResourceBuiltin, + "safe-recreate-builtin-running", + false, + ginkgo.SpecTimeout(framework.TimeoutLong()), + ), + ginkgo.Entry( + "builtin stopped", + microsandboxResourceBuiltin, + "safe-recreate-builtin-stopped", + true, + ginkgo.SpecTimeout(framework.TimeoutLong()), + ), + ginkgo.Entry( + "external running", + microsandboxExternalProvider, + "safe-recreate-external-running", + false, + ginkgo.SpecTimeout(framework.TimeoutLong()), + ), + ginkgo.Entry( + "external stopped", + microsandboxExternalProvider, + "safe-recreate-external-stopped", + true, + ginkgo.SpecTimeout(framework.TimeoutLong()), + ), + ) + }) + +type safeRecreateFixture struct { + framework *framework.Framework + workspace, sandbox, uid, label string + volume string + volumeOwned bool + original safeRecreateInspection + bootID string + record []byte + sshFiles map[string]safeRecreateFile + workspaceInfo *providerpkg.Workspace + source safeRecreateFile +} + +type safeRecreateInspection struct { + CreatedAt string `json:"created_at"` + Status string `json:"status"` + ActiveConfig json.RawMessage `json:"active_config"` + Config json.RawMessage `json:"config"` +} + +type safeRecreateFile struct { + Data []byte + Mode os.FileMode + Owner string +} + +func newSafeRecreateFixture(ctx context.Context, source, name string) *safeRecreateFixture { + skipIfNoMicrosandbox(ctx) + initialDir, err := os.Getwd() + framework.ExpectNoError(err) + home := ginkgo.GinkgoT().TempDir() + ginkgo.GinkgoT().Setenv("HOME", home) + ginkgo.GinkgoT().Setenv("USERPROFILE", home) + ginkgo.GinkgoT().Setenv("DEVSY_HOME", ginkgo.GinkgoT().TempDir()) + ginkgo.GinkgoT().Setenv("DEVSY_CONFIG", "") + ginkgo.GinkgoT().Setenv("MSB_HOME", filepath.Join(home, "microsandbox")) + ginkgo.GinkgoT().Setenv("MSB_CONFIG_PATH", filepath.Join(home, "microsandbox", "config.json")) + workspace, err := framework.CreateTempDir() + framework.ExpectNoError(err) + ginkgo.DeferCleanup(framework.CleanupTempDir, initialDir, workspace) + f := framework.NewDefaultFramework(filepath.Join(initialDir, "bin")) + framework.ExpectNoError(f.DevsyProviderAdd(ctx, source, "--name", name)) + ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { + cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), time.Minute) + defer cancel() + return f.DevsyProviderDelete(cleanupCtx, name) + }) + framework.ExpectNoError(f.DevsyProviderUse(ctx, name, + microsandboxResourceOption, "MICROSANDBOX_MEMORY=0", + microsandboxResourceOption, "MICROSANDBOX_MAX_MEMORY=1536", + microsandboxResourceOption, "MICROSANDBOX_CPUS=1", + microsandboxResourceOption, "MICROSANDBOX_MAX_CPUS=1", + microsandboxResourceOption, "MICROSANDBOX_STORAGE=2", + microsandboxResourceOption, "MICROSANDBOX_EPHEMERAL=false")) + fixture := &safeRecreateFixture{framework: f, workspace: workspace} + fixture.uid = encoding.CreateNewUID("", filepath.Base(workspace)) + fixture.sandbox = "devsy-" + fixture.uid + fixture.label = "devsy.safe-recreate=" + fixture.uid + fixture.volume = "devsy-safe-recreate-" + fixture.uid + assertMicrosandboxEphemeralAbsent(ctx, fixture.label) + out, err := microsandboxResourceCommand(ctx, "list", "--quiet") + framework.ExpectNoError(err, string(out)) + gomega.Expect(strings.Fields(string(out))).NotTo(gomega.ContainElement(fixture.sandbox)) + fixture.registerCleanup() + out, err = microsandboxResourceCommand(ctx, "volume", "create", fixture.volume) + framework.ExpectNoError(err, string(out)) + fixture.volumeOwned = true + return fixture +} + +func (f *safeRecreateFixture) registerCleanup() { + // Independent LIFO callbacks attempt workspace/backend cleanup before volume removal. + ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { + if !f.volumeOwned { + return nil + } + cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), time.Minute) + defer cancel() + out, err := microsandboxResourceCommand(cleanupCtx, "volume", "rm", f.volume) + if err != nil { + return fmt.Errorf("remove owned replacement volume: %w: %s", err, out) + } + return nil + }) + ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { + cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), time.Minute) + defer cancel() + out, err := microsandboxResourceCommand(cleanupCtx, "list", "--quiet") + if err != nil { + return fmt.Errorf("list owned replacement sandbox: %w: %s", err, out) + } + for name := range strings.FieldsSeq(string(out)) { + if name == f.sandbox { + out, err = microsandboxResourceCommand(cleanupCtx, "remove", "--force", name) + if err != nil { + return fmt.Errorf("remove owned replacement sandbox: %w: %s", err, out) + } + } + } + return nil + }) + ginkgo.DeferCleanup(f.framework.CleanupWorkspace, f.workspace) +} + +func (f *safeRecreateFixture) upArgs() []string { + return []string{"--devcontainer", safeRecreateConfig, "--uid", f.uid, "--id-label", f.label} +} + +func (f *safeRecreateFixture) writeConfig(memory string) { + configuration := struct { + Image string `json:"image"` + ContainerUser string `json:"containerUser"` + RemoteUser string `json:"remoteUser"` + Mounts []string `json:"mounts"` + HostRequirements *devconfig.HostRequirements `json:"hostRequirements"` + }{ + Image: microsandboxResourceImage, + ContainerUser: microsandboxRootUser, + RemoteUser: microsandboxRootUser, + Mounts: []string{ + "type=volume,source=" + f.volume + ",target=" + safeRecreateVolumeDir, + }, + HostRequirements: &devconfig.HostRequirements{Memory: memory}, + } + data, err := json.Marshal(configuration) + framework.ExpectNoError(err) + framework.ExpectNoError( + os.WriteFile(filepath.Join(f.workspace, safeRecreateConfig), data, 0o600), + ) +} + +func (f *safeRecreateFixture) initialize(ctx context.Context, stopped bool) { + f.writeConfig("1gb") + framework.ExpectNoError( + os.WriteFile( + filepath.Join(f.workspace, safeRecreateSource), + []byte(safeRecreateMarker), + 0o600, + ), + ) + f.source = snapshotSafeRecreateFile(ctx, filepath.Join(f.workspace, safeRecreateSource)) + framework.ExpectNoError( + f.framework.DevsyUp(ctx, append([]string{f.workspace}, f.upArgs()...)...), + ) + workspace, err := f.framework.FindWorkspace(ctx, f.workspace) + framework.ExpectNoError(err) + gomega.Expect(workspace.UID).To(gomega.Equal(f.uid)) + gomega.Expect(microsandboxResourceSandbox(ctx, f.framework, f.workspace)). + To(gomega.Equal(f.sandbox)) + f.workspaceInfo = workspace + out, err := safeRecreateExec(ctx, f.sandbox, "sh", "-c", + `printf '%s' "$1" > "$2"; printf '%s' "$1" > "$3"`, "sh", safeRecreateMarker, + safeRecreateRootPath, safeRecreateVolumeDir+"/marker") + framework.ExpectNoError(err, string(out)) + f.bootID = microsandboxStorageBootID(ctx, f.framework, f.workspace) + gomega.Expect(microsandboxActiveResources(ctx, f.sandbox)). + To(gomega.Equal(microsandboxResources{ + CPUs: 1, MemoryMiB: 1024, MaxCPUs: 1, MaxMemoryMiB: 1536, + })) + if stopped { + framework.ExpectNoError(f.framework.DevsyWorkspaceStop(ctx, f.workspace)) + } + f.original = inspectSafeRecreate(ctx, f.sandbox) + gomega.Expect(f.original.CreatedAt).NotTo(gomega.BeEmpty()) + gomega.Expect(f.original.Status). + To(gomega.Equal(map[bool]string{false: "Running", true: "Stopped"}[stopped])) + f.record = snapshotSafeRecreateRecord(workspace) + f.sshFiles = snapshotSafeRecreateSSH(ctx, workspace) +} + +func (f *safeRecreateFixture) rejectInvalidReplacement(ctx context.Context) { + f.writeConfig("2gb") + stdout, stderr, err := f.framework.DevsyUpStreams( + ctx, + f.workspace, + append(f.upArgs(), "--recreate")...) + gomega.Expect(err).To(gomega.HaveOccurred(), "stdout: %s stderr: %s", stdout, stderr) + gomega.Expect(stdout + stderr).To(gomega.ContainSubstring(safeRecreateMemoryErr)) + // Inspect status before any SSH/start, especially for an originally stopped VM. + gomega.Expect(inspectSafeRecreate(ctx, f.sandbox)).To(gomega.Equal(f.original)) + f.assertHostState(ctx) +} + +func (f *safeRecreateFixture) assertHostState(ctx context.Context) { + current, err := f.framework.FindWorkspace(ctx, f.workspace) + framework.ExpectNoError(err) + gomega.Expect(current.ID).To(gomega.Equal(f.workspaceInfo.ID)) + gomega.Expect(current.UID).To(gomega.Equal(f.workspaceInfo.UID)) + gomega.Expect(current.Provider).To(gomega.Equal(f.workspaceInfo.Provider)) + gomega.Expect(current.CreationTimestamp).To(gomega.Equal(f.workspaceInfo.CreationTimestamp)) + gomega.Expect(snapshotSafeRecreateRecord(current)).To(gomega.Equal(f.record)) + gomega.Expect(snapshotSafeRecreateSSH(ctx, current)).To(gomega.Equal(f.sshFiles)) + gomega.Expect(snapshotSafeRecreateFile(ctx, filepath.Join(f.workspace, safeRecreateSource))). + To(gomega.Equal(f.source)) +} + +func (f *safeRecreateFixture) inspectOriginalFiles(ctx context.Context, stopped bool) { + f.writeConfig("1gb") + if stopped { + // The preceding refusal assertion already proved the old VM remained stopped. + framework.ExpectNoError( + f.framework.DevsyUp(ctx, append([]string{f.workspace}, f.upArgs()...)...), + ) + gomega.Expect(inspectSafeRecreate(ctx, f.sandbox).CreatedAt). + To(gomega.Equal(f.original.CreatedAt)) + gomega.Expect(microsandboxStorageBootID(ctx, f.framework, f.workspace)). + NotTo(gomega.Equal(f.bootID)) + f.bootID = microsandboxStorageBootID(ctx, f.framework, f.workspace) + } else { + gomega.Expect(microsandboxStorageBootID(ctx, f.framework, f.workspace)). + To(gomega.Equal(f.bootID)) + } + out, err := safeRecreateExec(ctx, f.sandbox, "cat", safeRecreateRootPath) + framework.ExpectNoError(err) + gomega.Expect(string(out)).To(gomega.Equal(safeRecreateMarker)) + f.assertPersistentMounts(ctx) + f.assertHostState(ctx) +} + +func (f *safeRecreateFixture) assertPersistentMounts(ctx context.Context) { + for _, path := range []string{ + safeRecreateVolumeDir + "/marker", + filepath.ToSlash(filepath.Join("/workspaces", filepath.Base(f.workspace), safeRecreateSource)), + } { + out, err := safeRecreateExec(ctx, f.sandbox, "cat", path) + framework.ExpectNoError(err) + gomega.Expect(string(out)).To(gomega.Equal(safeRecreateMarker)) + } +} + +func (f *safeRecreateFixture) replaceSuccessfully(ctx context.Context) { + f.writeConfig("1536mb") + framework.ExpectNoError( + f.framework.DevsyUpRecreate(ctx, append([]string{f.workspace}, f.upArgs()...)...), + ) + after := inspectSafeRecreate(ctx, f.sandbox) + gomega.Expect(after.Status).To(gomega.Equal("Running")) + gomega.Expect(after.CreatedAt).NotTo(gomega.Equal(f.original.CreatedAt)) + gomega.Expect(microsandboxStorageBootID(ctx, f.framework, f.workspace)). + NotTo(gomega.Equal(f.bootID)) + out, err := safeRecreateExec( + ctx, + f.sandbox, + "sh", + "-c", + `test ! -e "$1"`, + "sh", + safeRecreateRootPath, + ) + framework.ExpectNoError(err, string(out)) + f.assertPersistentMounts(ctx) + f.assertHostState(ctx) + expected := microsandboxResources{CPUs: 1, MemoryMiB: 1536, MaxCPUs: 1, MaxMemoryMiB: 1536} + gomega.Expect(microsandboxActiveResources(ctx, f.sandbox)).To(gomega.Equal(expected)) + assertMicrosandboxGuestResources(ctx, f.framework, f.workspace, expected) +} + +func inspectSafeRecreate(ctx context.Context, sandbox string) safeRecreateInspection { + out, err := microsandboxResourceCommand( + ctx, + "inspect", + sandbox, + "--format", + microsandboxResourceJSON, + ) + framework.ExpectNoError(err, string(out)) + var inspection safeRecreateInspection + framework.ExpectNoError(json.Unmarshal(out, &inspection)) + return inspection +} + +func safeRecreateExec(ctx context.Context, sandbox string, argv ...string) ([]byte, error) { + args := append( + []string{"exec", "--stream", "--user", microsandboxRootUser, sandbox, "--"}, + argv...) + // #nosec G204 -- Literal commands target only this fixture's exact owned VM. + command := exec.CommandContext(ctx, "msb", args...) + command.WaitDelay = 30 * time.Second + return command.CombinedOutput() +} + +func snapshotSafeRecreateFile(ctx context.Context, path string) safeRecreateFile { + // #nosec G304 -- Source/SSH path is inside this fixture's isolated HOME or workspace. + data, err := os.ReadFile(path) + framework.ExpectNoError(err) + info, err := os.Stat(path) + framework.ExpectNoError(err) + return safeRecreateFile{ + Data: data, + Mode: info.Mode().Perm(), + Owner: microsandboxHostOwner(ctx, path), + } +} + +func snapshotSafeRecreateRecord(workspace *providerpkg.Workspace) []byte { + dir, err := providerpkg.GetWorkspaceDir(workspace.Context, workspace.ID) + framework.ExpectNoError(err) + // #nosec G304 -- Persisted record belongs to the fixture's owned workspace. + data, err := os.ReadFile(filepath.Join(dir, providerpkg.WorkspaceConfigFile)) + framework.ExpectNoError(err) + var record map[string]json.RawMessage + framework.ExpectNoError(json.Unmarshal(data, &record)) + delete(record, "lastUsed") + data, err = json.Marshal(record) + framework.ExpectNoError(err) + return data +} + +func snapshotSafeRecreateSSH( + ctx context.Context, + workspace *providerpkg.Workspace, +) map[string]safeRecreateFile { + paths := []string{workspace.SSHConfigPath} + if workspace.SSHConfigIncludePath != "" { + paths = append(paths, workspace.SSHConfigIncludePath) + } + result := map[string]safeRecreateFile{} + for _, raw := range paths { + path, err := sshconfig.ResolveSSHConfigPath(raw) + framework.ExpectNoError(err) + result[path] = snapshotSafeRecreateFile(ctx, path) + } + return result +} diff --git a/go.mod b/go.mod index 51a67c52fc..35c3e737c9 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,7 @@ require ( github.com/devsy-org/agentapi v1.0.1 github.com/devsy-org/api v1.1.0 github.com/devsy-org/apiserver v1.5.4 - github.com/devsy-org/devsy-runtime-sdk v1.5.2 + github.com/devsy-org/devsy-runtime-sdk v1.6.0 github.com/devsy-org/ssh v1.2.9 github.com/distribution/reference v0.6.0 github.com/docker/cli v29.8.0+incompatible diff --git a/go.sum b/go.sum index 0ab3c4c089..1976099640 100644 --- a/go.sum +++ b/go.sum @@ -416,8 +416,8 @@ github.com/devsy-org/api v1.1.0 h1:l7T9k7RVwatwN4lxeDTF3iN6EYmfGZgR3ZTJMDGha1M= github.com/devsy-org/api v1.1.0/go.mod h1:mAZklKdnywJYiXDReBLte/H+3m69z6G7RHB3n1lI53Q= github.com/devsy-org/apiserver v1.5.4 h1:/bEPrSRSlfii2QHxc9qAiXlQawNJfIaPLraR3bYKHxg= github.com/devsy-org/apiserver v1.5.4/go.mod h1:sDFCTjCN13wAHhno4KX0Z756gCI8ttW/c7NwTzo16K0= -github.com/devsy-org/devsy-runtime-sdk v1.5.2 h1:nPQ3HyIrEgqlAU/a7bvFovm+VB9cdryUrejKKX0O2LQ= -github.com/devsy-org/devsy-runtime-sdk v1.5.2/go.mod h1:MiBP/fiY83DAS0TueEiJZSHYZ1vq85UR4KJTMfHIM+E= +github.com/devsy-org/devsy-runtime-sdk v1.6.0 h1:iEV4Y0Sc3LJNkTLadJSKcWcAf/oCXvC/AkHilrg/KyM= +github.com/devsy-org/devsy-runtime-sdk v1.6.0/go.mod h1:MiBP/fiY83DAS0TueEiJZSHYZ1vq85UR4KJTMfHIM+E= github.com/devsy-org/ssh v1.2.9 h1:KHqX1xAplGFanm0FMSAojtiC9nV/UFxUeP/5jU5quak= github.com/devsy-org/ssh v1.2.9/go.mod h1:Uff10+cSSDZk3bG07u5D9+eQ8GMGqsgE70WCUJWcHv4= github.com/devsy-org/tailscale v1.102.2 h1:9SB6htvO+HmG8alal8WGCshHapfHR7dUFRSMYiFIzIM= diff --git a/pkg/driver/external/capabilities.go b/pkg/driver/external/capabilities.go index bab90304a8..0f80e1da54 100644 --- a/pkg/driver/external/capabilities.go +++ b/pkg/driver/external/capabilities.go @@ -36,10 +36,14 @@ func (h *Host) SupportsReusePreflight() bool { } func (h *Host) RecreateMode() driver.RecreateMode { - if h.info.Capabilities.RecreateMode == runtimev1.RecreateMode_RECREATE_MODE_DELETE { + switch h.info.Capabilities.RecreateMode { + case runtimev1.RecreateMode_RECREATE_MODE_DELETE: return driver.RecreateDelete + case runtimev1.RecreateMode_RECREATE_MODE_ON_RUN: + return driver.RecreateOnRun + default: + return driver.RecreateStop } - return driver.RecreateStop } func mountType(kind string) (runtimev1.MountType, bool) { diff --git a/pkg/driver/external/host.go b/pkg/driver/external/host.go index 1ee2e367d4..65dcc53df0 100644 --- a/pkg/driver/external/host.go +++ b/pkg/driver/external/host.go @@ -105,7 +105,7 @@ func newHost(ctx context.Context, options hostOptions) (*Host, error) { infoContext, cancel := context.WithTimeout(ctx, options.timeout) defer cancel() err := h.call(infoContext, "Info", func(client runtimev1.RuntimeDriverClient) error { - info, err := client.Info(infoContext, &runtimev1.InfoRequest{}) + info, err := client.Info(infoContext, &runtimev1.InfoRequest{SupportsRecreateOnRun: true}) if err != nil { return err } diff --git a/pkg/driver/external/internal/testfixture/main.go b/pkg/driver/external/internal/testfixture/main.go index c020889662..00b6f33f57 100644 --- a/pkg/driver/external/internal/testfixture/main.go +++ b/pkg/driver/external/internal/testfixture/main.go @@ -49,10 +49,13 @@ func serve(mode, state string, delay time.Duration) { fixtureMode := mode if fixtureMode == "blocked" || fixtureMode == "environment" || fixtureMode == "block-info" || fixtureMode == "env-error" || fixtureMode == "workspace-identity" || + fixtureMode == "recreate-on-run" || strings.HasPrefix(fixtureMode, "stream-") { fixtureMode = fake.Normal } - runtime, err := fake.New(fake.Config{StateDir: state, Mode: fixtureMode}) + runtime, err := fake.New(fake.Config{ + StateDir: state, Mode: fixtureMode, RecreateOnRun: mode == "recreate-on-run", + }) if err != nil { panic(err) } diff --git a/pkg/driver/external/recreate_test.go b/pkg/driver/external/recreate_test.go new file mode 100644 index 0000000000..e9d0f5c36d --- /dev/null +++ b/pkg/driver/external/recreate_test.go @@ -0,0 +1,46 @@ +package external + +import ( + "context" + "time" + + "github.com/devsy-org/devsy-runtime-sdk/runtimev1" + "github.com/devsy-org/devsy/pkg/driver" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +func (s *HostSuite) TestNegotiatedRecreationAcrossFreshProcesses() { + host := s.host("recreate-on-run") + s.Equal(driver.RecreateOnRun, host.RecreateMode()) + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + params := &driver.RunImageDevContainerParams{ + WorkspaceID: fixtureWorkspace, + Options: &driver.RunOptions{ + Image: fixtureImage, Labels: []string{"generation=original"}, + }, + } + s.Require().NoError(host.RunImageDevContainer(ctx, params)) + original, err := host.FindDevContainer(ctx, fixtureWorkspace) + s.Require().NoError(err) + s.Require().NotNil(original) + err = host.RunImage( + ctx, + &runtimev1.RunImageRequest{WorkspaceId: fixtureWorkspace, Image: fixtureImage}, + ) + s.Equal(codes.AlreadyExists, status.Code(err)) + params.Options.AllowRecreate = true + params.Options.Labels = []string{"generation=replaced"} + s.Require().NoError(host.RunImageDevContainer(ctx, params)) + replaced, err := host.FindDevContainer(ctx, fixtureWorkspace) + s.Require().NoError(err) + s.Require().NotNil(replaced) + s.Equal(original.ID, replaced.ID) + s.Equal("replaced", replaced.Config.Labels["generation"]) + params.Options.AllowRecreate = false + s.Equal(codes.AlreadyExists, status.Code(host.RunImageDevContainer(ctx, params))) + again, err := host.FindDevContainer(ctx, fixtureWorkspace) + s.Require().NoError(err) + s.Equal(replaced, again) +} diff --git a/pkg/driver/external/run_image.go b/pkg/driver/external/run_image.go index 73ddcdc733..9d2539a319 100644 --- a/pkg/driver/external/run_image.go +++ b/pkg/driver/external/run_image.go @@ -55,6 +55,7 @@ func runImageRequest( User: options.User, RemoteUser: options.RemoteUser, Dockerless: options.Dockerless, + AllowRecreate: options.AllowRecreate, Entrypoint: options.Entrypoint, Args: slices.Clone(options.Cmd), Environment: maps.Clone(options.Env), diff --git a/pkg/driver/external/run_image_test.go b/pkg/driver/external/run_image_test.go index 134e340058..384b893386 100644 --- a/pkg/driver/external/run_image_test.go +++ b/pkg/driver/external/run_image_test.go @@ -212,6 +212,7 @@ func (s *ImageRunSuite) TestNegotiatedCapabilities() { for _, mode := range []runtimev1.RecreateMode{ runtimev1.RecreateMode_RECREATE_MODE_DELETE, runtimev1.RecreateMode_RECREATE_MODE_STOP, + runtimev1.RecreateMode_RECREATE_MODE_ON_RUN, } { h := &Host{ info: &runtimev1.InfoResponse{ @@ -233,7 +234,20 @@ func (s *ImageRunSuite) TestNegotiatedCapabilities() { want := driver.RecreateStop if mode == runtimev1.RecreateMode_RECREATE_MODE_DELETE { want = driver.RecreateDelete + } else if mode == runtimev1.RecreateMode_RECREATE_MODE_ON_RUN { + want = driver.RecreateOnRun } s.Equal(want, driver.DriverRecreateMode(h)) } } + +func (s *ImageRunSuite) TestReplacementPermissionForwardingIsRequestLocal() { + for _, allowed := range []bool{false, true, false} { + request, err := runImageRequest(&driver.RunImageDevContainerParams{ + WorkspaceID: fixtureWorkspace, + Options: &driver.RunOptions{Image: imageRunImage, AllowRecreate: allowed}, + }) + s.Require().NoError(err) + s.Equal(allowed, request.GetAllowRecreate()) + } +} diff --git a/pkg/driver/microsandbox/cliclient.go b/pkg/driver/microsandbox/cliclient.go index 4c0230b314..24d63fbcbe 100644 --- a/pkg/driver/microsandbox/cliclient.go +++ b/pkg/driver/microsandbox/cliclient.go @@ -202,6 +202,9 @@ const ( // plus argv overrides the image CMD. func runArgs(sandbox string, spec sandboxSpec) []string { args := []string{msbCmdRun, msbFlagDetach, names.Flag(names.Name), sandbox} + if spec.AllowRecreate { + args = append(args, "--replace") + } if spec.Entrypoint != "" { args = append(args, "--entrypoint", spec.Entrypoint) } diff --git a/pkg/driver/microsandbox/client.go b/pkg/driver/microsandbox/client.go index 6fa3be4f9b..b825468b33 100644 --- a/pkg/driver/microsandbox/client.go +++ b/pkg/driver/microsandbox/client.go @@ -16,21 +16,22 @@ type preparedImage struct { } type sandboxSpec struct { - Image string - User string - Entrypoint string - Cmd []string - Memory uint32 - CPUs uint8 - Env map[string]string - Labels map[string]string - Ephemeral bool - IdleTimeout time.Duration - Mounts []volumeMount - MaxMemory uint32 - MaxCPUs uint8 - BlockEgress bool - RootDiskGB uint32 + AllowRecreate bool + Image string + User string + Entrypoint string + Cmd []string + Memory uint32 + CPUs uint8 + Env map[string]string + Labels map[string]string + Ephemeral bool + IdleTimeout time.Duration + Mounts []volumeMount + MaxMemory uint32 + MaxCPUs uint8 + BlockEgress bool + RootDiskGB uint32 } type volumeMount struct { diff --git a/pkg/driver/microsandbox/image_accounts_test.go b/pkg/driver/microsandbox/image_accounts_test.go index 53749a5bde..a9500fba2a 100644 --- a/pkg/driver/microsandbox/image_accounts_test.go +++ b/pkg/driver/microsandbox/image_accounts_test.go @@ -112,6 +112,7 @@ func (s *imageUserSuite) TestInvalidFinalAccountPreservesExistingSandbox() { }), ) client := &accountImageClient{fakeClient: newFakeClient(), image: image} + client.version = recreateTestVersion client.info[wsName] = &sandboxInfo{Name: wsName, Running: true} d := newDriver(client, nil, specDefaults{}) err := d.RunDevContainer(context.Background(), wsID, &driver.RunOptions{ diff --git a/pkg/driver/microsandbox/microsandbox.go b/pkg/driver/microsandbox/microsandbox.go index 15201519cf..c58ee0e68d 100644 --- a/pkg/driver/microsandbox/microsandbox.go +++ b/pkg/driver/microsandbox/microsandbox.go @@ -93,7 +93,7 @@ func (d *microsandboxDriver) Preflight(ctx context.Context, _ driver.PreflightOp } // CanReprovision returns false: in-place reprovision passes nil options, which -// a microVM cannot rebuild from. --recreate still works via delete+create. +// a microVM cannot rebuild from. Explicit recreation uses runtime replacement. func (d *microsandboxDriver) CanReprovision() bool { return false } @@ -359,6 +359,11 @@ func (d *microsandboxDriver) runFromOptions( if err := d.ProvisioningPreflight(ctx); err != nil { return err } + if options.AllowRecreate { + if err := d.recreationPreflight(ctx); err != nil { + return err + } + } prepared, err := d.client.PrepareImage(ctx, options.Image, options.ImageBuilt) if err != nil { return fmt.Errorf("prepare microsandbox final image: %w", err) @@ -383,13 +388,11 @@ func (d *microsandboxDriver) runFromOptions( ) } spec := d.buildSpec(workspaceID, options, hostReqs, owner) + spec.AllowRecreate = options.AllowRecreate spec.Image = prepared.reference if err := d.client.PrepareVolumes(ctx, spec.Mounts); err != nil { return fmt.Errorf("prepare microsandbox volumes: %w", err) } - if err := d.deleteSandbox(ctx, sandboxName(workspaceID), info); err != nil { - return fmt.Errorf("clean stale microsandbox VM before create: %w", err) - } if err := d.client.Create( ctx, sandboxName(workspaceID), diff --git a/pkg/driver/microsandbox/microsandbox_test.go b/pkg/driver/microsandbox/microsandbox_test.go index 6e9823b5bc..36bef06113 100644 --- a/pkg/driver/microsandbox/microsandbox_test.go +++ b/pkg/driver/microsandbox/microsandbox_test.go @@ -189,6 +189,7 @@ func TestRunDevContainerBuildsSpec(t *testing.T) { func TestRunDevContainerReplacesStaleSandbox(t *testing.T) { f := newFakeClient() + f.version = recreateTestVersion f.info[wsName] = &sandboxInfo{Name: wsName, Running: true} d := newDriver(f, nil, specDefaults{}) @@ -203,13 +204,12 @@ func TestRunDevContainerReplacesStaleSandbox(t *testing.T) { want := []string{ callEnsure, callFind, - "stop:devsy-ws1", - callRemove, "create:devsy-ws1", } if !slices.Equal(f.calls, want) { t.Errorf("call order = %v, want %v", f.calls, want) } + require.True(t, f.created[wsName].AllowRecreate) } func TestRunDevContainerRejectsOldRuntimeBeforeMutation(t *testing.T) { @@ -773,6 +773,7 @@ func TestReplacementRequiresConsentAndSuccessfulPreparation(t *testing.T) { } { t.Run(fmt.Sprintf("running=%t/%s", running, tc.name), func(t *testing.T) { f := newFakeClient() + f.version = recreateTestVersion original := &sandboxInfo{Name: wsName, Running: running, CreatedAt: time.Now()} f.info[wsName] = original f.failPrepare, f.failVolumes = tc.imageErr, tc.volumesErr diff --git a/pkg/driver/microsandbox/recreate.go b/pkg/driver/microsandbox/recreate.go new file mode 100644 index 0000000000..dc27a5f595 --- /dev/null +++ b/pkg/driver/microsandbox/recreate.go @@ -0,0 +1,35 @@ +package microsandbox + +import ( + "context" + "fmt" + + "github.com/blang/semver/v4" + "github.com/devsy-org/devsy/pkg/driver" + "github.com/devsy-org/devsy/pkg/provider" +) + +var minimumMicrosandboxRecreateVersion = semver.MustParse("0.7.7") + +// Replacement relies on the supported runtime's final validation before teardown, +// independently of the ordinary ownership policy and its provisioning minimum. +func (d *microsandboxDriver) recreationPreflight(ctx context.Context) error { + if err := ctx.Err(); err != nil { + return err + } + raw, err := d.client.Version(ctx) + if err != nil { + return &driver.PreflightError{Provider: provider.MicrosandboxDriver, Err: err} + } + version, err := parseMicrosandboxVersion(raw) + if err != nil { + return &driver.PreflightError{Provider: provider.MicrosandboxDriver, Err: err} + } + if version.LT(minimumMicrosandboxRecreateVersion) { + return &driver.PreflightError{Provider: provider.MicrosandboxDriver, Err: fmt.Errorf( + "MicroSandbox v%s or newer is required for explicit recreation; update with msb self update", + minimumMicrosandboxRecreateVersion, + )} + } + return nil +} diff --git a/pkg/driver/microsandbox/recreate_cli_test.go b/pkg/driver/microsandbox/recreate_cli_test.go new file mode 100644 index 0000000000..6e675d3b53 --- /dev/null +++ b/pkg/driver/microsandbox/recreate_cli_test.go @@ -0,0 +1,138 @@ +package microsandbox + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "runtime" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +const ( + recreateCLIBaseName = "msb" + recreateCLIFixtureEnv = "DEVSY_TEST_MSB_CREATE_FIXTURE" + recreateCLIFixtureMode = "capture-argv" + recreateCLIArgumentFile = "create-argv.json" +) + +func TestMain(m *testing.M) { + if os.Getenv(recreateCLIFixtureEnv) == recreateCLIFixtureMode { + if handled, err := captureRecreateCLIArguments(); handled { + if err != nil { + _, _ = fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + os.Exit(0) + } + } + os.Exit(m.Run()) +} + +// Only the test executable copied to an owned msb path enters fixture mode. +// Its fixed result file is adjacent to that copy; an environment value cannot +// select an arbitrary output path or divert the ordinary package test binary. +func captureRecreateCLIArguments() (bool, error) { + executable, err := os.Executable() + if err != nil { + return false, err + } + if filepath.Base(executable) != recreateCLIExecutableName() { + return false, nil + } + data, err := json.Marshal(os.Args[1:]) + if err != nil { + return true, err + } + path := filepath.Join(filepath.Dir(executable), recreateCLIArgumentFile) + // #nosec G703 -- Fixed result beside this fixture's private copied test executable. + return true, os.WriteFile(path, data, 0o600) +} + +func TestCLIClientCreateReplacementAuthorizationIsRequestLocal(t *testing.T) { + fixture := installRecreateCLIExecutable(t) + argumentPath := filepath.Join(filepath.Dir(fixture), recreateCLIArgumentFile) + const sandbox = "owned-create-argv" + spec := sandboxSpec{ + Image: "registry.invalid/owned/image:literal", + Entrypoint: "/owned/entry point", + Cmd: []string{ + "-c", `printf '%s' "$1"; : 'literal --replace $(not-a-command)'`, + "sh", "two words", "", `quotes'"\雪`, + }, + } + client := cliClient{} + ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second) + defer cancel() + for _, allowed := range []bool{false, true, false} { + removeRecreateCLIArguments(t, argumentPath) + spec.AllowRecreate = allowed + require.NoError(t, client.Create(ctx, sandbox, spec)) + var actual []string + // #nosec G304 -- Fixed result file beside the owned executable copy. + data, err := os.ReadFile(argumentPath) + require.NoError(t, err) + require.NoError(t, json.Unmarshal(data, &actual)) + want := []string{"run", "--detach", "--name", sandbox} + if allowed { + want = append(want, "--replace") + } + want = append(want, "--entrypoint", spec.Entrypoint, spec.Image, "--") + want = append(want, spec.Cmd...) + require.Equal(t, want, actual, "actual child argv with allow-recreate=%t", allowed) + t.Logf("allow-recreate=%t actual child argv=%q", allowed, actual) + } +} + +func recreateCLIExecutableName() string { + if runtime.GOOS == "windows" { + return recreateCLIBaseName + ".exe" + } + return recreateCLIBaseName +} + +func installRecreateCLIExecutable(t *testing.T) string { + t.Helper() + path := filepath.Join(t.TempDir(), recreateCLIExecutableName()) + require.NoError(t, copyRecreateCLIExecutable(path)) + t.Setenv(recreateCLIFixtureEnv, recreateCLIFixtureMode) + t.Setenv("PATH", filepath.Dir(path)+string(os.PathListSeparator)+os.Getenv("PATH")) + resolved, err := exec.LookPath(recreateCLIBaseName) + require.NoError(t, err) + require.Equal(t, path, resolved, "Create must execute only this owned fixture") + return path +} + +func copyRecreateCLIExecutable(path string) error { + executable, err := os.Executable() + if err != nil { + return err + } + // #nosec G304 -- Copy only the current test executable, never a supplied binary. + source, err := os.Open(executable) + if err != nil { + return err + } + // #nosec G304 G302 -- New executable copy is inside the test's private TempDir. + target, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o700) + if err != nil { + return errors.Join(err, source.Close()) + } + _, copyErr := io.Copy(target, source) + return errors.Join(copyErr, target.Close(), source.Close()) +} + +func removeRecreateCLIArguments(t *testing.T, path string) { + t.Helper() + err := os.Remove(path) + if !errors.Is(err, os.ErrNotExist) { + require.NoError(t, err) + } +} diff --git a/pkg/driver/microsandbox/recreate_preservation_test.go b/pkg/driver/microsandbox/recreate_preservation_test.go new file mode 100644 index 0000000000..8e263e1e8f --- /dev/null +++ b/pkg/driver/microsandbox/recreate_preservation_test.go @@ -0,0 +1,72 @@ +package microsandbox + +import ( + "context" + "errors" + "strings" + "testing" + "time" + + "github.com/devsy-org/devsy/pkg/driver" + "github.com/stretchr/testify/require" +) + +const recreateTestVersion = "0.7.7" + +func TestRejectedReplacementKeepsExistingSandbox(t *testing.T) { + for _, running := range []bool{false, true} { + t.Run(map[bool]string{false: "stopped", true: "running"}[running], func(t *testing.T) { + client := newFakeClient() + client.version = recreateTestVersion + original := &sandboxInfo{Name: wsName, Running: running, CreatedAt: time.Unix(123, 0)} + client.info[wsName] = original + rejected := errors.New("invalid final effective configuration") + client.failCreat = rejected + err := newDriver(client, nil, specDefaults{}).RunDevContainer(t.Context(), wsID, + &driver.RunOptions{Image: imgX, AllowRecreate: true}) + require.ErrorIs(t, err, rejected) + require.Same(t, original, client.info[wsName]) + require.Equal(t, running, original.Running) + for _, call := range client.calls { + require.False( + t, + strings.HasPrefix(call, "stop:") || strings.HasPrefix(call, "remove:"), + call, + ) + } + }) + } +} + +func TestRecreationVersionGateAppliesWithOwnershipOff(t *testing.T) { + for _, version := range []string{"0.7.2", "0.7.6", "0.7.7-rc.1", "unknown"} { + t.Run(version, func(t *testing.T) { + client := newFakeClient() + client.version = version + client.failPrepare = errors.New("image preparation must not run") + d := newDriver(client, nil, specDefaults{}) + d.workspaceMountPolicy.StatVirtualization = statVirtOff + err := d.RunDevContainer( + t.Context(), + wsID, + &driver.RunOptions{Image: imgX, AllowRecreate: true}, + ) + var preflight *driver.PreflightError + require.ErrorAs(t, err, &preflight) + require.NotErrorIs(t, err, client.failPrepare) + require.Empty(t, client.calls) + require.Empty(t, client.created) + }) + } +} + +func TestRecreationPreflightPreservesProbeFailureAndCancellation(t *testing.T) { + client := newFakeClient() + client.failVersion = errors.New("owned version probe failed") + d := newDriver(client, nil, specDefaults{}) + require.ErrorIs(t, d.recreationPreflight(t.Context()), client.failVersion) + ctx, cancel := context.WithCancel(t.Context()) + cancel() + require.ErrorIs(t, d.recreationPreflight(ctx), context.Canceled) + require.Equal(t, 1, client.versionCalls) +} diff --git a/pkg/driver/microsandbox/workspace_owner_test.go b/pkg/driver/microsandbox/workspace_owner_test.go index d4f13609fc..6e322203c3 100644 --- a/pkg/driver/microsandbox/workspace_owner_test.go +++ b/pkg/driver/microsandbox/workspace_owner_test.go @@ -173,6 +173,7 @@ func TestDockerlessOwnerNeverLooksUpRunnerAccounts(t *testing.T) { for _, user := range []string{testUser, "1000", testNumericIdentity, rootUser} { t.Run(user, func(t *testing.T) { f := newFakeClient() + f.version = recreateTestVersion f.info[wsName] = &sandboxInfo{Name: wsName, Running: true} d := newDriver(f, nil, specDefaults{}) resolver := &recordingUserResolver{err: errors.New("runner accounts must not be read")} diff --git a/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx b/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx index e2e470e2f7..895d6b6c19 100644 --- a/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx @@ -7,7 +7,7 @@ description: Contract for external runtime driver authors using the Devsy Runtim Providers select this protocol with `agent.driver: external`. See the [external driver](./driver.mdx#external-driver). -The canonical [protobuf schema](https://github.com/devsy-org/devsy-runtime-sdk/blob/main/proto/devsy/runtime/v1/runtime.proto) is maintained in the [Devsy Runtime SDK](https://github.com/devsy-org/devsy-runtime-sdk). The module path is `github.com/devsy-org/devsy-runtime-sdk`; the logical plugin name is `devsy-runtime`. HashiCorp application protocol 1 and Info API major 1/minor 2 are separate version checks. Same-major newer minor versions are accepted. Unknown mount/recreate enum values are rejected because they control host behavior. +The canonical [protobuf schema](https://github.com/devsy-org/devsy-runtime-sdk/blob/main/proto/devsy/runtime/v1/runtime.proto) is maintained in the [Devsy Runtime SDK](https://github.com/devsy-org/devsy-runtime-sdk). The module path is `github.com/devsy-org/devsy-runtime-sdk`; the logical plugin name is `devsy-runtime`. HashiCorp application protocol 1 and Info API major 1/minor 3 are separate version checks. Same-major newer minor versions are accepted. Unknown mount/recreate enum values are rejected because they control host behavior. ## Runtime boundary @@ -17,7 +17,7 @@ RunImage receives resolved intent. Its empty response acknowledges completion; F `remote_user` carries the developer identity used for workspace ownership, separately from the container process `user`. The host forwards both values without substituting one for the other. Runtimes use `remote_user` when set, otherwise `user`, otherwise `root` for workspace ownership. `dockerless` indicates that the host will build the developer filesystem after the image starts, so that identity may not yet exist in the image. A runtime that resolves mount ownership from image contents must validate this case before changing workspace resources. These fields describe provisioning intent and do not authorize replacement of an existing workspace. -Runtime name, driver name/version, and capabilities are required in Info. Runtime version may be empty when a backend cannot report it without expensive setup. An empty mount list means no supported mount types. ProvisioningPreflight may be a no-op when its capability is false. Logs may return Unimplemented when its capability is false. Reprovision means RunImage can update an existing workspace with complete resolved intent; it does not imply that an empty request is safe. The Devsy host does not enable in-place reprovisioning; workspace changes follow the negotiated stop/delete recreate policy. +Runtime name, driver name/version, and capabilities are required in Info. Runtime version may be empty when a backend cannot report it without expensive setup. An empty mount list means no supported mount types. ProvisioningPreflight may be a no-op when its capability is false. Logs may return Unimplemented when its capability is false. Reprovision means RunImage can update an existing workspace with complete resolved intent; it does not imply that an empty request is safe. The Devsy host does not enable in-place reprovisioning; workspace changes follow the negotiated recreation policy. TargetArchitecture returns canonical `amd64` or `arm64`. A runtime may require an existing workspace to answer; hosts must not require pre-start architecture discovery from such runtimes. @@ -27,7 +27,13 @@ Find returns `found=false` for ordinary absence, without a NotFound RPC error. A Start on an already running workspace succeeds; missing returns NotFound. Stop on an already stopped workspace succeeds; missing may return NotFound. Delete normalizes missing state to success for cleanup. Provisioning compatibility checks must precede destructive teardown. -API 1.2 adds optional `capabilities.reuse_preflight`. Before reusing a workspace, the host calls ReusePreflight with its workspace ID and current resolved developer identity. The runtime validates its own creation-time contract, such as mount policy or ownership, without starting, stopping, deleting, or modifying the workspace. An incompatible contract returns structured FailedPrecondition with explicit `--recreate` guidance. The host propagates that error without scheduling replacement, preserving the existing VM. Missing workspaces return NotFound; backend, permission, and context failures remain errors. When the capability is absent, hosts skip the RPC and retain their existing identity-resolution behavior. Explicit recreation follows the separate provisioning checks and negotiated stop/delete policy. +API 1.2 adds optional `capabilities.reuse_preflight`. Before reusing a workspace, the host calls ReusePreflight with its workspace ID and current resolved developer identity. The runtime validates its own creation-time contract, such as mount policy or ownership, without starting, stopping, deleting, or modifying the workspace. An incompatible contract returns structured FailedPrecondition with explicit `--recreate` guidance. The host propagates that error without scheduling replacement, preserving the existing VM. Missing workspaces return NotFound; backend, permission, and context failures remain errors. When the capability is absent, hosts skip the RPC and retain their existing identity-resolution behavior. Explicit recreation follows separate provisioning checks and the negotiated recreation policy. + +API 1.3 adds negotiated recreation during RunImage. A host opts in with `InfoRequest.supports_recreate_on_run`; a supporting runtime responds with `RECREATE_MODE_ON_RUN`. Devsy then leaves the existing VM in place and forwards the current operation's explicit recreation permission as `RunImageRequest.allow_recreate`. Negotiation alone grants no replacement permission, and a later request with false remains false. Existing explicit reset and rebuild operations also grant recreation permission. Automatic identity or secret migrations must request consent instead of silently enabling replacement. + +Older hosts and providers retain their negotiated DELETE or STOP behavior. ON_RUN does not enable in-place reprovisioning: it delegates replacement to the runtime so final effective configuration can be validated before teardown. + +For MicroSandbox, explicit replacement requires v0.7.7 or newer even with workspace permission policy `off`. The built-in and supporting external driver pass `--replace` to that operation instead of stopping and removing the old VM first. The runtime validates the effective configuration, including managed overrides, before removing the target. This preserves the old VM when configuration is rejected; it does not roll back failures after replacement begins. Image preparation, cache imports or new volumes can precede final validation. Root preparation, launch failures, cancellation or crashes after teardown can still lose the old VM. ## Exec and Logs @@ -119,6 +125,8 @@ This suite requires Linux with KVM and public access to the base and Dockerless DEVSY_REQUIRE_MICROSANDBOX=true task cli:test:e2e:suite -- up-provider-microsandbox-dockerless ``` +The `up-provider-microsandbox-recreate` label tests rejected explicit replacement for running and stopped VMs against both providers. Each case checks the unchanged VM generation and active configuration before resuming a stopped original, then checks persistent root, bind and named-volume data together with the workspace and SSH records. A subsequent valid replacement must change the VM generation, reset the root marker and preserve the external data. These request-invalid cases do not establish preservation for managed-policy conflicts or failures after replacement begins. Each scenario has a ten-minute deadline, with a 20-minute CI test deadline and a 25-minute job deadline. + These scenarios do not establish complete parity. Logs, cancellation, and runtime compatibility failures still require coverage before replacing the built-in provider. Green lifecycle, image, mount, resource, persistent-storage, ephemeral-root, TCP-egress, prebuild, and Dockerless checks alone do not authorize that cutover. Dockerless refusal checks cover fresh creation, not preservation after an invalid recreation request. Egress coverage does not establish UDP, DNS, TLS, ingress, or live-policy modification behavior. Prebuild invalidation coverage changes the Dockerfile; it does not independently establish hashing of local Feature script contents. From cc21822a24b1eb2a8a659012f9628d64679f84ee Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 13 Oct 2026 00:33:40 -0600 Subject: [PATCH 2/7] test(runtime): pin published MicroSandbox provider 0.1.8 --- e2e/tests/up/provider_microsandbox.go | 4 ++-- .../content/docs/developing-providers/runtime-protocol.mdx | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/e2e/tests/up/provider_microsandbox.go b/e2e/tests/up/provider_microsandbox.go index 2b671575db..cff5cb0f4a 100644 --- a/e2e/tests/up/provider_microsandbox.go +++ b/e2e/tests/up/provider_microsandbox.go @@ -23,7 +23,7 @@ import ( const ( osLinux = "linux" - microsandboxExternalProvider = "github.com/devsy-org/devsy-provider-microsandbox@v0.1.7" + microsandboxExternalProvider = "github.com/devsy-org/devsy-provider-microsandbox@v0.1.8" microsandboxRootUser = "root" ) @@ -213,7 +213,7 @@ var _ = ginkgo.Describe( ginkgo.Entry("built-in", "microsandbox", "microsandbox-builtin-parity", ginkgo.SpecTimeout(framework.TimeoutLong())), ginkgo.Entry( - "external v0.1.7", + "external v0.1.8", microsandboxExternalProvider, "microsandbox-external-parity", ginkgo.SpecTimeout(framework.TimeoutLong()), diff --git a/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx b/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx index 895d6b6c19..d0737b188d 100644 --- a/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx @@ -53,7 +53,7 @@ For SDK usage and development commands, see the [SDK README](https://github.com/ ## MicroSandbox parity gate -The `up-provider-microsandbox` E2E label runs the same lifecycle and ownership scenario against the built-in provider and the external v0.1.7 release, each with isolated Devsy configuration. CI pins MicroSandbox v0.7.7 by checksum and requires access to KVM; unavailable virtualization fails this job instead of producing a passing skipped test. Each provider scenario has a ten-minute deadline and the CI job has a 25-minute deadline. +The `up-provider-microsandbox` E2E label runs the same lifecycle and ownership scenario against the built-in provider and the external v0.1.8 release, each with isolated Devsy configuration. CI pins MicroSandbox v0.7.7 by checksum and requires access to KVM; unavailable virtualization fails this job instead of producing a passing skipped test. Each provider scenario has a ten-minute deadline and the CI job has a 25-minute deadline. The shared scenario exercises agent delivery, SSH, a 1 MiB binary stdin/stdout round trip with separate stderr and a nonzero guest exit, root workload versus developer identity, bind-mount ownership and mode mirroring, stop/start, recreation, rejection of an identity change without recreation while preserving VM-local data, and deletion of the VM. From 9e4b615e0438f37b29622150626c54d0b9b3f856 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 13 Oct 2026 02:26:44 -0600 Subject: [PATCH 3/7] test(runtime): preserve replacements rejected by managed policy --- .github/workflows/pr-ci.yml | 42 ++- e2e/tests/up/managed_policy_fixture_linux.go | 280 ++++++++++++++++++ ...der_microsandbox_managed_recreate_linux.go | 155 ++++++++++ .../up/provider_microsandbox_recreate.go | 28 +- hack/run-managed-policy-e2e.sh | 96 ++++++ .../developing-providers/runtime-protocol.mdx | 4 +- 6 files changed, 589 insertions(+), 16 deletions(-) create mode 100644 e2e/tests/up/managed_policy_fixture_linux.go create mode 100644 e2e/tests/up/provider_microsandbox_managed_recreate_linux.go create mode 100755 hack/run-managed-policy-e2e.sh diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index c663e33822..1020e59b09 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -732,6 +732,15 @@ jobs: test-timeout: 1200s job-timeout-minutes: 25 + - label: up-provider-microsandbox-managed-recreate + runner: ubuntu-latest + free-disk-space: false + install-kind: false + requires-secret: false + install-microsandbox: true + test-timeout: 1200s + job-timeout-minutes: 25 + - label: up-provider-microsandbox-logs-cancel runner: ubuntu-latest free-disk-space: false @@ -1176,15 +1185,30 @@ jobs: PATH="${PATH}" \ ./e2e.test -test.v -ginkgo.v -test.timeout "${TEST_TIMEOUT}" -ginkgo.fail-on-empty -ginkgo.flake-attempts="${FLAKE_ATTEMPTS}" --ginkgo.label-filter="${{ matrix.label }}" else - sudo \ - DEVSY_REQUIRE_MICROSANDBOX="${DEVSY_REQUIRE_MICROSANDBOX}" \ - GH_USERNAME="${GH_USERNAME}" \ - GH_ACCESS_TOKEN="${GH_ACCESS_TOKEN}" \ - GH_CREDENTIAL_USERNAME="${GH_CREDENTIAL_USERNAME}" \ - KUBECONFIG="${KUBECONFIG:-$HOME/.kube/config}" \ - ${DOCKER_HOST:+DOCKER_HOST="${DOCKER_HOST}"} \ - PATH="${PATH}" \ - ./e2e.test -test.v -ginkgo.v -test.timeout "${TEST_TIMEOUT}" -ginkgo.fail-on-empty -ginkgo.flake-attempts="${FLAKE_ATTEMPTS}" --ginkgo.label-filter="${{ matrix.label }}" + if [ "${{ matrix.label }}" = "up-provider-microsandbox-managed-recreate" ]; then + sudo \ + DEVSY_REQUIRE_MICROSANDBOX="${DEVSY_REQUIRE_MICROSANDBOX}" \ + DEVSY_REQUIRE_MSB_MANAGED_POLICY_FIXTURE="true" \ + GH_USERNAME="${GH_USERNAME}" \ + GH_ACCESS_TOKEN="${GH_ACCESS_TOKEN}" \ + GH_CREDENTIAL_USERNAME="${GH_CREDENTIAL_USERNAME}" \ + KUBECONFIG="${KUBECONFIG:-$HOME/.kube/config}" \ + ${DOCKER_HOST:+DOCKER_HOST="${DOCKER_HOST}"} \ + PATH="${PATH}" \ + RUNNER_TEMP="${RUNNER_TEMP}" \ + ../hack/run-managed-policy-e2e.sh \ + ./e2e.test -test.v -ginkgo.v -test.timeout "${TEST_TIMEOUT}" -ginkgo.fail-on-empty -ginkgo.flake-attempts="${FLAKE_ATTEMPTS}" --ginkgo.label-filter="${{ matrix.label }}" + else + sudo \ + DEVSY_REQUIRE_MICROSANDBOX="${DEVSY_REQUIRE_MICROSANDBOX}" \ + GH_USERNAME="${GH_USERNAME}" \ + GH_ACCESS_TOKEN="${GH_ACCESS_TOKEN}" \ + GH_CREDENTIAL_USERNAME="${GH_CREDENTIAL_USERNAME}" \ + KUBECONFIG="${KUBECONFIG:-$HOME/.kube/config}" \ + ${DOCKER_HOST:+DOCKER_HOST="${DOCKER_HOST}"} \ + PATH="${PATH}" \ + ./e2e.test -test.v -ginkgo.v -test.timeout "${TEST_TIMEOUT}" -ginkgo.fail-on-empty -ginkgo.flake-attempts="${FLAKE_ATTEMPTS}" --ginkgo.label-filter="${{ matrix.label }}" + fi fi else GH_USERNAME="${GH_USERNAME}" \ diff --git a/e2e/tests/up/managed_policy_fixture_linux.go b/e2e/tests/up/managed_policy_fixture_linux.go new file mode 100644 index 0000000000..0ad6aaf8b9 --- /dev/null +++ b/e2e/tests/up/managed_policy_fixture_linux.go @@ -0,0 +1,280 @@ +//go:build linux + +package up + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "strings" + "syscall" +) + +const ( + safeRecreateManagedPolicyPath = "/etc/microsandbox/managed.json" + safeRecreateEnabledValue = "true" +) + +type safeRecreateSandboxDefaults struct { + MemoryMiB uint64 `json:"memory_mib"` +} + +type safeRecreatePolicyOverrides struct { + SandboxDefaults *safeRecreateSandboxDefaults `json:"sandbox_defaults,omitempty"` +} + +type safeRecreateManagedPolicy struct { + Version int `json:"version"` + Overrides safeRecreatePolicyOverrides `json:"overrides"` +} + +type safeRecreateManagedFixturePaths struct { + policyPath string + root string + upper string + work string +} + +// setSafeRecreateManagedMemory512 installs the valid policy used to make the +// 2048 MiB / tmpfs:1G request invalid only after managed precedence is applied. +func setSafeRecreateManagedMemory512() error { + policy := safeRecreateManagedPolicy{ + Version: 1, + Overrides: safeRecreatePolicyOverrides{ + SandboxDefaults: &safeRecreateSandboxDefaults{MemoryMiB: 512}, + }, + } + return writeSafeRecreateManagedPolicy(policy) +} + +// clearSafeRecreateManagedPolicy restores the valid empty policy before cleanup. +func clearSafeRecreateManagedPolicy() error { + return writeSafeRecreateManagedPolicy(safeRecreateManagedPolicy{Version: 1}) +} + +func writeSafeRecreateManagedPolicy(policy safeRecreateManagedPolicy) error { + paths, err := verifySafeRecreateManagedPolicyFixture() + if err != nil { + return err + } + data, err := json.Marshal(policy) + if err != nil { + return fmt.Errorf("encode managed policy: %w", err) + } + return publishSafeRecreateManagedPolicy(paths.policyPath, append(data, '\n')) +} + +func publishSafeRecreateManagedPolicy(path string, data []byte) (returnErr error) { + tmp, err := os.CreateTemp(filepath.Dir(path), ".managed.json.tmp-") + if err != nil { + return fmt.Errorf("create managed policy temporary file: %w", err) + } + tmpName := tmp.Name() + defer func() { + if err := os.Remove(tmpName); err != nil && !errors.Is(err, os.ErrNotExist) { + returnErr = errors.Join( + returnErr, + fmt.Errorf("remove managed policy temporary file: %w", err), + ) + } + }() + if err := writeSafeRecreateManagedPolicyTemp(tmp, data); err != nil { + return err + } + if err := verifySafeRecreateRootFile(tmpName, 0o600); err != nil { + return fmt.Errorf("verify managed policy temporary file: %w", err) + } + if err := os.Rename(tmpName, path); err != nil { + return fmt.Errorf("replace managed policy atomically: %w", err) + } + if _, err := verifySafeRecreateManagedPolicyFixture(); err != nil { + return fmt.Errorf("verify managed policy after replacement: %w", err) + } + return nil +} + +func writeSafeRecreateManagedPolicyTemp(tmp *os.File, data []byte) (returnErr error) { + defer func() { returnErr = errors.Join(returnErr, tmp.Close()) }() + if err := tmp.Chmod(0o600); err != nil { + return fmt.Errorf("set managed policy mode: %w", err) + } + if err := tmp.Chown(0, 0); err != nil { + return fmt.Errorf("set managed policy ownership: %w", err) + } + if _, err := tmp.Write(data); err != nil { + return fmt.Errorf("write managed policy: %w", err) + } + if err := tmp.Sync(); err != nil { + return fmt.Errorf("sync managed policy: %w", err) + } + return nil +} + +func verifySafeRecreateManagedPolicyFixture() (safeRecreateManagedFixturePaths, error) { + if os.Geteuid() != 0 { + return safeRecreateManagedFixturePaths{}, errors.New( + "managed policy fixture requires euid 0", + ) + } + paths, err := safeRecreateManagedPolicyPathsFromEnv() + if err != nil { + return safeRecreateManagedFixturePaths{}, err + } + if err := verifySafeRecreateManagedFixtureDirectories(paths); err != nil { + return safeRecreateManagedFixturePaths{}, err + } + if err := verifySafeRecreateManagedFixtureNamespace(); err != nil { + return safeRecreateManagedFixturePaths{}, err + } + if err := verifySafeRecreateEtcOverlay(paths.upper, paths.work); err != nil { + return safeRecreateManagedFixturePaths{}, err + } + if err := verifySafeRecreateManagedPolicyTarget(paths.policyPath); err != nil { + return safeRecreateManagedFixturePaths{}, err + } + return paths, nil +} + +func safeRecreateManagedPolicyPathsFromEnv() (safeRecreateManagedFixturePaths, error) { + paths := safeRecreateManagedFixturePaths{ + policyPath: os.Getenv("DEVSY_TEST_MSB_MANAGED_POLICY"), + root: os.Getenv("DEVSY_TEST_MSB_FIXTURE_ROOT"), + upper: os.Getenv("DEVSY_TEST_MSB_EXPECTED_UPPER"), + work: os.Getenv("DEVSY_TEST_MSB_EXPECTED_WORK"), + } + if paths.policyPath != safeRecreateManagedPolicyPath { + return safeRecreateManagedFixturePaths{}, fmt.Errorf( + "managed policy fixture path must be %q", + safeRecreateManagedPolicyPath, + ) + } + if paths.root == "" || paths.upper != filepath.Join(paths.root, "upper") || + paths.work != filepath.Join(paths.root, "work") { + return safeRecreateManagedFixturePaths{}, errors.New( + "managed policy fixture root and overlay paths are inconsistent", + ) + } + return paths, nil +} + +func verifySafeRecreateManagedFixtureDirectories(paths safeRecreateManagedFixturePaths) error { + for _, directory := range []string{paths.root, paths.upper, paths.work} { + if err := verifySafeRecreateRootDirectory(directory, 0o700); err != nil { + return fmt.Errorf("verify owned overlay directory: %w", err) + } + } + return nil +} + +func verifySafeRecreateManagedFixtureNamespace() error { + parentNS := os.Getenv("DEVSY_TEST_MSB_PARENT_MNT_NS") + expectedNS := os.Getenv("DEVSY_TEST_MSB_EXPECTED_MNT_NS") + currentNS, err := os.Readlink("/proc/self/ns/mnt") + if err != nil { + return fmt.Errorf("read current mount namespace: %w", err) + } + if parentNS == "" || expectedNS == "" || parentNS == currentNS || expectedNS != currentNS { + return errors.New("managed policy fixture is outside its verified private mount namespace") + } + return nil +} + +func verifySafeRecreateManagedPolicyTarget(path string) error { + if err := verifySafeRecreateRootDirectory(filepath.Dir(path), 0o755); err != nil { + return err + } + return verifySafeRecreateRootFile(path, 0o600) +} + +func verifySafeRecreateRootDirectory(path string, mode os.FileMode) error { + // #nosec G703 -- Callers pass fixed policy parents or validated owned overlay paths. + info, err := os.Lstat(path) + if err != nil { + return fmt.Errorf("inspect %s: %w", path, err) + } + if !info.IsDir() || info.Mode().Perm() != mode { + return fmt.Errorf("%s must be a real directory with mode %04o", path, mode) + } + return verifySafeRecreateOwner(info, path) +} + +func verifySafeRecreateRootFile(path string, mode os.FileMode) error { + // #nosec G703 -- Callers pass the fixed policy path or its CreateTemp-owned sibling. + info, err := os.Lstat(path) + if err != nil { + return fmt.Errorf("inspect %s: %w", path, err) + } + if !info.Mode().IsRegular() || info.Mode().Perm() != mode { + return fmt.Errorf("%s must be a regular file with mode %04o", path, mode) + } + return verifySafeRecreateOwner(info, path) +} + +func verifySafeRecreateOwner(info os.FileInfo, path string) error { + stat, ok := info.Sys().(*syscall.Stat_t) + if !ok || stat.Uid != 0 || stat.Gid != 0 { + return fmt.Errorf("%s must be owned by uid 0 and gid 0", path) + } + return nil +} + +func verifySafeRecreateEtcOverlay(upper, work string) error { + data, err := os.ReadFile("/proc/self/mountinfo") + if err != nil { + return fmt.Errorf("read mount table: %w", err) + } + return verifySafeRecreateEtcOverlayMount(data, upper, work) +} + +func verifySafeRecreateEtcOverlayMount(data []byte, upper, work string) error { + for line := range strings.SplitSeq(string(data), "\n") { + before, after, found := strings.Cut(line, " - ") + if !found { + continue + } + fields := strings.Fields(before) + if len(fields) < 6 || unescapeMountInfo(fields[4]) != "/etc" { + continue + } + post := strings.Fields(after) + if len(post) < 3 || post[0] != "overlay" { + return errors.New("/etc is not mounted as overlay") + } + return verifySafeRecreateOverlayOptions(post[2], upper, work) + } + return errors.New("/etc does not have a mountinfo entry in this namespace") +} + +func verifySafeRecreateOverlayOptions(options, upper, work string) error { + expected := map[string]string{ + "upperdir": filepath.Clean(upper), + "workdir": filepath.Clean(work), + } + seen := make(map[string]bool, len(expected)) + for option := range strings.SplitSeq(options, ",") { + key, value, hasValue := strings.Cut(option, "=") + want, owned := expected[key] + if !owned { + continue + } + if !hasValue || seen[key] || unescapeMountInfo(value) != want { + return fmt.Errorf("/etc overlay %s is not owned by this fixture", key) + } + seen[key] = true + } + if len(seen) != len(expected) { + return errors.New("/etc overlay is missing its owned upper or work directory") + } + return nil +} + +func unescapeMountInfo(value string) string { + for _, escape := range []struct{ encoded, decoded string }{ + {`\040`, " "}, {`\011`, "\t"}, {`\012`, "\n"}, {`\134`, `\`}, + } { + value = strings.ReplaceAll(value, escape.encoded, escape.decoded) + } + return value +} diff --git a/e2e/tests/up/provider_microsandbox_managed_recreate_linux.go b/e2e/tests/up/provider_microsandbox_managed_recreate_linux.go new file mode 100644 index 0000000000..6d06f4a86d --- /dev/null +++ b/e2e/tests/up/provider_microsandbox_managed_recreate_linux.go @@ -0,0 +1,155 @@ +//go:build linux + +package up + +import ( + "context" + "fmt" + "os" + "strings" + + "github.com/devsy-org/devsy/e2e/framework" + "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" +) + +const safeRecreateManagedConflictErr = "tmpfs root disk size (1024 MiB) must not exceed sandbox memory (512 MiB)" + +var _ = ginkgo.Describe("MicroSandbox managed-policy rejected replacement", + ginkgo.Label("up-provider-microsandbox-managed-recreate"), func() { + ginkgo.DescribeTable( + "keeps the original generation until the managed-policy conflict is removed", + func(ctx context.Context, source, name string, stopped bool) { + requireManagedRecreateFixture() + fixture := newSafeRecreateFixture(ctx, source, name, 2048) + fixture.initialize(ctx, stopped) + ginkgo.DeferCleanup(func(context.Context) error { + return clearSafeRecreateManagedPolicy() + }) + + // Prove an empty managed policy works before adding the conflicting override. + framework.ExpectNoError(clearSafeRecreateManagedPolicy()) + framework.ExpectNoError(fixture.useManagedRecreateOptions(ctx, false)) + fixture.writeConfig("2gb") + framework.ExpectNoError(setSafeRecreateManagedMemory512()) + out, err := microsandboxResourceCommand(ctx, "list", "--quiet") + framework.ExpectNoError(err, string(out)) + gomega.Expect(strings.Fields(string(out))). + To(gomega.ContainElement(fixture.sandbox)) + + stdout, stderr, err := fixture.framework.DevsyUpStreams( + ctx, + fixture.workspace, + append(fixture.upArgs(), "--recreate")..., + ) + gomega.Expect(err). + To(gomega.HaveOccurred(), "stdout: %s stderr: %s", stdout, stderr) + gomega.Expect(stdout+stderr).To( + gomega.ContainSubstring(safeRecreateManagedConflictErr), + "request must reach final managed-layer tmpfs validation", + ) + // Inspect status and host state before any command can start a stopped VM. + gomega.Expect(inspectSafeRecreate(ctx, fixture.sandbox)). + To(gomega.Equal(fixture.original)) + fixture.assertHostState(ctx) + + // Restore policy and original provider options before resuming stopped originals. + framework.ExpectNoError(clearSafeRecreateManagedPolicy()) + framework.ExpectNoError(fixture.useManagedRecreateOptions(ctx, true)) + fixture.inspectOriginalFiles(ctx, stopped) + + // The exact same request is valid without managed policy and must replace. + framework.ExpectNoError(fixture.useManagedRecreateOptions(ctx, false)) + fixture.writeConfig("2gb") + framework.ExpectNoError(fixture.framework.DevsyUpRecreate( + ctx, append([]string{fixture.workspace}, fixture.upArgs()...)..., + )) + after := inspectSafeRecreate(ctx, fixture.sandbox) + gomega.Expect(after.Status).To(gomega.Equal("Running")) + gomega.Expect(after.CreatedAt).NotTo(gomega.Equal(fixture.original.CreatedAt)) + gomega.Expect(microsandboxStorageBootID(ctx, fixture.framework, fixture.workspace)). + NotTo(gomega.Equal(fixture.bootID)) + out, err = safeRecreateExec(ctx, fixture.sandbox, "sh", "-c", + `test ! -e "$1"`, "sh", safeRecreateRootPath) + framework.ExpectNoError(err, string(out)) + fixture.assertPersistentMounts(ctx) + fixture.assertHostState(ctx) + expected := microsandboxResources{ + CPUs: 1, MemoryMiB: 2048, MaxCPUs: 1, MaxMemoryMiB: 2048, + } + gomega.Expect(microsandboxActiveResources(ctx, fixture.sandbox)). + To(gomega.Equal(expected)) + assertMicrosandboxGuestResources( + ctx, + fixture.framework, + fixture.workspace, + expected, + ) + }, + ginkgo.Entry( + "builtin running", + microsandboxResourceBuiltin, + "safe-recreate-managed-builtin-running", + false, + ginkgo.SpecTimeout(framework.TimeoutLong()), + ), + ginkgo.Entry( + "builtin stopped", + microsandboxResourceBuiltin, + "safe-recreate-managed-builtin-stopped", + true, + ginkgo.SpecTimeout(framework.TimeoutLong()), + ), + ginkgo.Entry( + "external running", + microsandboxExternalProvider, + "safe-recreate-managed-external-running", + false, + ginkgo.SpecTimeout(framework.TimeoutLong()), + ), + ginkgo.Entry( + "external stopped", + microsandboxExternalProvider, + "safe-recreate-managed-external-stopped", + true, + ginkgo.SpecTimeout(framework.TimeoutLong()), + ), + ) + }) + +func requireManagedRecreateFixture() { + required := os.Getenv("DEVSY_REQUIRE_MSB_MANAGED_POLICY_FIXTURE") == safeRecreateEnabledValue + marker := os.Getenv("DEVSY_TEST_MSB_MANAGED_POLICY") + if required { + gomega.Expect(marker). + To( + gomega.Equal(safeRecreateManagedPolicyPath), + "required managed-policy fixture wrapper did not provide its isolated policy path", + ) + return + } + if marker == "" { + ginkgo.Skip("requires the isolated managed-policy E2E wrapper") + } + gomega.Expect(marker). + To(gomega.Equal(safeRecreateManagedPolicyPath), "managed-policy fixture marker has unexpected path") +} + +func (f *safeRecreateFixture) useManagedRecreateOptions( + ctx context.Context, + originalOptions bool, +) error { + storage := 1 + ephemeral := safeRecreateEnabledValue + if originalOptions { + storage = 2 + ephemeral = "false" + } + return f.framework.DevsyProviderUse(ctx, f.provider, + microsandboxResourceOption, "MICROSANDBOX_MEMORY=0", + microsandboxResourceOption, fmt.Sprintf("MICROSANDBOX_MAX_MEMORY=%d", f.maxMemoryMiB), + microsandboxResourceOption, "MICROSANDBOX_CPUS=1", + microsandboxResourceOption, "MICROSANDBOX_MAX_CPUS=1", + microsandboxResourceOption, fmt.Sprintf("MICROSANDBOX_STORAGE=%d", storage), + microsandboxResourceOption, "MICROSANDBOX_EPHEMERAL="+ephemeral) +} diff --git a/e2e/tests/up/provider_microsandbox_recreate.go b/e2e/tests/up/provider_microsandbox_recreate.go index ce4602041e..92b224f638 100644 --- a/e2e/tests/up/provider_microsandbox_recreate.go +++ b/e2e/tests/up/provider_microsandbox_recreate.go @@ -35,7 +35,7 @@ var _ = ginkgo.Describe("MicroSandbox rejected replacement preservation", ginkgo.DescribeTable( "keeps the original generation until valid explicit replacement", func(ctx context.Context, source, name string, stopped bool) { - fixture := newSafeRecreateFixture(ctx, source, name) + fixture := newSafeRecreateFixture(ctx, source, name, 1536) fixture.initialize(ctx, stopped) fixture.rejectInvalidReplacement(ctx) fixture.inspectOriginalFiles(ctx, stopped) @@ -76,6 +76,8 @@ type safeRecreateFixture struct { framework *framework.Framework workspace, sandbox, uid, label string volume string + provider string + maxMemoryMiB int volumeOwned bool original safeRecreateInspection bootID string @@ -98,7 +100,11 @@ type safeRecreateFile struct { Owner string } -func newSafeRecreateFixture(ctx context.Context, source, name string) *safeRecreateFixture { +func newSafeRecreateFixture( + ctx context.Context, + source, name string, + maxMemoryMiB int, +) *safeRecreateFixture { skipIfNoMicrosandbox(ctx) initialDir, err := os.Getwd() framework.ExpectNoError(err) @@ -121,12 +127,17 @@ func newSafeRecreateFixture(ctx context.Context, source, name string) *safeRecre }) framework.ExpectNoError(f.DevsyProviderUse(ctx, name, microsandboxResourceOption, "MICROSANDBOX_MEMORY=0", - microsandboxResourceOption, "MICROSANDBOX_MAX_MEMORY=1536", + microsandboxResourceOption, fmt.Sprintf("MICROSANDBOX_MAX_MEMORY=%d", maxMemoryMiB), microsandboxResourceOption, "MICROSANDBOX_CPUS=1", microsandboxResourceOption, "MICROSANDBOX_MAX_CPUS=1", microsandboxResourceOption, "MICROSANDBOX_STORAGE=2", microsandboxResourceOption, "MICROSANDBOX_EPHEMERAL=false")) - fixture := &safeRecreateFixture{framework: f, workspace: workspace} + fixture := &safeRecreateFixture{ + framework: f, + workspace: workspace, + provider: name, + maxMemoryMiB: maxMemoryMiB, + } fixture.uid = encoding.CreateNewUID("", filepath.Base(workspace)) fixture.sandbox = "devsy-" + fixture.uid fixture.label = "devsy.safe-recreate=" + fixture.uid @@ -229,7 +240,7 @@ func (f *safeRecreateFixture) initialize(ctx context.Context, stopped bool) { f.bootID = microsandboxStorageBootID(ctx, f.framework, f.workspace) gomega.Expect(microsandboxActiveResources(ctx, f.sandbox)). To(gomega.Equal(microsandboxResources{ - CPUs: 1, MemoryMiB: 1024, MaxCPUs: 1, MaxMemoryMiB: 1536, + CPUs: 1, MemoryMiB: 1024, MaxCPUs: 1, MaxMemoryMiB: f.maxMemoryMiB, })) if stopped { framework.ExpectNoError(f.framework.DevsyWorkspaceStop(ctx, f.workspace)) @@ -324,7 +335,12 @@ func (f *safeRecreateFixture) replaceSuccessfully(ctx context.Context) { framework.ExpectNoError(err, string(out)) f.assertPersistentMounts(ctx) f.assertHostState(ctx) - expected := microsandboxResources{CPUs: 1, MemoryMiB: 1536, MaxCPUs: 1, MaxMemoryMiB: 1536} + expected := microsandboxResources{ + CPUs: 1, + MemoryMiB: 1536, + MaxCPUs: 1, + MaxMemoryMiB: f.maxMemoryMiB, + } gomega.Expect(microsandboxActiveResources(ctx, f.sandbox)).To(gomega.Equal(expected)) assertMicrosandboxGuestResources(ctx, f.framework, f.workspace, expected) } diff --git a/hack/run-managed-policy-e2e.sh b/hack/run-managed-policy-e2e.sh new file mode 100755 index 0000000000..9a5366350e --- /dev/null +++ b/hack/run-managed-policy-e2e.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash +set -euo pipefail +umask 077 + +if [[ ${EUID} -ne 0 ]]; then + echo "managed-policy fixture must run as root" >&2 + exit 1 +fi +if [[ ${DEVSY_REQUIRE_MSB_MANAGED_POLICY_FIXTURE:-} != true ]]; then + echo "managed-policy E2E wrapper requires the CI expectation marker" >&2 + exit 1 +fi +if [[ -z ${RUNNER_TEMP:-} || ! -d ${RUNNER_TEMP} ]]; then + echo "RUNNER_TEMP must name an existing directory" >&2 + exit 1 +fi +if [[ ${1:-} != ./e2e.test ]]; then + echo "expected the already-built ./e2e.test binary as the first argument" >&2 + exit 1 +fi + +fixture_root=$(mktemp -d "${RUNNER_TEMP%/}/devsy-managed-policy.XXXXXX") +chmod 700 "$fixture_root" +mkdir -m 700 "$fixture_root/upper" "$fixture_root/work" +for directory in "$fixture_root" "$fixture_root/upper" "$fixture_root/work"; do + if [[ $(stat -c '%u:%g:%a' "$directory") != "0:0:700" ]]; then + echo "managed-policy fixture directory has unexpected ownership or mode: $directory" >&2 + exit 1 + fi +done +namespace_id_file="$fixture_root/mount-namespace-id" +parent_mount_namespace=$(readlink /proc/self/ns/mnt) +export RUNNER_TEMP fixture_root namespace_id_file parent_mount_namespace + +# Deliberately retain the small private scratch under RUNNER_TEMP for runner +# job teardown. Do not remove overlay upper/work after an abnormal namespace +# exit: an unobserved namespace reference could still use them. +unshare --mount --propagation private -- /bin/bash -s -- "$fixture_root" "$namespace_id_file" "$parent_mount_namespace" "$@" <<'NAMESPACE' +set -euo pipefail +umask 077 +fixture_root=$1 +namespace_id_file=$2 +parent_mount_namespace=$3 +shift 3 + +if [[ ${EUID} -ne 0 ]]; then + echo "managed-policy namespace must run as root" >&2 + exit 1 +fi +current_mount_namespace=$(readlink /proc/self/ns/mnt) +if [[ $current_mount_namespace == "$parent_mount_namespace" ]]; then + echo "mount namespace did not change" >&2 + exit 1 +fi +printf '%s\n' "$current_mount_namespace" > "$namespace_id_file" + +# Overlay /etc before creating any managed-policy path so no host file changes. +mount -t overlay overlay -o "lowerdir=/etc,upperdir=$fixture_root/upper,workdir=$fixture_root/work" /etc +if ! awk -v upper="$fixture_root/upper" -v work="$fixture_root/work" ' + $5 == "/etc" { + separator = 0 + for (i = 1; i <= NF; i++) if ($i == "-") separator = i + if (separator && $(separator + 1) == "overlay") { + count = split($(separator + 3), options, ",") + for (i = 1; i <= count; i++) { + if (options[i] == "upperdir=" upper) found_upper = 1 + if (options[i] == "workdir=" work) found_work = 1 + } + if (found_upper && found_work) found = 1 + } + } + END { exit !found } +' /proc/self/mountinfo; then + echo "private /etc overlay does not use this fixture's owned upper and work directories" >&2 + exit 1 +fi +if [[ -e /etc/microsandbox || -L /etc/microsandbox ]]; then + echo "managed-policy fixture requires /etc/microsandbox to be absent in the overlay lower layer" >&2 + exit 1 +fi +install -d -o 0 -g 0 -m 755 /etc/microsandbox +policy_tmp=/etc/microsandbox/.managed.json.initial.$$ +printf '%s\n' '{"version":1,"overrides":{}}' > "$policy_tmp" +chown 0:0 "$policy_tmp" +chmod 600 "$policy_tmp" +mv -T -- "$policy_tmp" /etc/microsandbox/managed.json + +export DEVSY_REQUIRE_MSB_MANAGED_POLICY_FIXTURE=true +export DEVSY_TEST_MSB_MANAGED_POLICY=/etc/microsandbox/managed.json +export DEVSY_TEST_MSB_FIXTURE_ROOT="$fixture_root" +export DEVSY_TEST_MSB_PARENT_MNT_NS="$parent_mount_namespace" +export DEVSY_TEST_MSB_EXPECTED_MNT_NS="$current_mount_namespace" +export DEVSY_TEST_MSB_EXPECTED_UPPER="$fixture_root/upper" +export DEVSY_TEST_MSB_EXPECTED_WORK="$fixture_root/work" +exec "$@" +NAMESPACE diff --git a/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx b/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx index b063b08833..2d6ec224d4 100644 --- a/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx @@ -127,6 +127,8 @@ DEVSY_REQUIRE_MICROSANDBOX=true task cli:test:e2e:suite -- up-provider-microsand The `up-provider-microsandbox-recreate` label tests rejected explicit replacement for running and stopped VMs against both providers. Each case checks the unchanged VM generation and active configuration before resuming a stopped original, then checks persistent root, bind and named-volume data together with the workspace and SSH records. A subsequent valid replacement must change the VM generation, reset the root marker and preserve the external data. These request-invalid cases do not establish preservation for managed-policy conflicts or failures after replacement begins. Each scenario has a ten-minute deadline, with a 20-minute CI test deadline and a 25-minute job deadline. +The Linux-only `up-provider-microsandbox-managed-recreate` label checks a valid 2048 MiB / 1 GiB tmpfs replacement that becomes invalid after a managed policy sets memory to 512 MiB. Running and stopped originals must retain their VM generation, root and persistent mount data, workspace record and SSH files for both providers. Resetting the policy must allow the same replacement request. The CI wrapper overlays `/etc` in a private mount namespace before creating any policy file; its scratch remains in the disposable runner for job teardown. These checks require that isolated wrapper and do not establish rollback after replacement has begun. Each scenario and the suite retain the request-invalid suite's time bounds. + The `up-provider-microsandbox-logs-cancel` label checks completed stdout/stderr workload logs while running and stopped, direct runtime Exec cancellation of a ready parent and child, and cancellation with blocked owned stdin against each provider. A separate SSH observer verifies process identity using PID, kernel start time, and a unique command marker. Caller return and process death must both occur within ten seconds of cancellation; the VM's creation time, boot ID, and source files must remain intact. Logs do not start a stopped VM. These checks do not establish log ordering, binary log fidelity, TTY cancellation, or cancellation of arbitrary nonclosable input. ```sh @@ -135,6 +137,6 @@ DEVSY_REQUIRE_MICROSANDBOX=true task cli:test:e2e:suite -- up-provider-microsand For built-in Exec, cancellation closes non-file `io.ReadCloser` input whose `Close` unblocks `Read`; ordinary completion and caller-owned file input retain their ownership. Agent binary delivery owns its acquired source and closes it when the individual transfer attempt is canceled, before retrying with a fresh source. Cleanup joins cancellation callbacks before returning. -These scenarios do not establish complete parity. Runtime compatibility failures and managed-policy invalid-recreate preservation still require coverage before replacing the built-in provider. Green lifecycle, image, mount, resource, persistent-storage, ephemeral-root, TCP-egress, prebuild, Dockerless, request-invalid recreation, logs, and cancellation checks alone do not authorize that cutover. Dockerless refusal checks cover fresh creation, not preservation after an invalid recreation request. Egress coverage does not establish UDP, DNS, TLS, ingress, or live-policy modification behavior. +These scenarios do not establish complete parity. Runtime compatibility failures still require coverage before replacing the built-in provider. Green lifecycle, image, mount, resource, persistent-storage, ephemeral-root, TCP-egress, prebuild, Dockerless, request-invalid and managed-policy recreation, logs, and cancellation checks alone do not authorize that cutover. Dockerless refusal checks cover fresh creation, not preservation after an invalid recreation request. Egress coverage does not establish UDP, DNS, TLS, ingress, or live-policy modification behavior. Prebuild invalidation coverage changes the Dockerfile; it does not independently establish hashing of local Feature script contents. From 44373d0bec993074d1e9db0442c86bf65605a390 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 13 Oct 2026 02:35:06 -0600 Subject: [PATCH 4/7] docs(runtime): document isolated recreation suite commands --- .../developing-providers/runtime-protocol.mdx | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx b/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx index 2d6ec224d4..cec6a8610e 100644 --- a/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx @@ -127,8 +127,24 @@ DEVSY_REQUIRE_MICROSANDBOX=true task cli:test:e2e:suite -- up-provider-microsand The `up-provider-microsandbox-recreate` label tests rejected explicit replacement for running and stopped VMs against both providers. Each case checks the unchanged VM generation and active configuration before resuming a stopped original, then checks persistent root, bind and named-volume data together with the workspace and SSH records. A subsequent valid replacement must change the VM generation, reset the root marker and preserve the external data. These request-invalid cases do not establish preservation for managed-policy conflicts or failures after replacement begins. Each scenario has a ten-minute deadline, with a 20-minute CI test deadline and a 25-minute job deadline. +```sh +DEVSY_REQUIRE_MICROSANDBOX=true task cli:test:e2e:suite -- up-provider-microsandbox-recreate +``` + The Linux-only `up-provider-microsandbox-managed-recreate` label checks a valid 2048 MiB / 1 GiB tmpfs replacement that becomes invalid after a managed policy sets memory to 512 MiB. Running and stopped originals must retain their VM generation, root and persistent mount data, workspace record and SSH files for both providers. Resetting the policy must allow the same replacement request. The CI wrapper overlays `/etc` in a private mount namespace before creating any policy file; its scratch remains in the disposable runner for job teardown. These checks require that isolated wrapper and do not establish rollback after replacement has begun. Each scenario and the suite retain the request-invalid suite's time bounds. +On a disposable Linux host with the same prerequisites, prepare the E2E binaries and run the managed suite through its isolated wrapper. The scratch directory is retained for host teardown; running the label without the wrapper does not exercise these cases. + +```sh +task cli:test:e2e:build +(cd e2e && go test -c -o e2e.test .) +(cd e2e && sudo env PATH="$PATH" RUNNER_TEMP="$(mktemp -d)" \ + DEVSY_REQUIRE_MICROSANDBOX=true DEVSY_REQUIRE_MSB_MANAGED_POLICY_FIXTURE=true \ + ../hack/run-managed-policy-e2e.sh ./e2e.test -test.v -ginkgo.v \ + -test.timeout 1200s -ginkgo.fail-on-empty -ginkgo.flake-attempts=1 \ + -ginkgo.label-filter=up-provider-microsandbox-managed-recreate) +``` + The `up-provider-microsandbox-logs-cancel` label checks completed stdout/stderr workload logs while running and stopped, direct runtime Exec cancellation of a ready parent and child, and cancellation with blocked owned stdin against each provider. A separate SSH observer verifies process identity using PID, kernel start time, and a unique command marker. Caller return and process death must both occur within ten seconds of cancellation; the VM's creation time, boot ID, and source files must remain intact. Logs do not start a stopped VM. These checks do not establish log ordering, binary log fidelity, TTY cancellation, or cancellation of arbitrary nonclosable input. ```sh From 737b345b1889d138c59b1da602072b61d0001081 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 13 Oct 2026 03:35:30 -0600 Subject: [PATCH 5/7] refactor(test): consolidate MicroSandbox integration harness --- .github/workflows/pr-ci.yml | 345 +++++++++--------- e2e/tests/up/MICROSANDBOX.md | 66 ++++ e2e/tests/up/microsandbox_session.go | 131 +++++++ e2e/tests/up/provider_microsandbox.go | 54 +-- .../up/provider_microsandbox_dockerless.go | 7 +- e2e/tests/up/provider_microsandbox_egress.go | 7 +- .../up/provider_microsandbox_ephemeral.go | 7 +- e2e/tests/up/provider_microsandbox_images.go | 26 +- .../up/provider_microsandbox_logs_cancel.go | 7 +- ...der_microsandbox_managed_recreate_linux.go | 38 +- e2e/tests/up/provider_microsandbox_mounts.go | 26 +- .../up/provider_microsandbox_prebuilds.go | 7 +- .../up/provider_microsandbox_recreate.go | 33 +- .../up/provider_microsandbox_resources.go | 33 +- e2e/tests/up/provider_microsandbox_storage.go | 34 +- hack/microsandbox/test_verify_report.py | 125 +++++++ hack/microsandbox/verify_report.py | 93 +++++ .../developing-providers/runtime-protocol.mdx | 2 + 18 files changed, 660 insertions(+), 381 deletions(-) create mode 100644 e2e/tests/up/MICROSANDBOX.md create mode 100644 e2e/tests/up/microsandbox_session.go create mode 100644 hack/microsandbox/test_verify_report.py create mode 100644 hack/microsandbox/verify_report.py diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index 1020e59b09..839150a882 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -30,6 +30,8 @@ jobs: - 'go.sum' - '.goreleaser.yml' - 'e2e/**' + - 'hack/microsandbox/**' + - 'hack/run-managed-policy-e2e.sh' - '.github/workflows/pr-ci.yml' - 'hack/licenses/**' - 'hack/podman_ci/**' @@ -232,7 +234,7 @@ jobs: if: matrix.runner == 'ubuntu-latest' || matrix.runner == 'windows-latest' uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 with: - name: e2e-test-${{ steps.os.outputs.runner_os }} + name: e2e-test-linux path: ./e2e/e2e.test* @@ -292,14 +294,14 @@ jobs: - name: download CLI artifacts uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 with: - pattern: devsy-${{ steps.os.outputs.runner_os }} + pattern: devsy-linux path: ${{ runner.temp }}/devsy-bin/ merge-multiple: true - name: download e2e test binary uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 with: - name: e2e-test-${{ steps.os.outputs.runner_os }} + name: e2e-test-linux path: ./e2e/ - name: setup executable @@ -324,6 +326,169 @@ jobs: run: | ./e2e.test -test.v -ginkgo.v -test.timeout 1500s -ginkgo.fail-on-empty --ginkgo.label-filter="${{ matrix.label }}" + integration-tests-microsandbox: + name: Test ${{ matrix.label }} on ubuntu-latest + needs: [changes, build-cli] + if: needs.changes.outputs.go == 'true' + strategy: + fail-fast: false + matrix: + include: + - label: up-provider-microsandbox + expected-spec-count: 2 + - label: up-provider-microsandbox-images + expected-spec-count: 6 + - label: up-provider-microsandbox-mounts + expected-spec-count: 10 + - label: up-provider-microsandbox-resources + expected-spec-count: 12 + - label: up-provider-microsandbox-storage + expected-spec-count: 6 + - label: up-provider-microsandbox-ephemeral + expected-spec-count: 6 + - label: up-provider-microsandbox-egress + expected-spec-count: 4 + - label: up-provider-microsandbox-prebuilds + expected-spec-count: 4 + - label: up-provider-microsandbox-dockerless + expected-spec-count: 8 + - label: up-provider-microsandbox-recreate + expected-spec-count: 4 + - label: up-provider-microsandbox-managed-recreate + expected-spec-count: 4 + - label: up-provider-microsandbox-logs-cancel + expected-spec-count: 6 + runs-on: ubuntu-latest + timeout-minutes: 25 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + + - name: install gnupg2 + run: sudo apt-get install -y gnupg2 + + - name: download CLI artifacts + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + pattern: devsy-linux + path: ${{ runner.temp }}/devsy-bin/ + merge-multiple: true + + - name: download e2e test binary + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: e2e-test-linux + path: ./e2e/ + + - name: setup executable + shell: bash + working-directory: ./e2e + run: | + ls -R "${RUNNER_TEMP}/devsy-bin/" + mkdir -p ./bin/ + find "${RUNNER_TEMP}/devsy-bin/" -type f -name "devsy-*" -exec cp {} ./bin \; + find ./bin -name "devsy-*" -exec chmod +x {} \; + chmod +x ./e2e.test + ls -R ./bin/ + + - name: test Ginkgo report validator + run: python3 -m unittest discover -s hack/microsandbox -p 'test_*.py' + + - name: cache microsandbox release bundle (Linux) + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ runner.temp }}/microsandbox-linux-x86_64-v0.7.7.tar.gz + key: ${{ runner.os }}-${{ runner.arch }}-microsandbox-bundle-v0.7.7 + + - name: install checksum-pinned microsandbox (Linux) + timeout-minutes: 5 + run: | + archive="${RUNNER_TEMP}/microsandbox-linux-x86_64-v0.7.7.tar.gz" + if [ ! -f "$archive" ]; then + curl -fsSL --retry 3 -o "$archive" \ + https://github.com/superradcompany/microsandbox/releases/download/v0.7.7/microsandbox-linux-x86_64.tar.gz + fi + echo "b3cc4a5e3f52dfdd938a6f67ac4a9a959ddfe304bab56de4964044b8613f01bb $archive" | sha256sum -c - + staging="$(mktemp -d)" + trap 'rm -rf "$staging"' EXIT + tar -xzf "$archive" -C "$staging" + mkdir -p "$HOME/.microsandbox/bin" "$HOME/.microsandbox/lib" "$HOME/.local/bin" + install -m 755 "$staging/msb" "$HOME/.microsandbox/bin/msb" + install -m 644 "$staging/libkrunfw.so.5.6.1" "$HOME/.microsandbox/lib/" + ln -sf libkrunfw.so.5.6.1 "$HOME/.microsandbox/lib/libkrunfw.so.5" + ln -sf libkrunfw.so.5 "$HOME/.microsandbox/lib/libkrunfw.so" + ln -sf "$HOME/.microsandbox/bin/msb" "$HOME/.local/bin/msb" + echo "$HOME/.local/bin" >> "$GITHUB_PATH" + "$HOME/.local/bin/msb" --version + sudo test -r /dev/kvm + sudo test -w /dev/kvm + + - name: run MicroSandbox test + shell: bash + working-directory: ./e2e + env: + GH_USERNAME: ${{ github.repository_owner }} + GH_ACCESS_TOKEN: ${{ github.token }} + GH_CREDENTIAL_USERNAME: x-access-token + TEST_TIMEOUT: 1200s + DEVSY_REQUIRE_MICROSANDBOX: "true" + run: | + set -uo pipefail + report="${RUNNER_TEMP}/microsandbox-${{ matrix.label }}.json" + test_log="${RUNNER_TEMP}/microsandbox-${{ matrix.label }}.log" + command=(./e2e.test -test.v -ginkgo.v -test.timeout "${TEST_TIMEOUT}" + -ginkgo.fail-on-empty -ginkgo.flake-attempts=1 + --ginkgo.label-filter="${{ matrix.label }}" -ginkgo.json-report="$report") + fixture_environment=() + if [ "${{ matrix.label }}" = "up-provider-microsandbox-managed-recreate" ]; then + command=(../hack/run-managed-policy-e2e.sh "${command[@]}") + fixture_environment=(DEVSY_REQUIRE_MSB_MANAGED_POLICY_FIXTURE=true "RUNNER_TEMP=${RUNNER_TEMP}") + fi + set +e + sudo \ + DEVSY_REQUIRE_MICROSANDBOX="${DEVSY_REQUIRE_MICROSANDBOX}" \ + GH_USERNAME="${GH_USERNAME}" \ + GH_ACCESS_TOKEN="${GH_ACCESS_TOKEN}" \ + GH_CREDENTIAL_USERNAME="${GH_CREDENTIAL_USERNAME}" \ + KUBECONFIG="${KUBECONFIG:-$HOME/.kube/config}" \ + ${DOCKER_HOST:+DOCKER_HOST="${DOCKER_HOST}"} \ + PATH="${PATH}" \ + "${fixture_environment[@]}" "${command[@]}" 2>&1 | tee "$test_log" + pipeline_status=("${PIPESTATUS[@]}") + set -e + test_status=${pipeline_status[0]} + log_status=${pipeline_status[1]} + chown_status=0 + if [ -e "$report" ]; then + sudo chown "$(id -u):$(id -g)" "$report" || chown_status=$? + fi + report_status=0 + if [ "$chown_status" -ne 0 ]; then + echo "::error::could not make Ginkgo report readable for artifact upload" + report_status="$chown_status" + else + python3 ../hack/microsandbox/verify_report.py \ + --report "$report" --label "${{ matrix.label }}" \ + --expected-count "${{ matrix.expected-spec-count }}" || report_status=$? + fi + if [ "$test_status" -ne 0 ]; then + exit "$test_status" + fi + if [ "$log_status" -ne 0 ]; then + exit "$log_status" + fi + exit "$report_status" + + - name: upload MicroSandbox test evidence + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: microsandbox-${{ matrix.label }}-evidence + path: | + ${{ runner.temp }}/microsandbox-${{ matrix.label }}.json + ${{ runner.temp }}/microsandbox-${{ matrix.label }}.log + if-no-files-found: warn + retention-days: 14 + integration-tests: name: Test ${{ matrix.label }}${{ (matrix.install-podman == 'rootless' || matrix.install-podman == 'rootful') && format(' ({0})', matrix.install-podman) || '' }} on ${{ matrix.runner }} needs: [changes, can-read-secret, build-cli] @@ -642,114 +807,6 @@ jobs: install-kind: true requires-secret: false - - label: up-provider-microsandbox - runner: ubuntu-latest - free-disk-space: false - install-kind: false - requires-secret: false - install-microsandbox: true - test-timeout: 1200s - job-timeout-minutes: 25 - - - label: up-provider-microsandbox-images - runner: ubuntu-latest - free-disk-space: false - install-kind: false - requires-secret: false - install-microsandbox: true - test-timeout: 1200s - job-timeout-minutes: 25 - - - label: up-provider-microsandbox-mounts - runner: ubuntu-latest - free-disk-space: false - install-kind: false - requires-secret: false - install-microsandbox: true - test-timeout: 1200s - job-timeout-minutes: 25 - - - label: up-provider-microsandbox-resources - runner: ubuntu-latest - free-disk-space: false - install-kind: false - requires-secret: false - install-microsandbox: true - test-timeout: 1200s - job-timeout-minutes: 25 - - - label: up-provider-microsandbox-storage - runner: ubuntu-latest - free-disk-space: false - install-kind: false - requires-secret: false - install-microsandbox: true - test-timeout: 1200s - job-timeout-minutes: 25 - - - label: up-provider-microsandbox-ephemeral - runner: ubuntu-latest - free-disk-space: false - install-kind: false - requires-secret: false - install-microsandbox: true - test-timeout: 1200s - job-timeout-minutes: 25 - - - label: up-provider-microsandbox-egress - runner: ubuntu-latest - free-disk-space: false - install-kind: false - requires-secret: false - install-microsandbox: true - test-timeout: 1200s - job-timeout-minutes: 25 - - - label: up-provider-microsandbox-prebuilds - runner: ubuntu-latest - free-disk-space: false - install-kind: false - requires-secret: false - install-microsandbox: true - test-timeout: 1200s - job-timeout-minutes: 25 - - - label: up-provider-microsandbox-dockerless - runner: ubuntu-latest - free-disk-space: false - install-kind: false - requires-secret: false - install-microsandbox: true - test-timeout: 1200s - job-timeout-minutes: 25 - - - label: up-provider-microsandbox-recreate - runner: ubuntu-latest - free-disk-space: false - install-kind: false - requires-secret: false - install-microsandbox: true - test-timeout: 1200s - job-timeout-minutes: 25 - - - label: up-provider-microsandbox-managed-recreate - runner: ubuntu-latest - free-disk-space: false - install-kind: false - requires-secret: false - install-microsandbox: true - test-timeout: 1200s - job-timeout-minutes: 25 - - - label: up-provider-microsandbox-logs-cancel - runner: ubuntu-latest - free-disk-space: false - install-kind: false - requires-secret: false - install-microsandbox: true - test-timeout: 1200s - job-timeout-minutes: 25 - # Snapshot tests - label: snapshot @@ -1053,37 +1110,6 @@ jobs: --crun-path /usr/local/bin/crun \ --bootstrap-timeout 270s - - name: cache microsandbox release bundle (Linux) - if: matrix.install-microsandbox == true && runner.os == 'Linux' - uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 - with: - path: ${{ runner.temp }}/microsandbox-linux-x86_64-v0.7.7.tar.gz - key: ${{ runner.os }}-${{ runner.arch }}-microsandbox-bundle-v0.7.7 - - - name: install checksum-pinned microsandbox (Linux) - if: matrix.install-microsandbox == true && runner.os == 'Linux' - timeout-minutes: 5 - run: | - archive="${RUNNER_TEMP}/microsandbox-linux-x86_64-v0.7.7.tar.gz" - if [ ! -f "$archive" ]; then - curl -fsSL --retry 3 -o "$archive" \ - https://github.com/superradcompany/microsandbox/releases/download/v0.7.7/microsandbox-linux-x86_64.tar.gz - fi - echo "b3cc4a5e3f52dfdd938a6f67ac4a9a959ddfe304bab56de4964044b8613f01bb $archive" | sha256sum -c - - staging="$(mktemp -d)" - trap 'rm -rf "$staging"' EXIT - tar -xzf "$archive" -C "$staging" - mkdir -p "$HOME/.microsandbox/bin" "$HOME/.microsandbox/lib" "$HOME/.local/bin" - install -m 755 "$staging/msb" "$HOME/.microsandbox/bin/msb" - install -m 644 "$staging/libkrunfw.so.5.6.1" "$HOME/.microsandbox/lib/" - ln -sf libkrunfw.so.5.6.1 "$HOME/.microsandbox/lib/libkrunfw.so.5" - ln -sf libkrunfw.so.5 "$HOME/.microsandbox/lib/libkrunfw.so" - ln -sf "$HOME/.microsandbox/bin/msb" "$HOME/.local/bin/msb" - echo "$HOME/.local/bin" >> "$GITHUB_PATH" - "$HOME/.local/bin/msb" --version - sudo test -r /dev/kvm - sudo test -w /dev/kvm - - name: remove docker if: matrix.label == 'docker-install' && (matrix.requires-secret == false || needs.can-read-secret.outputs.secret-set == 'true') run: | @@ -1169,7 +1195,6 @@ jobs: GH_CREDENTIAL_USERNAME: x-access-token TEST_TIMEOUT: ${{ matrix.test-timeout || '1500s' }} FLAKE_ATTEMPTS: ${{ matrix.flake-attempts || '1' }} - DEVSY_REQUIRE_MICROSANDBOX: ${{ matrix.install-microsandbox == true && 'true' || 'false' }} run: | if [ "${{ runner.os }}" == "Linux" ]; then if [ "${{ matrix.install-podman || '' }}" = "rootless" ]; then @@ -1185,30 +1210,14 @@ jobs: PATH="${PATH}" \ ./e2e.test -test.v -ginkgo.v -test.timeout "${TEST_TIMEOUT}" -ginkgo.fail-on-empty -ginkgo.flake-attempts="${FLAKE_ATTEMPTS}" --ginkgo.label-filter="${{ matrix.label }}" else - if [ "${{ matrix.label }}" = "up-provider-microsandbox-managed-recreate" ]; then - sudo \ - DEVSY_REQUIRE_MICROSANDBOX="${DEVSY_REQUIRE_MICROSANDBOX}" \ - DEVSY_REQUIRE_MSB_MANAGED_POLICY_FIXTURE="true" \ - GH_USERNAME="${GH_USERNAME}" \ - GH_ACCESS_TOKEN="${GH_ACCESS_TOKEN}" \ - GH_CREDENTIAL_USERNAME="${GH_CREDENTIAL_USERNAME}" \ - KUBECONFIG="${KUBECONFIG:-$HOME/.kube/config}" \ - ${DOCKER_HOST:+DOCKER_HOST="${DOCKER_HOST}"} \ - PATH="${PATH}" \ - RUNNER_TEMP="${RUNNER_TEMP}" \ - ../hack/run-managed-policy-e2e.sh \ - ./e2e.test -test.v -ginkgo.v -test.timeout "${TEST_TIMEOUT}" -ginkgo.fail-on-empty -ginkgo.flake-attempts="${FLAKE_ATTEMPTS}" --ginkgo.label-filter="${{ matrix.label }}" - else - sudo \ - DEVSY_REQUIRE_MICROSANDBOX="${DEVSY_REQUIRE_MICROSANDBOX}" \ - GH_USERNAME="${GH_USERNAME}" \ - GH_ACCESS_TOKEN="${GH_ACCESS_TOKEN}" \ - GH_CREDENTIAL_USERNAME="${GH_CREDENTIAL_USERNAME}" \ - KUBECONFIG="${KUBECONFIG:-$HOME/.kube/config}" \ - ${DOCKER_HOST:+DOCKER_HOST="${DOCKER_HOST}"} \ - PATH="${PATH}" \ - ./e2e.test -test.v -ginkgo.v -test.timeout "${TEST_TIMEOUT}" -ginkgo.fail-on-empty -ginkgo.flake-attempts="${FLAKE_ATTEMPTS}" --ginkgo.label-filter="${{ matrix.label }}" - fi + sudo \ + GH_USERNAME="${GH_USERNAME}" \ + GH_ACCESS_TOKEN="${GH_ACCESS_TOKEN}" \ + GH_CREDENTIAL_USERNAME="${GH_CREDENTIAL_USERNAME}" \ + KUBECONFIG="${KUBECONFIG:-$HOME/.kube/config}" \ + ${DOCKER_HOST:+DOCKER_HOST="${DOCKER_HOST}"} \ + PATH="${PATH}" \ + ./e2e.test -test.v -ginkgo.v -test.timeout "${TEST_TIMEOUT}" -ginkgo.fail-on-empty -ginkgo.flake-attempts="${FLAKE_ATTEMPTS}" --ginkgo.label-filter="${{ matrix.label }}" fi else GH_USERNAME="${GH_USERNAME}" \ @@ -1238,6 +1247,7 @@ jobs: - build-cli - integration-tests-unprivileged - integration-tests + - integration-tests-microsandbox steps: - name: Check required jobs run: | @@ -1274,5 +1284,6 @@ jobs: check_result "build-cli" "${{ needs.build-cli.result }}" true || status=1 check_result "integration-tests-unprivileged" "${{ needs.integration-tests-unprivileged.result }}" true || status=1 check_result "integration-tests" "${{ needs.integration-tests.result }}" true || status=1 + check_result "integration-tests-microsandbox" "${{ needs.integration-tests-microsandbox.result }}" true || status=1 exit "$status" diff --git a/e2e/tests/up/MICROSANDBOX.md b/e2e/tests/up/MICROSANDBOX.md new file mode 100644 index 0000000000..54d42bba27 --- /dev/null +++ b/e2e/tests/up/MICROSANDBOX.md @@ -0,0 +1,66 @@ +# MicroSandbox integration tests + +Each capability has a Ginkgo label and scenario file. The built-in and external +providers run the same contracts. Keep scenario assertions in those files so +resource, ownership and lifecycle expectations remain visible. + +## Shared setup + +Use `microsandboxTestSession` for runtime prerequisites, isolated Devsy +configuration, the CLI framework, temporary workspaces and provider registration. +The shared provider selector supplies the pinned source, runtime identity and +provider label while callers retain their descriptive provider names. + +Workspace cleanup registration is explicit. Ginkgo cleanup runs in reverse +order; register volume cleanup before workspace cleanup so the VM is torn down +first. Specialized fixtures own their volumes, registries, endpoints, processes +and environment overrides. Register independent cleanup callbacks when every +resource needs a cleanup attempt after failure. + +The image, mount, resource, storage and recreation fixtures share session setup. +Other fixtures may adopt it when touched, preserving their existing resource +ownership and cleanup order. Do not put scenario switches or arbitrary cleanup +callbacks into the session. + +## Continuous integration + +The dedicated `integration-tests-microsandbox` job in +`.github/workflows/pr-ci.yml` owns the suite catalogue and common setup. Each +suite still gets a fresh runner, a 25-minute job limit and a 20-minute test limit, +with one attempt. The checked runtime bundle, build artifacts, required KVM gate +and credentials are configured once in the job definition. + +Each matrix entry names its Ginkgo label and expected scenario count. +`hack/microsandbox/verify_report.py` requires a real successful run with that +exact count, split equally between the explicit `microsandbox-builtin` and +`microsandbox-external` labels. Missing cases, skips, retries, dry runs and +programmatic focus fail the gate. Reports and test output are retained even when +the test command fails. + +To add a scenario: + +1. Extend the appropriate capability file using the shared provider selector. +2. Keep a focused scenario deadline and explicit expected behavior. +3. Update the existing suite's expected count in the catalogue. +4. Run that label locally and obtain actual supported-runtime CI evidence. + +A new suite needs a catalogue entry only when it has a separate contract or +isolation requirement. Avoid copying runner setup or adding a job for each +individual assertion. + +## Managed policy boundary + +Managed-policy recreation is Linux-only and runs through +`hack/run-managed-policy-e2e.sh`. It overlays `/etc` in a private mount namespace +before writing policy and validates namespace identity, owned overlay paths, +ownership and file modes. Ordinary suites never enter that fixture. + +Both recreation suites use `safeRecreateFixture` for host-state snapshots and +replacement assertions. Inspect a rejected stopped VM before any command can +resume it. A later successful replacement must change the VM generation, clear +the old root marker, preserve external data and records, and expose the requested +resources. + +See the runtime protocol documentation for supported runtime versions, individual +suite commands and the isolated managed-policy command. A dry run proves +selection only; it does not prove VM behavior. diff --git a/e2e/tests/up/microsandbox_session.go b/e2e/tests/up/microsandbox_session.go new file mode 100644 index 0000000000..0bf10a17e3 --- /dev/null +++ b/e2e/tests/up/microsandbox_session.go @@ -0,0 +1,131 @@ +package up + +import ( + "context" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "time" + + "github.com/blang/semver/v4" + "github.com/devsy-org/devsy/e2e/framework" + "github.com/devsy-org/devsy/pkg/provider" + "github.com/onsi/ginkgo/v2" +) + +const ( + microsandboxResourceBuiltin = "microsandbox" + osLinux = "linux" + microsandboxExternalProvider = "github.com/devsy-org/devsy-provider-microsandbox@v0.1.8" + microsandboxRootUser = "root" +) + +func skipIfNoMicrosandbox(ctx context.Context) { + checkMicrosandboxVersion(ctx) + switch { + case runtime.GOOS == osLinux: + kvm, err := os.OpenFile("/dev/kvm", os.O_RDWR, 0) + if err != nil { + microsandboxUnavailable("microsandbox requires KVM (/dev/kvm not accessible)") + } + _ = kvm.Close() + case runtime.GOOS == "darwin" && runtime.GOARCH == "arm64": + // Apple silicon supports microsandbox via the hypervisor framework. + default: + microsandboxUnavailable("microsandbox requires Apple silicon or Linux with KVM") + } +} + +func checkMicrosandboxVersion(ctx context.Context) { + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + if _, err := exec.LookPath("msb"); err != nil { + microsandboxUnavailable("microsandbox runtime (msb) not found on PATH") + } + output, err := exec.CommandContext(ctx, "msb", "--version").Output() + if err != nil { + microsandboxUnavailable("microsandbox runtime (msb) cannot execute: " + err.Error()) + } + version, err := semver.ParseTolerant( + strings.TrimPrefix(strings.TrimSpace(string(output)), "msb "), + ) + if err != nil || version.LT(semver.Version{Major: 0, Minor: 7, Patch: 7}) { + microsandboxUnavailable( + "microsandbox parity requires msb v0.7.7 or newer: " + string(output), + ) + } +} + +func microsandboxUnavailable(reason string) { + if os.Getenv("DEVSY_REQUIRE_MICROSANDBOX") == "true" { + ginkgo.Fail(reason) + } + ginkgo.Skip(reason) +} + +type microsandboxProviderCase struct { + name, source, runtime, label string +} + +func microsandboxProviders(builtinName, externalName string) []microsandboxProviderCase { + return []microsandboxProviderCase{ + { + name: builtinName, + source: microsandboxResourceBuiltin, + runtime: provider.MicrosandboxDriver, + label: "microsandbox-builtin", + }, + { + name: externalName, + source: microsandboxExternalProvider, + runtime: provider.ExternalDriver, + label: "microsandbox-external", + }, + } +} + +type microsandboxTestSession struct { + framework *framework.Framework + initialDir string +} + +func newMicrosandboxTestSession(ctx context.Context) *microsandboxTestSession { + skipIfNoMicrosandbox(ctx) + ginkgo.GinkgoT().Setenv("DEVSY_HOME", ginkgo.GinkgoT().TempDir()) + ginkgo.GinkgoT().Setenv("DEVSY_CONFIG", "") + initialDir, err := os.Getwd() + framework.ExpectNoError(err) + return µsandboxTestSession{ + framework: framework.NewDefaultFramework(filepath.Join(initialDir, "bin")), + initialDir: initialDir, + } +} + +func (s *microsandboxTestSession) newWorkspace() string { + workspace, err := framework.CreateTempDir() + framework.ExpectNoError(err) + ginkgo.DeferCleanup(framework.CleanupTempDir, s.initialDir, workspace) + return workspace +} + +func (s *microsandboxTestSession) copyWorkspace(source string) string { + workspace, err := framework.CopyToTempDir(source) + framework.ExpectNoError(err) + ginkgo.DeferCleanup(framework.CleanupTempDir, s.initialDir, workspace) + return workspace +} + +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) + defer cancel() + return s.framework.DevsyProviderDelete(cleanupCtx, name) + }) +} + +func (s *microsandboxTestSession) cleanupWorkspace(workspace string) { + ginkgo.DeferCleanup(s.framework.CleanupWorkspace, workspace) +} diff --git a/e2e/tests/up/provider_microsandbox.go b/e2e/tests/up/provider_microsandbox.go index cff5cb0f4a..bcce747670 100644 --- a/e2e/tests/up/provider_microsandbox.go +++ b/e2e/tests/up/provider_microsandbox.go @@ -13,7 +13,6 @@ import ( "strings" "time" - "github.com/blang/semver/v4" "github.com/devsy-org/devsy/e2e/framework" "github.com/devsy-org/devsy/pkg/devcontainer" "github.com/devsy-org/devsy/pkg/docker" @@ -21,55 +20,6 @@ import ( "github.com/onsi/gomega" ) -const ( - osLinux = "linux" - microsandboxExternalProvider = "github.com/devsy-org/devsy-provider-microsandbox@v0.1.8" - microsandboxRootUser = "root" -) - -func skipIfNoMicrosandbox(ctx context.Context) { - checkMicrosandboxVersion(ctx) - switch { - case runtime.GOOS == osLinux: - kvm, err := os.OpenFile("/dev/kvm", os.O_RDWR, 0) - if err != nil { - microsandboxUnavailable("microsandbox requires KVM (/dev/kvm not accessible)") - } - _ = kvm.Close() - case runtime.GOOS == "darwin" && runtime.GOARCH == "arm64": - // Apple silicon supports microsandbox via the hypervisor framework. - default: - microsandboxUnavailable("microsandbox requires Apple silicon or Linux with KVM") - } -} - -func checkMicrosandboxVersion(ctx context.Context) { - ctx, cancel := context.WithTimeout(ctx, 10*time.Second) - defer cancel() - if _, err := exec.LookPath("msb"); err != nil { - microsandboxUnavailable("microsandbox runtime (msb) not found on PATH") - } - output, err := exec.CommandContext(ctx, "msb", "--version").Output() - if err != nil { - microsandboxUnavailable("microsandbox runtime (msb) cannot execute: " + err.Error()) - } - version, err := semver.ParseTolerant( - strings.TrimPrefix(strings.TrimSpace(string(output)), "msb "), - ) - if err != nil || version.LT(semver.Version{Major: 0, Minor: 7, Patch: 7}) { - microsandboxUnavailable( - "microsandbox parity requires msb v0.7.7 or newer: " + string(output), - ) - } -} - -func microsandboxUnavailable(reason string) { - if os.Getenv("DEVSY_REQUIRE_MICROSANDBOX") == "true" { - ginkgo.Fail(reason) - } - ginkgo.Skip(reason) -} - var _ = ginkgo.Describe( "testing up command for microsandbox provider", ginkgo.Label("up-provider-microsandbox"), @@ -211,12 +161,12 @@ var _ = ginkgo.Describe( gomega.Expect(string(output)).NotTo(gomega.ContainSubstring(sandbox)) }, ginkgo.Entry("built-in", "microsandbox", "microsandbox-builtin-parity", - ginkgo.SpecTimeout(framework.TimeoutLong())), + ginkgo.Label("microsandbox-builtin"), ginkgo.SpecTimeout(framework.TimeoutLong())), ginkgo.Entry( "external v0.1.8", microsandboxExternalProvider, "microsandbox-external-parity", - ginkgo.SpecTimeout(framework.TimeoutLong()), + ginkgo.Label("microsandbox-external"), ginkgo.SpecTimeout(framework.TimeoutLong()), ), ) }, diff --git a/e2e/tests/up/provider_microsandbox_dockerless.go b/e2e/tests/up/provider_microsandbox_dockerless.go index 87680302e5..9f8f5ff601 100644 --- a/e2e/tests/up/provider_microsandbox_dockerless.go +++ b/e2e/tests/up/provider_microsandbox_dockerless.go @@ -58,11 +58,8 @@ type microsandboxDockerlessFixtureState struct { var _ = ginkgo.Describe("microsandbox Dockerless parity", ginkgo.Label("up-provider-microsandbox-dockerless"), func() { - for _, provider := range []struct{ name, source string }{ - {"builtin-dockerless", microsandboxResourceBuiltin}, - {"external-dockerless", microsandboxExternalProvider}, - } { - ginkgo.Context(provider.name, func() { + for _, provider := range microsandboxProviders("builtin-dockerless", "external-dockerless") { + ginkgo.Context(provider.name, ginkgo.Label(provider.label), func() { ginkgo.DescribeTable( "runs without host Docker and enforces developer ownership", func(ctx context.Context, testCase microsandboxDockerlessCase) { diff --git a/e2e/tests/up/provider_microsandbox_egress.go b/e2e/tests/up/provider_microsandbox_egress.go index 9c45f134f7..274226043f 100644 --- a/e2e/tests/up/provider_microsandbox_egress.go +++ b/e2e/tests/up/provider_microsandbox_egress.go @@ -41,11 +41,8 @@ type microsandboxEgressEndpoint struct { var _ = ginkgo.Describe("microsandbox egress parity", ginkgo.Label("up-provider-microsandbox-egress"), func() { - for _, provider := range []struct{ name, source string }{ - {"builtin-egress", microsandboxResourceBuiltin}, - {"external-egress", microsandboxExternalProvider}, - } { - ginkgo.Context(provider.name, func() { + for _, provider := range microsandboxProviders("builtin-egress", "external-egress") { + ginkgo.Context(provider.name, ginkgo.Label(provider.label), func() { var f *framework.Framework var workspace, initialDir string var endpoint *microsandboxEgressEndpoint diff --git a/e2e/tests/up/provider_microsandbox_ephemeral.go b/e2e/tests/up/provider_microsandbox_ephemeral.go index 15f287b6c9..4d398a2848 100644 --- a/e2e/tests/up/provider_microsandbox_ephemeral.go +++ b/e2e/tests/up/provider_microsandbox_ephemeral.go @@ -22,11 +22,8 @@ type microsandboxEphemeralCase struct { var _ = ginkgo.Describe("microsandbox ephemeral root parity", ginkgo.Label("up-provider-microsandbox-ephemeral"), func() { - for _, provider := range []struct{ name, source string }{ - {"builtin-ephemeral", microsandboxResourceBuiltin}, - {"external-ephemeral", microsandboxExternalProvider}, - } { - ginkgo.Context(provider.name, func() { + for _, provider := range microsandboxProviders("builtin-ephemeral", "external-ephemeral") { + ginkgo.Context(provider.name, ginkgo.Label(provider.label), func() { var f *framework.Framework var workspace, volume, ownedLabel string var volumeOwned bool diff --git a/e2e/tests/up/provider_microsandbox_images.go b/e2e/tests/up/provider_microsandbox_images.go index aafeaef9e7..36123a3487 100644 --- a/e2e/tests/up/provider_microsandbox_images.go +++ b/e2e/tests/up/provider_microsandbox_images.go @@ -27,29 +27,17 @@ const microsandboxImageFixture = "tests/up/testdata/microsandbox-images" var _ = ginkgo.Describe("microsandbox image backend parity", ginkgo.Label("up-provider-microsandbox-images"), func() { - for _, provider := range []struct{ name, source string }{ - {"builtin", "microsandbox"}, - {"external", microsandboxExternalProvider}, - } { - ginkgo.Context(provider.name, func() { + for _, provider := range microsandboxProviders("builtin", "external") { + ginkgo.Context(provider.name, ginkgo.Label(provider.label), func() { var f *framework.Framework var tempDir string ginkgo.BeforeEach(func(ctx context.Context) { - skipIfNoMicrosandbox(ctx) - ginkgo.GinkgoT().Setenv("DEVSY_HOME", ginkgo.GinkgoT().TempDir()) - ginkgo.GinkgoT().Setenv("DEVSY_CONFIG", "") - initialDir, err := os.Getwd() - framework.ExpectNoError(err) - f = framework.NewDefaultFramework(filepath.Join(initialDir, "bin")) - tempDir, err = framework.CopyToTempDir(microsandboxImageFixture) - framework.ExpectNoError(err) - ginkgo.DeferCleanup(framework.CleanupTempDir, initialDir, tempDir) - framework.ExpectNoError( - f.DevsyProviderAdd(ctx, provider.source, "--name", provider.name), - ) - ginkgo.DeferCleanup(f.DevsyProviderDelete, provider.name) - ginkgo.DeferCleanup(f.CleanupWorkspace, tempDir) + session := newMicrosandboxTestSession(ctx) + f = session.framework + tempDir = session.copyWorkspace(microsandboxImageFixture) + session.addProvider(ctx, provider.source, provider.name) + session.cleanupWorkspace(tempDir) }) ginkgo.It( diff --git a/e2e/tests/up/provider_microsandbox_logs_cancel.go b/e2e/tests/up/provider_microsandbox_logs_cancel.go index 41cc08ca24..474ba9e64c 100644 --- a/e2e/tests/up/provider_microsandbox_logs_cancel.go +++ b/e2e/tests/up/provider_microsandbox_logs_cancel.go @@ -75,11 +75,8 @@ type microsandboxLogCancelFixture struct { var _ = ginkgo.Describe("microsandbox logs and cancellation parity", ginkgo.Label("up-provider-microsandbox-logs-cancel"), func() { - for _, selection := range []struct{ name, source, runtime string }{ - {"builtin-logs-cancel", microsandboxResourceBuiltin, provider.MicrosandboxDriver}, - {"external-logs-cancel", microsandboxExternalProvider, provider.ExternalDriver}, - } { - ginkgo.Context(selection.name, func() { + for _, selection := range microsandboxProviders("builtin-logs-cancel", "external-logs-cancel") { + ginkgo.Context(selection.name, ginkgo.Label(selection.label), func() { var fixture *microsandboxLogCancelFixture ginkgo.BeforeEach(func(ctx context.Context) { fixture = newMicrosandboxLogCancelFixture(ctx, selection.name, selection.source) diff --git a/e2e/tests/up/provider_microsandbox_managed_recreate_linux.go b/e2e/tests/up/provider_microsandbox_managed_recreate_linux.go index 6d06f4a86d..ea4636526e 100644 --- a/e2e/tests/up/provider_microsandbox_managed_recreate_linux.go +++ b/e2e/tests/up/provider_microsandbox_managed_recreate_linux.go @@ -64,55 +64,35 @@ var _ = ginkgo.Describe("MicroSandbox managed-policy rejected replacement", framework.ExpectNoError(fixture.framework.DevsyUpRecreate( ctx, append([]string{fixture.workspace}, fixture.upArgs()...)..., )) - after := inspectSafeRecreate(ctx, fixture.sandbox) - gomega.Expect(after.Status).To(gomega.Equal("Running")) - gomega.Expect(after.CreatedAt).NotTo(gomega.Equal(fixture.original.CreatedAt)) - gomega.Expect(microsandboxStorageBootID(ctx, fixture.framework, fixture.workspace)). - NotTo(gomega.Equal(fixture.bootID)) - out, err = safeRecreateExec(ctx, fixture.sandbox, "sh", "-c", - `test ! -e "$1"`, "sh", safeRecreateRootPath) - framework.ExpectNoError(err, string(out)) - fixture.assertPersistentMounts(ctx) - fixture.assertHostState(ctx) - expected := microsandboxResources{ - CPUs: 1, MemoryMiB: 2048, MaxCPUs: 1, MaxMemoryMiB: 2048, - } - gomega.Expect(microsandboxActiveResources(ctx, fixture.sandbox)). - To(gomega.Equal(expected)) - assertMicrosandboxGuestResources( - ctx, - fixture.framework, - fixture.workspace, - expected, - ) + fixture.assertReplacement(ctx, 2048) }, ginkgo.Entry( "builtin running", microsandboxResourceBuiltin, - "safe-recreate-managed-builtin-running", + "msb-managed-builtin-running", false, - ginkgo.SpecTimeout(framework.TimeoutLong()), + ginkgo.Label("microsandbox-builtin"), ginkgo.SpecTimeout(framework.TimeoutLong()), ), ginkgo.Entry( "builtin stopped", microsandboxResourceBuiltin, - "safe-recreate-managed-builtin-stopped", + "msb-managed-builtin-stopped", true, - ginkgo.SpecTimeout(framework.TimeoutLong()), + ginkgo.Label("microsandbox-builtin"), ginkgo.SpecTimeout(framework.TimeoutLong()), ), ginkgo.Entry( "external running", microsandboxExternalProvider, - "safe-recreate-managed-external-running", + "msb-managed-external-running", false, - ginkgo.SpecTimeout(framework.TimeoutLong()), + ginkgo.Label("microsandbox-external"), ginkgo.SpecTimeout(framework.TimeoutLong()), ), ginkgo.Entry( "external stopped", microsandboxExternalProvider, - "safe-recreate-managed-external-stopped", + "msb-managed-external-stopped", true, - ginkgo.SpecTimeout(framework.TimeoutLong()), + ginkgo.Label("microsandbox-external"), ginkgo.SpecTimeout(framework.TimeoutLong()), ), ) }) diff --git a/e2e/tests/up/provider_microsandbox_mounts.go b/e2e/tests/up/provider_microsandbox_mounts.go index 56cfe7543f..bedc97276e 100644 --- a/e2e/tests/up/provider_microsandbox_mounts.go +++ b/e2e/tests/up/provider_microsandbox_mounts.go @@ -17,36 +17,24 @@ import ( var _ = ginkgo.Describe("microsandbox mount parity", ginkgo.Label("up-provider-microsandbox-mounts"), func() { - for _, provider := range []struct{ name, source string }{ - {"builtin", "microsandbox"}, - {"external", microsandboxExternalProvider}, - } { - ginkgo.Context(provider.name, func() { + for _, provider := range microsandboxProviders("builtin", "external") { + ginkgo.Context(provider.name, ginkgo.Label(provider.label), func() { var f *framework.Framework var workspace, volume string ginkgo.BeforeEach(func(ctx context.Context) { - skipIfNoMicrosandbox(ctx) - ginkgo.GinkgoT().Setenv("DEVSY_HOME", ginkgo.GinkgoT().TempDir()) - ginkgo.GinkgoT().Setenv("DEVSY_CONFIG", "") - initialDir, err := os.Getwd() - framework.ExpectNoError(err) - f = framework.NewDefaultFramework(filepath.Join(initialDir, "bin")) - workspace, err = framework.CreateTempDir() - framework.ExpectNoError(err) + session := newMicrosandboxTestSession(ctx) + f = session.framework + workspace = session.newWorkspace() volume = "" - ginkgo.DeferCleanup(framework.CleanupTempDir, initialDir, workspace) - framework.ExpectNoError( - f.DevsyProviderAdd(ctx, provider.source, "--name", provider.name), - ) - ginkgo.DeferCleanup(f.DevsyProviderDelete, provider.name) + session.addProvider(ctx, provider.source, provider.name) // Cleanup is LIFO: tear down the VM before attempting volume removal. ginkgo.DeferCleanup(func(cleanupCtx context.Context) { if volume != "" { microsandboxMountCommand(cleanupCtx, "volume", "rm", volume) } }) - ginkgo.DeferCleanup(f.CleanupWorkspace, workspace) + session.cleanupWorkspace(workspace) }) ginkgo.It( diff --git a/e2e/tests/up/provider_microsandbox_prebuilds.go b/e2e/tests/up/provider_microsandbox_prebuilds.go index e7cdef432b..5dc273d150 100644 --- a/e2e/tests/up/provider_microsandbox_prebuilds.go +++ b/e2e/tests/up/provider_microsandbox_prebuilds.go @@ -27,11 +27,8 @@ const microsandboxPrebuildFixture = "tests/up/testdata/microsandbox-prebuilds" var _ = ginkgo.Describe("microsandbox prebuild parity", ginkgo.Label("up-provider-microsandbox-prebuilds"), func() { - for _, provider := range []struct{ name, source string }{ - {"prebuild-builtin", microsandboxResourceBuiltin}, - {"prebuild-external", microsandboxExternalProvider}, - } { - ginkgo.Context(provider.name, func() { + for _, provider := range microsandboxProviders("prebuild-builtin", "prebuild-external") { + ginkgo.Context(provider.name, ginkgo.Label(provider.label), func() { ginkgo.DescribeTable( "selects prebuilds by input hash", func(ctx context.Context, changed bool) { diff --git a/e2e/tests/up/provider_microsandbox_recreate.go b/e2e/tests/up/provider_microsandbox_recreate.go index 92b224f638..4b6b6f232f 100644 --- a/e2e/tests/up/provider_microsandbox_recreate.go +++ b/e2e/tests/up/provider_microsandbox_recreate.go @@ -46,28 +46,28 @@ var _ = ginkgo.Describe("MicroSandbox rejected replacement preservation", microsandboxResourceBuiltin, "safe-recreate-builtin-running", false, - ginkgo.SpecTimeout(framework.TimeoutLong()), + ginkgo.Label("microsandbox-builtin"), ginkgo.SpecTimeout(framework.TimeoutLong()), ), ginkgo.Entry( "builtin stopped", microsandboxResourceBuiltin, "safe-recreate-builtin-stopped", true, - ginkgo.SpecTimeout(framework.TimeoutLong()), + ginkgo.Label("microsandbox-builtin"), ginkgo.SpecTimeout(framework.TimeoutLong()), ), ginkgo.Entry( "external running", microsandboxExternalProvider, "safe-recreate-external-running", false, - ginkgo.SpecTimeout(framework.TimeoutLong()), + ginkgo.Label("microsandbox-external"), ginkgo.SpecTimeout(framework.TimeoutLong()), ), ginkgo.Entry( "external stopped", microsandboxExternalProvider, "safe-recreate-external-stopped", true, - ginkgo.SpecTimeout(framework.TimeoutLong()), + ginkgo.Label("microsandbox-external"), ginkgo.SpecTimeout(framework.TimeoutLong()), ), ) }) @@ -105,26 +105,15 @@ func newSafeRecreateFixture( source, name string, maxMemoryMiB int, ) *safeRecreateFixture { - skipIfNoMicrosandbox(ctx) - initialDir, err := os.Getwd() - framework.ExpectNoError(err) + session := newMicrosandboxTestSession(ctx) home := ginkgo.GinkgoT().TempDir() ginkgo.GinkgoT().Setenv("HOME", home) ginkgo.GinkgoT().Setenv("USERPROFILE", home) - ginkgo.GinkgoT().Setenv("DEVSY_HOME", ginkgo.GinkgoT().TempDir()) - ginkgo.GinkgoT().Setenv("DEVSY_CONFIG", "") ginkgo.GinkgoT().Setenv("MSB_HOME", filepath.Join(home, "microsandbox")) ginkgo.GinkgoT().Setenv("MSB_CONFIG_PATH", filepath.Join(home, "microsandbox", "config.json")) - workspace, err := framework.CreateTempDir() - framework.ExpectNoError(err) - ginkgo.DeferCleanup(framework.CleanupTempDir, initialDir, workspace) - f := framework.NewDefaultFramework(filepath.Join(initialDir, "bin")) - framework.ExpectNoError(f.DevsyProviderAdd(ctx, source, "--name", name)) - ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { - cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), time.Minute) - defer cancel() - return f.DevsyProviderDelete(cleanupCtx, name) - }) + workspace := session.newWorkspace() + f := session.framework + session.addProvider(ctx, source, name) framework.ExpectNoError(f.DevsyProviderUse(ctx, name, microsandboxResourceOption, "MICROSANDBOX_MEMORY=0", microsandboxResourceOption, fmt.Sprintf("MICROSANDBOX_MAX_MEMORY=%d", maxMemoryMiB), @@ -318,6 +307,10 @@ func (f *safeRecreateFixture) replaceSuccessfully(ctx context.Context) { framework.ExpectNoError( f.framework.DevsyUpRecreate(ctx, append([]string{f.workspace}, f.upArgs()...)...), ) + f.assertReplacement(ctx, 1536) +} + +func (f *safeRecreateFixture) assertReplacement(ctx context.Context, memoryMiB int) { after := inspectSafeRecreate(ctx, f.sandbox) gomega.Expect(after.Status).To(gomega.Equal("Running")) gomega.Expect(after.CreatedAt).NotTo(gomega.Equal(f.original.CreatedAt)) @@ -337,7 +330,7 @@ func (f *safeRecreateFixture) replaceSuccessfully(ctx context.Context) { f.assertHostState(ctx) expected := microsandboxResources{ CPUs: 1, - MemoryMiB: 1536, + MemoryMiB: memoryMiB, MaxCPUs: 1, MaxMemoryMiB: f.maxMemoryMiB, } diff --git a/e2e/tests/up/provider_microsandbox_resources.go b/e2e/tests/up/provider_microsandbox_resources.go index 8b1505ff47..40d6440661 100644 --- a/e2e/tests/up/provider_microsandbox_resources.go +++ b/e2e/tests/up/provider_microsandbox_resources.go @@ -18,10 +18,9 @@ import ( ) const ( - microsandboxResourceBuiltin = "microsandbox" - microsandboxResourceOption = "--option" - microsandboxResourceJSON = "json" - microsandboxResourceImage = "ghcr.io/devsy-org/test-images/base:alpine" + microsandboxResourceOption = "--option" + microsandboxResourceJSON = "json" + microsandboxResourceImage = "ghcr.io/devsy-org/test-images/base:alpine" ) type microsandboxResources struct { @@ -47,29 +46,17 @@ type microsandboxResourceConfig struct { var _ = ginkgo.Describe("microsandbox resource parity", ginkgo.Label("up-provider-microsandbox-resources"), func() { - for _, provider := range []struct{ name, source string }{ - {"builtin-resources", microsandboxResourceBuiltin}, - {"external-resources", microsandboxExternalProvider}, - } { - ginkgo.Context(provider.name, func() { + for _, provider := range microsandboxProviders("builtin-resources", "external-resources") { + ginkgo.Context(provider.name, ginkgo.Label(provider.label), func() { var f *framework.Framework var workspace string ginkgo.BeforeEach(func(ctx context.Context) { - skipIfNoMicrosandbox(ctx) - ginkgo.GinkgoT().Setenv("DEVSY_HOME", ginkgo.GinkgoT().TempDir()) - ginkgo.GinkgoT().Setenv("DEVSY_CONFIG", "") - initialDir, err := os.Getwd() - framework.ExpectNoError(err) - f = framework.NewDefaultFramework(filepath.Join(initialDir, "bin")) - workspace, err = framework.CreateTempDir() - framework.ExpectNoError(err) - ginkgo.DeferCleanup(framework.CleanupTempDir, initialDir, workspace) - framework.ExpectNoError( - f.DevsyProviderAdd(ctx, provider.source, "--name", provider.name), - ) - ginkgo.DeferCleanup(f.DevsyProviderDelete, provider.name) - ginkgo.DeferCleanup(f.CleanupWorkspace, workspace) + session := newMicrosandboxTestSession(ctx) + f = session.framework + workspace = session.newWorkspace() + session.addProvider(ctx, provider.source, provider.name) + session.cleanupWorkspace(workspace) }) ginkgo.DescribeTable( diff --git a/e2e/tests/up/provider_microsandbox_storage.go b/e2e/tests/up/provider_microsandbox_storage.go index f0e60ee3f0..d022e98678 100644 --- a/e2e/tests/up/provider_microsandbox_storage.go +++ b/e2e/tests/up/provider_microsandbox_storage.go @@ -7,7 +7,6 @@ import ( "os" "path/filepath" "strings" - "time" "github.com/devsy-org/devsy/e2e/framework" "github.com/devsy-org/devsy/pkg/devcontainer/config" @@ -36,36 +35,17 @@ type microsandboxStorageDisk struct { var _ = ginkgo.Describe("microsandbox storage parity", ginkgo.Label("up-provider-microsandbox-storage"), func() { - for _, provider := range []struct{ name, source string }{ - {"builtin-storage", microsandboxResourceBuiltin}, - {"external-storage", microsandboxExternalProvider}, - } { - ginkgo.Context(provider.name, func() { + for _, provider := range microsandboxProviders("builtin-storage", "external-storage") { + ginkgo.Context(provider.name, ginkgo.Label(provider.label), func() { var f *framework.Framework var workspace string ginkgo.BeforeEach(func(ctx context.Context) { - skipIfNoMicrosandbox(ctx) - ginkgo.GinkgoT().Setenv("DEVSY_HOME", ginkgo.GinkgoT().TempDir()) - ginkgo.GinkgoT().Setenv("DEVSY_CONFIG", "") - initialDir, err := os.Getwd() - framework.ExpectNoError(err) - f = framework.NewDefaultFramework(filepath.Join(initialDir, "bin")) - workspace, err = framework.CreateTempDir() - framework.ExpectNoError(err) - ginkgo.DeferCleanup(framework.CleanupTempDir, initialDir, workspace) - framework.ExpectNoError( - f.DevsyProviderAdd(ctx, provider.source, "--name", provider.name), - ) - ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { - cleanupCtx, cancel := context.WithTimeout( - context.WithoutCancel(cleanupCtx), - time.Minute, - ) - defer cancel() - return f.DevsyProviderDelete(cleanupCtx, provider.name) - }) - ginkgo.DeferCleanup(f.CleanupWorkspace, workspace) + session := newMicrosandboxTestSession(ctx) + f = session.framework + workspace = session.newWorkspace() + session.addProvider(ctx, provider.source, provider.name) + session.cleanupWorkspace(workspace) }) ginkgo.DescribeTable( diff --git a/hack/microsandbox/test_verify_report.py b/hack/microsandbox/test_verify_report.py new file mode 100644 index 0000000000..15a2720cfb --- /dev/null +++ b/hack/microsandbox/test_verify_report.py @@ -0,0 +1,125 @@ +import json +from pathlib import Path +import tempfile +import unittest + +from verify_report import validate_report + + +LABEL = "up-provider-microsandbox-images" +PROVIDERS = ("microsandbox-builtin", "microsandbox-external") + + +def report(count=2): + return [{ + "SuiteConfig": {"DryRun": False}, + "SuiteSucceeded": True, + "SuiteHasProgrammaticFocus": False, + "SpecReports": [ + { + "ContainerHierarchyLabels": [[LABEL]], + "LeafNodeLabels": [PROVIDERS[index % 2]], + "LeafNodeType": "It", + "State": "passed", + "NumAttempts": 1, + } + for index in range(count) + ], + }] + + +class VerifyReportTest(unittest.TestCase): + def setUp(self): + self.directory = tempfile.TemporaryDirectory() + self.path = Path(self.directory.name) / "report.json" + + def tearDown(self): + self.directory.cleanup() + + def write(self, value): + self.path.write_text(json.dumps(value), encoding="utf-8") + + def test_accepts_exact_successful_label_count(self): + self.write(report()) + self.assertEqual(validate_report(self.path, LABEL, 2), 2) + + def test_rejects_missing_report(self): + with self.assertRaisesRegex(ValueError, "cannot read"): + validate_report(self.path, LABEL, 2) + + def test_rejects_malformed_report(self): + self.path.write_text("{", encoding="utf-8") + with self.assertRaisesRegex(ValueError, "cannot read"): + validate_report(self.path, LABEL, 2) + + def test_rejects_skipped_or_pending_spec(self): + for state in ("skipped", "pending"): + data = report() + data[0]["SpecReports"][0]["State"] = state + self.write(data) + with self.subTest(state=state), self.assertRaisesRegex(ValueError, "not passed"): + validate_report(self.path, LABEL, 2) + + def test_rejects_failed_suite_or_spec(self): + data = report() + data[0]["SuiteSucceeded"] = False + self.write(data) + with self.assertRaisesRegex(ValueError, "suite did not succeed"): + validate_report(self.path, LABEL, 2) + data = report() + data[0]["SpecReports"][0]["State"] = "failed" + self.write(data) + with self.assertRaisesRegex(ValueError, "not passed"): + validate_report(self.path, LABEL, 2) + + def test_rejects_dry_run(self): + data = report() + data[0]["SuiteConfig"]["DryRun"] = True + self.write(data) + with self.assertRaisesRegex(ValueError, "real test run"): + validate_report(self.path, LABEL, 2) + + def test_rejects_programmatic_focus(self): + data = report() + data[0]["SuiteHasProgrammaticFocus"] = True + self.write(data) + with self.assertRaisesRegex(ValueError, "programmatic focus"): + validate_report(self.path, LABEL, 2) + + def test_rejects_retry(self): + data = report() + data[0]["SpecReports"][1]["NumAttempts"] = 2 + self.write(data) + with self.assertRaisesRegex(ValueError, "expected one"): + validate_report(self.path, LABEL, 2) + + def test_rejects_wrong_label(self): + self.write(report()) + with self.assertRaisesRegex(ValueError, "found 0"): + validate_report(self.path, "up-provider-microsandbox-storage", 2) + + def test_rejects_wrong_count(self): + self.write(report()) + with self.assertRaisesRegex(ValueError, "expected 3 specs, found 2"): + validate_report(self.path, LABEL, 3) + + def test_rejects_missing_provider_coverage(self): + data = report() + data[0]["SpecReports"][1]["LeafNodeLabels"] = [PROVIDERS[0]] + self.write(data) + with self.assertRaisesRegex(ValueError, "provider label counts"): + validate_report(self.path, LABEL, 2) + + def test_rejects_ambiguous_or_missing_provider_label(self): + for labels in ([], list(PROVIDERS)): + data = report() + data[0]["SpecReports"][0]["LeafNodeLabels"] = labels + self.write(data) + with self.subTest(labels=labels), self.assertRaisesRegex( + ValueError, "exactly one provider label" + ): + validate_report(self.path, LABEL, 2) + + +if __name__ == "__main__": + unittest.main() diff --git a/hack/microsandbox/verify_report.py b/hack/microsandbox/verify_report.py new file mode 100644 index 0000000000..defb57ed91 --- /dev/null +++ b/hack/microsandbox/verify_report.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python3 +"""Validate that one MicroSandbox Ginkgo label completed its expected specs.""" + +import argparse +import json +from pathlib import Path +import sys + + +def _spec_labels(spec): + labels = spec.get("LeafNodeLabels") or [] + hierarchy = spec.get("ContainerHierarchyLabels") or [] + if not isinstance(labels, list) or any(not isinstance(label, str) for label in labels): + raise ValueError("spec report has malformed leaf labels") + if not isinstance(hierarchy, list) or any( + not isinstance(group, list) or any(not isinstance(label, str) for label in group) + for group in hierarchy + ): + raise ValueError("spec report has malformed container labels") + result = set(labels) + for group in hierarchy: + result.update(group) + return result + + +def validate_report(path, expected_label, expected_count): + try: + reports = json.loads(Path(path).read_text(encoding="utf-8")) + except (OSError, UnicodeError, json.JSONDecodeError) as exc: + raise ValueError(f"cannot read Ginkgo JSON report: {exc}") from exc + + if not isinstance(reports, list) or len(reports) != 1 or not isinstance(reports[0], dict): + raise ValueError("expected one Ginkgo suite report") + suite = reports[0] + config = suite.get("SuiteConfig") + if not isinstance(config, dict) or config.get("DryRun") is not False: + raise ValueError("report does not prove a real test run") + if suite.get("SuiteSucceeded") is not True: + raise ValueError("Ginkgo suite did not succeed") + if suite.get("SuiteHasProgrammaticFocus") is not False: + raise ValueError("report has programmatic focus") + specs = suite.get("SpecReports") + if not isinstance(specs, list): + raise ValueError("report has no spec list") + + if any(not isinstance(spec, dict) for spec in specs): + raise ValueError("report contains a malformed spec entry") + selected = [spec for spec in specs if expected_label in _spec_labels(spec)] + if len(selected) != expected_count: + raise ValueError( + f"label {expected_label!r}: expected {expected_count} specs, found {len(selected)}" + ) + providers = {"microsandbox-builtin": 0, "microsandbox-external": 0} + for index, spec in enumerate(selected, start=1): + spec_labels = _spec_labels(spec) + provider_labels = spec_labels & providers.keys() + if len(provider_labels) != 1: + raise ValueError(f"selected spec {index} must have exactly one provider label") + providers[next(iter(provider_labels))] += 1 + if spec.get("LeafNodeType") != "It": + raise ValueError(f"selected report {index} is not an It spec") + if spec.get("State") != "passed": + raise ValueError(f"selected spec {index} state is {spec.get('State')!r}, not passed") + if spec.get("NumAttempts") != 1: + raise ValueError( + f"selected spec {index} used {spec.get('NumAttempts')!r} attempts, expected one" + ) + if any(count != expected_count // 2 for count in providers.values()): + raise ValueError( + f"provider label counts are {providers}, expected {expected_count // 2} each" + ) + return len(selected) + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--report", required=True, type=Path) + parser.add_argument("--label", required=True) + parser.add_argument("--expected-count", required=True, type=int) + args = parser.parse_args(argv) + if args.expected_count <= 0 or args.expected_count % 2: + parser.error("--expected-count must be a positive even number") + try: + count = validate_report(args.report, args.label, args.expected_count) + except ValueError as exc: + print(f"::error::{exc}", file=sys.stderr) + return 1 + print(f"{args.label}: {count} specs passed in one attempt each") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx b/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx index cec6a8610e..92547fcd5e 100644 --- a/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx @@ -53,6 +53,8 @@ For SDK usage and development commands, see the [SDK README](https://github.com/ ## MicroSandbox parity gate +MicroSandbox integration suites share a provider matrix and test-session setup. Their dedicated CI job defines the suite catalogue and common runtime installation once, while keeping each suite on an independent runner. Every native run must report the exact expected count, split equally between built-in and external provider cases, with no skips or retries. Dry-run and programmatic-focus reports cannot satisfy this gate. See [the test architecture guide](https://github.com/devsy-org/devsy/blob/main/e2e/tests/up/MICROSANDBOX.md) for fixture ownership and extension rules. + The `up-provider-microsandbox` E2E label runs the same lifecycle and ownership scenario against the built-in provider and the external v0.1.8 release, each with isolated Devsy configuration. CI pins MicroSandbox v0.7.7 by checksum and requires access to KVM; unavailable virtualization fails this job instead of producing a passing skipped test. Each provider scenario has a ten-minute deadline and the CI job has a 25-minute deadline. The shared scenario exercises agent delivery, SSH, a 1 MiB binary stdin/stdout round trip with separate stderr and a nonzero guest exit, root workload versus developer identity, bind-mount ownership and mode mirroring, stop/start, recreation, rejection of an identity change without recreation while preserving VM-local data, and deletion of the VM. From e8e119046ecab8d5906a638021dcb8f8dbb94745 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 13 Oct 2026 03:39:36 -0600 Subject: [PATCH 6/7] fix(ci): preserve platform-specific E2E artifacts --- .github/workflows/pr-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index 839150a882..883ae64237 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -234,7 +234,7 @@ jobs: if: matrix.runner == 'ubuntu-latest' || matrix.runner == 'windows-latest' uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 with: - name: e2e-test-linux + name: e2e-test-${{ steps.os.outputs.runner_os }} path: ./e2e/e2e.test* From 2b4e7e3bc9838960af1996f1fd6e2a58ab0d7aba Mon Sep 17 00:00:00 2001 From: Samuel K Date: Tue, 13 Oct 2026 07:55:35 -0600 Subject: [PATCH 7/7] test(runtime): enforce managed recreation conflict with saved options --- e2e/tests/up/managed_policy_fixture_linux.go | 8 ++--- ...der_microsandbox_managed_recreate_linux.go | 31 +++---------------- hack/microsandbox/verify_report.py | 15 ++++++++- .../developing-providers/runtime-protocol.mdx | 2 +- 4 files changed, 23 insertions(+), 33 deletions(-) diff --git a/e2e/tests/up/managed_policy_fixture_linux.go b/e2e/tests/up/managed_policy_fixture_linux.go index 0ad6aaf8b9..53ba107872 100644 --- a/e2e/tests/up/managed_policy_fixture_linux.go +++ b/e2e/tests/up/managed_policy_fixture_linux.go @@ -37,13 +37,13 @@ type safeRecreateManagedFixturePaths struct { work string } -// setSafeRecreateManagedMemory512 installs the valid policy used to make the -// 2048 MiB / tmpfs:1G request invalid only after managed precedence is applied. -func setSafeRecreateManagedMemory512() error { +// setSafeRecreateManagedMemory3072 raises memory above the explicit 2048 MiB +// boot ceiling only after managed precedence is applied. +func setSafeRecreateManagedMemory3072() error { policy := safeRecreateManagedPolicy{ Version: 1, Overrides: safeRecreatePolicyOverrides{ - SandboxDefaults: &safeRecreateSandboxDefaults{MemoryMiB: 512}, + SandboxDefaults: &safeRecreateSandboxDefaults{MemoryMiB: 3072}, }, } return writeSafeRecreateManagedPolicy(policy) diff --git a/e2e/tests/up/provider_microsandbox_managed_recreate_linux.go b/e2e/tests/up/provider_microsandbox_managed_recreate_linux.go index ea4636526e..95a1b07d86 100644 --- a/e2e/tests/up/provider_microsandbox_managed_recreate_linux.go +++ b/e2e/tests/up/provider_microsandbox_managed_recreate_linux.go @@ -4,7 +4,6 @@ package up import ( "context" - "fmt" "os" "strings" @@ -13,7 +12,7 @@ import ( "github.com/onsi/gomega" ) -const safeRecreateManagedConflictErr = "tmpfs root disk size (1024 MiB) must not exceed sandbox memory (512 MiB)" +const safeRecreateManagedConflictErr = "max_memory 2048 MiB must be greater than or equal to memory 3072 MiB" var _ = ginkgo.Describe("MicroSandbox managed-policy rejected replacement", ginkgo.Label("up-provider-microsandbox-managed-recreate"), func() { @@ -29,9 +28,8 @@ var _ = ginkgo.Describe("MicroSandbox managed-policy rejected replacement", // Prove an empty managed policy works before adding the conflicting override. framework.ExpectNoError(clearSafeRecreateManagedPolicy()) - framework.ExpectNoError(fixture.useManagedRecreateOptions(ctx, false)) fixture.writeConfig("2gb") - framework.ExpectNoError(setSafeRecreateManagedMemory512()) + framework.ExpectNoError(setSafeRecreateManagedMemory3072()) out, err := microsandboxResourceCommand(ctx, "list", "--quiet") framework.ExpectNoError(err, string(out)) gomega.Expect(strings.Fields(string(out))). @@ -46,20 +44,18 @@ var _ = ginkgo.Describe("MicroSandbox managed-policy rejected replacement", To(gomega.HaveOccurred(), "stdout: %s stderr: %s", stdout, stderr) gomega.Expect(stdout+stderr).To( gomega.ContainSubstring(safeRecreateManagedConflictErr), - "request must reach final managed-layer tmpfs validation", + "request must reach final managed-layer memory validation", ) // Inspect status and host state before any command can start a stopped VM. gomega.Expect(inspectSafeRecreate(ctx, fixture.sandbox)). To(gomega.Equal(fixture.original)) fixture.assertHostState(ctx) - // Restore policy and original provider options before resuming stopped originals. + // Restore policy before resuming stopped originals. framework.ExpectNoError(clearSafeRecreateManagedPolicy()) - framework.ExpectNoError(fixture.useManagedRecreateOptions(ctx, true)) fixture.inspectOriginalFiles(ctx, stopped) // The exact same request is valid without managed policy and must replace. - framework.ExpectNoError(fixture.useManagedRecreateOptions(ctx, false)) fixture.writeConfig("2gb") framework.ExpectNoError(fixture.framework.DevsyUpRecreate( ctx, append([]string{fixture.workspace}, fixture.upArgs()...)..., @@ -114,22 +110,3 @@ func requireManagedRecreateFixture() { gomega.Expect(marker). To(gomega.Equal(safeRecreateManagedPolicyPath), "managed-policy fixture marker has unexpected path") } - -func (f *safeRecreateFixture) useManagedRecreateOptions( - ctx context.Context, - originalOptions bool, -) error { - storage := 1 - ephemeral := safeRecreateEnabledValue - if originalOptions { - storage = 2 - ephemeral = "false" - } - return f.framework.DevsyProviderUse(ctx, f.provider, - microsandboxResourceOption, "MICROSANDBOX_MEMORY=0", - microsandboxResourceOption, fmt.Sprintf("MICROSANDBOX_MAX_MEMORY=%d", f.maxMemoryMiB), - microsandboxResourceOption, "MICROSANDBOX_CPUS=1", - microsandboxResourceOption, "MICROSANDBOX_MAX_CPUS=1", - microsandboxResourceOption, fmt.Sprintf("MICROSANDBOX_STORAGE=%d", storage), - microsandboxResourceOption, "MICROSANDBOX_EPHEMERAL="+ephemeral) -} diff --git a/hack/microsandbox/verify_report.py b/hack/microsandbox/verify_report.py index defb57ed91..e6f8b90d99 100644 --- a/hack/microsandbox/verify_report.py +++ b/hack/microsandbox/verify_report.py @@ -23,7 +23,7 @@ def _spec_labels(spec): return result -def validate_report(path, expected_label, expected_count): +def _read_suite_specs(path): try: reports = json.loads(Path(path).read_text(encoding="utf-8")) except (OSError, UnicodeError, json.JSONDecodeError) as exc: @@ -42,7 +42,10 @@ def validate_report(path, expected_label, expected_count): specs = suite.get("SpecReports") if not isinstance(specs, list): raise ValueError("report has no spec list") + return specs + +def _select_specs(specs, expected_label, expected_count): if any(not isinstance(spec, dict) for spec in specs): raise ValueError("report contains a malformed spec entry") selected = [spec for spec in specs if expected_label in _spec_labels(spec)] @@ -50,6 +53,10 @@ def validate_report(path, expected_label, expected_count): raise ValueError( f"label {expected_label!r}: expected {expected_count} specs, found {len(selected)}" ) + return selected + + +def _validate_selected_specs(selected, expected_count): providers = {"microsandbox-builtin": 0, "microsandbox-external": 0} for index, spec in enumerate(selected, start=1): spec_labels = _spec_labels(spec) @@ -72,6 +79,12 @@ def validate_report(path, expected_label, expected_count): return len(selected) +def validate_report(path, expected_label, expected_count): + specs = _read_suite_specs(path) + selected = _select_specs(specs, expected_label, expected_count) + return _validate_selected_specs(selected, expected_count) + + def main(argv=None): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--report", required=True, type=Path) diff --git a/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx b/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx index 92547fcd5e..114107a1bb 100644 --- a/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-providers/runtime-protocol.mdx @@ -133,7 +133,7 @@ The `up-provider-microsandbox-recreate` label tests rejected explicit replacemen DEVSY_REQUIRE_MICROSANDBOX=true task cli:test:e2e:suite -- up-provider-microsandbox-recreate ``` -The Linux-only `up-provider-microsandbox-managed-recreate` label checks a valid 2048 MiB / 1 GiB tmpfs replacement that becomes invalid after a managed policy sets memory to 512 MiB. Running and stopped originals must retain their VM generation, root and persistent mount data, workspace record and SSH files for both providers. Resetting the policy must allow the same replacement request. The CI wrapper overlays `/etc` in a private mount namespace before creating any policy file; its scratch remains in the disposable runner for job teardown. These checks require that isolated wrapper and do not establish rollback after replacement has begun. Each scenario and the suite retain the request-invalid suite's time bounds. +The Linux-only `up-provider-microsandbox-managed-recreate` label checks a valid 2048 MiB replacement with an explicit 2048 MiB boot ceiling. A managed policy raises initial memory to 3072 MiB, making the effective configuration invalid. The workspace retains its original provider options throughout; changing global provider defaults would not override those saved workspace values. Running and stopped originals must retain their VM generation, root and persistent mount data, workspace record and SSH files for both providers. Resetting the policy must allow the same replacement request. The CI wrapper overlays `/etc` in a private mount namespace before creating any policy file; its scratch remains in the disposable runner for job teardown. These checks require that isolated wrapper and do not establish rollback after replacement has begun. Each scenario and the suite retain the request-invalid suite's time bounds. On a disposable Linux host with the same prerequisites, prepare the E2E binaries and run the managed suite through its isolated wrapper. The scratch directory is retained for host teardown; running the label without the wrapper does not exercise these cases.