diff --git a/src/boot_model.rs b/src/boot_model.rs index dfd733e..2d11438 100644 --- a/src/boot_model.rs +++ b/src/boot_model.rs @@ -955,76 +955,38 @@ fn summary(facts: &UnitFacts) -> String { #[cfg(test)] mod tests { - use std::iter::once; - use super::*; - - const MS: Duration = Duration::from_millis(1); - - fn facts(name: &str, after: &[&str]) -> UnitFacts { - UnitFacts { - name: name.to_owned(), - description: format!("{name} description"), - after: after.iter().map(ToString::to_string).collect(), - before: Vec::new(), - } - } - - fn activating(since: Duration) -> Activity { - Activity { - state: Some(ActiveState::Activating), - inactive_exit: Some(since), - active_enter: None, - active_exit: None, - } - } + use crate::boot_test_fixtures::{ + MS, activating, facts, queue_unit, queued_boot, running, tree_outline, waiting, + }; fn sample() -> BootState { - let mut state = BootState::new(Phase::Host); - state.set_root("graphical.target".to_owned()); - [ - ( - facts("graphical.target", &["multi-user.target"]), - Activity::default(), - ), - ( - facts( + queued_boot( + Phase::Host, + 500 * MS, + [ + waiting("graphical.target", &["multi-user.target"]), + waiting( "multi-user.target", &["docker.service", "nm-wait.service", "basic.target"], ), - Activity::default(), - ), - ( - facts("nm-wait.service", &["nm.service"]), - Activity::default(), - ), - (facts("nm.service", &[]), activating(1000 * MS)), - ( - facts("docker.service", &["containerd.service"]), - activating(3000 * MS), - ), - (facts("containerd.service", &[]), Activity::default()), - (facts("basic.target", &[]), Activity::default()), - ] - .into_iter() - .zip(1..) - .for_each(|((unit, activity), id)| { - let name = unit.name.clone(); - state.track_unit(unit, activity); - state.queue_job(id, &name, JobKind::BringsUp, 500 * MS); - }); - state + waiting("nm-wait.service", &["nm.service"]), + running("nm.service", &[], 1000 * MS), + running("docker.service", &["containerd.service"], 3000 * MS), + waiting("containerd.service", &[]), + waiting("basic.target", &[]), + ], + ) + } + + fn outline(state: &BootState) -> Vec { + tree_outline(&state.tree_rows(Duration::MAX)) } #[test] fn pending_units_nest_under_what_waits_on_them() { - let outline: Vec = sample() - .tree_rows(Duration::MAX) - .into_iter() - .map(|row| format!("{}{}", " ".repeat(row.depth), row.label)) - .collect(); assert_eq!( - outline, + outline(&sample()), [ "basic.target", "multi-user.target", @@ -1039,13 +1001,8 @@ mod tests { #[test] fn only_chains_leading_to_running_units_stay_in_view() { - let rows = fit_to_height(sample().tree_rows(Duration::MAX), 0); - let outline: Vec = rows - .iter() - .map(|row| format!("{}{}", " ".repeat(row.depth), row.label)) - .collect(); assert_eq!( - outline, + tree_outline(&fit_to_height(sample().tree_rows(Duration::MAX), 0)), [ "basic.target", "multi-user.target", @@ -1069,12 +1026,7 @@ mod tests { ], "b.service", ); - let outline = |budget| -> Vec { - fit_to_height(state.tree_rows(Duration::MAX), budget) - .into_iter() - .map(|row| format!("{}{}", " ".repeat(row.depth), row.label)) - .collect() - }; + let outline = |budget| tree_outline(&fit_to_height(state.tree_rows(Duration::MAX), budget)); assert_eq!(outline(4), ["root.target", " b", " a"]); assert_eq!( outline(5), @@ -1084,31 +1036,21 @@ mod tests { #[test] fn a_unit_sits_in_the_earliest_stage_that_waits_on_it() { - let mut state = BootState::new(Phase::Host); - state.set_root("multi-user.target".to_owned()); - [ - facts( - "multi-user.target", - &["basic.target", "dbus.service", "web.service"], - ), - facts("basic.target", &["dbus.service"]), - facts("dbus.service", &[]), - facts("web.service", &[]), - ] - .into_iter() - .zip(1..) - .for_each(|(unit, id)| { - let name = unit.name.clone(); - state.track_unit(unit, Activity::default()); - state.queue_job(id, &name, JobKind::BringsUp, Duration::ZERO); - }); - let outline: Vec = state - .tree_rows(Duration::MAX) - .into_iter() - .map(|row| format!("{}{}", " ".repeat(row.depth), row.label)) - .collect(); + let state = queued_boot( + Phase::Host, + Duration::ZERO, + [ + waiting( + "multi-user.target", + &["basic.target", "dbus.service", "web.service"], + ), + waiting("basic.target", &["dbus.service"]), + waiting("dbus.service", &[]), + waiting("web.service", &[]), + ], + ); assert_eq!( - outline, + outline(&state), ["basic.target", " dbus", "multi-user.target", " web"] ); } @@ -1127,12 +1069,7 @@ mod tests { #[test] fn descriptions_that_repeat_the_label_are_dropped() { let described = |name: &str, description: &str| { - summary(&UnitFacts { - name: name.to_owned(), - description: description.to_owned(), - after: Vec::new(), - before: Vec::new(), - }) + summary(&waiting(name, &[]).described(description).facts) }; assert_eq!( described("multi-user.target", "Multi-User System"), @@ -1144,32 +1081,25 @@ mod tests { assert_eq!(described("demo.service", ""), ""); } - fn chain_state(waits: &[(&str, &[&str])], running: &str) -> BootState { - let mut state = BootState::new(Phase::Host); - state.set_root(waits[0].0.to_owned()); - waits.iter().zip(1..).for_each(|((name, after), id)| { - let activity = if *name == running { - activating(Duration::ZERO) - } else { - Activity::default() - }; - state.track_unit(facts(name, after), activity); - state.queue_job(id, name, JobKind::BringsUp, Duration::ZERO); - }); - state + fn chain_state(waits: &[(&str, &[&str])], running_unit: &str) -> BootState { + queued_boot( + Phase::Host, + Duration::ZERO, + waits.iter().map(|(name, after)| { + if *name == running_unit { + running(name, after, Duration::ZERO) + } else { + waiting(name, after) + } + }), + ) } fn folded_outline(state: &BootState) -> Vec { - fold_chains(fit_to_height(state.tree_rows(Duration::MAX), 0)) - .into_iter() - .map(|row| { - let chain = once(row.label.as_str()) - .chain(row.chain.iter().map(String::as_str)) - .collect::>() - .join(" > "); - format!("{}{chain}", " ".repeat(row.depth)) - }) - .collect() + tree_outline(&fold_chains(fit_to_height( + state.tree_rows(Duration::MAX), + 0, + ))) } #[test] @@ -1224,12 +1154,14 @@ mod tests { #[test] fn with_nothing_running_only_the_root_target_stays_in_view() { - let mut state = BootState::new(Phase::Host); - state.set_root("demo.target".to_owned()); - state.track_unit(facts("demo.target", &["idle.service"]), Activity::default()); - state.track_unit(facts("idle.service", &[]), Activity::default()); - state.queue_job(1, "demo.target", JobKind::BringsUp, Duration::ZERO); - state.queue_job(2, "idle.service", JobKind::BringsUp, Duration::ZERO); + let state = queued_boot( + Phase::Host, + Duration::ZERO, + [ + waiting("demo.target", &["idle.service"]), + waiting("idle.service", &[]), + ], + ); let rows = fit_to_height(state.tree_rows(Duration::MAX), 0); assert_eq!(rows.len(), 1); assert_eq!(rows[0].label, "demo.target"); @@ -1283,8 +1215,7 @@ mod tests { #[test] fn display_manager_activation_hands_the_console_over() { let mut state = sample(); - state.track_unit(facts(HANDOFF_UNIT, &[]), Activity::default()); - state.queue_job(20, HANDOFF_UNIT, JobKind::BringsUp, 500 * MS); + queue_unit(&mut state, 20, waiting(HANDOFF_UNIT, &[]), 500 * MS); assert!(!state.is_complete()); state.update_activity(HANDOFF_UNIT, activating(6000 * MS)); assert!(state.is_complete()); @@ -1293,8 +1224,7 @@ mod tests { #[test] fn display_manager_started_in_one_step_hands_the_console_over() { let mut state = sample(); - state.track_unit(facts(HANDOFF_UNIT, &[]), Activity::default()); - state.queue_job(20, HANDOFF_UNIT, JobKind::BringsUp, 500 * MS); + queue_unit(&mut state, 20, waiting(HANDOFF_UNIT, &[]), 500 * MS); state.finish_job(20, JobResult::Done, 6000 * MS); assert!(!state.is_complete()); state.update_activity( @@ -1311,12 +1241,11 @@ mod tests { #[test] fn switching_root_freezes_the_initrd_summary() { - let mut state = BootState::new(Phase::Initrd); - state.set_root("initrd.target".to_owned()); - state.track_unit(facts("initrd.target", &[]), Activity::default()); - state.track_unit(facts("sysroot.mount", &[]), Activity::default()); - state.queue_job(1, "initrd.target", JobKind::BringsUp, Duration::ZERO); - state.queue_job(2, "sysroot.mount", JobKind::BringsUp, Duration::ZERO); + let mut state = queued_boot( + Phase::Initrd, + Duration::ZERO, + [waiting("initrd.target", &[]), waiting("sysroot.mount", &[])], + ); state.finish_job(2, JobResult::Done, 1000 * MS); state.finish_job(1, JobResult::Done, 1500 * MS); assert!(!state.is_complete()); @@ -1339,17 +1268,11 @@ mod tests { #[test] fn devices_show_their_description() { - let mut state = BootState::new(Phase::Host); - state.track_unit( - UnitFacts { - name: "dev-nvme1n1.device".to_owned(), - description: "Lexar SSD NM790 2TB".to_owned(), - after: Vec::new(), - before: Vec::new(), - }, - Activity::default(), + let state = queued_boot( + Phase::Host, + Duration::ZERO, + [waiting("dev-nvme1n1.device", &[]).described("Lexar SSD NM790 2TB")], ); - state.queue_job(1, "dev-nvme1n1.device", JobKind::BringsUp, Duration::ZERO); assert_eq!( state.tree_rows(Duration::MAX)[0].label, "Lexar SSD NM790 2TB" @@ -1358,76 +1281,39 @@ mod tests { #[test] fn units_nest_under_the_deepest_pending_waiter() { - let mut state = BootState::new(Phase::Host); - state.set_root("demo.target".to_owned()); - [ - facts( - "demo.target", - &["report.service", "web.service", "db.service"], - ), - facts("report.service", &["web.service"]), - facts("web.service", &["db.service"]), - facts("db.service", &[]), - ] - .into_iter() - .zip(1..) - .for_each(|(unit, id)| { - let name = unit.name.clone(); - state.track_unit(unit, Activity::default()); - state.queue_job(id, &name, JobKind::BringsUp, Duration::ZERO); - }); - let outline: Vec = state - .tree_rows(Duration::MAX) - .into_iter() - .map(|row| format!("{}{}", " ".repeat(row.depth), row.label)) - .collect(); - assert_eq!(outline, ["demo.target", " report", " web", " db"]); - } - - fn ordered(name: &str, after: &[&str], before: &[&str]) -> UnitFacts { - UnitFacts { - before: before.iter().map(ToString::to_string).collect(), - ..facts(name, after) - } - } - - fn outline(state: &BootState) -> Vec { - state - .tree_rows(Duration::MAX) - .into_iter() - .map(|row| format!("{}{}", " ".repeat(row.depth), row.label)) - .collect() + let state = queued_boot( + Phase::Host, + Duration::ZERO, + [ + waiting( + "demo.target", + &["report.service", "web.service", "db.service"], + ), + waiting("report.service", &["web.service"]), + waiting("web.service", &["db.service"]), + waiting("db.service", &[]), + ], + ); + assert_eq!( + outline(&state), + ["demo.target", " report", " web", " db"] + ); } #[test] fn stopping_units_wait_on_what_was_ordered_after_them() { - let mut state = BootState::new(Phase::Shutdown); - state.set_root("poweroff.target".to_owned()); - [ - ( - ordered("poweroff.target", &["shutdown.target"], &[]), - JobKind::BringsUp, - ), - ( - ordered("shutdown.target", &[], &["poweroff.target"]), - JobKind::BringsUp, - ), - ( - ordered("network.target", &[], &["web.service"]), - JobKind::TakesDown, - ), - ( - ordered("web.service", &["network.target"], &[]), - JobKind::TakesDown, - ), - ] - .into_iter() - .zip(1..) - .for_each(|((unit, kind), id)| { - let name = unit.name.clone(); - state.track_unit(unit, Activity::default()); - state.queue_job(id, &name, kind, Duration::ZERO); - }); + let state = queued_boot( + Phase::Shutdown, + Duration::ZERO, + [ + waiting("poweroff.target", &["shutdown.target"]), + waiting("shutdown.target", &[]).ordered_before(&["poweroff.target"]), + waiting("network.target", &[]) + .ordered_before(&["web.service"]) + .stopping(), + waiting("web.service", &["network.target"]).stopping(), + ], + ); assert_eq!( outline(&state), [ @@ -1441,25 +1327,14 @@ mod tests { #[test] fn starting_units_wait_on_stops_in_either_direction() { - let mut state = BootState::new(Phase::Shutdown); - state.set_root("shutdown.target".to_owned()); - [ - ( - ordered("shutdown.target", &[], &["db.service"]), - JobKind::BringsUp, - ), - ( - ordered("db.service", &["shutdown.target"], &[]), - JobKind::TakesDown, - ), - ] - .into_iter() - .zip(1..) - .for_each(|((unit, kind), id)| { - let name = unit.name.clone(); - state.track_unit(unit, Activity::default()); - state.queue_job(id, &name, kind, Duration::ZERO); - }); + let state = queued_boot( + Phase::Shutdown, + Duration::ZERO, + [ + waiting("shutdown.target", &[]).ordered_before(&["db.service"]), + waiting("db.service", &["shutdown.target"]).stopping(), + ], + ); assert_eq!(outline(&state), ["shutdown.target", " db"]); } @@ -1498,14 +1373,15 @@ mod tests { #[test] fn shutdown_counts_toward_its_own_milestones_and_never_completes() { - let mut state = BootState::new(Phase::Shutdown); - state.set_root("poweroff.target".to_owned()); - ["poweroff.target", "shutdown.target", "db.service"] - .into_iter() - .for_each(|name| state.track_unit(facts(name, &[]), Activity::default())); - state.queue_job(1, "poweroff.target", JobKind::BringsUp, Duration::ZERO); - state.queue_job(2, "shutdown.target", JobKind::BringsUp, Duration::ZERO); - state.queue_job(3, "db.service", JobKind::TakesDown, Duration::ZERO); + let mut state = queued_boot( + Phase::Shutdown, + Duration::ZERO, + [ + waiting("poweroff.target", &[]), + waiting("shutdown.target", &[]), + waiting("db.service", &[]).stopping(), + ], + ); state.finish_job(3, JobResult::Done, 500 * MS); state.finish_job(2, JobResult::Done, 600 * MS); state.finish_job(1, JobResult::Done, 700 * MS); @@ -1517,11 +1393,14 @@ mod tests { #[test] fn ordering_cycles_do_not_repeat_units() { - let mut state = BootState::new(Phase::Host); - state.track_unit(facts("a.service", &["b.service"]), Activity::default()); - state.track_unit(facts("b.service", &["a.service"]), Activity::default()); - state.queue_job(1, "a.service", JobKind::BringsUp, Duration::ZERO); - state.queue_job(2, "b.service", JobKind::BringsUp, Duration::ZERO); + let state = queued_boot( + Phase::Host, + Duration::ZERO, + [ + waiting("a.service", &["b.service"]), + waiting("b.service", &["a.service"]), + ], + ); assert_eq!(state.tree_rows(Duration::MAX).len(), 2); } } diff --git a/src/boot_test_fixtures.rs b/src/boot_test_fixtures.rs new file mode 100644 index 0000000..1d95fcd --- /dev/null +++ b/src/boot_test_fixtures.rs @@ -0,0 +1,95 @@ +use std::{iter::once, time::Duration}; + +use crate::boot_model::{ActiveState, Activity, BootState, JobKind, Phase, TreeRow, UnitFacts}; + +pub const MS: Duration = Duration::from_millis(1); + +pub struct QueuedUnit { + pub facts: UnitFacts, + pub activity: Activity, + pub kind: JobKind, +} + +pub fn facts(name: &str, after: &[&str]) -> UnitFacts { + UnitFacts { + name: name.to_owned(), + description: String::new(), + after: after.iter().map(ToString::to_string).collect(), + before: Vec::new(), + } +} + +pub fn activating(since: Duration) -> Activity { + Activity { + state: Some(ActiveState::Activating), + inactive_exit: Some(since), + ..Activity::default() + } +} + +pub fn waiting(name: &str, after: &[&str]) -> QueuedUnit { + QueuedUnit { + facts: facts(name, after), + activity: Activity::default(), + kind: JobKind::BringsUp, + } +} + +pub fn running(name: &str, after: &[&str], since: Duration) -> QueuedUnit { + QueuedUnit { + activity: activating(since), + ..waiting(name, after) + } +} + +impl QueuedUnit { + pub fn stopping(self) -> Self { + Self { + kind: JobKind::TakesDown, + ..self + } + } + + pub fn ordered_before(mut self, before: &[&str]) -> Self { + self.facts.before = before.iter().map(ToString::to_string).collect(); + self + } + + pub fn described(mut self, description: &str) -> Self { + description.clone_into(&mut self.facts.description); + self + } +} + +pub fn queued_boot( + phase: Phase, + queued_at: Duration, + units: impl IntoIterator, +) -> BootState { + let mut state = BootState::new(phase); + units.into_iter().zip(1..).for_each(|(unit, id)| { + if id == 1 { + state.set_root(unit.facts.name.clone()); + } + queue_unit(&mut state, id, unit, queued_at); + }); + state +} + +pub fn queue_unit(state: &mut BootState, id: u32, unit: QueuedUnit, queued_at: Duration) { + let name = unit.facts.name.clone(); + state.track_unit(unit.facts, unit.activity); + state.queue_job(id, &name, unit.kind, queued_at); +} + +pub fn tree_outline(rows: &[TreeRow]) -> Vec { + rows.iter() + .map(|row| { + let chain = once(row.label.as_str()) + .chain(row.chain.iter().map(String::as_str)) + .collect::>() + .join(" > "); + format!("{}{chain}", " ".repeat(row.depth)) + }) + .collect() +} diff --git a/src/boot_view.rs b/src/boot_view.rs index 951c139..9d98ac0 100644 --- a/src/boot_view.rs +++ b/src/boot_view.rs @@ -236,18 +236,10 @@ mod tests { use ratatui::{Terminal, backend::TestBackend}; use super::*; - use crate::boot_model::{ActiveState, Activity, JobKind, JobResult, Phase, UnitFacts}; - - const MS: Duration = Duration::from_millis(1); - - fn facts(name: &str, after: &[&str]) -> UnitFacts { - UnitFacts { - name: name.to_owned(), - description: String::new(), - after: after.iter().map(ToString::to_string).collect(), - before: Vec::new(), - } - } + use crate::{ + boot_model::{JobResult, Phase}, + boot_test_fixtures::{MS, queue_unit, queued_boot, running, waiting}, + }; fn render(state: &BootState, now: Duration, width: u16, height: u16) -> Vec { screen_lines(&render_prompted(state, now, width, height, None)) @@ -298,47 +290,24 @@ mod tests { .collect() } - fn running_since(since: Duration) -> Activity { - Activity { - state: Some(ActiveState::Activating), - inactive_exit: Some(since), - active_enter: None, - active_exit: None, - } - } - fn boot_in_progress() -> BootState { - let mut state = BootState::new(Phase::Host); - state.set_root("graphical.target".to_owned()); - [ - ( - facts("graphical.target", &["multi-user.target"]), - Activity::default(), - ), - ( - facts( + let mut state = queued_boot( + Phase::Host, + 1000 * MS, + [ + waiting("graphical.target", &["multi-user.target"]), + waiting( "multi-user.target", &["docker.service", "systemd-rfkill.service", "web.service"], ), - Activity::default(), - ), - (facts("docker.service", &[]), running_since(9000 * MS)), - ( - facts("systemd-rfkill.service", &[]), - running_since(6000 * MS), - ), - (facts("web.service", &[]), Activity::default()), - (facts("sysinit.target", &[]), Activity::default()), - (facts("nvidia-powerd.service", &[]), Activity::default()), - (facts("udev-settle.service", &[]), running_since(2000 * MS)), - ] - .into_iter() - .zip(1..) - .for_each(|((unit, activity), id)| { - let name = unit.name.clone(); - state.track_unit(unit, activity); - state.queue_job(id, &name, JobKind::BringsUp, 1000 * MS); - }); + running("docker.service", &[], 9000 * MS), + running("systemd-rfkill.service", &[], 6000 * MS), + waiting("web.service", &[]), + waiting("sysinit.target", &[]), + waiting("nvidia-powerd.service", &[]), + running("udev-settle.service", &[], 2000 * MS), + ], + ); state.finish_job(6, JobResult::Done, 4000 * MS); state.finish_job(7, JobResult::Failed, 5000 * MS); state.finish_job(8, JobResult::Done, 3500 * MS); @@ -403,22 +372,17 @@ mod tests { #[test] fn failures_stay_in_the_stage_that_wanted_them() { - let mut state = BootState::new(Phase::Host); - state.set_root("multi-user.target".to_owned()); - [ - facts("multi-user.target", &["basic.target", "web.service"]), - facts("basic.target", &["flaky.service", "quick.service"]), - facts("flaky.service", &[]), - facts("quick.service", &[]), - facts("web.service", &[]), - ] - .into_iter() - .zip(1..) - .for_each(|(unit, id)| { - let name = unit.name.clone(); - state.track_unit(unit, Activity::default()); - state.queue_job(id, &name, JobKind::BringsUp, Duration::ZERO); - }); + let mut state = queued_boot( + Phase::Host, + Duration::ZERO, + [ + waiting("multi-user.target", &["basic.target", "web.service"]), + waiting("basic.target", &["flaky.service", "quick.service"]), + waiting("flaky.service", &[]), + waiting("quick.service", &[]), + waiting("web.service", &[]), + ], + ); state.finish_job(3, JobResult::Failed, 1000 * MS); state.finish_job(4, JobResult::Done, 1000 * MS); state.finish_job(2, JobResult::Done, 1500 * MS); @@ -436,25 +400,15 @@ mod tests { #[test] fn finished_units_linger_in_place_before_leaving() { - let mut state = BootState::new(Phase::Host); - state.set_root("multi-user.target".to_owned()); - [ - facts("multi-user.target", &["early.service", "late.service"]), - facts("early.service", &[]), - facts("late.service", &[]), - ] - .into_iter() - .zip(1..) - .for_each(|(unit, id)| { - let name = unit.name.clone(); - let activity = if name.ends_with(".service") { - running_since(Duration::ZERO) - } else { - Activity::default() - }; - state.track_unit(unit, activity); - state.queue_job(id, &name, JobKind::BringsUp, Duration::ZERO); - }); + let mut state = queued_boot( + Phase::Host, + Duration::ZERO, + [ + waiting("multi-user.target", &["early.service", "late.service"]), + running("early.service", &[], Duration::ZERO), + running("late.service", &[], Duration::ZERO), + ], + ); state.finish_job(2, JobResult::Done, 3000 * MS); let lingering = render(&state, 4000 * MS, 110, 24); let screen = lingering.join("\n"); @@ -571,8 +525,12 @@ mod tests { #[test] fn sub_second_runs_stay_off_the_timeline() { let mut state = boot_in_progress(); - state.track_unit(facts("blink.service", &[]), running_since(4000 * MS)); - state.queue_job(20, "blink.service", JobKind::BringsUp, 4000 * MS); + queue_unit( + &mut state, + 20, + running("blink.service", &[], 4000 * MS), + 4000 * MS, + ); state.finish_job(20, JobResult::Done, 4400 * MS); let lines = render(&state, 12_400 * MS, 110, 24); assert!( @@ -583,29 +541,18 @@ mod tests { } fn chain_boot() -> BootState { - let mut state = BootState::new(Phase::Host); - state.set_root("graphical.target".to_owned()); - [ - ("graphical.target", "network-wait.service"), - ("network-wait.service", "network.target"), - ("network.target", "network-manager.service"), - ("network-manager.service", "dbus.service"), - ("dbus.service", "udev-settle.service"), - ("udev-settle.service", ""), - ] - .into_iter() - .zip(1..) - .for_each(|((name, waits_on), id)| { - let after: Vec<&str> = [waits_on].into_iter().filter(|w| !w.is_empty()).collect(); - let activity = if name == "udev-settle.service" { - running_since(1000 * MS) - } else { - Activity::default() - }; - state.track_unit(facts(name, &after), activity); - state.queue_job(id, name, JobKind::BringsUp, Duration::ZERO); - }); - state + queued_boot( + Phase::Host, + Duration::ZERO, + [ + waiting("graphical.target", &["network-wait.service"]), + waiting("network-wait.service", &["network.target"]), + waiting("network.target", &["network-manager.service"]), + waiting("network-manager.service", &["dbus.service"]), + waiting("dbus.service", &["udev-settle.service"]), + running("udev-settle.service", &[], 1000 * MS), + ], + ) } #[test] @@ -649,34 +596,22 @@ mod tests { } fn described_boot() -> BootState { - let mut state = BootState::new(Phase::Host); - state.set_root("multi-user.target".to_owned()); - [ - ( - "multi-user.target", - "Multi-User System", - &["docker.service", "systemd-rfkill.service"][..], - ), - ("docker.service", "Docker Application Container Engine", &[]), - ( - "systemd-rfkill.service", - "Load/Save RF Kill Switch Status", - &[], - ), - ] - .into_iter() - .zip(1..) - .for_each(|((name, description, after), id)| { - state.track_unit( - UnitFacts { - description: description.to_owned(), - ..facts(name, after) - }, - running_since(1000 * MS), - ); - state.queue_job(id, name, JobKind::BringsUp, Duration::ZERO); - }); - state + queued_boot( + Phase::Host, + Duration::ZERO, + [ + running( + "multi-user.target", + &["docker.service", "systemd-rfkill.service"], + 1000 * MS, + ) + .described("Multi-User System"), + running("docker.service", &[], 1000 * MS) + .described("Docker Application Container Engine"), + running("systemd-rfkill.service", &[], 1000 * MS) + .described("Load/Save RF Kill Switch Status"), + ], + ) } #[test] @@ -704,14 +639,12 @@ mod tests { #[test] fn non_service_units_carry_their_kind() { let mut state = described_boot(); - state.track_unit( - UnitFacts { - description: "Docker Socket for the API".to_owned(), - ..facts("docker.socket", &[]) - }, - running_since(1000 * MS), + queue_unit( + &mut state, + 9, + running("docker.socket", &[], 1000 * MS).described("Docker Socket for the API"), + Duration::ZERO, ); - state.queue_job(9, "docker.socket", JobKind::BringsUp, Duration::ZERO); let lines = render(&state, 3000 * MS, 110, 24); let screen = lines.join("\n"); let socket_row = lines @@ -739,14 +672,13 @@ mod tests { #[test] fn long_timeline_labels_end_in_an_ellipsis() { let mut state = boot_in_progress(); - state.track_unit( - UnitFacts { - description: "Kernel Configuration File System".to_owned(), - ..facts("sys-kernel-config.mount", &[]) - }, - running_since(2000 * MS), + queue_unit( + &mut state, + 30, + running("sys-kernel-config.mount", &[], 2000 * MS) + .described("Kernel Configuration File System"), + 2000 * MS, ); - state.queue_job(30, "sys-kernel-config.mount", JobKind::BringsUp, 2000 * MS); state.finish_job(30, JobResult::Done, 4000 * MS); let lines = render(&state, 12_400 * MS, 110, 24); assert!( @@ -771,15 +703,15 @@ mod tests { #[test] fn finished_stages_collapse_into_a_summary_rule() { - let mut state = BootState::new(Phase::Initrd); - state.set_root("initrd.target".to_owned()); - state.track_unit(facts("initrd.target", &[]), Activity::default()); - state.queue_job(1, "initrd.target", JobKind::BringsUp, Duration::ZERO); + let mut state = queued_boot( + Phase::Initrd, + Duration::ZERO, + [waiting("initrd.target", &[])], + ); state.finish_job(1, JobResult::Done, 6000 * MS); state.switch_root(7200 * MS); state.set_root("graphical.target".to_owned()); - state.track_unit(facts("graphical.target", &[]), Activity::default()); - state.queue_job(2, "graphical.target", JobKind::BringsUp, 7300 * MS); + queue_unit(&mut state, 2, waiting("graphical.target", &[]), 7300 * MS); let lines = render(&state, 17_300 * MS, 110, 24); let screen = lines.join("\n"); assert!(lines[0].ends_with("17.3s"), "{screen}"); @@ -800,10 +732,11 @@ mod tests { #[test] fn completion_announces_the_handover() { - let mut state = BootState::new(Phase::Host); - state.set_root("graphical.target".to_owned()); - state.track_unit(facts("graphical.target", &[]), Activity::default()); - state.queue_job(1, "graphical.target", JobKind::BringsUp, Duration::ZERO); + let mut state = queued_boot( + Phase::Host, + Duration::ZERO, + [waiting("graphical.target", &[])], + ); state.finish_job(1, JobResult::Done, 9000 * MS); let lines = render(&state, 9500 * MS, 110, 24); assert!( @@ -817,11 +750,12 @@ mod tests { #[test] fn shutdown_in_progress_names_its_stage() { - let mut state = BootState::new(Phase::Shutdown); + let mut state = queued_boot( + Phase::Shutdown, + 20_000 * MS, + [waiting("poweroff.target", &[])], + ); state.origin = 20_000 * MS; - state.set_root("poweroff.target".to_owned()); - state.track_unit(facts("poweroff.target", &[]), Activity::default()); - state.queue_job(1, "poweroff.target", JobKind::BringsUp, 20_000 * MS); let lines = render(&state, 21_500 * MS, 110, 24); assert!( lines[1].starts_with("└─ Shutdown 1.5s") && lines[1].ends_with("Running"), @@ -858,22 +792,28 @@ mod tests { #[test] fn the_timeline_keeps_only_the_slowest_finished_runs() { - let mut state = BootState::new(Phase::Host); - state.set_root("multi-user.target".to_owned()); let quick_units: Vec = (0..10).map(|n| format!("run{n:02}.service")).collect(); let after: Vec<&str> = quick_units .iter() .map(String::as_str) .chain(["slow.service"]) .collect(); - state.track_unit(facts("multi-user.target", &after), Activity::default()); - state.queue_job(100, "multi-user.target", JobKind::BringsUp, Duration::ZERO); - state.track_unit(facts("slow.service", &[]), running_since(Duration::ZERO)); - state.queue_job(101, "slow.service", JobKind::BringsUp, Duration::ZERO); - quick_units.iter().zip(0..).for_each(|(name, id)| { - state.track_unit(facts(name, &[]), running_since(Duration::ZERO)); - state.queue_job(id, name, JobKind::BringsUp, Duration::ZERO); - state.finish_job(id, JobResult::Done, (1000 + 1000 * id) * MS); + let mut state = queued_boot( + Phase::Host, + Duration::ZERO, + [ + waiting("multi-user.target", &after), + running("slow.service", &[], Duration::ZERO), + ], + ); + quick_units.iter().zip(10..).for_each(|(name, id)| { + queue_unit( + &mut state, + id, + running(name, &[], Duration::ZERO), + Duration::ZERO, + ); + state.finish_job(id, JobResult::Done, (1000 * id - 9000) * MS); }); let lines = render(&state, 20_000 * MS, 110, 40); let screen = lines.join("\n"); @@ -897,12 +837,12 @@ mod tests { #[test] fn overflowing_trees_are_cut_with_a_count() { - let mut state = BootState::new(Phase::Host); - (0..30).for_each(|id| { - let name = format!("unit{id:02}.service"); - state.track_unit(facts(&name, &[]), Activity::default()); - state.queue_job(id, &name, JobKind::BringsUp, Duration::ZERO); - }); + let names: Vec = (0..30).map(|id| format!("unit{id:02}.service")).collect(); + let state = queued_boot( + Phase::Host, + Duration::ZERO, + names.iter().map(|name| waiting(name, &[])), + ); let lines = render(&state, 1000 * MS, 110, 20); assert!( lines.iter().any(|line| line.starts_with(" … ")), diff --git a/src/lib.rs b/src/lib.rs index 1b48f72..3298a2c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ pub mod boot_model; pub mod boot_origin; +#[cfg(test)] +mod boot_test_fixtures; pub mod boot_view; pub mod color_scheme; pub mod config_file;