Files
Reims-Go/internal/qmp/watcher_test.go
T
NwflowerandClaude Opus 5.5 c639682a88 refactor: remove unused functions and keep the codebase free of them
Delete functions that no product code calls, and fold "simple plus
WithOptions" pairs into the one signature callers actually use. Rename
the live macOS lifecycle functions that were labelled Legacy.

The architecture tests now share one source loader and reject functions
without a product caller and catalog messages nobody reads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 20:55:03 -07:00

108 lines
3.7 KiB
Go

package qmp
import (
"bufio"
"context"
"net"
"path/filepath"
"strings"
"testing"
"time"
)
func TestHasWin10RebootUnixArgs(t *testing.T) {
sock := "D:\\VM data\\group,1\\reims-go-reboot.sock"
args := MergeWin10RebootUnixArgs("-machine q35", sock)
if !HasWin10RebootUnixArgs(args, sock) {
t.Fatal("saved socket with escaped comma and quoted path was not recognized")
}
if HasWin10RebootUnixArgs(strings.ReplaceAll(args, "-no-reboot", ""), sock) {
t.Fatal("recognized a monitor without the cold-reboot flag")
}
if HasWin10RebootUnixArgs(args, "D:/other/reims-go-reboot.sock") {
t.Fatal("recognized a different monitor")
}
if HasWin10RebootUnixArgs("-no-reboot -qmp tcp:127.0.0.1:45555,server=on,wait=off", sock) {
t.Fatal("recognized a legacy TCP monitor")
}
}
func TestMergeWin10RebootUnixArgs(t *testing.T) {
got := MergeWin10RebootUnixArgs("", `D:\Data\reims-go\vms\m0\reims-go-reboot.sock`)
want := "-no-reboot -qmp unix:D:/Data/reims-go/vms/m0/reims-go-reboot.sock,server=on,wait=off"
if got != want {
t.Fatalf("empty merge: got %q, want %q", got, want)
}
// Migrates legacy insecure TCP QMP args and preserves unrelated flags.
got = MergeWin10RebootUnixArgs("-boot menu=on -qmp 'tcp:127.0.0.1:58557,server=on,wait=off' -no-reboot -d guest_errors", `D:\Data\vms\m0\reims-go-reboot.sock`)
want = "-boot menu=on -d guest_errors -no-reboot -qmp unix:D:/Data/vms/m0/reims-go-reboot.sock,server=on,wait=off"
if got != want {
t.Fatalf("legacy tcp migration: got %q, want %q", got, want)
}
// Idempotent replacement with spaces and commas in path.
got = MergeWin10RebootUnixArgs(got, `D:\My Data\vm,1\reims-go-reboot.sock`)
want = `-boot menu=on -d guest_errors -no-reboot -qmp "unix:D:/My Data/vm,,1/reims-go-reboot.sock,server=on,wait=off"`
if got != want {
t.Fatalf("space/comma path merge: got %q, want %q", got, want)
}
got2 := MergeWin10RebootUnixArgs(got, `D:\Data\vms\m0\reims-go-reboot.sock`)
if strings.Count(got2, "-qmp ") != 1 {
t.Fatalf("expected single -qmp after replace, got %q", got2)
}
}
func TestRebootSocketPathFallback(t *testing.T) {
shortDir := filepath.Join("D:", "Data", "vms", "m0")
if p := RebootSocketPath(shortDir, filepath.Join("D:", "Data", "tmp"), "m0"); !strings.HasSuffix(p, "reims-go-reboot.sock") {
t.Fatalf("short path unexpected: %q", p)
}
longDir := filepath.Join("D:", strings.Repeat("verylongdir/", 10), "m0")
tmpDir := filepath.Join("D:", "Data", "tmp")
p := RebootSocketPath(longDir, tmpDir, "m0")
if !strings.HasPrefix(p, tmpDir) || len(p) > 100 {
t.Fatalf("expected short tmp fallback, got %q (len=%d)", p, len(p))
}
}
func runMockQMPUnix(t *testing.T, eventJSON string) (string, error) {
t.Helper()
const sockPath = "reims-go-reboot.sock"
ln, err := net.Listen("unix", sockPath)
if err != nil {
t.Fatal(err)
}
defer ln.Close()
go func() {
conn, err := ln.Accept()
if err != nil {
return
}
defer conn.Close()
_, _ = conn.Write([]byte("{\"QMP\":{\"version\":{\"qemu\":{\"major\":11}}}}\r\n"))
sc := bufio.NewScanner(conn)
if sc.Scan() {
_, _ = conn.Write([]byte("{\"return\":{}}\r\n"))
}
_, _ = conn.Write([]byte(eventJSON + "\r\n"))
}()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
return WatchShutdownReason(ctx, "unix:"+sockPath)
}
func TestWatchShutdownReason(t *testing.T) {
// Unix socket addresses have a small byte limit. The isolated working
// directory lets the mock and watcher use the same short relative address.
t.Chdir(t.TempDir())
for _, want := range []string{"guest-reset", "guest-shutdown", "host-ui"} {
got, err := runMockQMPUnix(t, `{"event":"SHUTDOWN","data":{"guest":true,"reason":"`+want+`"}}`)
if err != nil || got != want {
t.Fatalf("reason = %q, %v, want %q", got, err, want)
}
}
}