Files
Reims-Go/internal/lifecycle/lifecycle_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

141 lines
5.1 KiB
Go

package lifecycle
import (
"context"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/Nwflower/reims-go/internal/driver"
"github.com/Nwflower/reims-go/internal/plugin"
)
type testDriver struct {
id string
vms []driver.VMInfo
starts, stops, sets int
}
func (d *testDriver) ID() string { return d.id }
func (d *testDriver) DisplayName() plugin.LocalizedText { return plugin.LocalizedText{EnUS: d.id} }
func (d *testDriver) RuntimeKind() string { return "test-runtime" }
func (d *testDriver) RuntimeAPIRange() string { return "^1.0.0" }
func (d *testDriver) Capabilities() []string { return nil }
func (d *testDriver) CreateSchema() *plugin.SettingsSchema { return nil }
func (d *testDriver) SettingsSchema() *plugin.SettingsSchema { return nil }
func (d *testDriver) List(context.Context) ([]driver.VMInfo, error) { return d.vms, nil }
func (d *testDriver) 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 *testDriver) ValidateCreate(context.Context, driver.CreateRequest) []driver.ValidationIssue {
return nil
}
func (d *testDriver) Create(context.Context, driver.CreateRequest, io.Writer, io.Writer) error {
return nil
}
func (d *testDriver) ValidateSet(context.Context, driver.SetRequest) []driver.ValidationIssue {
return nil
}
func (d *testDriver) Set(context.Context, driver.SetRequest, io.Writer, io.Writer) error {
d.sets++
return nil
}
func (d *testDriver) Start(context.Context, driver.StartRequest) (driver.StartResult, error) {
d.starts++
return driver.StartResult{}, nil
}
func (d *testDriver) Stop(context.Context, string, bool, io.Writer, io.Writer) error {
d.stops++
return nil
}
func (d *testDriver) Delete(context.Context, string, bool, io.Writer, io.Writer) error { return nil }
func registerTestDriver(t *testing.T, reg *plugin.Registry, d *testDriver) {
t.Helper()
if err := reg.Register(plugin.PluginDef{Manifest: plugin.Manifest{ID: d.id + ".plugin", Version: "1.0.0"},
Source: plugin.SourceBuiltin, Desired: true, Activate: func(_ context.Context, sc *plugin.Scope) error {
reg.RegisterGuestDriver(sc, d.id, d)
return nil
}}); err != nil {
t.Fatal(err)
}
}
func TestSecondDriverListStartStop(t *testing.T) {
reg := plugin.NewRegistry(nil)
drv := &testDriver{id: "test.driver.linux", vms: []driver.VMInfo{{Name: "ubuntu", DriverID: "test.driver.linux", State: "stopped"}}}
registerTestDriver(t, reg, drv)
reg.Reconcile(context.Background())
svc := New(reg, driver.NewRouter(reg, nil), nil)
vms, err := svc.List(context.Background())
if err != nil || len(vms) != 1 || vms[0].DriverID != drv.id {
t.Fatalf("List = %+v, %v", vms, err)
}
if _, err := svc.StartNamed(context.Background(), "", "ubuntu", io.Discard, io.Discard); err != nil {
t.Fatal(err)
}
if err := svc.Set(context.Background(), driver.NewVMRef(drv.id, "ubuntu"), map[string]any{"memory": "8G"}, io.Discard, io.Discard); err != nil {
t.Fatal(err)
}
if err := svc.Stop(context.Background(), driver.NewVMRef(drv.id, "ubuntu"), false, io.Discard, io.Discard); err != nil {
t.Fatal(err)
}
if drv.sets != 1 {
t.Fatalf("sets=%d", drv.sets)
}
if drv.starts != 1 || drv.stops != 1 {
t.Fatalf("starts=%d stops=%d", drv.starts, drv.stops)
}
}
func TestVetoLeavesVMFilesUntouched(t *testing.T) {
reg := plugin.NewRegistry(nil)
vmDir := t.TempDir()
drv := &testDriver{id: "test.driver.macos", vms: []driver.VMInfo{{Name: "mac", DriverID: "test.driver.macos", Directory: vmDir, State: "stopped"}}}
registerTestDriver(t, reg, drv)
if err := reg.Register(plugin.PluginDef{
Manifest: plugin.Manifest{ID: "vendor.guard", Version: "1.0.0", Permissions: []string{"vm:intent"}}, Desired: true,
Activate: func(_ context.Context, sc *plugin.Scope) error {
sc.Registry().Hooks().Subscribe(sc, plugin.HookRegistration{Event: plugin.EventVMBeforeStart, Blocking: true, BlockingAllowed: true,
Handler: func(context.Context, any) (any, error) { return plugin.StartIntent{BlockReason: "veto"}, nil }})
return nil
},
}); err != nil {
t.Fatal(err)
}
reg.Reconcile(context.Background())
files := []string{"OpenCore.img", ".reims-go-recovery-picker", plugin.ArgLedgerFileName}
before := map[string][]byte{}
for _, name := range files {
body := []byte("before-" + name)
if err := os.WriteFile(filepath.Join(vmDir, name), body, 0o644); err != nil {
t.Fatal(err)
}
before[name] = body
}
svc := New(reg, driver.NewRouter(reg, nil), nil)
_, err := svc.StartNamed(context.Background(), drv.id, "mac", io.Discard, io.Discard)
if err == nil || !strings.Contains(err.Error(), "veto") {
t.Fatalf("Start error = %v", err)
}
if drv.starts != 0 {
t.Fatal("driver Start ran after veto")
}
for _, name := range files {
got, readErr := os.ReadFile(filepath.Join(vmDir, name))
if readErr != nil {
t.Fatal(readErr)
}
if string(got) != string(before[name]) {
t.Fatalf("%s changed after veto", name)
}
}
}