#[cfg(test)]
use crate::device::cpu_linux::LinuxCpuReader;
#[cfg(test)]
use crate::device::{CoreType, CpuPlatformType};
#[test]
fn test_parse_cpuinfo_intel() {
let cpuinfo_content = r#"processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 85
model name : Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
stepping : 10
cpu MHz : 3700.000
cache size : 12288 KB
physical id : 0
siblings : 12
core id : 0
cpu cores : 6
processor : 1
physical id : 0
core id : 1
processor : 11
physical id : 0
core id : 5"#;
let reader = LinuxCpuReader::new();
let result = reader.parse_cpuinfo(cpuinfo_content);
assert!(result.is_ok());
let (cpu_model, _arch, platform, sockets, _cores, threads, base_freq, _max_freq, cache) =
result.unwrap();
assert_eq!(cpu_model, "Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz");
assert_eq!(platform, CpuPlatformType::Intel);
assert_eq!(sockets, 1);
assert_eq!(threads, 3); assert!(base_freq > 0);
assert_eq!(cache, 12); }
#[test]
fn test_parse_cpuinfo_amd() {
let cpuinfo_content = r#"processor : 0
vendor_id : AuthenticAMD
model name : AMD Ryzen 9 5900X 12-Core Processor
cpu MHz : 3700.000
cache size : 512 KB
physical id : 0"#;
let reader = LinuxCpuReader::new();
let result = reader.parse_cpuinfo(cpuinfo_content);
assert!(result.is_ok());
let (cpu_model, _, platform, _, _, _, _, _, _) = result.unwrap();
assert_eq!(cpu_model, "AMD Ryzen 9 5900X 12-Core Processor");
assert_eq!(platform, CpuPlatformType::Amd);
}
#[test]
fn test_parse_cpu_stat() {
let stat_content = r#"cpu 10000 0 20000 70000 0 0 0 0 0 0
cpu0 2500 0 5000 17500 0 0 0 0 0 0
cpu1 2500 0 5000 17500 0 0 0 0 0 0
cpu2 2500 0 5000 17500 0 0 0 0 0 0
cpu3 2500 0 5000 17500 0 0 0 0 0 0"#;
let reader = LinuxCpuReader::new();
let result = reader.parse_cpu_stat(stat_content, 1);
assert!(result.is_ok());
let (overall_util, socket_info, core_utils) = result.unwrap();
assert!((0.0..=100.0).contains(&overall_util));
assert_eq!(socket_info.len(), 1);
assert!(!core_utils.is_empty());
for core in &core_utils {
assert!((0.0..=100.0).contains(&core.utilization));
assert_eq!(core.core_type, CoreType::Standard);
}
}
#[test]
fn test_container_aware_parsing() {
let reader = LinuxCpuReader::new();
if reader.container_info.is_container {
assert!(reader.container_info.effective_cpu_count > 0.0);
println!(
"Running in container with {} effective CPUs",
reader.container_info.effective_cpu_count
);
} else {
assert!(reader.container_info.effective_cpu_count > 0.0);
println!(
"Running on host with {} CPUs",
reader.container_info.effective_cpu_count
);
}
}
#[test]
fn test_parse_cpuinfo_with_container_limits() {
let cpuinfo_content = r#"processor : 0
model name : Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
cpu MHz : 3700.000
processor : 1
processor : 2
processor : 3"#;
let reader = LinuxCpuReader::new();
let result = reader.parse_cpuinfo(cpuinfo_content);
assert!(result.is_ok());
}
#[test]
fn test_get_cache_size_from_lscpu() {
let reader = LinuxCpuReader::new();
let cache_size = reader.get_cache_size_from_lscpu();
match cache_size {
Some(size) => println!("Found cache size: {size} MB"),
None => println!("No cache size found (lscpu not available or failed)"),
}
}