use super::{command_mechanism, ActionSpec};
use sysknife_types::RiskLevel;
const REBOOT_REQUIRED_FILE: &str = "/var/run/reboot-required";
const REBOOT_REQUIRED_PKGS_FILE: &str = "/var/run/reboot-required-pkgs";
pub fn specs() -> Vec<ActionSpec> {
vec![check_pending_reboot()]
}
pub fn check_pending_reboot() -> ActionSpec {
let script = format!(
"if test -f {sentinel}; then cat {sentinel}; cat {pkgs} 2>/dev/null; else echo 'No reboot required.'; fi",
sentinel = REBOOT_REQUIRED_FILE,
pkgs = REBOOT_REQUIRED_PKGS_FILE,
);
ActionSpec {
action_name: "CheckPendingReboot",
mechanism: command_mechanism("bash", ["-c", &script]),
risk_level: RiskLevel::Low,
reboot_required: false,
rollback_available: false,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::actions::ActionMechanism;
fn extract_cmd(spec: &ActionSpec) -> (&'static str, Vec<String>) {
match &spec.mechanism {
ActionMechanism::Command { program, args } => (*program, args.clone()),
_ => panic!("expected Command mechanism"),
}
}
#[test]
fn check_pending_reboot_action_name() {
assert_eq!(check_pending_reboot().action_name, "CheckPendingReboot");
}
#[test]
fn check_pending_reboot_uses_bash() {
let spec = check_pending_reboot();
let (prog, _) = extract_cmd(&spec);
assert_eq!(prog, "bash");
}
#[test]
fn check_pending_reboot_script_references_sentinel_file() {
let spec = check_pending_reboot();
let (_, args) = extract_cmd(&spec);
let joined = args.join(" ");
assert!(
joined.contains(REBOOT_REQUIRED_FILE),
"missing sentinel path in script: {joined}"
);
}
#[test]
fn check_pending_reboot_script_references_pkgs_file() {
let spec = check_pending_reboot();
let (_, args) = extract_cmd(&spec);
let joined = args.join(" ");
assert!(
joined.contains(REBOOT_REQUIRED_PKGS_FILE),
"missing pkgs path in script: {joined}"
);
}
#[test]
fn check_pending_reboot_risk_is_low() {
assert_eq!(check_pending_reboot().risk_level, RiskLevel::Low);
}
#[test]
fn check_pending_reboot_no_reboot_no_rollback() {
let spec = check_pending_reboot();
assert!(!spec.reboot_required);
assert!(!spec.rollback_available);
}
#[test]
fn specs_covers_check_pending_reboot() {
let spec_names: Vec<&str> = specs().iter().map(|s| s.action_name).collect();
assert!(
spec_names.contains(&"CheckPendingReboot"),
"specs() missing CheckPendingReboot"
);
}
}