Files
nano_metrics/agent/tests/payload_test.rs
T
Gilles Soulier 9e77d961f5 feat(agent): déconnexion propre sur SIGTERM/SIGINT
- Capture SIGTERM et SIGINT via libc::signal → AtomicBool RUNNING
- La boucle principale s'arrête proprement à la prochaine itération
- Envoi d'un paquet status:offline via UDP avant de quitter
- MQTT : publish status offline + disconnect() pour déconnexion gracieuse
  (le last_will reste actif pour les déconnexions brutales)
- payload.rs: #[serde(default)] sur version pour compatibilité descendante

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-22 22:34:55 +02:00

58 lines
2.0 KiB
Rust

use nanometrics_agent::payload::{AgentMetrics, SmartMetrics};
#[test]
fn test_serialize_json_complet() {
let m = AgentMetrics {
hostname: "srv-01".to_string(),
ip: "10.0.0.11".to_string(),
cpu_percent: Some(42.5),
memory_used: Some(3_000_000_000),
memory_free: Some(5_000_000_000),
memory_total: Some(8_000_000_000),
hdd_used: Some(60_000_000_000),
hdd_free: Some(140_000_000_000),
hdd_total: Some(200_000_000_000),
uptime: Some(1234567),
network_rx: Some(1024),
network_tx: Some(512),
temperature: None,
smart: None,
status: "online".to_string(),
version: "0.0.0".to_string(),
};
let json = serde_json::to_string(&m).unwrap();
assert!(json.contains("\"hostname\":\"srv-01\""));
assert!(json.contains("\"cpu_percent\":42.5"));
assert!(json.contains("\"status\":\"online\""));
assert!(json.contains("\"temperature\":null"));
}
#[test]
fn test_serialize_avec_smart() {
let m = AgentMetrics {
hostname: "srv-01".to_string(),
ip: "10.0.0.11".to_string(),
smart: Some(SmartMetrics {
passed: true,
temperature: Some(34),
reallocated_sectors: Some(0),
power_on_hours: Some(4213),
wear_level: Some(98),
}),
status: "online".to_string(),
..Default::default()
};
let json = serde_json::to_string(&m).unwrap();
assert!(json.contains("\"passed\":true"));
assert!(json.contains("\"temperature\":34"));
}
#[test]
fn test_deserialize_depuis_json() {
let json = r#"{"hostname":"host1","ip":"10.0.0.1","status":"online","cpu_percent":55.0,"memory_used":null,"memory_free":null,"memory_total":null,"hdd_used":null,"hdd_free":null,"hdd_total":null,"uptime":null,"network_rx":null,"network_tx":null,"temperature":null,"smart":null}"#;
let m: AgentMetrics = serde_json::from_str(json).unwrap();
assert_eq!(m.hostname, "host1");
assert_eq!(m.cpu_percent, Some(55.0));
assert!(m.smart.is_none());
}