tests.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. //! Hardware-free tests for the [`crate::docker::health`] collection
  2. //! model (verdict assembly, sweep parsing, summary building).
  3. use super::collect::{build_summary, net_check_script, parse_ps_output, verdict_for};
  4. use super::{HealthReport, NodeHealth, RecipesSummary};
  5. use crate::cluster_state::ClusterVerdict;
  6. use crate::config::{Cluster, Node};
  7. use crate::home_assistant::EntityStates;
  8. fn node(name: &str, online: bool, containers: &[&str]) -> NodeHealth {
  9. NodeHealth {
  10. name: name.into(),
  11. ib_ip: "10.0.0.1".into(),
  12. online,
  13. containers: containers.iter().map(|s| (*s).to_string()).collect(),
  14. net_up: true,
  15. net_details: vec![],
  16. }
  17. }
  18. fn test_cluster(name: &str, node_names: &[&str]) -> Cluster {
  19. Cluster {
  20. name: name.into(),
  21. master_ib_ip: None,
  22. nodes: node_names
  23. .iter()
  24. .enumerate()
  25. .map(|(i, n)| Node {
  26. name: (*n).to_string(),
  27. ssh_ip: format!("10.0.0.{i}"),
  28. ib_ip: None,
  29. rank: i as u32,
  30. host_key: None,
  31. entity: None,
  32. })
  33. .collect(),
  34. extra_entities: vec![],
  35. }
  36. }
  37. fn report(cluster: &str, switch_off: bool) -> HealthReport {
  38. HealthReport {
  39. cluster: cluster.into(),
  40. verdict: ClusterVerdict {
  41. up: true,
  42. switch_off,
  43. free: 0,
  44. },
  45. soft_off: vec![],
  46. nodes: vec![],
  47. tmux: vec![],
  48. }
  49. }
  50. #[test]
  51. fn verdict_all_online_counts_free() {
  52. let nodes = vec![node("a", true, &[]), node("b", true, &["c1"])];
  53. let v = verdict_for(&nodes, false);
  54. assert_eq!(
  55. v,
  56. ClusterVerdict {
  57. up: true,
  58. switch_off: false,
  59. free: 1
  60. }
  61. );
  62. }
  63. #[test]
  64. fn verdict_offline_node_downs_cluster() {
  65. let nodes = vec![node("a", true, &["c1"]), node("b", false, &[])];
  66. let v = verdict_for(&nodes, false);
  67. assert!(!v.up);
  68. assert_eq!(v.free, 0);
  69. }
  70. #[test]
  71. fn verdict_from_entity_states_switch_off_downs_cluster() {
  72. let nodes = vec![node("a", true, &[]), node("b", true, &["c1"])];
  73. let es = EntityStates {
  74. blocking_off: true,
  75. soft_off: vec!["switch.fan".into()],
  76. };
  77. let v = verdict_for(&nodes, es.blocking_off);
  78. assert!(!v.up && v.switch_off && v.free == 0);
  79. }
  80. #[test]
  81. fn parse_ps_output_splits_containers_and_net() {
  82. let out = "sm121_ds4_0\tUp 2 hours\timg:tag\n\
  83. \n\
  84. sudo: permission denied\n\
  85. docker: error line\n\
  86. ---NET---\n\
  87. socket:UP\n\
  88. ---END---\n";
  89. let (containers, net_up, details) = parse_ps_output(out);
  90. assert_eq!(containers, vec!["sm121_ds4_0\tUp 2 hours\timg:tag"]);
  91. assert!(net_up);
  92. assert_eq!(details, vec!["socket:UP"]);
  93. }
  94. #[test]
  95. fn parse_ps_output_down_missing_error_flip_net_up() {
  96. for tail in ["socket:DOWN", "socket:missing", "socket:error"] {
  97. let out = format!("---NET---\n{tail}\n---END---\n");
  98. assert!(!parse_ps_output(&out).1, "{tail} must flip net_up");
  99. }
  100. }
  101. #[test]
  102. fn net_check_script_validates_interface_name() {
  103. assert!(net_check_script(Some("eth0")).is_some());
  104. assert!(net_check_script(Some("enp2s0.100")).is_some());
  105. assert!(net_check_script(Some("ib-weird_1")).is_some());
  106. assert!(net_check_script(Some("bad;rm -rf")).is_none());
  107. assert!(net_check_script(None).is_none());
  108. }
  109. #[tokio::test]
  110. async fn build_summary_excludes_down_clusters_and_sorts_nodes() {
  111. let clusters = vec![
  112. test_cluster("up", &["n0", "n0b"]),
  113. test_cluster("down", &["n1"]),
  114. ];
  115. // Sweep: both "up" nodes reachable with ds4|up; "down" node unreachable.
  116. let sweep: Vec<tokio::task::JoinHandle<(String, anyhow::Result<String>)>> = vec![
  117. tokio::spawn(async { ("n0".into(), Ok("ds4|up".into())) }),
  118. tokio::spawn(async { ("n0b".into(), Ok("ds4|up".into())) }),
  119. tokio::spawn(async { ("n1".into(), Err(anyhow::anyhow!("ssh down"))) }),
  120. ];
  121. let reports = vec![report("up", false), report("down", false)];
  122. let summary: RecipesSummary = build_summary(&clusters, &reports, sweep).await;
  123. assert_eq!(
  124. summary.by_recipe["ds4"]["up"],
  125. vec!["n0".to_string(), "n0b".to_string()]
  126. );
  127. assert!(!summary.by_recipe["ds4"].contains_key("down"));
  128. }
  129. /// Renderer tests for [`super::render`] — plain, markdown, and HTML —
  130. /// reusing the collection-model fixtures above.
  131. mod render {
  132. use std::collections::HashMap;
  133. use super::super::FleetHealth;
  134. use super::super::render::render_fleet;
  135. use super::{HealthReport, RecipesSummary, node, report};
  136. use crate::cli::OutputFormat;
  137. use crate::tmux::discover::TmuxHealthEntry;
  138. fn fleet(clusters: Vec<HealthReport>, recipes: Option<RecipesSummary>) -> FleetHealth {
  139. FleetHealth { clusters, recipes }
  140. }
  141. fn container(name: &str, status: &str, image: &str) -> String {
  142. format!("{name}\t{status}\t{image}")
  143. }
  144. fn up_report() -> HealthReport {
  145. let mut r = report("c1", false);
  146. r.nodes = vec![node(
  147. "n0",
  148. true,
  149. &[&container("app", "Up 2 hours", "img:tag")],
  150. )];
  151. r
  152. }
  153. fn down_report() -> HealthReport {
  154. let mut r = report("down", false);
  155. r.verdict.up = false;
  156. r.verdict.free = 0;
  157. r.nodes = vec![
  158. node("alive", true, &[&container("secret", "Up", "img")]),
  159. node("dead", false, &[]),
  160. ];
  161. r
  162. }
  163. fn tmux_entry(recipe: &str, node_name: &str, session: bool, running: bool) -> TmuxHealthEntry {
  164. TmuxHealthEntry {
  165. recipe_display: recipe.into(),
  166. node_name: node_name.into(),
  167. session_exists: session,
  168. process_running: running,
  169. api_ok: true,
  170. }
  171. }
  172. fn tmux_report(entries: Vec<TmuxHealthEntry>) -> HealthReport {
  173. let mut r = report("c1", false);
  174. r.tmux = entries;
  175. r
  176. }
  177. #[test]
  178. fn render_fleet_handles_each_format() {
  179. let f = fleet(vec![up_report()], None);
  180. let plain = render_fleet(&f, OutputFormat::Plain);
  181. let md = render_fleet(&f, OutputFormat::Md);
  182. let html = render_fleet(&f, OutputFormat::Html);
  183. assert!(plain.contains("=== c1 ==="));
  184. // Container columns parsed out of the tab-separated lines.
  185. assert!(plain.contains("app Up 2 hours img:tag"));
  186. assert!(!plain.contains('\t'), "plain must not emit raw tab blobs");
  187. assert!(md.contains("## c1"));
  188. assert!(html.contains("<h2>c1</h2>"));
  189. assert_ne!(plain, md);
  190. }
  191. #[test]
  192. fn html_escapes_hostile_names() {
  193. let mut r = report("a<b>&\"c'", false);
  194. r.nodes = vec![node(
  195. "<script>",
  196. true,
  197. &[&container("evil&img", "Up", "<x>")],
  198. )];
  199. let out = render_fleet(&fleet(vec![r], None), OutputFormat::Html);
  200. assert!(
  201. out.contains("&lt;script&gt;"),
  202. "node name must be escaped: {out}"
  203. );
  204. assert!(
  205. out.contains("evil&amp;img"),
  206. "container name must be escaped: {out}"
  207. );
  208. assert!(!out.contains("<script>"));
  209. assert!(!out.contains("evil&img"));
  210. }
  211. #[test]
  212. fn md_renders_headings_and_tables() {
  213. let out = render_fleet(&fleet(vec![up_report()], None), OutputFormat::Md);
  214. assert!(out.contains("## c1"));
  215. assert!(out.contains("| Node | Address | Status | Containers |"));
  216. assert!(out.contains("| --- | --- | --- | --- |"));
  217. assert!(out.contains("| app | n0 | Up 2 hours | img:tag |"));
  218. }
  219. #[test]
  220. fn md_neutralizes_pipes_in_cells() {
  221. let mut r = report("c1", false);
  222. r.nodes = vec![node("n0", true, &[&container("bad|name", "Up", "img")])];
  223. let out = render_fleet(&fleet(vec![r], None), OutputFormat::Md);
  224. assert!(out.contains("bad\\|name"), "pipe must be escaped: {out}");
  225. assert!(
  226. out.contains("| bad\\|name | n0 | Up | img |"),
  227. "table structure must stay intact: {out}"
  228. );
  229. }
  230. #[test]
  231. fn down_cluster_suppresses_container_data() {
  232. for fmt in [OutputFormat::Plain, OutputFormat::Md, OutputFormat::Html] {
  233. let out = render_fleet(&fleet(vec![down_report()], None), fmt);
  234. assert!(
  235. !out.contains("secret"),
  236. "{fmt:?} must suppress containers: {out}"
  237. );
  238. assert!(
  239. out.contains("offline"),
  240. "{fmt:?} must show the offline node: {out}"
  241. );
  242. }
  243. }
  244. #[test]
  245. fn recipes_none_renders_no_recipes_section() {
  246. for fmt in [OutputFormat::Plain, OutputFormat::Md, OutputFormat::Html] {
  247. let out = render_fleet(&fleet(vec![up_report()], None), fmt);
  248. assert!(!out.contains("Running recipes:"), "{fmt:?}");
  249. }
  250. }
  251. #[test]
  252. fn tmux_all_stopped_shows_summary_line() {
  253. for fmt in [OutputFormat::Plain, OutputFormat::Md, OutputFormat::Html] {
  254. let r = tmux_report(vec![tmux_entry("rec", "dgx1", false, false)]);
  255. let out = render_fleet(&fleet(vec![r], Some(RecipesSummary::default())), fmt);
  256. assert!(out.contains("all stopped"), "{fmt:?}: {out}");
  257. }
  258. }
  259. #[test]
  260. fn tmux_running_entry_rendered_stopped_skipped() {
  261. for fmt in [OutputFormat::Plain, OutputFormat::Md, OutputFormat::Html] {
  262. let r = tmux_report(vec![
  263. tmux_entry("rec", "dgx1", true, true),
  264. tmux_entry("rec", "dgx2", false, false),
  265. ]);
  266. let out = render_fleet(&fleet(vec![r], None), fmt);
  267. assert!(out.contains("rec \u{2014} dgx1"), "{fmt:?}: {out}");
  268. assert!(!out.contains("dgx2"), "{fmt:?} must skip stopped: {out}");
  269. }
  270. }
  271. #[test]
  272. fn recipes_summary_is_sorted_and_deterministic() {
  273. let build = || {
  274. let mut summary = RecipesSummary::default();
  275. let mut z = HashMap::new();
  276. z.insert("zcluster".to_string(), vec!["n1".to_string()]);
  277. summary.by_recipe.insert("zebra".to_string(), z);
  278. let mut a = HashMap::new();
  279. a.insert("c".to_string(), vec!["n2".to_string()]);
  280. summary.by_recipe.insert("alpha".to_string(), a);
  281. FleetHealth {
  282. clusters: vec![],
  283. recipes: Some(summary),
  284. }
  285. };
  286. let first = render_fleet(&build(), OutputFormat::Plain);
  287. let second = render_fleet(&build(), OutputFormat::Plain);
  288. assert_eq!(first, second, "rendering must be deterministic");
  289. let alpha = first.find("alpha").expect("alpha rendered");
  290. let zebra = first.find("zebra").expect("zebra rendered");
  291. assert!(alpha < zebra, "recipes must be sorted");
  292. }
  293. /// The improved plain layout: headers + verdict tails, one line per
  294. /// node, report-globally aligned container columns, down-cluster
  295. /// suppression, tmux collapse, sorted recipes.
  296. #[test]
  297. fn plain_layout_is_aligned_and_suppressed() {
  298. let mut up = report("up1", false);
  299. let mut n2 = node("n2", true, &[]);
  300. n2.net_up = false;
  301. n2.net_details = vec!["socket:DOWN".to_string()];
  302. up.nodes = vec![
  303. node(
  304. "n0",
  305. true,
  306. &[
  307. &container("web-a", "Up 5 minutes", "img/one:1"),
  308. &container("web-b-long", "Exited (0) 3 hours ago", "img/two:22"),
  309. ],
  310. ),
  311. node(
  312. "n1",
  313. true,
  314. &[&container("web-c", "Up 2 hours", "img/three:3")],
  315. ),
  316. n2,
  317. ];
  318. up.tmux = vec![
  319. tmux_entry("rec", "dgx1", true, true),
  320. tmux_entry("rec", "dgx2", false, false),
  321. ];
  322. let mut down = down_report();
  323. let mut alive = node("alive", true, &[&container("hidden", "Up", "img")]);
  324. alive.net_up = false;
  325. alive.net_details = vec!["socket:DOWN".to_string()];
  326. down.nodes = vec![alive, node("dead", false, &[])];
  327. down.tmux = vec![tmux_entry("stopped_rec", "dgx3", false, false)];
  328. let mut summary = RecipesSummary::default();
  329. summary
  330. .by_recipe
  331. .entry("zebra".to_string())
  332. .or_default()
  333. .insert("zc".to_string(), vec!["n9".to_string()]);
  334. summary
  335. .by_recipe
  336. .entry("alpha".to_string())
  337. .or_default()
  338. .insert("ac".to_string(), vec!["n8".to_string()]);
  339. let out = render_fleet(&fleet(vec![up, down], Some(summary)), OutputFormat::Plain);
  340. // One header + verdict tail per cluster.
  341. assert!(out.contains("=== up1 ==="), "{out}");
  342. assert!(out.contains("=== down ==="), "{out}");
  343. assert!(out.contains("cluster offline"), "down verdict tail: {out}");
  344. assert!(
  345. out.contains("interface down"),
  346. "net-down tail on the up cluster: {out}"
  347. );
  348. // One line per node; offline nodes get an explicit offline
  349. // line; node/container rows are indented under the header.
  350. assert!(out.contains("dead (10.0.0.1)"), "{out}");
  351. assert!(out.contains("— offline"), "{out}");
  352. assert!(
  353. out.lines().any(|l| l.starts_with(" ● ")),
  354. "node indent: {out}"
  355. );
  356. assert!(
  357. out.lines().any(|l| l.starts_with(" ✗ dead")),
  358. "offline node indent: {out}"
  359. );
  360. assert!(
  361. out.lines().any(|l| l.starts_with(" web-a ")),
  362. "container indent: {out}"
  363. );
  364. // Net details whenever the interface is down — on the up
  365. // cluster (n2) and on the down cluster (alive).
  366. assert_eq!(
  367. out.matches("socket:DOWN").count(),
  368. 2,
  369. "net details must show on down nodes in both clusters: {out}"
  370. );
  371. // Down cluster suppresses container data entirely.
  372. assert!(!out.contains("hidden"), "{out}");
  373. // Container rows from different nodes align on report-global
  374. // widths: the image column starts at the same offset everywhere.
  375. let image_col = |needle: &str| {
  376. out.lines()
  377. .find(|l| l.contains(needle))
  378. .unwrap_or_else(|| panic!("{needle} missing: {out}"))
  379. .find(needle)
  380. .unwrap()
  381. };
  382. assert_eq!(
  383. image_col("img/one:1"),
  384. image_col("img/three:3"),
  385. "container rows must align across nodes: {out}"
  386. );
  387. assert_eq!(image_col("img/one:1"), image_col("img/two:22"), "{out}");
  388. assert!(!out.contains('\t'), "plain output must have no tabs: {out}");
  389. // tmux: active entries render, stopped ones are skipped; an
  390. // all-stopped cluster collapses to one count line.
  391. assert!(out.contains("rec — dgx1"), "{out}");
  392. assert!(!out.contains("dgx2"), "{out}");
  393. assert!(out.contains("all stopped"), "{out}");
  394. // Recipes summary sorted by recipe.
  395. let alpha = out.find("alpha").expect("alpha rendered");
  396. let zebra = out.find("zebra").expect("zebra rendered");
  397. assert!(alpha < zebra, "recipes sorted: {out}");
  398. // Single-cluster runs omit the recipes section.
  399. let solo = render_fleet(
  400. &fleet(vec![report("solo", false)], None),
  401. OutputFormat::Plain,
  402. );
  403. assert!(
  404. !solo.contains("Running recipes:"),
  405. "single-cluster run omits recipes: {solo}"
  406. );
  407. }
  408. #[tokio::test]
  409. async fn renders_in_active_locale() {
  410. crate::locale::set_global_locale(Some("ru-RU".to_string()));
  411. let out = crate::locale::spawn_with_locale(async {
  412. render_fleet(
  413. &fleet(vec![], Some(RecipesSummary::default())),
  414. OutputFormat::Plain,
  415. )
  416. })
  417. .await
  418. .unwrap();
  419. assert!(
  420. out.contains("Нет запущенных"),
  421. "expected Russian no-recipes line: {out}"
  422. );
  423. }
  424. }