Files
pkh/src/test/options.rs
T
vhaudiquet 674b99b00b
CI / build (push) Successful in 1m40s
CI / test (push) Skipped
CI / publish (push) Skipped
CI / snap (push) Successful in 2m39s
CI / release (push) Skipped
docs: describe the implemented sandbox architecture
Update the design doc to the shipped shape: the public API, the module
layout, the crate-private Unshare constructors and what the conformance
tests actually cover. Refresh the AGENTS.md scope list and the test
mode wording in the README and CLI.

Rename the last context-named helpers (new_with_base,
unshare_test_sandbox, RunEnv) and the comments that still described the
old abstraction.
2026-11-24 18:35:17 +01:00

220 lines
7.7 KiB
Rust

//! Options of a test run (`pkh test`), and their pure resolution rules.
use crate::report::BuildView;
use std::path::PathBuf;
/// Testbed mode: where the tests run.
///
/// The default is [`TestMode::Chroot`]: `pkh deb` defaults to the local
/// sandbox because builds are non-destructive, but autopkgtests install
/// packages and are destructive by design — isolation is the default and
/// running on the host is the explicit choice.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TestMode {
/// An ephemeral unshare chroot, bootstrapped like a `pkh deb` build
/// chroot (shared tarball cache).
#[default]
Chroot,
/// The host directly: no isolation, tests can rewrite it.
Local,
/// A qemu virtual machine, through the installed autopkgtest's qemu
/// runner — the one backend pkh does not reimplement.
Vm,
}
impl TestMode {
/// Label used in the report and the view status.
pub fn label(&self) -> &'static str {
match self {
TestMode::Chroot => "chroot",
TestMode::Local => "local",
TestMode::Vm => "vm",
}
}
/// Parse a `--mode` value.
pub fn parse(value: &str) -> Option<TestMode> {
match value {
"chroot" => Some(TestMode::Chroot),
"local" => Some(TestMode::Local),
"vm" => Some(TestMode::Vm),
_ => None,
}
}
}
/// Parameters of one [`crate::test::run`] call.
pub struct TestOptions<'a> {
/// Source tree to test; defaults to the process working directory.
pub tree: Option<PathBuf>,
/// Target distribution series; defaults to the changelog series
/// (UNRELEASED resolves to the vendor's development series).
pub series: Option<String>,
/// Target distribution (debian/ubuntu); defaults to the host vendor.
pub dist: Option<String>,
/// Target architecture; defaults to the host architecture.
pub arch: Option<String>,
/// Distribution pocket to resolve test dependencies from.
pub pocket: Option<String>,
/// PPAs to add as dependency sources (`user/ppa_name`).
pub ppa: Vec<String>,
/// Packages to inject into the testbed before the tests (.deb paths
/// or archive names).
pub inject: Vec<String>,
/// Testbed mode; defaults to [`TestMode::Chroot`].
pub mode: Option<TestMode>,
/// VM image for [`TestMode::Vm`] (required there).
pub image: Option<PathBuf>,
/// VM virtual CPUs ([`TestMode::Vm`], passthrough).
pub cpus: Option<u32>,
/// VM memory size in MiB ([`TestMode::Vm`], passthrough).
pub ram_size: Option<String>,
/// Explicit binary packages to test; overrides the `pkh deb` output
/// reuse.
pub debs: Vec<PathBuf>,
/// Never build: error out when no fresh `pkh deb` output exists.
pub no_build: bool,
/// Ignore fresh `pkh deb` output and rebuild through the deb flow.
pub build: bool,
/// Run only these tests (exact names); empty = all.
pub test_names: Vec<String>,
/// Never run these tests (they are reported as skipped).
pub skip_tests: Vec<String>,
/// Extra shell commands run in the testbed after the APT setup.
pub setup_commands: Vec<String>,
/// Upgrade the testbed (update/upgrade/autopurge) before the tests.
pub apt_upgrade: bool,
/// Drop into an interactive testbed shell after the run.
pub shell: bool,
/// Drop into an interactive testbed shell when a test failed.
pub shell_fail: bool,
/// Per-test timeout in seconds; defaults to autopkgtest's 10 000 s,
/// scaled by `timeout_factor`.
pub timeout: Option<u64>,
/// Scale the default timeouts (`--timeout-factor`).
pub timeout_factor: Option<f64>,
/// Exit 1 when tests were skipped or flaky (`--fail-on skip`).
pub fail_on_skip: bool,
/// Exit 1 when no tests ran (`--fail-on no-tests`).
pub fail_on_no_tests: bool,
/// Render the report as JSON instead of text.
pub json: bool,
/// Raw test output instead of the summarized live view.
pub verbose: bool,
/// Where run events (phases, progress, outcome) are reported.
pub view: &'a dyn BuildView,
}
impl Default for TestOptions<'_> {
fn default() -> Self {
static QUIET: crate::report::Quiet = crate::report::Quiet;
TestOptions {
tree: None,
series: None,
dist: None,
arch: None,
pocket: None,
ppa: Vec::new(),
inject: Vec::new(),
mode: None,
image: None,
cpus: None,
ram_size: None,
debs: Vec::new(),
no_build: false,
build: false,
test_names: Vec::new(),
skip_tests: Vec::new(),
setup_commands: Vec::new(),
apt_upgrade: false,
shell: false,
shell_fail: false,
timeout: None,
timeout_factor: None,
fail_on_skip: false,
fail_on_no_tests: false,
json: false,
verbose: false,
view: &QUIET,
}
}
}
impl TestOptions<'_> {
/// Whether a declared test is selected for the run: an explicit
/// `--test-name` list must contain it, and no `--skip-test` may.
pub fn is_selected(&self, name: &str) -> bool {
(self.test_names.is_empty() || self.test_names.iter().any(|n| n == name))
&& !self.skip_tests.iter().any(|n| n == name)
}
/// The per-test timeout in seconds: an explicit `--timeout` stands as
/// given; otherwise the autopkgtest default is scaled by
/// `--timeout-factor`.
pub fn timeout_secs(&self) -> u64 {
match (self.timeout, self.timeout_factor) {
(Some(secs), _) => secs,
(None, factor) => (10_000_f64 * factor.unwrap_or(1.0)) as u64,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn options(
test_names: &[&str],
skip_tests: &[&str],
timeout: Option<u64>,
factor: Option<f64>,
) -> TestOptions<'static> {
TestOptions {
test_names: test_names.iter().map(|s| s.to_string()).collect(),
skip_tests: skip_tests.iter().map(|s| s.to_string()).collect(),
timeout,
timeout_factor: factor,
..Default::default()
}
}
#[test]
fn selection_filters() {
let all = options(&[], &[], None, None);
assert!(all.is_selected("smoke"));
let only = options(&["smoke", "lint"], &[], None, None);
assert!(only.is_selected("smoke"));
assert!(!only.is_selected("other"));
let skipped = options(&[], &["heavy"], None, None);
assert!(skipped.is_selected("smoke"));
assert!(!skipped.is_selected("heavy"));
// An explicit list and a skip compose: the skip wins.
let both = options(&["smoke", "heavy"], &["heavy"], None, None);
assert!(both.is_selected("smoke"));
assert!(!both.is_selected("heavy"));
}
#[test]
fn timeout_resolution() {
// autopkgtest's default test timeout, unscaled.
assert_eq!(options(&[], &[], None, None).timeout_secs(), 10_000);
// The factor scales only the default.
assert_eq!(options(&[], &[], None, Some(2.0)).timeout_secs(), 20_000);
// An explicit timeout stands as given.
assert_eq!(options(&[], &[], Some(60), Some(2.0)).timeout_secs(), 60);
}
#[test]
fn mode_parsing() {
assert_eq!(TestMode::parse("chroot"), Some(TestMode::Chroot));
assert_eq!(TestMode::parse("local"), Some(TestMode::Local));
assert_eq!(TestMode::parse("vm"), Some(TestMode::Vm));
assert_eq!(TestMode::parse("other"), None);
assert_eq!(TestMode::default(), TestMode::Chroot);
assert_eq!(TestMode::Vm.label(), "vm");
}
}