phoebus_os/installer-rs/crates/phoebus-installer/tests/cli_coverage.rs
Phoebus dc5e27e60a fix(nvidia): deterministic live GPU decision, nouveau fallback and fail-safe
The live load script previously modprobed the NVIDIA stack for any PCI
vendor 0x10de and the build required etc/modprobe.d/phoebus-nvidia.conf
that nothing created. Replace the vendor-name check with a deterministic
decision computed from the actual display controllers and the bundled
branch's capabilities:

- phoebus-core gains live_nvidia_plan(): supported GPU -> load the
  bundled accelerated stack; unsupported GPU -> explicit nouveau
  fallback; no GPU -> leave auto-detection; fail-safe -> load nothing.
- phoebus-installer --live-gpu-plan scans sysfs (overridable for tests)
  and prints stable key=value lines; probe_nvidia_display_devices() only
  considers display-class NVIDIA functions.
- phoebus-nvidia-load consults it, falls back to nouveau on an
  unsupported GPU or a failed accelerated load, and never fails the
  unit; the service honours phoebus.nvidia=off.
- build.sh now creates the live modprobe policy, replaces Debian's
  unconditional nouveau blacklist, and marks the fail-safe entry.
2026-09-23 06:05:27 -07:00

228 lines
7.9 KiB
Rust

