diff options
Diffstat (limited to 'tests/staleness.rs')
| -rw-r--r-- | tests/staleness.rs | 203 |
1 files changed, 203 insertions, 0 deletions
diff --git a/tests/staleness.rs b/tests/staleness.rs new file mode 100644 index 0000000..c99c666 --- /dev/null +++ b/tests/staleness.rs @@ -0,0 +1,203 @@ +//! Staleness, blocked builds and the dependents question (§10). +//! +//! The design's position is that staleness is a fact to be shown, not a state to +//! be stored: there is no automatic rebuild engine, and no `stale` column, because +//! a stored flag goes wrong the moment anything else is published and something +//! else always is. These tests pin that down — including the part that is easy to +//! fake, which is that the derivation is real and not a cached answer. + +mod common; + +use aur_build::db::{builds, model::*}; +use aur_build::pipeline; +use aur_build::review::{candidates, diff}; +use aur_build::watch::staleness; +use common::*; + +fn approved(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(); + let c = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap(); + aur_build::review::approve::approve(&env.db, "t", c.id, &c.identity_hash, &dd, None).unwrap(); + candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap() +} + +fn publish(env: &Env, name: &str) { + let c = approved(env, name); + let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); + pipeline::run(&env.cfg, &env.db, &id).unwrap(); +} + +#[test] +fn staleness_is_derived_on_read_and_nothing_stores_it() { + let (env, plain, client) = env_with_two_fixtures("plain-make", "dep-client"); + publish(&env, "plain-make"); + publish(&env, "dep-client"); + assert!(staleness::stale_rows(&env.db).unwrap().is_empty(), "both were just published"); + + plain.bump(&[("pkgver", "2.0")]); + publish(&env, "plain-make"); + + let stale = staleness::stale_rows(&env.db).unwrap(); + assert_eq!(stale.len(), 1, "dep-client should be stale: {stale:?}"); + assert_eq!(stale[0].pkgbase, "dep-client"); + assert_eq!(stale[0].dependency, "plain-make"); + assert_eq!(stale[0].built_with, "1.0-1"); + assert_eq!(stale[0].now, "2.0-1"); + + // "Derived purely on read" has to mean there is nowhere for a stale answer to + // hide. No column on any table stores it. + let cols: Vec<String> = env + .db + .read(|c| { + let mut st = c.prepare( + "SELECT m.name || '.' || p.name FROM sqlite_master m, pragma_table_info(m.name) p + WHERE m.type='table' AND p.name LIKE '%stale%'", + )?; + let out = st.query_map([], |r| r.get::<_, String>(0))?.filter_map(|x| x.ok()).collect(); + Ok(out) + }) + .unwrap(); + assert!(cols.is_empty(), "staleness must not be persisted anywhere: {cols:?}"); + + // The answer tracks the index, not any stored flag: withdrawing plain-make + // changes what is true about dep-client without touching dep-client at all. + pipeline::publish::withdraw_package(&env.cfg, &env.db, "plain-make").unwrap(); + let after = staleness::stale_rows(&env.db).unwrap(); + assert!( + after.iter().any(|s| s.pkgbase == "dep-client" && s.kind == staleness::Kind::Missing), + "withdrawing the dependency should flip the answer to missing: {after:?}" + ); + // And nothing about dep-client was written to make that happen. + assert_eq!( + env.query_i64("SELECT count(*) FROM release WHERE pkgbase_id=(SELECT id FROM pkgbase WHERE name='dep-client')", &[]), + Some(1), + "the dependent was untouched by its dependency moving" + ); + let _ = client; +} + +#[test] +fn a_missing_dependency_blocks_the_build_and_names_the_blocker_everywhere() { + let (env, fx, _partner) = env_with_two_fixtures("missing-dep", "dep-partner"); + // dep-partner is a package we intend to publish but have not yet: the build + // cannot run, and that is a fact to report rather than a failure to retry. + let c = approved(&env, "missing-dep"); + assert_eq!(c.missing_deps(), vec!["dep-partner".to_string()], "the review must warn before approving"); + let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); + let out = pipeline::run(&env.cfg, &env.db, &id).unwrap(); + assert!(matches!(out, pipeline::Outcome::Blocked { .. }), "{out:?}"); + + let b = builds::get(&env.db, &id).unwrap().unwrap(); + assert_eq!(b.state, build_state::BLOCKED_MISSING_DEP); + let blocked_on: Vec<String> = serde_json::from_str(&b.blocked_on_json).unwrap(); + assert_eq!(blocked_on, vec!["dep-partner".to_string()]); + // Blocked is not failed: nothing went wrong with the build itself. + assert_eq!(env.query_i64("SELECT count(*) FROM build WHERE state='failed'", &[]), Some(0)); + + // It shows on Attention with the blocker named, not just "something is wrong". + let web = Web::attach(env, fx); + let page = web.get("/attention"); + assert!(page.body.contains("Blocked builds"), "no blocked section: {}", &page.body[..600.min(page.body.len())]); + assert!(page.body.contains("dep-partner"), "the blocking pkgbase is not named"); + assert!(page.body.contains("missing-dep"), "the blocked package is not named"); +} + +#[test] +fn a_blocked_build_waits_and_is_requeued_only_when_the_blocker_arrives() { + let (env, fx, _partner) = env_with_two_fixtures("missing-dep", "dep-partner"); + let c = approved(&env, "missing-dep"); + let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap(); + let _ = fx; + pipeline::run(&env.cfg, &env.db, &id).unwrap(); + assert_eq!(builds::get(&env.db, &id).unwrap().unwrap().state, build_state::BLOCKED_MISSING_DEP); + + // The dispatcher sweeps blocked builds every pass. Requeueing one that will + // immediately block again is a busy loop, so it must stay put. + assert_eq!(builds::requeue_blocked(&env.db).unwrap(), 0, "requeued while still blocked"); + assert_eq!(builds::get(&env.db, &id).unwrap().unwrap().state, build_state::BLOCKED_MISSING_DEP); + + // Publishing dep-partner is what unblocks it. A build that was blocked and + // can now run is not a new decision — a human already approved it (§10). + publish(&env, "dep-partner"); + assert!( + aur_build::db::our_package_names(&env.db).unwrap().contains("dep-partner"), + "dep-partner should be in the index now: {:?}", + aur_build::db::our_package_names(&env.db) + ); + // Publishing the blocker requeues what was waiting on it — the publish path + // sweeps blocked builds itself, so an explicit call afterwards is a no-op. + assert_eq!(builds::get(&env.db, &id).unwrap().unwrap().state, build_state::QUEUED, "the blocker arrived and the build stayed blocked"); + assert_eq!(builds::requeue_blocked(&env.db).unwrap(), 0, "nothing left to requeue"); + + // And it now runs to completion. + pipeline::run(&env.cfg, &env.db, &id).unwrap(); + assert_eq!(builds::get(&env.db, &id).unwrap().unwrap().state, build_state::SUCCEEDED); + assert!(aur_build::db::index_version(&env.db, "missing-dep").unwrap().is_some()); +} + +#[test] +fn the_review_page_warns_about_missing_deps_before_approval() { + let (env, fx, _partner) = env_with_two_fixtures("missing-dep", "dep-partner"); + aur_build::watch::detect::check_pkgbase(&env.cfg, &env.db, "missing-dep").unwrap(); + let c = candidates::pending_candidates(&env.db).unwrap().remove(0); + diff::render_and_store(&env.db, &env.cfg, &c).unwrap(); + + let web = Web::attach(env, fx); + let page = web.get(&format!("/reviews/{}", c.id)); + assert!(page.body.contains("Missing dependencies"), "no warning: {}", &page.body[..600.min(page.body.len())]); + assert!(page.body.contains("dep-partner"), "the missing dep is not named"); +} + +#[test] +fn the_review_page_answers_what_approving_this_would_disturb() { + // §10.3: "approving this marks N packages stale". A reviewer approving a + // plain-make bump needs to know that dep-client is about to need a rebuild. + let (env, plain, _client) = env_with_two_fixtures("plain-make", "dep-client"); + publish(&env, "plain-make"); + publish(&env, "dep-client"); + plain.bump(&[("pkgver", "2.0")]); + aur_build::watch::detect::check_pkgbase(&env.cfg, &env.db, "plain-make").unwrap(); + let c = candidates::pending_candidates(&env.db).unwrap().remove(0); + diff::render_and_store(&env.db, &env.cfg, &c).unwrap(); + + let web = Web::attach(env, plain); + let page = web.get(&format!("/reviews/{}", c.id)); + assert!( + page.body.contains("will mark 1 package(s) stale"), + "the dependents question is not answered: {}", + &page.body[..800.min(page.body.len())] + ); + assert!(page.body.contains("dep-client"), "the dependent is not named"); + + // And the same answer is available without the UI. + assert_eq!(staleness::dependents_of(&web.env.db, "plain-make").unwrap(), vec!["dep-client".to_string()]); + // Approving it does not rebuild anything by itself (§10: no rebuild engine). + assert_eq!(web.env.query_i64("SELECT count(*) FROM build", &[]), Some(2), "one build per published package"); +} + +#[test] +fn a_dependency_we_stopped_publishing_shows_as_missing_not_outdated() { + let (env, _plain, client) = env_with_two_fixtures("plain-make", "dep-client"); + publish(&env, "plain-make"); + publish(&env, "dep-client"); + // Withdraw plain-make from the index: dep-client was built against it and now + // depends on something we no longer serve. + pipeline::publish::withdraw_package(&env.cfg, &env.db, "plain-make").unwrap(); + + let stale = staleness::stale_rows(&env.db).unwrap(); + let missing: Vec<_> = stale + .iter() + .filter(|s| s.kind == staleness::Kind::Missing) + .cloned() + .collect(); + assert_eq!(missing.len(), 1, "withdrawal should read as missing: {stale:?}"); + assert_eq!(missing[0].pkgbase, "dep-client"); + assert_eq!(missing[0].dependency, "plain-make"); + + let web = Web::attach(env, client); + let page = web.get("/attention"); + assert!(page.body.contains("Missing:"), "no missing section: {}", &page.body[..600.min(page.body.len())]); + assert!(page.body.contains("dep-client")); +} |
