//! 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 { 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, /// Target distribution series; defaults to the changelog series /// (UNRELEASED resolves to the vendor's development series). pub series: Option, /// Target distribution (debian/ubuntu); defaults to the host vendor. pub dist: Option, /// Target architecture; defaults to the host architecture. pub arch: Option, /// Distribution pocket to resolve test dependencies from. pub pocket: Option, /// PPAs to add as dependency sources (`user/ppa_name`). pub ppa: Vec, /// Packages to inject into the testbed before the tests (.deb paths /// or archive names). pub inject: Vec, /// Testbed mode; defaults to [`TestMode::Chroot`]. pub mode: Option, /// VM image for [`TestMode::Vm`] (required there). pub image: Option, /// VM virtual CPUs ([`TestMode::Vm`], passthrough). pub cpus: Option, /// VM memory size in MiB ([`TestMode::Vm`], passthrough). pub ram_size: Option, /// Explicit binary packages to test; overrides the `pkh deb` output /// reuse. pub debs: Vec, /// 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, /// Never run these tests (they are reported as skipped). pub skip_tests: Vec, /// Extra shell commands run in the testbed after the APT setup. pub setup_commands: Vec, /// 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, /// Scale the default timeouts (`--timeout-factor`). pub timeout_factor: Option, /// 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, factor: Option, ) -> 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"); } }