refactor: split model and view into files under 500 lines
This commit is contained in:
1 parent
4c8268a719
commit
589cc3c82b
10 files changed
+1744
-1655
No files matched your search
+10
-1050
@@ -1,17 +1,15 @@
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
time::Duration,
|
||||
};
|
||||
mod job_ingestion;
|
||||
mod reached_milestones;
|
||||
mod unit_facts;
|
||||
mod waiting_tree;
|
||||
|
||||
use petgraph::{
|
||||
Direction::Incoming,
|
||||
algo::{DfsSpace, has_path_connecting},
|
||||
graphmap::DiGraphMap,
|
||||
visit::Visitable,
|
||||
};
|
||||
use std::{collections::HashMap, time::Duration};
|
||||
|
||||
type WaitingGraph<'a> = DiGraphMap<&'a str, ()>;
|
||||
type WaitingSearch<'a> = DfsSpace<&'a str, <WaitingGraph<'a> as Visitable>::Map>;
|
||||
pub use self::{
|
||||
reached_milestones::MilestoneRow,
|
||||
unit_facts::{ActiveState, Activity, JobKind, JobResult, UnitFacts, UnitKind},
|
||||
waiting_tree::{RowStatus, TreeNode, TreeRow, flatten_forest},
|
||||
};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Phase {
|
||||
@@ -33,77 +31,6 @@ impl Phase {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ActiveState {
|
||||
Inactive,
|
||||
Activating,
|
||||
Active,
|
||||
Deactivating,
|
||||
Reloading,
|
||||
Failed,
|
||||
}
|
||||
|
||||
impl ActiveState {
|
||||
#[must_use]
|
||||
pub fn parse(raw: &str) -> Self {
|
||||
match raw {
|
||||
"activating" => Self::Activating,
|
||||
"active" => Self::Active,
|
||||
"deactivating" => Self::Deactivating,
|
||||
"reloading" => Self::Reloading,
|
||||
"failed" => Self::Failed,
|
||||
_ => Self::Inactive,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum JobResult {
|
||||
Done,
|
||||
Skipped,
|
||||
Canceled,
|
||||
Failed,
|
||||
Timeout,
|
||||
Dependency,
|
||||
}
|
||||
|
||||
impl JobResult {
|
||||
#[must_use]
|
||||
pub fn parse(raw: &str) -> Self {
|
||||
match raw {
|
||||
"done" => Self::Done,
|
||||
"skipped" => Self::Skipped,
|
||||
"canceled" => Self::Canceled,
|
||||
"timeout" => Self::Timeout,
|
||||
"dependency" => Self::Dependency,
|
||||
_ => Self::Failed,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct UnitFacts {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub after: Vec<String>,
|
||||
pub before: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Activity {
|
||||
pub state: Option<ActiveState>,
|
||||
pub inactive_exit: Option<Duration>,
|
||||
pub active_enter: Option<Duration>,
|
||||
pub active_exit: Option<Duration>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum JobKind {
|
||||
BringsUp,
|
||||
TakesDown,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
struct PendingJob {
|
||||
id: u32,
|
||||
@@ -135,161 +62,6 @@ struct UnitRecord {
|
||||
progress: Progress,
|
||||
}
|
||||
|
||||
impl UnitRecord {
|
||||
fn apply(&mut self, activity: Activity) {
|
||||
if let Some(state) = activity.state {
|
||||
self.state = state;
|
||||
}
|
||||
if activity.inactive_exit.is_some() {
|
||||
self.inactive_exit = activity.inactive_exit;
|
||||
}
|
||||
if activity.active_enter.is_some() {
|
||||
self.active_enter = activity.active_enter;
|
||||
}
|
||||
if activity.active_exit.is_some() {
|
||||
self.active_exit = activity.active_exit;
|
||||
}
|
||||
}
|
||||
|
||||
fn transition_started(&self, kind: JobKind) -> Option<Duration> {
|
||||
match kind {
|
||||
JobKind::TakesDown => self.active_exit,
|
||||
JobKind::BringsUp | JobKind::Unknown => self.inactive_exit,
|
||||
}
|
||||
}
|
||||
|
||||
fn running_since(&self) -> Option<Duration> {
|
||||
let Progress::Pending(job) = self.progress else {
|
||||
return None;
|
||||
};
|
||||
let transitioning = matches!(
|
||||
self.state,
|
||||
ActiveState::Activating | ActiveState::Deactivating | ActiveState::Reloading
|
||||
);
|
||||
transitioning.then(|| self.transition_started(job.kind).unwrap_or(job.queued_at))
|
||||
}
|
||||
|
||||
fn row_status(&self) -> RowStatus {
|
||||
match self.progress {
|
||||
Progress::Pending(job) => {
|
||||
self.running_since()
|
||||
.map_or(RowStatus::Waiting, |since| RowStatus::Running {
|
||||
queued_at: job.queued_at,
|
||||
since,
|
||||
})
|
||||
}
|
||||
Progress::Finished(Outcome::Failed) => RowStatus::Failed,
|
||||
Progress::Finished(_) | Progress::Untouched => RowStatus::Done,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_pending(&self) -> bool {
|
||||
matches!(self.progress, Progress::Pending(_))
|
||||
}
|
||||
|
||||
fn is_finished(&self) -> bool {
|
||||
matches!(self.progress, Progress::Finished(_))
|
||||
}
|
||||
|
||||
fn takes_down(&self) -> bool {
|
||||
matches!(
|
||||
self.progress,
|
||||
Progress::Pending(PendingJob {
|
||||
kind: JobKind::TakesDown,
|
||||
..
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum UnitKind {
|
||||
Service,
|
||||
Target,
|
||||
Socket,
|
||||
Mount,
|
||||
Automount,
|
||||
Device,
|
||||
Swap,
|
||||
Path,
|
||||
Timer,
|
||||
Slice,
|
||||
Scope,
|
||||
Other,
|
||||
}
|
||||
|
||||
impl UnitKind {
|
||||
#[must_use]
|
||||
pub fn of(name: &str) -> Self {
|
||||
match name.rsplit_once('.').map(|(_, suffix)| suffix) {
|
||||
Some("service") => Self::Service,
|
||||
Some("target") => Self::Target,
|
||||
Some("socket") => Self::Socket,
|
||||
Some("mount") => Self::Mount,
|
||||
Some("automount") => Self::Automount,
|
||||
Some("device") => Self::Device,
|
||||
Some("swap") => Self::Swap,
|
||||
Some("path") => Self::Path,
|
||||
Some("timer") => Self::Timer,
|
||||
Some("slice") => Self::Slice,
|
||||
Some("scope") => Self::Scope,
|
||||
_ => Self::Other,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn tag(self) -> &'static str {
|
||||
match self {
|
||||
Self::Service | Self::Target | Self::Other => "",
|
||||
Self::Socket => "Socket",
|
||||
Self::Mount => "Mount",
|
||||
Self::Automount => "Automount",
|
||||
Self::Device => "Device",
|
||||
Self::Swap => "Swap",
|
||||
Self::Path => "Path",
|
||||
Self::Timer => "Timer",
|
||||
Self::Slice => "Slice",
|
||||
Self::Scope => "Scope",
|
||||
}
|
||||
}
|
||||
|
||||
fn labelled_by_description(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::Device | Self::Mount | Self::Swap | Self::Automount
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum RowStatus {
|
||||
Waiting,
|
||||
Running {
|
||||
queued_at: Duration,
|
||||
since: Duration,
|
||||
},
|
||||
Failed,
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TreeRow {
|
||||
pub depth: usize,
|
||||
pub label: String,
|
||||
pub description: String,
|
||||
pub kind: UnitKind,
|
||||
pub status: RowStatus,
|
||||
pub chain: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct MilestoneRow {
|
||||
pub label: String,
|
||||
pub finished_units: usize,
|
||||
pub started_at: Duration,
|
||||
pub reached_at: Duration,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Tally {
|
||||
pub done: usize,
|
||||
@@ -358,815 +130,3 @@ pub const HOST_MILESTONES: [&str; 4] = [
|
||||
pub const SHUTDOWN_MILESTONES: [&str; 3] = ["shutdown.target", "umount.target", "final.target"];
|
||||
|
||||
pub const HANDOFF_UNIT: &str = "display-manager.service";
|
||||
|
||||
impl BootState {
|
||||
#[must_use]
|
||||
pub fn new(phase: Phase) -> Self {
|
||||
let (milestone_targets, handoff_unit) = match phase {
|
||||
Phase::Initrd | Phase::Host => (
|
||||
HOST_MILESTONES.iter().map(ToString::to_string).collect(),
|
||||
Some(HANDOFF_UNIT.to_owned()),
|
||||
),
|
||||
Phase::Shutdown => (
|
||||
SHUTDOWN_MILESTONES
|
||||
.iter()
|
||||
.map(ToString::to_string)
|
||||
.collect(),
|
||||
None,
|
||||
),
|
||||
Phase::User => (Vec::new(), None),
|
||||
};
|
||||
Self {
|
||||
phase,
|
||||
origin: Duration::ZERO,
|
||||
root: Root::Unresolved,
|
||||
failed: Vec::new(),
|
||||
finished_stages: Vec::new(),
|
||||
finished_runs: Vec::new(),
|
||||
handoff_unit,
|
||||
manager_exited_at: None,
|
||||
milestone_targets,
|
||||
finished_before_reconnect: 0,
|
||||
units: HashMap::new(),
|
||||
jobs: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_root(&mut self, root: String) {
|
||||
if self.phase == Phase::User && !self.milestone_targets.contains(&root) {
|
||||
self.milestone_targets.push(root.clone());
|
||||
}
|
||||
if self.root.unit() != Some(&root) {
|
||||
self.root = Root::Awaiting(root);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_handoff(&mut self, unit: String) {
|
||||
if self.handoff_unit.is_some() {
|
||||
self.handoff_unit = Some(unit);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn track_unit(&mut self, facts: UnitFacts, activity: Activity) {
|
||||
let record = self
|
||||
.units
|
||||
.entry(facts.name.clone())
|
||||
.or_insert_with(|| UnitRecord {
|
||||
facts: facts.clone(),
|
||||
state: ActiveState::Inactive,
|
||||
inactive_exit: None,
|
||||
active_enter: None,
|
||||
active_exit: None,
|
||||
progress: Progress::Untouched,
|
||||
});
|
||||
record.facts = facts;
|
||||
record.apply(activity);
|
||||
}
|
||||
|
||||
pub fn update_activity(&mut self, unit: &str, activity: Activity) {
|
||||
if let Some(record) = self.units.get_mut(unit) {
|
||||
record.apply(activity);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn queue_job(&mut self, id: u32, unit: &str, kind: JobKind, at: Duration) {
|
||||
self.enqueue(id, unit, kind, at);
|
||||
if kind == JobKind::BringsUp
|
||||
&& self.jobs.get(&id).is_some_and(|tracked| tracked == unit)
|
||||
&& self.root == Root::Awaiting(unit.to_owned())
|
||||
{
|
||||
self.root = Root::Queued(unit.to_owned());
|
||||
}
|
||||
}
|
||||
|
||||
fn enqueue(&mut self, id: u32, unit: &str, kind: JobKind, at: Duration) {
|
||||
if self.jobs.contains_key(&id) {
|
||||
return;
|
||||
}
|
||||
let Some(record) = self.units.get_mut(unit) else {
|
||||
return;
|
||||
};
|
||||
record.progress = Progress::Pending(PendingJob {
|
||||
id,
|
||||
queued_at: at,
|
||||
kind,
|
||||
});
|
||||
self.jobs.insert(id, unit.to_owned());
|
||||
}
|
||||
|
||||
pub fn finish_job(&mut self, id: u32, result: JobResult, at: Duration) {
|
||||
let Some(unit) = self.jobs.remove(&id) else {
|
||||
return;
|
||||
};
|
||||
let Some(record) = self.units.get_mut(&unit) else {
|
||||
return;
|
||||
};
|
||||
let outcome = match result {
|
||||
JobResult::Done => Outcome::Succeeded { at },
|
||||
JobResult::Skipped | JobResult::Canceled => Outcome::Abandoned,
|
||||
JobResult::Failed | JobResult::Timeout | JobResult::Dependency => Outcome::Failed,
|
||||
};
|
||||
let label = display_label(&record.facts);
|
||||
if outcome == Outcome::Failed && !self.failed.contains(&label) {
|
||||
self.failed.push(label.clone());
|
||||
}
|
||||
let job = match record.progress {
|
||||
Progress::Pending(job) if job.id == id => job,
|
||||
_ => return,
|
||||
};
|
||||
record.progress = Progress::Finished(outcome);
|
||||
if let Some(started_at) = record
|
||||
.transition_started(job.kind)
|
||||
.filter(|started| job.kind != JobKind::Unknown && (self.origin..=at).contains(started))
|
||||
&& UnitKind::of(&record.facts.name) != UnitKind::Target
|
||||
{
|
||||
self.finished_runs.push(FinishedRun {
|
||||
label,
|
||||
started_at,
|
||||
finished_at: at,
|
||||
failed: outcome == Outcome::Failed,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn switch_root(&mut self, at: Duration) {
|
||||
let finished_here = self
|
||||
.units
|
||||
.values()
|
||||
.filter(|record| record.is_finished())
|
||||
.count();
|
||||
self.finished_stages.push(FinishedStage {
|
||||
label: Phase::Initrd.label(),
|
||||
finished_units: finished_here + self.finished_before_reconnect,
|
||||
failed_units: self
|
||||
.units
|
||||
.values()
|
||||
.filter(|record| record.progress == Progress::Finished(Outcome::Failed))
|
||||
.count(),
|
||||
started_at: self.origin,
|
||||
reached_at: at,
|
||||
});
|
||||
self.phase = Phase::Host;
|
||||
self.root = Root::Unresolved;
|
||||
self.finished_before_reconnect = 0;
|
||||
self.units.clear();
|
||||
self.jobs.clear();
|
||||
}
|
||||
|
||||
pub fn forget_jobs(&mut self) {
|
||||
self.finished_before_reconnect += self
|
||||
.units
|
||||
.values()
|
||||
.filter(|record| record.is_finished())
|
||||
.count();
|
||||
self.jobs.clear();
|
||||
self.units
|
||||
.values_mut()
|
||||
.for_each(|record| record.progress = Progress::Untouched);
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_complete(&self) -> bool {
|
||||
if matches!(self.phase, Phase::Initrd | Phase::Shutdown) {
|
||||
return false;
|
||||
}
|
||||
if self
|
||||
.handoff_unit
|
||||
.as_deref()
|
||||
.and_then(|unit| self.units.get(unit))
|
||||
.is_some_and(|handoff| handoff.state != ActiveState::Inactive)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
let Root::Queued(root) = &self.root else {
|
||||
return false;
|
||||
};
|
||||
self.units.get(root).is_some_and(|root| !root.is_pending())
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn tally(&self) -> Tally {
|
||||
let carried = Tally {
|
||||
done: self
|
||||
.finished_stages
|
||||
.iter()
|
||||
.map(|stage| stage.finished_units)
|
||||
.sum::<usize>()
|
||||
+ self.finished_before_reconnect,
|
||||
failed: self.failed.len(),
|
||||
..Tally::default()
|
||||
};
|
||||
self.units.values().fold(carried, |mut tally, record| {
|
||||
match record.progress {
|
||||
Progress::Pending(_) if record.running_since().is_some() => tally.running += 1,
|
||||
Progress::Pending(_) => tally.waiting += 1,
|
||||
Progress::Finished(Outcome::Succeeded { .. } | Outcome::Abandoned) => {
|
||||
tally.done += 1;
|
||||
}
|
||||
Progress::Finished(Outcome::Failed) | Progress::Untouched => {}
|
||||
}
|
||||
tally
|
||||
})
|
||||
}
|
||||
|
||||
fn milestone_reached_at(&self, target: &str) -> Option<Duration> {
|
||||
let record = self.units.get(target)?;
|
||||
match record.progress {
|
||||
Progress::Finished(Outcome::Succeeded { at }) => Some(at),
|
||||
Progress::Untouched => record
|
||||
.active_enter
|
||||
.filter(|entered| record.state == ActiveState::Active && *entered >= self.origin),
|
||||
Progress::Pending(_) | Progress::Finished(Outcome::Failed | Outcome::Abandoned) => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn milestones(&self) -> Vec<MilestoneRow> {
|
||||
let mut succeeded: Vec<Duration> = self
|
||||
.units
|
||||
.values()
|
||||
.filter_map(|record| match record.progress {
|
||||
Progress::Finished(Outcome::Succeeded { at }) => Some(at),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
succeeded.sort_unstable();
|
||||
let start = MilestoneWalk {
|
||||
rows: self
|
||||
.finished_stages
|
||||
.iter()
|
||||
.map(|stage| MilestoneRow {
|
||||
label: stage.label.to_owned(),
|
||||
finished_units: stage.finished_units,
|
||||
started_at: stage.started_at,
|
||||
reached_at: stage.reached_at,
|
||||
})
|
||||
.collect(),
|
||||
previous: self
|
||||
.finished_stages
|
||||
.last()
|
||||
.map_or(self.origin, |stage| stage.reached_at),
|
||||
consumed: 0,
|
||||
};
|
||||
self.milestone_targets
|
||||
.iter()
|
||||
.filter_map(|target| {
|
||||
self.milestone_reached_at(target)
|
||||
.map(|reached_at| ReachedMilestone { target, reached_at })
|
||||
})
|
||||
.fold(start, |walk, reached| walk.advance(&reached, &succeeded))
|
||||
.rows
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn waiting_forest(&self, finished_since: Duration) -> Vec<TreeNode> {
|
||||
let mut graph = WaitingGraph::new();
|
||||
self.units
|
||||
.iter()
|
||||
.filter(|(name, record)| match record.progress {
|
||||
Progress::Pending(_) | Progress::Finished(Outcome::Failed) => true,
|
||||
Progress::Finished(Outcome::Succeeded { at }) => {
|
||||
at >= finished_since || self.is_milestone(name)
|
||||
}
|
||||
Progress::Finished(Outcome::Abandoned) => self.is_milestone(name),
|
||||
Progress::Untouched => false,
|
||||
})
|
||||
.for_each(|(name, _)| {
|
||||
graph.add_node(name.as_str());
|
||||
});
|
||||
let edges: Vec<(&str, &str)> = graph
|
||||
.nodes()
|
||||
.flat_map(|waiter| {
|
||||
self.waits_for(waiter, &graph)
|
||||
.into_iter()
|
||||
.map(move |dependency| (waiter, dependency))
|
||||
})
|
||||
.collect();
|
||||
graph.extend(edges);
|
||||
let root = self.root.unit().filter(|root| graph.contains_node(root));
|
||||
let stages: Vec<&str> = self
|
||||
.milestone_targets
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
.filter(|target| graph.contains_node(target))
|
||||
.chain(root.filter(|root| !self.is_milestone(root)))
|
||||
.collect();
|
||||
let mut unblocked: Vec<&str> = graph
|
||||
.nodes()
|
||||
.filter(|name| {
|
||||
graph.neighbors_directed(name, Incoming).next().is_none() && !stages.contains(name)
|
||||
})
|
||||
.collect();
|
||||
self.sort_siblings(&mut unblocked);
|
||||
let mut leftovers: Vec<&str> = graph.nodes().collect();
|
||||
self.sort_siblings(&mut leftovers);
|
||||
if self.phase == Phase::User {
|
||||
unblocked.clear();
|
||||
leftovers.clear();
|
||||
}
|
||||
let mut space = DfsSpace::new(&graph);
|
||||
let mut visited = HashSet::new();
|
||||
let mut forest = Vec::new();
|
||||
stages
|
||||
.iter()
|
||||
.copied()
|
||||
.chain(unblocked)
|
||||
.chain(leftovers)
|
||||
.for_each(|top| {
|
||||
if visited.contains(top) {
|
||||
return;
|
||||
}
|
||||
let stage = self.grow(top, &graph, &mut space, &mut visited);
|
||||
if stage.row.status != RowStatus::Done || !stage.children.is_empty() {
|
||||
forest.push(stage);
|
||||
}
|
||||
});
|
||||
forest
|
||||
}
|
||||
|
||||
fn grow<'a>(
|
||||
&'a self,
|
||||
unit: &'a str,
|
||||
graph: &WaitingGraph<'a>,
|
||||
space: &mut WaitingSearch<'a>,
|
||||
visited: &mut HashSet<&'a str>,
|
||||
) -> TreeNode {
|
||||
visited.insert(unit);
|
||||
let record = &self.units[unit];
|
||||
let row = TreeRow {
|
||||
depth: 0,
|
||||
label: display_label(&record.facts),
|
||||
description: summary(&record.facts),
|
||||
kind: UnitKind::of(&record.facts.name),
|
||||
status: record.row_status(),
|
||||
chain: Vec::new(),
|
||||
};
|
||||
let candidates: Vec<&str> = graph
|
||||
.neighbors(unit)
|
||||
.filter(|dependency| !visited.contains(dependency) && !self.is_milestone(dependency))
|
||||
.collect();
|
||||
let mut waits_on =
|
||||
|waiter, dependency| has_path_connecting(graph, waiter, dependency, Some(&mut *space));
|
||||
let mut children: Vec<&str> = candidates
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|child| {
|
||||
!candidates.iter().any(|sibling| {
|
||||
sibling != child && waits_on(*sibling, *child) && !waits_on(*child, *sibling)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
self.sort_siblings(&mut children);
|
||||
visited.extend(children.iter().copied());
|
||||
TreeNode {
|
||||
row,
|
||||
children: children
|
||||
.into_iter()
|
||||
.map(|child| {
|
||||
visited.remove(child);
|
||||
self.grow(child, graph, space, visited)
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_milestone(&self, unit: &str) -> bool {
|
||||
self.milestone_targets.iter().any(|target| target == unit)
|
||||
}
|
||||
|
||||
fn waits_for<'a>(&'a self, waiter: &str, graph: &WaitingGraph) -> Vec<&'a str> {
|
||||
let Some(record) = self.units.get(waiter) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let pending_job = |dependency: &&'a str| graph.contains_node(dependency);
|
||||
let taking_down = |dependency: &&'a str| self.units[*dependency].takes_down();
|
||||
let after = record.facts.after.iter().map(String::as_str);
|
||||
let before = record.facts.before.iter().map(String::as_str);
|
||||
if record.takes_down() {
|
||||
before.filter(pending_job).filter(taking_down).collect()
|
||||
} else {
|
||||
after
|
||||
.filter(pending_job)
|
||||
.chain(before.filter(pending_job).filter(taking_down))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
fn sort_siblings(&self, names: &mut [&str]) {
|
||||
let rank = |name: &str| {
|
||||
let record = &self.units[name];
|
||||
match record.progress {
|
||||
Progress::Pending(_) => record
|
||||
.running_since()
|
||||
.map_or(SiblingRank::Waiting, SiblingRank::RunningSince),
|
||||
Progress::Finished(Outcome::Succeeded { at }) => {
|
||||
SiblingRank::RunningSince(record.inactive_exit.unwrap_or(at))
|
||||
}
|
||||
Progress::Finished(Outcome::Failed) => SiblingRank::Failed,
|
||||
Progress::Finished(Outcome::Abandoned) | Progress::Untouched => {
|
||||
SiblingRank::Waiting
|
||||
}
|
||||
}
|
||||
};
|
||||
names.sort_by(|left, right| rank(left).cmp(&rank(right)).then_with(|| left.cmp(right)));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
enum SiblingRank {
|
||||
RunningSince(Duration),
|
||||
Failed,
|
||||
Waiting,
|
||||
}
|
||||
|
||||
struct ReachedMilestone<'a> {
|
||||
target: &'a str,
|
||||
reached_at: Duration,
|
||||
}
|
||||
|
||||
struct MilestoneWalk {
|
||||
rows: Vec<MilestoneRow>,
|
||||
previous: Duration,
|
||||
consumed: usize,
|
||||
}
|
||||
|
||||
impl MilestoneWalk {
|
||||
fn advance(mut self, reached: &ReachedMilestone, succeeded_sorted: &[Duration]) -> Self {
|
||||
let upto = succeeded_sorted.partition_point(|at| *at <= reached.reached_at);
|
||||
self.rows.push(MilestoneRow {
|
||||
label: reached.target.to_owned(),
|
||||
finished_units: upto.saturating_sub(self.consumed),
|
||||
started_at: self.previous.min(reached.reached_at),
|
||||
reached_at: reached.reached_at,
|
||||
});
|
||||
self.consumed = self.consumed.max(upto);
|
||||
self.previous = reached.reached_at;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TreeNode {
|
||||
pub row: TreeRow,
|
||||
pub children: Vec<TreeNode>,
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
fn flatten_into(self, depth: usize, rows: &mut Vec<TreeRow>) {
|
||||
rows.push(TreeRow { depth, ..self.row });
|
||||
self.children
|
||||
.into_iter()
|
||||
.for_each(|child| child.flatten_into(depth + 1, rows));
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn flatten_forest(forest: Vec<TreeNode>) -> Vec<TreeRow> {
|
||||
let mut rows = Vec::new();
|
||||
forest
|
||||
.into_iter()
|
||||
.for_each(|root| root.flatten_into(0, &mut rows));
|
||||
rows
|
||||
}
|
||||
|
||||
fn labelled_by_description(facts: &UnitFacts) -> bool {
|
||||
!facts.description.is_empty() && UnitKind::of(&facts.name).labelled_by_description()
|
||||
}
|
||||
|
||||
fn display_label(facts: &UnitFacts) -> String {
|
||||
if labelled_by_description(facts) {
|
||||
return facts.description.clone();
|
||||
}
|
||||
match UnitKind::of(&facts.name) {
|
||||
UnitKind::Target | UnitKind::Other => facts.name.clone(),
|
||||
_ => facts
|
||||
.name
|
||||
.rsplit_once('.')
|
||||
.map_or_else(|| facts.name.clone(), |(stem, _)| stem.to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
fn summary(facts: &UnitFacts) -> String {
|
||||
let repeats_the_name = facts.description == facts.name
|
||||
|| facts
|
||||
.description
|
||||
.eq_ignore_ascii_case(&display_label(facts));
|
||||
if labelled_by_description(facts) || repeats_the_name {
|
||||
return String::new();
|
||||
}
|
||||
facts.description.clone()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::boot_test_fixtures::{
|
||||
MS, activating, facts, nested_boot_sample as sample, queue_unit, queued_boot, tree_outline,
|
||||
waiting,
|
||||
};
|
||||
|
||||
fn rows(state: &BootState) -> Vec<TreeRow> {
|
||||
flatten_forest(state.waiting_forest(Duration::MAX))
|
||||
}
|
||||
|
||||
fn outline(state: &BootState) -> Vec<String> {
|
||||
tree_outline(&rows(state))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_units_nest_under_what_waits_on_them() {
|
||||
assert_eq!(
|
||||
outline(&sample()),
|
||||
[
|
||||
"basic.target",
|
||||
"multi-user.target",
|
||||
" docker",
|
||||
" containerd",
|
||||
" nm-wait",
|
||||
" nm",
|
||||
"graphical.target",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_unit_sits_in_the_earliest_stage_that_waits_on_it() {
|
||||
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(&state),
|
||||
["basic.target", " dbus", "multi-user.target", " web"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kinds_with_a_tag_drop_their_suffix() {
|
||||
let socket = facts("docker.socket", &[]);
|
||||
assert_eq!(UnitKind::of(&socket.name), UnitKind::Socket);
|
||||
assert_eq!(UnitKind::Socket.tag(), "Socket");
|
||||
assert_eq!(display_label(&socket), "docker");
|
||||
assert_eq!(display_label(&facts("sshd.service", &[])), "sshd");
|
||||
assert_eq!(display_label(&facts("basic.target", &[])), "basic.target");
|
||||
assert_eq!(UnitKind::Target.tag(), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn descriptions_that_repeat_the_label_are_dropped() {
|
||||
let described = |name: &str, description: &str| {
|
||||
summary(&waiting(name, &[]).described(description).facts)
|
||||
};
|
||||
assert_eq!(
|
||||
described("multi-user.target", "Multi-User System"),
|
||||
"Multi-User System"
|
||||
);
|
||||
assert_eq!(described("greetd.service", "greetd.service"), "");
|
||||
assert_eq!(described("greetd.service", "Greetd"), "");
|
||||
assert_eq!(described("dev-sda.device", "Samsung SSD"), "");
|
||||
assert_eq!(described("demo.service", ""), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finished_jobs_leave_the_tree_and_count_toward_their_milestone() {
|
||||
let mut state = sample();
|
||||
state.finish_job(6, JobResult::Done, 3500 * MS);
|
||||
state.finish_job(7, JobResult::Done, 4000 * MS);
|
||||
state.update_activity(
|
||||
"basic.target",
|
||||
Activity {
|
||||
state: Some(ActiveState::Active),
|
||||
inactive_exit: None,
|
||||
active_enter: Some(4000 * MS),
|
||||
active_exit: None,
|
||||
},
|
||||
);
|
||||
assert!(
|
||||
rows(&state)
|
||||
.iter()
|
||||
.all(|row| row.label != "containerd" && row.label != "basic.target")
|
||||
);
|
||||
let milestones = state.milestones();
|
||||
assert_eq!(milestones.len(), 1);
|
||||
assert_eq!(milestones[0].label, "basic.target");
|
||||
assert_eq!(milestones[0].finished_units, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failures_are_listed_and_not_counted_as_done() {
|
||||
let mut state = sample();
|
||||
state.finish_job(4, JobResult::Failed, 2000 * MS);
|
||||
assert_eq!(state.failed, ["nm"]);
|
||||
let tally = state.tally();
|
||||
assert_eq!(tally.failed, 1);
|
||||
assert_eq!(tally.done, 0);
|
||||
assert_eq!(tally.running, 1);
|
||||
assert_eq!(tally.waiting, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boot_completes_when_the_root_job_is_gone() {
|
||||
let mut state = sample();
|
||||
assert!(!state.is_complete());
|
||||
(1..=7).for_each(|id| state.finish_job(id, JobResult::Done, 5000 * MS));
|
||||
assert!(state.is_complete());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn display_manager_activation_hands_the_console_over() {
|
||||
let mut state = sample();
|
||||
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());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn display_manager_started_in_one_step_hands_the_console_over() {
|
||||
let mut state = sample();
|
||||
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(
|
||||
HANDOFF_UNIT,
|
||||
Activity {
|
||||
state: Some(ActiveState::Active),
|
||||
inactive_exit: Some(6000 * MS),
|
||||
active_enter: Some(6000 * MS),
|
||||
active_exit: None,
|
||||
},
|
||||
);
|
||||
assert!(state.is_complete());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn switching_root_freezes_the_initrd_summary() {
|
||||
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());
|
||||
state.switch_root(2000 * MS);
|
||||
assert_eq!(state.phase, Phase::Host);
|
||||
assert_eq!(
|
||||
state.finished_stages,
|
||||
[FinishedStage {
|
||||
label: "Initrd",
|
||||
finished_units: 2,
|
||||
failed_units: 0,
|
||||
started_at: Duration::ZERO,
|
||||
reached_at: 2000 * MS,
|
||||
}]
|
||||
);
|
||||
assert_eq!(state.milestones()[0].reached_at, 2000 * MS);
|
||||
assert_eq!(state.tally().done, 2);
|
||||
assert!(rows(&state).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn devices_show_their_description() {
|
||||
let state = queued_boot(
|
||||
Phase::Host,
|
||||
Duration::ZERO,
|
||||
[waiting("dev-nvme1n1.device", &[]).described("Lexar SSD NM790 2TB")],
|
||||
);
|
||||
assert_eq!(rows(&state)[0].label, "Lexar SSD NM790 2TB");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn units_nest_under_the_deepest_pending_waiter() {
|
||||
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 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),
|
||||
[
|
||||
"shutdown.target",
|
||||
"poweroff.target",
|
||||
"network.target",
|
||||
" web",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn starting_units_wait_on_stops_in_either_direction() {
|
||||
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"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stopped_units_join_the_timeline_from_when_they_began_stopping() {
|
||||
let mut state = BootState::new(Phase::Shutdown);
|
||||
state.origin = 1000 * MS;
|
||||
state.track_unit(facts("db.service", &[]), Activity::default());
|
||||
state.queue_job(1, "db.service", JobKind::TakesDown, 1000 * MS);
|
||||
state.update_activity(
|
||||
"db.service",
|
||||
Activity {
|
||||
state: Some(ActiveState::Deactivating),
|
||||
active_exit: Some(1200 * MS),
|
||||
..Activity::default()
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
rows(&state)[0].status,
|
||||
RowStatus::Running {
|
||||
queued_at: 1000 * MS,
|
||||
since: 1200 * MS
|
||||
}
|
||||
);
|
||||
state.finish_job(1, JobResult::Done, 1700 * MS);
|
||||
assert_eq!(
|
||||
state.finished_runs,
|
||||
[FinishedRun {
|
||||
label: "db".to_owned(),
|
||||
started_at: 1200 * MS,
|
||||
finished_at: 1700 * MS,
|
||||
failed: false,
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shutdown_counts_toward_its_own_milestones_and_never_completes() {
|
||||
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);
|
||||
let milestones = state.milestones();
|
||||
assert_eq!(milestones[0].label, "shutdown.target");
|
||||
assert_eq!(milestones[0].finished_units, 2);
|
||||
assert!(!state.is_complete());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ordering_cycles_do_not_repeat_units() {
|
||||
let state = queued_boot(
|
||||
Phase::Host,
|
||||
Duration::ZERO,
|
||||
[
|
||||
waiting("a.service", &["b.service"]),
|
||||
waiting("b.service", &["a.service"]),
|
||||
],
|
||||
);
|
||||
assert_eq!(rows(&state).len(), 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,390 @@
|
||||
use std::{collections::HashMap, time::Duration};
|
||||
|
||||
use super::{
|
||||
ActiveState, Activity, BootState, FinishedRun, FinishedStage, HANDOFF_UNIT, HOST_MILESTONES,
|
||||
JobKind, JobResult, Outcome, PendingJob, Phase, Progress, Root, SHUTDOWN_MILESTONES, Tally,
|
||||
UnitFacts, UnitKind, UnitRecord, unit_facts::display_label,
|
||||
};
|
||||
|
||||
impl UnitRecord {
|
||||
fn apply(&mut self, activity: Activity) {
|
||||
if let Some(state) = activity.state {
|
||||
self.state = state;
|
||||
}
|
||||
if activity.inactive_exit.is_some() {
|
||||
self.inactive_exit = activity.inactive_exit;
|
||||
}
|
||||
if activity.active_enter.is_some() {
|
||||
self.active_enter = activity.active_enter;
|
||||
}
|
||||
if activity.active_exit.is_some() {
|
||||
self.active_exit = activity.active_exit;
|
||||
}
|
||||
}
|
||||
|
||||
fn transition_started(&self, kind: JobKind) -> Option<Duration> {
|
||||
match kind {
|
||||
JobKind::TakesDown => self.active_exit,
|
||||
JobKind::BringsUp | JobKind::Unknown => self.inactive_exit,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn running_since(&self) -> Option<Duration> {
|
||||
let Progress::Pending(job) = self.progress else {
|
||||
return None;
|
||||
};
|
||||
let transitioning = matches!(
|
||||
self.state,
|
||||
ActiveState::Activating | ActiveState::Deactivating | ActiveState::Reloading
|
||||
);
|
||||
transitioning.then(|| self.transition_started(job.kind).unwrap_or(job.queued_at))
|
||||
}
|
||||
|
||||
fn is_pending(&self) -> bool {
|
||||
matches!(self.progress, Progress::Pending(_))
|
||||
}
|
||||
|
||||
fn is_finished(&self) -> bool {
|
||||
matches!(self.progress, Progress::Finished(_))
|
||||
}
|
||||
|
||||
pub(super) fn takes_down(&self) -> bool {
|
||||
matches!(
|
||||
self.progress,
|
||||
Progress::Pending(PendingJob {
|
||||
kind: JobKind::TakesDown,
|
||||
..
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl BootState {
|
||||
#[must_use]
|
||||
pub fn new(phase: Phase) -> Self {
|
||||
let (milestone_targets, handoff_unit) = match phase {
|
||||
Phase::Initrd | Phase::Host => (
|
||||
HOST_MILESTONES.iter().map(ToString::to_string).collect(),
|
||||
Some(HANDOFF_UNIT.to_owned()),
|
||||
),
|
||||
Phase::Shutdown => (
|
||||
SHUTDOWN_MILESTONES
|
||||
.iter()
|
||||
.map(ToString::to_string)
|
||||
.collect(),
|
||||
None,
|
||||
),
|
||||
Phase::User => (Vec::new(), None),
|
||||
};
|
||||
Self {
|
||||
phase,
|
||||
origin: Duration::ZERO,
|
||||
root: Root::Unresolved,
|
||||
failed: Vec::new(),
|
||||
finished_stages: Vec::new(),
|
||||
finished_runs: Vec::new(),
|
||||
handoff_unit,
|
||||
manager_exited_at: None,
|
||||
milestone_targets,
|
||||
finished_before_reconnect: 0,
|
||||
units: HashMap::new(),
|
||||
jobs: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_root(&mut self, root: String) {
|
||||
if self.phase == Phase::User && !self.milestone_targets.contains(&root) {
|
||||
self.milestone_targets.push(root.clone());
|
||||
}
|
||||
if self.root.unit() != Some(&root) {
|
||||
self.root = Root::Awaiting(root);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_handoff(&mut self, unit: String) {
|
||||
if self.handoff_unit.is_some() {
|
||||
self.handoff_unit = Some(unit);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn track_unit(&mut self, facts: UnitFacts, activity: Activity) {
|
||||
let record = self
|
||||
.units
|
||||
.entry(facts.name.clone())
|
||||
.or_insert_with(|| UnitRecord {
|
||||
facts: facts.clone(),
|
||||
state: ActiveState::Inactive,
|
||||
inactive_exit: None,
|
||||
active_enter: None,
|
||||
active_exit: None,
|
||||
progress: Progress::Untouched,
|
||||
});
|
||||
record.facts = facts;
|
||||
record.apply(activity);
|
||||
}
|
||||
|
||||
pub fn update_activity(&mut self, unit: &str, activity: Activity) {
|
||||
if let Some(record) = self.units.get_mut(unit) {
|
||||
record.apply(activity);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn queue_job(&mut self, id: u32, unit: &str, kind: JobKind, at: Duration) {
|
||||
self.enqueue(id, unit, kind, at);
|
||||
if kind == JobKind::BringsUp
|
||||
&& self.jobs.get(&id).is_some_and(|tracked| tracked == unit)
|
||||
&& self.root == Root::Awaiting(unit.to_owned())
|
||||
{
|
||||
self.root = Root::Queued(unit.to_owned());
|
||||
}
|
||||
}
|
||||
|
||||
fn enqueue(&mut self, id: u32, unit: &str, kind: JobKind, at: Duration) {
|
||||
if self.jobs.contains_key(&id) {
|
||||
return;
|
||||
}
|
||||
let Some(record) = self.units.get_mut(unit) else {
|
||||
return;
|
||||
};
|
||||
record.progress = Progress::Pending(PendingJob {
|
||||
id,
|
||||
queued_at: at,
|
||||
kind,
|
||||
});
|
||||
self.jobs.insert(id, unit.to_owned());
|
||||
}
|
||||
|
||||
pub fn finish_job(&mut self, id: u32, result: JobResult, at: Duration) {
|
||||
let Some(unit) = self.jobs.remove(&id) else {
|
||||
return;
|
||||
};
|
||||
let Some(record) = self.units.get_mut(&unit) else {
|
||||
return;
|
||||
};
|
||||
let outcome = match result {
|
||||
JobResult::Done => Outcome::Succeeded { at },
|
||||
JobResult::Skipped | JobResult::Canceled => Outcome::Abandoned,
|
||||
JobResult::Failed | JobResult::Timeout | JobResult::Dependency => Outcome::Failed,
|
||||
};
|
||||
let label = display_label(&record.facts);
|
||||
if outcome == Outcome::Failed && !self.failed.contains(&label) {
|
||||
self.failed.push(label.clone());
|
||||
}
|
||||
let job = match record.progress {
|
||||
Progress::Pending(job) if job.id == id => job,
|
||||
_ => return,
|
||||
};
|
||||
record.progress = Progress::Finished(outcome);
|
||||
if let Some(started_at) = record
|
||||
.transition_started(job.kind)
|
||||
.filter(|started| job.kind != JobKind::Unknown && (self.origin..=at).contains(started))
|
||||
&& UnitKind::of(&record.facts.name) != UnitKind::Target
|
||||
{
|
||||
self.finished_runs.push(FinishedRun {
|
||||
label,
|
||||
started_at,
|
||||
finished_at: at,
|
||||
failed: outcome == Outcome::Failed,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn switch_root(&mut self, at: Duration) {
|
||||
let finished_here = self
|
||||
.units
|
||||
.values()
|
||||
.filter(|record| record.is_finished())
|
||||
.count();
|
||||
self.finished_stages.push(FinishedStage {
|
||||
label: Phase::Initrd.label(),
|
||||
finished_units: finished_here + self.finished_before_reconnect,
|
||||
failed_units: self
|
||||
.units
|
||||
.values()
|
||||
.filter(|record| record.progress == Progress::Finished(Outcome::Failed))
|
||||
.count(),
|
||||
started_at: self.origin,
|
||||
reached_at: at,
|
||||
});
|
||||
self.phase = Phase::Host;
|
||||
self.root = Root::Unresolved;
|
||||
self.finished_before_reconnect = 0;
|
||||
self.units.clear();
|
||||
self.jobs.clear();
|
||||
}
|
||||
|
||||
pub fn forget_jobs(&mut self) {
|
||||
self.finished_before_reconnect += self
|
||||
.units
|
||||
.values()
|
||||
.filter(|record| record.is_finished())
|
||||
.count();
|
||||
self.jobs.clear();
|
||||
self.units
|
||||
.values_mut()
|
||||
.for_each(|record| record.progress = Progress::Untouched);
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_complete(&self) -> bool {
|
||||
if matches!(self.phase, Phase::Initrd | Phase::Shutdown) {
|
||||
return false;
|
||||
}
|
||||
if self
|
||||
.handoff_unit
|
||||
.as_deref()
|
||||
.and_then(|unit| self.units.get(unit))
|
||||
.is_some_and(|handoff| handoff.state != ActiveState::Inactive)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
let Root::Queued(root) = &self.root else {
|
||||
return false;
|
||||
};
|
||||
self.units.get(root).is_some_and(|root| !root.is_pending())
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn tally(&self) -> Tally {
|
||||
let carried = Tally {
|
||||
done: self
|
||||
.finished_stages
|
||||
.iter()
|
||||
.map(|stage| stage.finished_units)
|
||||
.sum::<usize>()
|
||||
+ self.finished_before_reconnect,
|
||||
failed: self.failed.len(),
|
||||
..Tally::default()
|
||||
};
|
||||
self.units.values().fold(carried, |mut tally, record| {
|
||||
match record.progress {
|
||||
Progress::Pending(_) if record.running_since().is_some() => tally.running += 1,
|
||||
Progress::Pending(_) => tally.waiting += 1,
|
||||
Progress::Finished(Outcome::Succeeded { .. } | Outcome::Abandoned) => {
|
||||
tally.done += 1;
|
||||
}
|
||||
Progress::Finished(Outcome::Failed) | Progress::Untouched => {}
|
||||
}
|
||||
tally
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
boot_model::RowStatus,
|
||||
boot_test_fixtures::{
|
||||
MS, activating, facts, nested_boot_sample as sample, queue_unit, queued_boot, waiting,
|
||||
waiting_rows,
|
||||
},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn failures_are_listed_and_not_counted_as_done() {
|
||||
let mut state = sample();
|
||||
state.finish_job(4, JobResult::Failed, 2000 * MS);
|
||||
assert_eq!(state.failed, ["nm"]);
|
||||
let tally = state.tally();
|
||||
assert_eq!(tally.failed, 1);
|
||||
assert_eq!(tally.done, 0);
|
||||
assert_eq!(tally.running, 1);
|
||||
assert_eq!(tally.waiting, 5);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boot_completes_when_the_root_job_is_gone() {
|
||||
let mut state = sample();
|
||||
assert!(!state.is_complete());
|
||||
(1..=7).for_each(|id| state.finish_job(id, JobResult::Done, 5000 * MS));
|
||||
assert!(state.is_complete());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn display_manager_activation_hands_the_console_over() {
|
||||
let mut state = sample();
|
||||
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());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn display_manager_started_in_one_step_hands_the_console_over() {
|
||||
let mut state = sample();
|
||||
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(
|
||||
HANDOFF_UNIT,
|
||||
Activity {
|
||||
state: Some(ActiveState::Active),
|
||||
inactive_exit: Some(6000 * MS),
|
||||
active_enter: Some(6000 * MS),
|
||||
active_exit: None,
|
||||
},
|
||||
);
|
||||
assert!(state.is_complete());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn switching_root_freezes_the_initrd_summary() {
|
||||
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());
|
||||
state.switch_root(2000 * MS);
|
||||
assert_eq!(state.phase, Phase::Host);
|
||||
assert_eq!(
|
||||
state.finished_stages,
|
||||
[FinishedStage {
|
||||
label: "Initrd",
|
||||
finished_units: 2,
|
||||
failed_units: 0,
|
||||
started_at: Duration::ZERO,
|
||||
reached_at: 2000 * MS,
|
||||
}]
|
||||
);
|
||||
assert_eq!(state.milestones()[0].reached_at, 2000 * MS);
|
||||
assert_eq!(state.tally().done, 2);
|
||||
assert!(waiting_rows(&state).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stopped_units_join_the_timeline_from_when_they_began_stopping() {
|
||||
let mut state = BootState::new(Phase::Shutdown);
|
||||
state.origin = 1000 * MS;
|
||||
state.track_unit(facts("db.service", &[]), Activity::default());
|
||||
state.queue_job(1, "db.service", JobKind::TakesDown, 1000 * MS);
|
||||
state.update_activity(
|
||||
"db.service",
|
||||
Activity {
|
||||
state: Some(ActiveState::Deactivating),
|
||||
active_exit: Some(1200 * MS),
|
||||
..Activity::default()
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
waiting_rows(&state)[0].status,
|
||||
RowStatus::Running {
|
||||
queued_at: 1000 * MS,
|
||||
since: 1200 * MS
|
||||
}
|
||||
);
|
||||
state.finish_job(1, JobResult::Done, 1700 * MS);
|
||||
assert_eq!(
|
||||
state.finished_runs,
|
||||
[FinishedRun {
|
||||
label: "db".to_owned(),
|
||||
started_at: 1200 * MS,
|
||||
finished_at: 1700 * MS,
|
||||
failed: false,
|
||||
}]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use super::{ActiveState, BootState, Outcome, Progress};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct MilestoneRow {
|
||||
pub label: String,
|
||||
pub finished_units: usize,
|
||||
pub started_at: Duration,
|
||||
pub reached_at: Duration,
|
||||
}
|
||||
|
||||
struct ReachedMilestone<'a> {
|
||||
target: &'a str,
|
||||
reached_at: Duration,
|
||||
}
|
||||
|
||||
struct MilestoneWalk {
|
||||
rows: Vec<MilestoneRow>,
|
||||
previous: Duration,
|
||||
consumed: usize,
|
||||
}
|
||||
|
||||
impl MilestoneWalk {
|
||||
fn advance(mut self, reached: &ReachedMilestone, succeeded_sorted: &[Duration]) -> Self {
|
||||
let upto = succeeded_sorted.partition_point(|at| *at <= reached.reached_at);
|
||||
self.rows.push(MilestoneRow {
|
||||
label: reached.target.to_owned(),
|
||||
finished_units: upto.saturating_sub(self.consumed),
|
||||
started_at: self.previous.min(reached.reached_at),
|
||||
reached_at: reached.reached_at,
|
||||
});
|
||||
self.consumed = self.consumed.max(upto);
|
||||
self.previous = reached.reached_at;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl BootState {
|
||||
fn milestone_reached_at(&self, target: &str) -> Option<Duration> {
|
||||
let record = self.units.get(target)?;
|
||||
match record.progress {
|
||||
Progress::Finished(Outcome::Succeeded { at }) => Some(at),
|
||||
Progress::Untouched => record
|
||||
.active_enter
|
||||
.filter(|entered| record.state == ActiveState::Active && *entered >= self.origin),
|
||||
Progress::Pending(_) | Progress::Finished(Outcome::Failed | Outcome::Abandoned) => None,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn milestones(&self) -> Vec<MilestoneRow> {
|
||||
let mut succeeded: Vec<Duration> = self
|
||||
.units
|
||||
.values()
|
||||
.filter_map(|record| match record.progress {
|
||||
Progress::Finished(Outcome::Succeeded { at }) => Some(at),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
succeeded.sort_unstable();
|
||||
let start = MilestoneWalk {
|
||||
rows: self
|
||||
.finished_stages
|
||||
.iter()
|
||||
.map(|stage| MilestoneRow {
|
||||
label: stage.label.to_owned(),
|
||||
finished_units: stage.finished_units,
|
||||
started_at: stage.started_at,
|
||||
reached_at: stage.reached_at,
|
||||
})
|
||||
.collect(),
|
||||
previous: self
|
||||
.finished_stages
|
||||
.last()
|
||||
.map_or(self.origin, |stage| stage.reached_at),
|
||||
consumed: 0,
|
||||
};
|
||||
self.milestone_targets
|
||||
.iter()
|
||||
.filter_map(|target| {
|
||||
self.milestone_reached_at(target)
|
||||
.map(|reached_at| ReachedMilestone { target, reached_at })
|
||||
})
|
||||
.fold(start, |walk, reached| walk.advance(&reached, &succeeded))
|
||||
.rows
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
boot_model::{Activity, JobResult, Phase},
|
||||
boot_test_fixtures::{MS, nested_boot_sample, queued_boot, waiting, waiting_rows},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn finished_jobs_leave_the_tree_and_count_toward_their_milestone() {
|
||||
let mut state = nested_boot_sample();
|
||||
state.finish_job(6, JobResult::Done, 3500 * MS);
|
||||
state.finish_job(7, JobResult::Done, 4000 * MS);
|
||||
state.update_activity(
|
||||
"basic.target",
|
||||
Activity {
|
||||
state: Some(ActiveState::Active),
|
||||
inactive_exit: None,
|
||||
active_enter: Some(4000 * MS),
|
||||
active_exit: None,
|
||||
},
|
||||
);
|
||||
assert!(
|
||||
waiting_rows(&state)
|
||||
.iter()
|
||||
.all(|row| row.label != "containerd" && row.label != "basic.target")
|
||||
);
|
||||
let milestones = state.milestones();
|
||||
assert_eq!(milestones.len(), 1);
|
||||
assert_eq!(milestones[0].label, "basic.target");
|
||||
assert_eq!(milestones[0].finished_units, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shutdown_counts_toward_its_own_milestones_and_never_completes() {
|
||||
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);
|
||||
let milestones = state.milestones();
|
||||
assert_eq!(milestones[0].label, "shutdown.target");
|
||||
assert_eq!(milestones[0].finished_units, 2);
|
||||
assert!(!state.is_complete());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ActiveState {
|
||||
Inactive,
|
||||
Activating,
|
||||
Active,
|
||||
Deactivating,
|
||||
Reloading,
|
||||
Failed,
|
||||
}
|
||||
|
||||
impl ActiveState {
|
||||
#[must_use]
|
||||
pub fn parse(raw: &str) -> Self {
|
||||
match raw {
|
||||
"activating" => Self::Activating,
|
||||
"active" => Self::Active,
|
||||
"deactivating" => Self::Deactivating,
|
||||
"reloading" => Self::Reloading,
|
||||
"failed" => Self::Failed,
|
||||
_ => Self::Inactive,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum JobResult {
|
||||
Done,
|
||||
Skipped,
|
||||
Canceled,
|
||||
Failed,
|
||||
Timeout,
|
||||
Dependency,
|
||||
}
|
||||
|
||||
impl JobResult {
|
||||
#[must_use]
|
||||
pub fn parse(raw: &str) -> Self {
|
||||
match raw {
|
||||
"done" => Self::Done,
|
||||
"skipped" => Self::Skipped,
|
||||
"canceled" => Self::Canceled,
|
||||
"timeout" => Self::Timeout,
|
||||
"dependency" => Self::Dependency,
|
||||
_ => Self::Failed,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct UnitFacts {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub after: Vec<String>,
|
||||
pub before: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct Activity {
|
||||
pub state: Option<ActiveState>,
|
||||
pub inactive_exit: Option<Duration>,
|
||||
pub active_enter: Option<Duration>,
|
||||
pub active_exit: Option<Duration>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum JobKind {
|
||||
BringsUp,
|
||||
TakesDown,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum UnitKind {
|
||||
Service,
|
||||
Target,
|
||||
Socket,
|
||||
Mount,
|
||||
Automount,
|
||||
Device,
|
||||
Swap,
|
||||
Path,
|
||||
Timer,
|
||||
Slice,
|
||||
Scope,
|
||||
Other,
|
||||
}
|
||||
|
||||
impl UnitKind {
|
||||
#[must_use]
|
||||
pub fn of(name: &str) -> Self {
|
||||
match name.rsplit_once('.').map(|(_, suffix)| suffix) {
|
||||
Some("service") => Self::Service,
|
||||
Some("target") => Self::Target,
|
||||
Some("socket") => Self::Socket,
|
||||
Some("mount") => Self::Mount,
|
||||
Some("automount") => Self::Automount,
|
||||
Some("device") => Self::Device,
|
||||
Some("swap") => Self::Swap,
|
||||
Some("path") => Self::Path,
|
||||
Some("timer") => Self::Timer,
|
||||
Some("slice") => Self::Slice,
|
||||
Some("scope") => Self::Scope,
|
||||
_ => Self::Other,
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn tag(self) -> &'static str {
|
||||
match self {
|
||||
Self::Service | Self::Target | Self::Other => "",
|
||||
Self::Socket => "Socket",
|
||||
Self::Mount => "Mount",
|
||||
Self::Automount => "Automount",
|
||||
Self::Device => "Device",
|
||||
Self::Swap => "Swap",
|
||||
Self::Path => "Path",
|
||||
Self::Timer => "Timer",
|
||||
Self::Slice => "Slice",
|
||||
Self::Scope => "Scope",
|
||||
}
|
||||
}
|
||||
|
||||
fn labelled_by_description(self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::Device | Self::Mount | Self::Swap | Self::Automount
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn labelled_by_description(facts: &UnitFacts) -> bool {
|
||||
!facts.description.is_empty() && UnitKind::of(&facts.name).labelled_by_description()
|
||||
}
|
||||
|
||||
pub(super) fn display_label(facts: &UnitFacts) -> String {
|
||||
if labelled_by_description(facts) {
|
||||
return facts.description.clone();
|
||||
}
|
||||
match UnitKind::of(&facts.name) {
|
||||
UnitKind::Target | UnitKind::Other => facts.name.clone(),
|
||||
_ => facts
|
||||
.name
|
||||
.rsplit_once('.')
|
||||
.map_or_else(|| facts.name.clone(), |(stem, _)| stem.to_owned()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn summary(facts: &UnitFacts) -> String {
|
||||
let repeats_the_name = facts.description == facts.name
|
||||
|| facts
|
||||
.description
|
||||
.eq_ignore_ascii_case(&display_label(facts));
|
||||
if labelled_by_description(facts) || repeats_the_name {
|
||||
return String::new();
|
||||
}
|
||||
facts.description.clone()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::boot_test_fixtures::{facts, waiting};
|
||||
|
||||
#[test]
|
||||
fn kinds_with_a_tag_drop_their_suffix() {
|
||||
let socket = facts("docker.socket", &[]);
|
||||
assert_eq!(UnitKind::of(&socket.name), UnitKind::Socket);
|
||||
assert_eq!(UnitKind::Socket.tag(), "Socket");
|
||||
assert_eq!(display_label(&socket), "docker");
|
||||
assert_eq!(display_label(&facts("sshd.service", &[])), "sshd");
|
||||
assert_eq!(display_label(&facts("basic.target", &[])), "basic.target");
|
||||
assert_eq!(UnitKind::Target.tag(), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn descriptions_that_repeat_the_label_are_dropped() {
|
||||
let described = |name: &str, description: &str| {
|
||||
summary(&waiting(name, &[]).described(description).facts)
|
||||
};
|
||||
assert_eq!(
|
||||
described("multi-user.target", "Multi-User System"),
|
||||
"Multi-User System"
|
||||
);
|
||||
assert_eq!(described("greetd.service", "greetd.service"), "");
|
||||
assert_eq!(described("greetd.service", "Greetd"), "");
|
||||
assert_eq!(described("dev-sda.device", "Samsung SSD"), "");
|
||||
assert_eq!(described("demo.service", ""), "");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,370 @@
|
||||
use std::{collections::HashSet, time::Duration};
|
||||
|
||||
use petgraph::{
|
||||
Direction::Incoming,
|
||||
algo::{DfsSpace, has_path_connecting},
|
||||
graphmap::DiGraphMap,
|
||||
visit::Visitable,
|
||||
};
|
||||
|
||||
use super::{
|
||||
BootState, Outcome, Phase, Progress, UnitKind, UnitRecord,
|
||||
unit_facts::{display_label, summary},
|
||||
};
|
||||
|
||||
type WaitingGraph<'a> = DiGraphMap<&'a str, ()>;
|
||||
type WaitingSearch<'a> = DfsSpace<&'a str, <WaitingGraph<'a> as Visitable>::Map>;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum RowStatus {
|
||||
Waiting,
|
||||
Running {
|
||||
queued_at: Duration,
|
||||
since: Duration,
|
||||
},
|
||||
Failed,
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct TreeRow {
|
||||
pub depth: usize,
|
||||
pub label: String,
|
||||
pub description: String,
|
||||
pub kind: UnitKind,
|
||||
pub status: RowStatus,
|
||||
pub chain: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct TreeNode {
|
||||
pub row: TreeRow,
|
||||
pub children: Vec<TreeNode>,
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
fn flatten_into(self, depth: usize, rows: &mut Vec<TreeRow>) {
|
||||
rows.push(TreeRow { depth, ..self.row });
|
||||
self.children
|
||||
.into_iter()
|
||||
.for_each(|child| child.flatten_into(depth + 1, rows));
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn flatten_forest(forest: Vec<TreeNode>) -> Vec<TreeRow> {
|
||||
let mut rows = Vec::new();
|
||||
forest
|
||||
.into_iter()
|
||||
.for_each(|root| root.flatten_into(0, &mut rows));
|
||||
rows
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
enum SiblingRank {
|
||||
RunningSince(Duration),
|
||||
Failed,
|
||||
Waiting,
|
||||
}
|
||||
|
||||
impl UnitRecord {
|
||||
fn row_status(&self) -> RowStatus {
|
||||
match self.progress {
|
||||
Progress::Pending(job) => {
|
||||
self.running_since()
|
||||
.map_or(RowStatus::Waiting, |since| RowStatus::Running {
|
||||
queued_at: job.queued_at,
|
||||
since,
|
||||
})
|
||||
}
|
||||
Progress::Finished(Outcome::Failed) => RowStatus::Failed,
|
||||
Progress::Finished(_) | Progress::Untouched => RowStatus::Done,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BootState {
|
||||
#[must_use]
|
||||
pub fn waiting_forest(&self, finished_since: Duration) -> Vec<TreeNode> {
|
||||
let mut graph = WaitingGraph::new();
|
||||
self.units
|
||||
.iter()
|
||||
.filter(|(name, record)| match record.progress {
|
||||
Progress::Pending(_) | Progress::Finished(Outcome::Failed) => true,
|
||||
Progress::Finished(Outcome::Succeeded { at }) => {
|
||||
at >= finished_since || self.is_milestone(name)
|
||||
}
|
||||
Progress::Finished(Outcome::Abandoned) => self.is_milestone(name),
|
||||
Progress::Untouched => false,
|
||||
})
|
||||
.for_each(|(name, _)| {
|
||||
graph.add_node(name.as_str());
|
||||
});
|
||||
let edges: Vec<(&str, &str)> = graph
|
||||
.nodes()
|
||||
.flat_map(|waiter| {
|
||||
self.waits_for(waiter, &graph)
|
||||
.into_iter()
|
||||
.map(move |dependency| (waiter, dependency))
|
||||
})
|
||||
.collect();
|
||||
graph.extend(edges);
|
||||
let root = self.root.unit().filter(|root| graph.contains_node(root));
|
||||
let stages: Vec<&str> = self
|
||||
.milestone_targets
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
.filter(|target| graph.contains_node(target))
|
||||
.chain(root.filter(|root| !self.is_milestone(root)))
|
||||
.collect();
|
||||
let mut unblocked: Vec<&str> = graph
|
||||
.nodes()
|
||||
.filter(|name| {
|
||||
graph.neighbors_directed(name, Incoming).next().is_none() && !stages.contains(name)
|
||||
})
|
||||
.collect();
|
||||
self.sort_siblings(&mut unblocked);
|
||||
let mut leftovers: Vec<&str> = graph.nodes().collect();
|
||||
self.sort_siblings(&mut leftovers);
|
||||
if self.phase == Phase::User {
|
||||
unblocked.clear();
|
||||
leftovers.clear();
|
||||
}
|
||||
let mut space = DfsSpace::new(&graph);
|
||||
let mut visited = HashSet::new();
|
||||
let mut forest = Vec::new();
|
||||
stages
|
||||
.iter()
|
||||
.copied()
|
||||
.chain(unblocked)
|
||||
.chain(leftovers)
|
||||
.for_each(|top| {
|
||||
if visited.contains(top) {
|
||||
return;
|
||||
}
|
||||
let stage = self.grow(top, &graph, &mut space, &mut visited);
|
||||
if stage.row.status != RowStatus::Done || !stage.children.is_empty() {
|
||||
forest.push(stage);
|
||||
}
|
||||
});
|
||||
forest
|
||||
}
|
||||
|
||||
fn grow<'a>(
|
||||
&'a self,
|
||||
unit: &'a str,
|
||||
graph: &WaitingGraph<'a>,
|
||||
space: &mut WaitingSearch<'a>,
|
||||
visited: &mut HashSet<&'a str>,
|
||||
) -> TreeNode {
|
||||
visited.insert(unit);
|
||||
let record = &self.units[unit];
|
||||
let row = TreeRow {
|
||||
depth: 0,
|
||||
label: display_label(&record.facts),
|
||||
description: summary(&record.facts),
|
||||
kind: UnitKind::of(&record.facts.name),
|
||||
status: record.row_status(),
|
||||
chain: Vec::new(),
|
||||
};
|
||||
let candidates: Vec<&str> = graph
|
||||
.neighbors(unit)
|
||||
.filter(|dependency| !visited.contains(dependency) && !self.is_milestone(dependency))
|
||||
.collect();
|
||||
let mut waits_on =
|
||||
|waiter, dependency| has_path_connecting(graph, waiter, dependency, Some(&mut *space));
|
||||
let mut children: Vec<&str> = candidates
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|child| {
|
||||
!candidates.iter().any(|sibling| {
|
||||
sibling != child && waits_on(*sibling, *child) && !waits_on(*child, *sibling)
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
self.sort_siblings(&mut children);
|
||||
visited.extend(children.iter().copied());
|
||||
TreeNode {
|
||||
row,
|
||||
children: children
|
||||
.into_iter()
|
||||
.map(|child| {
|
||||
visited.remove(child);
|
||||
self.grow(child, graph, space, visited)
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_milestone(&self, unit: &str) -> bool {
|
||||
self.milestone_targets.iter().any(|target| target == unit)
|
||||
}
|
||||
|
||||
fn waits_for<'a>(&'a self, waiter: &str, graph: &WaitingGraph) -> Vec<&'a str> {
|
||||
let Some(record) = self.units.get(waiter) else {
|
||||
return Vec::new();
|
||||
};
|
||||
let pending_job = |dependency: &&'a str| graph.contains_node(dependency);
|
||||
let taking_down = |dependency: &&'a str| self.units[*dependency].takes_down();
|
||||
let after = record.facts.after.iter().map(String::as_str);
|
||||
let before = record.facts.before.iter().map(String::as_str);
|
||||
if record.takes_down() {
|
||||
before.filter(pending_job).filter(taking_down).collect()
|
||||
} else {
|
||||
after
|
||||
.filter(pending_job)
|
||||
.chain(before.filter(pending_job).filter(taking_down))
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
fn sort_siblings(&self, names: &mut [&str]) {
|
||||
let rank = |name: &str| {
|
||||
let record = &self.units[name];
|
||||
match record.progress {
|
||||
Progress::Pending(_) => record
|
||||
.running_since()
|
||||
.map_or(SiblingRank::Waiting, SiblingRank::RunningSince),
|
||||
Progress::Finished(Outcome::Succeeded { at }) => {
|
||||
SiblingRank::RunningSince(record.inactive_exit.unwrap_or(at))
|
||||
}
|
||||
Progress::Finished(Outcome::Failed) => SiblingRank::Failed,
|
||||
Progress::Finished(Outcome::Abandoned) | Progress::Untouched => {
|
||||
SiblingRank::Waiting
|
||||
}
|
||||
}
|
||||
};
|
||||
names.sort_by(|left, right| rank(left).cmp(&rank(right)).then_with(|| left.cmp(right)));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::boot_test_fixtures::{
|
||||
nested_boot_sample, queued_boot, tree_outline, waiting, waiting_rows,
|
||||
};
|
||||
|
||||
fn outline(state: &BootState) -> Vec<String> {
|
||||
tree_outline(&waiting_rows(state))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_units_nest_under_what_waits_on_them() {
|
||||
assert_eq!(
|
||||
outline(&nested_boot_sample()),
|
||||
[
|
||||
"basic.target",
|
||||
"multi-user.target",
|
||||
" docker",
|
||||
" containerd",
|
||||
" nm-wait",
|
||||
" nm",
|
||||
"graphical.target",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_unit_sits_in_the_earliest_stage_that_waits_on_it() {
|
||||
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(&state),
|
||||
["basic.target", " dbus", "multi-user.target", " web"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn devices_show_their_description() {
|
||||
let state = queued_boot(
|
||||
Phase::Host,
|
||||
Duration::ZERO,
|
||||
[waiting("dev-nvme1n1.device", &[]).described("Lexar SSD NM790 2TB")],
|
||||
);
|
||||
assert_eq!(waiting_rows(&state)[0].label, "Lexar SSD NM790 2TB");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn units_nest_under_the_deepest_pending_waiter() {
|
||||
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 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),
|
||||
[
|
||||
"shutdown.target",
|
||||
"poweroff.target",
|
||||
"network.target",
|
||||
" web",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn starting_units_wait_on_stops_in_either_direction() {
|
||||
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"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ordering_cycles_do_not_repeat_units() {
|
||||
let state = queued_boot(
|
||||
Phase::Host,
|
||||
Duration::ZERO,
|
||||
[
|
||||
waiting("a.service", &["b.service"]),
|
||||
waiting("b.service", &["a.service"]),
|
||||
],
|
||||
);
|
||||
assert_eq!(waiting_rows(&state).len(), 2);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
use std::{iter::once, time::Duration};
|
||||
|
||||
use crate::boot_model::{ActiveState, Activity, BootState, JobKind, Phase, TreeRow, UnitFacts};
|
||||
use crate::boot_model::{
|
||||
ActiveState, Activity, BootState, JobKind, Phase, TreeRow, UnitFacts, flatten_forest,
|
||||
};
|
||||
|
||||
pub const MS: Duration = Duration::from_millis(1);
|
||||
|
||||
@@ -101,6 +103,10 @@ pub fn queue_unit(state: &mut BootState, id: u32, unit: QueuedUnit, queued_at: D
|
||||
state.queue_job(id, &name, unit.kind, queued_at);
|
||||
}
|
||||
|
||||
pub fn waiting_rows(state: &BootState) -> Vec<TreeRow> {
|
||||
flatten_forest(state.waiting_forest(Duration::MAX))
|
||||
}
|
||||
|
||||
pub fn tree_outline(rows: &[TreeRow]) -> Vec<String> {
|
||||
rows.iter()
|
||||
.map(|row| {
|
||||
|
||||
+6
-604
@@ -1,7 +1,13 @@
|
||||
#[cfg(test)]
|
||||
mod panel_render_tests;
|
||||
mod run_timeline;
|
||||
mod screen_chrome;
|
||||
#[cfg(test)]
|
||||
mod screen_render_fixtures;
|
||||
mod stage_summary;
|
||||
mod tree_height_fitting;
|
||||
#[cfg(test)]
|
||||
mod tree_render_tests;
|
||||
mod unit_tree;
|
||||
|
||||
use std::time::Duration;
|
||||
@@ -233,550 +239,7 @@ fn unit_count(count: usize) -> String {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ratatui::{Terminal, backend::TestBackend};
|
||||
|
||||
use super::*;
|
||||
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<String> {
|
||||
screen_lines(&render_prompted(state, now, width, height, None))
|
||||
}
|
||||
|
||||
fn render_prompted(
|
||||
state: &BootState,
|
||||
now: Duration,
|
||||
width: u16,
|
||||
height: u16,
|
||||
password_prompt: Option<PasswordPrompt>,
|
||||
) -> Buffer {
|
||||
let mut terminal = Terminal::new(TestBackend::new(width, height)).expect("test backend");
|
||||
terminal
|
||||
.draw(|frame| {
|
||||
frame.render_widget(
|
||||
BootView {
|
||||
state,
|
||||
colors: &Colors::default(),
|
||||
timing: &Timing::default(),
|
||||
glyphs: &Glyphs::default(),
|
||||
now,
|
||||
origin: &BootOrigin {
|
||||
host: "ryzen".to_owned(),
|
||||
system: "NixOS 26.11".to_owned(),
|
||||
generation: "2771".to_owned(),
|
||||
kernel: "Linux 7.2.9".to_owned(),
|
||||
},
|
||||
frame: 1,
|
||||
password_prompt,
|
||||
},
|
||||
frame.area(),
|
||||
);
|
||||
})
|
||||
.expect("draw");
|
||||
terminal.backend().buffer().clone()
|
||||
}
|
||||
|
||||
fn screen_lines(buffer: &Buffer) -> Vec<String> {
|
||||
let area = buffer.area;
|
||||
(0..area.height)
|
||||
.map(|y| {
|
||||
let line: String = (0..area.width)
|
||||
.map(|x| buffer[(x, y)].symbol().to_owned())
|
||||
.collect();
|
||||
line.trim_end().to_owned()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn boot_in_progress() -> BootState {
|
||||
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"],
|
||||
),
|
||||
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);
|
||||
state
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tree_on_top_timeline_below() {
|
||||
let lines = render(&boot_in_progress(), 12_400 * MS, 110, 24);
|
||||
let screen = lines.join("\n");
|
||||
assert_eq!(
|
||||
lines[0], "ryzen · NixOS 26.11 · generation 2771 · Linux 7.2.9",
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines[1].starts_with("└─ Stage 2 12.4s"), "{screen}");
|
||||
assert!(lines[1].ends_with("Running"), "{screen}");
|
||||
assert!(
|
||||
lines[2].starts_with(" ├─ ◆ multi-user.target"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(
|
||||
lines[3].starts_with(" │ ├─ [/] systemd-rfkill"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines[4].starts_with(" │ ├─ [/] docker"), "{screen}");
|
||||
assert!(lines[5].starts_with(" │ └─ [ ] web"), "{screen}");
|
||||
assert!(lines[6].starts_with(" ├─ ◇ graphical.target"), "{screen}");
|
||||
assert!(lines[7].starts_with(" └─ [×] nvidia-powerd"), "{screen}");
|
||||
let timeline = lines
|
||||
.iter()
|
||||
.position(|line| line.contains(" Timeline "))
|
||||
.expect("timeline panel");
|
||||
assert!(
|
||||
lines[timeline + 1].starts_with(&format!("{:29}Kernel", "")),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(
|
||||
lines[timeline + 2].starts_with("√ sysinit.target (2 units)"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines[timeline + 3].starts_with("√ udev-settle"), "{screen}");
|
||||
assert!(lines[timeline + 3].contains('■'), "{screen}");
|
||||
assert!(
|
||||
lines[timeline + 4].starts_with("► systemd-rfkill"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(
|
||||
lines[timeline + 4].contains("·■") && lines[timeline + 4].ends_with('▶'),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines[timeline + 5].starts_with("► docker"), "{screen}");
|
||||
assert!(
|
||||
lines[timeline..].iter().all(|line| !line.contains(" web")),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(!screen.contains("root switch"), "{screen}");
|
||||
assert!(
|
||||
lines[23].starts_with("√ 2 done 2 running 3 waiting × 1 failed"),
|
||||
"{screen}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failures_stay_in_the_stage_that_wanted_them() {
|
||||
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);
|
||||
let lines = render(&state, 5000 * MS, 110, 24);
|
||||
let screen = lines.join("\n");
|
||||
assert!(lines[2].starts_with(" ├─ √ basic.target"), "{screen}");
|
||||
assert!(lines[3].starts_with(" │ └─ [×] flaky"), "{screen}");
|
||||
assert!(
|
||||
lines[4].starts_with(" └─ ◆ multi-user.target"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines[5].starts_with(" └─ [ ] web"), "{screen}");
|
||||
assert!(!screen.contains("quick"), "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finished_units_linger_in_place_before_leaving() {
|
||||
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");
|
||||
assert!(lingering[3].starts_with(" ├─ [√] early"), "{screen}");
|
||||
assert!(lingering[4].starts_with(" └─ [/] late"), "{screen}");
|
||||
let gone = render(&state, 5500 * MS, 110, 24);
|
||||
let tree: Vec<&String> = gone
|
||||
.iter()
|
||||
.take_while(|line| !line.contains(" Timeline "))
|
||||
.collect();
|
||||
assert!(
|
||||
tree.iter().all(|line| !line.contains("early")),
|
||||
"{}",
|
||||
gone.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_running_units_carry_a_timer() {
|
||||
let lines = render(&boot_in_progress(), 12_400 * MS, 110, 24);
|
||||
let screen = lines.join("\n");
|
||||
assert!(lines[2].ends_with("multi-user.target"), "{screen}");
|
||||
assert!(lines[3].ends_with("6.4s"), "{screen}");
|
||||
assert!(lines[5].ends_with("web"), "{screen}");
|
||||
}
|
||||
|
||||
const CRYPTSETUP_MESSAGE: &str =
|
||||
"Please enter passphrase for disk Samsung SSD 980 PRO 1TB (cryptroot):";
|
||||
|
||||
#[test]
|
||||
fn password_prompt_floats_over_a_dimmed_view() {
|
||||
let buffer = render_prompted(
|
||||
&boot_in_progress(),
|
||||
12_400 * MS,
|
||||
110,
|
||||
24,
|
||||
Some(PasswordPrompt {
|
||||
message: CRYPTSETUP_MESSAGE,
|
||||
input: "***".to_owned(),
|
||||
sent: false,
|
||||
}),
|
||||
);
|
||||
let lines = screen_lines(&buffer);
|
||||
let screen = lines.join("\n");
|
||||
let title_row = lines
|
||||
.iter()
|
||||
.position(|line| line.contains("┌ Password "))
|
||||
.unwrap_or_else(|| panic!("{screen}"));
|
||||
assert!(lines[title_row].starts_with(' '), "{screen}");
|
||||
assert!(
|
||||
lines[title_row + 1].contains(&format!("│ {CRYPTSETUP_MESSAGE}")),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines[title_row + 3].contains("│ > ***_"), "{screen}");
|
||||
assert!(lines[title_row + 4].contains("Enter unlock"), "{screen}");
|
||||
assert!(
|
||||
lines[23].starts_with("√ 2 done 2 running 3 waiting × 1 failed"),
|
||||
"{screen}"
|
||||
);
|
||||
assert_eq!(
|
||||
buffer[(0, 0)].fg,
|
||||
Colors::default().password.backdrop,
|
||||
"{screen}"
|
||||
);
|
||||
let corner = buffer
|
||||
.content
|
||||
.iter()
|
||||
.find(|cell| cell.symbol() == "┌")
|
||||
.unwrap_or_else(|| panic!("{screen}"));
|
||||
assert_eq!(corner.fg, Colors::default().password.frame, "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn answered_password_prompt_shows_it_is_checking() {
|
||||
let lines = screen_lines(&render_prompted(
|
||||
&boot_in_progress(),
|
||||
12_400 * MS,
|
||||
110,
|
||||
24,
|
||||
Some(PasswordPrompt {
|
||||
message: CRYPTSETUP_MESSAGE,
|
||||
input: String::new(),
|
||||
sent: true,
|
||||
}),
|
||||
));
|
||||
assert!(
|
||||
lines.iter().any(|line| line.contains("│ Checking…")),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_password_messages_wrap_inside_narrow_screens() {
|
||||
let lines = screen_lines(&render_prompted(
|
||||
&boot_in_progress(),
|
||||
12_400 * MS,
|
||||
50,
|
||||
24,
|
||||
Some(PasswordPrompt {
|
||||
message: CRYPTSETUP_MESSAGE,
|
||||
input: String::new(),
|
||||
sent: false,
|
||||
}),
|
||||
));
|
||||
let screen = lines.join("\n");
|
||||
assert!(
|
||||
lines.iter().any(|line| line.contains("(cryptroot):")),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines.iter().any(|line| line.contains("> _")), "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sub_second_runs_stay_off_the_timeline() {
|
||||
let mut state = boot_in_progress();
|
||||
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!(
|
||||
lines.iter().all(|line| !line.contains("blink")),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
fn chain_boot() -> BootState {
|
||||
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]
|
||||
fn chains_stay_unfolded_while_they_fit() {
|
||||
let lines = render(&chain_boot(), 5000 * MS, 110, 24);
|
||||
let screen = lines.join("\n");
|
||||
assert!(
|
||||
lines[3].starts_with(" └─ [ ] network-wait"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(
|
||||
lines[4].starts_with(" └─ network.target"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(
|
||||
lines[7].starts_with(" └─ [/] udev-settle"),
|
||||
"{screen}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chains_fold_when_short_and_shorten_to_their_ends_when_narrow() {
|
||||
let short = render(&chain_boot(), 5000 * MS, 110, 9);
|
||||
assert!(
|
||||
short[3]
|
||||
.starts_with(" └─ [ ] network-wait ─ network.target ─ network-manager ─ dbus"),
|
||||
"{}",
|
||||
short.join("\n")
|
||||
);
|
||||
assert!(
|
||||
short[4].starts_with(" └─ [/] udev-settle"),
|
||||
"{}",
|
||||
short.join("\n")
|
||||
);
|
||||
let narrow = render(&chain_boot(), 5000 * MS, 45, 8);
|
||||
assert!(
|
||||
narrow[3].starts_with(" └─ [ ] network-wait ─ … ─ dbus"),
|
||||
"{}",
|
||||
narrow.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
fn described_boot() -> BootState {
|
||||
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]
|
||||
fn descriptions_line_up_in_a_column() {
|
||||
let lines = render(&described_boot(), 3000 * MS, 110, 24);
|
||||
let screen = lines.join("\n");
|
||||
let column_of = |text: &str| {
|
||||
lines
|
||||
.iter()
|
||||
.find_map(|line| line.find(text).map(|at| line[..at].width()))
|
||||
.unwrap_or_else(|| panic!("{text} missing\n{screen}"))
|
||||
};
|
||||
assert_eq!(column_of("Multi-User System"), column_of("Description"));
|
||||
assert_eq!(
|
||||
column_of("Multi-User System"),
|
||||
column_of("Docker Application")
|
||||
);
|
||||
assert_eq!(
|
||||
column_of("Multi-User System"),
|
||||
column_of("Load/Save RF Kill")
|
||||
);
|
||||
assert!(column_of("Multi-User System") > column_of("systemd-rfkill"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_service_units_carry_their_kind() {
|
||||
let mut state = described_boot();
|
||||
queue_unit(
|
||||
&mut state,
|
||||
9,
|
||||
running("docker.socket", &[], 1000 * MS).described("Docker Socket for the API"),
|
||||
Duration::ZERO,
|
||||
);
|
||||
let lines = render(&state, 3000 * MS, 110, 24);
|
||||
let screen = lines.join("\n");
|
||||
let socket_row = lines
|
||||
.iter()
|
||||
.find(|line| line.contains("Docker Socket"))
|
||||
.unwrap_or_else(|| panic!("{screen}"));
|
||||
assert!(socket_row.contains("[/] docker "), "{screen}");
|
||||
assert!(socket_row.contains(" Socket "), "{screen}");
|
||||
assert!(
|
||||
render(&described_boot(), 3000 * MS, 110, 24)
|
||||
.iter()
|
||||
.all(|line| !line.contains(" Socket ")),
|
||||
"{screen}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn narrow_screens_keep_names_and_drop_descriptions() {
|
||||
let lines = render(&described_boot(), 3000 * MS, 40, 24);
|
||||
let screen = lines.join("\n");
|
||||
assert!(screen.contains("systemd-rfkill"), "{screen}");
|
||||
assert!(!screen.contains("Docker"), "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_timeline_labels_end_in_an_ellipsis() {
|
||||
let mut state = boot_in_progress();
|
||||
queue_unit(
|
||||
&mut state,
|
||||
30,
|
||||
running("sys-kernel-config.mount", &[], 2000 * MS)
|
||||
.described("Kernel Configuration File System"),
|
||||
2000 * MS,
|
||||
);
|
||||
state.finish_job(30, JobResult::Done, 4000 * MS);
|
||||
let lines = render(&state, 12_400 * MS, 110, 24);
|
||||
assert!(
|
||||
lines
|
||||
.iter()
|
||||
.any(|line| line.starts_with("√ Kernel Configuration File…")),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn narrow_screens_drop_the_timeline() {
|
||||
let lines = render(&boot_in_progress(), 12_400 * MS, 40, 24);
|
||||
assert!(
|
||||
lines.iter().all(|line| !line.contains('■')),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
assert!(lines[3].contains("systemd-rfkill"), "{}", lines.join("\n"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finished_stages_collapse_into_a_summary_rule() {
|
||||
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());
|
||||
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}");
|
||||
assert!(
|
||||
lines[1].starts_with("├─ Initrd ■ done in 7.2s · 1 unit"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines[2].starts_with("└─ Stage 2 10.1s"), "{screen}");
|
||||
assert!(lines[3].starts_with(" └─ ◆ graphical.target"), "{screen}");
|
||||
let timeline = lines
|
||||
.iter()
|
||||
.position(|line| line.contains(" Timeline "))
|
||||
.expect("timeline panel");
|
||||
let initrd_bar = &lines[timeline + 2];
|
||||
assert!(initrd_bar.starts_with("√ Initrd (1 unit)"), "{screen}");
|
||||
assert!(initrd_bar.ends_with("■│"), "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completion_announces_the_handover() {
|
||||
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!(
|
||||
lines[1].starts_with(
|
||||
"└─ ■ Boot finished in 9.5s · handing over to display-manager.service"
|
||||
),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shutdown_in_progress_names_its_stage() {
|
||||
let mut state = queued_boot(
|
||||
Phase::Shutdown,
|
||||
20_000 * MS,
|
||||
[waiting("poweroff.target", &[])],
|
||||
);
|
||||
state.origin = 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"),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shutdown_announces_the_handover_once_the_manager_exits() {
|
||||
let mut state = BootState::new(Phase::Shutdown);
|
||||
state.origin = 20_000 * MS;
|
||||
state.manager_exited_at = Some(24_200 * MS);
|
||||
let lines = render(&state, 30_000 * MS, 110, 24);
|
||||
assert!(
|
||||
lines[1]
|
||||
.starts_with("└─ ■ Shutdown finished in 4.2s · handing over to systemd-shutdown"),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_tree_takes_the_room_and_the_timeline_only_what_is_left() {
|
||||
@@ -789,65 +252,4 @@ mod tests {
|
||||
let heights = Heights::share(30, 12, 8);
|
||||
assert_eq!((heights.tree, heights.timeline), (22, 8));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_timeline_keeps_only_the_slowest_finished_runs() {
|
||||
let quick_units: Vec<String> = (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();
|
||||
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");
|
||||
let timeline = lines
|
||||
.iter()
|
||||
.position(|line| line.contains(" Timeline "))
|
||||
.expect("timeline panel");
|
||||
let (tree, panel) = lines.split_at(timeline);
|
||||
assert!(tree.iter().all(|line| !line.contains('…')), "{screen}");
|
||||
let shown: Vec<&str> = quick_units
|
||||
.iter()
|
||||
.map(|name| name.trim_end_matches(".service"))
|
||||
.filter(|label| panel.iter().any(|line| line.contains(label)))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
shown,
|
||||
["run05", "run06", "run07", "run08", "run09"],
|
||||
"{screen}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overflowing_trees_are_cut_with_a_count() {
|
||||
let names: Vec<String> = (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(" … ")),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,324 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use super::{
|
||||
PasswordPrompt,
|
||||
screen_render_fixtures::{boot_in_progress, render, render_prompted, screen_lines},
|
||||
};
|
||||
use crate::{
|
||||
boot_model::{BootState, JobResult, Phase},
|
||||
boot_test_fixtures::{MS, queue_unit, queued_boot, running, waiting},
|
||||
color_scheme::Colors,
|
||||
};
|
||||
|
||||
const CRYPTSETUP_MESSAGE: &str =
|
||||
"Please enter passphrase for disk Samsung SSD 980 PRO 1TB (cryptroot):";
|
||||
|
||||
#[test]
|
||||
fn tree_on_top_timeline_below() {
|
||||
let lines = render(&boot_in_progress(), 12_400 * MS, 110, 24);
|
||||
let screen = lines.join("\n");
|
||||
assert_eq!(
|
||||
lines[0], "ryzen · NixOS 26.11 · generation 2771 · Linux 7.2.9",
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines[1].starts_with("└─ Stage 2 12.4s"), "{screen}");
|
||||
assert!(lines[1].ends_with("Running"), "{screen}");
|
||||
assert!(
|
||||
lines[2].starts_with(" ├─ ◆ multi-user.target"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(
|
||||
lines[3].starts_with(" │ ├─ [/] systemd-rfkill"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines[4].starts_with(" │ ├─ [/] docker"), "{screen}");
|
||||
assert!(lines[5].starts_with(" │ └─ [ ] web"), "{screen}");
|
||||
assert!(lines[6].starts_with(" ├─ ◇ graphical.target"), "{screen}");
|
||||
assert!(lines[7].starts_with(" └─ [×] nvidia-powerd"), "{screen}");
|
||||
let timeline = lines
|
||||
.iter()
|
||||
.position(|line| line.contains(" Timeline "))
|
||||
.expect("timeline panel");
|
||||
assert!(
|
||||
lines[timeline + 1].starts_with(&format!("{:29}Kernel", "")),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(
|
||||
lines[timeline + 2].starts_with("√ sysinit.target (2 units)"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines[timeline + 3].starts_with("√ udev-settle"), "{screen}");
|
||||
assert!(lines[timeline + 3].contains('■'), "{screen}");
|
||||
assert!(
|
||||
lines[timeline + 4].starts_with("► systemd-rfkill"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(
|
||||
lines[timeline + 4].contains("·■") && lines[timeline + 4].ends_with('▶'),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines[timeline + 5].starts_with("► docker"), "{screen}");
|
||||
assert!(
|
||||
lines[timeline..].iter().all(|line| !line.contains(" web")),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(!screen.contains("root switch"), "{screen}");
|
||||
assert!(
|
||||
lines[23].starts_with("√ 2 done 2 running 3 waiting × 1 failed"),
|
||||
"{screen}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn password_prompt_floats_over_a_dimmed_view() {
|
||||
let buffer = render_prompted(
|
||||
&boot_in_progress(),
|
||||
12_400 * MS,
|
||||
110,
|
||||
24,
|
||||
Some(PasswordPrompt {
|
||||
message: CRYPTSETUP_MESSAGE,
|
||||
input: "***".to_owned(),
|
||||
sent: false,
|
||||
}),
|
||||
);
|
||||
let lines = screen_lines(&buffer);
|
||||
let screen = lines.join("\n");
|
||||
let title_row = lines
|
||||
.iter()
|
||||
.position(|line| line.contains("┌ Password "))
|
||||
.unwrap_or_else(|| panic!("{screen}"));
|
||||
assert!(lines[title_row].starts_with(' '), "{screen}");
|
||||
assert!(
|
||||
lines[title_row + 1].contains(&format!("│ {CRYPTSETUP_MESSAGE}")),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines[title_row + 3].contains("│ > ***_"), "{screen}");
|
||||
assert!(lines[title_row + 4].contains("Enter unlock"), "{screen}");
|
||||
assert!(
|
||||
lines[23].starts_with("√ 2 done 2 running 3 waiting × 1 failed"),
|
||||
"{screen}"
|
||||
);
|
||||
assert_eq!(
|
||||
buffer[(0, 0)].fg,
|
||||
Colors::default().password.backdrop,
|
||||
"{screen}"
|
||||
);
|
||||
let corner = buffer
|
||||
.content
|
||||
.iter()
|
||||
.find(|cell| cell.symbol() == "┌")
|
||||
.unwrap_or_else(|| panic!("{screen}"));
|
||||
assert_eq!(corner.fg, Colors::default().password.frame, "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn answered_password_prompt_shows_it_is_checking() {
|
||||
let lines = screen_lines(&render_prompted(
|
||||
&boot_in_progress(),
|
||||
12_400 * MS,
|
||||
110,
|
||||
24,
|
||||
Some(PasswordPrompt {
|
||||
message: CRYPTSETUP_MESSAGE,
|
||||
input: String::new(),
|
||||
sent: true,
|
||||
}),
|
||||
));
|
||||
assert!(
|
||||
lines.iter().any(|line| line.contains("│ Checking…")),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_password_messages_wrap_inside_narrow_screens() {
|
||||
let lines = screen_lines(&render_prompted(
|
||||
&boot_in_progress(),
|
||||
12_400 * MS,
|
||||
50,
|
||||
24,
|
||||
Some(PasswordPrompt {
|
||||
message: CRYPTSETUP_MESSAGE,
|
||||
input: String::new(),
|
||||
sent: false,
|
||||
}),
|
||||
));
|
||||
let screen = lines.join("\n");
|
||||
assert!(
|
||||
lines.iter().any(|line| line.contains("(cryptroot):")),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines.iter().any(|line| line.contains("> _")), "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sub_second_runs_stay_off_the_timeline() {
|
||||
let mut state = boot_in_progress();
|
||||
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!(
|
||||
lines.iter().all(|line| !line.contains("blink")),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_timeline_labels_end_in_an_ellipsis() {
|
||||
let mut state = boot_in_progress();
|
||||
queue_unit(
|
||||
&mut state,
|
||||
30,
|
||||
running("sys-kernel-config.mount", &[], 2000 * MS)
|
||||
.described("Kernel Configuration File System"),
|
||||
2000 * MS,
|
||||
);
|
||||
state.finish_job(30, JobResult::Done, 4000 * MS);
|
||||
let lines = render(&state, 12_400 * MS, 110, 24);
|
||||
assert!(
|
||||
lines
|
||||
.iter()
|
||||
.any(|line| line.starts_with("√ Kernel Configuration File…")),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn narrow_screens_drop_the_timeline() {
|
||||
let lines = render(&boot_in_progress(), 12_400 * MS, 40, 24);
|
||||
assert!(
|
||||
lines.iter().all(|line| !line.contains('■')),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
assert!(lines[3].contains("systemd-rfkill"), "{}", lines.join("\n"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finished_stages_collapse_into_a_summary_rule() {
|
||||
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());
|
||||
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}");
|
||||
assert!(
|
||||
lines[1].starts_with("├─ Initrd ■ done in 7.2s · 1 unit"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines[2].starts_with("└─ Stage 2 10.1s"), "{screen}");
|
||||
assert!(lines[3].starts_with(" └─ ◆ graphical.target"), "{screen}");
|
||||
let timeline = lines
|
||||
.iter()
|
||||
.position(|line| line.contains(" Timeline "))
|
||||
.expect("timeline panel");
|
||||
let initrd_bar = &lines[timeline + 2];
|
||||
assert!(initrd_bar.starts_with("√ Initrd (1 unit)"), "{screen}");
|
||||
assert!(initrd_bar.ends_with("■│"), "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completion_announces_the_handover() {
|
||||
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!(
|
||||
lines[1]
|
||||
.starts_with("└─ ■ Boot finished in 9.5s · handing over to display-manager.service"),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shutdown_in_progress_names_its_stage() {
|
||||
let mut state = queued_boot(
|
||||
Phase::Shutdown,
|
||||
20_000 * MS,
|
||||
[waiting("poweroff.target", &[])],
|
||||
);
|
||||
state.origin = 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"),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shutdown_announces_the_handover_once_the_manager_exits() {
|
||||
let mut state = BootState::new(Phase::Shutdown);
|
||||
state.origin = 20_000 * MS;
|
||||
state.manager_exited_at = Some(24_200 * MS);
|
||||
let lines = render(&state, 30_000 * MS, 110, 24);
|
||||
assert!(
|
||||
lines[1].starts_with("└─ ■ Shutdown finished in 4.2s · handing over to systemd-shutdown"),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_timeline_keeps_only_the_slowest_finished_runs() {
|
||||
let quick_units: Vec<String> = (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();
|
||||
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");
|
||||
let timeline = lines
|
||||
.iter()
|
||||
.position(|line| line.contains(" Timeline "))
|
||||
.expect("timeline panel");
|
||||
let (tree, panel) = lines.split_at(timeline);
|
||||
assert!(tree.iter().all(|line| !line.contains('…')), "{screen}");
|
||||
let shown: Vec<&str> = quick_units
|
||||
.iter()
|
||||
.map(|name| name.trim_end_matches(".service"))
|
||||
.filter(|label| panel.iter().any(|line| line.contains(label)))
|
||||
.collect();
|
||||
assert_eq!(
|
||||
shown,
|
||||
["run05", "run06", "run07", "run08", "run09"],
|
||||
"{screen}"
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use ratatui::{Terminal, backend::TestBackend, buffer::Buffer};
|
||||
|
||||
use super::{BootView, PasswordPrompt};
|
||||
use crate::{
|
||||
boot_model::{BootState, JobResult, Phase},
|
||||
boot_origin::BootOrigin,
|
||||
boot_test_fixtures::{MS, queued_boot, running, waiting},
|
||||
color_scheme::Colors,
|
||||
config_file::Timing,
|
||||
font_glyphs::Glyphs,
|
||||
};
|
||||
|
||||
pub fn render(state: &BootState, now: Duration, width: u16, height: u16) -> Vec<String> {
|
||||
screen_lines(&render_prompted(state, now, width, height, None))
|
||||
}
|
||||
|
||||
pub fn render_prompted(
|
||||
state: &BootState,
|
||||
now: Duration,
|
||||
width: u16,
|
||||
height: u16,
|
||||
password_prompt: Option<PasswordPrompt>,
|
||||
) -> Buffer {
|
||||
let mut terminal = Terminal::new(TestBackend::new(width, height)).expect("test backend");
|
||||
terminal
|
||||
.draw(|frame| {
|
||||
frame.render_widget(
|
||||
BootView {
|
||||
state,
|
||||
colors: &Colors::default(),
|
||||
timing: &Timing::default(),
|
||||
glyphs: &Glyphs::default(),
|
||||
now,
|
||||
origin: &BootOrigin {
|
||||
host: "ryzen".to_owned(),
|
||||
system: "NixOS 26.11".to_owned(),
|
||||
generation: "2771".to_owned(),
|
||||
kernel: "Linux 7.2.9".to_owned(),
|
||||
},
|
||||
frame: 1,
|
||||
password_prompt,
|
||||
},
|
||||
frame.area(),
|
||||
);
|
||||
})
|
||||
.expect("draw");
|
||||
terminal.backend().buffer().clone()
|
||||
}
|
||||
|
||||
pub fn screen_lines(buffer: &Buffer) -> Vec<String> {
|
||||
let area = buffer.area;
|
||||
(0..area.height)
|
||||
.map(|y| {
|
||||
let line: String = (0..area.width)
|
||||
.map(|x| buffer[(x, y)].symbol().to_owned())
|
||||
.collect();
|
||||
line.trim_end().to_owned()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn boot_in_progress() -> BootState {
|
||||
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"],
|
||||
),
|
||||
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);
|
||||
state
|
||||
}
|
||||
|
||||
pub fn chain_boot() -> BootState {
|
||||
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),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn described_boot() -> BootState {
|
||||
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"),
|
||||
],
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use super::screen_render_fixtures::{boot_in_progress, chain_boot, described_boot, render};
|
||||
use crate::{
|
||||
boot_model::{JobResult, Phase},
|
||||
boot_test_fixtures::{MS, queue_unit, queued_boot, running, waiting},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn failures_stay_in_the_stage_that_wanted_them() {
|
||||
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);
|
||||
let lines = render(&state, 5000 * MS, 110, 24);
|
||||
let screen = lines.join("\n");
|
||||
assert!(lines[2].starts_with(" ├─ √ basic.target"), "{screen}");
|
||||
assert!(lines[3].starts_with(" │ └─ [×] flaky"), "{screen}");
|
||||
assert!(
|
||||
lines[4].starts_with(" └─ ◆ multi-user.target"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(lines[5].starts_with(" └─ [ ] web"), "{screen}");
|
||||
assert!(!screen.contains("quick"), "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finished_units_linger_in_place_before_leaving() {
|
||||
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");
|
||||
assert!(lingering[3].starts_with(" ├─ [√] early"), "{screen}");
|
||||
assert!(lingering[4].starts_with(" └─ [/] late"), "{screen}");
|
||||
let gone = render(&state, 5500 * MS, 110, 24);
|
||||
let tree: Vec<&String> = gone
|
||||
.iter()
|
||||
.take_while(|line| !line.contains(" Timeline "))
|
||||
.collect();
|
||||
assert!(
|
||||
tree.iter().all(|line| !line.contains("early")),
|
||||
"{}",
|
||||
gone.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_running_units_carry_a_timer() {
|
||||
let lines = render(&boot_in_progress(), 12_400 * MS, 110, 24);
|
||||
let screen = lines.join("\n");
|
||||
assert!(lines[2].ends_with("multi-user.target"), "{screen}");
|
||||
assert!(lines[3].ends_with("6.4s"), "{screen}");
|
||||
assert!(lines[5].ends_with("web"), "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chains_stay_unfolded_while_they_fit() {
|
||||
let lines = render(&chain_boot(), 5000 * MS, 110, 24);
|
||||
let screen = lines.join("\n");
|
||||
assert!(
|
||||
lines[3].starts_with(" └─ [ ] network-wait"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(
|
||||
lines[4].starts_with(" └─ network.target"),
|
||||
"{screen}"
|
||||
);
|
||||
assert!(
|
||||
lines[7].starts_with(" └─ [/] udev-settle"),
|
||||
"{screen}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chains_fold_when_short_and_shorten_to_their_ends_when_narrow() {
|
||||
let short = render(&chain_boot(), 5000 * MS, 110, 9);
|
||||
assert!(
|
||||
short[3].starts_with(" └─ [ ] network-wait ─ network.target ─ network-manager ─ dbus"),
|
||||
"{}",
|
||||
short.join("\n")
|
||||
);
|
||||
assert!(
|
||||
short[4].starts_with(" └─ [/] udev-settle"),
|
||||
"{}",
|
||||
short.join("\n")
|
||||
);
|
||||
let narrow = render(&chain_boot(), 5000 * MS, 45, 8);
|
||||
assert!(
|
||||
narrow[3].starts_with(" └─ [ ] network-wait ─ … ─ dbus"),
|
||||
"{}",
|
||||
narrow.join("\n")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn descriptions_line_up_in_a_column() {
|
||||
let lines = render(&described_boot(), 3000 * MS, 110, 24);
|
||||
let screen = lines.join("\n");
|
||||
let column_of = |text: &str| {
|
||||
lines
|
||||
.iter()
|
||||
.find_map(|line| line.find(text).map(|at| line[..at].width()))
|
||||
.unwrap_or_else(|| panic!("{text} missing\n{screen}"))
|
||||
};
|
||||
assert_eq!(column_of("Multi-User System"), column_of("Description"));
|
||||
assert_eq!(
|
||||
column_of("Multi-User System"),
|
||||
column_of("Docker Application")
|
||||
);
|
||||
assert_eq!(
|
||||
column_of("Multi-User System"),
|
||||
column_of("Load/Save RF Kill")
|
||||
);
|
||||
assert!(column_of("Multi-User System") > column_of("systemd-rfkill"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_service_units_carry_their_kind() {
|
||||
let mut state = described_boot();
|
||||
queue_unit(
|
||||
&mut state,
|
||||
9,
|
||||
running("docker.socket", &[], 1000 * MS).described("Docker Socket for the API"),
|
||||
Duration::ZERO,
|
||||
);
|
||||
let lines = render(&state, 3000 * MS, 110, 24);
|
||||
let screen = lines.join("\n");
|
||||
let socket_row = lines
|
||||
.iter()
|
||||
.find(|line| line.contains("Docker Socket"))
|
||||
.unwrap_or_else(|| panic!("{screen}"));
|
||||
assert!(socket_row.contains("[/] docker "), "{screen}");
|
||||
assert!(socket_row.contains(" Socket "), "{screen}");
|
||||
assert!(
|
||||
render(&described_boot(), 3000 * MS, 110, 24)
|
||||
.iter()
|
||||
.all(|line| !line.contains(" Socket ")),
|
||||
"{screen}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn narrow_screens_keep_names_and_drop_descriptions() {
|
||||
let lines = render(&described_boot(), 3000 * MS, 40, 24);
|
||||
let screen = lines.join("\n");
|
||||
assert!(screen.contains("systemd-rfkill"), "{screen}");
|
||||
assert!(!screen.contains("Docker"), "{screen}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overflowing_trees_are_cut_with_a_count() {
|
||||
let names: Vec<String> = (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(" … ")),
|
||||
"{}",
|
||||
lines.join("\n")
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user