//! End-to-end pipeline tests driven entirely by the toolchain shims (§16a): //! no root, no network, no real chroot — but the *real* step machine runs. mod common; use aur_build::db::{self, builds, model::*}; use aur_build::pipeline::{self, publish}; use aur_build::review::{approve, candidates, diff}; use common::*; /// subscribe -> detect -> render the review -> approve. fn approve_fixture(env: &Env, name: &str) -> CandidateRow { aur_build::watch::detect::check_pkgbase(&env.cfg, &env.db, name).unwrap(); let c = candidates::pending_candidates(&env.db).unwrap().remove(0); let dd = diff::render_and_store(&env.db, &env.cfg, &c).unwrap(); approve::approve(&env.db, "tester", c.id, &c.identity_hash, &dd, None).unwrap(); candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap() } /// Re-detect and approve whatever is pending now (a later version). fn approve_pending(env: &Env, name: &str) -> CandidateRow { approve_fixture(env, name) } fn run_for(env: &Env, c: &CandidateRow) -> pipeline::Outcome { let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, c).unwrap(); pipeline::run(&env.cfg, &env.db, &id).unwrap() } fn in_index(env: &Env) -> Vec<(String, String)> { env.db .read(|c| { let mut st = c.prepare("SELECT name, version FROM package WHERE state='in_index' ORDER BY name")?; let rows = st.query_map([], |r| Ok((r.get(0)?, r.get(1)?)))?; let mut v = Vec::new(); for r in rows { v.push(r?); } Ok(v) }) .unwrap() } fn step_rows(env: &Env) -> Vec<(String, String)> { env.db .read(|c| { let mut st = c.prepare("SELECT step, status FROM build_step ORDER BY rowid")?; let rows = st.query_map([], |r| Ok((r.get(0)?, r.get(1)?)))?; let mut v = Vec::new(); for r in rows { v.push(r?); } Ok(v) }) .unwrap() } #[test] fn end_to_end_publishes_a_signed_index() { let (env, _fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); let out = run_for(&env, &c); assert!(matches!(out, pipeline::Outcome::Succeeded { .. }), "{out:?}"); let rel = db::accepted_release(&env.db, "plain-make").unwrap().unwrap(); assert_eq!(rel.state, ReleaseState::Published); assert_eq!(rel.pkgver, "1.0"); assert_eq!(rel.version(), "1.0-1"); // Both the packages and the database are signed (§9). let arch = env.cfg.repo_arch_dir(); assert!(arch.join("plain-make-1.0-1-x86_64.pkg.tar.zst").exists()); assert!(arch.join("plain-make-1.0-1-x86_64.pkg.tar.zst.sig").exists()); assert!(arch.join("aur-build.db.tar.gz").exists()); assert!(arch.join("aur-build.db.tar.gz.sig").exists()); // One checkpoint row per step, all green (§8.1). let all = step_rows(&env); assert_eq!(all.len(), BUILD_STEPS.len(), "one row per step: {all:?}"); assert!( all.iter().all(|(_, s)| s == "succeeded" || s == "skipped"), "{all:?}" ); } #[test] fn the_build_never_signs_ingest_does() { // Signing moved hub-side: the build environment is a sandbox that must // never hold the key, so the build command carries no `-si` and the // signature over each artifact is made at ingest by the hub's own gpg. // This is the property that makes a remote build sandbox safe to run at // all — worst the sandbox ever returns is unsigned bytes. let (env, _fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); assert!(matches!(run_for(&env, &c), pipeline::Outcome::Succeeded { .. })); let shimlog = env.cfg.hooks.shim_dir.clone().unwrap(); assert_eq!( aur_build::shims::called_with(&shimlog, "makechrootpkg", "-si"), 0, "the build step must not hand a signing key to the sandbox" ); let (digest, sig_path): (String, Option) = env .db .read(|c| { Ok(c.prepare("SELECT digest, signature_path FROM artifact LIMIT 1")? .query_row([], |r| Ok((r.get(0)?, r.get(1)?)))?) }) .unwrap(); assert!(sig_path.is_some(), "ingest must record a signature for the artifact"); assert!(std::path::Path::new(sig_path.as_ref().unwrap()).exists()); assert!( aur_build::shims::called_with(&shimlog, "gpg", &digest) > 0, "the signature ingest recorded was made over the stored artifact's bytes" ); } #[test] fn i1_no_index_entry_without_an_approval() { let (env, _fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); run_for(&env, &c); let orphans: i64 = env .db .read(|c| { Ok(c.query_row( "SELECT count(*) FROM package p LEFT JOIN release r ON r.id = p.release_id WHERE p.state='in_index' AND (r.id IS NULL OR r.approval_id IS NULL)", [], |r| r.get(0), )?) }) .unwrap(); assert_eq!(orphans, 0, "I1: index entry with no approval behind it"); } #[test] fn i8_served_index_holds_exactly_the_accepted_pointers() { let (env, fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); run_for(&env, &c); assert_eq!(in_index(&env), vec![("plain-make".into(), "1.0-1".into())]); fx.bump(&[("pkgver", "1.0.1")]); let c2 = approve_pending(&env, "plain-make"); run_for(&env, &c2); // The old version is gone from the index, not merely superseded in place. assert_eq!(in_index(&env), vec![("plain-make".into(), "1.0.1-1".into())]); let rel = db::accepted_release(&env.db, "plain-make").unwrap().unwrap(); assert_eq!(rel.pkgver, "1.0.1"); let old: i64 = env .db .read(|c| { Ok(c.query_row( "SELECT count(*) FROM package WHERE state='in_index' AND version='1.0-1'", [], |r| r.get(0), )?) }) .unwrap(); assert_eq!(old, 0, "the previous release must leave the index"); } #[test] fn step0_fails_closed_when_the_recipe_hash_drifts() { let (env, _fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); // The mirror no longer matches what was approved. There is no path from // "AUR moved since the review" to "we built it anyway" (I3). env.exec( "UPDATE snapshot SET recipe_hash='deadbeef' WHERE id=?1", ¶ms(vec![c.snapshot_id]), ); let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); match pipeline::run(&env.cfg, &env.db, &id).unwrap() { pipeline::Outcome::Failed { step, error, .. } => { assert_eq!(step, "materialize"); assert!(error.contains("recipe_hash mismatch"), "{error}"); } other => panic!("expected a fail-closed failure, got {other:?}"), } assert!(db::accepted_release(&env.db, "plain-make").unwrap().is_none()); assert!(in_index(&env).is_empty()); } #[test] fn step4_fails_closed_when_pkgver_no_longer_matches() { let (env, fx) = env_with_fixture("git-sourced"); let c = approve_fixture(&env, "git-sourced"); assert_eq!(c.pkgver, "r1.0"); // Upstream moves after approval: the tool now resolves a different pkgver. // The build must refuse rather than silently ship the newer one (§8.1). let mut m = fx.manifest_json(); m["vercheck"] = serde_json::json!("r9.9-1"); std::fs::write(&fx.manifest, serde_json::to_string(&m).unwrap()).unwrap(); match run_for(&env, &c) { pipeline::Outcome::Failed { step, error, .. } => { assert_eq!(step, "pkgver"); assert!(error.contains("r9.9") && error.contains("r1.0"), "{error}"); } other => panic!("expected a fail-closed pkgver failure, got {other:?}"), } assert!(in_index(&env).is_empty()); } #[test] fn step4_passes_when_the_vcs_pkgver_still_matches() { let (env, _fx) = env_with_fixture("git-sourced"); let c = approve_fixture(&env, "git-sourced"); assert!(matches!(run_for(&env, &c), pipeline::Outcome::Succeeded { .. })); assert_eq!(in_index(&env), vec![("git-sourced".into(), "r1.0-1".into())]); } #[test] fn i4_kill_mid_publish_leaves_the_old_index_intact() { let (mut env, fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); run_for(&env, &c); let v1 = env.cfg.repo_arch_dir().join("aur-build.db.tar.gz"); let before = std::fs::read(&v1).unwrap(); fx.bump(&[("pkgver", "1.0.1")]); let c2 = approve_pending(&env, "plain-make"); env.set_publish_fault("before-rename"); let out = run_for(&env, &c2); assert!(matches!(out, pipeline::Outcome::Failed { .. }), "{out:?}"); // The served database is byte-for-byte the old one, and the DB never // recorded the new release: consumers saw either the old world or the new // one, never a half-written index (I4). assert_eq!(std::fs::read(&v1).unwrap(), before); assert_eq!(in_index(&env), vec![("plain-make".into(), "1.0-1".into())]); assert_eq!( env.query_i64("SELECT count(*) FROM release WHERE pkgver='1.0.1'", &[]), Some(0) ); } #[test] fn i5_a_restarted_build_resumes_at_the_next_incomplete_step() { let (mut env, fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); run_for(&env, &c); fx.bump(&[("pkgver", "1.0.1")]); let c2 = approve_pending(&env, "plain-make"); let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c2).unwrap(); env.set_publish_fault("before-rename"); assert!(matches!( pipeline::run(&env.cfg, &env.db, &id).unwrap(), pipeline::Outcome::Failed { .. } )); let builds_after_crash = aur_build::shims::called_with(&env.cfg.hooks.shim_dir.clone().unwrap(), "makechrootpkg", ""); assert_eq!(builds_after_crash, 2, "one real build before the crash"); // "Restart": same build row, no fault. Steps 0-8 are checkpointed as // succeeded, so only publish re-runs — the expensive step is not repeated. env.clear_publish_fault(); match pipeline::run(&env.cfg, &env.db, &id).unwrap() { pipeline::Outcome::Succeeded { release_id } => assert!(release_id > 0), other => panic!("{other:?}"), } assert_eq!( aur_build::shims::called_with(&env.cfg.hooks.shim_dir.clone().unwrap(), "makechrootpkg", ""), builds_after_crash, "I5: resuming must not rebuild" ); assert_eq!(in_index(&env), vec![("plain-make".into(), "1.0.1-1".into())]); // Exactly one release row per version: no duplicates from the resume. assert_eq!( env.query_i64("SELECT count(*) FROM release WHERE pkgver='1.0.1'", &[]), Some(1) ); } #[test] fn split_pkgbase_is_one_approval_and_many_index_rows() { let (env, _fx) = env_with_fixture("split-pkgbase"); let c = approve_fixture(&env, "split-pkgbase"); assert!(matches!(run_for(&env, &c), pipeline::Outcome::Succeeded { .. })); let names: Vec = in_index(&env).into_iter().map(|(n, _)| n).collect(); assert!(names.contains(&"split-pkgbase".to_string()), "{names:?}"); assert!(names.contains(&"split-pkgbase-docs".to_string()), "{names:?}"); // One approval, one release, N packages — the §3 split-pkgbase rule. assert_eq!(env.query_i64("SELECT count(*) FROM approval", &[]), Some(1)); assert_eq!(env.query_i64("SELECT count(*) FROM release", &[]), Some(1)); } #[test] fn a_failed_build_leaves_the_previous_version_serving() { let (mut env, fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); run_for(&env, &c); fx.bump(&[("pkgver", "1.0.1")]); let c2 = approve_pending(&env, "plain-make"); // The build itself fails this time. env.set_shim_fail("makechrootpkg"); let out = run_for(&env, &c2); match out { pipeline::Outcome::Failed { step, .. } => assert_eq!(step, "build"), other => panic!("{other:?}"), } // §8.4: the old version keeps serving and the candidate stays approved. assert_eq!(in_index(&env), vec![("plain-make".into(), "1.0-1".into())]); assert_eq!( candidates::candidate_by_id(&env.db, c2.id).unwrap().unwrap().state, CandidateState::Approved ); assert_eq!( env.query_i64("SELECT count(*) FROM package WHERE state='in_index' AND version='1.0.1-1'", &[]), Some(0) ); } #[test] fn an_unresolvable_dependency_blocks_instead_of_failing() { let (env, _fx) = env_with_fixture("missing-dep"); // We subscribe to `dep-partner` but have never published it: that is exactly // "unresolvable" in §10.4, and the answer is to wait, not to fail. env.subscribe("dep-partner"); let c = approve_fixture(&env, "missing-dep"); let out = run_for(&env, &c); match out { pipeline::Outcome::Blocked { on } => { assert!(on.contains(&"dep-partner".to_string()), "{on:?}"); } other => panic!("expected a blocked build, got {other:?}"), } let (bid, _name, blocked_on) = builds::blocked_builds(&env.db).unwrap().remove(0); assert_eq!(blocked_on, vec!["dep-partner".to_string()]); assert_eq!(builds::get(&env.db, &bid).unwrap().unwrap().state, BuildState::BlockedMissingDep); assert!(in_index(&env).is_empty(), "a blocked build publishes nothing"); } #[test] fn a_blocked_build_waits_until_its_dependency_actually_arrives() { let (env, _fx) = env_with_fixture("missing-dep"); env.subscribe("dep-partner"); let c = approve_fixture(&env, "missing-dep"); assert!(matches!(run_for(&env, &c), pipeline::Outcome::Blocked { .. })); assert_eq!(builds::blocked_builds(&env.db).unwrap().len(), 1); // The dispatcher sweeps blocked builds every pass. Requeueing one whose // blocker is still missing would block again immediately — a busy loop that // burns a claim and a plan step each tick — so it must stay put. let _ = publish::reconcile(&env.cfg, &env.db); assert_eq!(builds::requeue_blocked(&env.db).unwrap(), 0, "requeued while still blocked"); assert_eq!(builds::blocked_builds(&env.db).unwrap().len(), 1); } #[test] fn re_enqueueing_the_same_intent_is_a_noop() { let (env, _fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); let a = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); let b = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); assert_eq!(a, b, "build ids are deterministic (§8.2)"); assert_eq!(env.query_i64("SELECT count(*) FROM build", &[]), Some(1)); } #[test] fn i6_manual_rebuild_reuses_the_approved_identity() { let (env, _fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); run_for(&env, &c); let before = env.query_i64("SELECT count(*) FROM approval", &[]); let id = pipeline::enqueue_manual_rebuild(&env.cfg, &env.db, "plain-make").unwrap(); assert!(matches!( pipeline::run(&env.cfg, &env.db, &id).unwrap(), pipeline::Outcome::Succeeded { .. } )); // Same version, new release row, and crucially: no new approval (§2 I6). assert_eq!(env.query_i64("SELECT count(*) FROM approval", &[]), before); assert_eq!(in_index(&env), vec![("plain-make".into(), "1.0-1".into())]); assert_eq!(env.query_i64("SELECT count(*) FROM release", &[]), Some(2)); } #[test] fn i12_world_snapshot_is_recorded_at_ingest() { let (env, fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); run_for(&env, &c); // Now a second build whose chroot had our own published package installed: // that is the pair staleness tracking is computed from (§10.1). let mut m = fx.manifest_json(); m["installed"] = serde_json::json!([ {"name": "plain-make", "version": "1.0-1", "arch": "x86_64"}, {"name": "glibc", "version": "2.39-1", "arch": "x86_64"} ]); std::fs::write(&fx.manifest, serde_json::to_string(&m).unwrap()).unwrap(); fx.bump(&[("pkgver", "1.0.1")]); let c2 = approve_pending(&env, "plain-make"); run_for(&env, &c2); let wid = env .db .read(|c| { Ok(c.query_row( "SELECT world_snapshot_id FROM build WHERE world_snapshot_id IS NOT NULL ORDER BY rowid DESC LIMIT 1", [], |r| r.get::<_, i64>(0), )?) }) .unwrap(); let json: String = env .db .read(|c| { Ok(c.query_row("SELECT members_json FROM world_snapshot WHERE id=?1", rusqlite::params![wid], |r| r.get(0))?) }) .unwrap(); let parsed: Vec = serde_json::from_str(&json).unwrap(); let members: Vec<(String, String)> = parsed .iter() .map(|v| { ( v["pkgbase"].as_str().unwrap_or_default().to_string(), v["version"].as_str().unwrap_or_default().to_string(), ) }) .collect(); // The snapshot is the installed set INTERSECTED with our own packages: // unfiltered, Arch moving would make every package permanently stale. assert!(members.iter().any(|(n, v)| n == "plain-make" && v == "1.0-1"), "{members:?}"); assert!(!members.iter().any(|(n, _)| n == "glibc"), "{members:?}"); } #[test] fn publish_refuses_without_an_approval_behind_it() { let (env, _fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); run_for(&env, &c); // Approval rows are immutable (I10 / §7.2), so we build the bad state from // the other side: a release with nothing behind it. A rebuild of it must // still refuse to publish — the guard is on publishing, not on noticing. let rid = db::accepted_release(&env.db, "plain-make").unwrap().unwrap().id; env.exec("UPDATE release SET approval_id=NULL WHERE id=?1", ¶ms(vec![rid])); let id = pipeline::enqueue_manual_rebuild(&env.cfg, &env.db, "plain-make").unwrap(); match pipeline::run(&env.cfg, &env.db, &id).unwrap() { pipeline::Outcome::Failed { step, error, .. } => { assert_eq!(step, "publish"); assert!(error.contains("no approval"), "{error}"); } other => panic!("I1 requires a refusal, got {other:?}"), } // The index still holds exactly the one entry that does have an approval. assert_eq!(in_index(&env).len(), 1); } #[test] fn a_stale_accepted_pointer_refuses_to_publish() { let (env, _fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); // The approval was taken against "nothing accepted yet". Move the pointer // out from under the build and the compare-and-swap must refuse. env.exec( "UPDATE pkgbase SET current_accepted_release_id=4242 WHERE name='plain-make'", &[], ); match pipeline::run(&env.cfg, &env.db, &id).unwrap() { pipeline::Outcome::Failed { step, error, .. } => { assert_eq!(step, "publish"); assert!(error.contains("accepted pointer moved"), "{error}"); } other => panic!("expected a CAS refusal, got {other:?}"), } assert!(in_index(&env).is_empty()); } // ------------------------------------------------------- withdrawal and cleanup -- #[test] fn withdrawing_a_release_twice_is_refused_the_second_time() { // m-10: the second withdraw used to report success while rewriting nothing, // so an operator who clicked twice got a green result and no explanation. let (env, _fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); run_for(&env, &c); let rid = db::accepted_release(&env.db, "plain-make").unwrap().unwrap().id; approve::withdraw(&env.db, "tester", rid, "upstream pulled the tarball").unwrap(); let err = approve::withdraw(&env.db, "tester", rid, "again").unwrap_err(); assert!(err.to_string().contains("not published"), "{err}"); let err = approve::withdraw(&env.db, "tester", rid + 999, "x").unwrap_err(); assert!(err.to_string().contains("no release"), "{err}"); // The first withdrawal stands: the index no longer serves it. assert!(in_index(&env).is_empty(), "the withdrawn release is still in the index"); } #[test] fn a_publish_that_does_not_land_leaves_no_orphan_files_in_the_served_tree() { // m-16: the artifacts are copied into the served tree before the DB commit, // and when the commit refused, those bytes used to stay — files no index // names, sitting in the directory consumers download from. let (mut env, _fx) = env_with_fixture("plain-make"); let c = approve_fixture(&env, "plain-make"); env.set_publish_fault("before-rename"); let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); assert!(matches!( pipeline::run(&env.cfg, &env.db, &id).unwrap(), pipeline::Outcome::Failed { .. } )); let stray: Vec = std::fs::read_dir(env.cfg.repo_arch_dir()) .expect("arch dir") .filter_map(|e| e.ok()) .map(|e| e.file_name().to_string_lossy().to_string()) .filter(|n| n.ends_with(".pkg.tar.zst") || n.ends_with(".sig")) .collect(); assert!(stray.is_empty(), "a publish that did not land left files behind: {stray:?}"); // And the retry still works: the artifacts come from the collect dir, not // from the served tree, so cleaning up costs nothing but the copy. env.clear_publish_fault(); assert!( matches!(pipeline::run(&env.cfg, &env.db, &id).unwrap(), pipeline::Outcome::Succeeded { .. }), "the retry after cleanup failed" ); assert_eq!(in_index(&env), vec![("plain-make".into(), "1.0-1".into())]); }