phoebus_os/installer-rs/crates/phoebus-installer/tests/sys_cli.rs
Phoebus 297f3201ff
Some checks are pending
ci / test (push) Waiting to run
feat(sys): hardware-aware lane selection and new lanes
Replace the fixed category routing (ADR 0006) with a shared, inspectable,
hardware- and preference-aware selection policy over resolved candidates from
Portage (source/binary/vendor), Nix, Flatpak, Snap, AppImage and the Phoebus
overlay. The policy keeps base components under Portage, separates hard
constraints from preferences, honours and persists explicit choices, errors
instead of silently switching lanes, and reuses an already-installed lane.

- phoebus-core::selection: the pure policy (16 tests: base-system, low-memory
  vs high-core, sandbox requirement, GPU/arch/storage rejection, explicit
  preference, unavailable backend, reproducibility, render).
- phoebus-executor::chooser: resolves real metadata and hardware facts.
- phoebus-executor::{snap,appimage,ebuild}: new lanes. Snap detects daemon and
  confinement and installs app-containers/snapd through Portage; AppImage
  adopts images with a pinned SHA-256, checks the ELF architecture without
  executing, integrates a desktop entry and preserves user data on removal; the
  custom ebuild overlay binds approval to the ebuild content digest and builds
  through Portage's sandbox.
- Typed actions choose_package / set_package_preference / snap_* / appimage_* /
  custom_ebuild_* with strict validators; the assistant prompt and schema are
  reconciled (no category routing; custom ebuilds are untrusted).
- Installed sys: the installer locates the trusted static binary, copies it to
  /usr/local/bin/sys and proves it runs in the target chroot before the
  recovery snapshot, failing closed if absent; the VM harness fetches sys.
- docs/SYS.md: command/action reference and acceptance matrix; ADR 0006 revised.

Host gates pass: fmt, clippy (no new warnings), workspace tests, make test,
release and static sys builds. VM lane journeys remain pending (see HANDOFF).
2026-09-23 18:35:26 -07:00

177 lines
5.5 KiB
Rust

//! CLI-level regression tests for the `sys` entry paths.
//!
//! These exercise the real binary so the deterministic (manual) and JSON paths
//! are proven to fail closed on invalid input and to exit nonzero on failure.
//! They never mutate the system: the invalid cases are refused before any
//! command is built, and the read-only case targets a temporary root.
use std::process::Command;
fn sys() -> Command {
Command::new(env!("CARGO_BIN_EXE_sys"))
}
#[test]
fn natural_option_like_operand_is_rejected() {
// `--` stops clap option parsing, so the operand reaches the parser intact.
let out = sys()
.args(["--", "install", "--color=n"])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("could not parse") || stderr.contains("invalid"),
"stderr: {stderr}"
);
}
#[test]
fn json_option_like_operand_is_rejected() {
let out = sys()
.args(["--action", r#"{"action":"install_package","atom":"-C"}"#])
.output()
.expect("run sys");
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(stderr.contains("invalid"), "stderr: {stderr}");
}
#[test]
fn read_only_history_succeeds_against_a_temporary_root() {
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.arg("--root")
.arg(root.path())
.arg("history")
.output()
.expect("run sys");
assert!(
out.status.success(),
"stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
stdout.contains("no snapshot history") || stdout.contains("snapshots:"),
"stdout: {stdout}"
);
}
#[test]
fn version_is_reported() {
let out = sys().arg("--version").output().expect("run sys");
assert!(out.status.success());
assert!(String::from_utf8_lossy(&out.stdout).contains("sys"));
}
#[test]
fn appimage_and_custom_ebuild_inventories_are_safe_and_empty_on_a_fresh_root() {
let root = tempfile::tempdir().expect("temp root");
for args in [vec!["appimage", "list"], vec!["ebuilds"]] {
let out = sys()
.arg("--root")
.arg(root.path())
.args(&args)
.output()
.expect("run sys");
assert!(
out.status.success(),
"{args:?} stderr: {}",
String::from_utf8_lossy(&out.stderr)
);
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
stdout.contains("no managed AppImages")
|| stdout.contains("no Phoebus-managed custom ebuilds"),
"{args:?} stdout: {stdout}"
);
}
}
#[test]
fn invalid_snap_name_via_json_fails_closed() {
let out = sys()
.args(["--action", r#"{"action":"snap_install","name":"Bad Name"}"#])
.output()
.expect("run sys");
assert!(!out.status.success());
assert!(String::from_utf8_lossy(&out.stderr).contains("invalid"));
}
#[test]
fn invalid_appimage_hash_via_json_fails_closed() {
let out = sys()
.args([
"--action",
r#"{"action":"appimage_acquire","name":"x","url":"https://e/x","sha256":"00"}"#,
])
.output()
.expect("run sys");
assert!(!out.status.success());
assert!(String::from_utf8_lossy(&out.stderr).contains("SHA-256"));
}
#[test]
fn a_mutating_action_without_root_is_refused_before_any_command() {
// This runs as the test user (not root); the mutation must be refused.
if effective_uid_is_root() {
return;
}
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.arg("--root")
.arg(root.path())
.args(["--action", r#"{"action":"snap_list"}"#])
.output()
.expect("run sys");
// snap_list is read-only, so it will instead fail because snapd is absent;
// the point is only that it never mutates. A mutating action is checked
// separately below.
let _ = out;
let mutating = sys()
.arg("--root")
.arg(root.path())
.args([
"--action",
r#"{"action":"set_package_preference","name":"firefox","backend":"flatpak"}"#,
])
.output()
.expect("run sys");
assert!(!mutating.status.success());
assert!(String::from_utf8_lossy(&mutating.stderr).contains("root"));
}
fn effective_uid_is_root() -> bool {
std::process::Command::new("id")
.arg("-u")
.output()
.ok()
.map(|o| String::from_utf8_lossy(&o.stdout).trim() == "0")
.unwrap_or(false)
}
#[test]
fn choose_never_invents_a_candidate() {
// Depending on the host, a lane may or may not be resolvable. Either way the
// output must be a real selection with rejected alternatives, or a clear
// fail-closed error — never a fabricated package.
let root = tempfile::tempdir().expect("temp root");
let out = sys()
.arg("--root")
.arg(root.path())
.args(["choose", "some-package"])
.output()
.expect("run sys");
let stdout = String::from_utf8_lossy(&out.stdout);
let stderr = String::from_utf8_lossy(&out.stderr);
if out.status.success() {
assert!(stdout.contains("Selected package:"), "stdout: {stdout}");
assert!(stdout.contains("source:"), "stdout: {stdout}");
} else {
assert!(
stderr.contains("no compatible") || stderr.contains("sys:"),
"stderr: {stderr}"
);
}
}