Files
mfd-monitor-agent/internal/collector/collector_windows_test.go
codex 89d5e27874 💥 feat(agent)!: 添加主机监控代理首版
* 实现周期采集、快照缓存、本地 HTTP API 和主动上报

* 补齐 Linux 和 Windows 主机、网络、资源、进程和 TCP 采集

* 添加服务生命周期管理、配置加载、构建脚本和测试覆盖

BREAKING CHANGE: network.ips 拆分为 network.ipv4s 和 network.ipv6s。
2026-05-18 15:25:51 +00:00

88 lines
2.7 KiB
Go

//go:build windows
package collector
import (
"syscall"
"testing"
)
// TestWindowsMemoryInfoFromStatus verifies Windows memory counters are exposed in bytes.
func TestWindowsMemoryInfoFromStatus(t *testing.T) {
got := windowsMemoryInfoFromStatus(windowsMemoryStatus{
TotalPhys: 10,
AvailPhys: 4,
})
if got.Unit != "byte" {
t.Fatalf("unit = %q, want byte", got.Unit)
}
if got.Total != 10 || got.Available != 4 || got.Free != 4 || got.Used != 6 {
t.Fatalf("memory = %+v", got)
}
if got.UsedPercent != 60 {
t.Fatalf("used percent = %f, want 60", got.UsedPercent)
}
}
// TestWindowsTCPPortFromNetworkOrder verifies Windows TCP port values are decoded.
func TestWindowsTCPPortFromNetworkOrder(t *testing.T) {
if got := windowsTCPPortFromNetworkOrder(0x0000901f); got != 8080 {
t.Fatalf("port = %d, want 8080", got)
}
}
// TestWindowsDiskInfoFromSpace verifies Windows disk counters are mapped to API disk fields.
func TestWindowsDiskInfoFromSpace(t *testing.T) {
got := windowsDiskInfoFromSpace(`C:\`, windowsDriveFixed, 100, 40)
if got.Device != `C:\` || got.MountPoint != `C:\` {
t.Fatalf("disk paths = %q/%q", got.Device, got.MountPoint)
}
if got.FSType != "fixed" || got.Unit != "byte" {
t.Fatalf("disk type/unit = %q/%q", got.FSType, got.Unit)
}
if got.Total != 100 || got.Free != 40 || got.Used != 60 {
t.Fatalf("disk capacity = %+v", got)
}
if got.UsedPercent != 60 {
t.Fatalf("used percent = %f, want 60", got.UsedPercent)
}
}
// TestWindowsCPUUsagePercent verifies idle and kernel counters are converted to busy percentage.
func TestWindowsCPUUsagePercent(t *testing.T) {
prev := windowsSystemTimes{Idle: 100, Kernel: 500, User: 500}
next := windowsSystemTimes{Idle: 150, Kernel: 650, User: 700}
got := windowsCPUUsagePercent(prev, next)
if got != 75 {
t.Fatalf("cpu usage = %f, want 75", got)
}
}
// TestWindowsProcessInfoFromEntry verifies Toolhelp process data is enriched with memory and command line data.
func TestWindowsProcessInfoFromEntry(t *testing.T) {
entry := syscall.ProcessEntry32{ProcessID: 123, ParentProcessID: 12}
copy(entry.ExeFile[:], syscall.StringToUTF16("agent.exe"))
got := windowsProcessInfoFromEntry(entry, windowsProcessDetails{
Exe: `C:\agent\agent.exe`,
Cmdline: `"C:\agent\agent.exe" run`,
Username: `NT AUTHORITY\SYSTEM`,
MemoryRSS: 2048,
MemoryVMS: 4096,
})
if got.PID != 123 || got.PPID != 12 || got.Name != "agent.exe" {
t.Fatalf("process identity = %+v", got)
}
if got.Exe != `C:\agent\agent.exe` || got.Cmdline != `"C:\agent\agent.exe" run` {
t.Fatalf("process command fields = %+v", got)
}
if got.Username != `NT AUTHORITY\SYSTEM` || got.MemoryRSS != 2048 || got.MemoryVMS != 4096 {
t.Fatalf("process details = %+v", got)
}
}