将代码与测试注释改为中文以符合仓库规范;新增中文部署指南、 示例配置,并完善构建脚本对 VERSION 注入的说明。 Co-authored-by: Cursor <cursoragent@cursor.com>
88 lines
2.7 KiB
Go
88 lines
2.7 KiB
Go
//go:build windows
|
|
|
|
package collector
|
|
|
|
import (
|
|
"syscall"
|
|
"testing"
|
|
)
|
|
|
|
// TestWindowsMemoryInfoFromStatus 验证 Windows 内存计数以字节对外暴露。
|
|
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 验证 Windows TCP 端口值被正确解码。
|
|
func TestWindowsTCPPortFromNetworkOrder(t *testing.T) {
|
|
if got := windowsTCPPortFromNetworkOrder(0x0000901f); got != 8080 {
|
|
t.Fatalf("port = %d, want 8080", got)
|
|
}
|
|
}
|
|
|
|
// TestWindowsDiskInfoFromSpace 验证 Windows 磁盘计数映射到 API 磁盘字段。
|
|
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 验证空闲与内核计数转换为繁忙百分比。
|
|
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 验证 Toolhelp 进程数据补充内存与命令行信息。
|
|
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)
|
|
}
|
|
}
|