Every VM action had two bindings: one by name and one by driver and name. The window used only the second, so the first is gone and the WithDriver suffix with it. Unused bindings and frontend exports are removed too. Tests now reject bindings the window never calls, stale generated bindings and unused frontend exports. TypeScript rejects unused locals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
278 lines
9.5 KiB
Go
278 lines
9.5 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/guesttools"
|
|
vmlifecycle "github.com/Nwflower/reims-go/internal/lifecycle"
|
|
"github.com/Nwflower/reims-go/internal/macos/reims"
|
|
"github.com/Nwflower/reims-go/internal/plugin"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
)
|
|
|
|
type resumeGuestToolsDriver struct {
|
|
*appSecondDriver
|
|
driver.SuspendDriver
|
|
onResume func(driver.ResumeRequest)
|
|
resumeErr error
|
|
}
|
|
|
|
func (d *resumeGuestToolsDriver) Resume(_ context.Context, req driver.ResumeRequest) (driver.StartResult, error) {
|
|
d.onResume(req)
|
|
return driver.StartResult{}, d.resumeErr
|
|
}
|
|
|
|
func TestResumeVMRegistersSavedGuestToolsWithoutUnicodeInput(t *testing.T) {
|
|
resumeErr := errors.New("resume failed")
|
|
for _, tc := range []struct {
|
|
name string
|
|
manifest string
|
|
unicodeInput bool
|
|
windowDrop bool
|
|
savedConfig bool
|
|
preRegistered bool
|
|
concurrentUpdate bool
|
|
concurrentReplace bool
|
|
resumeErr error
|
|
}{
|
|
{name: "runtime installed=true", manifest: `{"commit":"legacy"}`, savedConfig: true},
|
|
{name: "runtime installed=false", savedConfig: true},
|
|
{name: "failure rolls back new registration", savedConfig: true, resumeErr: resumeErr},
|
|
{name: "failure keeps preexisting registration", savedConfig: true, preRegistered: true, resumeErr: resumeErr},
|
|
{name: "failure keeps concurrent registration update", savedConfig: true, concurrentUpdate: true, resumeErr: resumeErr},
|
|
{name: "failure keeps replacement registration", savedConfig: true, concurrentReplace: true, resumeErr: resumeErr},
|
|
{name: "resume preserves both window bridges", savedConfig: true, unicodeInput: true, windowDrop: true,
|
|
manifest: `{"commit":"window-bridges","capabilities":["win32-unicode-input","window-file-drop"]}`},
|
|
{name: "Unicode input failure rolls back new registration", savedConfig: true, unicodeInput: true, resumeErr: resumeErr,
|
|
manifest: `{"commit":"unicode","capabilities":["win32-unicode-input"]}`},
|
|
{name: "no saved config leaves bridge stopped", resumeErr: resumeErr},
|
|
{name: "Unicode input without saved config leaves bridge stopped", unicodeInput: true,
|
|
manifest: `{"commit":"unicode","capabilities":["win32-unicode-input"]}`, resumeErr: resumeErr},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
cfg := &config.Config{DataRoot: t.TempDir()}
|
|
if tc.manifest != "" {
|
|
runtimeDir := filepath.Join(cfg.RuntimesDir(), "legacy")
|
|
if err := os.MkdirAll(runtimeDir, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(runtimeDir, "manifest.json"), []byte(tc.manifest), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
active, err := rt.ResolveActive(cfg)
|
|
if err != nil || active.Manifest.HasCapability("win32-unicode-input") != tc.unicodeInput ||
|
|
active.Manifest.HasCapability("window-file-drop") != tc.windowDrop {
|
|
t.Fatalf("unexpected runtime capabilities: %+v, %v", active, err)
|
|
}
|
|
}
|
|
|
|
const name = "resume-guest-tools"
|
|
vmDir := t.TempDir()
|
|
t.Cleanup(func() { stopVMMonitor(name) })
|
|
if err := os.WriteFile(filepath.Join(vmDir, "vm.toml"), nil, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var srv *guesttools.Server
|
|
var port int
|
|
var saved guesttools.VMConfig
|
|
if tc.savedConfig {
|
|
srv = guesttools.NewServer(nil)
|
|
var err error
|
|
port, err = srv.Start(-1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
if err := srv.Close(ctx); err != nil {
|
|
t.Error(err)
|
|
}
|
|
})
|
|
saved, err = guesttools.LoadOrCreateVMConfig(vmDir, name, filepath.Join(cfg.SharedDir(), name), port)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
client := &http.Client{Timeout: 2 * time.Second}
|
|
ping := func(want int) {
|
|
t.Helper()
|
|
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, fmt.Sprintf("http://127.0.0.1:%d/v1/ping", port), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+saved.Token)
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != want {
|
|
t.Fatalf("Guest Tools ping status = %d, want %d", resp.StatusCode, want)
|
|
}
|
|
}
|
|
if tc.savedConfig {
|
|
ping(http.StatusUnauthorized)
|
|
if tc.preRegistered {
|
|
srv.RegisterVM(saved)
|
|
ping(http.StatusOK)
|
|
}
|
|
}
|
|
|
|
const driverID = driver.BuiltinReimsMacOSDriverID
|
|
drv := &resumeGuestToolsDriver{
|
|
appSecondDriver: &appSecondDriver{id: driverID, vms: []driver.VMInfo{
|
|
{Name: name, DriverID: driverID, Directory: vmDir, State: "suspended"},
|
|
}},
|
|
onResume: func(req driver.ResumeRequest) {
|
|
var wantEnv []string
|
|
if tc.unicodeInput && tc.savedConfig {
|
|
wantEnv = append(wantEnv, fmt.Sprintf("REIMS_GO_INPUT_URL=http://127.0.0.1:%d/v1/host-text", port), "REIMS_GO_INPUT_TOKEN="+saved.Token)
|
|
}
|
|
if tc.windowDrop && tc.savedConfig {
|
|
token := srv.HostDropToken(driverID, name)
|
|
if token == "" {
|
|
t.Fatal("missing window drop registration")
|
|
}
|
|
wantEnv = append(wantEnv, fmt.Sprintf("REIMS_GO_DROP_URL=http://127.0.0.1:%d/v1/host-drop", port), "REIMS_GO_DROP_TOKEN="+token)
|
|
}
|
|
if !slices.Equal(req.ExtraEnv, wantEnv) {
|
|
t.Error("unexpected window bridge environment")
|
|
}
|
|
if tc.savedConfig {
|
|
ping(http.StatusOK)
|
|
if tc.concurrentReplace {
|
|
srv.UnregisterVM(name)
|
|
}
|
|
if tc.concurrentUpdate || tc.concurrentReplace {
|
|
updated := saved
|
|
updated.PreventSleep = !updated.PreventSleep
|
|
srv.RegisterVM(updated)
|
|
}
|
|
}
|
|
},
|
|
resumeErr: tc.resumeErr,
|
|
}
|
|
reg := plugin.NewRegistry(nil)
|
|
if err := reg.Register(plugin.PluginDef{
|
|
Manifest: plugin.Manifest{ID: "test.resume", 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(t.Context())
|
|
a := &App{
|
|
cfg: cfg, guestTools: srv, transitions: map[string]string{},
|
|
lifecycleSvc: vmlifecycle.New(reg, driver.NewRouter(reg, nil), nil),
|
|
}
|
|
t.Cleanup(func() {
|
|
if a.guestTools != nil && a.guestTools != srv {
|
|
if err := a.guestTools.Close(context.Background()); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
})
|
|
if err := a.ResumeVM("", name); !errors.Is(err, tc.resumeErr) {
|
|
t.Fatalf("ResumeVM error = %v, want %v", err, tc.resumeErr)
|
|
}
|
|
if !tc.savedConfig {
|
|
if a.guestTools != nil {
|
|
t.Fatal("resuming a VM without Guest Tools config started a bridge")
|
|
}
|
|
} else if tc.resumeErr != nil && !tc.preRegistered && !tc.concurrentUpdate && !tc.concurrentReplace {
|
|
ping(http.StatusUnauthorized)
|
|
} else {
|
|
ping(http.StatusOK)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEnrichVMListStatesAndPopulateSuspendDetail(t *testing.T) {
|
|
suspendedDir := t.TempDir()
|
|
stoppedDir := t.TempDir()
|
|
|
|
if err := os.WriteFile(reims.SuspendStatePath(suspendedDir)+".tmp", []byte("0123456789"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
media := filepath.Join(suspendedDir, "disk.qcow2")
|
|
for _, path := range []string{media, filepath.Join(suspendedDir, "vm.toml")} {
|
|
if err := os.WriteFile(path, []byte("state"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err := reims.SealSuspend(
|
|
reims.VMDetail{Name: "mac-a", Directory: suspendedDir, CPUs: 8, Memory: "8G", Graphics: "reims-vgpu"},
|
|
rt.Installed{Commit: "3731afd"},
|
|
[]string{media},
|
|
); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
vms := []driver.VMListItem{
|
|
{Name: "mac-a", Dir: suspendedDir, State: "stopped"},
|
|
{Name: "mac-b", Dir: stoppedDir, State: "stopped"},
|
|
{Name: "mac-c", Dir: suspendedDir, State: "running"},
|
|
}
|
|
enrichMacOSVMListStates(vms)
|
|
if vms[0].State != "suspended" {
|
|
t.Fatalf("mac-a state = %q, want suspended", vms[0].State)
|
|
}
|
|
if vms[1].State != "stopped" {
|
|
t.Fatalf("mac-b state = %q, want stopped", vms[1].State)
|
|
}
|
|
if vms[2].State != "running" {
|
|
t.Fatalf("running VM must stay running, got %q", vms[2].State)
|
|
}
|
|
|
|
det := reims.VMDetail{Name: "mac-a", Directory: suspendedDir, State: "stopped"}
|
|
populateSuspendDetail(&det)
|
|
if det.State != "suspended" || det.SuspendInfo == nil || det.SuspendInfo.VMStateSize != 10 {
|
|
t.Fatalf("populateSuspendDetail failed: %+v", det)
|
|
}
|
|
}
|
|
|
|
func TestStripLegacyManagedExtraArgsStripsIncomingDefer(t *testing.T) {
|
|
got := stripLegacyManagedExtraArgs("-machine kernel-irqchip=on -incoming defer")
|
|
if got != "-machine kernel-irqchip=on" {
|
|
t.Fatalf("stripLegacyManagedExtraArgs = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestSuspendProgressPercentTracksRemainingAcrossSparseRAM(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
prog driver.SuspendProgress
|
|
pct int
|
|
ok bool
|
|
}{
|
|
{"setup", driver.SuspendProgress{Status: "setup"}, 0, false},
|
|
{"active sparse", driver.SuspendProgress{Status: "active", Transferred: 400, Remaining: 1000, Total: 2000}, 50, true},
|
|
{"device stage caps below 100", driver.SuspendProgress{Status: "device", Transferred: 1080, Remaining: 0, Total: 2000}, 99, true},
|
|
{"completed always 100", driver.SuspendProgress{Status: "completed", Transferred: 1080, Remaining: 0, Total: 2000}, 100, true},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
pct, ok := suspendProgressPercent(tc.prog)
|
|
if pct != tc.pct || ok != tc.ok {
|
|
t.Fatalf("suspendProgressPercent(%+v) = (%d, %t), want (%d, %t)", tc.prog, pct, ok, tc.pct, tc.ok)
|
|
}
|
|
})
|
|
}
|
|
}
|