package macosdriver import ( "os" "path/filepath" "slices" "strings" "testing" "github.com/Nwflower/reims-go/internal/config" "github.com/Nwflower/reims-go/internal/host" "github.com/Nwflower/reims-go/internal/macos/reims" "github.com/Nwflower/reims-go/internal/plugin" "github.com/Nwflower/reims-go/internal/plugin/builtin" "github.com/Nwflower/reims-go/internal/qmp" rt "github.com/Nwflower/reims-go/internal/runtime" "github.com/Nwflower/reims-go/internal/vmstart" ) func argKeys(in plugin.StartIntent) []string { keys := make([]string, 0, len(in.Args)) for _, arg := range in.Args { keys = append(keys, arg.Key) } return keys } func TestDirectStartKeepsGuestToolsAndClearsDesktopReboot(t *testing.T) { const owner = "builtin.guest.macos" cfg := config.Config{DataRoot: t.TempDir()} rep := host.Report{NeedsColdRebootWorkaround: true} root := cfg.DataRoot tools := reims.MergeGuestToolsArgs("-cpu host", "tools.img") management, err := reims.GuestToolsNetworkUnixArgs(root, "out.sock", "in.sock") if err != nil { t.Fatal(err) } tools += " " + reims.JoinShellArgs(management) attached := qmp.MergeWin10RebootUnixArgs(tools, qmp.RebootSocketPath(root, cfg.TempDir(), "mac")) d := &ReimsMacOSDriver{opts: Options{OwnerID: owner}} intent := plugin.StartIntent{PluginID: owner} if err := d.prepareColdReboot(cfg, "mac", root, rep, &intent); err != nil { t.Fatal(err) } if len(intent.PostStart) != 0 || !slices.Contains(intent.ClearKeys, keyWin10ColdReboot) { t.Fatalf("direct start cold-reboot intent = %+v", intent) } for _, enabled := range []bool{true, false} { active := map[string]bool{owner: true, builtin.IDGuestTools: enabled} base := startBaseArgs(attached, false, active) out := plugin.ReconcileStartIntentsForDriver(base, &plugin.ArgLedger{}, active, owner, []plugin.StartIntent{intent}) if strings.Contains(out.ExtraArgs, "-no-reboot") || strings.Contains(out.ExtraArgs, "reimstools-net") || !strings.Contains(out.ExtraArgs, "-cpu host") || reims.HasGuestToolsArgs(out.ExtraArgs) != enabled { t.Fatalf("Guest Tools enabled=%v: unexpected launch args %q", enabled, out.ExtraArgs) } } } func TestPrepareWorkaroundsHonourSettingsAndRuntime(t *testing.T) { whpx, cold := true, true d := &ReimsMacOSDriver{opts: Options{OwnerID: "test.macos", ManagedByDesktop: true, WHPXFallbackEnabled: func() bool { return whpx }, ColdRebootEnabled: func() bool { return cold }}} rep := host.Report{WHPXCaps: host.WHPXCapabilities{KernelIrqchipAvailable: true, NestedVirtProperty: false}} var in plugin.StartIntent d.prepareWHPXFallback(config.Config{}, rt.Installed{}, reims.VMDetail{}, rep, &in) if !slices.Equal(argKeys(in), []string{keyKernelIrqchip}) { t.Fatalf("WHPX args = %+v", in.Args) } for _, key := range []string{keyOfflineHelper, keyWin10ColdReboot} { in.ClearKeys = append(in.ClearKeys, key) } in = plugin.StartIntent{} d.prepareWHPXFallback(config.Config{}, rt.Installed{Manifest: rt.Manifest{Capabilities: []string{"nested-virt-nonfatal"}}}, reims.VMDetail{}, rep, &in) if len(in.Args) != 0 || !slices.Contains(in.ClearKeys, keyKernelIrqchip) { t.Fatalf("capable runtime = %+v", in) } whpx = false in = plugin.StartIntent{} d.prepareWHPXFallback(config.Config{}, rt.Installed{}, reims.VMDetail{}, rep, &in) if len(in.Args) != 0 { t.Fatalf("disabled WHPX fallback = %+v", in) } rep.NeedsColdRebootWorkaround = true root := t.TempDir() cfg := config.Config{DataRoot: root} in = plugin.StartIntent{} if err := d.prepareColdReboot(cfg, "mac", root, rep, &in); err != nil { t.Fatal(err) } if !slices.Contains(argKeys(in), keyWin10ColdReboot) || len(in.PostStart) != 1 { t.Fatalf("cold reboot = %+v", in) } cold = false in = plugin.StartIntent{} if err := d.prepareColdReboot(cfg, "mac", root, rep, &in); err != nil { t.Fatal(err) } if len(in.Args) != 0 || len(in.PostStart) != 0 { t.Fatalf("disabled cold reboot = %+v", in) } } func TestNestedVirtFollowsTheVMSwitch(t *testing.T) { const owner = "test.macos" const owned = "-global driver=x86_64-cpu,property=vmx,value=on" dir := t.TempDir() rep := host.Report{WHPXCaps: host.WHPXCapabilities{NestedVMX: true}} prepare := func(userArgs string, rep host.Report) plugin.StartIntent { in := plugin.StartIntent{PluginID: owner} AppendNestedVirtIntent(&in, "en-US", dir, userArgs, rep) return in } if in := prepare("", rep); !slices.Contains(in.ClearKeys, keyNestedVirt) || len(in.Args) != 0 { t.Fatalf("switched off = %+v", in) } if err := os.WriteFile(filepath.Join(dir, ".reims-go-nested-virt.json"), []byte("{"), 0o644); err != nil { t.Fatal(err) } if in := prepare("", rep); len(in.Warnings) != 1 || len(in.Args) != 0 { t.Fatalf("damaged switch = %+v", in) } if err := vmstart.SaveVMNestedVirt(dir, vmstart.VMNestedVirtConfig{Enabled: true}); err != nil { t.Fatal(err) } if in := prepare("", host.Report{}); len(in.Args) != 0 || len(in.Warnings) != 1 { t.Fatalf("unsupported host = %+v", in) } if in := prepare("-accel whpx,kernel-irqchip=off", rep); len(in.Args) != 0 || len(in.Warnings) != 1 { t.Fatalf("userspace interrupt controller = %+v", in) } if in := prepare(owned, rep); len(in.Args) != 0 { t.Fatalf("user owns VMX argument = %+v", in) } in := prepare("", rep) if !slices.Equal(argKeys(in), []string{keyNestedVirt}) || !in.Args[0].Builtin || in.Args[0].PluginID != owner || strings.Join(in.Args[0].Tokens, " ") != owned { t.Fatalf("switched on = %+v", in.Args) } const user = "-cpu host -machine kernel-irqchip=on" active := map[string]bool{owner: true} ledger := &plugin.ArgLedger{} on := plugin.ReconcileStartIntentsForDriver(user, ledger, active, owner, []plugin.StartIntent{in}) if on.ExtraArgs != user+" "+owned { t.Fatalf("launch args with the switch on = %q", on.ExtraArgs) } // reims-ctl 3731afd-p3 quotes the property when returning extra args. readback := user + " -global 'driver=x86_64-cpu,property=vmx,value=on'" if got := plugin.UserExtraArgs(readback, ledger); got != user { t.Fatalf("what is left to the VM's own arguments = %q", got) } if err := vmstart.SaveVMNestedVirt(dir, vmstart.VMNestedVirtConfig{}); err != nil { t.Fatal(err) } off := plugin.ReconcileStartIntentsForDriver(readback, ledger, active, owner, []plugin.StartIntent{prepare("", rep)}) if off.ExtraArgs != user { t.Fatalf("launch args after switching off = %q", off.ExtraArgs) } }