package monitoring import "testing" func TestParseCPUStatAndMemoryInfo(t *testing.T) { idle, total, err := parseCPUStat("cpu 100 20 30 400 50 6 7 8 0 0\n") if err != nil || idle != 450 || total != 621 { t.Fatalf("idle=%d total=%d err=%v", idle, total, err) } used, memoryTotal, err := parseMemoryInfo("MemTotal: 1000 kB\nMemAvailable: 250 kB\n") if err != nil || used != 750*1024 || memoryTotal != 1000*1024 { t.Fatalf("used=%d total=%d err=%v", used, memoryTotal, err) } } func TestCPUPercentUsesTwoMonotonicSamples(t *testing.T) { value := cpuPercent(450, 621, 500, 721) if value != 50 { t.Fatalf("cpu percent=%v, want 50", value) } if cpuPercent(500, 721, 450, 621) != 0 { t.Fatal("non-monotonic CPU counters must fail closed to zero") } }