1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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 makepkg.
env.cfg.hooks.shim_kill_parent = Some("makepkg".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 == BuildState::Leased || b.state == BuildState::Running,
"a killed build must still look claimed, not finished: {}",
b.state
);
assert_eq!(
builds::step_status(&env.db, &id, "build").unwrap(),
Some(StepState::Running),
"the step that was running should be recorded as running"
);
let chroot_status = builds::step_status(&env.db, &id, "chroot").unwrap();
assert!(
chroot_status.is_some_and(|s| s.is_done()),
"the completed step before the crash must be checkpointed: {chroot_status:?}"
);
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", ¶ms(vec![id.as_str()]));
assert_eq!(builds::reap_expired(&env.db).unwrap(), 1);
assert_eq!(
builds::step_status(&env.db, &id, "build").unwrap(),
Some(StepState::Interrupted)
);
// Resume. The completed steps are not repeated; only the interrupted one and
// what follows run.
env.cfg.hooks.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.hooks.shim_dir.clone().unwrap();
assert_eq!(
aur_build::shims::called_with(&shim, "mkarchroot", ""),
0,
"the provisioning model never calls devtools, before or after a crash"
);
assert_eq!(
aur_build::shims::called_with(&shim, "makepkg", ""),
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, BuildState::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, BuildState::Cancelled);
// Nothing ran, so nothing to undo.
let shim = env.cfg.hooks.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("makepkg");
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 = BuildState::Queued;
for _ in 0..400 {
if let Some(b) = builds::get(&env.db, &id).unwrap() {
state = b.state;
if state == BuildState::Failed {
break;
}
}
std::thread::sleep(std::time::Duration::from_millis(25));
}
pool.shutdown();
assert_eq!(state, BuildState::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;
}
|