The busy check treated the merged "suspended" state as live, so one suspended macOS VM blocked turning off macOS VM Support. A suspended VM has no process; its saved state stays in the VM folder and resumes once the plugin is enabled again. The disable dialog and plugin docs say so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
162 lines
6.8 KiB
Go
162 lines
6.8 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)
|
|
}
|
|
}
|
|
|
|
// 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")
|
|
}
|
|
}
|