#[cfg(test)]
use crate::device::MemoryReader;
#[cfg(test)]
use crate::device::memory_linux::LinuxMemoryReader;
#[test]
fn test_memory_reader_creation() {
let reader = LinuxMemoryReader::new();
if let Some(container_info) = reader.container_info {
if container_info.is_container {
println!("Created memory reader in container environment");
} else {
println!("Created memory reader in non-container environment");
}
} else {
println!("Created memory reader with no container info");
}
}
#[test]
fn test_memory_info_retrieval() {
let reader = LinuxMemoryReader::new();
let memory_infos = reader.get_memory_info();
assert!(!memory_infos.is_empty());
let memory_info = &memory_infos[0];
assert!(memory_info.total_bytes > 0);
assert!(memory_info.utilization >= 0.0 && memory_info.utilization <= 100.0);
if let Some(container_info) = reader.container_info {
if container_info.is_container {
println!("Container memory:");
println!(" Total: {} MB", memory_info.total_bytes / 1024 / 1024);
println!(" Used: {} MB", memory_info.used_bytes / 1024 / 1024);
println!(" Utilization: {:.2}%", memory_info.utilization);
if let Some(limit) = container_info.memory_limit_bytes {
assert_eq!(memory_info.total_bytes, limit);
}
} else {
println!("System memory:");
println!(" Total: {} MB", memory_info.total_bytes / 1024 / 1024);
println!(" Used: {} MB", memory_info.used_bytes / 1024 / 1024);
println!(
" Available: {} MB",
memory_info.available_bytes / 1024 / 1024
);
println!(" Cached: {} MB", memory_info.cached_bytes / 1024 / 1024);
println!(" Utilization: {:.2}%", memory_info.utilization);
}
} else {
println!("No container info available");
}
}
#[test]
fn test_container_memory_detection() {
let reader = LinuxMemoryReader::new();
let memory_infos = reader.get_memory_info();
assert!(!memory_infos.is_empty());
let memory_info = &memory_infos[0];
if let Some(container_info) = reader.container_info {
if container_info.is_container {
println!("Container memory detected:");
if let Some(limit) = container_info.memory_limit_bytes {
println!(" Memory limit: {} MB", limit / 1024 / 1024);
assert_eq!(memory_info.total_bytes, limit);
}
} else {
println!("Host memory detected:");
println!(" Total: {} MB", memory_info.total_bytes / 1024 / 1024);
}
} else {
println!("No container info available");
}
}