diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index 339c3fd0c8..de918b1568 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -431,6 +431,9 @@ jobs: - label: up-provider-microsandbox-remote-agent-binaries expected-spec-count: 1 provider-mode: external + - label: up-provider-microsandbox-source-migration + expected-spec-count: 2 + provider-mode: external runs-on: ubuntu-latest timeout-minutes: 25 steps: diff --git a/cmd/provider/set_source_init_failure_test.go b/cmd/provider/set_source_init_failure_test.go new file mode 100644 index 0000000000..65df1b6d77 --- /dev/null +++ b/cmd/provider/set_source_init_failure_test.go @@ -0,0 +1,122 @@ +package provider + +import ( + "context" + "os" + "path/filepath" + "testing" + "time" + + "github.com/devsy-org/devsy/cmd/flags" + "github.com/devsy-org/devsy/pkg/config" + provider2 "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/types" + "github.com/devsy-org/devsy/pkg/workspace" + "github.com/stretchr/testify/require" + "sigs.k8s.io/yaml" +) + +func TestSetSourceInitFailureKeepsCommittedReplacementAndWorkspace(t *testing.T) { + initial, userOption := setupInitializedSourceProvider(t) + + associated := &provider2.Workspace{ + ID: "existing", UID: "existing-generation", Context: initial.DefaultContext, + Provider: provider2.WorkspaceProviderConfig{Name: testProviderFoo}, + } + require.NoError(t, provider2.SaveWorkspaceConfig(associated)) + workspaceDir, err := provider2.GetWorkspaceDir(initial.DefaultContext, associated.ID) + require.NoError(t, err) + recordPath := filepath.Join(workspaceDir, provider2.WorkspaceConfigFile) + // #nosec G304 -- recordPath is the isolated fixture workspace record. + recordBefore, err := os.ReadFile(recordPath) + require.NoError(t, err) + markerPath := filepath.Join(workspaceDir, "retained-content") + require.NoError(t, os.WriteFile(markerPath, []byte("existing-workspace-content"), 0o600)) + + missingInit := filepath.Join(t.TempDir(), "missing-init-executable") + replacement, err := provider2.LoadProviderConfig(initial.DefaultContext, testProviderFoo) + require.NoError(t, err) + replacement.Version = "2.0.0" + replacement.Exec.Init = []string{missingInit} + replacementSource := filepath.Join(t.TempDir(), "replacement.yaml") + replacement.Source = provider2.ProviderSource{File: replacementSource, Raw: replacementSource} + replacementBytes, err := yaml.Marshal(replacement) + require.NoError(t, err) + require.NoError(t, os.WriteFile(replacementSource, replacementBytes, 0o600)) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + cmd := &SetSourceCmd{GlobalFlags: &flags.GlobalFlags{}, Use: true} + err = cmd.Run(ctx, initial, []string{testProviderFoo, replacementSource}) + require.ErrorContains(t, err, "init:") + require.ErrorContains(t, err, "missing-init-executable") + + assertFailedSourceReplacement( + t, + initial.DefaultContext, + replacement, + userOption, + ) + // #nosec G304 -- recordPath is the same isolated fixture workspace record. + recordAfter, err := os.ReadFile(recordPath) + require.NoError(t, err) + require.Equal(t, recordBefore, recordAfter) + // #nosec G304 -- markerPath is created in this fixture workspace directory. + markerAfter, err := os.ReadFile(markerPath) + require.NoError(t, err) + require.Equal(t, "existing-workspace-content", string(markerAfter)) +} + +func setupInitializedSourceProvider(t *testing.T) (*config.Config, config.OptionValue) { + t.Helper() + t.Setenv("DEVSY_HOME", t.TempDir()) + initial := setupProviderOperationTest(t) + oldSource := filepath.Join(t.TempDir(), "old.yaml") + old := &provider2.ProviderConfig{ + Name: testProviderFoo, Version: "1.0.0", + Options: map[string]*types.Option{"REGION": {Default: "default-region"}}, + Exec: provider2.ProviderCommands{Command: []string{"unused-command"}}, + } + oldBytes, err := yaml.Marshal(old) + require.NoError(t, err) + require.NoError(t, os.WriteFile(oldSource, oldBytes, 0o600)) + _, err = workspace.AddProvider(context.Background(), initial, testProviderFoo, oldSource) + require.NoError(t, err) + userOption := config.OptionValue{Value: "user-region", UserProvided: true} + require.NoError( + t, + config.UpdateConfig(initial.DefaultContext, "", func(c *config.Config) error { + c.Current().DefaultProvider = testProviderFoo + c.Current().Providers[testProviderFoo].Initialized = true + c.Current().Providers[testProviderFoo].Options = map[string]config.OptionValue{ + "REGION": userOption, + } + return nil + }), + ) + + return initial, userOption +} + +func assertFailedSourceReplacement( + t *testing.T, + contextName string, + expected *provider2.ProviderConfig, + userOption config.OptionValue, +) { + t.Helper() + installed, err := provider2.LoadProviderConfig(contextName, testProviderFoo) + require.NoError(t, err) + require.Equal(t, expected.Version, installed.Version) + require.Equal(t, expected.Source.Raw, installed.Source.Raw) + require.Equal(t, expected.Exec.Init, installed.Exec.Init) + stored, err := config.LoadConfig(contextName, "") + require.NoError(t, err) + state := stored.Current().Providers[testProviderFoo] + require.NotNil(t, state) + require.False(t, state.Initialized) + require.True(t, state.InitAttempted) + require.Contains(t, state.InitError, "missing-init-executable") + require.Equal(t, userOption.Value, state.Options["REGION"].Value) + require.True(t, state.Options["REGION"].UserProvided) + require.Equal(t, testProviderFoo, stored.Current().DefaultProvider) +} diff --git a/e2e/tests/up/MICROSANDBOX.md b/e2e/tests/up/MICROSANDBOX.md index 01b4809e7c..4adea6c2d3 100644 --- a/e2e/tests/up/MICROSANDBOX.md +++ b/e2e/tests/up/MICROSANDBOX.md @@ -105,11 +105,37 @@ Devsy agent endpoint stays available; this is workspace-runtime reuse, not an offline-agent executable guarantee. Workspace, container and server cleanup have independent bounded attempts. -This external-only row brings the configured catalogue to 15 suites/78 cases +The remote-agent acceptance expanded the catalogue to 15 suites/78 cases (37 built-in, 41 external). Earlier 77-case consumer reports do not prove the remote-agent case. Its native Linux/amd64 KVM report must pass once without selected skips; source checks, fixture unit tests and dry runs cannot prove it. +## Existing-workspace source migration + +The `up-provider-microsandbox-source-migration` external-only row covers explicit +`provider set-source` migration from the built-in MicroSandbox descriptor to the +published external provider, for both running and stopped workspaces. Each case +starts with a unique provider/workspace/volume and user-provided options. It +checks stored source and driver identity, successful activation, preserved +provider name/default/options, workspace and SSH records, VM creation time, +root data, named-volume data and a host bind marker. + +The running case requires the same guest boot ID through migration and a later +`up`. The stopped case checks that migration leaves the VM stopped before any +command resumes it; subsequent `up` must start that same VM with its data intact. +Deletion must remove the migrated workspace record and the exact owned sandbox. +Independent cleanup attempts cover the workspace, sandbox fallback and volume. +The command-package unit test separately covers activation failure after a +committed replacement: it retains the replacement source and workspace data, +records the initialization error and preserves user options and default choice. + +This row brings the configured catalogue to 16 suites/80 cases (37 built-in, +43 external). It requires two actual native cases, each passed once +without selected skips. Compilation and dry-run registration are not migration +proof. Stored internal descriptors refresh executable blocks; they do not +implicitly migrate driver or binaries. A future bundled-default transition must +retain a literal legacy built-in fixture for these starting workspaces. + ## Runtime startup measurements The existing two-case lifecycle label optionally captures its ordinary fresh diff --git a/e2e/tests/up/provider_microsandbox_migration.go b/e2e/tests/up/provider_microsandbox_migration.go new file mode 100644 index 0000000000..5ff2d6f7c5 --- /dev/null +++ b/e2e/tests/up/provider_microsandbox_migration.go @@ -0,0 +1,322 @@ +package up + +import ( + "context" + "encoding/json" + "fmt" + "os" + "path/filepath" + "strings" + "time" + + "github.com/devsy-org/devsy/e2e/framework" + "github.com/devsy-org/devsy/pkg/config" + "github.com/devsy-org/devsy/pkg/devcontainer" + "github.com/devsy-org/devsy/pkg/encoding" + providerpkg "github.com/devsy-org/devsy/pkg/provider" + "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" +) + +const ( + microsandboxMigrationSuiteLabel = "up-provider-microsandbox-source-migration" + microsandboxMigrationRootMarker = "/root/devsy-source-migration-marker" + microsandboxMigrationVolumeDir = "/source-migration-volume" + microsandboxMigrationMarker = "source-migration-preserved" + microsandboxMigrationSource = "source-migration-source" +) + +var _ = ginkgo.Describe( + "existing MicroSandbox workspace source migration", + ginkgo.Label(microsandboxMigrationSuiteLabel), + func() { + ginkgo.DescribeTable( + "migrates the existing provider without replacing its workspace or VM", + func(ctx context.Context, stopped bool) { + fixture := newMicrosandboxMigrationFixture(ctx) + fixture.initialize(ctx, stopped) + fixture.migrate(ctx, stopped) + fixture.deleteWorkspace(ctx) + }, + ginkgo.Entry( + "running workspace", + false, + ginkgo.Label("microsandbox-external"), + ginkgo.SpecTimeout(framework.TimeoutLong()), + ), + ginkgo.Entry( + "stopped workspace", + true, + ginkgo.Label("microsandbox-external"), + ginkgo.SpecTimeout(framework.TimeoutLong()), + ), + ) + }, +) + +type microsandboxMigrationFixture struct { + framework *framework.Framework + workspace string + sandbox string + uid string + label string + volume string + volumeOwned bool + workspaceState compatibilityWorkspaceState + vmState safeRecreateInspection + bootID string + options map[string]config.OptionValue +} + +func newMicrosandboxMigrationFixture( + ctx context.Context, +) *microsandboxMigrationFixture { + session := newMicrosandboxTestSession(ctx) + home := ginkgo.GinkgoT().TempDir() + ginkgo.GinkgoT().Setenv("HOME", home) + ginkgo.GinkgoT().Setenv("USERPROFILE", home) + ginkgo.GinkgoT().Setenv("MSB_HOME", filepath.Join(home, "microsandbox")) + ginkgo.GinkgoT().Setenv( + "MSB_CONFIG_PATH", + filepath.Join(home, "microsandbox", "config.json"), + ) + + workspace := session.newWorkspace() + session.addProvider(ctx, microsandboxResourceBuiltin, "microsandbox") + uid := encoding.CreateNewUID("", filepath.Base(workspace)) + fixture := µsandboxMigrationFixture{ + framework: session.framework, + workspace: workspace, + uid: uid, + label: "devsy.source-migration=" + uid, + volume: "devsy-source-migration-" + uid, + } + fixture.sandbox = "devsy-" + fixture.uid + fixture.registerCleanup() + + framework.ExpectNoError(fixture.framework.DevsyProviderUse(ctx, "microsandbox", + microsandboxResourceOption, "MICROSANDBOX_MEMORY=1024", + microsandboxResourceOption, "MICROSANDBOX_STORAGE=2", + microsandboxResourceOption, "MICROSANDBOX_EPHEMERAL=false")) + 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)) + out, err = microsandboxResourceCommand(ctx, "volume", "create", fixture.volume) + framework.ExpectNoError(err, string(out)) + fixture.volumeOwned = true + fixture.writeConfig() + + return fixture +} + +func (fixture *microsandboxMigrationFixture) registerCleanup() { + // Cleanup is deliberately independent: a failed workspace deletion must not + // prevent the exact owned sandbox fallback or named volume removal. + ginkgo.DeferCleanup(func(cleanupCtx context.Context) error { + if !fixture.volumeOwned { + return nil + } + cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(cleanupCtx), time.Minute) + defer cancel() + out, err := microsandboxResourceCommand(cleanupCtx, "volume", "rm", fixture.volume) + if err != nil { + return fmt.Errorf("remove owned migration 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 migration sandbox for cleanup: %w: %s", err, out) + } + for name := range strings.FieldsSeq(string(out)) { + if name == fixture.sandbox { + out, err = microsandboxResourceCommand(cleanupCtx, "remove", "--force", name) + if err != nil { + return fmt.Errorf("remove owned migration sandbox: %w: %s", err, out) + } + break + } + } + return nil + }) + ginkgo.DeferCleanup(fixture.framework.CleanupWorkspace, fixture.workspace) +} + +func (fixture *microsandboxMigrationFixture) writeConfig() { + devcontainerConfig := struct { + Image string `json:"image"` + ContainerUser string `json:"containerUser"` + RemoteUser string `json:"remoteUser"` + Mounts []string `json:"mounts"` + }{ + Image: microsandboxResourceImage, + ContainerUser: microsandboxRootUser, + RemoteUser: microsandboxRootUser, + Mounts: []string{ + "type=volume,source=" + fixture.volume + ",target=" + microsandboxMigrationVolumeDir, + }, + } + data, err := json.Marshal(devcontainerConfig) + framework.ExpectNoError(err) + framework.ExpectNoError(os.WriteFile( + filepath.Join(fixture.workspace, ".devcontainer.json"), data, 0o600, + )) + framework.ExpectNoError(os.WriteFile( + filepath.Join(fixture.workspace, microsandboxMigrationSource), + []byte(microsandboxMigrationMarker), 0o600, + )) +} + +func (fixture *microsandboxMigrationFixture) initialize(ctx context.Context, stopped bool) { + assertMigrationProvider("microsandbox", true, providerpkg.MicrosandboxDriver, "microsandbox") + devsyConfig, err := config.LoadConfig("", "") + framework.ExpectNoError(err) + gomega.Expect(devsyConfig.Current().DefaultProvider).To(gomega.Equal("microsandbox")) + providerConfig := devsyConfig.Current().Providers["microsandbox"] + gomega.Expect(providerConfig).NotTo(gomega.BeNil()) + gomega.Expect(providerConfig.Initialized).To(gomega.BeTrue()) + fixture.options = migrationOptions(providerConfig.Options) + assertMigrationOptions(fixture.options) + + framework.ExpectNoError(fixture.framework.DevsyUp( + ctx, + fixture.workspace, + "--devcontainer", ".devcontainer.json", + "--uid", fixture.uid, + "--id-label", fixture.label, + )) + workspace, err := fixture.framework.FindWorkspace(ctx, fixture.workspace) + framework.ExpectNoError(err) + gomega.Expect(workspace.UID).To(gomega.Equal(fixture.uid)) + gomega.Expect(workspace.Provider.Name).To(gomega.Equal("microsandbox")) + gomega.Expect("devsy-" + devcontainer.GetRunnerIDFromWorkspace(workspace)). + To(gomega.Equal(fixture.sandbox)) + fixture.workspaceState = snapshotCompatibilityWorkspace(ctx, workspace) + fixture.vmState = inspectSafeRecreate(ctx, fixture.sandbox) + gomega.Expect(fixture.vmState.Status).To(gomega.Equal("Running")) + _, err = fixture.framework.DevsySSHOnce(ctx, fixture.workspace, + "printf '%s' '"+microsandboxMigrationMarker+"' > "+microsandboxMigrationRootMarker+ + " && printf '%s' '"+microsandboxMigrationMarker+"' > "+ + microsandboxMigrationVolumeDir+"/marker") + framework.ExpectNoError(err) + fixture.bootID = microsandboxStorageBootID(ctx, fixture.framework, fixture.workspace) + if stopped { + framework.ExpectNoError(fixture.framework.DevsyWorkspaceStop(ctx, fixture.workspace)) + fixture.vmState = inspectSafeRecreate(ctx, fixture.sandbox) + gomega.Expect(fixture.vmState.Status).To(gomega.Equal("Stopped")) + } +} + +func (fixture *microsandboxMigrationFixture) migrate(ctx context.Context, stopped bool) { + framework.ExpectNoError(fixture.framework.DevsyProviderSetSource( + ctx, "microsandbox", microsandboxExternalProvider, + )) + assertMigrationProvider("microsandbox", false, providerpkg.ExternalDriver, + microsandboxExternalProvider) + devsyConfig, err := config.LoadConfig("", "") + framework.ExpectNoError(err) + gomega.Expect(devsyConfig.Current().DefaultProvider).To(gomega.Equal("microsandbox")) + providerConfig := devsyConfig.Current().Providers["microsandbox"] + gomega.Expect(providerConfig).NotTo(gomega.BeNil()) + gomega.Expect(providerConfig.Initialized).To(gomega.BeTrue()) + gomega.Expect(providerConfig.InitError).To(gomega.BeEmpty()) + afterOptions := migrationOptions(providerConfig.Options) + assertMigrationOptions(afterOptions) + gomega.Expect(afterOptions).To(gomega.Equal(fixture.options)) + + workspace, err := fixture.framework.FindWorkspace(ctx, fixture.workspace) + framework.ExpectNoError(err) + gomega.Expect(snapshotCompatibilityWorkspace(ctx, workspace)). + To(gomega.Equal(fixture.workspaceState)) + vmAfterMigration := inspectSafeRecreate(ctx, fixture.sandbox) + gomega.Expect(vmAfterMigration.CreatedAt).To(gomega.Equal(fixture.vmState.CreatedAt)) + if stopped { + gomega.Expect(vmAfterMigration.Status).To(gomega.Equal("Stopped")) + } else { + gomega.Expect(vmAfterMigration.Status).To(gomega.Equal("Running")) + gomega.Expect(microsandboxStorageBootID(ctx, fixture.framework, fixture.workspace)). + To(gomega.Equal(fixture.bootID)) + fixture.assertMarkers(ctx) + } + + framework.ExpectNoError(fixture.framework.DevsyUp(ctx, fixture.workspace)) + workspace, err = fixture.framework.FindWorkspace(ctx, fixture.workspace) + framework.ExpectNoError(err) + gomega.Expect(workspace.ID).To(gomega.Equal(fixture.workspaceState.ID)) + gomega.Expect(workspace.UID).To(gomega.Equal(fixture.uid)) + gomega.Expect(workspace.Provider.Name).To(gomega.Equal("microsandbox")) + gomega.Expect(snapshotCompatibilityWorkspace(ctx, workspace)). + To(gomega.Equal(fixture.workspaceState)) + vmAfterUp := inspectSafeRecreate(ctx, fixture.sandbox) + gomega.Expect(vmAfterUp.Status).To(gomega.Equal("Running")) + gomega.Expect(vmAfterUp.CreatedAt).To(gomega.Equal(fixture.vmState.CreatedAt)) + bootAfterUp := microsandboxStorageBootID(ctx, fixture.framework, fixture.workspace) + if stopped { + gomega.Expect(bootAfterUp).NotTo(gomega.Equal(fixture.bootID)) + } else { + gomega.Expect(bootAfterUp).To(gomega.Equal(fixture.bootID)) + } + fixture.assertMarkers(ctx) +} + +func (fixture *microsandboxMigrationFixture) assertMarkers(ctx context.Context) { + for _, path := range []string{ + microsandboxMigrationRootMarker, + microsandboxMigrationVolumeDir + "/marker", + filepath.ToSlash(filepath.Join("/workspaces", filepath.Base(fixture.workspace), microsandboxMigrationSource)), + } { + out, err := fixture.framework.DevsySSHOnce(ctx, fixture.workspace, "cat "+path) + framework.ExpectNoError(err, out) + gomega.Expect(strings.TrimSpace(out)).To(gomega.Equal(microsandboxMigrationMarker)) + } +} + +func (fixture *microsandboxMigrationFixture) deleteWorkspace(ctx context.Context) { + framework.ExpectNoError(fixture.framework.DevsyWorkspaceDelete(ctx, fixture.workspace)) + gomega.Expect(providerpkg.WorkspaceExists(config.DefaultContext, fixture.workspaceState.ID)). + To(gomega.BeFalse(), "the migrated workspace record should be deleted") + out, err := microsandboxResourceCommand(ctx, "list", "--quiet") + framework.ExpectNoError(err, string(out)) + gomega.Expect(strings.Fields(string(out))).NotTo(gomega.ContainElement(fixture.sandbox)) +} + +func assertMigrationProvider( + name string, + internal bool, + driver string, + rawSource string, +) { + providerConfig, err := providerpkg.LoadProviderConfig(config.DefaultContext, name) + framework.ExpectNoError(err) + gomega.Expect(providerConfig.Name).To(gomega.Equal(name)) + gomega.Expect(providerConfig.Source.Internal).To(gomega.Equal(internal)) + gomega.Expect(providerConfig.Source.Raw).To(gomega.Equal(rawSource)) + gomega.Expect(providerConfig.Agent.Driver).To(gomega.Equal(driver)) +} + +func migrationOptions(options map[string]config.OptionValue) map[string]config.OptionValue { + shared := make(map[string]config.OptionValue, 3) + for _, key := range []string{ + "MICROSANDBOX_MEMORY", "MICROSANDBOX_STORAGE", "MICROSANDBOX_EPHEMERAL", + } { + shared[key] = options[key] + } + return shared +} + +func assertMigrationOptions(options map[string]config.OptionValue) { + for key, value := range map[string]string{ + "MICROSANDBOX_MEMORY": "1024", + "MICROSANDBOX_STORAGE": "2", + "MICROSANDBOX_EPHEMERAL": "false", + } { + option, ok := options[key] + gomega.Expect(ok).To(gomega.BeTrue(), "shared option %s must remain configured", key) + gomega.Expect(option.Value).To(gomega.Equal(value), "shared option %s", key) + gomega.Expect(option.UserProvided).To(gomega.BeTrue(), "shared option %s", key) + } +}