package tests import ( "strings" "testing" ) // TestParseMemAvailable_RealSample exercises parseMemAvailable on a // real /proc/meminfo snippet. Units are always kB and always the // second field; we just want to confirm we strip it correctly. func TestParseMemAvailable_RealSample(t *testing.T) { sample := `MemTotal: 8053292 kB MemFree: 3205104 kB MemAvailable: 6742180 kB Buffers: 145332 kB Cached: 2934064 kB ` got, err := parseMemAvailable(strings.NewReader(sample)) if err != nil { t.Fatalf("parseMemAvailable: %v", err) } want := int64(6742180) * 1024 if got != want { t.Errorf("MemAvailable = %d bytes, want %d", got, want) } } func TestParseMemAvailable_Missing(t *testing.T) { sample := "MemTotal: 8053292 kB\nMemFree: 3205104 kB\n" if _, err := parseMemAvailable(strings.NewReader(sample)); err == nil { t.Errorf("expected error when MemAvailable absent") } } func TestParseMemAvailable_Malformed(t *testing.T) { sample := "MemAvailable:\n" if _, err := parseMemAvailable(strings.NewReader(sample)); err == nil { t.Errorf("expected error on single-field MemAvailable line") } } // TestMemCap_Normal: on a healthy 8GiB box with ~6.4GiB available, // cap lands at ~4.9GiB — well above floor, well below total. func TestMemCap_Normal(t *testing.T) { avail := int64(6742180) * 1024 // ~6.4 GiB cap := avail - memHeadroomBytes if cap < memFloorBytes { t.Errorf("cap=%d should be ≥ floor=%d on 6.4GiB available", cap, memFloorBytes) } // Sanity: headroom carved off 1.5 GiB. if got := avail - cap; got != memHeadroomBytes { t.Errorf("headroom = %d, want %d", got, memHeadroomBytes) } } // TestMemCap_FloorHit: a box with <1.75 GiB available should fall // below the floor so CPUStress refuses the memory pass rather than // running a window too small to be meaningful. func TestMemCap_FloorHit(t *testing.T) { avail := int64(1_500_000_000) // 1.4 GiB cap := avail - memHeadroomBytes if cap >= memFloorBytes { t.Errorf("cap=%d should be < floor=%d on 1.4GiB available (cap pre-clamp)", cap, memFloorBytes) } } // TestMemCap_HugeBox: a 128 GiB box still honors the 1.5 GiB // headroom (no weird upper clamp that would cap us at a tiny // fraction of the RAM). func TestMemCap_HugeBox(t *testing.T) { avail := int64(128) * 1024 * 1024 * 1024 cap := avail - memHeadroomBytes if cap < avail-2*memHeadroomBytes { t.Errorf("cap=%d unexpectedly below avail=%d − 2×headroom", cap, avail) } // Should be comfortably above 100 GiB. if cap < 100*1024*1024*1024 { t.Errorf("cap=%d should exceed 100 GiB on 128 GiB box", cap) } } // TestDurationSeconds_BelowOne floors at "1s"; stress-ng rejects 0. func TestDurationSeconds_BelowOne(t *testing.T) { got := durationSeconds(0) if got != "1s" { t.Errorf("durationSeconds(0) = %q, want 1s", got) } }