From b00fb70e19155d61dde65f19a7cd1ac5bb935a30 Mon Sep 17 00:00:00 2001 From: Helight Date: Sun, 15 Nov 2026 20:41:57 +0100 Subject: [PATCH 1/3] feat(serve): berth serve --simulate for testing against the API Each host is a berthd in single-user mode in the scratch directory, and its commands run through a shell of a few canned commands. The plugin sim holds the provisioner sim/cloud, which berth runs itself. --- crates/berth-api/src/types.rs | 9 + crates/berth-core/src/host.rs | 4 + crates/berth-core/src/host_type.rs | 1 + crates/berth-core/src/lib.rs | 1 + crates/berth-core/src/paths.rs | 2 + crates/berth-core/src/rollout.rs | 1 + crates/berth-core/src/simulate.rs | 1079 +++++++++++++++++++ crates/berth-core/src/tailnet.rs | 13 + crates/berth-page/src/app.rs | 1 + crates/berth-tui/src/screen.rs | 15 + crates/berth/src/cli/args.rs | 19 + crates/berth/src/cli/mod.rs | 2 + crates/berth/src/local/mod.rs | 23 + crates/berth/src/local/serve/actions.rs | 53 +- crates/berth/src/local/serve/api.rs | 13 + crates/berth/src/local/serve/connect.rs | 10 + crates/berth/src/local/serve/mod.rs | 51 +- crates/berth/src/local/serve/simulate.rs | 102 ++ crates/berth/tests/simulate.rs | 360 +++++++ crates/berthd/src/http.rs | 33 + crates/berthd/src/system.rs | 45 +- crates/berthd/src/users.rs | 15 +- docs/content/docs/(guide)/reference/cli.mdx | 14 +- docs/openapi/berth.openapi.json | 9 + docs/openapi/berth.operations.json | 4 + docs/openapi/berthd.openapi.json | 9 + docs/openapi/berthd.operations.json | 9 + 27 files changed, 1880 insertions(+), 17 deletions(-) create mode 100644 crates/berth-core/src/simulate.rs create mode 100644 crates/berth/src/local/serve/simulate.rs create mode 100644 crates/berth/tests/simulate.rs diff --git a/crates/berth-api/src/types.rs b/crates/berth-api/src/types.rs index 043f7f8..dded842 100644 --- a/crates/berth-api/src/types.rs +++ b/crates/berth-api/src/types.rs @@ -66,6 +66,10 @@ pub struct Version { /// when the host keeps no record. #[serde(rename = "type", default, skip_serializing_if = "Option::is_none")] pub host_type: Option, + /// Whether it is simulated, as `berth serve --simulate` and its hosts + /// are: nothing it does reaches a real machine. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub simulated: bool, } impl Version { @@ -1014,6 +1018,10 @@ pub struct Config { pub systemd: Option, /// A wrapper that gives the agent its environment. pub agent_env: String, + /// Whether the host is simulated: in single-user mode its commands then + /// run through `agent_env` too, and its version says so. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub simulated: bool, } impl Default for Config { @@ -1025,6 +1033,7 @@ impl Default for Config { units: vec![], systemd: None, agent_env: "/usr/local/bin/agent-env".into(), + simulated: false, } } } diff --git a/crates/berth-core/src/host.rs b/crates/berth-core/src/host.rs index 173a6dd..06e7039 100644 --- a/crates/berth-core/src/host.rs +++ b/crates/berth-core/src/host.rs @@ -116,6 +116,10 @@ impl Host { /// How to open a connection to the host's berthd. `password` is the SSH /// user's sudo password, for a host that asks for one. pub fn link(&self, paths: &Paths, password: Option<&str>) -> Result { + // A simulated server reaches no machine: each host is a berthd of its own. + if let Some(simulation) = crate::simulate::active() { + return simulation.link(&self.name); + } // A profile can name the command itself, as for a container: // ["docker", "exec", "-i", "NAME", "berthd", "connect"]. if let Some(command) = self.access().link { diff --git a/crates/berth-core/src/host_type.rs b/crates/berth-core/src/host_type.rs index 28426f3..883f386 100644 --- a/crates/berth-core/src/host_type.rs +++ b/crates/berth-core/src/host_type.rs @@ -500,6 +500,7 @@ mod tests { mode: "system".into(), applied: None, host_type, + simulated: false, }; assert_eq!(seen(Ok(&version(None))), Some(HostType::Managed)); assert_eq!( diff --git a/crates/berth-core/src/lib.rs b/crates/berth-core/src/lib.rs index f46b8c3..080c175 100644 --- a/crates/berth-core/src/lib.rs +++ b/crates/berth-core/src/lib.rs @@ -28,6 +28,7 @@ pub mod seal; pub mod seed; pub mod server; pub mod server_image; +pub mod simulate; pub mod store; pub mod tailnet; diff --git a/crates/berth-core/src/paths.rs b/crates/berth-core/src/paths.rs index 4783da8..576a316 100644 --- a/crates/berth-core/src/paths.rs +++ b/crates/berth-core/src/paths.rs @@ -49,6 +49,8 @@ pub mod layout { pub const CONTROLLER_PLAYBOOK: &str = "provision/ansible/controller.yml"; /// Ansible's settings for that playbook. pub const ANSIBLE_CONFIG: &str = "provision/ansible.cfg"; + /// The controller a host runs, as built into the role's files. + pub const BERTHD: &str = "provision/ansible/roles/devbox/files/bin/berthd"; /// The profile a new host starts from. pub const EXAMPLE: &str = "provision/config/example.json"; /// Where a source of berth 0.2 and earlier had its builder. diff --git a/crates/berth-core/src/rollout.rs b/crates/berth-core/src/rollout.rs index 9f3d5e3..eb542c0 100644 --- a/crates/berth-core/src/rollout.rs +++ b/crates/berth-core/src/rollout.rs @@ -533,6 +533,7 @@ mod tests { mode: "system".into(), applied: self.applied.clone(), host_type: None, + simulated: false, })) } fn apply(&mut self, _out: &mut dyn Write) -> std::result::Result<(), String> { diff --git a/crates/berth-core/src/simulate.rs b/crates/berth-core/src/simulate.rs new file mode 100644 index 0000000..a815ece --- /dev/null +++ b/crates/berth-core/src/simulate.rs @@ -0,0 +1,1079 @@ +//! A simulated world, for `berth serve --simulate`: the real server on a +//! scratch store, with only what reaches other machines stood in for. +//! +//! A host is a berthd of its own in single-user mode, in a folder of the +//! scratch data, reached over a link like any other. Its commands run +//! through a small shell that knows a few commands and nothing else, and it +//! shows only the processes it started. Applying a host writes progress and +//! the records a real apply leaves. The plugin `sim` holds a provisioner that +//! makes machines and services without making anything, and the tailnet +//! hands out keys that join nothing. + +use crate::{ + Result, host::Host, host_type::HostType, link::LinkSpec, paths::Paths, plugins::Setup, + profiles::Place, +}; +use serde::Deserialize; +use serde_json::{Value, json}; +use std::{ + collections::BTreeMap, + fs, + io::Write, + path::{Path, PathBuf}, + process::{Child, Command, Stdio}, + sync::{Mutex, OnceLock}, + time::Duration, +}; + +/// The seconds a step takes, as `--delay` sets it for the server and its +/// provisioner. +pub const DELAY: &str = "BERTH_SIMULATE_DELAY"; +/// The simulated provisioner's type, and the name it is registered under. +pub const PROVISIONER: &str = "sim/cloud"; +pub const REGISTERED: &str = "sim"; +/// What a scratch directory of a simulated server holds, so that no other +/// directory is ever taken for one. +pub const MARKER: &str = ".berth-simulated"; + +static ACTIVE: OnceLock = OnceLock::new(); + +/// The simulation this process runs, when it runs one. +pub fn active() -> Option<&'static Simulation> { + ACTIVE.get() +} + +/// How long one step takes: two seconds unless [`DELAY`] says otherwise. +pub fn delay() -> Duration { + std::env::var(DELAY) + .ok() + .and_then(|seconds| seconds.trim().parse::().ok()) + .filter(|seconds| seconds.is_finite() && *seconds >= 0.0) + .map_or(Duration::from_secs(2), Duration::from_secs_f64) +} + +/// Wait `steps` of the delay. +pub fn pause(steps: f64) { + std::thread::sleep(delay().mul_f64(steps)); +} + +/// Refuse a directory for a simulated server's state that is, holds or lies +/// in berth's own configuration or data, or that holds anything but an +/// earlier simulation. +pub fn check_dir(dir: &Path, own: Option<&Paths>) -> Result<()> { + let absolute = |path: &Path| { + let path = std::path::absolute(path).unwrap_or_else(|_| path.to_owned()); + fs::canonicalize(&path).unwrap_or(path) + }; + let dir = absolute(dir); + for real in own + .into_iter() + .flat_map(|own| [&own.config_dir, &own.data_dir]) + { + let real = absolute(real); + if dir.starts_with(&real) || real.starts_with(&dir) { + return Err(format!( + "{} is berth's own configuration or data, or holds it ({}): a simulated server never uses it. Give another directory", + dir.display(), + real.display() + ) + .into()); + } + } + let used = fs::read_dir(&dir).is_ok_and(|mut entries| entries.next().is_some()); + if used && !dir.join(MARKER).exists() { + return Err(format!( + "{} holds files of something else: a simulated server starts in an empty directory, or in one of an earlier simulation", + dir.display() + ) + .into()); + } + Ok(()) +} + +// ── The world a test starts from ──────────────────────────────────────────── + +/// A known world to start from: a JSON file. +#[derive(Debug, Default, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Seed { + /// The token clients over HTTP show, at least 32 characters. One is made + /// without it. + #[serde(default)] + pub token: Option, + /// Saved hosts by name. + #[serde(default)] + pub hosts: BTreeMap, + /// Registered provisioners by name, as `provisioners.save` takes them. + /// Without any, `sim` is registered as `sim/cloud`. + #[serde(default)] + pub provisioners: BTreeMap, + /// Secrets by name. + #[serde(default)] + pub secrets: BTreeMap, +} + +#[derive(Debug, Default, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct SeedHost { + /// Its profile. A host without an address gets one of 192.0.2.0/24. + #[serde(default)] + pub profile: Option, + /// Whether it starts applied, at this berth's version. + #[serde(default)] + pub applied: bool, +} + +impl Seed { + pub fn read(file: &Path) -> Result { + let text = fs::read_to_string(file) + .map_err(|error| format!("The seed {}: {error}", file.display()))?; + Seed::parse(&text).map_err(|error| format!("The seed {}: {error}", file.display()).into()) + } + + pub fn parse(text: &str) -> Result { + let seed: Seed = serde_json::from_str(text)?; + if let Some(token) = &seed.token + && (token.trim().len() < 32 || token.trim().contains(char::is_whitespace)) + { + return Err("token has at least 32 characters and no spaces".into()); + } + for (name, host) in &seed.hosts { + Place::own(name).map_err(|error| format!("hosts.{name}: {error}"))?; + if host + .profile + .as_ref() + .is_some_and(|profile| !profile.is_object()) + { + return Err(format!("hosts.{name}.profile is an object").into()); + } + } + Ok(seed) + } + + /// The profile of each host, with an address where it has none. + pub fn profiles(&self) -> Vec<(String, Value, bool)> { + self.hosts + .iter() + .enumerate() + .map(|(index, (name, host))| { + let mut profile = host.profile.clone().unwrap_or_else(|| json!({})); + if profile["hostname"].as_str().is_none_or(str::is_empty) { + profile["hostname"] = json!(name); + } + // A workstation's profile has a builder, which may be empty. + let workstation = profile["type"] + .as_str() + .is_none_or(|kind| kind == "workstation"); + if workstation && !profile["builder"].is_object() { + profile["builder"] = json!({}); + } + if !profile["access"].is_object() { + profile["access"] = json!({}); + } + let access = &mut profile["access"]; + if access["address"].as_str().is_none_or(str::is_empty) { + access["address"] = json!(format!("192.0.2.{}", 20 + index % 200)); + } + if access["user"].as_str().is_none_or(str::is_empty) { + access["user"] = json!("ops"); + } + (name.clone(), profile, host.applied) + }) + .collect() + } + + /// Put the world into the store, with the simulated plugin there already. + pub fn plant(&self, paths: &Paths, simulation: &Simulation) -> Result<()> { + let store = crate::store::shared(paths)?; + if let Some(token) = &self.token { + fs::create_dir_all(&paths.data_dir)?; + write_private( + &paths.data_dir.join("serve.token"), + &format!("{}\n", token.trim()), + )?; + } + let registries = crate::registry::Store::own(store.clone()); + for (name, value) in &self.secrets { + registries.set_secret(name, Some(value))?; + } + let provisioners = match self.provisioners.is_empty() { + true => BTreeMap::from([( + REGISTERED.to_owned(), + berth_api::SavedProvisioner { + kind: PROVISIONER.into(), + settings: None, + }, + )]), + false => self.provisioners.clone(), + }; + for (name, saved) in &provisioners { + crate::provisioner::register(paths, name, saved) + .map_err(|error| format!("provisioners.{name}: {error}"))?; + } + for (name, profile, applied) in self.profiles() { + let place = Place::own(&name)?; + place.save(paths, &profile)?; + if applied { + let host = Host::load_at(paths, place)?; + simulation + .record(paths, &host, level(&host)) + .map_err(|error| format!("hosts.{name}: {error}"))?; + } + } + Ok(()) + } +} + +fn write_private(file: &Path, text: &str) -> Result<()> { + use std::os::unix::fs::OpenOptionsExt; + let mut opened = fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .mode(0o600) + .open(file)?; + opened.write_all(text.as_bytes())?; + Ok(()) +} + +/// What an apply of `host` records it as, as the role's `level`. +fn level(host: &Host) -> Option<&'static str> { + match crate::host_type::of(&host.config) { + HostType::Workstation => None, + HostType::Node => Some("node"), + HostType::Managed | HostType::Ssh => Some("controller"), + } +} + +// ── Hosts ─────────────────────────────────────────────────────────────────── + +/// In a host's folder while its machine is stopped or gone: its berthd +/// answers nothing then. +const STOPPED: &str = "stopped"; + +pub struct Simulation { + /// Where it keeps what it simulates: the hosts are in `hosts/` of it. + root: PathBuf, + /// The berthd the hosts run, and their links reach them with. + berthd: PathBuf, + running: Mutex>, +} + +impl Simulation { + /// Where the simulation of the installation at `paths` keeps its hosts + /// and what its provisioner made. + pub fn dir_of(paths: &Paths) -> PathBuf { + paths.data_dir.join("simulated") + } + + /// Run this process's simulation of the installation at `paths`, with + /// the hosts' berthd copied from `berthd`. Only one can run. + pub fn begin(paths: &Paths, berthd: &Path) -> Result<&'static Simulation> { + let root = &Simulation::dir_of(paths); + if !berthd.is_file() { + return Err( + format!("No berthd to run the simulated hosts: {}", berthd.display()).into(), + ); + } + // The source keeps it as a file to copy, which need not be executable. + let program = root.join("bin/berthd"); + fs::create_dir_all(root.join("bin"))?; + fs::copy(berthd, &program)?; + { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(&program, fs::Permissions::from_mode(0o755))?; + } + let simulation = Simulation { + root: root.to_owned(), + berthd: program, + running: Mutex::default(), + }; + ACTIVE + .set(simulation) + .map_err(|_| "A simulation runs in this process already")?; + Ok(ACTIVE.get().expect("just set")) + } + + fn dir(&self, name: &str) -> Result { + Place::own(name)?; + Ok(self.root.join("hosts").join(name)) + } + + /// The link to the host's berthd, which is started when it does not run. + /// Until the host is first applied, the link finds no berthd, as on a + /// machine nothing of berth was installed on. + pub fn link(&self, name: &str) -> Result { + let dir = self.dir(name)?; + self.ensure(name, &dir)?; + let script = "[ -e \"$1/etc/applied.json\" ] || { echo 'sh: 1: berthd: not found' >&2; exit 127; }; exec \"$2\" connect --dir \"$1\""; + Ok(LinkSpec { + program: "/bin/sh".into(), + args: vec![ + "-c".into(), + script.into(), + "sh".into(), + dir.to_string_lossy().into_owned(), + self.berthd.to_string_lossy().into_owned(), + ], + name: name.to_owned(), + ..LinkSpec::default() + }) + } + + fn ensure(&self, name: &str, dir: &Path) -> Result<()> { + let mut running = self.running.lock().unwrap(); + if let Some(child) = running.get_mut(name) + && child.try_wait().ok().flatten().is_none() + { + return Ok(()); + } + let (etc, home, bin) = (dir.join("etc"), dir.join("home"), dir.join("bin")); + for made in [&etc, &home, &bin] { + fs::create_dir_all(made)?; + } + let shell = bin.join("shell"); + fs::write(&shell, SHELL)?; + { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(&shell, fs::Permissions::from_mode(0o755))?; + } + let config = json!({ + "agent": "agent", + "roots": [home], + "agent_env": shell, + "simulated": true, + }); + fs::write(etc.join("berthd.json"), config.to_string())?; + if !etc.join("actions.json").exists() { + fs::write(etc.join("actions.json"), "[]")?; + } + let readme = home.join("README.md"); + if !readme.exists() { + fs::write( + &readme, + format!("# {name}\n\nA simulated host of berth serve --simulate.\n"), + )?; + } + let _ = fs::remove_file(dir.join(berth_api::CONTROL_SOCKET)); + let log = fs::File::create(dir.join("berthd.log"))?; + let child = Command::new(&self.berthd) + .arg("--single") + .arg(dir) + .current_dir(&home) + .env("HOME", &home) + .env("BERTH_SIM_HOST", name) + .stdin(Stdio::null()) + .stdout(log.try_clone()?) + .stderr(log) + .spawn() + .map_err(|error| format!("{}: {error}", self.berthd.display()))?; + running.insert(name.to_owned(), child); + for _ in 0..100 { + if dir.join(berth_api::CONTROL_SOCKET).exists() { + return Ok(()); + } + std::thread::sleep(Duration::from_millis(20)); + } + Err(format!( + "The simulated berthd of {name} did not start: see {}", + dir.join("berthd.log").display() + ) + .into()) + } + + /// Tell the host's berthd to read its configuration again. + fn reload(&self, name: &str) { + if let Some(child) = self.running.lock().unwrap().get(name) { + // SAFETY: a plain system call on a child of ours. + unsafe { libc::kill(child.id() as i32, libc::SIGHUP) }; + } + } + + /// End every host's berthd. + pub fn stop(&self) { + for (_, mut child) in std::mem::take(&mut *self.running.lock().unwrap()) { + // SAFETY: a plain system call on a child of ours. + unsafe { libc::kill(child.id() as i32, libc::SIGTERM) }; + let _ = child.wait(); + } + } + + /// Apply the host's profile, with progress on `out`: steps that take + /// the delay, then what the role records. Whether it went well. + pub fn apply(&self, paths: &Paths, host: &Host, out: &mut dyn Write) -> bool { + self.run(paths, host, level(host), true, out) + } + + /// Install the controller alone. + pub fn install_controller(&self, paths: &Paths, host: &Host, out: &mut dyn Write) -> bool { + self.run(paths, host, Some("controller"), false, out) + } + + fn run( + &self, + paths: &Paths, + host: &Host, + level: Option<&str>, + whole: bool, + out: &mut dyn Write, + ) -> bool { + let name = &host.name; + if whole + && let Err(error) = crate::host_type::guard(host, crate::host_type::Capability::Apply) + { + let _ = writeln!(out, "✗ {error}"); + return false; + } + let mut tasks = vec!["Install the base packages", "Install berthd and box"]; + if whole && level.is_none() { + tasks.splice(1..1, ["Install the coding agents", "Configure the desktop"]); + } + let _ = writeln!(out, "▸ {name}: applying, simulated: nothing is installed"); + let _ = writeln!(out, "PLAY [{name}]"); + for task in &tasks { + pause(0.5); + let _ = writeln!(out, "TASK [{task}] changed: [{name}]"); + } + pause(0.5); + let _ = writeln!(out, "TASK [Record what was applied] changed: [{name}]"); + if let Err(error) = self.record(paths, host, level) { + let _ = writeln!(out, "✗ {name}: {error}"); + return false; + } + let _ = writeln!( + out, + "PLAY RECAP {name} : ok={count} changed={count} failed=0", + count = tasks.len() + 1 + ); + if let Err(error) = crate::apply::settle_with(paths, host, out) { + let _ = writeln!( + out, + "! {name} is applied, but its profile was not updated: {error}" + ); + } + let _ = writeln!(out, "✓ {name} is applied"); + true + } + + /// Write what the role leaves on a host it applied: the record of the + /// berth and plugins, the profile and the actions. + fn record(&self, paths: &Paths, host: &Host, level: Option<&str>) -> Result<()> { + let name = &host.name; + let etc = self.dir(name)?.join("etc"); + self.link(name)?; + let source = paths.source()?; + let plugins = + crate::plugins::load(&[source.join("plugins"), paths.config_dir.join("plugins")]); + let features: Vec = crate::node::features(&host.config) + .iter() + .filter_map(|feature| feature.split('/').next().map(str::to_owned)) + .collect(); + let records: Vec<_> = crate::plugins::records(&plugins) + .into_iter() + .filter(|record| features.contains(&record.id)) + .collect(); + let mut applied = json!({"berth": berth_api::VERSION, "plugins": records}); + if let Some(level) = level { + applied["level"] = json!(level); + } + fs::write(etc.join("applied.json"), format!("{applied:#}\n"))?; + let mut kept = crate::kept_profile::for_host(paths, host); + kept["written"] = json!(berth_api::now()); + write_private(&etc.join("profile.json"), &format!("{kept:#}\n"))?; + fs::write(etc.join("actions.json"), actions(level).to_string())?; + self.reload(name); + Ok(()) + } +} + +/// The actions a simulated host's controller lists once applied, which its +/// shell answers. +fn actions(level: Option<&str>) -> Value { + let action = |name: &str, label: &str, group: &str, help: &str| json!({"name": name, "label": label, "group": group, "help": help, "run": name}); + let mut listed = vec![ + action("status", "Status", "Host", "How the simulated host is."), + action( + "update", + "Update", + "Sync", + "Pretend to bring the packages up to date.", + ), + ]; + if level.is_none() { + let mut smoke = action( + "smoke", + "Self-Test", + "Host", + "Simulated checks that all pass.", + ); + smoke["agent"] = json!(true); + listed.insert(1, smoke); + } + let mut reboot = action("reboot", "Reboot", "Power", "Pretend to restart the host."); + reboot["confirm"] = json!("Restart the host?"); + listed.push(reboot); + let mut secrets = action( + "secrets", + "Secrets", + "Sync", + "Take the host's secrets on standard input.", + ); + secrets["input"] = json!("secrets"); + listed.push(secrets); + let mut fail = action( + "fail", + "Fail", + "Host", + "Fail with exit code 3, to test failures.", + ); + fail["arguments"] = json!([{"name": "message", "default": "simulated failure"}]); + listed.push(fail); + Value::Array(listed) +} + +/// The shell of a simulated host: every command of a session or an action +/// runs through it, and it runs only the commands it knows itself. +pub const SHELL: &str = r#"#!/bin/sh +# The shell of a host of berth serve --simulate. It runs the few commands +# below itself and no program of the machine it is on. +host=${BERTH_SIM_HOST:-host} +cd "$HOME" 2>/dev/null || exit 1 +# The session's own input, for the commands that read it. +exec 3<&0 +known="echo hostname whoami id uname pwd date ls cat sleep true false exit help status smoke update reboot fail secrets" + +inside() { + case "$1" in /*|..|../*|*/..|*/../*) return 1 ;; esac +} + +run() { + case "$1" in + echo) shift; printf '%s\n' "$*" ;; + hostname) printf '%s\n' "$host" ;; + whoami) echo agent ;; + id) echo "uid=1000(agent) gid=1000(agent) groups=1000(agent)" ;; + uname) + case "$2" in + -a) echo "Linux $host 6.8.0-simulated #1 SMP x86_64 GNU/Linux" ;; + *) echo Linux ;; + esac ;; + pwd) echo /home/agent ;; + date) date -u '+%Y-%m-%dT%H:%M:%SZ' ;; + ls) + shift + [ $# -eq 0 ] && set -- . + for path in "$@"; do + inside "$path" || { echo "ls: $path: outside the home" >&2; return 2; } + ls -1 -- "$path" || return 2 + done ;; + cat) + shift + for path in "$@"; do + inside "$path" || { echo "cat: $path: outside the home" >&2; return 1; } + cat -- "$path" || return 1 + done ;; + sleep) + case "${2:-1}" in + *[!0-9.]*|'') echo "sleep: ${2}: not a number" >&2; return 1 ;; + *) sleep "${2:-1}" ;; + esac ;; + true) return 0 ;; + false) return 1 ;; + exit) exit "${2:-0}" ;; + help) echo "A simulated host runs: $known" ;; + status) + echo "$host: simulated" + echo "Provisioning: done" + echo "berthd: running" + echo "Disk: 12% of 80G used" ;; + smoke) + for check in graphics browser agents; do echo "✓ $check (simulated)"; done ;; + update) + echo "Reading package lists... done (simulated)" + echo "0 upgraded, 0 newly installed. No reboot is needed." ;; + reboot) echo "$host restarts (simulated): nothing stops" ;; + fail) echo "${BERTH_ARG_MESSAGE:-simulated failure}" >&2; return 3 ;; + secrets) + count=$(wc -c <&3 | tr -d ' ') + echo "Took $count bytes of secrets (simulated): nothing is installed" ;; + *) echo "$1: not a command of this simulated host; help lists them" >&2; return 127 ;; + esac +} + +line() { + set -f + # shellcheck disable=SC2086 + set -- $1 + set +f + [ $# -eq 0 ] && return 0 + run "$@" +} + +script() { + printf '%s\n' "$1" | tr ';' '\n' | { + code=0 + while IFS= read -r each; do + line "$each" + code=$? + done + exit $code + } +} + +interactive() { + while :; do + printf 'agent@%s:~$ ' "$host" + IFS= read -r typed || { echo; exit 0; } + line "$typed" + done +} + +case "$1" in + sh|bash|zsh|*/sh|*/bash|*/zsh) + case "$2" in + -c) script "$3" ;; + *) interactive ;; + esac ;; + '') interactive ;; + *) run "$@" ;; +esac +"#; + +// ── The simulated provisioner ─────────────────────────────────────────────── + +/// The plugin that holds the simulated provisioner, written into the +/// server's own plugins. Its command runs `berth` at `program` again. +pub fn write_plugin(paths: &Paths, program: &Path) -> Result<()> { + use std::os::unix::fs::PermissionsExt; + // Opened first, so the store takes the plugin as one added now. + crate::store::shared(paths)?; + let dir = crate::plugins::own_dir(&paths.config_dir).join(REGISTERED); + fs::create_dir_all(dir.join("bin"))?; + let quoted = program.to_string_lossy().replace('\'', r"'\''"); + let script = dir.join("bin/provision"); + fs::write( + &script, + format!("#!/bin/sh\nexec '{quoted}' simulated-provisioner \"$@\"\n"), + )?; + fs::set_permissions(&script, fs::Permissions::from_mode(0o755))?; + let mut manifest = manifest(); + manifest["version"] = json!(berth_api::VERSION); + fs::write(dir.join("package.json"), format!("{manifest:#}\n"))?; + crate::plugin_state::remember(paths) +} + +fn manifest() -> Value { + json!({ + "name": "berth-plugin-sim", + "displayName": "Simulated", + "description": "Machines and services of berth serve --simulate, which make nothing.", + "files": ["bin"], + "berth": {"provisioners": [{ + "id": "cloud", + "label": "Simulated cloud", + "description": "Makes simulated machines for hosts and simulated services, with addresses of 192.0.2.0/24.", + "command": "bin/provision", + "protocol": 2, + "verbs": ["list"], + "arguments": [{"name": "region", "label": "Region", "default": "sim-1", "values": ["sim-1", "sim-2"]}], + "kinds": [ + {"id": "machine", "label": "Simulated machine", "setup": "apply", + "verbs": ["start", "stop", "logs"], + "arguments": [ + {"name": "cpus", "label": "CPUs", "default": "2", "kind": "resource"}, + {"name": "memory", "label": "Memory", "default": "4GiB", "kind": "resource"}]}, + {"id": "service", "label": "Simulated service", + "verbs": ["start", "stop", "change", "logs", "exec"], + "spec": {"format": "text", "label": "Spec"}, + "arguments": [{"name": "image", "label": "Image", "default": "web:1"}], + "actions": [{"name": "restart", "label": "Restart", "confirm": "Restart the service?"}]} + ], + "actions": [ + {"name": "images", "label": "Images", "changes": false}, + {"name": "inspect", "label": "Inspect", "changes": false, + "arguments": [{"name": "image", "required": true}]} + ], + "collections": [{ + "id": "images", "label": "Image", "plural": "Images", + "key": ["image"], + "columns": [{"name": "size", "label": "Size"}], + "item": [{"action": "inspect"}], + "show": "images" + }] + }]} + }) +} + +/// The images the simulated cloud has, with their sizes. +const IMAGES: &[(&str, &str)] = &[ + ("ubuntu-26.04", "2.1 GiB"), + ("web:1", "180 MiB"), + ("db:16", "420 MiB"), +]; + +/// What the simulated provisioner made, kept in the scratch data. +#[derive(Debug, Default, serde::Serialize, Deserialize)] +struct Made { + #[serde(default)] + next: u32, + #[serde(default)] + resources: BTreeMap, +} + +/// Run one verb of the simulated provisioner: the request on `input`, the +/// answer returned, progress on `progress`. The answer of `logs` is the log +/// as text. +pub fn provision(verb: &str, input: &str, progress: &mut dyn Write) -> Result { + let request: Value = serde_json::from_str(input)?; + let paths = Paths::locate()?; + let dir = Simulation::dir_of(&paths); + fs::create_dir_all(&dir)?; + let lock = fs::File::create(dir.join("cloud.lock"))?; + use std::os::fd::AsRawFd; + // SAFETY: the descriptor is open; the lock goes when the file is closed. + unsafe { libc::flock(lock.as_raw_fd(), libc::LOCK_EX) }; + let file = dir.join("cloud.json"); + let mut made: Made = fs::read(&file) + .ok() + .and_then(|data| serde_json::from_slice(&data).ok()) + .unwrap_or_default(); + let answer = answer(verb, &request, &mut made, progress, delay())?; + fs::write(&file, serde_json::to_vec_pretty(&made)?)?; + // A host whose machine does not run does not answer. + if request["kind"] == "machine" + && let Some(host) = request["host"] + .as_str() + .filter(|host| Place::own(host).is_ok()) + { + let stopped = dir.join("hosts").join(host).join(STOPPED); + match verb { + "stop" | "destroy" => { + fs::create_dir_all(dir.join("hosts").join(host))?; + fs::write(stopped, "")?; + } + "start" | "create" => { + let _ = fs::remove_file(stopped); + } + _ => {} + } + } + Ok(answer) +} + +fn say(progress: &mut dyn Write, line: &str) { + let _ = writeln!(progress, "{line}"); + let _ = progress.flush(); +} + +fn endpoints(kind: &str, address: &str) -> Value { + match kind { + "machine" => json!([{"name": "ssh", "address": address, "port": 22, "hint": "ssh"}]), + _ => json!([{"name": "http", "address": address, "port": 80, "hint": "http"}]), + } +} + +fn answer( + verb: &str, + request: &Value, + made: &mut Made, + progress: &mut dyn Write, + delay: Duration, +) -> Result { + let pause = |steps: f64| std::thread::sleep(delay.mul_f64(steps)); + let id = request["id"].as_str().unwrap_or("").to_owned(); + let kind = request["kind"].as_str().unwrap_or("machine").to_owned(); + let found = |made: &Made| -> Result { + made.resources + .get(&id) + .cloned() + .ok_or_else(|| format!("No simulated resource {id}").into()) + }; + let set = |made: &mut Made, state: &str| -> Result { + let mut resource = found(made)?; + resource["state"] = json!(state); + made.resources.insert(id.clone(), resource.clone()); + Ok(resource) + }; + Ok(match verb { + "status" if id.is_empty() => json!({ + "state": "ready", + "message": "Simulated: nothing is made", + "details": [ + {"label": "Region", "value": request["arguments"]["region"].as_str().unwrap_or("sim-1")}, + {"label": "Resources", "value": made.resources.len().to_string()} + ], + "values": {"region": ["sim-1", "sim-2"]}, + "collections": {"images": IMAGES.iter().map(|(name, size)| { + json!({"name": name, "columns": {"size": size}}) + }).collect::>()} + }), + "status" => match made.resources.get(&id) { + Some(resource) => json!({ + "state": resource["state"], + "address": resource["address"], + "endpoints": endpoints(&kind, resource["address"].as_str().unwrap_or("")), + }), + None => json!({"state": "gone"}), + }, + "create" => { + // Asked again with its token, it is the one made before. + let token = request["token"].as_str().unwrap_or(""); + if let Some((id, resource)) = made + .resources + .iter() + .find(|(_, resource)| resource["token"] == token) + { + return Ok(json!({"id": id, "address": resource["address"], "port": 22})); + } + say(progress, &format!("Allocating a simulated {kind}")); + pause(1.0); + made.next += 1; + let id = format!("sim-{}", made.next); + let address = format!("192.0.2.{}", 100 + made.next % 150); + say(progress, &format!("Starting {id} at {address}")); + pause(1.0); + made.resources.insert( + id.clone(), + json!({ + "kind": kind, + "name": request["name"], + "host": request["host"], + "token": token, + "state": "running", + "address": address, + "arguments": request["arguments"], + }), + ); + say(progress, &format!("{id} is running")); + match kind.as_str() { + "machine" => json!({"id": id, "address": address, "port": 22}), + _ => json!({"id": id, "endpoints": endpoints(&kind, &address)}), + } + } + "start" | "stop" => { + let state = if verb == "start" { + "running" + } else { + "stopped" + }; + say( + progress, + &format!( + "{}ing {id}", + if verb == "start" { "Start" } else { "Stopp" } + ), + ); + pause(1.0); + let resource = set(made, state)?; + json!({"state": state, "endpoints": endpoints(&kind, resource["address"].as_str().unwrap_or(""))}) + } + "change" => { + say(progress, &format!("Changing {id}")); + pause(1.0); + let mut resource = found(made)?; + resource["arguments"] = request["arguments"].clone(); + made.resources.insert(id.clone(), resource.clone()); + json!({"state": resource["state"]}) + } + "destroy" => { + say(progress, &format!("Removing {id}")); + pause(1.0); + made.resources.remove(&id); + json!({}) + } + // The log is the answer itself, as text. + "logs" => { + let resource = found(made).unwrap_or_default(); + let state = resource["state"].as_str().unwrap_or("gone"); + json!(format!("{id}: simulated boot\n{id}: {state}\n")) + } + "exec" => { + if found(made)?["state"] != "running" { + return Err(format!("{id} is not running").into()); + } + let words: Vec<&str> = request["command"] + .as_array() + .map(|words| words.iter().filter_map(Value::as_str).collect()) + .unwrap_or_default(); + match words.as_slice() { + ["echo", rest @ ..] => say(progress, &rest.join(" ")), + ["hostname"] => say(progress, &id), + ["false"] => return Ok(json!({"exit_code": 1})), + ["true"] => {} + other => { + say( + progress, + &format!("{}: not a command of a simulated service", other.join(" ")), + ); + return Ok(json!({"exit_code": 127})); + } + } + json!({"exit_code": 0}) + } + "list" => json!({"resources": made.resources.iter().map(|(id, resource)| json!({ + "id": id, + "kind": resource["kind"], + "name": resource["name"], + "host": resource["host"], + "state": resource["state"], + })).collect::>()}), + "act" => match request["action"]["name"].as_str().unwrap_or("") { + "images" => json!({ + "message": format!("{} images", IMAGES.len()), + "details": IMAGES.iter().map(|(name, size)| json!({"label": name, "value": size})).collect::>(), + }), + "inspect" => { + let wanted = request["action"]["arguments"]["image"] + .as_str() + .unwrap_or(""); + match IMAGES.iter().find(|(name, _)| *name == wanted) { + Some((name, size)) => json!({"message": format!("{name}: {size}")}), + None => return Err(format!("No image {wanted}").into()), + } + } + "restart" => { + say(progress, &format!("Restarting {id}")); + pause(1.0); + set(made, "running")?; + json!({"message": format!("{id} restarted")}) + } + other => return Err(format!("No action {other}").into()), + }, + other => return Err(format!("The simulated provisioner has no verb {other}").into()), + }) +} + +// ── What a provision waits for ────────────────────────────────────────────── + +/// How a new machine of the simulated provisioner is waited for and set up: +/// it answers at once and is applied by the simulation. +pub fn options() -> crate::provision::Options { + crate::provision::Options { + every: (delay() / 4).max(Duration::from_millis(20)), + boot: Duration::from_secs(60), + ssh: Duration::from_secs(60), + reaches: |_, _| true, + answers: |_, _| true, + first_boot: |_, _, _| Ok(String::new()), + finish: |paths, host, setup, out| { + let simulation = active().ok_or("No simulation runs")?; + let done = match setup { + Setup::Apply => simulation.apply(paths, host, out), + Setup::Seed => simulation.record(paths, host, level(host)).is_ok(), + }; + match done { + true => Ok(()), + false => Err(format!("Applying {} failed", host.name).into()), + } + }, + ..Default::default() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_seed_names_hosts_provisioners_and_secrets() { + let seed = Seed::parse( + r#"{"token": "0123456789abcdef0123456789abcdef", + "hosts": {"studio": {"applied": true}, + "dock": {"profile": {"type": "node", "access": {"address": "192.0.2.7", "user": "admin"}}}}, + "provisioners": {"cloud": {"type": "sim/cloud", "settings": {"region": "sim-2"}}}, + "secrets": {"api_token": "made-up"}}"#, + ) + .unwrap(); + let profiles = seed.profiles(); + let (name, dock, applied) = &profiles[0]; + assert_eq!((name.as_str(), *applied), ("dock", false)); + assert_eq!( + dock["access"], + json!({"address": "192.0.2.7", "user": "admin"}) + ); + assert_eq!(dock["hostname"], "dock"); + let (name, studio, applied) = &profiles[1]; + assert_eq!((name.as_str(), *applied), ("studio", true)); + assert_eq!(studio["access"]["address"], "192.0.2.21"); + assert_eq!(seed.provisioners["cloud"].kind, "sim/cloud"); + assert_eq!(seed.secrets["api_token"], "made-up"); + } + + #[test] + fn a_seed_that_is_wrong_says_where() { + let wrong = |text: &str| Seed::parse(text).unwrap_err().to_string(); + assert!(wrong(r#"{"token": "short"}"#).contains("32 characters")); + assert!(wrong(r#"{"hosts": {"a/b": {}}}"#).contains("hosts.a/b")); + assert!(wrong(r#"{"hosts": {"studio": {"profile": []}}}"#).contains("is an object")); + assert!(wrong(r#"{"host": {}}"#).contains("unknown field")); + assert!(Seed::parse("{}").unwrap().hosts.is_empty()); + } + + #[test] + fn berths_own_directories_and_others_in_use_are_refused() { + let scratch = tempfile::tempdir().unwrap(); + let own = Paths { + config_dir: scratch.path().join("home/.config/berth"), + data_dir: scratch.path().join("home/.local/share/berth"), + }; + fs::create_dir_all(&own.config_dir).unwrap(); + let refused = |dir: &Path| check_dir(dir, Some(&own)).unwrap_err().to_string(); + assert!(refused(&own.config_dir).contains("berth's own")); + assert!(refused(&own.data_dir.join("sim")).contains("berth's own")); + assert!(refused(&scratch.path().join("home")).contains("berth's own")); + + let other = scratch.path().join("other"); + fs::create_dir_all(&other).unwrap(); + check_dir(&other, Some(&own)).unwrap(); + check_dir(&scratch.path().join("new"), Some(&own)).unwrap(); + fs::write(other.join("notes.txt"), "mine").unwrap(); + assert!(refused(&other).contains("something else")); + fs::write(other.join(MARKER), "").unwrap(); + check_dir(&other, Some(&own)).unwrap(); + } + + #[test] + fn the_provisioner_moves_what_it_made_through_its_states() { + let mut made = Made::default(); + let mut progress = vec![]; + let mut ask = |verb: &str, request: Value| { + answer(verb, &request, &mut made, &mut progress, Duration::ZERO) + }; + let created = ask( + "create", + json!({"kind": "service", "name": "web", "token": "t1"}), + ) + .unwrap(); + let id = created["id"].as_str().unwrap().to_owned(); + assert_eq!(created["endpoints"][0]["port"], 80); + assert!( + created["endpoints"][0]["address"] + .as_str() + .unwrap() + .starts_with("192.0.2.") + ); + let again = ask("create", json!({"kind": "service", "token": "t1"})).unwrap(); + assert_eq!(again["id"], id.as_str()); + let stopped = ask("stop", json!({"kind": "service", "id": id})).unwrap(); + assert_eq!(stopped["state"], "stopped"); + assert_eq!( + ask("status", json!({"kind": "service", "id": id})).unwrap()["state"], + "stopped" + ); + assert!(ask("exec", json!({"id": id, "command": ["echo", "hi"]})).is_err()); + ask("start", json!({"kind": "service", "id": id})).unwrap(); + let ran = ask("exec", json!({"id": id, "command": ["echo", "hi"]})).unwrap(); + assert_eq!(ran["exit_code"], 0); + let listed = ask("list", json!({})).unwrap(); + assert_eq!(listed["resources"][0]["state"], "running"); + let status = ask("status", json!({})).unwrap(); + assert_eq!( + status["collections"]["images"].as_array().unwrap().len(), + IMAGES.len() + ); + ask("destroy", json!({"id": id})).unwrap(); + assert_eq!(ask("status", json!({"id": id})).unwrap()["state"], "gone"); + let said = String::from_utf8(progress).unwrap(); + assert!(said.contains("hi") && said.contains("Removing"), "{said}"); + } +} diff --git a/crates/berth-core/src/tailnet.rs b/crates/berth-core/src/tailnet.rs index 5605dd5..3172d2e 100644 --- a/crates/berth-core/src/tailnet.rs +++ b/crates/berth-core/src/tailnet.rs @@ -145,6 +145,9 @@ impl Control { /// Why berth cannot create keys at this control server, if it cannot. /// Only what berth knows without asking the server. pub fn problem(&self) -> Option { + if crate::simulate::active().is_some() { + return None; + } let credential = match &self.credential { Ok(credential) => credential, Err(why) => return Some(why.clone()), @@ -170,6 +173,13 @@ impl Control { /// A new pre-approved key that joins one host, once, within `lifetime` seconds. pub fn create_key(&self, description: &str, lifetime: u64) -> Result { + // A simulated server's keys join nothing. + if crate::simulate::active().is_some() { + return Ok(format!( + "tskey-auth-simulated-{}", + crate::provision::new_token()? + )); + } if let Some(problem) = self.problem() { return Err(problem.into()); } @@ -320,6 +330,9 @@ enum Body { /// login is a line of curl's configuration, given on its standard input, and /// the body is a private file, so neither appears in the process list. fn call(url: &str, login: &str, body: Option) -> Result<(u16, Value)> { + if crate::simulate::active().is_some() { + return Err("A simulated server reaches no tailnet".into()); + } let mut file = tempfile::NamedTempFile::new()?; let mut command = Command::new("curl"); command.args([ diff --git a/crates/berth-page/src/app.rs b/crates/berth-page/src/app.rs index 71bc2b6..ade5fe1 100644 --- a/crates/berth-page/src/app.rs +++ b/crates/berth-page/src/app.rs @@ -293,6 +293,7 @@ impl App { let version = said["version"].as_str().unwrap_or(""); let api = said["api"].as_u64().unwrap_or(0) as u32; *older.borrow_mut() = berth_api::compat::older_server(&url, version, api); + berth_tui::screen::mark_simulated(said["simulated"] == true); } }); // Who the token is here decides what the page offers. diff --git a/crates/berth-tui/src/screen.rs b/crates/berth-tui/src/screen.rs index 27dd1f7..b86209e 100644 --- a/crates/berth-tui/src/screen.rs +++ b/crates/berth-tui/src/screen.rs @@ -64,6 +64,14 @@ pub fn wrap(text: &str, width: usize) -> Vec { } /// The top line: the name, the tabs and what is open. Returns the tabs' areas. +/// Whether the server the screens use is a simulated one, which the header says. +static SIMULATED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + +/// Say in the header, from now on, whether the server is simulated. +pub fn mark_simulated(simulated: bool) { + SIMULATED.store(simulated, std::sync::atomic::Ordering::Relaxed); +} + pub fn draw_header( frame: &mut Frame, area: Rect, @@ -78,6 +86,13 @@ pub fn draw_header( Span::styled(" berth ", Style::new().fg(theme::TEXT).bold()), ]; let mut x = area.x + 11; + if SIMULATED.load(std::sync::atomic::Ordering::Relaxed) { + spans.push(Span::styled( + "simulated ", + Style::new().fg(theme::WARN).bold(), + )); + x += 11; + } let mut areas = vec![]; for (index, tab) in tabs.iter().enumerate() { let width = tab.len() as u16 + 4; diff --git a/crates/berth/src/cli/args.rs b/crates/berth/src/cli/args.rs index cc46a3f..d386041 100644 --- a/crates/berth/src/cli/args.rs +++ b/crates/berth/src/cli/args.rs @@ -185,6 +185,10 @@ pub enum Command { #[cfg(target_os = "linux")] #[command(hide = true)] TailnetKey { host: String }, + /// The provisioner of a simulated server, which it runs itself. + #[cfg(target_os = "linux")] + #[command(hide = true)] + SimulatedProvisioner { verb: String }, } // ── host ──────────────────────────────────────────────────────────────────── @@ -960,6 +964,21 @@ pub struct ServerRun { /// and containers and units of that version still name it. #[arg(long, hide = true)] pub web: bool, + /// Run a simulated server to test an application against: the real API on a scratch store, with hosts and provisioners that reach no machine. + #[arg(long)] + pub simulate: bool, + /// A JSON file with the hosts, provisioners, secrets and token the simulated server starts with. + #[arg(long, value_name = "FILE", requires = "simulate")] + pub seed: Option, + /// Seconds a simulated step takes, 2 unless given; 0 for none. + #[arg(long, value_name = "SECONDS", requires = "simulate")] + pub delay: Option, + /// Keep the simulated server's state in DIR, an empty one or one of an earlier simulation, instead of a scratch directory. + #[arg(long, value_name = "DIR", requires = "simulate")] + pub data: Option, + /// Leave the scratch directory when the simulated server ends. + #[arg(long, requires = "simulate")] + pub keep: bool, } #[cfg(target_os = "linux")] diff --git a/crates/berth/src/cli/mod.rs b/crates/berth/src/cli/mod.rs index 6c30f91..c1a89be 100644 --- a/crates/berth/src/cli/mod.rs +++ b/crates/berth/src/cli/mod.rs @@ -327,6 +327,8 @@ fn run(cli: Cli) -> Result<()> { )), #[cfg(target_os = "linux")] Command::TailnetKey { host } => crate::local::tailnet_key(&host), + #[cfg(target_os = "linux")] + Command::SimulatedProvisioner { verb } => crate::local::simulated_provisioner(&verb), } } diff --git a/crates/berth/src/local/mod.rs b/crates/berth/src/local/mod.rs index 518924f..c2856b6 100644 --- a/crates/berth/src/local/mod.rs +++ b/crates/berth/src/local/mod.rs @@ -164,6 +164,20 @@ pub fn tailnet_key(host: &str) -> Done<()> { Ok(()) } +/// `berth simulated-provisioner VERB`: the provisioner of a simulated +/// server, which berth runs as the plugin `sim`, with the request on +/// standard input. +pub fn simulated_provisioner(verb: &str) -> Done<()> { + let mut input = String::new(); + std::io::Read::read_to_string(&mut std::io::stdin(), &mut input)?; + let answer = berth_core::simulate::provision(verb, &input, &mut std::io::stderr())?; + match answer { + serde_json::Value::String(log) => print!("{log}"), + answer => println!("{answer}"), + } + Ok(()) +} + /// `berth registry publish`: upload the release archive built here, and /// make it the release address of a host through the server. pub fn publish( @@ -221,6 +235,15 @@ pub const WEB_GONE: &str = "--web does nothing any more: berth has no page in th /// `berth server …`: the server of this machine itself. pub fn server_command(command: args::Server) -> Done<()> { + // Before berth's own directories are looked for, which it never uses. + if let args::Server::Run(run) = &command + && (run.simulate || serve::simulate::asked()) + { + if run.web { + out::warn(WEB_GONE); + } + return Ok(serve::simulate::command(run)?); + } let paths = Paths::locate()?; match command { args::Server::Run(run) => { diff --git a/crates/berth/src/local/serve/actions.rs b/crates/berth/src/local/serve/actions.rs index 4ddec5f..54e9a87 100644 --- a/crates/berth/src/local/serve/actions.rs +++ b/crates/berth/src/local/serve/actions.rs @@ -172,6 +172,26 @@ pub(super) static INSTALLATION: &[Builtin] = &[ }, ]; +/// berth's own actions that a simulated server does not simulate: each logs +/// in to a machine or works with this one's drives. +const NOT_SIMULATED: &[&str] = &[ + "seed", + "trust-key", + "host-key", + "installer", + "rollout", + "rotate-key", + "adopt", +]; + +/// The action as a simulated server offers it. +fn simulated(action: Action) -> Action { + let off = + berth_core::simulate::active().is_some() && NOT_SIMULATED.contains(&action.name.as_str()); + let why = format!("berth serve --simulate does not simulate {}", action.name); + unavailable(action, off.then_some(why)) +} + fn unavailable(mut action: Action, why: Option) -> Action { if let Some(why) = why { action.available = false; @@ -274,7 +294,7 @@ fn host_list(seen: &Seen) -> Vec { .filter(|builtin| { builtin.name != UPGRADE_SERVER || listed.iter().any(|kept| kept.name == UPGRADE_SERVER) }) - .map(|builtin| (builtin.describe)(seen)) + .map(|builtin| simulated((builtin.describe)(seen))) .collect(); let mut actions: Vec = Action::of_host(listed) .into_iter() @@ -511,7 +531,7 @@ fn installation_list() -> Vec { }; INSTALLATION .iter() - .map(|builtin| (builtin.describe)(&seen)) + .map(|builtin| simulated((builtin.describe)(&seen))) .collect() } @@ -731,9 +751,17 @@ fn install_controller(state: Arc, name: String, act: Act) -> Run { request.token = act.token; request.secrets = password.iter().cloned().collect(); let paths = state.paths.clone(); - let work = Work::Function(Box::new(move |out| { - apply::install_controller(&paths, &host, password.as_deref(), out) - })); + let work = match berth_core::simulate::active() { + Some(simulation) => Work::Function(Box::new(move |out| { + let done = simulation.install_controller(&paths, &host, out); + // What it was seen to be before is not so any more. + *remote.seen.lock().unwrap() = None; + done + })), + None => Work::Function(Box::new(move |out| { + apply::install_controller(&paths, &host, password.as_deref(), out) + })), + }; let job = state.runner.start(request, work).map_err(refused)?; Ok(Acted { job: Some(job_info(&state.paths, job)), @@ -758,6 +786,21 @@ fn apply(state: Arc, name: String, act: Act) -> Run { format!("{name} asks for a sudo password: send it as sudo_password"), )); } + if let Some(simulation) = berth_core::simulate::active() { + let mut request = Request::new(Subject::Host(name), "apply"); + request.token = act.token; + let paths = state.paths.clone(); + let work = Work::Function(Box::new(move |out| { + let done = simulation.apply(&paths, &host, out); + *remote.seen.lock().unwrap() = None; + done + })); + let job = state.runner.start(request, work).map_err(refused)?; + return Ok(Acted { + job: Some(job_info(&state.paths, job)), + ..Acted::default() + }); + } let prepared = apply::prepare(&state.paths, &host, password.as_deref()).map_err(bad)?; let notes = prepared.notes; let mut request = Request::new(Subject::Host(name.clone()), "apply"); diff --git a/crates/berth/src/local/serve/api.rs b/crates/berth/src/local/serve/api.rs index b027344..1f67f9e 100644 --- a/crates/berth/src/local/serve/api.rs +++ b/crates/berth/src/local/serve/api.rs @@ -362,6 +362,9 @@ fn guarded_routes() -> OpenApiRouter> { .routes(routes!(kv_get, kv_put, kv_delete)) } +/// The header on every answer of a simulated server. +pub const SIMULATED: &str = "berth-simulated"; + /// Where a host's controller's operations are passed through: berthd's /// `/v1/PATH` is `/v1/hosts/NAME/controller/PATH` here. pub const HOST_PREFIX: &str = "/v1/hosts/{name}/controller/{*rest}"; @@ -395,6 +398,15 @@ pub fn router(state: Arc, guarded: bool) -> Router { router = router.route("/", get(front)); // Added after the guard, which they are not behind: they check their key themselves. router = router.merge(keyed_routes().split_for_parts().0); + // Every answer of a simulated server says so, refusals included. + if berth_core::simulate::active().is_some() { + router = router.layer(middleware::map_response(async |mut response: Response| { + response + .headers_mut() + .insert(SIMULATED, HeaderValue::from_static("1")); + response + })); + } router .layer(DefaultBodyLimit::max(64 * 1024 * 1024)) .with_state(state) @@ -659,6 +671,7 @@ async fn version() -> Json { mode: "serve".into(), applied: None, host_type: None, + simulated: berth_core::simulate::active().is_some(), }) } diff --git a/crates/berth/src/local/serve/connect.rs b/crates/berth/src/local/serve/connect.rs index 0cfbcf4..137662e 100644 --- a/crates/berth/src/local/serve/connect.rs +++ b/crates/berth/src/local/serve/connect.rs @@ -344,6 +344,16 @@ pub async fn connect( ), )); } + if berth_core::simulate::active().is_some() { + return Err(Fail::new( + code::NOT_AVAILABLE, + format!( + "berth serve --simulate dials no endpoint: {} of {} leads nowhere", + endpoint.name, + kept.title() + ), + )); + } let connection = Dialler::of(&state.paths, &kept) .dial(&state, &endpoint) .await?; diff --git a/crates/berth/src/local/serve/mod.rs b/crates/berth/src/local/serve/mod.rs index 9302ef7..e599b49 100644 --- a/crates/berth/src/local/serve/mod.rs +++ b/crates/berth/src/local/serve/mod.rs @@ -12,6 +12,7 @@ mod provisioners; mod resources; mod setup; pub mod setup_key; +pub mod simulate; #[cfg(test)] mod tests; @@ -215,8 +216,13 @@ impl Remote { let Some(link) = &self.link else { return Err(self.problem.clone().unwrap_or_else(|| "no link".into())); }; + // A simulated host changes within a step, which tests wait for. + let fresh = match berth_core::simulate::active() { + Some(_) => berth_core::simulate::delay().min(SEEN_FOR), + None => SEEN_FOR, + }; if let Some((when, answer)) = &*self.seen.lock().unwrap() - && when.elapsed() < SEEN_FOR + && when.elapsed() < fresh { return answer.clone(); } @@ -586,9 +592,7 @@ pub fn command(paths: &Paths, args: &[&str]) -> Result<()> { let listen = match listen { None => None, Some(address) => { - let address: std::net::SocketAddr = address - .parse() - .map_err(|_| format!("Not an address and port: {address}"))?; + let address = parse_listen(address)?; if !address.ip().is_loopback() && !expose { return Err(format!( "{address} can be reached from other machines, over plain HTTP: add --expose to allow it, or use 127.0.0.1 or [::1]" @@ -598,6 +602,22 @@ pub fn command(paths: &Paths, args: &[&str]) -> Result<()> { Some(address) } }; + run(paths, listen, false) +} + +pub(crate) fn parse_listen(address: &str) -> Result { + Ok(address + .parse() + .map_err(|_| format!("Not an address and port: {address}"))?) +} + +/// Serve until told to stop. A simulated server says how to log in to it, +/// and its machines are those of the simulation. +pub(crate) fn run( + paths: &Paths, + listen: Option, + simulated: bool, +) -> Result<()> { let _lock = lock(paths)?; let store = open_store(paths)?; store.end_lost_jobs()?; @@ -612,9 +632,14 @@ pub fn command(paths: &Paths, args: &[&str]) -> Result<()> { Some(_) => Some(token(paths)?), None => None, }; - let auth = auth::Auth::configured(paths, token)?; + let auth = auth::Auth::configured(paths, token.clone())?; let admitted = auth.describe(); - let state = Serve::with(paths.clone(), store, auth); + let mut state = Serve::with(paths.clone(), store, auth); + if simulated { + let serving = Arc::get_mut(&mut state).expect("nothing else holds it yet"); + serving.provision = berth_core::simulate::options; + serving.known_hosts = Mutex::new(None); + } std::fs::create_dir_all(&state.jobs_dir)?; let runtime = tokio::runtime::Builder::new_multi_thread() .enable_all() @@ -658,6 +683,12 @@ pub fn command(paths: &Paths, args: &[&str]) -> Result<()> { tcp.local_addr()?, state.paths.data_dir.join("serve.token").display() ); + // A simulated server's token is a throw-away one, for the tests that use it. + if let (true, Some(token)) = (simulated, &token) { + println!("Token: {token}"); + let (key, _) = setup_key::make(&state.paths)?; + println!("Setup key: {key}"); + } let guarded = api::router(state.clone(), true); Some(tokio::spawn(async move { axum::serve(tcp, guarded).await })) } @@ -665,8 +696,16 @@ pub fn command(paths: &Paths, args: &[&str]) -> Result<()> { }; let known = state.hosts.read().unwrap().len(); println!("{known} host{}", if known == 1 { "" } else { "s" }); + // A simulated server cleans up after itself when a test ends it. + let mut terminate = match simulated { + true => Some(tokio::signal::unix::signal( + tokio::signal::unix::SignalKind::terminate(), + )?), + false => None, + }; tokio::select! { _ = tokio::signal::ctrl_c() => {} + _ = async { match &mut terminate { Some(signal) => { signal.recv().await; } None => std::future::pending().await } } => {} _ = over_unix => {} _ = async { match over_tcp { Some(task) => { let _ = task.await; } None => std::future::pending().await } } => {} } diff --git a/crates/berth/src/local/serve/simulate.rs b/crates/berth/src/local/serve/simulate.rs new file mode 100644 index 0000000..0e8b240 --- /dev/null +++ b/crates/berth/src/local/serve/simulate.rs @@ -0,0 +1,102 @@ +//! `berth serve --simulate`: the real server on a scratch store, whose hosts +//! and provisioners are those of [`berth_core::simulate`]. + +use crate::Result; +use crate::cli::args::ServerRun; +use berth_core::{ + paths::{Paths, layout}, + simulate::{self, Seed, Simulation}, +}; +use std::path::{Path, PathBuf}; + +/// Set to 1, it makes `berth serve` a simulated server, as for a container +/// whose command cannot be changed, such as a service of GitHub Actions. +pub const ASKED: &str = "BERTH_SIMULATE"; +/// The seed itself, as JSON, when no file is given. +pub const SEED: &str = "BERTH_SIMULATE_SEED"; + +/// Whether the environment asks for a simulated server. +pub fn asked() -> bool { + std::env::var(ASKED).is_ok_and(|value| value.trim() == "1") +} + +/// The directories the simulated server keeps its state in, under `root`. +/// They are set in the environment, so that its provisioner and anything +/// else it starts find them, and never the user's own. +fn take_over(root: &Path) -> Result { + let run = root.join("run"); + { + use std::os::unix::fs::DirBuilderExt; + std::fs::DirBuilder::new() + .recursive(true) + .mode(0o700) + .create(&run)?; + } + // SAFETY: berth starts no thread before a server command. + unsafe { + std::env::set_var("XDG_CONFIG_HOME", root.join("config")); + std::env::set_var("XDG_DATA_HOME", root.join("data")); + std::env::set_var("XDG_RUNTIME_DIR", &run); + std::env::remove_var("BERTH_SERVER"); + } + Paths::locate() +} + +/// Where the simulation runs: the directory given, or a new scratch one. +fn root(given: Option<&Path>) -> Result<(PathBuf, Option)> { + let own = Paths::locate().ok(); + let (root, scratch) = match given { + Some(dir) => (std::path::absolute(dir)?, None), + None => { + let scratch = tempfile::Builder::new() + .prefix("berth-simulated-") + .tempdir()?; + (scratch.path().to_owned(), Some(scratch)) + } + }; + simulate::check_dir(&root, own.as_ref())?; + std::fs::create_dir_all(&root)?; + std::fs::write(root.join(simulate::MARKER), "")?; + Ok((root, scratch)) +} + +pub fn command(run: &ServerRun) -> Result<()> { + let listen = run.listen.as_deref().map(super::parse_listen).transpose()?; + let seed = match (&run.seed, std::env::var(SEED)) { + (Some(file), _) => Seed::read(file)?, + (None, Ok(text)) if !text.trim().is_empty() => { + Seed::parse(&text).map_err(|error| format!("{SEED}: {error}"))? + } + (None, _) => Seed::default(), + }; + if let Some(delay) = run.delay { + if !delay.is_finite() || delay < 0.0 { + return Err("--delay is a number of seconds, 0 or more".into()); + } + // SAFETY: berth starts no thread before a server command. + unsafe { std::env::set_var(simulate::DELAY, delay.to_string()) }; + } + let (root, scratch) = root(run.data.as_deref())?; + let paths = take_over(&root)?; + paths.prepare()?; + let berthd = paths.source()?.join(layout::BERTHD); + let simulation = Simulation::begin(&paths, &berthd)?; + simulate::write_plugin(&paths, &std::env::current_exe()?)?; + let planted = seed.plant(&paths, simulation); + println!( + "Simulated berth server: its hosts and provisioners reach no machine. State in {}{}", + root.display(), + match (&scratch, run.keep) { + (Some(_), false) => ", removed when it ends", + _ => "", + } + ); + let served = planted.and_then(|()| super::run(&paths, listen, true)); + simulation.stop(); + if let Some(scratch) = scratch + && run.keep + { + let _ = scratch.keep(); + } + served +} diff --git a/crates/berth/tests/simulate.rs b/crates/berth/tests/simulate.rs new file mode 100644 index 0000000..e4dc4b1 --- /dev/null +++ b/crates/berth/tests/simulate.rs @@ -0,0 +1,360 @@ +//! `berth serve --simulate` as an application's test sees it: started from a +//! seed, logged in to over HTTP with its token, and driven through a host's +//! whole life, with the events of the portal followed. It runs the berth +//! that was built, in scratch directories, with made-up hosts. +#![cfg(target_os = "linux")] + +use berth_api::client::{self, Client}; +use berth_api::portal::{Piece, Portal, PortalStream}; +use serde_json::{Value, json}; +use std::io::{BufRead, BufReader}; +use std::path::{Path, PathBuf}; +use std::process::{Child, Command, Stdio}; +use std::time::{Duration, Instant}; + +const TOKEN: &str = "made-up-token-of-a-simulated-server-0123"; + +/// A simulated server, ended with SIGTERM when dropped. +struct Simulated { + child: Child, + address: String, + state: PathBuf, + _scratch: tempfile::TempDir, +} + +impl Drop for Simulated { + fn drop(&mut self) { + terminate(&mut self.child); + } +} + +fn terminate(child: &mut Child) { + // SAFETY: a plain system call on a child of ours. + unsafe { libc::kill(child.id() as i32, libc::SIGTERM) }; + let deadline = Instant::now() + Duration::from_secs(10); + while Instant::now() < deadline { + if let Ok(Some(_)) = child.try_wait() { + return; + } + std::thread::sleep(Duration::from_millis(50)); + } + let _ = child.kill(); + let _ = child.wait(); +} + +impl Simulated { + /// Started with `seed`, its scratch state in a directory of its own. + fn start(seed: &Value) -> Simulated { + let scratch = tempfile::Builder::new().prefix("bs").tempdir().unwrap(); + let path = scratch.path(); + for sub in ["r", "c", "d", "home", "tmp"] { + std::fs::create_dir_all(path.join(sub)).unwrap(); + } + std::fs::write(path.join("seed.json"), seed.to_string()).unwrap(); + let mut child = Command::new(env!("CARGO_BIN_EXE_berth")) + .args(["serve", "--simulate", "--listen", "127.0.0.1:0"]) + .args(["--delay", "0.1", "--seed"]) + .arg(path.join("seed.json")) + .env_clear() + .env("PATH", std::env::var_os("PATH").unwrap_or_default()) + .env("HOME", path.join("home")) + .env("TMPDIR", path.join("tmp")) + .env("XDG_RUNTIME_DIR", path.join("r")) + .env("XDG_CONFIG_HOME", path.join("c")) + .env("XDG_DATA_HOME", path.join("d")) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::inherit()) + .spawn() + .unwrap(); + let mut lines = BufReader::new(child.stdout.take().unwrap()).lines(); + let (mut address, mut state, mut token) = (None, None, None); + while address.is_none() || token.is_none() { + let line = lines + .next() + .expect("the server says where it listens") + .unwrap(); + if let Some(rest) = line.strip_prefix("Listening on http://") { + address = rest.split_whitespace().next().map(str::to_owned); + } else if let Some(rest) = line.strip_prefix("Token: ") { + token = Some(rest.to_owned()); + } else if let Some(rest) = line.split("State in ").nth(1) { + state = rest.split(',').next().map(PathBuf::from); + } + } + assert_eq!(token.as_deref(), Some(TOKEN), "the seed's token"); + // The rest of what it says is not read. + std::thread::spawn(move || lines.for_each(drop)); + Simulated { + child, + address: address.unwrap(), + state: state.expect("it says where its state is"), + _scratch: scratch, + } + } + + async fn send(&self, method: &str, path: &str, body: Option) -> client::Response { + let stream = tokio::net::TcpStream::connect(&self.address).await.unwrap(); + let mut client = Client::over(stream).await.unwrap(); + let bearer = format!("Bearer {TOKEN}"); + let mut headers = vec![("authorization", bearer.as_str())]; + let body = match body { + Some(body) => { + headers.push(("content-type", "application/json")); + body.to_string().into_bytes() + } + None => vec![], + }; + client + .send(method, path, &headers, body.into()) + .await + .unwrap() + } + + /// A call that has to work, and its answer. + async fn call(&self, method: &str, path: &str, body: Option) -> Value { + let response = self.send(method, path, body).await; + assert!( + (200..300).contains(&response.status), + "{method} {path}: {} {}", + response.status, + String::from_utf8_lossy(&response.body) + ); + assert_eq!(response.headers["berth-simulated"], "1"); + serde_json::from_slice(&response.body).unwrap() + } + + async fn portal(&self) -> Portal { + let stream = tokio::net::TcpStream::connect(&self.address).await.unwrap(); + let socket = client::websocket_at(stream, &self.address, "/v1/portal", TOKEN) + .await + .unwrap(); + Portal::over(socket) + } + + /// The job once it has ended, which has to be with 0, and its output. + async fn job_done(&self, acted: &Value) -> (Value, String) { + let id = acted["job"]["id"].as_str().expect("a job").to_owned(); + let deadline = Instant::now() + Duration::from_secs(30); + loop { + let job = self.call("GET", &format!("/v1/jobs/{id}"), None).await; + if !job["ended"].is_null() { + let output = self + .call("GET", &format!("/v1/jobs/{id}/output"), None) + .await; + let data = berth_api::unb64(output["data_b64"].as_str().unwrap()).unwrap(); + let output = String::from_utf8_lossy(&data).into_owned(); + assert_eq!(job["exit_code"], 0, "{job}: {output}"); + return (job, output); + } + assert!(Instant::now() < deadline, "the job {id} ends in time"); + tokio::time::sleep(Duration::from_millis(50)).await; + } + } + + async fn act(&self, host: &str, action: &str) -> (Value, String) { + let path = format!("/v1/hosts/{host}/actions/{action}"); + let acted = self.call("POST", &path, Some(json!({}))).await; + self.job_done(&acted).await + } + + /// The host once `check` holds of it. + async fn host_until(&self, name: &str, check: impl Fn(&Value) -> bool) -> Value { + let deadline = Instant::now() + Duration::from_secs(20); + loop { + let host = self.call("GET", &format!("/v1/hosts/{name}"), None).await; + if check(&host) { + return host; + } + assert!(Instant::now() < deadline, "{name} is not as asked: {host}"); + tokio::time::sleep(Duration::from_millis(100)).await; + } + } +} + +/// The next change of the events stream that is `change` of `source`. +async fn change_of(events: &mut PortalStream, source: &str, change: &str) -> Value { + let mut seen = vec![]; + loop { + let piece = tokio::time::timeout(Duration::from_secs(20), events.recv()) + .await + .unwrap_or_else(|_| panic!("no {source} {change} after {seen:?}")); + match piece { + Some(Ok(Piece::Text(frame))) + if frame["source"] == source && frame["change"] == change => + { + return frame; + } + Some(Ok(Piece::Text(frame))) => seen.push(frame), + other => panic!("the events ended after {seen:?}: {other:?}"), + } + } +} + +fn gone(dir: &Path) -> bool { + let deadline = Instant::now() + Duration::from_secs(10); + while dir.exists() && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(50)); + } + !dir.exists() +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_host_lives_its_whole_life_on_a_simulated_server() { + let seed = json!({ + "token": TOKEN, + "hosts": {"studio": {"applied": true}}, + "secrets": {"api_token": "made-up"} + }); + let mut server = Simulated::start(&seed); + + // It says what it is, and lets nobody in without the token. + let version = server.call("GET", "/v1/version", None).await; + assert_eq!( + (&version["mode"], &version["simulated"]), + (&json!("serve"), &json!(true)) + ); + let stream = tokio::net::TcpStream::connect(&server.address) + .await + .unwrap(); + let refused = Client::over(stream) + .await + .unwrap() + .send("GET", "/v1/hosts", &[], Vec::new().into()) + .await + .unwrap(); + assert_eq!(refused.status, 401); + assert_eq!(refused.headers["berth-simulated"], "1"); + + // The seed's world. + let studio = server + .host_until("studio", |host| host["reachable"] == true) + .await; + assert_eq!( + (&studio["applied"], &studio["address"]), + (&json!(berth_api::VERSION), &json!("192.0.2.20")) + ); + let secrets = server.call("GET", "/v1/secrets", None).await; + assert!(secrets.to_string().contains("api_token"), "{secrets}"); + let provisioners = server.call("GET", "/v1/provisioners", None).await; + assert_eq!( + (&provisioners[0]["id"], &provisioners[0]["type"]), + (&json!("sim"), &json!("sim/cloud")) + ); + let shown = server.call("GET", "/v1/provisioners/sim", None).await; + assert!( + shown.to_string().contains("ubuntu-26.04"), + "its images: {shown}" + ); + + let portal = server.portal().await; + let mut events = portal.open("events.follow", None, json!({})).await.unwrap(); + events + .push(Piece::Text(json!({"changes": ["hosts", "resources"]}))) + .unwrap(); + + // A new host, made by the simulated provisioner and applied. + let profile = json!({"hostname": "dock", "builder": {}, "access": {"user": "ops"}}); + server + .call("PUT", "/v1/hosts/dock/profile", Some(profile)) + .await; + assert_eq!( + change_of(&mut events, "host", "added").await["host"], + "dock" + ); + let made = json!({"provisioner": "sim", "kind": "machine", "host": "dock"}); + let acted = server.call("POST", "/v1/resources", Some(made)).await; + let machine = acted["resource"]["id"].as_str().unwrap().to_owned(); + let (_, output) = server.job_done(&acted).await; + assert!(output.contains("✓ dock is applied"), "{output}"); + assert_eq!( + change_of(&mut events, "resource", "created").await["resource"], + machine.as_str() + ); + let dock = server + .host_until("dock", |host| host["applied"].is_string()) + .await; + assert!( + dock["address"].as_str().unwrap().starts_with("192.0.2."), + "{dock}" + ); + assert_eq!(dock["power"]["state"], "running"); + + // Applied again, with an action and a session of its controller. + let (_, output) = server.act("dock", "apply").await; + assert!(output.contains("PLAY RECAP dock"), "{output}"); + let (_, output) = server.act("dock", "status").await; + assert!(output.contains("dock: simulated"), "{output}"); + let started = portal + .call( + "sessions.start", + Some("dock"), + json!({"wait": "10s"}), + Some(json!({"kind": "process", "command": "hostname; echo hello; rm -rf /"})), + ) + .await + .unwrap(); + let said = |key: &str| { + String::from_utf8(berth_api::unb64(started[key].as_str().unwrap()).unwrap()).unwrap() + }; + assert_eq!(said("stdout_b64"), "dock\nhello\n"); + assert!( + said("stderr_b64").contains("rm: not a command"), + "{started}" + ); + assert_eq!(started["exit_code"], 127); + // Nothing of the machine it runs on is the host's. + let processes = server + .call("GET", "/v1/hosts/dock/controller/processes", None) + .await; + let state = server.state.to_string_lossy().into_owned(); + let commands: Vec<&str> = processes + .as_array() + .unwrap() + .iter() + .map(|process| process["command"].as_str().unwrap()) + .collect(); + assert!( + !commands.is_empty() && commands.iter().all(|command| command.contains(&state)), + "{commands:?}" + ); + let port = server + .send("GET", "/v1/hosts/dock/controller/ports/22", None) + .await; + let said = String::from_utf8_lossy(&port.body); + assert!(said.contains("no ports to reach"), "{said}"); + + // Stopped, it does not answer; started, it does again. + server.act("dock", "stop").await; + let stopped = server + .host_until("dock", |host| host["reachable"] == false) + .await; + assert_eq!(stopped["power"]["state"], "stopped"); + server.act("dock", "start").await; + server + .host_until("dock", |host| host["reachable"] == true) + .await; + + // Removed with its machine. + let acted = server + .call("DELETE", &format!("/v1/resources/{machine}"), None) + .await; + server.job_done(&acted).await; + assert_eq!( + change_of(&mut events, "resource", "removed").await["resource"], + machine.as_str() + ); + server.act("dock", "remove").await; + assert_eq!( + change_of(&mut events, "host", "removed").await["host"], + "dock" + ); + let hosts = server.call("GET", "/v1/hosts", None).await; + assert_eq!(hosts.as_array().unwrap().len(), 1, "{hosts}"); + + // Ended, it leaves nothing behind. + let state = server.state.clone(); + assert!(state.exists()); + terminate(&mut server.child); + assert!(gone(&state), "{} is removed", state.display()); +} diff --git a/crates/berthd/src/http.rs b/crates/berthd/src/http.rs index fbea5cd..f72acb9 100644 --- a/crates/berthd/src/http.rs +++ b/crates/berthd/src/http.rs @@ -113,6 +113,7 @@ async fn version(AppState(state): AppState>) -> Json { mode: mode.into(), applied, host_type, + simulated: state.config().berthd.simulated, }) } @@ -165,6 +166,34 @@ async fn host_only(Extension(caller): Extension, request: Request, next: next.run(request).await } +/// In the folder of a simulated host while its machine is stopped. +const STOPPED: &str = "stopped"; + +/// A simulated host whose machine is stopped answers nothing, as a stopped +/// machine would not. Its ports would be those of the machine it runs on, +/// which is not the host, so none are reached. +async fn simulated( + AppState(state): AppState>, + request: Request, + next: Next, +) -> Response { + if !state.config().berthd.simulated { + return next.run(request).await; + } + let stopped = state + .state_dir + .parent() + .is_some_and(|dir| dir.join(STOPPED).exists()); + if stopped { + return E::unavailable("The simulated machine is stopped").into_response(); + } + let path = request.uri().path(); + if path.starts_with("/v1/ports/") || path.starts_with("/v1/endpoints/") { + return E::unavailable("A simulated host has no ports to reach").into_response(); + } + next.run(request).await +} + pub fn router(state: Arc) -> Router { let mut router = Router::new(); for route in ROUTES { @@ -210,6 +239,10 @@ pub fn router(state: Arc) -> Router { router .route(ops::PORTAL, get(portal)) .fallback(|| async { E::missing("such call") }) + .layer(axum::middleware::from_fn_with_state( + state.clone(), + simulated, + )) .layer(axum::extract::DefaultBodyLimit::disable()) .with_state(state) } diff --git a/crates/berthd/src/system.rs b/crates/berthd/src/system.rs index d171517..24fedd5 100644 --- a/crates/berthd/src/system.rs +++ b/crates/berthd/src/system.rs @@ -443,7 +443,43 @@ pub async fn logs( })) } -pub async fn processes() -> Json> { +/// Whether a simulated host shows and signals only what berthd started: the +/// rest of the machine is not the host's. +fn own_tree(state: &State) -> bool { + state.config().berthd.simulated +} + +/// berthd and what descends from it, as /proc says now. +fn descendants() -> std::collections::BTreeSet { + let parents: Vec<(u32, u32)> = std::fs::read_dir("/proc") + .into_iter() + .flatten() + .flatten() + .filter_map(|entry| { + let pid = entry.file_name().to_str()?.parse::().ok()?; + let stat = read(&format!("/proc/{pid}/stat")); + let rest = &stat[stat.rfind(')')? + 1..]; + let parent = rest.split_whitespace().nth(1)?.parse().ok()?; + Some((pid, parent)) + }) + .collect(); + let mut tree = std::collections::BTreeSet::from([std::process::id()]); + // Each pass takes in the children of what is in already. + loop { + let before = tree.len(); + for (pid, parent) in &parents { + if tree.contains(parent) { + tree.insert(*pid); + } + } + if tree.len() == before { + return tree; + } + } +} + +pub async fn processes(AppState(state): AppState>) -> Json> { + let only = own_tree(&state).then(descendants); // SAFETY: sysconf only reads. let (tick, page) = unsafe { ( @@ -461,6 +497,9 @@ pub async fn processes() -> Json> { else { continue; }; + if only.as_ref().is_some_and(|only| !only.contains(&pid)) { + continue; + } let stat = read(&format!("/proc/{pid}/stat")); // The name is in brackets and may itself contain brackets and spaces. let (Some(open), Some(close)) = (stat.find('('), stat.rfind(')')) else { @@ -504,6 +543,7 @@ pub async fn processes() -> Json> { } pub async fn process_signal( + AppState(state): AppState>, Path(pid): Path, Json(signal): Json, ) -> R> { @@ -525,6 +565,9 @@ pub async fn process_signal( if pid <= 1 || pid as u32 == std::process::id() { return Err(E::bad("Not a process to signal")); } + if own_tree(&state) && !descendants().contains(&(pid as u32)) { + return Err(E::missing(format!("process {pid}"))); + } // SAFETY: a plain system call. if unsafe { libc::kill(pid, number) } != 0 { let error = std::io::Error::last_os_error(); diff --git a/crates/berthd/src/users.rs b/crates/berthd/src/users.rs index b4bc326..eef58a2 100644 --- a/crates/berthd/src/users.rs +++ b/crates/berthd/src/users.rs @@ -129,9 +129,18 @@ pub fn command( vec![shell.into(), "-c".into(), script.clone()] } }; - // The agent gets its display and tool paths from the wrapper the role installs. - let wrapper = state.config().berthd.agent_env.clone(); - if user == "agent" && account.is_some() && std::path::Path::new(&wrapper).exists() { + // The agent gets its display and tool paths from the wrapper the role + // installs. A simulated host's wrapper is its shell, for whoever runs berthd. + let config = state.config(); + let wrapper = config.berthd.agent_env.clone(); + let simulated = config.berthd.simulated && state.mode == Mode::Single; + let wrapped = std::path::Path::new(&wrapper).exists(); + if simulated && !wrapped { + return Err(E::unavailable(format!( + "The simulated host has no shell at {wrapper}" + ))); + } + if user == "agent" && (account.is_some() || simulated) && wrapped { words.insert(0, wrapper); } let mut command = Command::new(&words[0]); diff --git a/docs/content/docs/(guide)/reference/cli.mdx b/docs/content/docs/(guide)/reference/cli.mdx index 7dbf800..5ac2059 100644 --- a/docs/content/docs/(guide)/reference/cli.mdx +++ b/docs/content/docs/(guide)/reference/cli.mdx @@ -1380,13 +1380,18 @@ The berth server of this machine: running it, its setup key, its store and its A Serve the API over all hosts on a socket of this user, and over HTTP with --listen. ```text -berth server run [--listen ADDRESS:PORT] [--expose] +berth server run [--listen ADDRESS:PORT] [--expose] [--simulate] [--seed FILE] [--delay SECONDS] [--data DIR] [--keep] ``` | Argument | What it is | | --- | --- | | `--listen ADDRESS:PORT` | Also listen over HTTP, behind a token. | | `--expose` | Allow an address other than this machine's own. | +| `--simulate` | Run a simulated server to test an application against: the real API on a scratch store, with hosts and provisioners that reach no machine. | +| `--seed FILE` | A JSON file with the hosts, provisioners, secrets and token the simulated server starts with. | +| `--delay SECONDS` | Seconds a simulated step takes, 2 unless given; 0 for none. | +| `--data DIR` | Keep the simulated server's state in DIR, an empty one or one of an earlier simulation, instead of a scratch directory. | +| `--keep` | Leave the scratch directory when the simulated server ends. | ### berth server setup-key @@ -1510,13 +1515,18 @@ berth server deploy [USER@]HOST [--container NAME] [--port PORT] [--release VERS Short for berth server run. ```text -berth serve [--listen ADDRESS:PORT] [--expose] +berth serve [--listen ADDRESS:PORT] [--expose] [--simulate] [--seed FILE] [--delay SECONDS] [--data DIR] [--keep] ``` | Argument | What it is | | --- | --- | | `--listen ADDRESS:PORT` | Also listen over HTTP, behind a token. | | `--expose` | Allow an address other than this machine's own. | +| `--simulate` | Run a simulated server to test an application against: the real API on a scratch store, with hosts and provisioners that reach no machine. | +| `--seed FILE` | A JSON file with the hosts, provisioners, secrets and token the simulated server starts with. | +| `--delay SECONDS` | Seconds a simulated step takes, 2 unless given; 0 for none. | +| `--data DIR` | Keep the simulated server's state in DIR, an empty one or one of an earlier simulation, instead of a scratch directory. | +| `--keep` | Leave the scratch directory when the simulated server ends. | ## berth login diff --git a/docs/openapi/berth.openapi.json b/docs/openapi/berth.openapi.json index 2b2dfaf..5fbf046 100644 --- a/docs/openapi/berth.openapi.json +++ b/docs/openapi/berth.openapi.json @@ -601,6 +601,11 @@ }, "type": "array" }, + "simulated": { + "default": false, + "description": "Whether the host is simulated: in single-user mode its commands then\nrun through `agent_env` too, and its version says so.", + "type": "boolean" + }, "systemd": { "default": null, "description": "Whether the calls that need systemd work. Off in single-user mode\nunless set.", @@ -3455,6 +3460,10 @@ "description": "`system` or `single`.", "type": "string" }, + "simulated": { + "description": "Whether it is simulated, as `berth serve --simulate` and its hosts\nare: nothing it does reaches a real machine.", + "type": "boolean" + }, "type": { "oneOf": [ { diff --git a/docs/openapi/berth.operations.json b/docs/openapi/berth.operations.json index 0fefea4..c9e9b45 100644 --- a/docs/openapi/berth.operations.json +++ b/docs/openapi/berth.operations.json @@ -2446,6 +2446,10 @@ "description": "`system` or `single`.", "type": "string" }, + "simulated": { + "description": "Whether it is simulated, as `berth serve --simulate` and its hosts\nare: nothing it does reaches a real machine.", + "type": "boolean" + }, "type": { "oneOf": [ { diff --git a/docs/openapi/berthd.openapi.json b/docs/openapi/berthd.openapi.json index 77b4e37..f0c7e19 100644 --- a/docs/openapi/berthd.openapi.json +++ b/docs/openapi/berthd.openapi.json @@ -171,6 +171,11 @@ }, "type": "array" }, + "simulated": { + "default": false, + "description": "Whether the host is simulated: in single-user mode its commands then\nrun through `agent_env` too, and its version says so.", + "type": "boolean" + }, "systemd": { "default": null, "description": "Whether the calls that need systemd work. Off in single-user mode\nunless set.", @@ -1482,6 +1487,10 @@ "description": "`system` or `single`.", "type": "string" }, + "simulated": { + "description": "Whether it is simulated, as `berth serve --simulate` and its hosts\nare: nothing it does reaches a real machine.", + "type": "boolean" + }, "type": { "oneOf": [ { diff --git a/docs/openapi/berthd.operations.json b/docs/openapi/berthd.operations.json index 6203557..3a0ea7d 100644 --- a/docs/openapi/berthd.operations.json +++ b/docs/openapi/berthd.operations.json @@ -171,6 +171,11 @@ }, "type": "array" }, + "simulated": { + "default": false, + "description": "Whether the host is simulated: in single-user mode its commands then\nrun through `agent_env` too, and its version says so.", + "type": "boolean" + }, "systemd": { "default": null, "description": "Whether the calls that need systemd work. Off in single-user mode\nunless set.", @@ -1482,6 +1487,10 @@ "description": "`system` or `single`.", "type": "string" }, + "simulated": { + "description": "Whether it is simulated, as `berth serve --simulate` and its hosts\nare: nothing it does reaches a real machine.", + "type": "boolean" + }, "type": { "oneOf": [ { From e763745dfcdaa22392bba1d8ec321915b617a59b Mon Sep 17 00:00:00 2001 From: Helight Date: Sun, 15 Nov 2026 20:41:57 +0100 Subject: [PATCH 2/3] docs: testing against a simulated server --- AGENTS.md | 2 +- docs/content/docs/(guide)/extend/serve.mdx | 2 +- docs/content/docs/api/meta.json | 2 +- docs/content/docs/api/testing.mdx | 168 +++++++++++++++++++++ 4 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 docs/content/docs/api/testing.mdx diff --git a/AGENTS.md b/AGENTS.md index 9e495b8..d558649 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -8,7 +8,7 @@ berth sets up and looks after agent workstations. These are Ubuntu machines with | --- | --- | | `crates/berth/` | The `berth` program, for Linux, macOS and Windows. `main.rs` opens the screens, and `src/cli/` is the command line, the same on every system. In `cli/`, `args.rs` is the grammar in clap and `docs.rs` writes the reference page from it. `client.rs` reaches the server. `ask.rs` asks, `follow.rs` shows progress, and `out.rs` prints. On Linux, `src/local/` adds the commands of the server's own machine and `berth serve` (`src/local/serve/`), behind one `#[cfg(target_os = "linux")]`, and `client_only.rs` stands in for it elsewhere. In `serve/`, `here.rs` is the server in berth's own process that the screens and commands use when no other is set, `actions.rs` has the actions of hosts and of the installation, `keys.rs` the key ring's routes, `provisioners.rs` and `resources.rs` those of provisioners and resources | | `crates/berth-api/` | Types and the operations table shared by every program (`ops.rs` names each operation, its HTTP route and its role) | -| `crates/berth-core/` | Hosts, profiles, links to hosts, registries and secrets, and plugins. The store is in `store/`. `kept.rs` and `plugin_state.rs` write files from it, and `import.rs` takes in the files of older versions. `kept_profile.rs` is the profile a host keeps of itself and `berth host adopt`. `runner.rs` runs every action as a job. `keys/` is the key ring and how berth logs in to hosts. `host_type.rs` says what a host of each type can do. `provisioner.rs` and `resource.rs` are the provisioners of plugins and what they made | +| `crates/berth-core/` | Hosts, profiles, links to hosts, registries and secrets, and plugins. The store is in `store/`. `kept.rs` and `plugin_state.rs` write files from it, and `import.rs` takes in the files of older versions. `kept_profile.rs` is the profile a host keeps of itself and `berth host adopt`. `runner.rs` runs every action as a job. `keys/` is the key ring and how berth logs in to hosts. `host_type.rs` says what a host of each type can do. `provisioner.rs` and `resource.rs` are the provisioners of plugins and what they made. `simulate.rs` is the world of `berth serve --simulate`: its hosts, its provisioner `sim/cloud` and its seed | | `crates/berth-tui/` | The parts of the screens with no terminal in them. `fields.rs`, `setup.rs` and `flow.rs` describe a host's set-up once, for the screens' tabs and the command line's prompts and flags alike, and `theme.rs` has the colours both draw with | | `crates/berth-pty/` | Terminals for sessions | | `crates/berthd/`, `crates/box/` | The controller on each host and the agent's own tool there | diff --git a/docs/content/docs/(guide)/extend/serve.mdx b/docs/content/docs/(guide)/extend/serve.mdx index f0ea353..76c2444 100644 --- a/docs/content/docs/(guide)/extend/serve.mdx +++ b/docs/content/docs/(guide)/extend/serve.mdx @@ -10,7 +10,7 @@ icon: Plug berth serve ``` -It prints where it listens and how many hosts it knows. Only one runs per user. To use its hosts from another machine's berth, see [A berth server](/docs/use/server). +It prints where it listens and how many hosts it knows. Only one runs per user. To use its hosts from another machine's berth, see [A berth server](/docs/use/server). To test an application against the API without machines, see [Testing against a simulated server](/docs/api/testing). ## What it adds diff --git a/docs/content/docs/api/meta.json b/docs/content/docs/api/meta.json index e8d3d2e..d195692 100644 --- a/docs/content/docs/api/meta.json +++ b/docs/content/docs/api/meta.json @@ -3,5 +3,5 @@ "description": "berthd and berth serve", "icon": "Plug", "root": true, - "pages": ["index", "portal", "host", "serve"] + "pages": ["index", "portal", "testing", "host", "serve"] } diff --git a/docs/content/docs/api/testing.mdx b/docs/content/docs/api/testing.mdx new file mode 100644 index 0000000..406151f --- /dev/null +++ b/docs/content/docs/api/testing.mdx @@ -0,0 +1,168 @@ +--- +title: Testing against a simulated server +description: Run berth serve with hosts and provisioners that reach no machine, so an application's tests can use the real API in CI. +icon: FlaskConical +--- + +`berth serve --simulate` runs the real server on a scratch store. Its routes, roles, tokens, jobs, events and portal are the ones a real server has. The hosts, the provisioners and the tailnet are simulated, so nothing connects to another machine. An application's tests go through a host's whole life in seconds, with no SSH, Ansible or cloud account. + +```bash +berth serve --simulate --listen 127.0.0.1:7777 +``` + +It prints where its state is, where it listens, its token and a setup key: + +```text +Simulated berth server: its hosts and provisioners reach no machine. State in /tmp/berth-simulated-x1Yz, removed when it ends +Listening on /tmp/berth-simulated-x1Yz/run/berth/serve.sock +Listening on http://127.0.0.1:7777 for the token (token in /tmp/berth-simulated-x1Yz/data/berth/serve.token) +Token: 3f9c… +Setup key: 81ad… +``` + +Send the token as `Authorization: Bearer TOKEN`, as with any [berth server](/docs/extend/serve). The token and the setup key belong to this throw-away server alone, so it prints them. `berth login http://127.0.0.1:7777` with the setup key works too, and `berth ui` then shows **simulated** in its header. + +## Telling it apart + +Nobody should take a simulated server for a real one: + +| Where | What it says | +| --- | --- | +| Every HTTP answer, refusals included | The header `berth-simulated: 1` | +| `version` of the server | `"simulated": true` | +| `version` of each host's controller | `"simulated": true` | +| The output of an apply | `applying, simulated: nothing is installed` | +| The screens | **simulated** beside the name in the header | + +## Options + +| Option | Environment | Means | +| --- | --- | --- | +| `--simulate` | `BERTH_SIMULATE=1` | Run a simulated server | +| `--listen ADDRESS:PORT` | | Listen over HTTP too. Any address is allowed without `--expose` | +| `--seed FILE` | `BERTH_SIMULATE_SEED` holds the JSON itself | The world it starts with. See [The seed](#the-seed) | +| `--delay SECONDS` | `BERTH_SIMULATE_DELAY` | How long one step takes. `2` by default, `0` for none, and fractions such as `0.1` work | +| `--data DIR` | | Keep the state in `DIR` and leave it there. `DIR` is empty or holds an earlier simulation | +| `--keep` | | Leave the scratch directory when the server ends | + +The state is a new directory under `TMPDIR`. The server removes it when it ends on `Ctrl+C` or `SIGTERM`. It never reads or writes your own configuration. `--data` refuses berth's own configuration and data directories, any directory that holds them or lies in them, and a directory with files of something else in it. The server's socket is in its scratch directory, so your own `berth` commands keep using your own server. + +## The seed + +A seed is a JSON file that a test starts from. Every field can be left out. + +```json title="seed.json" +{ + "token": "made-up-token-for-the-tests-0123456789", + "hosts": { + "studio": {"applied": true}, + "dock": {"profile": {"type": "node", "access": {"address": "192.0.2.7", "user": "ops"}}} + }, + "provisioners": {"sim": {"type": "sim/cloud", "settings": {"region": "sim-2"}}}, + "secrets": {"api_token": "made-up"} +} +``` + +| Field | Holds | +| --- | --- | +| `token` | The token for HTTP, at least 32 characters. Without it the server makes one | +| `hosts` | Saved hosts by name. `profile` is the host's profile as `hosts.save` takes it, and `applied: true` makes the host start applied at this berth's version | +| `provisioners` | Registered provisioners by name, as `provisioners.save` takes them. Without any, `sim` is registered as `sim/cloud` | +| `secrets` | Secrets by name | + +A host without an address gets one of `192.0.2.0/24`, and one without `access.user` gets `ops`. A workstation without `builder` gets an empty one. + +## What is real and what is simulated + +| Part | Real or simulated | +| --- | --- | +| Routes, the portal, roles, tokens, setup keys | Real | +| The store, secrets, registries, settings, the key-value store, the inbox | Real, on the scratch store | +| Jobs and their output, the events stream | Real | +| A host's actions, sessions and files | Real berthd in single-user mode, one per host in the scratch directory. Its commands run through a [shell of its own](#a-hosts-shell) | +| A host's processes | Only what its berthd started | +| A host's ports | Refused, as they would be the ports of the machine the server runs on | +| A host's units, logs and power | Not there, as on any berthd in single-user mode | +| Applying a host, and installing its controller | Simulated. Progress lines come over a few steps, then the records a real apply leaves | +| A host before its first apply | Has no controller, as a fresh machine has none | +| Provisioners | The built-in `sim/cloud`. Other plugins' provisioners run as they are | +| The tailnet | Simulated. Its keys are made up and join nothing | +| Rolling out, rotating keys and adopting | Not simulated, and their actions say they are unavailable | +| First contact over SSH, seeds and installers | Not simulated, and unavailable too | + +### A host's shell + +A session or an action on a simulated host doesn't run programs of the machine. Its shell knows these commands and answers any other with exit code 127: + +| Command | Does | +| --- | --- | +| `echo`, `hostname`, `whoami` | What they do on Linux, as the host `NAME` and the user `agent` | +| `id`, `uname`, `pwd`, `date` | The same | +| `ls`, `cat` | Read the host's home in the scratch directory. A path outside it is refused | +| `sleep N`, `true`, `false`, `exit N` | As in a shell | +| `status`, `smoke`, `update`, `reboot`, `secrets` | The host's actions, with made-up output | +| `fail` | Fails with exit code 3, to test how failures are shown | +| `help` | Lists them | + +`;` separates commands in one line. A terminal session gets a prompt and runs each line typed. + +### The provisioner `sim/cloud` + +It makes nothing, and keeps what it made in the scratch directory. + +| Kind | Verbs | Answers | +| --- | --- | --- | +| `machine` | create, start, stop, logs and destroy | An address of `192.0.2.0/24`. berth then applies the host, as with a provisioner of the `apply` set-up. The host's controller does not answer when the machine is stopped | +| `service` | create, start, stop, change, logs, exec and destroy | An endpoint `http` on port 80 of an address of `192.0.2.0/24`. `exec` runs `echo`, `hostname`, `true` and `false`. Its action `restart` restarts it | + +The provisioner lists what it made, and its page has a collection `images` of three images. Its actions `images` and `inspect` read them. Endpoints of `192.0.2.0/24` lead nowhere, and `resources.connect` refuses every endpoint of a simulated server. + +## Timings + +Each step takes the delay, two seconds by default: + +| What | Steps | +| --- | --- | +| Create a machine or a service | 2, and a machine's apply after them | +| Apply a host | 1.5 for a node or a managed host, 2.5 for a workstation | +| Start, stop, change, remove, `restart` | 1 | + +The server looks at a simulated host again after one step, where a real server waits up to ten seconds. A test that polls `hosts.get` sees a host stop or start within a step. With `--delay 0` everything is as fast as the store, and the order of jobs and events stays the same. + +## Running it in continuous integration + +GitHub Actions starts a service container before the job's steps and cannot change its command. The environment turns the published image into a simulated server instead: + +```yaml title=".github/workflows/test.yml" +jobs: + test: + runs-on: ubuntu-latest + services: + berth: + image: ghcr.io/helightdev/berth:VERSION + env: + BERTH_SIMULATE: "1" + BERTH_SIMULATE_DELAY: "0.2" + BERTH_SIMULATE_SEED: >- + {"token": "made-up-token-for-the-tests-0123456789", + "hosts": {"studio": {"applied": true}}} + ports: + - 7777:7777 + options: >- + --health-cmd "curl -fsS http://127.0.0.1:7777/" + --health-interval 2s --health-retries 15 + steps: + - uses: actions/checkout@v4 + - run: npm test + env: + BERTH_URL: http://127.0.0.1:7777 + BERTH_TOKEN: made-up-token-for-the-tests-0123456789 +``` + +The token is made up for the tests and opens nothing else, so it can stay in the workflow. Elsewhere, start the image with its command: + +```bash +docker run --rm -p 7777:7777 ghcr.io/helightdev/berth:VERSION serve --simulate --listen 0.0.0.0:7777 +``` + +A test on a machine with berth installed starts `berth serve --simulate` itself and reads the token from its output. `SIGTERM` ends it. berth's own test of this, `crates/berth/tests/simulate.rs`, does that and goes through a host's whole life. From a58bc7e6bd817a4ef32c7ee5d7531ad1b0de0c6e Mon Sep 17 00:00:00 2001 From: Helight Date: Sun, 15 Nov 2026 20:51:16 +0100 Subject: [PATCH 3/3] test(simulate): compare the seed's secret by name without printing the list --- crates/berth/tests/simulate.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/berth/tests/simulate.rs b/crates/berth/tests/simulate.rs index e4dc4b1..829cc1a 100644 --- a/crates/berth/tests/simulate.rs +++ b/crates/berth/tests/simulate.rs @@ -235,7 +235,17 @@ async fn a_host_lives_its_whole_life_on_a_simulated_server() { (&json!(berth_api::VERSION), &json!("192.0.2.20")) ); let secrets = server.call("GET", "/v1/secrets", None).await; - assert!(secrets.to_string().contains("api_token"), "{secrets}"); + // Only the names are compared; the list is not printed on failure. + let names: Vec<&str> = secrets["names"] + .as_array() + .into_iter() + .flatten() + .filter_map(|name| name.as_str()) + .collect(); + assert!( + names.contains(&"api_token"), + "the seed's secret is not listed" + ); let provisioners = server.call("GET", "/v1/provisioners", None).await; assert_eq!( (&provisioners[0]["id"], &provisioners[0]["type"]),