Skip to main content

isothrum_machine/
selftest.rs

1//! Self-tests: proof that this computer runs deployments, not only that its tools are there. Each
2//! runs a throwaway deployment through a runner of its own (its home in the test's folder, so
3//! nothing of it reaches a caller's runner), says each step as it goes, and always takes the
4//! deployment down. What a test downloads is handed back, for a caller that keeps a record of its
5//! downloads. A step's reason is the tool's or the runner's text; the UI words the steps.
6//!
7//! - [`engine`]: a two-container deployment on a container engine.
8//! - [`hypervisor`]: a VM on a hypervisor, through Vagrant.
9//! - [`server`]: a VM on a server (Proxmox VE through Terraform, ESXi through Vagrant).
10
11pub mod engine;
12pub mod hypervisor;
13pub mod server;
14
15use std::path::{Path, PathBuf};
16
17use isothrum_runner::{Event, RunName, Runner, RunnerConfig};
18
19use crate::Context;
20
21/// Where a step of a self-test stands.
22#[derive(Debug, Clone, PartialEq, Eq)]
23#[non_exhaustive]
24pub enum Progress<S, F, E> {
25    /// It started.
26    Running(S),
27    /// It passed, having found `F`.
28    Passed(S, F),
29    /// It failed: why.
30    Failed(S, E),
31    /// It was skipped: why.
32    Skipped(S, F),
33}
34
35/// How long an elevated step may wait for its prompt's answer and its end.
36const PROMPT_LIMIT: std::time::Duration = std::time::Duration::from_secs(300);
37
38/// Says each step's progress to its sink.
39struct Steps<'a, S, F, E>(&'a (dyn Fn(Progress<S, F, E>) + Send + Sync));
40
41impl<S: Copy, F, E: Clone> Steps<'_, S, F, E> {
42    /// Runs `step`'s work, saying it started, then that it passed or why it failed.
43    async fn run(&self, step: S, work: impl Future<Output = Result<F, E>>) -> Result<(), E> {
44        (self.0)(Progress::Running(step));
45        match work.await {
46            Ok(found) => {
47                (self.0)(Progress::Passed(step, found));
48                Ok(())
49            }
50            Err(why) => {
51                (self.0)(Progress::Failed(step, why.clone()));
52                Err(why)
53            }
54        }
55    }
56}
57
58/// A runner of the test's own, its home `dir/home`, and the test's project folder `dir/project`
59/// holding `file` with `text`.
60fn throwaway(cx: &Context, dir: &Path, file: &str, text: &str) -> Result<(Runner, PathBuf), String> {
61    let project = dir.join("project");
62    let written = std::fs::create_dir_all(&project).and_then(|()| std::fs::write(project.join(file), text));
63    written.map_err(|e| format!("{} couldn't be written: {e}", project.display()))?;
64    let mut config = RunnerConfig::new(dir.join("home"));
65    config.processes = std::sync::Arc::clone(&cx.runner);
66    let runner = Runner::new(config).map_err(|e| e.to_string())?;
67    Ok((runner, project))
68}
69
70/// The test's run `name` in `runner`, its project `project`.
71fn open<'a>(runner: &'a Runner, project: &Path, name: &str) -> Result<isothrum_runner::Running<'a>, String> {
72    let name = RunName::new(name).map_err(|e| e.to_string())?;
73    runner.open(project, Some(name)).map_err(|e| e.to_string())
74}
75
76/// Events nobody reads: a self-test says its own steps.
77fn quiet(_: &Event) {}