Delete functions that no product code calls, and fold "simple plus WithOptions" pairs into the one signature callers actually use. Rename the live macOS lifecycle functions that were labelled Legacy. The architecture tests now share one source loader and reject functions without a product caller and catalog messages nobody reads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
184 lines
7.2 KiB
Go
184 lines
7.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 }
|
|
|
|
// declareVMRoot registers an installed but disabled driver plugin that keeps
|
|
// its VMs in <dataRoot>/vms.
|
|
func declareVMRoot(t *testing.T, reg *plugin.Registry, driverID string) {
|
|
t.Helper()
|
|
if err := reg.Register(plugin.PluginDef{
|
|
Manifest: plugin.Manifest{ID: "test.disabled-driver", Version: "1.0.0",
|
|
Contributes: plugin.Contributes{GuestDrivers: []plugin.GuestDriverContribution{{ID: driverID, VMRoots: []string{"vms"}}}}},
|
|
Source: plugin.SourceBuiltin,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestOwnerMarkerZeroMigrationAndRouterUnavailable(t *testing.T) {
|
|
dataRoot := t.TempDir()
|
|
vmRoot := filepath.Join(dataRoot, "vms")
|
|
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())
|
|
declareVMRoot(t, reg, "reims.driver.linux")
|
|
|
|
router := NewRouter(reg, func() string { return dataRoot })
|
|
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) })
|
|
|
|
dataRoot := t.TempDir()
|
|
vmRoot := filepath.Join(dataRoot, "vms")
|
|
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)
|
|
}
|
|
|
|
reg := plugin.NewRegistry(nil)
|
|
declareVMRoot(t, reg, newID)
|
|
router := NewRouter(reg, func() string { return dataRoot })
|
|
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)
|
|
}
|
|
}
|
|
|
|
// A plugin whose driver owns only stopped VMs may be disabled (busy=false)
|
|
// but not removed (inUse=true); a VM in any other state makes it busy.
|
|
func TestPluginBusyAndInUseCheckers(t *testing.T) {
|
|
const pluginID, driverID = "test.guest", "test.driver"
|
|
drv := &fakeDriver{id: driverID, vms: []VMInfo{{Name: "a", State: "stopped"}, {Name: "b", State: "broken"}, {Name: "c", State: "suspended"}}}
|
|
reg := plugin.NewRegistry(nil)
|
|
if err := reg.Register(plugin.PluginDef{
|
|
Manifest: plugin.Manifest{ID: pluginID, Version: "1.0.0",
|
|
Contributes: plugin.Contributes{GuestDrivers: []plugin.GuestDriverContribution{{ID: driverID}}}},
|
|
Source: plugin.SourceBuiltin, Desired: true,
|
|
Activate: func(_ context.Context, sc *plugin.Scope) error {
|
|
reg.RegisterGuestDriver(sc, driverID, drv)
|
|
return nil
|
|
},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reg.Reconcile(context.Background())
|
|
router := NewRouter(reg, nil)
|
|
busy, inUse := router.PluginBusyChecker(), router.PluginInUseChecker()
|
|
|
|
if hit, _, err := busy(pluginID); err != nil || hit {
|
|
t.Fatalf("stopped/broken/suspended VMs: busy=%v err=%v", hit, err)
|
|
}
|
|
if hit, _, err := inUse(pluginID); err != nil || !hit {
|
|
t.Fatalf("owned VMs: inUse=%v err=%v", hit, err)
|
|
}
|
|
for _, state := range []string{"running", "starting", "stopping", "paused", ""} {
|
|
drv.vms[0].State = state
|
|
if hit, reason, err := busy(pluginID); err != nil || !hit || !strings.Contains(reason, "a") {
|
|
t.Fatalf("state %q: busy=%v reason=%q err=%v", state, hit, reason, err)
|
|
}
|
|
}
|
|
if hit, _, _ := busy("other.plugin"); hit {
|
|
t.Fatal("a plugin without drivers must never be busy")
|
|
}
|
|
}
|