* refactor: 收拢核心管线、强化错误处理、优化热路径与更新体积 * fix: preserve download recovery and bound PBZX decoding Keep VM diagnostics available during plugin initialization failures and verify settings through the unified driver lifecycle.
664 lines
23 KiB
Go
664 lines
23 KiB
Go
package app
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/driver"
|
|
"github.com/Nwflower/reims-go/internal/host"
|
|
"github.com/Nwflower/reims-go/internal/macos/macosdriver"
|
|
"github.com/Nwflower/reims-go/internal/macos/reims"
|
|
rt "github.com/Nwflower/reims-go/internal/runtime"
|
|
"github.com/Nwflower/reims-go/internal/vmstart"
|
|
)
|
|
|
|
const (
|
|
displayFixtureVM = "test-vm"
|
|
displayFixtureClone = "test-clone"
|
|
)
|
|
|
|
type displayCtlFixture struct {
|
|
Dir, State, Resolution string
|
|
Memory string
|
|
BeforeLockResolution string
|
|
Shows int
|
|
Graphics string
|
|
CloneDir string
|
|
Borderless bool
|
|
FailSet bool
|
|
NestedVirtBeforeSet bool
|
|
FailShowAfterSet bool
|
|
ReplaceDisplayAfterSet bool
|
|
Commands []string
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
if path := os.Getenv("REIMS_TEST_DISPLAY_CTL"); path != "" {
|
|
switch filepath.Base(os.Args[0]) {
|
|
case "reims-ctl", "reims-ctl.exe":
|
|
if err := runDisplayCtl(path, os.Args[1:]); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
return
|
|
case "qemu-img", "qemu-img.exe":
|
|
if os.Args[1] == "info" {
|
|
fmt.Println(`{"format":"qcow2"}`)
|
|
return
|
|
}
|
|
if os.Args[1] == "create" {
|
|
if err := os.WriteFile(os.Args[len(os.Args)-1], []byte("overlay"), 0o600); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
return
|
|
}
|
|
if os.Args[1] == "convert" {
|
|
if err := os.WriteFile(os.Args[len(os.Args)-1], []byte("converted disk"), 0o600); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
return
|
|
}
|
|
fmt.Fprintln(os.Stderr, "unexpected qemu-img call", os.Args[1:])
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
func runDisplayCtl(path string, args []string) error {
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var vm displayCtlFixture
|
|
if err := json.Unmarshal(raw, &vm); err != nil {
|
|
return err
|
|
}
|
|
switch args[0] {
|
|
case "show":
|
|
if args[1] == displayFixtureClone && vm.CloneDir != "" {
|
|
fmt.Printf("state stopped\ndirectory %s\ndisplay one opening at %s\n", vm.CloneDir, vm.Resolution)
|
|
return nil
|
|
}
|
|
if vm.State == "" || args[1] != displayFixtureVM || vm.FailShowAfterSet && len(vm.Commands) > 0 {
|
|
return fmt.Errorf("VM %q not found", args[1])
|
|
}
|
|
if vm.BeforeLockResolution != "" {
|
|
vm.Shows++
|
|
if vm.Shows == 2 {
|
|
unlock, err := reims.LockVMOperation(vm.Dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer unlock()
|
|
vm.Resolution = vm.BeforeLockResolution
|
|
if err := writeDisplayCtlResolution(vm.Dir, vm.Resolution); err != nil {
|
|
return err
|
|
}
|
|
if _, err := vmstart.SaveVMAutoResolution(vm.Dir, false); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
raw, err := json.Marshal(vm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := os.WriteFile(path, raw, 0o600); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
fmt.Printf("state %s\ndirectory %s\ndisk system.qcow2\ngraphics %s\nmemory %s\n", vm.State, vm.Dir, vm.Graphics, vm.Memory)
|
|
if vm.Borderless {
|
|
fmt.Printf("display one borderless at %s\n", vm.Resolution)
|
|
} else {
|
|
fmt.Printf("display one opening at %s\n", vm.Resolution)
|
|
}
|
|
return nil
|
|
case "list":
|
|
if vm.State == "" {
|
|
fmt.Println("[]")
|
|
return nil
|
|
}
|
|
return json.NewEncoder(os.Stdout).Encode([]reims.VMListItem{{Name: displayFixtureVM, State: vm.State, Dir: vm.Dir}})
|
|
case "create":
|
|
if args[1] == displayFixtureClone {
|
|
vm.CloneDir = filepath.Join(filepath.Dir(vm.Dir), displayFixtureClone)
|
|
if err := os.MkdirAll(vm.CloneDir, 0o700); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
vm.State = "stopped"
|
|
if err := os.MkdirAll(vm.Dir, 0o700); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
case "set", "start":
|
|
if args[0] == "set" && len(args) > 1 && args[1] == "--help" {
|
|
return nil
|
|
}
|
|
default:
|
|
return fmt.Errorf("unexpected command %v", args)
|
|
}
|
|
failSet := args[0] == "set" && vm.FailSet
|
|
if failSet {
|
|
nested, err := vmstart.ReadVMNestedVirt(vm.Dir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
vm.NestedVirtBeforeSet = nested.Enabled
|
|
}
|
|
for i := 2; !failSet && i+1 < len(args); i++ {
|
|
switch args[i] {
|
|
case "--resolution":
|
|
vm.Resolution = args[i+1]
|
|
if err := writeDisplayCtlResolution(vm.Dir, vm.Resolution); err != nil {
|
|
return err
|
|
}
|
|
case "--borderless":
|
|
vm.Borderless = args[i+1] == "true"
|
|
case "--graphics":
|
|
vm.Graphics = args[i+1]
|
|
case "--memory":
|
|
vm.Memory = args[i+1]
|
|
}
|
|
}
|
|
if args[0] == "set" && vm.ReplaceDisplayAfterSet {
|
|
vm.Resolution = "1024x768"
|
|
if _, err := vmstart.SaveVMAutoResolution(vm.Dir, false); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
command := strings.Join(args, " ")
|
|
if args[0] == "start" {
|
|
command += " fixed=" + os.Getenv("REIMS_VGPU_WINDOW_FIXED_MODE")
|
|
}
|
|
vm.Commands = append(vm.Commands, command)
|
|
raw, err = json.Marshal(vm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := os.WriteFile(path, raw, 0o600); err != nil {
|
|
return err
|
|
}
|
|
if args[0] == "start" {
|
|
return fmt.Errorf("stop before launching QEMU")
|
|
}
|
|
if failSet {
|
|
return fmt.Errorf("fixture refused set")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func writeDisplayCtlResolution(dir, resolution string) error {
|
|
config := fmt.Sprintf("disk = 'system.qcow2'\n[display]\nresolution = %q\n", resolution)
|
|
return os.WriteFile(filepath.Join(dir, "vm.toml"), []byte(config), 0o600)
|
|
}
|
|
|
|
func newDisplayCtlApp(t *testing.T, vm displayCtlFixture, useDriver bool) (*App, func() displayCtlFixture) {
|
|
t.Helper()
|
|
cfg := config.Config{DataRoot: t.TempDir(), ActiveRuntime: "test-rt"}
|
|
if err := cfg.EnsureDirs(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
active := rt.Installed{Dir: filepath.Join(cfg.RuntimesDir(), cfg.ActiveRuntime)}
|
|
control := active.ControlPath()
|
|
if err := os.MkdirAll(filepath.Dir(control), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(active.Dir, "manifest.json"), []byte(`{"schema_version":2,"commit":"test-rt","kind":"reims-vgpu","runtime_api":"1.0.0","capabilities":["display-fixed-mode","vm-suspend"]}`), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
self, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
exe, err := os.ReadFile(self)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(control, exe, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(active.QemuImgPath()), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Link(control, active.QemuImgPath()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fixturePath := filepath.Join(cfg.DataRoot, "display-fixture.json")
|
|
t.Setenv("REIMS_TEST_DISPLAY_CTL", fixturePath)
|
|
t.Setenv("REIMS_VGPU_WINDOW_FIXED_MODE", "")
|
|
t.Setenv("GORACE", "atexit_sleep_ms=0")
|
|
registryDir := filepath.Join(cfg.XDGConfigDir(), "reims-ctl")
|
|
t.Setenv("REIMS_CTL_CONFIG_DIR", registryDir)
|
|
if err := os.MkdirAll(registryDir, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
vm.Dir = filepath.Join(cfg.DataRoot, displayFixtureVM)
|
|
if vm.State != "" {
|
|
if err := os.MkdirAll(vm.Dir, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := writeDisplayCtlResolution(vm.Dir, vm.Resolution); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(vm.Dir, "system.qcow2"), []byte("source disk fixture"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
registry := fmt.Sprintf("[[vm]]\nname = %q\ndir = '%s'\n", displayFixtureVM, vm.Dir)
|
|
if err := os.WriteFile(filepath.Join(registryDir, "vms.toml"), []byte(registry), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
raw, err := json.Marshal(vm)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(fixturePath, raw, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
a := &App{}
|
|
if useDriver {
|
|
a = newDisplayTestApp(t, macosdriver.NewReimsMacOSDriver(func() config.Config { return cfg }))
|
|
}
|
|
a.cfg = &cfg
|
|
a.transitions = map[string]string{}
|
|
a.pendingChanges = map[string][]PendingChange{}
|
|
return a, func() displayCtlFixture {
|
|
t.Helper()
|
|
raw, err := os.ReadFile(fixturePath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var current displayCtlFixture
|
|
if err := json.Unmarshal(raw, ¤t); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return current
|
|
}
|
|
}
|
|
|
|
func TestApplyAutoResolutionWhileRunning(t *testing.T) {
|
|
for _, state := range []string{"running", "paused"} {
|
|
t.Run(state, func(t *testing.T) {
|
|
initial := "1234x876"
|
|
a, fixture := newDisplayCtlApp(t, displayCtlFixture{State: state, Resolution: initial}, true)
|
|
on := true
|
|
if err := a.ApplyVMSettings(reims.SetOpts{Name: displayFixtureVM, Resolution: "1280x800", AutoResolution: &on}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
vm := fixture()
|
|
want := []PendingChange{
|
|
{Field: "auto_resolution", From: "false", To: "true"},
|
|
}
|
|
if vm.Resolution != initial || len(vm.Commands) != 0 || !vmstart.ReadVMDisplay(vm.Dir).AutoResolution || !slices.Equal(a.pendingChanges[vmKey(displayFixtureVM)], want) {
|
|
t.Fatalf("enable auto: VM %+v, changes %+v, want %+v", vm, a.pendingChanges, want)
|
|
}
|
|
if raw, found := reims.ReadConfiguredResolution(vm.Dir); !found || raw != initial {
|
|
t.Fatalf("configured resolution = %q, found = %t, want %q", raw, found, initial)
|
|
}
|
|
if err := a.ApplyVMSettings(reims.SetOpts{Name: displayFixtureVM, Resolution: initial}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
vm = fixture()
|
|
if vm.Resolution != initial || vmstart.ReadVMDisplay(vm.Dir).AutoResolution || len(a.pendingChanges) != 0 {
|
|
t.Fatalf("manual resolution: VM %+v, want initial resolution and no pending changes, got %+v", vm, a.pendingChanges)
|
|
}
|
|
if err := a.ApplyVMSettings(reims.SetOpts{Name: displayFixtureVM, Resolution: "1280x800"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want = []PendingChange{{Field: "resolution", From: initial, To: "1280x800"}}
|
|
if vm = fixture(); vm.Resolution != "1280x800" || !slices.Equal(a.pendingChanges[vmKey(displayFixtureVM)], want) {
|
|
t.Fatalf("changed fixed resolution: VM %+v, changes %+v, want %+v", vm, a.pendingChanges, want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyAutoResolutionDoesNotReadBackSettings(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
failShow, replace bool
|
|
}{
|
|
{name: "show fails", failShow: true},
|
|
{name: "display changed externally", replace: true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
const initial = "1234x876"
|
|
a, fixture := newDisplayCtlApp(t, displayCtlFixture{
|
|
State: "running", Resolution: initial, FailShowAfterSet: tc.failShow, ReplaceDisplayAfterSet: tc.replace,
|
|
}, true)
|
|
on := true
|
|
if err := a.ApplyVMSettings(reims.SetOpts{Name: displayFixtureVM, CPUs: 1, AutoResolution: &on}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := []PendingChange{
|
|
{Field: "cpus", From: "0", To: "1"},
|
|
{Field: "auto_resolution", From: "false", To: "true"},
|
|
}
|
|
if got := a.pendingChanges[vmKey(displayFixtureVM)]; !slices.Equal(got, want) {
|
|
t.Fatalf("pending changes = %+v, want %+v", got, want)
|
|
}
|
|
if got := fixture().Commands; !slices.Equal(got, []string{"set test-vm --cpus 1"}) {
|
|
t.Fatalf("commands = %v", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyAutoResolutionUsesLockedSettingsForPendingChanges(t *testing.T) {
|
|
a, fixture := newDisplayCtlApp(t, displayCtlFixture{
|
|
State: "running", Resolution: "1234x876", BeforeLockResolution: "2560x1600", Memory: "2G",
|
|
}, true)
|
|
if _, err := vmstart.SaveVMAutoResolution(fixture().Dir, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.ApplyVMSettings(reims.SetOpts{Name: displayFixtureVM, Memory: "3G"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
vm := fixture()
|
|
if vm.Resolution != "2560x1600" || vm.Memory != "3G" || vmstart.ReadVMDisplay(vm.Dir).AutoResolution {
|
|
t.Fatalf("saved VM = %+v, display = %+v", vm, vmstart.ReadVMDisplay(vm.Dir))
|
|
}
|
|
want := []PendingChange{{Field: "memory", From: "2G", To: "3G"}}
|
|
if got := a.pendingChanges[vmKey(displayFixtureVM)]; !slices.Equal(got, want) {
|
|
t.Fatalf("pending changes = %+v, want %+v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestMergePendingChangesKeepsRunningValues(t *testing.T) {
|
|
existing := []PendingChange{
|
|
{Field: "memory", From: "8G", To: "12G"},
|
|
{Field: "resolution", From: "1920x1080", To: "2560x1440"},
|
|
{Field: "gpu", From: "gpu-a", To: "gpu-b"},
|
|
{Field: "cpus", From: "4", To: "6"},
|
|
}
|
|
incoming := []PendingChange{
|
|
{Field: "memory", From: "12G", To: "8g"},
|
|
{Field: "resolution", From: "2560x1440", To: "1920X1080"},
|
|
{Field: "gpu", From: "gpu-b", To: "GPU-A"},
|
|
{Field: "cpus", From: "6", To: "8"},
|
|
}
|
|
want := []PendingChange{
|
|
{Field: "gpu", From: "gpu-a", To: "GPU-A"},
|
|
{Field: "cpus", From: "4", To: "8"},
|
|
}
|
|
if got := mergePendingChanges(existing, incoming); !slices.Equal(got, want) {
|
|
t.Fatalf("merge = %+v, want %+v", got, want)
|
|
}
|
|
// When show reports live running value (4 -> 4) while user explicitly sets cpus back to 4,
|
|
// pendingVMSettingsChanges still emits cpus so mergePendingChanges removes stale 4 -> 8
|
|
// without disturbing unrelated pending entries.
|
|
liveOutcome := macosdriver.SettingsOutcome{
|
|
Valid: true,
|
|
Running: true,
|
|
Before: macosdriver.SettingsState{CPUs: 4, Memory: "8G"},
|
|
After: macosdriver.SettingsState{CPUs: 4, Memory: "8G"},
|
|
}
|
|
diffs := pendingVMSettingsChanges(liveOutcome, reims.SetOpts{Name: displayFixtureVM, CPUs: 4})
|
|
got := mergePendingChanges([]PendingChange{
|
|
{Field: "cpus", From: "4", To: "8"},
|
|
{Field: "memory", From: "8G", To: "12G"},
|
|
}, diffs)
|
|
wantAfterRevert := []PendingChange{{Field: "memory", From: "8G", To: "12G"}}
|
|
if !slices.Equal(got, wantAfterRevert) {
|
|
t.Fatalf("merge after live-value revert = %+v, want %+v", got, wantAfterRevert)
|
|
}
|
|
}
|
|
|
|
func TestApplyAutoResolutionTransaction(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
failSidecar, failSet bool
|
|
}{
|
|
{name: "saved"},
|
|
{name: "sidecar write fails", failSidecar: true},
|
|
{name: "set fails", failSet: true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
const initial = "1234x876"
|
|
a, fixture := newDisplayCtlApp(t, displayCtlFixture{State: "stopped", Resolution: initial, FailSet: tc.failSet}, true)
|
|
dir := fixture().Dir
|
|
prev := vmstart.VMDisplayConfig{TargetDisplay: "unplugged-display"}
|
|
if tc.failSidecar {
|
|
if err := os.Mkdir(filepath.Join(dir, ".reims-go-display.json"), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
} else if _, err := vmstart.SaveVMDisplay(dir, prev); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
on := true
|
|
err := a.ApplyVMSettings(reims.SetOpts{Name: displayFixtureVM, Resolution: "1280x800", AutoResolution: &on, NestedVirt: &on})
|
|
if tc.failSidecar {
|
|
if err == nil || len(fixture().Commands) != 0 {
|
|
t.Fatalf("save = %v, commands = %v, want sidecar error before set", err, fixture().Commands)
|
|
}
|
|
return
|
|
}
|
|
vm := fixture()
|
|
target, _ := host.RecommendAutoResolution(host.PrimaryDisplayOf(host.ConnectedDisplays()), false, true)
|
|
if !slices.Equal(vm.Commands, []string{"set test-vm --resolution " + target}) {
|
|
t.Fatalf("commands = %v", vm.Commands)
|
|
}
|
|
if nested, readErr := vmstart.ReadVMNestedVirt(dir); readErr != nil || nested.Enabled != !tc.failSet {
|
|
t.Fatalf("nested virtualization = %+v, %v, want enabled = %t", nested, readErr, !tc.failSet)
|
|
}
|
|
if tc.failSet {
|
|
if err == nil || !vm.NestedVirtBeforeSet || vm.Resolution != initial || vmstart.ReadVMDisplay(dir) != prev {
|
|
t.Fatalf("save = %v, VM = %+v, display = %+v, want original settings", err, vm, vmstart.ReadVMDisplay(dir))
|
|
}
|
|
return
|
|
}
|
|
prev.AutoResolution = true
|
|
if err != nil || vm.Resolution != target || vmstart.ReadVMDisplay(dir) != prev {
|
|
t.Fatalf("save = %v, VM = %+v, display = %+v, want %s and %+v", err, vm, vmstart.ReadVMDisplay(dir), target, prev)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyAutoResolutionWithSVGA(t *testing.T) {
|
|
for _, graphics := range []string{"vmware-svga", "reims-vgpu"} {
|
|
t.Run(graphics, func(t *testing.T) {
|
|
const initial = "1234x876"
|
|
a, fixture := newDisplayCtlApp(t, displayCtlFixture{State: "stopped", Resolution: initial, Graphics: graphics}, true)
|
|
dir := fixture().Dir
|
|
prev := vmstart.VMDisplayConfig{TargetDisplay: "secondary", AutoResolution: true}
|
|
if _, err := vmstart.SaveVMDisplay(dir, prev); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
detail, err := a.GetVMDetail(displayFixtureVM)
|
|
if err != nil || detail.AutoResolution != (graphics == "reims-vgpu") {
|
|
t.Fatalf("details = %+v, %v", detail, err)
|
|
}
|
|
on := true
|
|
opts := reims.SetOpts{Name: displayFixtureVM, AutoResolution: &on}
|
|
var want []string
|
|
if graphics != "vmware-svga" {
|
|
opts.Graphics = "vmware-svga"
|
|
want = []string{"set test-vm --graphics vmware-svga"}
|
|
}
|
|
if err := a.ApplyVMSettings(opts); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
prev.AutoResolution = false
|
|
if vm := fixture(); vm.Graphics != "vmware-svga" || vm.Resolution != initial ||
|
|
!slices.Equal(vm.Commands, want) || vmstart.ReadVMDisplay(dir) != prev {
|
|
t.Fatalf("VM = %+v, display = %+v, want commands %v and display %+v", vm, vmstart.ReadVMDisplay(dir), want, prev)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyAutoResolutionRollsBackDisplayOnLaterFailure(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
state, blockedFile string
|
|
}{
|
|
{"running", ".reims-go-nested-virt.json"},
|
|
{"stopped", ".reims-go-nested-virt.json"},
|
|
{"running", ".reims-go-guest-lang"},
|
|
{"stopped", ".reims-go-guest-lang"},
|
|
} {
|
|
t.Run(tc.state+"/"+tc.blockedFile, func(t *testing.T) {
|
|
const initial = "1234x876"
|
|
a, fixture := newDisplayCtlApp(t, displayCtlFixture{State: tc.state, Resolution: initial}, true)
|
|
dir := fixture().Dir
|
|
prev := vmstart.VMDisplayConfig{TargetDisplay: "unplugged-display"}
|
|
if _, err := vmstart.SaveVMDisplay(dir, prev); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Mkdir(filepath.Join(dir, tc.blockedFile), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
on := true
|
|
opts := reims.SetOpts{Name: displayFixtureVM, Memory: "4G", AutoResolution: &on, NestedVirt: &on}
|
|
if tc.blockedFile == ".reims-go-guest-lang" {
|
|
opts.GuestLang = "zh-Hans:0"
|
|
}
|
|
if err := a.ApplyVMSettings(opts); err == nil || !strings.Contains(err.Error(), tc.blockedFile) {
|
|
t.Fatalf("save = %v, want error writing %s", err, tc.blockedFile)
|
|
}
|
|
if commands := fixture().Commands; len(commands) != 0 {
|
|
t.Fatalf("commands = %v, want no reims-ctl set call before local failures", commands)
|
|
}
|
|
if got := vmstart.ReadVMDisplay(dir); got != prev || len(a.pendingChanges) != 0 {
|
|
t.Fatalf("display = %+v, want %+v; pending = %+v", got, prev, a.pendingChanges)
|
|
}
|
|
if raw, found := reims.ReadConfiguredResolution(dir); !found || raw != initial {
|
|
t.Fatalf("configured resolution = %q, found = %t, want %q", raw, found, initial)
|
|
}
|
|
if opts.GuestLang != "" {
|
|
if nested, err := vmstart.ReadVMNestedVirt(dir); err != nil || nested.Enabled {
|
|
t.Fatalf("nested virtualization = %+v, %v, want disabled", nested, err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyAutoResolutionSyncsExistingConfiguration(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name, graphics string
|
|
borderless bool
|
|
}{
|
|
{name: "windowed multi-display"},
|
|
{name: "borderless multi-display", borderless: true},
|
|
{name: "from SVGA", graphics: "vmware-svga"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
res, _ := host.RecommendAutoResolution(host.PrimaryDisplayOf(host.ConnectedDisplays()), tc.borderless, true)
|
|
raw := res
|
|
want := "set test-vm --resolution " + res
|
|
on := true
|
|
opts := reims.SetOpts{Name: displayFixtureVM, AutoResolution: &on}
|
|
if tc.graphics == "vmware-svga" {
|
|
opts.Graphics = "reims-vgpu"
|
|
want = "set test-vm --graphics reims-vgpu --resolution " + res
|
|
} else {
|
|
raw += ",2560x1440"
|
|
}
|
|
a, fixture := newDisplayCtlApp(t, displayCtlFixture{
|
|
State: "stopped", Resolution: res, Graphics: tc.graphics, Borderless: tc.borderless,
|
|
}, true)
|
|
dir := fixture().Dir
|
|
if err := writeDisplayCtlResolution(dir, raw); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.ApplyVMSettings(opts); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := fixture().Commands; !slices.Equal(got, []string{want}) {
|
|
t.Fatalf("commands = %v, want %q", got, want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCreateVMWithAutoResolution(t *testing.T) {
|
|
for _, useDriver := range []bool{false, true} {
|
|
for _, tc := range []struct {
|
|
name, graphics, resolution string
|
|
wantAuto bool
|
|
}{
|
|
{name: "no draft", wantAuto: true},
|
|
{name: "stale draft", resolution: "1234x876", wantAuto: true},
|
|
{name: "SVGA", graphics: "vmware-svga", resolution: "1234x876"},
|
|
{name: "SVGA without resolution", graphics: "vmware-svga"},
|
|
} {
|
|
t.Run(fmt.Sprintf("driver=%t/%s", useDriver, tc.name), func(t *testing.T) {
|
|
a, fixture := newDisplayCtlApp(t, displayCtlFixture{}, false)
|
|
if useDriver {
|
|
d := macosdriver.NewReimsMacOSDriver(func() config.Config { return *a.cfg })
|
|
req := driver.CreateRequest{Name: displayFixtureVM, Options: map[string]any{"auto_resolution": true, "resolution": tc.resolution, "graphics": tc.graphics}}
|
|
if err := d.Create(t.Context(), req, io.Discard, io.Discard); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
} else {
|
|
on := true
|
|
if err := a.CreateVM(reims.CreateOpts{Name: displayFixtureVM, CPUs: 1, SourceMode: "disk", AutoResolution: &on, Resolution: tc.resolution, Graphics: tc.graphics}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
vm := fixture()
|
|
want := tc.resolution
|
|
if tc.wantAuto {
|
|
want, _ = host.RecommendAutoResolution(host.PrimaryDisplayOf(host.ConnectedDisplays()), false, true)
|
|
}
|
|
if vm.Resolution != want || vmstart.ReadVMDisplay(vm.Dir).AutoResolution != tc.wantAuto {
|
|
t.Fatalf("created VM = %+v, want resolution %s and automatic mode %t", vm, want, tc.wantAuto)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCreateVMWithAutoResolutionRefreshesAfterDiskConversion(t *testing.T) {
|
|
a, fixture := newDisplayCtlApp(t, displayCtlFixture{}, false)
|
|
source := filepath.Join(a.cfg.DataRoot, "source.raw")
|
|
if err := os.WriteFile(source, []byte("raw disk"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
connectedDisplays := func() []host.DisplayInfo {
|
|
converted, err := filepath.Glob(filepath.Join(a.cfg.TempDir(), "import-*.qcow2"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(converted) == 0 {
|
|
return []host.DisplayInfo{{Primary: true, Width: 3840, Height: 2160, ScalePct: 150}}
|
|
}
|
|
return []host.DisplayInfo{{Primary: true, Width: 3200, Height: 2000, ScalePct: 200}}
|
|
}
|
|
on := true
|
|
opts := reims.CreateOpts{Name: displayFixtureVM, CPUs: 1, SourceMode: "disk", Disk: source, AutoResolution: &on, Borderless: &on}
|
|
if err := a.createVMFlow(opts, connectedDisplays); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if vm := fixture(); vm.Resolution != "3200x2000" || !vmstart.ReadVMDisplay(vm.Dir).AutoResolution {
|
|
t.Fatalf("created VM = %+v, want automatic 3200x2000", vm)
|
|
}
|
|
}
|
|
|
|
func TestClonePreservesAutoResolution(t *testing.T) {
|
|
a, fixture := newDisplayCtlApp(t, displayCtlFixture{State: "stopped", Resolution: "2880x1800"}, false)
|
|
source := fixture().Dir
|
|
if _, err := vmstart.SaveVMDisplay(source, vmstart.VMDisplayConfig{TargetDisplay: "DISPLAY2", AutoResolution: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
d := macosdriver.NewReimsMacOSDriver(func() config.Config { return *a.cfg })
|
|
if err := d.CloneCoW(t.Context(), displayFixtureVM, displayFixtureClone, io.Discard, io.Discard); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := vmstart.ReadVMDisplay(fixture().CloneDir); got != (vmstart.VMDisplayConfig{AutoResolution: true}) {
|
|
t.Fatalf("cloned display preference = %+v", got)
|
|
}
|
|
}
|