| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- //! Hardware-free tests for the [`crate::docker::health`] collection
- //! model (verdict assembly, sweep parsing, summary building).
- use super::collect::{build_summary, net_check_script, parse_ps_output, verdict_for};
- use super::{HealthReport, NodeHealth, RecipesSummary};
- use crate::cluster_state::ClusterVerdict;
- use crate::config::{Cluster, Node};
- use crate::home_assistant::EntityStates;
- fn node(name: &str, online: bool, containers: &[&str]) -> NodeHealth {
- NodeHealth {
- name: name.into(),
- ib_ip: "10.0.0.1".into(),
- online,
- containers: containers.iter().map(|s| (*s).to_string()).collect(),
- net_up: true,
- net_details: vec![],
- }
- }
- fn test_cluster(name: &str, node_names: &[&str]) -> Cluster {
- Cluster {
- name: name.into(),
- master_ib_ip: None,
- nodes: node_names
- .iter()
- .enumerate()
- .map(|(i, n)| Node {
- name: (*n).to_string(),
- ssh_ip: format!("10.0.0.{i}"),
- ib_ip: None,
- rank: i as u32,
- host_key: None,
- entity: None,
- })
- .collect(),
- extra_entities: vec![],
- }
- }
- fn report(cluster: &str, switch_off: bool) -> HealthReport {
- HealthReport {
- cluster: cluster.into(),
- verdict: ClusterVerdict {
- up: true,
- switch_off,
- free: 0,
- },
- soft_off: vec![],
- nodes: vec![],
- tmux: vec![],
- }
- }
- #[test]
- fn verdict_all_online_counts_free() {
- let nodes = vec![node("a", true, &[]), node("b", true, &["c1"])];
- let v = verdict_for(&nodes, false);
- assert_eq!(
- v,
- ClusterVerdict {
- up: true,
- switch_off: false,
- free: 1
- }
- );
- }
- #[test]
- fn verdict_offline_node_downs_cluster() {
- let nodes = vec![node("a", true, &["c1"]), node("b", false, &[])];
- let v = verdict_for(&nodes, false);
- assert!(!v.up);
- assert_eq!(v.free, 0);
- }
- #[test]
- fn verdict_from_entity_states_switch_off_downs_cluster() {
- let nodes = vec![node("a", true, &[]), node("b", true, &["c1"])];
- let es = EntityStates {
- blocking_off: true,
- soft_off: vec!["switch.fan".into()],
- };
- let v = verdict_for(&nodes, es.blocking_off);
- assert!(!v.up && v.switch_off && v.free == 0);
- }
- #[test]
- fn parse_ps_output_splits_containers_and_net() {
- let out = "sm121_ds4_0\tUp 2 hours\timg:tag\n\
- \n\
- sudo: permission denied\n\
- docker: error line\n\
- ---NET---\n\
- socket:UP\n\
- ---END---\n";
- let (containers, net_up, details) = parse_ps_output(out);
- assert_eq!(containers, vec!["sm121_ds4_0\tUp 2 hours\timg:tag"]);
- assert!(net_up);
- assert_eq!(details, vec!["socket:UP"]);
- }
- #[test]
- fn parse_ps_output_down_missing_error_flip_net_up() {
- for tail in ["socket:DOWN", "socket:missing", "socket:error"] {
- let out = format!("---NET---\n{tail}\n---END---\n");
- assert!(!parse_ps_output(&out).1, "{tail} must flip net_up");
- }
- }
- #[test]
- fn net_check_script_validates_interface_name() {
- assert!(net_check_script(Some("eth0")).is_some());
- assert!(net_check_script(Some("enp2s0.100")).is_some());
- assert!(net_check_script(Some("ib-weird_1")).is_some());
- assert!(net_check_script(Some("bad;rm -rf")).is_none());
- assert!(net_check_script(None).is_none());
- }
- #[tokio::test]
- async fn build_summary_excludes_down_clusters_and_sorts_nodes() {
- let clusters = vec![
- test_cluster("up", &["n0", "n0b"]),
- test_cluster("down", &["n1"]),
- ];
- // Sweep: both "up" nodes reachable with ds4|up; "down" node unreachable.
- let sweep: Vec<tokio::task::JoinHandle<(String, anyhow::Result<String>)>> = vec![
- tokio::spawn(async { ("n0".into(), Ok("ds4|up".into())) }),
- tokio::spawn(async { ("n0b".into(), Ok("ds4|up".into())) }),
- tokio::spawn(async { ("n1".into(), Err(anyhow::anyhow!("ssh down"))) }),
- ];
- let reports = vec![report("up", false), report("down", false)];
- let summary: RecipesSummary = build_summary(&clusters, &reports, sweep).await;
- assert_eq!(
- summary.by_recipe["ds4"]["up"],
- vec!["n0".to_string(), "n0b".to_string()]
- );
- assert!(!summary.by_recipe["ds4"].contains_key("down"));
- }
- /// Renderer tests for [`super::render`] — plain, markdown, and HTML —
- /// reusing the collection-model fixtures above.
- mod render {
- use std::collections::HashMap;
- use super::super::FleetHealth;
- use super::super::render::render_fleet;
- use super::{HealthReport, RecipesSummary, node, report};
- use crate::cli::OutputFormat;
- use crate::tmux::discover::TmuxHealthEntry;
- fn fleet(clusters: Vec<HealthReport>, recipes: Option<RecipesSummary>) -> FleetHealth {
- FleetHealth { clusters, recipes }
- }
- fn container(name: &str, status: &str, image: &str) -> String {
- format!("{name}\t{status}\t{image}")
- }
- fn up_report() -> HealthReport {
- let mut r = report("c1", false);
- r.nodes = vec![node(
- "n0",
- true,
- &[&container("app", "Up 2 hours", "img:tag")],
- )];
- r
- }
- fn down_report() -> HealthReport {
- let mut r = report("down", false);
- r.verdict.up = false;
- r.verdict.free = 0;
- r.nodes = vec![
- node("alive", true, &[&container("secret", "Up", "img")]),
- node("dead", false, &[]),
- ];
- r
- }
- fn tmux_entry(recipe: &str, node_name: &str, session: bool, running: bool) -> TmuxHealthEntry {
- TmuxHealthEntry {
- recipe_display: recipe.into(),
- node_name: node_name.into(),
- session_exists: session,
- process_running: running,
- api_ok: true,
- }
- }
- fn tmux_report(entries: Vec<TmuxHealthEntry>) -> HealthReport {
- let mut r = report("c1", false);
- r.tmux = entries;
- r
- }
- #[test]
- fn render_fleet_handles_each_format() {
- let f = fleet(vec![up_report()], None);
- let plain = render_fleet(&f, OutputFormat::Plain);
- let md = render_fleet(&f, OutputFormat::Md);
- let html = render_fleet(&f, OutputFormat::Html);
- assert!(plain.contains("=== c1 ==="));
- // Container columns parsed out of the tab-separated lines.
- assert!(plain.contains("app Up 2 hours img:tag"));
- assert!(!plain.contains('\t'), "plain must not emit raw tab blobs");
- assert!(md.contains("## c1"));
- assert!(html.contains("<h2>c1</h2>"));
- assert_ne!(plain, md);
- }
- #[test]
- fn html_escapes_hostile_names() {
- let mut r = report("a<b>&\"c'", false);
- r.nodes = vec![node(
- "<script>",
- true,
- &[&container("evil&img", "Up", "<x>")],
- )];
- let out = render_fleet(&fleet(vec![r], None), OutputFormat::Html);
- assert!(
- out.contains("<script>"),
- "node name must be escaped: {out}"
- );
- assert!(
- out.contains("evil&img"),
- "container name must be escaped: {out}"
- );
- assert!(!out.contains("<script>"));
- assert!(!out.contains("evil&img"));
- }
- #[test]
- fn md_renders_headings_and_tables() {
- let out = render_fleet(&fleet(vec![up_report()], None), OutputFormat::Md);
- assert!(out.contains("## c1"));
- assert!(out.contains("| Node | Address | Status | Containers |"));
- assert!(out.contains("| --- | --- | --- | --- |"));
- assert!(out.contains("| app | n0 | Up 2 hours | img:tag |"));
- }
- #[test]
- fn md_neutralizes_pipes_in_cells() {
- let mut r = report("c1", false);
- r.nodes = vec![node("n0", true, &[&container("bad|name", "Up", "img")])];
- let out = render_fleet(&fleet(vec![r], None), OutputFormat::Md);
- assert!(out.contains("bad\\|name"), "pipe must be escaped: {out}");
- assert!(
- out.contains("| bad\\|name | n0 | Up | img |"),
- "table structure must stay intact: {out}"
- );
- }
- #[test]
- fn down_cluster_suppresses_container_data() {
- for fmt in [OutputFormat::Plain, OutputFormat::Md, OutputFormat::Html] {
- let out = render_fleet(&fleet(vec![down_report()], None), fmt);
- assert!(
- !out.contains("secret"),
- "{fmt:?} must suppress containers: {out}"
- );
- assert!(
- out.contains("offline"),
- "{fmt:?} must show the offline node: {out}"
- );
- }
- }
- #[test]
- fn recipes_none_renders_no_recipes_section() {
- for fmt in [OutputFormat::Plain, OutputFormat::Md, OutputFormat::Html] {
- let out = render_fleet(&fleet(vec![up_report()], None), fmt);
- assert!(!out.contains("Running recipes:"), "{fmt:?}");
- }
- }
- #[test]
- fn tmux_all_stopped_shows_summary_line() {
- for fmt in [OutputFormat::Plain, OutputFormat::Md, OutputFormat::Html] {
- let r = tmux_report(vec![tmux_entry("rec", "dgx1", false, false)]);
- let out = render_fleet(&fleet(vec![r], Some(RecipesSummary::default())), fmt);
- assert!(out.contains("all stopped"), "{fmt:?}: {out}");
- }
- }
- #[test]
- fn tmux_running_entry_rendered_stopped_skipped() {
- for fmt in [OutputFormat::Plain, OutputFormat::Md, OutputFormat::Html] {
- let r = tmux_report(vec![
- tmux_entry("rec", "dgx1", true, true),
- tmux_entry("rec", "dgx2", false, false),
- ]);
- let out = render_fleet(&fleet(vec![r], None), fmt);
- assert!(out.contains("rec \u{2014} dgx1"), "{fmt:?}: {out}");
- assert!(!out.contains("dgx2"), "{fmt:?} must skip stopped: {out}");
- }
- }
- #[test]
- fn recipes_summary_is_sorted_and_deterministic() {
- let build = || {
- let mut summary = RecipesSummary::default();
- let mut z = HashMap::new();
- z.insert("zcluster".to_string(), vec!["n1".to_string()]);
- summary.by_recipe.insert("zebra".to_string(), z);
- let mut a = HashMap::new();
- a.insert("c".to_string(), vec!["n2".to_string()]);
- summary.by_recipe.insert("alpha".to_string(), a);
- FleetHealth {
- clusters: vec![],
- recipes: Some(summary),
- }
- };
- let first = render_fleet(&build(), OutputFormat::Plain);
- let second = render_fleet(&build(), OutputFormat::Plain);
- assert_eq!(first, second, "rendering must be deterministic");
- let alpha = first.find("alpha").expect("alpha rendered");
- let zebra = first.find("zebra").expect("zebra rendered");
- assert!(alpha < zebra, "recipes must be sorted");
- }
- /// The improved plain layout: headers + verdict tails, one line per
- /// node, report-globally aligned container columns, down-cluster
- /// suppression, tmux collapse, sorted recipes.
- #[test]
- fn plain_layout_is_aligned_and_suppressed() {
- let mut up = report("up1", false);
- let mut n2 = node("n2", true, &[]);
- n2.net_up = false;
- n2.net_details = vec!["socket:DOWN".to_string()];
- up.nodes = vec![
- node(
- "n0",
- true,
- &[
- &container("web-a", "Up 5 minutes", "img/one:1"),
- &container("web-b-long", "Exited (0) 3 hours ago", "img/two:22"),
- ],
- ),
- node(
- "n1",
- true,
- &[&container("web-c", "Up 2 hours", "img/three:3")],
- ),
- n2,
- ];
- up.tmux = vec![
- tmux_entry("rec", "dgx1", true, true),
- tmux_entry("rec", "dgx2", false, false),
- ];
- let mut down = down_report();
- let mut alive = node("alive", true, &[&container("hidden", "Up", "img")]);
- alive.net_up = false;
- alive.net_details = vec!["socket:DOWN".to_string()];
- down.nodes = vec![alive, node("dead", false, &[])];
- down.tmux = vec![tmux_entry("stopped_rec", "dgx3", false, false)];
- let mut summary = RecipesSummary::default();
- summary
- .by_recipe
- .entry("zebra".to_string())
- .or_default()
- .insert("zc".to_string(), vec!["n9".to_string()]);
- summary
- .by_recipe
- .entry("alpha".to_string())
- .or_default()
- .insert("ac".to_string(), vec!["n8".to_string()]);
- let out = render_fleet(&fleet(vec![up, down], Some(summary)), OutputFormat::Plain);
- // One header + verdict tail per cluster.
- assert!(out.contains("=== up1 ==="), "{out}");
- assert!(out.contains("=== down ==="), "{out}");
- assert!(out.contains("cluster offline"), "down verdict tail: {out}");
- assert!(
- out.contains("interface down"),
- "net-down tail on the up cluster: {out}"
- );
- // One line per node; offline nodes get an explicit offline
- // line; node/container rows are indented under the header.
- assert!(out.contains("dead (10.0.0.1)"), "{out}");
- assert!(out.contains("— offline"), "{out}");
- assert!(
- out.lines().any(|l| l.starts_with(" ● ")),
- "node indent: {out}"
- );
- assert!(
- out.lines().any(|l| l.starts_with(" ✗ dead")),
- "offline node indent: {out}"
- );
- assert!(
- out.lines().any(|l| l.starts_with(" web-a ")),
- "container indent: {out}"
- );
- // Net details whenever the interface is down — on the up
- // cluster (n2) and on the down cluster (alive).
- assert_eq!(
- out.matches("socket:DOWN").count(),
- 2,
- "net details must show on down nodes in both clusters: {out}"
- );
- // Down cluster suppresses container data entirely.
- assert!(!out.contains("hidden"), "{out}");
- // Container rows from different nodes align on report-global
- // widths: the image column starts at the same offset everywhere.
- let image_col = |needle: &str| {
- out.lines()
- .find(|l| l.contains(needle))
- .unwrap_or_else(|| panic!("{needle} missing: {out}"))
- .find(needle)
- .unwrap()
- };
- assert_eq!(
- image_col("img/one:1"),
- image_col("img/three:3"),
- "container rows must align across nodes: {out}"
- );
- assert_eq!(image_col("img/one:1"), image_col("img/two:22"), "{out}");
- assert!(!out.contains('\t'), "plain output must have no tabs: {out}");
- // tmux: active entries render, stopped ones are skipped; an
- // all-stopped cluster collapses to one count line.
- assert!(out.contains("rec — dgx1"), "{out}");
- assert!(!out.contains("dgx2"), "{out}");
- assert!(out.contains("all stopped"), "{out}");
- // Recipes summary sorted by recipe.
- let alpha = out.find("alpha").expect("alpha rendered");
- let zebra = out.find("zebra").expect("zebra rendered");
- assert!(alpha < zebra, "recipes sorted: {out}");
- // Single-cluster runs omit the recipes section.
- let solo = render_fleet(
- &fleet(vec![report("solo", false)], None),
- OutputFormat::Plain,
- );
- assert!(
- !solo.contains("Running recipes:"),
- "single-cluster run omits recipes: {solo}"
- );
- }
- #[tokio::test]
- async fn renders_in_active_locale() {
- crate::locale::set_global_locale(Some("ru-RU".to_string()));
- let out = crate::locale::spawn_with_locale(async {
- render_fleet(
- &fleet(vec![], Some(RecipesSummary::default())),
- OutputFormat::Plain,
- )
- })
- .await
- .unwrap();
- assert!(
- out.contains("Нет запущенных"),
- "expected Russian no-recipes line: {out}"
- );
- }
- }
|