nmbrs-runtime 0.7.3

Workload execution runtime for nmbrs
Documentation
// Copyright 2024-2026 Jonathan Shook
// SPDX-License-Identifier: Apache-2.0

//! Tests of the `sysmon=` setting and the host requirements.

use super::*;
use nmbrs_host::category::CATEGORY_NAMES;

/// `all` and the full comma list mean the same thing — the user's words.
#[test]
fn all_equals_the_full_category_list() {
    assert_eq!(parse_categories("all").unwrap(), Categories::ALL);
    assert_eq!(
        parse_categories("cpu,io,ram,rambw,storage,net,pressure,proc,cgroup").unwrap(),
        Categories::ALL
    );
}

#[test]
fn category_subsets_parse_and_typos_are_named_errors() {
    let c = parse_categories("cpu, io").unwrap();
    assert!(c.cpu && c.io && !c.ram && !c.rambw && !c.storage);
    let err = parse_categories("cpu,ramb").unwrap_err();
    assert!(
        err.contains("ramb") && err.contains("rambw"),
        "the error names the typo and the valid set: {err}"
    );
    assert!(parse_categories("").is_err(), "empty enables nothing");
}

/// rambw enabled on a host without resctrl is an ABORT with instructions,
/// not a skip. (This box has no resctrl, so this exercises the real
/// probe.)
#[test]
fn rambw_without_resctrl_aborts_with_instructions() {
    let config = SysmonConfig {
        cats: parse_categories("rambw").unwrap(),
        interval: Duration::from_secs(5),
        membw_peak_bytes_per_s: Some(100e9),
        procs: Vec::new(),
        cgroups: Vec::new(),
    };
    if std::path::Path::new("/sys/fs/resctrl/mon_data").exists() {
        // Host actually has resctrl — the gate passes instead; nothing
        // to assert about instructions here.
        return;
    }
    let err = check_rambw_requirements(&config).unwrap_err();
    assert!(
        err.contains("mount -t resctrl"),
        "the abort must tell the user HOW to enable it: {err}"
    );
    assert!(
        err.contains("sysmon=any"),
        "…and how to run without it: {err}"
    );
}

/// rambw with resctrl but no peak reference is equally an abort — a
/// bytes/s figure with no denominator is not a utilization.
#[test]
fn rambw_without_a_peak_reference_aborts() {
    let config = SysmonConfig {
        cats: parse_categories("rambw").unwrap(),
        interval: Duration::from_secs(5),
        membw_peak_bytes_per_s: None,
        procs: Vec::new(),
        cgroups: Vec::new(),
    };
    let err = check_rambw_requirements(&config).unwrap_err();
    assert!(
        err.contains("sysmon_membw_gbps") || err.contains("resctrl"),
        "must name the missing prerequisite: {err}"
    );
}

/// Disabled rambw asks nothing of the host.
#[test]
fn no_rambw_no_requirements() {
    let config = SysmonConfig {
        cats: parse_categories("cpu,io,ram,storage").unwrap(),
        interval: Duration::from_secs(5),
        membw_peak_bytes_per_s: None,
        procs: Vec::new(),
        cgroups: Vec::new(),
    };
    assert!(check_rambw_requirements(&config).is_ok());
}

/// `any` stands alone; combined with names it is ambiguous and refused.
#[test]
fn any_parses_alone_and_refuses_combination() {
    assert_eq!(parse_selection("any").unwrap(), Selection::Any);
    assert_eq!(parse_selection("ANY").unwrap(), Selection::Any);
    assert!(parse_selection("any,cpu").is_err());
    assert!(matches!(parse_selection("all").unwrap(),
        Selection::Cats(c) if c == Categories::ALL));
}

/// On a host without resctrl, `any` yields everything but rambw and
/// SAYS SO; the skip line points at the full instructions. (This box
/// has no resctrl; a host that has it passes the gate instead.)
#[test]
fn any_downgrades_rambw_with_an_announced_reason() {
    let config = SysmonConfig {
        cats: Categories::ALL,
        interval: Duration::from_secs(5),
        membw_peak_bytes_per_s: None,
        procs: Vec::new(),
        cgroups: Vec::new(),
    };
    let (cats, skipped) = resolve_any(&config);
    if !platform_supported() {
        assert!(!cats.any() && skipped.len() == 1);
        return;
    }
    assert!(cats.cpu && cats.io && cats.ram && cats.storage && cats.net && cats.proc);
    if std::path::Path::new("/sys/fs/resctrl/mon_data").exists() {
        return; // host genuinely supports it; nothing to skip here
    }
    assert!(!cats.rambw, "unavailable rambw is disabled under `any`");
    assert!(
        skipped.iter().any(|s| s.contains("sysmon=rambw")),
        "the skip points at the full enable steps: {skipped:?}"
    );
    // Each skipped category has its line, and no other line is there.
    assert_eq!(
        skipped.len(),
        usize::from(!cats.rambw) + usize::from(!cats.pressure) + usize::from(!cats.cgroup)
    );
}

/// `sysmon_procs=` with the `proc` category off would name processes
/// nothing watches, and is refused.
#[test]
fn procs_without_the_proc_category_are_refused() {
    if !platform_supported() {
        return;
    }
    let config = SysmonConfig {
        cats: parse_categories("cpu").unwrap(),
        interval: Duration::from_secs(5),
        membw_peak_bytes_per_s: None,
        procs: process::parse_procs("server:1").unwrap(),
        cgroups: Vec::new(),
    };
    let err = check_requirements(&config).unwrap_err();
    assert!(err.contains("add `proc` to `sysmon=`"), "{err}");
}

/// `sysmon_cgroups=` with the `cgroup` category off is refused for
/// the same reason.
#[test]
fn cgroups_without_the_cgroup_category_are_refused() {
    if !platform_supported() {
        return;
    }
    let config = SysmonConfig {
        cats: parse_categories("cpu").unwrap(),
        interval: Duration::from_secs(5),
        membw_peak_bytes_per_s: None,
        procs: Vec::new(),
        cgroups: cgroup::parse_cgroups("node:system.slice").unwrap(),
    };
    let err = check_requirements(&config).unwrap_err();
    assert!(err.contains("add `cgroup` to `sysmon=`"), "{err}");
}

#[test]
fn enabled_categories_are_named_in_order() {
    assert_eq!(
        parse_categories("proc, cpu,net").unwrap().names(),
        "cpu,net,proc"
    );
    assert_eq!(Categories::ALL.names(), CATEGORY_NAMES.join(","));
}