added test section
This commit is contained in:
@@ -70,6 +70,7 @@ struct HeartbeatPayload {
|
||||
ip_address: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
struct HardwareInfo {
|
||||
cpu_type: String,
|
||||
cpu_cores: i32,
|
||||
@@ -683,9 +684,131 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_metrics() {
|
||||
println!("CPU Temp: {:?}", get_cpu_temp());
|
||||
println!("Disk Info: {:?}", get_disk_info());
|
||||
println!("Network: {:?}", get_network_traffic());
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
// Test CPU temperature collection
|
||||
#[test]
|
||||
fn test_cpu_temp() {
|
||||
let temp = get_cpu_temp();
|
||||
println!("CPU Temperature: {:?}°C", temp);
|
||||
|
||||
// Basic validation - temp should be between 0-100°C if available
|
||||
if let Some(t) = temp {
|
||||
assert!(
|
||||
t >= 0.0 && t <= 100.0,
|
||||
"CPU temperature out of reasonable range"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Test disk information collection
|
||||
#[test]
|
||||
fn test_disk_info() {
|
||||
let (size, usage, _temp) = get_disk_info();
|
||||
println!("Disk Size: {:.2}GB, Usage: {:.2}%", size, usage);
|
||||
|
||||
assert!(size >= 0.0, "Disk size should be non-negative");
|
||||
assert!(
|
||||
usage >= 0.0 && usage <= 100.0,
|
||||
"Disk usage should be 0-100%"
|
||||
);
|
||||
}
|
||||
|
||||
// Test network traffic collection
|
||||
#[test]
|
||||
fn test_network_traffic() {
|
||||
let traffic = get_network_traffic();
|
||||
println!("Network Traffic: {:?}", traffic);
|
||||
|
||||
if let Some((rx, tx)) = traffic {
|
||||
assert!(rx >= 0, "RX bytes should be non-negative");
|
||||
assert!(tx >= 0, "TX bytes should be non-negative");
|
||||
}
|
||||
}
|
||||
|
||||
// Test hardware info collection
|
||||
#[tokio::test]
|
||||
async fn test_hardware_info() {
|
||||
let hardware = HardwareInfo::collect().await.unwrap();
|
||||
println!("Hardware Info: {:?}", hardware);
|
||||
|
||||
assert!(
|
||||
!hardware.cpu_type.is_empty(),
|
||||
"CPU type should not be empty"
|
||||
);
|
||||
assert!(hardware.cpu_cores > 0, "CPU cores should be positive");
|
||||
assert!(
|
||||
!hardware.gpu_type.is_empty(),
|
||||
"GPU type should not be empty"
|
||||
);
|
||||
assert!(hardware.ram_size > 0.0, "RAM size should be positive");
|
||||
assert!(
|
||||
!hardware.ip_address.is_empty(),
|
||||
"IP address should not be empty"
|
||||
);
|
||||
}
|
||||
|
||||
// Test metrics collector
|
||||
#[tokio::test]
|
||||
async fn test_metrics_collector() {
|
||||
let mut collector = MetricsCollector::new(1, "127.0.0.1".to_string());
|
||||
let metrics = collector.collect_metrics();
|
||||
println!("Collected Metrics: {:?}", metrics);
|
||||
|
||||
// Validate basic metrics ranges
|
||||
assert!(
|
||||
metrics.cpu_load >= 0.0 && metrics.cpu_load <= 100.0,
|
||||
"CPU load should be 0-100%"
|
||||
);
|
||||
assert!(
|
||||
metrics.ram_load >= 0.0 && metrics.ram_load <= 100.0,
|
||||
"RAM load should be 0-100%"
|
||||
);
|
||||
assert!(metrics.ram_size > 0.0, "RAM size should be positive");
|
||||
}
|
||||
|
||||
// Test registration flow (mock server needed for full test)
|
||||
#[tokio::test]
|
||||
async fn test_registration_flow() {
|
||||
// Note: This would require a mock server for proper testing
|
||||
// Currently just testing the hardware detection part
|
||||
let hardware = HardwareInfo::collect().await.unwrap();
|
||||
let registration = RegistrationDto {
|
||||
id: 1,
|
||||
ip_address: hardware.ip_address.clone(),
|
||||
cpu_type: hardware.cpu_type,
|
||||
cpu_cores: hardware.cpu_cores,
|
||||
gpu_type: hardware.gpu_type,
|
||||
ram_size: hardware.ram_size,
|
||||
};
|
||||
|
||||
println!("Registration DTO: {:?}", registration);
|
||||
assert_eq!(registration.id, 1);
|
||||
assert!(!registration.ip_address.is_empty());
|
||||
}
|
||||
|
||||
// Test error cases
|
||||
#[test]
|
||||
fn test_error_handling() {
|
||||
// Test with invalid paths
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
let temp = get_cpu_temp_with_path("/invalid/path");
|
||||
assert!(temp.is_none(), "Should handle invalid paths gracefully");
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function for testing with custom paths
|
||||
#[cfg(target_os = "linux")]
|
||||
fn get_cpu_temp_with_path(path: &str) -> Option<f32> {
|
||||
fs::read_to_string(path)
|
||||
.ok()?
|
||||
.trim()
|
||||
.parse::<f32>()
|
||||
.map(|t| t / 1000.0)
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user