summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authoraur-build <aur-build@localhost>2026-11-18 16:49:40 +0000
committeraur-build <aur-build@localhost>2026-11-18 16:49:40 +0000
commit41fa1684de8eb13c5b76c3a3e174fece04ac5afa (patch)
tree491a2faf2dd2314f537300ccaf1e0150b54d1123 /tests
parent101f077200e80c7869e4549db645ed94b01acb1b (diff)
downloadaur-build-41fa1684de8eb13c5b76c3a3e174fece04ac5afa.tar.gz
aur-build-41fa1684de8eb13c5b76c3a3e174fece04ac5afa.zip
db: the review, step, release and subscription states are enums too
M-8 named four more machines after `build_state`, and each had the same defect in miniature: `&str` constants, a writer that took any `&str`, and a rule that lived only in the caller's discipline. They are `CandidateState`, `StepState`, `ReleaseState` and `SubscriptionState` now, all parsed through one `db::state_col` at the row boundary — a column value the code cannot name fails the read rather than becoming a state it did not ask for. The one that mattered is the review machine. `resolve_in(tx, id, &str)` would move a candidate to any state from any state, so an approval racing a rejection, or a stale worker resolving a review that had already been superseded, silently overwrote what a human had decided — the direct route to breaking I2 and I3 with no symptom anywhere. It is a compare-and-swap on `CandidateState` now: a decided review is only ever frozen, never re-decided, and a refusal names both states. `supersede_in` goes through it, so the freeze cannot resurrect a superseded review either. `StepState` gives I5 teeth. `begin_step` upserted over any existing checkpoint, including one recorded `succeeded`, so a resume that asked to re-run a finished step could rewrite the very row that told it to skip; the upsert now refuses to touch a done checkpoint and `finish_step` is a CAS. The blocked path (`running → pending`) and the reaper's `running → interrupted` are unchanged, and both are in the table. Two behaviour changes, both for the better and both tested: the UI's pause button now says "orphaned: nothing to pause" instead of rewriting an orphaned subscription back to a pausable state, and a step checkpoint that is already done makes `begin_step` an error rather than a quiet overwrite. Five tests in `tests/state_machine.rs` cover the review CAS in both directions, the frozen checkpoint, the interrupted/blocked transitions, and the unknown value in a candidate and a step row.
Diffstat (limited to 'tests')
-rw-r--r--tests/approve.rs4
-rw-r--r--tests/crash.rs12
-rw-r--r--tests/detect.rs8
-rw-r--r--tests/dispatch.rs8
-rw-r--r--tests/invariants.rs2
-rw-r--r--tests/pipeline.rs4
-rw-r--r--tests/state_machine.rs117
7 files changed, 136 insertions, 19 deletions
diff --git a/tests/approve.rs b/tests/approve.rs
index 61e42c2..1f61182 100644
--- a/tests/approve.rs
+++ b/tests/approve.rs
@@ -125,7 +125,7 @@ fn a_broken_database_is_not_reported_as_a_stale_review() {
// And the refusal is atomic: the half-written approval rolled back with it.
assert_eq!(env.query_i64("SELECT count(*) FROM approval", &[]), Some(0));
let after = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap();
- assert_eq!(after.state, "pending_review", "a failed approval must not resolve the review");
+ assert_eq!(after.state, CandidateState::PendingReview, "a failed approval must not resolve the review");
}
#[test]
@@ -208,7 +208,7 @@ fn once_viewed_a_new_state_freezes_the_old_one_as_superseded() {
assert_eq!(open.len(), 1);
assert_ne!(open[0].id, c.id, "a fresh review was opened");
let frozen = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap();
- assert_eq!(frozen.state, candidate_state::SUPERSEDED);
+ assert_eq!(frozen.state, CandidateState::Superseded);
// The frozen one can no longer be approved: approving it would build a
// version nobody is looking at.
let err = approve(&env.db, "tester", c.id, &frozen.identity_hash, &frozen.diff_digest, None).unwrap_err();
diff --git a/tests/crash.rs b/tests/crash.rs
index 0510aab..66e6043 100644
--- a/tests/crash.rs
+++ b/tests/crash.rs
@@ -67,13 +67,13 @@ fn a_process_killed_mid_step_resumes_from_its_checkpoints_with_no_duplicate_work
b.state
);
assert_eq!(
- builds::step_status(&env.db, &id, "build").unwrap().as_deref(),
- Some(step_state::RUNNING),
+ builds::step_status(&env.db, &id, "build").unwrap(),
+ Some(StepState::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),
+ builds::step_status(&env.db, &id, "chroot").unwrap(),
+ Some(StepState::Succeeded),
"the completed step before the crash must be checkpointed"
);
assert!(db::accepted_release(&env.db, "plain-make").unwrap().is_none(), "nothing was published");
@@ -83,8 +83,8 @@ fn a_process_killed_mid_step_resumes_from_its_checkpoints_with_no_duplicate_work
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)
+ builds::step_status(&env.db, &id, "build").unwrap(),
+ Some(StepState::Interrupted)
);
// Resume. The completed steps are not repeated; only the interrupted one and
diff --git a/tests/detect.rs b/tests/detect.rs
index 939dce1..e674c8a 100644
--- a/tests/detect.rs
+++ b/tests/detect.rs
@@ -230,7 +230,11 @@ fn a_viewed_review_is_frozen_and_a_fresh_one_opened() {
)?)
})
.unwrap();
- assert_eq!(old_state, candidate_state::SUPERSEDED);
+ assert_eq!(
+ CandidateState::parse(&old_state),
+ Some(CandidateState::Superseded),
+ "the frozen review must be superseded in the column, not only in memory"
+ );
}
#[test]
@@ -326,7 +330,7 @@ fn a_deleted_upstream_marks_the_subscription_and_alerts() {
let rep = check_pkgbase(&env.cfg, &env.db, "foo").unwrap();
assert!(matches!(rep.outcome, Outcome::Gone), "{:?}", rep.outcome);
let sub = db::get_subscription(&env.db, "foo").unwrap().unwrap();
- assert_eq!(sub.state, subscription_state::ORPHANED);
+ assert_eq!(sub.state, SubscriptionState::Orphaned);
assert!(db::list_notices(&env.db, false)
.unwrap()
.iter()
diff --git a/tests/dispatch.rs b/tests/dispatch.rs
index 6721fc8..d0d4bd7 100644
--- a/tests/dispatch.rs
+++ b/tests/dispatch.rs
@@ -95,7 +95,7 @@ fn an_expired_lease_is_requeued_and_its_running_step_marked_interrupted() {
let id = queued_build(&env, "plain-make");
assert_eq!(pass(&env), vec![id.clone()]);
builds::begin_step(&env.db, &id, "build", 1).unwrap();
- assert_eq!(builds::step_status(&env.db, &id, "build").unwrap().as_deref(), Some("running"));
+ assert_eq!(builds::step_status(&env.db, &id, "build").unwrap(), Some(StepState::Running));
// Time passes — the lease expires, as it would if the process were killed.
env.exec("UPDATE build SET lease_expires_at='2000-01-01T00:00:00Z'", &[]);
@@ -105,8 +105,8 @@ fn an_expired_lease_is_requeued_and_its_running_step_marked_interrupted() {
assert_eq!(reaped, 1, "the expired lease was not requeued");
assert_eq!(builds::get(&env.db, &id).unwrap().unwrap().state, BuildState::Queued);
assert_eq!(
- builds::step_status(&env.db, &id, "build").unwrap().as_deref(),
- Some("interrupted"),
+ builds::step_status(&env.db, &id, "build").unwrap(),
+ Some(StepState::Interrupted),
"the in-flight step was not marked interrupted"
);
// And it is claimable again.
@@ -230,7 +230,7 @@ fn a_reaped_build_resumes_at_the_next_incomplete_step_with_no_duplicate_work() {
let steps = builds::steps(&env.db, &id).unwrap();
let done = steps
.iter()
- .filter(|s| s.status == step_state::SUCCEEDED || s.status == step_state::SKIPPED)
+ .filter(|s| s.status == StepState::Succeeded || s.status == StepState::Skipped)
.count();
assert_eq!(done, BUILD_STEPS.len(), "not every step ended succeeded or skipped: {steps:?}");
assert_eq!(
diff --git a/tests/invariants.rs b/tests/invariants.rs
index ceaddc4..b7bfc0d 100644
--- a/tests/invariants.rs
+++ b/tests/invariants.rs
@@ -214,7 +214,7 @@ fn i7_the_schema_refuses_a_second_open_review() {
upstream_pins_json, source_digests_json, identity_hash,
diff_digest, state, detected_at)
VALUES (?1,'new_version',?2,'0','9','1','[]','[]','other-hash','',?3,'now')",
- rusqlite::params![pb, snap, candidate_state::PENDING],
+ rusqlite::params![pb, snap, CandidateState::PendingReview.as_str()],
)?;
Ok(())
})
diff --git a/tests/pipeline.rs b/tests/pipeline.rs
index 01ae5f7..a8eb1d9 100644
--- a/tests/pipeline.rs
+++ b/tests/pipeline.rs
@@ -64,7 +64,7 @@ fn end_to_end_publishes_a_signed_index() {
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.state, ReleaseState::Published);
assert_eq!(rel.pkgver, "1.0");
assert_eq!(rel.version(), "1.0-1");
@@ -278,7 +278,7 @@ fn a_failed_build_leaves_the_previous_version_serving() {
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
+ CandidateState::Approved
);
assert_eq!(
env.query_i64("SELECT count(*) FROM package WHERE state='in_index' AND version='1.0.1-1'", &[]),
diff --git a/tests/state_machine.rs b/tests/state_machine.rs
index c5203d7..1c05fff 100644
--- a/tests/state_machine.rs
+++ b/tests/state_machine.rs
@@ -6,12 +6,12 @@
mod common;
-use aur_build::db::{builds, model::BuildState};
+use aur_build::db::{builds, model::{BuildState, CandidateState, StepState}};
use common::Env;
fn build(env: &Env, id: &str) -> String {
env.exec(
- "INSERT INTO pkgbase (name, aur_url, created_at) VALUES ('x', 'u', '2026-01-01T00:00:00Z')",
+ "INSERT OR IGNORE INTO pkgbase (name, aur_url, created_at) VALUES ('x', 'u', '2026-01-01T00:00:00Z')",
&[],
);
let pb = env.query_i64("SELECT id FROM pkgbase WHERE name='x'", &[]).expect("pkgbase id");
@@ -168,3 +168,116 @@ fn a_failed_build_is_resumed_on_its_own_row() {
assert_eq!(state(&env, &id), BuildState::Succeeded);
}
+
+// ---------------------------------------------------------- the other machines --
+
+/// A candidate row in `state`, inserted directly: these tests are about the
+/// state column, not about how a candidate is discovered.
+fn candidate(env: &Env, state: &str) -> i64 {
+ env.exec(
+ "INSERT OR IGNORE INTO pkgbase (name, aur_url, created_at) VALUES ('x', 'u', '2026-01-01T00:00:00Z')",
+ &[],
+ );
+ let pb = env.query_i64("SELECT id FROM pkgbase WHERE name='x'", &[]).unwrap();
+ let snap = aur_build::db::put_snapshot(&env.db.writer, pb, "commit", "rh", "{}", "[]").unwrap();
+ env.exec(
+ "INSERT INTO candidate (pkgbase_id, kind, snapshot_id, pkgver, pkgrel, identity_hash, state, detected_at)
+ VALUES (?1,'new_version',?2,'1.0','1','ih-1',?3,'2026-01-01T00:00:00Z')",
+ &[common::P::I(pb), common::P::I(snap), common::P::S(state.into())],
+ );
+ env.query_i64("SELECT id FROM candidate WHERE identity_hash='ih-1'", &[]).unwrap()
+}
+
+fn resolve(env: &Env, id: i64, to: CandidateState) -> aur_build::error::Result<()> {
+ env.db
+ .writer
+ .tx_for_tests(move |tx| aur_build::db::candidates::resolve_in(tx, id, to, "2026-01-01T00:00:00Z"))
+}
+
+fn candidate_state(env: &Env, id: i64) -> CandidateState {
+ aur_build::db::candidates::by_id(&env.db, id)
+ .unwrap()
+ .expect("candidate")
+ .state
+}
+
+#[test]
+fn a_decided_review_is_never_re_decided() {
+ // The one write in this system that must never overwrite a human's decision.
+ let env = Env::new();
+ let id = candidate(&env, "approved");
+ let err = resolve(&env, id, CandidateState::Rejected).unwrap_err();
+ assert!(err.to_string().contains("illegal candidate transition approved"), "{err}");
+ assert_eq!(candidate_state(&env, id), CandidateState::Approved);
+
+ // Superseding a decided review is the one move it makes (§7.1), and a
+ // superseded review is history.
+ resolve(&env, id, CandidateState::Superseded).unwrap();
+ assert_eq!(candidate_state(&env, id), CandidateState::Superseded);
+ assert!(resolve(&env, id, CandidateState::Approved).is_err());
+}
+
+#[test]
+fn only_an_open_review_can_be_approved() {
+ let env = Env::new();
+ let id = candidate(&env, "rejected");
+ assert!(resolve(&env, id, CandidateState::Approved).is_err(), "a rejection was re-decided");
+ assert_eq!(candidate_state(&env, id), CandidateState::Rejected);
+ // Re-writing the state it already has is a no-op, not a re-decision.
+ resolve(&env, id, CandidateState::Rejected).unwrap();
+}
+
+#[test]
+fn a_completed_step_checkpoint_is_frozen() {
+ // I5: a resumed build reads these rows to decide what not to redo. A
+ // checkpoint that can be rewritten is not a checkpoint.
+ let env = Env::new();
+ let id = build(&env, "b1");
+ builds::begin_step(&env.db, &id, "build", 1).unwrap();
+ builds::finish_step(&env.db, &id, "build", StepState::Succeeded, None, None).unwrap();
+
+ let err = builds::begin_step(&env.db, &id, "build", 2).unwrap_err();
+ assert!(err.to_string().contains("checkpointed as done"), "{err}");
+ assert!(builds::finish_step(&env.db, &id, "build", StepState::Failed, None, Some("x")).is_err());
+ assert_eq!(builds::step_status(&env.db, &id, "build").unwrap(), Some(StepState::Succeeded));
+}
+
+#[test]
+fn an_interrupted_step_resumes_and_a_blocked_step_waits() {
+ let env = Env::new();
+ let id = build(&env, "b1");
+ builds::begin_step(&env.db, &id, "build", 1).unwrap();
+ // The reaper's write, then the resume's re-begin.
+ env.exec(
+ "UPDATE build_step SET status='interrupted' WHERE build_id=?1 AND step='build'",
+ &[common::P::S(id.clone())],
+ );
+ assert_eq!(builds::step_status(&env.db, &id, "build").unwrap(), Some(StepState::Interrupted));
+ builds::begin_step(&env.db, &id, "build", 2).unwrap();
+ // The blocked path puts the checkpoint back to waiting rather than failing it.
+ builds::finish_step(&env.db, &id, "build", StepState::Pending, None, Some("blocked")).unwrap();
+ assert_eq!(builds::step_status(&env.db, &id, "build").unwrap(), Some(StepState::Pending));
+}
+
+#[test]
+fn an_unnamed_state_in_any_row_is_an_error() {
+ // Every state column is parsed through `db::state_col`, so a value the code
+ // does not know stops the read instead of rendering as some other state.
+ let env = Env::new();
+ let cid = candidate(&env, "approved");
+ env.exec(
+ "UPDATE candidate SET state='reconsidered' WHERE id=?1",
+ &[common::P::I(cid)],
+ );
+ let err = aur_build::db::candidates::by_id(&env.db, cid).unwrap_err();
+ assert!(err.to_string().contains("candidate has a state outside"), "{err}");
+
+ let id = build(&env, "b1");
+ builds::begin_step(&env.db, &id, "build", 1).unwrap();
+ env.exec(
+ "UPDATE build_step SET status='maybe' WHERE build_id=?1 AND step='build'",
+ &[common::P::S(id.clone())],
+ );
+ let err = builds::step_status(&env.db, &id, "build").unwrap_err();
+ assert!(err.to_string().contains("build step has a state outside"), "{err}");
+}