test: add pkh test, the native DEP-8 runner for local and chroot

The DEP-8 run-time contract is one page, so pkh implements it natively
instead of wrapping autopkgtest (see plans/pkh-test.md): the testbed is
a pkh context — chroot mode reuses the deb ephemeral chroots and their
cached mmdebstrap tarballs, local mode runs on the current context —
and the binaries under test are the freshness-guarded pkh deb output
next to the tree, rebuilt through the deb flow when stale or missing.

Per test: restrictions gate on the testbed (isolation-machine,
needs-reboot and container isolation skip with a pointer at --mode vm),
the stanza Depends are expanded (@, @builddeps@, @recommends@) and
installed, then the script or Test-Command runs in the staged tree with
the AUTOPKGTEST_* (and legacy ADT_*) environment, a per-test timeout
and the stderr/exit-77 verdict rules; flaky tests retry once. stdout,
stderr and $AUTOPKGTEST_ARTIFACTS are collected per test under
/var/tmp/pkh/tests/ (pruned separately). --shell/--shell-fail drop
into the testbed for debugging.

pin_pocket and install_injected_packages become crate-internal helpers
shared with the deb flow, which they mirror.
This commit is contained in:
vhaudiquet committed 2026-09-27 21:00:34 +02:00
1 parent d30b179430
commit 299b5c28c3
9 files changed
+2049 -3

No files matched your search

+204
View File
@@ -0,0 +1,204 @@
//! 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
/// context 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 current context directly: no isolation, tests can rewrite it.
Local,
}
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",
}
}
/// Parse a `--mode` value.
pub fn parse(value: &str) -> Option<TestMode> {
match value {
"chroot" => Some(TestMode::Chroot),
"local" => Some(TestMode::Local),
_ => 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>,
/// 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,
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"), None);
assert_eq!(TestMode::default(), TestMode::Chroot);
assert_eq!(TestMode::Local.label(), "local");
}
}