mod common; use std::sync::Arc; use aur_build::db::{self, builds, model::*}; use aur_build::error::{Error, Result}; use aur_build::pipeline::{self, Ctx}; use aur_build::sandbox::loopback::LoopbackBackend; use aur_build::sandbox::{Exit, Sandbox, SandboxBackend, SandboxInfo, SandboxSpec}; use common::*; #[derive(Clone, Copy, PartialEq, Eq)] enum Fault { DieMidRun, TtlMidBuild, NoCapacity, EmptyWorld, } struct FakeBackend { cfg: aur_build::config::Config, fault: Fault, } impl SandboxBackend for FakeBackend { fn acquire(&self, spec: &SandboxSpec) -> Result> { if self.fault == Fault::NoCapacity { return Err(Error::pool_exhausted("fake pool at quota")); } let inner = LoopbackBackend::new(&self.cfg).acquire(spec)?; Ok(Arc::new(FakeSandbox { inner, fault: self.fault })) } fn status(&self) -> Result> { Ok(vec![SandboxInfo { id: "fake".into(), profile: "fake".into() }]) } } struct FakeSandbox { inner: std::sync::Arc, fault: Fault, } impl FakeSandbox { fn is_build(cmd: &aur_build::sandbox::Cmd) -> bool { cmd.argv.first().is_some_and(|a| a.ends_with("makepkg")) } fn is_query(cmd: &aur_build::sandbox::Cmd) -> bool { cmd.argv.get(1).is_some_and(|a| a == "-Q") } } impl Sandbox for FakeSandbox { fn id(&self) -> &str { self.inner.id() } fn run(&self, cmd: &aur_build::sandbox::Cmd, on_log: &mut dyn FnMut(&[u8])) -> Result { match self.fault { Fault::DieMidRun if Self::is_build(cmd) => Err(Error::other("sandbox vanished mid-run (connection reset)")), Fault::TtlMidBuild if Self::is_build(cmd) => Err(Error::other("sandbox ttl expired during build")), Fault::EmptyWorld if Self::is_query(cmd) => { Ok(Exit { code: Some(0), stdout: Vec::new(), stderr: Vec::new() }) } _ => self.inner.run(cmd, on_log), } } fn put(&self, digest: &str, r: &mut dyn std::io::Read) -> Result<()> { self.inner.put(digest, r) } fn get(&self, digest: &str) -> Result> { self.inner.get(digest) } fn destroy(&self) -> Result<()> { self.inner.destroy() } } fn approve_and_enqueue(env: &Env, name: &str) -> String { aur_build::watch::detect::check_pkgbase(&env.cfg, &env.db, name).unwrap(); let c = aur_build::review::candidates::pending_candidates(&env.db).unwrap().remove(0); let dd = aur_build::review::diff::render_and_store(&env.db, &env.cfg, &c).unwrap(); aur_build::review::approve::approve(&env.db, "tester", c.id, &c.identity_hash, &dd, None).unwrap(); let c = aur_build::review::candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap(); pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap() } fn run_with_fault(env: &Env, id: &str, fault: Fault) -> pipeline::Outcome { let mut ctx = Ctx::new(&env.cfg, &env.db, id).unwrap(); ctx.backend = Arc::new(FakeBackend { cfg: env.cfg.clone(), fault }); pipeline::run_with(&ctx).unwrap() } #[test] fn a_sandbox_that_dies_mid_run_expires_its_lease_and_the_requeue_finishes_the_build() { let (env, _fx) = env_with_fixture("plain-make"); let id = approve_and_enqueue(&env, "plain-make"); match run_with_fault(&env, &id, Fault::DieMidRun) { pipeline::Outcome::Failed { step, error, .. } => { assert_eq!(step, "build"); assert!(error.contains("vanished"), "{error}"); } other => panic!("{other:?}"), } env.exec( "UPDATE build SET state='leased', lease_owner='dead-worker', lease_expires_at='2000-01-01T00:00:00Z' WHERE id=?1", ¶ms(vec![id.as_str()]), ); assert_eq!(builds::reap_expired(&env.db).unwrap(), 1); assert!(matches!(pipeline::run(&env.cfg, &env.db, &id).unwrap(), pipeline::Outcome::Succeeded { .. })); assert_eq!(db::index_version(&env.db, "plain-make").unwrap().as_deref(), Some("1.0-1")); } #[test] fn a_ttl_that_bites_mid_build_is_a_failed_step_not_a_lost_build() { let (env, _fx) = env_with_fixture("plain-make"); let id = approve_and_enqueue(&env, "plain-make"); match run_with_fault(&env, &id, Fault::TtlMidBuild) { pipeline::Outcome::Failed { step, error, transient } => { assert_eq!(step, "build"); assert!(error.contains("ttl expired"), "{error}"); assert!(!transient, "a sandbox TTL is deterministic; do not retry-burn it"); } other => panic!("{other:?}"), } assert!(db::accepted_release(&env.db, "plain-make").unwrap().is_none()); } #[test] fn a_full_pool_blocks_the_build_instead_of_breaking_it() { let (env, _fx) = env_with_fixture("plain-make"); let id = approve_and_enqueue(&env, "plain-make"); match run_with_fault(&env, &id, Fault::NoCapacity) { pipeline::Outcome::Blocked { on } => { assert_eq!(on, vec!["sandbox-pool".to_string()]); } other => panic!("a quota is a wait, not a failure: {other:?}"), } let b = builds::get(&env.db, &id).unwrap().unwrap(); assert_eq!( b.state, BuildState::BlockedMissingDep, "the build waits for capacity on the same blocked machinery it uses for missing deps, unharmed" ); assert_eq!(b.blocked_on_json.as_str(), r#"["sandbox-pool"]"#, "the block names what it waits on"); } #[test] fn an_incomplete_closure_fails_closed_as_a_world_mismatch() { let (env, _plain, _client) = env_with_two_fixtures("plain-make", "dep-client"); let id = approve_and_enqueue(&env, "plain-make"); assert!(matches!(pipeline::run(&env.cfg, &env.db, &id).unwrap(), pipeline::Outcome::Succeeded { .. })); let id2 = approve_and_enqueue(&env, "dep-client"); match run_with_fault(&env, &id2, Fault::EmptyWorld) { pipeline::Outcome::Failed { step, error, transient } => { assert_eq!(step, "chroot", "provisioning is where a lying world gets caught"); assert!(error.contains("world is missing plain-make"), "{error}"); assert!(!transient, "a mismatched world is fail-closed, never retried"); } other => panic!("{other:?}"), } assert!(db::accepted_release(&env.db, "dep-client").unwrap().is_none()); }