Files
Reims-Go/internal/driver/driver_test.go
T
NwflowerandClaude Opus 5.5 809ce68cf3 feat(datamigrate): version the data folder and migrate it on startup
The data folder had no record of which layout wrote it, so a new release
could not tell old data from current data. <DataRoot>\.reims-go-data.json
now records data_version and the app version that last raised it. It is
kept out of reims-go.json, which is rewritten whole on every save and
would drop the field under an older build.

Each build carries every step from older layouts to its own; the GUI and
CLI run them in order before reading the folder, so users can jump
straight from any old version. A failed step leaves the version at the
last success and resumes next launch; a folder from a newer build is left
untouched.

Step v1 writes the driver ownership marker into every reims-ctl VM.
driver.RenamedDriverIDs maps old driver IDs to new ones for when a driver
moves to another plugin; markers are not rewritten, so downgrades still
resolve.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 05:16:38 -07:00

124 lines
5.2 KiB
Go

package driver
import (
"context"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/Nwflower/reims-go/internal/plugin"
)
type fakeDriver struct {
id string
vms []VMInfo
}
func (f *fakeDriver) ID() string { return f.id }
func (f *fakeDriver) DisplayName() plugin.LocalizedText { return plugin.LocalizedText{EnUS: f.id} }
func (f *fakeDriver) RuntimeKind() string { return "qemu-ovmf" }
func (f *fakeDriver) RuntimeAPIRange() string { return "^1.0.0" }
func (f *fakeDriver) Capabilities() []string { return []string{CapCloudInit, CapSSH} }
func (f *fakeDriver) CreateSchema() *plugin.SettingsSchema { return nil }
func (f *fakeDriver) SettingsSchema() *plugin.SettingsSchema { return nil }
func (f *fakeDriver) List(context.Context) ([]VMInfo, error) { return f.vms, nil }
func (f *fakeDriver) Show(_ context.Context, n string) (VMInfo, error) { return VMInfo{Name: n, DriverID: f.id}, nil }
func (f *fakeDriver) ValidateCreate(context.Context, CreateRequest) []ValidationIssue { return nil }
func (f *fakeDriver) Create(context.Context, CreateRequest, io.Writer, io.Writer) error { return nil }
func (f *fakeDriver) ValidateSet(context.Context, SetRequest) []ValidationIssue { return nil }
func (f *fakeDriver) Set(context.Context, SetRequest, io.Writer, io.Writer) error { return nil }
func (f *fakeDriver) Start(context.Context, string, io.Writer, io.Writer) error { return nil }
func (f *fakeDriver) Stop(context.Context, string, bool, io.Writer, io.Writer) error { return nil }
func (f *fakeDriver) Delete(context.Context, string, bool, io.Writer, io.Writer) error { return nil }
func TestOwnerMarkerZeroMigrationAndRouterUnavailable(t *testing.T) {
vmRoot := t.TempDir()
legacyVM := filepath.Join(vmRoot, "macos-tahoe")
_ = os.MkdirAll(legacyVM, 0o755)
if got := ReadOwnerDriver(legacyVM); got != BuiltinReimsMacOSDriverID {
t.Fatalf("unmarked VM owner = %q, want %q", got, BuiltinReimsMacOSDriverID)
}
linuxVM := filepath.Join(vmRoot, "ubuntu-dev")
if err := WriteOwnerDriver(linuxVM, "reims.driver.linux"); err != nil {
t.Fatal(err)
}
if got := ReadOwnerDriver(linuxVM); got != "reims.driver.linux" {
t.Fatalf("marked VM owner = %q, want reims.driver.linux", got)
}
reg := plugin.NewRegistry(nil)
_ = reg.Register(plugin.PluginDef{
Manifest: plugin.Manifest{ID: "builtin.driver.reims-macos", Version: "1.0.0"},
Source: plugin.SourceBuiltin,
Desired: true,
Activate: func(_ context.Context, sc *plugin.Scope) error {
reg.RegisterGuestDriver(sc, BuiltinReimsMacOSDriverID, &fakeDriver{
id: BuiltinReimsMacOSDriverID,
vms: []VMInfo{{Name: "macos-tahoe", State: "stopped"}},
})
return nil
},
})
reg.Reconcile(context.Background())
router := NewRouter(reg, func() string { return vmRoot })
all, err := router.ListAllVMs(context.Background())
if err != nil {
t.Fatal(err)
}
if len(all) != 2 {
t.Fatalf("expected 2 VMs (1 active + 1 unavailable), got %+v", all)
}
foundUnavail := false
for _, vm := range all {
if vm.Name == "ubuntu-dev" {
foundUnavail = vm.DriverUnavailable && vm.State == "driver_unavailable" && vm.DriverID == "reims.driver.linux"
}
}
if !foundUnavail {
t.Fatalf("ubuntu-dev should be marked driver_unavailable: %+v", all)
}
// CheckDriverInUse must detect ubuntu-dev even when driver is disabled.
inUse, _, err := router.CheckDriverInUse(context.Background(), "reims.driver.linux")
if err != nil || !inUse {
t.Fatalf("expected reims.driver.linux in use, got inUse=%v err=%v", inUse, err)
}
}
// Simulates the release that moves the macOS driver into a market plugin:
// VMs stamped with the old built-in ID resolve to the new ID, and while
// that plugin is missing they stay listed as needing a driver.
func TestRenamedDriverKeepsMarkedVMsVisible(t *testing.T) {
const newID = "community.driver.reims-macos"
RenamedDriverIDs[BuiltinReimsMacOSDriverID] = newID
t.Cleanup(func() { delete(RenamedDriverIDs, BuiltinReimsMacOSDriverID) })
vmRoot := t.TempDir()
vmDir := filepath.Join(vmRoot, "macos-tahoe")
if err := WriteOwnerDriver(vmDir, BuiltinReimsMacOSDriverID); err != nil {
t.Fatal(err)
}
if got := ReadOwnerDriver(vmDir); got != newID {
t.Fatalf("renamed owner = %q, want %q", got, newID)
}
router := NewRouter(plugin.NewRegistry(nil), func() string { return vmRoot })
all, err := router.ListAllVMs(context.Background())
if err != nil {
t.Fatal(err)
}
if len(all) != 1 || !all[0].DriverUnavailable || all[0].DriverID != newID {
t.Fatalf("expected macos-tahoe as unavailable under %s, got %+v", newID, all)
}
if inUse, _, err := router.CheckDriverInUse(context.Background(), newID); err != nil || !inUse {
t.Fatalf("new driver ID should count the VM as in use: inUse=%v err=%v", inUse, err)
}
if _, err := router.DriverByID(BuiltinReimsMacOSDriverID); err == nil || !strings.Contains(err.Error(), newID) {
t.Fatalf("DriverByID(old ID) should look up %s, got %v", newID, err)
}
}