Files
Reims-Go/internal/app/lifecycle_flow_test.go
T
Nwflower b6fde8fd4b feat(lifecycle): route VM details and settings by driver
Resolve details through driver.Show with a generic GetVMInfo binding, and validate/apply changes through lifecycle.Set in App and CLI.

Move guest language and OpenCore replay into ReimsMacOSDriver.Set, preserving legacy fallbacks and adding second-driver detail/settings acceptance.
2026-11-30 09:04:14 -07:00

108 lines
4.0 KiB
Go

package app
import (
"context"
"io"
"os"
"testing"
"github.com/Nwflower/reims-go/internal/config"
"github.com/Nwflower/reims-go/internal/driver"
vmlifecycle "github.com/Nwflower/reims-go/internal/lifecycle"
"github.com/Nwflower/reims-go/internal/plugin"
)
type appSecondDriver struct {
id string
vms []driver.VMInfo
starts, stops, deletes, sets int
}
func (d *appSecondDriver) ID() string { return d.id }
func (d *appSecondDriver) DisplayName() plugin.LocalizedText { return plugin.LocalizedText{EnUS: d.id} }
func (d *appSecondDriver) RuntimeKind() string { return "test-runtime" }
func (d *appSecondDriver) RuntimeAPIRange() string { return "^1.0.0" }
func (d *appSecondDriver) Capabilities() []string { return nil }
func (d *appSecondDriver) CreateSchema() *plugin.SettingsSchema { return nil }
func (d *appSecondDriver) SettingsSchema() *plugin.SettingsSchema { return nil }
func (d *appSecondDriver) List(context.Context) ([]driver.VMInfo, error) { return d.vms, nil }
func (d *appSecondDriver) Show(_ context.Context, name string) (driver.VMInfo, error) {
for _, vm := range d.vms {
if vm.Name == name {
return vm, nil
}
}
return driver.VMInfo{}, os.ErrNotExist
}
func (d *appSecondDriver) ValidateCreate(context.Context, driver.CreateRequest) []driver.ValidationIssue {
return nil
}
func (d *appSecondDriver) Create(context.Context, driver.CreateRequest, io.Writer, io.Writer) error {
return nil
}
func (d *appSecondDriver) ValidateSet(context.Context, driver.SetRequest) []driver.ValidationIssue {
return nil
}
func (d *appSecondDriver) Set(context.Context, driver.SetRequest, io.Writer, io.Writer) error {
d.sets++
return nil
}
func (d *appSecondDriver) Start(context.Context, driver.StartRequest) (driver.StartResult, error) {
d.starts++
return driver.StartResult{}, nil
}
func (d *appSecondDriver) Stop(context.Context, string, bool, io.Writer, io.Writer) error {
d.stops++
return nil
}
func (d *appSecondDriver) Delete(context.Context, string, bool, io.Writer, io.Writer) error {
d.deletes++
return nil
}
func TestAppSecondDriverUserEntry(t *testing.T) {
const driverID = "test.driver.linux"
drv := &appSecondDriver{id: driverID, vms: []driver.VMInfo{{Name: "ubuntu", DriverID: driverID, Directory: t.TempDir(), State: "stopped"}}}
reg := plugin.NewRegistry(nil)
if err := reg.Register(plugin.PluginDef{Manifest: plugin.Manifest{ID: "test.linux", Version: "1.0.0"},
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 := driver.NewRouter(reg, nil)
a := &App{cfg: &config.Config{DataRoot: t.TempDir()}, lifecycleSvc: vmlifecycle.New(reg, router, nil),
transitions: map[string]string{}, pendingChanges: map[string][]PendingChange{}}
info, err := a.GetVMInfo(driverID, "ubuntu")
if err != nil || info.DriverID != driverID {
t.Fatalf("GetVMInfo = %+v, %v", info, err)
}
vms, err := a.listVMsInternal(context.Background())
if err != nil || len(vms) != 1 || vms[0].DriverID != driverID {
t.Fatalf("list = %+v, %v", vms, err)
}
if err := a.StartVMWithDriver(driverID, "ubuntu"); err != nil {
t.Fatal(err)
}
if err := a.StopVMWithDriver(driverID, "ubuntu", false); err != nil {
t.Fatal(err)
}
if err := a.startHandedVM(driverID, "ubuntu"); err != nil {
t.Fatal(err)
}
if err := a.lifecycleSvc.Set(context.Background(), driver.NewVMRef(driverID, "ubuntu"), map[string]any{"memory": "8G"}, io.Discard, io.Discard); err != nil {
t.Fatal(err)
}
if err := a.RestartVMWithDriver(driverID, "ubuntu"); err != nil {
t.Fatal(err)
}
if err := a.DeleteVMWithDriver(driverID, "ubuntu", false); err != nil {
t.Fatal(err)
}
if drv.starts != 3 || drv.stops != 2 || drv.deletes != 1 || drv.sets != 1 {
t.Fatalf("starts=%d stops=%d deletes=%d", drv.starts, drv.stops, drv.deletes)
}
}