#[cfg(test)]
mod tests {
use crate::core::types;
use indexmap::IndexMap;
fn machine(addr: &str, transport: Option<&str>) -> types::Machine {
types::Machine {
hostname: "h".to_string(),
addr: addr.to_string(),
user: "deploy".to_string(),
arch: "x86_64".to_string(),
ssh_key: None,
roles: vec![],
transport: transport.map(|s| s.to_string()),
container: None,
pepita: None,
cost: 0,
allowed_operators: vec![],
}
}
fn config_with(machines: Vec<(&str, types::Machine)>) -> types::ForjarConfig {
let mut map = IndexMap::new();
for (name, m) in machines {
map.insert(name.to_string(), m);
}
types::ForjarConfig {
machines: map,
..Default::default()
}
}
#[test]
fn preflight_ok_when_no_machines() {
let cfg = config_with(vec![]);
assert!(super::super::undo::preflight_ssh_check(&cfg, None).is_ok());
}
#[test]
fn preflight_skips_localhost() {
let cfg = config_with(vec![("web", machine("localhost", None))]);
assert!(super::super::undo::preflight_ssh_check(&cfg, None).is_ok());
}
#[test]
fn preflight_skips_loopback_ip() {
let cfg = config_with(vec![("web", machine("127.0.0.1", None))]);
assert!(super::super::undo::preflight_ssh_check(&cfg, None).is_ok());
}
#[test]
fn preflight_skips_local_transport() {
let cfg = config_with(vec![("web", machine("10.0.0.5", Some("local")))]);
assert!(super::super::undo::preflight_ssh_check(&cfg, None).is_ok());
}
#[test]
fn preflight_skips_container_transport() {
let cfg = config_with(vec![("ct", machine("10.0.0.6", Some("container")))]);
assert!(super::super::undo::preflight_ssh_check(&cfg, None).is_ok());
}
#[test]
fn preflight_errors_on_unreachable_remote() {
let cfg = config_with(vec![(
"remote",
machine("forjar-cov-preflight.invalid", None),
)]);
let result = super::super::undo::preflight_ssh_check(&cfg, None);
assert!(result.is_err(), "unreachable remote must Err: {result:?}");
let msg = result.unwrap_err();
assert!(msg.contains("pre-flight failed"), "msg: {msg}");
assert!(msg.contains("remote"), "should name the machine: {msg}");
}
#[test]
fn preflight_filter_skips_unreachable_remote() {
let cfg = config_with(vec![
("local", machine("localhost", None)),
("remote", machine("forjar-cov-preflight.invalid", None)),
]);
let result = super::super::undo::preflight_ssh_check(&cfg, Some("local"));
assert!(result.is_ok(), "filtered-out remote must not fail: {result:?}");
}
#[test]
fn preflight_filter_selects_unreachable_remote() {
let cfg = config_with(vec![
("local", machine("localhost", None)),
("remote", machine("forjar-cov-preflight.invalid", None)),
]);
let result = super::super::undo::preflight_ssh_check(&cfg, Some("remote"));
assert!(result.is_err(), "selected unreachable remote must Err: {result:?}");
}
}