//! Coverage of the reference Bash installer's command-line surface.
//!
//! Every mode and flag accepted by the reference Bash installer (see
//! `docs/BASH-MIGRATION.md`) must have a Rust equivalent, or be explicitly
//! recorded as intentionally dropped. The test inspects `--help` so it never
//! executes an installation or bootstraps OpenCode.
use std::path::PathBuf;
use std::process::Command;
/// Flags that must be present in the Rust CLI.
const REQUIRED_FLAGS: &[&str] = &[
"--config",
"--plan",
"--dry-run",
"--install",
"--yes",
"--luks-passphrase-file",
"--user-password-file",
"--root-password-file",
"--bootstrap-opencode",
];
/// Bash-only surface that is intentionally not ported, with the rationale so a
/// future session knows the omission was deliberate.
const INTENTIONALLY_DROPPED: &[(&str, &str)] = &[
(
"--backend",
"an alias for --plan; the Rust CLI has a single deterministic backend",
),
(
"--ui",
"UI_BACKEND only selected a dialog/whiptail variant; ratatui needs none",
),
("-y", "clap provides the short form of --yes"),
("--version", "clap provides -V/--version"),
("--help", "clap provides -h/--help"),
];
fn help_text() -> String {
let out = Command::new(env!("CARGO_BIN_EXE_phoebus-installer"))
.arg("--help")
.output()
.expect("run phoebus-installer --help");
assert!(out.status.success(), "`--help` must succeed");
String::from_utf8_lossy(&out.stdout).into_owned()
}
fn manifest_path() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("resources/opencode.manifest")
}
#[test]
fn every_bash_cli_mode_has_a_rust_flag() {
let help = help_text();
for flag in REQUIRED_FLAGS {
assert!(help.contains(flag), "Rust CLI is missing `{flag}`:\n{help}");
}
// The dropped surface is recorded in code, not silently forgotten.
for (flag, _reason) in INTENTIONALLY_DROPPED {
let _ = flag;
}
}
#[test]
fn verify_live_nvidia_accepts_a_complete_payload_and_rejects_an_incomplete_one() {
fn touch(path: &std::path::Path) {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, b"x").unwrap();
}
fn complete(root: &std::path::Path) {
let kver = "6.16.7-amd64";
touch(&root.join(format!("boot/vmlinuz-{kver}")));
let modules = root.join(format!("lib/modules/{kver}"));
std::fs::create_dir_all(&modules).unwrap();
std::fs::write(modules.join("modules.dep"), b"").unwrap();
for m in ["nvidia", "nvidia_modeset", "nvidia_uvm", "nvidia_drm"] {
touch(&modules.join(format!("updates/dkms/{m}.ko")));
}
for lib in [
"libnvidia-glcore.so.550",
"libGLX_nvidia.so.0",
"libnvidia-egl-gbm.so.1",
"libnvidia-egl-wayland.so.1",
] {
touch(&root.join("usr/lib/x86_64-linux-gnu").join(lib));
}
touch(&root.join("usr/bin/nvidia-smi"));
touch(&root.join("usr/share/egl/egl_external_platform.d/15_nvidia_gbm.json"));
touch(&root.join("usr/share/egl/egl_external_platform.d/10_nvidia_wayland.json"));
touch(&root.join("usr/lib/firmware/nvidia/550.163.01/gsp.bin"));
touch(&root.join("usr/local/bin/phoebus-nvidia-load"));
let blacklist = root.join("etc/modprobe.d/nvidia-blacklists-nouveau.conf");
std::fs::create_dir_all(blacklist.parent().unwrap()).unwrap();
std::fs::write(&blacklist, b"blacklist nouveau\n").unwrap();
}
let run = |root: &std::path::Path| {
Command::new(env!("CARGO_BIN_EXE_phoebus-installer"))
.arg("--verify-live-nvidia")
.arg(root)
.args([
"--nvidia-version",
"550.163.01",
"--nvidia-flavor",
"proprietary",
])
.output()
.expect("run --verify-live-nvidia")
};
let dir = tempfile::tempdir().unwrap();
complete(dir.path());
let out = run(dir.path());
assert!(
out.status.success(),
"{}",
String::from_utf8_lossy(&out.stderr)
);
let record = std::fs::read_to_string(dir.path().join("etc/phoebus/nvidia")).unwrap();
assert!(record.contains("driver_version=550.163.01"));
assert!(record.contains("kernel_version=6.16.7-amd64"));
let broken = tempfile::tempdir().unwrap();
complete(broken.path());
std::fs::remove_file(broken.path().join("usr/bin/nvidia-smi")).unwrap();
let out = run(broken.path());
assert!(
!out.status.success(),
"a missing component must fail the gate"
);
assert_eq!(out.status.code(), Some(1));
}
#[test]
fn live_gpu_plan_reflects_the_fixture_and_the_fail_safe_cmdline() {
fn write_device(root: &std::path::Path, addr: &str, class: &str, vendor: &str, device: &str) {
let d = root.join("bus/pci/devices").join(addr);
std::fs::create_dir_all(&d).unwrap();
std::fs::write(d.join("class"), class).unwrap();
std::fs::write(d.join("vendor"), vendor).unwrap();
std::fs::write(d.join("device"), device).unwrap();
}
let run = |sysfs: &std::path::Path, cmdline: &str| -> String {
let out = Command::new(env!("CARGO_BIN_EXE_phoebus-installer"))
.arg("--live-gpu-plan")
.arg("--sysfs-root")
.arg(sysfs)
.arg("--kernel-cmdline")
.arg(cmdline)
.output()
.expect("run --live-gpu-plan");
assert!(out.status.success());
String::from_utf8_lossy(&out.stdout).into_owned()
};
let supported = tempfile::tempdir().unwrap();
write_device(
supported.path(),
"0000:01:00.0",
"0x030000",
"0x10de",
"0x2482",
);
let plan = run(supported.path(), "quiet splash");
assert!(plan.contains("action=load-nvidia"), "{plan}");
assert!(plan.contains("device=10de:2482 Ampere"), "{plan}");
// Blackwell cannot be driven by the bundled 550 branch.
let blackwell = tempfile::tempdir().unwrap();
write_device(
blackwell.path(),
"0000:01:00.0",
"0x030000",
"0x10de",
"0x2b85",
);
let plan = run(blackwell.path(), "");
assert!(plan.contains("action=nouveau-fallback"), "{plan}");
assert!(plan.contains("unclaimed=10de:2b85 Blackwell"), "{plan}");
// Fail-safe graphics wins even with a supported GPU present.
let plan = run(supported.path(), "quiet nomodeset");
assert!(plan.contains("action=disabled"), "{plan}");
let plan = run(supported.path(), "quiet phoebus.nvidia=off");
assert!(plan.contains("action=disabled"), "{plan}");
// No NVIDIA display device -> leave auto-detection alone.
let none = tempfile::tempdir().unwrap();
write_device(none.path(), "0000:00:02.0", "0x030000", "0x8086", "0x9a60");
let plan = run(none.path(), "");
assert!(plan.contains("action=no-device"), "{plan}");
}
#[test]
fn relocated_opencode_manifest_is_pinned_and_complete() {
let path = manifest_path();
assert!(path.is_file(), "missing {}", path.display());
let text = std::fs::read_to_string(&path).expect("read opencode manifest");
let version = text
.lines()
.find_map(|line| line.strip_prefix("VERSION="))
.expect("manifest pins a VERSION");
assert!(version.starts_with('v'), "unexpected version `{version}`");
for key in ["x86_64.url", "x86_64.sha256", "x86_64.binary"] {
assert!(text.contains(key), "manifest is missing `{key}`");
}
let sha = text
.lines()
.find_map(|line| line.strip_prefix("x86_64.sha256="))
.expect("x86_64 checksum");
assert_eq!(
sha.len(),
64,
"x86_64 checksum must be a SHA-256 hex digest"
);
assert!(
sha.chars().all(|c| c.is_ascii_hexdigit()),
"x86_64 checksum must be hex"
);
assert!(
sha.chars().any(|c| c != '0'),
"x86_64 checksum must not be a placeholder"
);
}