Files
Reims-Go/internal/app/vm_monitor_test.go
T

108 lines
4.8 KiB
Go

package app
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
"time"
"github.com/Nwflower/reims-go/internal/i18n"
"github.com/Nwflower/reims-go/internal/macos/reims"
)
func TestLogTailDiscardsOversizeLinesAndResumes(t *testing.T) {
path := filepath.Join(t.TempDir(), "serial.log")
write := func(flags int, text string) {
t.Helper()
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|flags, 0o600)
if err != nil {
t.Fatal(err)
}
defer f.Close()
if _, err := f.WriteString(text); err != nil {
t.Fatal(err)
}
}
var tail logTail
write(os.O_TRUNC, "boot\r\n"+strings.Repeat("x", maxMonitorLine))
if got := tail.read(path); !slices.Equal(got, []string{"boot"}) || len(tail.partial) != maxMonitorLine {
t.Fatal("expected a complete line and an unfinished line at the limit")
}
for range 12 {
write(os.O_APPEND, strings.Repeat("y", maxMonitorLine))
if got := tail.read(path); len(got) != 0 || !tail.skipping || len(tail.partial) != 0 {
t.Fatal("oversized line retained across polls")
}
}
write(os.O_APPEND, "ignored\r\nnext\r\npar")
if got := tail.read(path); !slices.Equal(got, []string{"next"}) {
t.Fatalf("after discarded line: %q", got)
}
write(os.O_APPEND, "tial\n"+strings.Repeat("z", maxMonitorLine+1))
if got := tail.read(path); !slices.Equal(got, []string{"partial"}) || !tail.skipping {
t.Fatalf("after split line: %q", got)
}
write(os.O_TRUNC, "new boot\n")
if got := tail.read(path); !slices.Equal(got, []string{"new boot"}) {
t.Fatalf("after truncation: %q", got)
}
}
func TestParseSerialAndQemuMilestones(t *testing.T) {
k, msg := parseSerialMilestone(i18n.ZhCN, `BdsDxe: starting Boot0002 "UEFI QEMU HARDDISK"`)
if k != "serial:boot" || !strings.Contains(msg, "OpenCore") {
t.Fatalf("serial boot = %q, %q", k, msg)
}
k, msg = parseSerialMilestone(i18n.EnUS, `#[EB|LOG:HANDOFF TO XNU] _`)
if k != "serial:xnu" || !strings.Contains(msg, "XNU") {
t.Fatalf("serial xnu = %q, %q", k, msg)
}
k, msg = parseQemuMilestone(i18n.ZhCN, `reims-vgpu INFO reims_vgpu_vulkan::rt_platform: the rt platform's device: NVIDIA GeForce RTX 4070 Ti (10de:2782), driver NVIDIA 610.88; instance extensions`)
if k != "qemu:gpu" || !strings.Contains(msg, "RTX 4070 Ti") {
t.Fatalf("qemu gpu = %q, %q", k, msg)
}
k, msg = parseQemuMilestone(i18n.ZhCN, `reims-vgpu: rt-diag allocation sites, rt-presents 71011`)
if msg != "" {
t.Fatalf("expected rt-diag dump to be ignored, got %q", msg)
}
k, msg = parseQemuMilestone(i18n.ZhCN, `qemu: -accel whpx: warning: WHPX: Hyper-V refuses the TSC deadline timer's register, hr=c0350005: the guest is offered no TSC deadline timer`)
if msg != "" {
t.Fatalf("expected benign TSC deadline warning to be ignored, got %q", msg)
}
k, msg = parseQemuMilestone(i18n.ZhCN, `reims-vgpu WARN reims_vgpu_graphics: the control completion ring is full: a message of type 0x103 found no room, and ring 0's answers and events wait for the guest to take more`)
if k != "qemu:ring-full" || !strings.Contains(msg, "自动恢复") {
t.Fatalf("expected friendly ring-full hint (zh-CN), got k=%q msg=%q", k, msg)
}
k, msg = parseQemuMilestone(i18n.EnUS, `reims-vgpu WARN reims_vgpu_graphics: the control completion ring is full: a message of type 0x103 found no room, and ring 0's answers and events wait for the guest to take more`)
if k != "qemu:ring-full" || !strings.Contains(msg, "recover automatically") {
t.Fatalf("expected friendly ring-full hint (en-US), got k=%q msg=%q", k, msg)
}
k, msg = parseQemuMilestone(i18n.ZhCN, `reims-vgpu WARN reims_vgpu_graphics: the VBLANK of refresh 1496 found the control completion ring full and was dropped: the next carries the count`)
if msg != "" {
t.Fatalf("expected follow-up VBLANK drop line to be ignored, got %q", msg)
}
k, msg = parseQemuMilestone(i18n.ZhCN, `qemu: WHPX: Unexpected VP exit code 4`)
if k != "" || !strings.Contains(msg, "WHPX: Unexpected VP exit code 4") {
t.Fatalf("expected WHPX error to pass through, got k=%q msg=%q", k, msg)
}
}
func TestFormatBootHeartbeatAndRedactLogLine(t *testing.T) {
hb := formatBootHeartbeat(i18n.ZhCN, 4*time.Minute+30*time.Second, 15*1024*1024*1024, 256*1024*1024, 142.4)
if !strings.Contains(hb, "4m30s") || !strings.Contains(hb, "15.00 GB") || !strings.Contains(hb, "+256.0 MB") || !strings.Contains(hb, "142%") {
t.Fatalf("unexpected heartbeat: %q", hb)
}
redacted := reims.RedactLogLine("network user, mac 52:54:00:bc:bc:0d, ssh on 127.0.0.1:2222")
if strings.Contains(redacted, "bc:bc:0d") || !strings.Contains(redacted, "mac [redacted]") {
t.Fatalf("MAC not redacted: %q", redacted)
}
redactedSerial := reims.RedactLogLine("OpenCore presents the VM as an iMac20,1, serial C02FDGY9PN5T")
if strings.Contains(redactedSerial, "C02FDGY9PN5T") || !strings.Contains(redactedSerial, "serial [redacted]") {
t.Fatalf("serial not redacted: %q", redactedSerial)
}
}