Files
Reims-Go/internal/driver/driver_test.go
T
Nwflower 7ba5e3fa9c feat(lifecycle): route VM starts through owning drivers
Change Driver.Start to StartRequest/StartResult with a compile-time macOS adapter assertion and registry-to-router coverage. Keep legacy Sidecar name alongside VMRef and grant argument privileges by driver ownership.

Move macOS preparation into ReimsMacOSDriver after hook veto, fail preparation errors before launch, and verify veto leaves OpenCore, recovery markers and ledgers unchanged.

Unify GUI, CLI and window list/start/stop through internal/lifecycle, select frontend VMs by VMRef, lazily initialize reims only in legacy CLI branches, and cover a second driver through App user entry points.
2026-11-30 08:34:20 -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, StartRequest) (StartResult, error) { return StartResult{}, 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)
}
}