Files
Reims-Go/internal/app/legacy_ledger_test.go
T
千川白浪 107a8d73e0 refactor: 收拢核心管线、强化错误处理、优化热路径与更新体积 (#200)
* 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.
2026-12-05 20:54:41 +08:00

136 lines
5.4 KiB
Go

package app
import (
"encoding/json"
"strings"
"testing"
"github.com/Nwflower/reims-go/internal/driver"
"github.com/Nwflower/reims-go/internal/macos/guest"
"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"
)
func stripLegacyManagedExtraArgs(extraArgs string) string {
s := reims.StripGuestToolsArgs(extraArgs)
s = reims.StripPacketBridgeArgs(s)
s = reims.StripGuestToolsNetworkArgs(s)
s = reims.StripOfflineHelperArgs(s)
s = qmp.StripLegacyWin10RebootArgs(s)
s = reims.StripIncomingDeferArgs(s)
return strings.TrimSpace(s)
}
func vmInfoFromMacOSDetail(detail reims.VMDetail) driver.VMInfo {
raw, _ := json.Marshal(detail)
var data map[string]any
_ = json.Unmarshal(raw, &data)
return driver.VMInfo{Name: detail.Name, DriverID: driver.BuiltinReimsMacOSDriverID, State: detail.State, CPUs: detail.CPUs, Memory: detail.Memory, DiskSummary: detail.Disk, DiskPath: detail.DiskPath(), Directory: detail.Directory, Network: detail.Network, SSHPort: detail.SSHPort, DriverData: data}
}
func TestLegacyExtraArgsAdoptedWithoutDuplication(t *testing.T) {
// Simulate an existing pre-plugin VM whose vm.toml already contains:
// user arg + REIMSTOOLS + REIMSHELP + Win10 managed QMP args, with NO ledger file yet.
legacy := "-cpu host"
legacy = reims.MergeGuestToolsArgs(legacy, `D:\Data\cache\tools.img`)
legacy = reims.MergeOfflineHelperArgs(legacy, `D:\Data\cache\reimshelp.img`)
legacy = qmp.MergeWin10RebootUnixArgs(legacy, `D:\Data\vms\mac\reims-go-reboot.sock`)
ledger := &plugin.ArgLedger{Version: 1, Owners: map[string][]plugin.ArgFragment{}}
active := map[string]bool{
builtin.IDGuestTools: true,
guest.ID: true,
}
intents := []plugin.StartIntent{
{
PluginID: builtin.IDGuestTools,
Args: []plugin.ArgFragment{{
PluginID: builtin.IDGuestTools, Key: "reimstools", Builtin: true, Priority: 80,
Tokens: qmp.SplitArgs(reims.MergeGuestToolsArgs("", `D:\Data\cache\tools.img`)),
}},
},
{
PluginID: guest.ID,
Args: []plugin.ArgFragment{{
PluginID: guest.ID, Key: "reimshelp", Builtin: true, Priority: 85,
Tokens: qmp.SplitArgs(reims.MergeOfflineHelperArgs("", `D:\Data\cache\reimshelp.img`)),
}, {
PluginID: guest.ID, Key: "win10-cold-reboot", Builtin: true, Priority: 95,
Tokens: qmp.SplitArgs(qmp.MergeWin10RebootUnixArgs("", `D:\Data\vms\mac\reims-go-reboot.sock`)),
}},
},
}
base := stripLegacyManagedExtraArgs(legacy)
res := plugin.ReconcileStartIntentsForDriver(base, ledger, active, "", intents)
if !strings.Contains(res.ExtraArgs, "-cpu host") {
t.Fatalf("user arg lost: %s", res.ExtraArgs)
}
for _, needle := range []string{"id=reimstools,", "id=reimstools-dev,", "id=reimshelp,", "id=reimshelp-dev,", "-no-reboot", "-qmp"} {
if got := strings.Count(res.ExtraArgs, needle); got != 1 {
t.Errorf("expected exactly 1 occurrence of %q, got %d in: %s", needle, got, res.ExtraArgs)
}
}
// Simulate detaching Guest Tools & ejecting offline installer on next start.
nextIntents := []plugin.StartIntent{
{PluginID: builtin.IDGuestTools, ClearKeys: []string{"reimstools"}},
{PluginID: guest.ID, ClearKeys: []string{"reimshelp", "win10-cold-reboot"}},
}
base2 := stripLegacyManagedExtraArgs(res.ExtraArgs)
res2 := plugin.ReconcileStartIntentsForDriver(base2, ledger, active, "", nextIntents)
if res2.ExtraArgs != "-cpu host" {
t.Fatalf("expected only user arg after clearing managed keys, got: %q", res2.ExtraArgs)
}
}
func TestStripLegacyPreservesUserNoReboot(t *testing.T) {
// A user hand-wrote `-no-reboot` without any reims-go managed QMP target.
got := stripLegacyManagedExtraArgs("-cpu host -no-reboot")
if got != "-cpu host -no-reboot" {
t.Fatalf("user -no-reboot was stripped unexpectedly: %q", got)
}
}
// A VM started before the builtins were merged has kernel-irqchip=off owned
// by the retired whpx-fallback plugin. The core's WHPX step leaves an arg that
// is already there alone, so without handing the fragment over it would be
// stripped as belonging to a gone plugin and the next start could crash.
func TestPreMergeLedgerFragmentsCarriedIntoCore(t *testing.T) {
irq := plugin.ArgFragment{
PluginID: builtin.IDCore, Key: "kernel-irqchip", Builtin: true, Priority: 90,
Tokens: []string{"-machine", "kernel-irqchip=off"},
}
extra := "-cpu host -machine kernel-irqchip=off"
active := map[string]bool{guest.ID: true}
intents := []plugin.StartIntent{{PluginID: guest.ID}}
newLedger := func() *plugin.ArgLedger {
return &plugin.ArgLedger{Version: 1, Owners: map[string][]plugin.ArgFragment{
builtin.IDCore: {irq},
}}
}
unmigrated := plugin.ReconcileStartIntentsForDriver(extra, newLedger(), active, "", intents)
if strings.Contains(unmigrated.ExtraArgs, "kernel-irqchip") {
t.Fatalf("test premise: an unmigrated ledger should drop the fragment, got %q", unmigrated.ExtraArgs)
}
ledger := newLedger()
if !guest.MigrateLedger(ledger) {
t.Fatal("MigrateLedger reported no change for a retired owner")
}
res := plugin.ReconcileStartIntentsForDriver(extra, ledger, active, "", intents)
if got := strings.Count(res.ExtraArgs, "kernel-irqchip=off"); got != 1 {
t.Fatalf("want kernel-irqchip=off carried exactly once, got %d in %q", got, res.ExtraArgs)
}
if !strings.Contains(res.ExtraArgs, "-cpu host") {
t.Fatalf("user arg lost: %q", res.ExtraArgs)
}
if guest.MigrateLedger(ledger) {
t.Fatal("MigrateLedger should be a no-op once migrated")
}
}