summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/crash.rs198
1 files changed, 198 insertions, 0 deletions
diff --git a/tests/crash.rs b/tests/crash.rs
new file mode 100644
index 0000000..8d8ba35
--- /dev/null
+++ b/tests/crash.rs
@@ -0,0 +1,198 @@
+//! Crash, cancellation and the failure UI (§8.2, §8.4, I5).
+//!
+//! The difference between this file and the in-process fault hooks elsewhere is
+//! that here the process actually dies. A shim sends `SIGKILL` to its parent in
+//! the middle of the build step: no `Drop`, no unwinding, no chance to clean up.
+//! That is the only way to prove the checkpoint story, because every in-process
+//! simulation of a crash can be caught and tidied by accident.
+
+mod common;
+
+use std::os::unix::process::ExitStatusExt;
+use std::process::Command;
+
+use aur_build::db::{self, builds, model::*};
+use aur_build::pipeline;
+use aur_build::review::{candidates, diff};
+use common::*;
+
+/// Approve a fixture's candidate and hand back the approved row.
+fn approved(env: &Env, name: &str) -> aur_build::db::model::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()
+}
+
+/// Approve and enqueue the build without running it.
+fn queued(env: &Env, name: &str) -> String {
+ let c = approved(env, name);
+ pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap()
+}
+
+fn run_cli(env: &Env, args: &[&str]) -> std::process::Output {
+ Command::new(env!("CARGO_BIN_EXE_aur-build"))
+ .arg("--root")
+ .arg(env.root())
+ .args(args)
+ .output()
+ .expect("spawn aur-build")
+}
+
+#[test]
+fn a_process_killed_mid_step_resumes_from_its_checkpoints_with_no_duplicate_work() {
+ let (mut env, _fx) = env_with_fixture("plain-make");
+ let id = queued(&env, "plain-make");
+ // The child will be murdered the moment it invokes makechrootpkg.
+ env.cfg.shim_kill_parent = Some("makechrootpkg".into());
+ env.cfg.save().unwrap();
+
+ let out = run_cli(&env, &["run", &id]);
+ let status = out.status;
+ assert_eq!(
+ status.signal(),
+ Some(libc::SIGKILL),
+ "the child should have been SIGKILLed mid-step: {} {}",
+ String::from_utf8_lossy(&out.stdout),
+ String::from_utf8_lossy(&out.stderr)
+ );
+
+ // The database survived, because no transaction ever spans a build step.
+ let b = builds::get(&env.db, &id).unwrap().expect("build row survived the kill");
+ assert!(
+ b.state == build_state::LEASED || b.state == build_state::RUNNING,
+ "a killed build must still look claimed, not finished: {}",
+ b.state
+ );
+ assert_eq!(
+ builds::step_status(&env.db, &id, "build").unwrap().as_deref(),
+ Some(step_state::RUNNING),
+ "the step that was running should be recorded as running"
+ );
+ assert_eq!(
+ builds::step_status(&env.db, &id, "chroot").unwrap().as_deref(),
+ Some(step_state::SUCCEEDED),
+ "the completed step before the crash must be checkpointed"
+ );
+ assert!(db::accepted_release(&env.db, "plain-make").unwrap().is_none(), "nothing was published");
+
+ // Time passes; the lease expires. A fresh process's dispatcher reaps it —
+ // the same code path as any other expired lease, with no special recovery.
+ env.exec("UPDATE build SET lease_expires_at='2000-01-01T00:00:00Z' WHERE id=?1", &params(vec![id.as_str()]));
+ assert_eq!(builds::reap_expired(&env.db).unwrap(), 1);
+ assert_eq!(
+ builds::step_status(&env.db, &id, "build").unwrap().as_deref(),
+ Some(step_state::INTERRUPTED)
+ );
+
+ // Resume. The completed steps are not repeated; only the interrupted one and
+ // what follows run.
+ env.cfg.shim_kill_parent = None;
+ env.cfg.save().unwrap();
+ let out = run_cli(&env, &["run", &id]);
+ assert!(out.status.success(), "resume failed: {}", String::from_utf8_lossy(&out.stderr));
+
+ let shim = env.cfg.shim_dir.clone().unwrap();
+ assert_eq!(
+ aur_build::shims::called_with(&shim, "mkarchroot", ""),
+ 1,
+ "the chroot step was checkpointed as done and must not run again"
+ );
+ assert_eq!(
+ aur_build::shims::called_with(&shim, "makechrootpkg", ""),
+ 2,
+ "exactly one rebuild: the crashed attempt and the resumed one"
+ );
+ assert_eq!(aur_build::shims::called_with(&shim, "repo-add", ""), 1, "publish must happen once");
+ assert_eq!(
+ env.query_i64("SELECT count(*) FROM release WHERE pkgbase_id=(SELECT id FROM pkgbase WHERE name='plain-make')", &[]),
+ Some(1),
+ "no duplicate release from the resume"
+ );
+ assert_eq!(db::index_version(&env.db, "plain-make").unwrap().as_deref(), Some("1.0-1"));
+ assert_eq!(builds::get(&env.db, &id).unwrap().unwrap().state, build_state::SUCCEEDED);
+}
+
+#[test]
+fn a_cancelled_build_stops_at_a_step_boundary_and_publishes_nothing() {
+ // §8.2: cancellation is cooperative at a step boundary. A worker never kills
+ // a step halfway through, because that is exactly the state a crash leaves.
+ let (env, _fx) = env_with_fixture("plain-make");
+ let id = queued(&env, "plain-make");
+ builds::request_cancel(&env.db, &id).unwrap();
+ let out = pipeline::run(&env.cfg, &env.db, &id).unwrap();
+ assert!(matches!(out, pipeline::Outcome::Cancelled), "{out:?}");
+ assert_eq!(builds::get(&env.db, &id).unwrap().unwrap().state, build_state::CANCELLED);
+ // Nothing ran, so nothing to undo.
+ let shim = env.cfg.shim_dir.clone().unwrap();
+ assert_eq!(aur_build::shims::called_with(&shim, "repo-add", ""), 0);
+ assert!(db::accepted_release(&env.db, "plain-make").unwrap().is_none());
+ // A cancelled build is not a failure and is not retried.
+ assert_eq!(env.query_i64("SELECT count(*) FROM build WHERE state='failed'", &[]), Some(0));
+}
+
+#[test]
+fn a_failed_build_leaves_the_index_serving_the_old_version_and_says_so() {
+ // §8.4: "v2 approved, build failed, index still serving v1". The dangerous
+ // failure mode is a package that looks healthy while the new version never
+ // arrived, so the state has to be visible, not inferable.
+ let (mut env, fx) = env_with_fixture("plain-make");
+ // Publish v1 for real.
+ let c = approved(&env, "plain-make");
+ let id = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap();
+ pipeline::run(&env.cfg, &env.db, &id).unwrap();
+ assert_eq!(db::index_version(&env.db, "plain-make").unwrap().as_deref(), Some("1.0-1"));
+
+ // v2 is approved and its build fails deterministically.
+ fx.bump(&[("pkgver", "2.0")]);
+ env.set_shim_fail("makechrootpkg");
+ let c2 = approved(&env, "plain-make");
+ let id2 = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c2).unwrap();
+ let out = pipeline::run(&env.cfg, &env.db, &id2).unwrap();
+ assert!(matches!(out, pipeline::Outcome::Failed { .. }), "{out:?}");
+
+ // The index still serves v1 — that is correct — and the system knows the
+ // approved v2 never landed.
+ assert_eq!(db::index_version(&env.db, "plain-make").unwrap().as_deref(), Some("1.0-1"));
+ let unbuilt = db::approved_but_unbuilt(&env.db).unwrap();
+ assert_eq!(unbuilt.len(), 1, "the failed approval must be surfaced: {unbuilt:?}");
+ assert_eq!(unbuilt[0].pkgbase, "plain-make");
+ assert_eq!(unbuilt[0].approved, "2.0-1");
+ assert_eq!(unbuilt[0].serving, "1.0-1");
+
+ // And it is on the surfaces a human actually looks at.
+ let web = Web::attach(env, fx);
+ let attention = web.get("/attention");
+ assert!(attention.body.contains("Approved but unbuilt"), "no Attention section: {}", &attention.body[..600.min(attention.body.len())]);
+ assert!(attention.body.contains("still serving 1.0-1"), "Attention does not say what is served");
+ let pkg = web.get("/packages/plain-make");
+ assert!(pkg.body.contains("build failed"), "the package page hides the failure");
+ assert!(pkg.body.contains("2.0-1"), "the package page does not name the version that did not land");
+}
+
+#[test]
+fn a_transient_failure_retries_but_a_deterministic_one_never_does() {
+ // The retry policy is asserted at the state machine level here; the dispatch
+ // tests cover it through the pool.
+ let (mut env, _fx) = env_with_fixture("build-fails");
+ let id = queued(&env, "build-fails");
+ let pool = aur_build::pipeline::dispatch::Pool::spawn(std::sync::Arc::new(env.cfg.clone()), env.db.clone());
+ let mut state = String::new();
+ for _ in 0..400 {
+ if let Some(b) = builds::get(&env.db, &id).unwrap() {
+ state = b.state.clone();
+ if state == build_state::FAILED {
+ break;
+ }
+ }
+ std::thread::sleep(std::time::Duration::from_millis(25));
+ }
+ pool.shutdown();
+ assert_eq!(state, build_state::FAILED);
+ assert_eq!(builds::get(&env.db, &id).unwrap().unwrap().attempt, 1, "a deterministic failure must not retry");
+ // The release stays approved_but_unbuilt rather than vanishing.
+ assert_eq!(db::approved_but_unbuilt(&env.db).unwrap().len(), 1);
+ let _ = &mut env;
+}