//! Release/ISO automation foundations. //! //! A biweekly release should not require a small VPS to rebuild the Gentoo //! world. The model is: take a known-good installer-base ISO, overlay the //! Phoebus installer + configs + pinned OpenCode, verify checksums, smoke test, //! then publish atomically. This module produces and verifies the manifest that //! makes that reproducible. //! //! Actual ISO assembly needs `xorriso`; this module emits the intended command //! and refuses to publish without a passing verification. use std::path::{Path, PathBuf}; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Artifact { pub name: String, pub sha256: String, pub size_bytes: u64, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(default)] pub struct ReleaseManifest { pub version: String, pub created_unix: u64, pub installer_base: String, pub stage3: String, pub opencode_version: String, pub artifacts: Vec, } impl Default for ReleaseManifest { fn default() -> Self { Self { version: env!("CARGO_PKG_VERSION").to_string(), created_unix: 0, installer_base: String::new(), stage3: String::new(), opencode_version: String::new(), artifacts: Vec::new(), } } } pub fn sha256_file(path: &Path) -> std::io::Result { let mut file = std::fs::File::open(path)?; let mut hasher = Sha256::new(); std::io::copy(&mut file, &mut hasher)?; Ok(format!("{:x}", hasher.finalize())) } pub fn artifact_for(path: &Path) -> std::io::Result { let meta = std::fs::metadata(path)?; Ok(Artifact { name: path .file_name() .map(|s| s.to_string_lossy().to_string()) .unwrap_or_default(), sha256: sha256_file(path)?, size_bytes: meta.len(), }) } impl ReleaseManifest { pub fn save(&self, path: &Path) -> std::io::Result<()> { if let Some(parent) = path.parent() { std::fs::create_dir_all(parent)?; } let json = serde_json::to_string_pretty(self) .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?; std::fs::write(path, json) } pub fn load(path: &Path) -> std::io::Result { let text = std::fs::read_to_string(path)?; serde_json::from_str(&text) .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) } /// Verify a candidate artifact against the manifest. pub fn verify(&self, name: &str, path: &Path) -> bool { match self.artifacts.iter().find(|a| a.name == name) { Some(expected) => sha256_file(path) .map(|got| got == expected.sha256) .unwrap_or(false), None => false, } } /// The xorriso command that would assemble the candidate ISO. /// /// The base ISO is remastered with the Phoebus payload; nothing in the base /// system is rebuilt. pub fn assemble_command(base_iso: &Path, payload_dir: &Path, output: &Path) -> Vec { vec![ "xorriso".into(), "-indev".into(), base_iso.to_string_lossy().to_string(), "-outdev".into(), output.to_string_lossy().to_string(), "-map".into(), payload_dir.to_string_lossy().to_string(), "/phoebus".into(), "-boot_image".into(), "any".into(), "replay".into(), ] } } pub fn default_manifest_path(dir: &Path) -> PathBuf { dir.join("phoebus-release.json") } // --------------------------------------------------------------------------- // Candidate gates // --------------------------------------------------------------------------- /// What a gate needs to be runnable. A gate that is not runnable is recorded /// as `NotRun`, never as `Passed`. #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum GateRequirement { /// Runs anywhere. Host, /// Needs the disposable QEMU/OVMF VM. Vm, /// Needs a graphical seat. GraphicalSeat, /// Needs a live model credential or a local model. Model, /// Needs a configured CI runner. CiRunner, } impl GateRequirement { pub fn as_str(self) -> &'static str { match self { GateRequirement::Host => "host", GateRequirement::Vm => "vm", GateRequirement::GraphicalSeat => "graphical-seat", GateRequirement::Model => "model", GateRequirement::CiRunner => "ci-runner", } } } /// One release candidate check. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Gate { pub name: String, pub description: String, pub requires: GateRequirement, /// The exact command (argv) that performs the check. pub command: Vec, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum GateStatus { Passed, Failed, NotRun, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct GateResult { pub name: String, pub status: GateStatus, pub detail: String, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)] #[serde(default)] pub struct GateReport { pub version: String, pub created_unix: u64, /// Hash of the exact ISO passed to the VM harness via ISO. pub candidate_iso_sha256: Option, pub source_revision: Option, pub disk_images: Vec, pub results: Vec, } impl GateReport { /// Whether every gate passed. `NotRun` is not a pass. pub fn all_passed(&self) -> bool { !self.results.is_empty() && self.results.iter().all(|r| r.status == GateStatus::Passed) } pub fn matches_candidate(&self, candidate: &Path) -> bool { self.candidate_iso_sha256 .as_deref() .is_some_and(|expected| sha256_file(candidate).is_ok_and(|actual| actual == expected)) } pub fn ready_to_publish(&self, candidate: &Path) -> bool { if !self.all_passed() || !self.matches_candidate(candidate) || self.source_revision.as_deref().map_or(true, |rev| { rev.len() != 40 || !rev.bytes().all(|byte| byte.is_ascii_hexdigit()) }) { return false; } let expected: std::collections::BTreeSet<_> = candidate_gates().into_iter().map(|g| g.name).collect(); let actual: std::collections::BTreeSet<_> = self.results.iter().map(|r| r.name.clone()).collect(); actual == expected && self.results.len() == expected.len() } /// A report can only authorize publication from the same clean checkout /// that ran its gates. Call this with the revision observed at publish time. pub fn matches_source_revision(&self, current: Option<&str>) -> bool { current.is_some_and(|revision| { revision.len() == 40 && revision.bytes().all(|byte| byte.is_ascii_hexdigit()) && self.source_revision.as_deref() == Some(revision) }) } pub fn not_run(&self) -> usize { self.results .iter() .filter(|r| r.status == GateStatus::NotRun) .count() } pub fn save(&self, path: &Path) -> std::io::Result<()> { if let Some(parent) = path.parent() { std::fs::create_dir_all(parent)?; } let json = serde_json::to_string_pretty(self) .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?; std::fs::write(path, json) } pub fn load(path: &Path) -> std::io::Result { let text = std::fs::read_to_string(path)?; serde_json::from_str(&text) .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)) } } /// Modes the headless serial harness accepts (`serial-driver.py` `main`). /// A gate naming any other mode would fail at runtime; the /// `gate_commands_are_real_harness_modes` test pins this list. pub const SERIAL_DRIVER_MODES: &[&str] = &[ "install", "install-repeat", "boot", "boot-assessed", "rollback", "recovery", "fallback", "interrupted-update", ]; /// Modes the graphical harness accepts (`gui-driver.py` `run`). pub const GUI_DRIVER_MODES: &[&str] = &[ "gui", "opencode", "gui-install", "install", "boot", "desktop", ]; /// The candidate checks. Commands are the real harness invocations, not /// placeholders, so a configured runner executes exactly what is documented. /// /// - `no-model-recovery` actually selects and boots the recovery BLS entry. /// - `desktop-session` drives the seat and tests the installed session. pub fn candidate_gates() -> Vec { let driver = "installer/tests/vm/serial-driver.py"; let gui_driver = "installer/tests/vm/gui-driver.py"; vec![ Gate { name: "live-installer-ui".into(), description: "firmware boot of the candidate ISO, welcome and installer wizard".into(), requires: GateRequirement::GraphicalSeat, command: vec!["python3".into(), gui_driver.into(), "gui".into()], }, Gate { name: "install".into(), description: "full encrypted install in the disposable VM".into(), requires: GateRequirement::Vm, command: vec!["python3".into(), driver.into(), "install".into()], }, Gate { name: "install-repeat".into(), description: "second independent encrypted install from the same ISO".into(), requires: GateRequirement::Vm, command: vec!["python3".into(), driver.into(), "install-repeat".into()], }, Gate { name: "detached-boot".into(), description: "boot detached and complete the boot health assessment".into(), requires: GateRequirement::Vm, command: vec!["python3".into(), driver.into(), "boot-assessed".into()], }, Gate { name: "no-model-recovery".into(), description: "select and boot the snapshot recovery entry offline; run sys --legacy" .into(), requires: GateRequirement::Vm, command: vec!["python3".into(), driver.into(), "recovery".into()], }, Gate { name: "failed-boot-fallback".into(), description: "exhaust counted normal boots and auto-select recovery offline".into(), requires: GateRequirement::Vm, command: vec!["python3".into(), driver.into(), "fallback".into()], }, Gate { name: "desktop-session".into(), description: "graphical seat: first-login welcome, authenticated session, shortcuts, lock, logout".into(), requires: GateRequirement::GraphicalSeat, command: vec!["python3".into(), gui_driver.into(), "desktop".into()], }, Gate { name: "updates-and-rollback".into(), description: "snapshot, install a package, sys undo, and verify after reboot".into(), requires: GateRequirement::Vm, command: vec!["python3".into(), driver.into(), "rollback".into()], }, Gate { name: "interrupted-update".into(), description: "kill a sys package transaction, undo, and verify after reboot".into(), requires: GateRequirement::Vm, command: vec!["python3".into(), driver.into(), "interrupted-update".into()], }, Gate { name: "assistant-live".into(), description: "connect the live installer to a real OpenCode provider and validate its guided proposal".into(), requires: GateRequirement::Model, command: vec!["python3".into(), gui_driver.into(), "opencode".into()], }, Gate { name: "ci-verification".into(), description: "independent CI runner executes the Rust and VM quality gates".into(), requires: GateRequirement::CiRunner, command: vec!["make".into(), "check".into()], }, ] } /// A gate command is argv executed directly, never via a shell. Reject empty /// commands and shell metacharacters so a gate definition cannot smuggle in /// `sh -c` indirection or chained commands. pub fn validate_gate_command(gate: &Gate) -> Result<(), String> { if gate.command.is_empty() { return Err(format!( "gate '{}' declares no executable command", gate.name )); } if gate.command[0].is_empty() { return Err(format!("gate '{}' has an empty program", gate.name)); } for arg in &gate.command { if arg.contains('\0') { return Err(format!("gate '{}' command contains NUL", gate.name)); } } let prog = &gate.command[0]; for meta in [";", "|", "&", "`", "$(", "\n"] { if prog.contains(meta) { return Err(format!( "gate '{}' program must be a binary, not a shell snippet", gate.name )); } } Ok(()) } fn program_exists(prog: &str) -> bool { if prog.contains('/') { return Path::new(prog).is_file(); } std::env::var_os("PATH") .is_some_and(|paths| std::env::split_paths(&paths).any(|d| d.join(prog).is_file())) } /// The unmet prerequisite for a gate, if any. A missing prerequisite reports /// `NotRun`, never a pass or a failure. Credential values are never included /// in the reason. pub fn gate_prerequisite_missing(gate: &Gate) -> Option { match gate.requires { GateRequirement::Host => { if program_exists(&gate.command[0]) { None } else { Some(format!( "missing prerequisite: program '{}' not found on PATH", gate.command[0] )) } } GateRequirement::Vm => { if gate.command.len() >= 2 { let driver = &gate.command[1]; if !Path::new(driver).is_file() { return Some(format!( "missing prerequisite: harness '{driver}' not found" )); } } for var in ["OVMF_CODE", "OVMF_VARS"] { if std::env::var_os(var).is_none() { return Some(format!("missing prerequisite: {var} is not set")); } } if !std::env::var_os("ISO").is_some_and(|p| Path::new(&p).is_file()) { return Some("missing prerequisite: ISO is not a readable candidate file".into()); } if std::env::var_os("DISK").is_none() && std::env::var_os("DISKS").is_none() { return Some("missing prerequisite: DISK or DISKS is not set".into()); } if gate .command .get(2) .is_some_and(|mode| mode == "install-repeat") && std::env::var_os("DISK_SECOND").is_none() { return Some("missing prerequisite: DISK_SECOND is not set".into()); } if !program_exists(&gate.command[0]) { return Some(format!( "missing prerequisite: program '{}' not found on PATH", gate.command[0] )); } if gate .command .get(2) .is_some_and(|mode| mode == "fallback" || mode == "interrupted-update") && !program_exists("qemu-img") { return Some("missing prerequisite: qemu-img is not on PATH".into()); } None } GateRequirement::GraphicalSeat => { if gate.command.len() >= 2 { let driver = &gate.command[1]; if !Path::new(driver).is_file() { return Some(format!( "missing prerequisite: harness '{driver}' not found" )); } } for var in ["OVMF_CODE", "OVMF_VARS"] { if std::env::var_os(var).is_none() { return Some(format!("missing prerequisite: {var} is not set")); } } if !std::env::var_os("ISO").is_some_and(|p| Path::new(&p).is_file()) { return Some("missing prerequisite: ISO is not a readable candidate file".into()); } if std::env::var_os("DISK").is_none() && std::env::var_os("DISKS").is_none() { return Some("missing prerequisite: DISK or DISKS is not set".into()); } if !program_exists(&gate.command[0]) { return Some(format!( "missing prerequisite: program '{}' not found on PATH", gate.command[0] )); } if !std::process::Command::new("python3") .args(["-c", "import pexpect, PIL"]) .status() .is_ok_and(|status| status.success()) { return Some("missing prerequisite: Python pexpect and Pillow are required".into()); } None } GateRequirement::Model => { if gate .command .get(1) .is_some_and(|path| path.ends_with("gui-driver.py")) { let mut seat_gate = gate.clone(); seat_gate.requires = GateRequirement::GraphicalSeat; if let Some(missing) = gate_prerequisite_missing(&seat_gate) { return Some(missing); } for var in ["OC_TARGET_PORT", "OC_PROVIDER", "OC_MODEL"] { if std::env::var_os(var).map_or(true, |value| value.is_empty()) { return Some(format!("missing prerequisite: {var} is not set")); } } return None; } let key_file = std::env::var_os("PHOEBUS_AI_KEY_FILE") .map(|p| Path::new(&p).is_file()) .unwrap_or(false); let env_key = ["OPENAI_API_KEY", "DEEPSEEK_API_KEY"] .iter() .any(|v| std::env::var_os(v).is_some_and(|s| !s.is_empty())); if !key_file && !env_key { return Some( "missing prerequisite: no model credential (PHOEBUS_AI_KEY_FILE or OPENAI_API_KEY/DEEPSEEK_API_KEY)".to_string(), ); } if !program_exists(&gate.command[0]) { return Some(format!( "missing prerequisite: program '{}' not found on PATH", gate.command[0] )); } None } GateRequirement::CiRunner => { Some("missing prerequisite: no configured CI runner".to_string()) } } } fn bound_tail(text: &str, max_chars: usize) -> String { let trimmed = text.trim(); let char_count = trimmed.chars().count(); if char_count <= max_chars { return trimmed.to_string(); } let start = trimmed .char_indices() .nth(char_count - max_chars) .map(|(index, _)| index) .unwrap_or(0); format!("…{}", &trimmed[start..]) } /// Execute one gate: run its argv, capture process status and output, and /// report from evidence. Each gate runs in its own process; a failure never /// stops the remaining gates (see the isolation test). fn execute_gate(gate: &Gate) -> GateResult { if let Err(problem) = validate_gate_command(gate) { return GateResult { name: gate.name.clone(), status: GateStatus::Failed, detail: format!("invalid gate command: {problem}"), }; } if let Some(missing) = gate_prerequisite_missing(gate) { return GateResult { name: gate.name.clone(), status: GateStatus::NotRun, detail: missing, }; } let mut command = std::process::Command::new(&gate.command[0]); command.args(&gate.command[1..]); if gate .command .get(1) .is_some_and(|harness| harness.ends_with("gui-driver.py")) { // A release gate must test the immutable ISO named by `ISO`. command.env("SKIP_ENSURE_ISO", "1"); } if gate.name == "desktop-session" { command.env("DESKTOP_EXPECT_WELCOME", "1"); } let output = command.output(); match output { Err(e) if e.kind() == std::io::ErrorKind::NotFound => GateResult { name: gate.name.clone(), status: GateStatus::NotRun, detail: format!( "missing prerequisite: program '{}' could not start ({e})", gate.command[0] ), }, Err(e) => GateResult { name: gate.name.clone(), status: GateStatus::Failed, detail: format!("gate '{}' could not run: {e}", gate.name), }, Ok(out) => { let mut combined = String::new(); combined.push_str(&String::from_utf8_lossy(&out.stdout)); if !out.stderr.is_empty() { combined.push_str("\n--- stderr ---\n"); combined.push_str(&String::from_utf8_lossy(&out.stderr)); } let tail = bound_tail(&combined, 2000); if out.status.success() { GateResult { name: gate.name.clone(), status: GateStatus::Passed, detail: if tail.is_empty() { format!("exit {}", out.status) } else { format!("exit {}; {tail}", out.status) }, } } else { GateResult { name: gate.name.clone(), status: GateStatus::Failed, detail: if tail.is_empty() { format!("exit {}", out.status) } else { format!("exit {}; {tail}", out.status) }, } } } } } /// Record whether the candidate was built from a clean checkout. pub fn git_source_revision() -> Option { let head = std::process::Command::new("git") .args(["rev-parse", "HEAD"]) .output() .ok() .filter(|out| out.status.success())?; let revision = String::from_utf8_lossy(&head.stdout).trim().to_string(); if revision.is_empty() { return None; } let status = std::process::Command::new("git") .args(["status", "--porcelain"]) .output() .ok() .filter(|out| out.status.success())?; if status.stdout.is_empty() { Some(revision) } else { Some(format!("{revision}-dirty")) } } /// Run a gate list, isolating each gate in its own process. Exported for /// tests; production uses [`run_gates`]. pub fn run_gate_list(execute: bool, gates: &[Gate]) -> GateReport { let now = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .map(|d| d.as_secs()) .unwrap_or(0); let mut report = GateReport { version: env!("CARGO_PKG_VERSION").to_string(), created_unix: now, candidate_iso_sha256: if execute { std::env::var_os("ISO").and_then(|path| sha256_file(Path::new(&path)).ok()) } else { None }, source_revision: git_source_revision(), disk_images: std::env::var("DISKS") .or_else(|_| std::env::var("DISK")) .unwrap_or_default() .split(',') .filter(|s| !s.is_empty()) .map(str::to_owned) .chain(std::env::var("DISK_SECOND").ok()) .collect(), results: Vec::new(), }; for gate in gates { if !execute { report.results.push(GateResult { name: gate.name.clone(), status: GateStatus::NotRun, detail: format!("not run (requires {})", gate.requires.as_str()), }); } else { let mut result = execute_gate(gate); if let (Some(expected), Some(candidate)) = (&report.candidate_iso_sha256, std::env::var_os("ISO")) { if sha256_file(Path::new(&candidate)).ok().as_ref() != Some(expected) { result.status = GateStatus::Failed; result.detail = "candidate ISO changed during gate execution".into(); } } report.results.push(result); } } report } /// Run the candidate gates. With `execute = false` every gate is recorded as /// `NotRun`, so a report never claims a check that did not run. With /// `execute = true` each gate's real command runs in its own process: /// exit 0 is `Passed`, a nonzero exit is `Failed`, and a missing /// prerequisite (harness, OVMF firmware variables, seat, or model /// credential) is `NotRun`. A pass is never manufactured from a plan. pub fn run_gates(execute: bool) -> GateReport { run_gate_list(execute, &candidate_gates()) } // --------------------------------------------------------------------------- // Signing and atomic publication // --------------------------------------------------------------------------- /// The signing command for an artifact. The key path is supplied by the /// operator (via environment or a secret store), never committed and never /// placed in a model context. pub fn sign_command(artifact: &Path, key: &Path) -> Vec { vec![ "minisign".into(), "-S".into(), "-s".into(), key.to_string_lossy().to_string(), "-m".into(), artifact.to_string_lossy().to_string(), ] } /// The verification command for an artifact plus its detached signature. pub fn verify_signature_command(artifact: &Path) -> Vec { vec![ "minisign".into(), "-Vm".into(), artifact.to_string_lossy().to_string(), ] } /// Publish a candidate atomically, preserving the previous release. /// /// The candidate is first written to a `*.new` sibling, fsynced and renamed /// into place, so a crash never leaves a half-written `latest.iso`. The prior /// `latest.iso` is kept as `latest.iso.prev` for rollback. pub fn atomic_publish(candidate: &Path, dest: &Path) -> std::io::Result<()> { let parent = dest .parent() .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "no parent"))?; std::fs::create_dir_all(parent)?; let staging = dest.with_extension("new"); std::fs::copy(candidate, &staging)?; if let Ok(file) = std::fs::File::open(&staging) { let _ = file.sync_all(); } if dest.exists() { let previous = dest.with_extension("prev"); let _ = std::fs::remove_file(&previous); std::fs::rename(dest, &previous)?; } std::fs::rename(&staging, dest)?; Ok(()) } #[cfg(test)] mod tests { use super::*; #[test] fn sha256_of_known_bytes() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("f"); std::fs::write(&path, b"abc").unwrap(); // SHA-256("abc") assert_eq!( sha256_file(&path).unwrap(), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" ); } #[test] fn manifest_roundtrip_and_verify() { let dir = tempfile::tempdir().unwrap(); let iso = dir.path().join("phoebus.iso"); std::fs::write(&iso, b"iso-bytes").unwrap(); let mut manifest = ReleaseManifest::default(); manifest.artifacts.push(artifact_for(&iso).unwrap()); manifest.installer_base = "installer-base-2026.iso".into(); let path = default_manifest_path(dir.path()); manifest.save(&path).unwrap(); let loaded = ReleaseManifest::load(&path).unwrap(); assert_eq!(loaded, manifest); assert!(loaded.verify("phoebus.iso", &iso)); assert!(!loaded.verify("missing.iso", &iso)); } #[test] fn assemble_command_targets_payload() { let cmd = ReleaseManifest::assemble_command( Path::new("/base.iso"), Path::new("/payload"), Path::new("/out.iso"), ); assert_eq!(cmd[0], "xorriso"); assert!(cmd.iter().any(|a| a == "/phoebus")); assert!(cmd.iter().any(|a| a == "/base.iso")); assert_eq!(cmd.last().map(String::as_str), Some("replay")); } #[test] fn gate_output_tail_handles_multibyte_text() { assert_eq!(bound_tail("prefix café 🦀", 3), "…é 🦀"); } #[test] fn gates_cover_the_required_checks_and_never_fake_a_pass() { let names: Vec = candidate_gates().into_iter().map(|g| g.name).collect(); for required in [ "live-installer-ui", "install", "install-repeat", "detached-boot", "desktop-session", "updates-and-rollback", "interrupted-update", "no-model-recovery", "failed-boot-fallback", "assistant-live", "ci-verification", ] { assert!( names.contains(&required.to_string()), "missing gate {required}" ); } let report = run_gates(false); assert!(!report.all_passed(), "an unexecuted gate is not a pass"); assert_eq!(report.not_run(), report.results.len()); assert!(report.results.iter().all(|r| r.detail.contains("requires"))); let assistant = candidate_gates() .into_iter() .find(|gate| gate.name == "assistant-live") .unwrap(); assert_eq!(assistant.command[1], "installer/tests/vm/gui-driver.py"); assert_eq!(assistant.command[2], "opencode"); } #[test] fn gate_commands_are_real_harness_modes() { // Regression: candidate_gates once named `serial-driver.py checks`, // `serial-driver.py desktop` (and appliance desktop), modes no // harness accepts. Every declared gate must name a real mode. for gate in candidate_gates() { validate_gate_command(&gate) .unwrap_or_else(|e| panic!("gate '{}' has no real command: {e}", gate.name)); let argv = &gate.command; if argv[0] == "python3" && argv.len() >= 3 { let harness = argv[1].as_str(); let mode = argv[2].as_str(); if harness.ends_with("serial-driver.py") { assert!( SERIAL_DRIVER_MODES.contains(&mode), "gate '{}' names serial-driver mode '{mode}', accepted: {SERIAL_DRIVER_MODES:?}", gate.name ); } else if harness.ends_with("gui-driver.py") { assert!( GUI_DRIVER_MODES.contains(&mode), "gate '{}' names gui-driver mode '{mode}', accepted: {GUI_DRIVER_MODES:?}", gate.name ); } else { panic!("gate '{}' names an unknown harness '{harness}'", gate.name); } } } } fn host_gate(name: &str, program: &str, args: &[&str]) -> Gate { Gate { name: name.into(), description: "test gate".into(), requires: GateRequirement::Host, command: std::iter::once(program.to_string()) .chain(args.iter().map(|a| a.to_string())) .collect(), } } #[test] fn execute_reports_pass_fail_and_notrun_from_evidence() { // Exit 0 is Passed, nonzero is Failed, a missing program is NotRun, // and an empty (non-executable) command is Failed — never a pass. let gates = vec![ host_gate("ok", "/bin/true", &[]), host_gate("bad", "/bin/false", &[]), host_gate("missing", "/nonexistent-phoebus-gate-program", &[]), Gate { name: "manual".into(), description: "no executable command".into(), requires: GateRequirement::Host, command: vec![], }, ]; let report = run_gate_list(true, &gates); let status = |n: &str| { report .results .iter() .find(|r| r.name == n) .unwrap_or_else(|| panic!("missing result {n}")) .status }; assert_eq!(status("ok"), GateStatus::Passed); assert_eq!(status("bad"), GateStatus::Failed); assert_eq!(status("missing"), GateStatus::NotRun); assert_eq!(status("manual"), GateStatus::Failed); assert!( !report.all_passed(), "a mixed report must not count as passed" ); } #[test] fn invalid_commands_are_detected_not_executed() { let gate = Gate { name: "shell".into(), description: "must not run through a shell".into(), requires: GateRequirement::Host, command: vec!["sh; rm -rf /".into()], }; assert!(validate_gate_command(&gate).is_err()); let report = run_gate_list(true, &[gate]); assert_eq!(report.results[0].status, GateStatus::Failed); assert!(report.results[0].detail.contains("invalid gate command")); } #[test] fn gates_are_isolated_a_failure_does_not_stop_the_rest() { let gates = vec![ host_gate("first-fails", "/bin/false", &[]), host_gate("second-passes", "/bin/true", &[]), ]; let report = run_gate_list(true, &gates); assert_eq!(report.results.len(), 2); assert_eq!(report.results[0].status, GateStatus::Failed); assert_eq!(report.results[1].status, GateStatus::Passed); } #[test] fn publish_requires_every_gate_passed() { let passing = GateReport { version: "test".into(), created_unix: 0, results: vec![GateResult { name: "ok".into(), status: GateStatus::Passed, detail: "exit 0".into(), }], ..Default::default() }; assert!(passing.all_passed()); for status in [GateStatus::Failed, GateStatus::NotRun] { let report = GateReport { version: "test".into(), created_unix: 0, results: vec![ GateResult { name: "ok".into(), status: GateStatus::Passed, detail: "exit 0".into(), }, GateResult { name: "other".into(), status, detail: "evidence".into(), }, ], ..Default::default() }; assert!( !report.all_passed(), "publish must be refused when any gate is {status:?}" ); } assert!( !GateReport::default().all_passed(), "an empty report must not authorize publication" ); } #[test] fn publish_rejects_a_report_for_a_different_iso_or_missing_gate() { let dir = tempfile::tempdir().unwrap(); let candidate = dir.path().join("candidate.iso"); std::fs::write(&candidate, b"candidate one").unwrap(); let mut report = GateReport { candidate_iso_sha256: Some(sha256_file(&candidate).unwrap()), source_revision: Some("0123456789abcdef0123456789abcdef01234567".into()), results: candidate_gates() .into_iter() .map(|gate| GateResult { name: gate.name, status: GateStatus::Passed, detail: "verified".into(), }) .collect(), ..Default::default() }; assert!(report.ready_to_publish(&candidate)); assert!(report.matches_source_revision(Some("0123456789abcdef0123456789abcdef01234567"))); assert!(!report.matches_source_revision(Some("fedcba9876543210fedcba9876543210fedcba98"))); assert!(!report.matches_source_revision(None)); report.source_revision = Some("0123456789abcdef0123456789abcdef01234567-dirty".into()); assert!(!report.ready_to_publish(&candidate)); assert!(!report.matches_source_revision(Some("0123456789abcdef0123456789abcdef01234567"))); report.source_revision = Some("0123456789abcdef0123456789abcdef01234567".into()); report.results.pop(); assert!(!report.ready_to_publish(&candidate)); report.results.push(GateResult { name: "assistant-live".into(), status: GateStatus::Passed, detail: "verified".into(), }); std::fs::write(&candidate, b"candidate two").unwrap(); assert!(!report.ready_to_publish(&candidate)); } #[test] fn atomic_publish_keeps_the_previous_release() { let dir = tempfile::tempdir().unwrap(); let candidate = dir.path().join("candidate.iso"); let dest = dir.path().join("latest.iso"); std::fs::write(&candidate, b"v1").unwrap(); atomic_publish(&candidate, &dest).unwrap(); assert_eq!(std::fs::read(&dest).unwrap(), b"v1"); assert!(!dest.with_extension("prev").exists()); std::fs::write(&candidate, b"v2").unwrap(); atomic_publish(&candidate, &dest).unwrap(); assert_eq!(std::fs::read(&dest).unwrap(), b"v2"); assert_eq!( std::fs::read(dest.with_extension("prev")).unwrap(), b"v1", "the last known-good release must be preserved" ); // No partial staging file is left behind. assert!(!dest.with_extension("new").exists()); } #[test] fn signing_commands_keep_the_key_out_of_the_repo() { let cmd = sign_command( Path::new("/out.iso"), Path::new("/run/secrets/minisign.key"), ); assert_eq!(cmd[0], "minisign"); assert!(cmd.iter().any(|a| a == "/run/secrets/minisign.key")); let verify = verify_signature_command(Path::new("/out.iso")); assert!(verify.iter().any(|a| a == "-Vm")); } }