diff options
Diffstat (limited to 'tests/pipeline.rs')
| -rw-r--r-- | tests/pipeline.rs | 439 |
1 files changed, 439 insertions, 0 deletions
diff --git a/tests/pipeline.rs b/tests/pipeline.rs new file mode 100644 index 0000000..835dcf0 --- /dev/null +++ b/tests/pipeline.rs @@ -0,0 +1,439 @@ +//! 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, release_state::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 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.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.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<String> = 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, + candidate_state::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, build_state::BLOCKED_MISSING_DEP); + assert!(in_index(&env).is_empty(), "a blocked build publishes nothing"); +} + +#[test] +fn publishing_a_dependency_requeues_what_it_unblocks() { + 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); + // Something lands in the index: blocked builds get another look (§10.4). + let n = publish::reconcile(&env.cfg, &env.db).unwrap_or(0); + let _ = n; + let requeued = builds::requeue_blocked(&env.db).unwrap(); + assert_eq!(requeued, 1); + assert_eq!(builds::blocked_builds(&env.db).unwrap().len(), 0); +} + +#[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::Value> = 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"); + let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); + // Strip the approval: the publish step must refuse, not publish anyway. + env.exec("DELETE FROM approval", &[]); + 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:?}"), + } + assert!(in_index(&env).is_empty()); +} + +#[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()); +} |
