279 lines
9.9 KiB
Go
279 lines
9.9 KiB
Go
package lifecycle
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"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
|
|
suspends, resumes, discardSuspends int
|
|
lastResumeEnv []string
|
|
storageCalls []string
|
|
deletedName string
|
|
}
|
|
|
|
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 && vm.State != "broken" {
|
|
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, name string, _ bool, _, _ io.Writer) error {
|
|
d.deletedName = name
|
|
return nil
|
|
}
|
|
|
|
func (d *testDriver) Suspend(_ context.Context, req driver.SuspendRequest) (driver.SuspendResult, error) {
|
|
d.suspends++
|
|
if req.Progress != nil {
|
|
req.Progress(driver.SuspendProgress{Status: "completed", Total: 100})
|
|
}
|
|
return driver.SuspendResult{VMStateSize: 1024}, nil
|
|
}
|
|
|
|
func (d *testDriver) Resume(_ context.Context, req driver.ResumeRequest) (driver.StartResult, error) {
|
|
d.resumes++
|
|
d.lastResumeEnv = append([]string(nil), req.ExtraEnv...)
|
|
return driver.StartResult{
|
|
PostStart: []plugin.PostStartAction{{
|
|
Kind: "qmp.watch_cold_reboot",
|
|
Params: map[string]any{"qmp_addr": "unix:/tmp/reboot.sock"},
|
|
}},
|
|
}, nil
|
|
}
|
|
|
|
func (d *testDriver) DiscardSuspend(_ context.Context, _ string) error {
|
|
d.discardSuspends++
|
|
return nil
|
|
}
|
|
|
|
func (d *testDriver) InvokeAction(_ context.Context, _ string, action string, _ map[string]any) (any, error) {
|
|
return action, nil
|
|
}
|
|
func (d *testDriver) ReadLogs(_ context.Context, _ string, serial bool) (string, error) {
|
|
if serial {
|
|
return "serial-log", nil
|
|
}
|
|
return "qemu-log", 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 TestDeleteResolvesBrokenVMThroughItsDriverInventory(t *testing.T) {
|
|
reg := plugin.NewRegistry(nil)
|
|
drv := &testDriver{id: "test.driver.linux", vms: []driver.VMInfo{{Name: "Broken-VM", State: "broken"}}}
|
|
registerTestDriver(t, reg, drv)
|
|
ctx := context.Background()
|
|
reg.Reconcile(ctx)
|
|
svc := New(reg, driver.NewRouter(reg, nil), nil)
|
|
ref := driver.NewVMRef(drv.id, "broken-vm")
|
|
if err := svc.Delete(ctx, ref, false, io.Discard, io.Discard); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if drv.deletedName != "Broken-VM" {
|
|
t.Fatalf("deletion received %q instead of the registered name", drv.deletedName)
|
|
}
|
|
if _, _, err := svc.Resolve(ctx, drv.id, "Broken-VM"); err == nil {
|
|
t.Fatal("settings resolution should still report the broken VM")
|
|
}
|
|
}
|
|
|
|
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 got, err := svc.InvokeAction(context.Background(), driver.NewVMRef(drv.id, "ubuntu"), "eject", nil); err != nil || got != "eject" {
|
|
t.Fatalf("InvokeAction = %v, %v", got, err)
|
|
}
|
|
if got, err := svc.ReadLogs(context.Background(), driver.NewVMRef(drv.id, "ubuntu"), true); err != nil || got != "serial-log" {
|
|
t.Fatalf("ReadLogs = %q, %v", got, 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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSuspendResumeDiscardRouting(t *testing.T) {
|
|
reg := plugin.NewRegistry(nil)
|
|
drv := &testDriver{id: "test.driver.macos", vms: []driver.VMInfo{{Name: "mac", DriverID: "test.driver.macos", Directory: t.TempDir(), State: "running"}}}
|
|
registerTestDriver(t, reg, drv)
|
|
type obs struct {
|
|
ev string
|
|
qmp string
|
|
}
|
|
obsCh := make(chan obs, 4)
|
|
if err := reg.Register(plugin.PluginDef{
|
|
Manifest: plugin.Manifest{ID: "vendor.observer", Version: "1.0.0"}, Desired: true,
|
|
Activate: func(_ context.Context, sc *plugin.Scope) error {
|
|
for _, ev := range []string{plugin.EventVMBeforeStop, plugin.EventVMAfterStop, plugin.EventVMAfterStart} {
|
|
evName := ev
|
|
sc.Registry().Hooks().Subscribe(sc, plugin.HookRegistration{Event: evName, Handler: func(_ context.Context, payload any) (any, error) {
|
|
var qmpAddr string
|
|
if p, ok := payload.(plugin.VMEventPayload); ok {
|
|
qmpAddr = p.QMPAddr
|
|
}
|
|
obsCh <- obs{ev: evName, qmp: qmpAddr}
|
|
return nil, nil
|
|
}})
|
|
}
|
|
return nil
|
|
},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reg.Reconcile(context.Background())
|
|
|
|
svc := New(reg, driver.NewRouter(reg, nil), nil)
|
|
ref := driver.NewVMRef(drv.id, "mac")
|
|
|
|
gotProgress := false
|
|
res, err := svc.Suspend(context.Background(), ref, func(p driver.SuspendProgress) {
|
|
if p.Status == "completed" {
|
|
gotProgress = true
|
|
}
|
|
}, io.Discard, io.Discard)
|
|
if err != nil || res.VMStateSize != 1024 || !gotProgress || drv.suspends != 1 {
|
|
t.Fatalf("Suspend = %+v, err=%v, progress=%t, count=%d", res, err, gotProgress, drv.suspends)
|
|
}
|
|
|
|
startRes, err := svc.Resume(context.Background(), ref, []string{"K=V"}, io.Discard, io.Discard)
|
|
if err != nil || len(startRes.PostStart) != 1 || drv.resumes != 1 || len(drv.lastResumeEnv) != 1 || drv.lastResumeEnv[0] != "K=V" {
|
|
t.Fatalf("Resume = %+v, err=%v, count=%d, env=%v", startRes, err, drv.resumes, drv.lastResumeEnv)
|
|
}
|
|
|
|
if err := svc.DiscardSuspend(context.Background(), ref); err != nil || drv.discardSuspends != 1 {
|
|
t.Fatalf("DiscardSuspend err=%v, count=%d", err, drv.discardSuspends)
|
|
}
|
|
|
|
seen := map[string]bool{}
|
|
var seenQMP string
|
|
deadline := time.After(2 * time.Second)
|
|
for len(seen) < 3 {
|
|
select {
|
|
case o := <-obsCh:
|
|
seen[o.ev] = true
|
|
if o.qmp != "" {
|
|
seenQMP = o.qmp
|
|
}
|
|
case <-deadline:
|
|
t.Fatalf("timed out waiting for observe events: got %v", seen)
|
|
}
|
|
}
|
|
if !seen[plugin.EventVMBeforeStop] || !seen[plugin.EventVMAfterStop] || !seen[plugin.EventVMAfterStart] || seenQMP != "unix:/tmp/reboot.sock" {
|
|
t.Fatalf("events=%v qmp=%q", seen, seenQMP)
|
|
}
|
|
}
|