use crate::statements;
use asterust::config::ConfigLine;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct HealthMonitor {
pub ifstate_clients: Option<String>,
pub threshold_level: Option<String>,
pub action: Option<String>,
}
pub(super) fn parse_health_monitor(monitor: &ConfigLine) -> HealthMonitor {
let first = |keyword: &str| statements(monitor, keyword).first().map(|s| s.to_string());
HealthMonitor {
ifstate_clients: first("ifstate-clients"),
threshold_level: first("threshold-level"),
action: first("action"),
}
}
#[cfg(test)]
mod tests {
use crate::system::system;
use asterust::config::juniper;
#[test]
fn extracts_the_health_monitor() {
let monitor = system(&juniper::parse_braces(
"system {\n health-monitor {\n ifstate-clients {\n peer-stuck;\n }\n threshold-level {\n medium;\n }\n action {\n alarm-with-cores;\n }\n }\n}\n",
))
.unwrap()
.health_monitor
.unwrap();
assert_eq!(monitor.ifstate_clients.as_deref(), Some("peer-stuck"));
assert_eq!(monitor.threshold_level.as_deref(), Some("medium"));
assert_eq!(monitor.action.as_deref(), Some("alarm-with-cores"));
}
#[test]
fn health_monitor_accepts_the_inline_form() {
let monitor = system(&juniper::parse_braces(
"system {\n health-monitor {\n action restart;\n }\n}\n",
))
.unwrap()
.health_monitor
.unwrap();
assert_eq!(monitor.action.as_deref(), Some("restart"));
assert_eq!(monitor.ifstate_clients, None);
}
#[test]
fn no_health_monitor_is_none() {
let system = system(&juniper::parse_braces("system {\n host-name pe1;\n}\n")).unwrap();
assert!(system.health_monitor.is_none());
}
}