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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
mod common;
use std::sync::Arc;
use aur_build::db::{self, builds, model::*};
use aur_build::pipeline::{self, dispatch};
use aur_build::review::{candidates, diff};
use common::*;
fn queued_build(env: &Env, name: &str) -> String {
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();
let c = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap();
pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap()
}
fn pass(env: &Env) -> Vec<String> {
let (tx, _rx) = crossbeam_channel::bounded::<String>(8);
dispatch::dispatch_once(&env.cfg, &env.db, &tx)
}
#[test]
fn the_deterministic_build_id_makes_re_enqueue_a_noop() {
let (env, _fx) = env_with_fixture("plain-make");
aur_build::watch::detect::check_pkgbase(&env.cfg, &env.db, "plain-make").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();
let c = candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap();
let first = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap();
let again = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap();
assert_eq!(first, again, "the same intent produced two build ids");
assert_eq!(env.query_i64("SELECT count(*) FROM build", &[]), Some(1));
}
#[test]
fn backpressure_admits_no_more_than_max_concurrent_builds() {
let (mut env, _plain, _client) = env_with_two_fixtures("plain-make", "dep-client");
env.cfg.max_concurrent_builds = 1;
let a = queued_build(&env, "plain-make");
let b = queued_build(&env, "dep-client");
assert_ne!(a, b);
let claimed = pass(&env);
assert_eq!(claimed.len(), 1, "one slot must admit exactly one build");
assert_eq!(pass(&env).len(), 0, "admitted a second build while one is in flight");
assert_eq!(env.query_i64("SELECT count(*) FROM build WHERE state='leased'", &[]), Some(1));
assert_eq!(env.query_i64("SELECT count(*) FROM build WHERE state='queued'", &[]), Some(1));
env.cfg.max_concurrent_builds = 2;
assert_eq!(pass(&env).len(), 1);
}
#[test]
fn disk_headroom_is_part_of_backpressure() {
let (mut env, _fx) = env_with_fixture("plain-make");
queued_build(&env, "plain-make");
env.cfg.min_free_disk_mb = u64::MAX / 2;
assert_eq!(dispatch::admissible(&env.cfg, &env.db), 0);
assert!(pass(&env).is_empty(), "claimed a build with no disk headroom");
assert_eq!(env.query_i64("SELECT count(*) FROM build WHERE state='queued'", &[]), Some(1));
env.cfg.min_free_disk_mb = 1;
assert!(dispatch::free_disk_mb(&env.cfg.root) > 0, "free_disk_mb never measures anything");
assert_eq!(dispatch::admissible(&env.cfg, &env.db), 1);
}
#[test]
fn an_expired_lease_is_requeued_and_its_running_step_marked_interrupted() {
let (env, _fx) = env_with_fixture("plain-make");
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(), Some(StepState::Running));
env.exec("UPDATE build SET lease_expires_at='2000-01-01T00:00:00Z'", &[]);
let reaped = builds::reap_expired(&env.db).unwrap();
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(),
Some(StepState::Interrupted),
"the in-flight step was not marked interrupted"
);
assert_eq!(pass(&env), vec![id.clone()]);
assert_eq!(builds::get(&env.db, &id).unwrap().unwrap().attempt, 2, "the retry was not counted");
}
#[test]
fn a_newer_approval_cancels_an_in_flight_build_at_the_step_boundary() {
let (env, _fx) = env_with_fixture("plain-make");
let id = queued_build(&env, "plain-make");
pass(&env);
assert!(!builds::cancel_requested(&env.db, &id).unwrap());
env.exec("UPDATE candidate SET state='superseded' WHERE id=(SELECT candidate_id FROM build WHERE id=?1)", ¶ms(vec![id.as_str()]));
assert_eq!(builds::cancel_superseded(&env.db).unwrap(), 1);
assert!(builds::cancel_requested(&env.db, &id).unwrap());
let out = pipeline::run(&env.cfg, &env.db, &id).unwrap();
assert!(matches!(out, pipeline::Outcome::Cancelled), "run ignored the cancellation");
assert_eq!(builds::get(&env.db, &id).unwrap().unwrap().state, BuildState::Cancelled);
}
#[test]
fn a_deterministic_build_failure_is_not_retried() {
let (env, _fx) = env_with_fixture("build-fails");
let id = queued_build(&env, "build-fails");
let _clock = aur_build::time::virtualize();
let pool = dispatch::Pool::spawn(Arc::new(env.cfg.clone()), env.db.clone());
let state = wait_for(&env, &id, &[BuildState::Failed, BuildState::Succeeded]);
pool.shutdown();
assert_eq!(state, BuildState::Failed, "a deterministic failure should not succeed");
let b = builds::get(&env.db, &id).unwrap().unwrap();
assert_eq!(b.attempt, 1, "a deterministic failure must not be retried");
assert!(b.error.unwrap_or_default().contains("build"), "no step named in the error");
assert_eq!(db::index_version(&env.db, "build-fails").unwrap(), None);
}
#[test]
fn a_transient_failure_is_retried_up_to_the_cap() {
let (mut env, fx) = env_with_fixture("skip-checksum");
let id = queued_build(&env, "skip-checksum");
std::fs::remove_file(fx.data.join("payload.bin")).expect("payload exists");
env.exec("DELETE FROM source_cache", &[]);
env.cfg.max_transient_attempts = 3;
env.cfg.dispatch_tick_secs = 0;
let pool = dispatch::Pool::spawn(Arc::new(env.cfg.clone()), env.db.clone());
let b = wait_attempt(&env, &id, 3);
pool.shutdown();
assert_eq!(b.state, BuildState::Failed);
assert_eq!(b.attempt, 3, "an unreachable source should retry up to the cap");
assert!(
b.error.unwrap_or_default().contains("payload"),
"the recorded failure should name what could not be fetched"
);
}
#[test]
fn a_missing_toolchain_is_not_retried() {
let (mut env, _fx) = env_with_fixture("plain-make");
let id = queued_build(&env, "plain-make");
env.cfg.makepkg_bin = "/nonexistent/makepkg-for-test".into();
env.cfg.max_transient_attempts = 3;
env.cfg.dispatch_tick_secs = 0;
let _clock = aur_build::time::virtualize();
let pool = dispatch::Pool::spawn(Arc::new(env.cfg.clone()), env.db.clone());
let state = wait_for(&env, &id, &[BuildState::Failed]);
pool.shutdown();
assert_eq!(state, BuildState::Failed);
assert_eq!(
builds::get(&env.db, &id).unwrap().unwrap().attempt,
1,
"a missing binary must not be retried as if the network had hiccupped"
);
}
#[test]
fn the_pool_takes_a_queued_build_all_the_way_to_publication() {
let (env, _fx) = env_with_fixture("plain-make");
let id = queued_build(&env, "plain-make");
let _clock = aur_build::time::virtualize();
let cfg = Arc::new(env.cfg.clone());
let pool = dispatch::Pool::spawn(cfg, env.db.clone());
let state = wait_for(&env, &id, &[BuildState::Succeeded, BuildState::Failed]);
pool.shutdown();
assert_eq!(state, BuildState::Succeeded, "the pool did not finish the build");
assert!(db::accepted_release(&env.db, "plain-make").unwrap().is_some(), "nothing was published");
assert_eq!(db::index_version(&env.db, "plain-make").unwrap().as_deref(), Some("1.0-1"));
}
#[test]
fn a_reaped_build_resumes_at_the_next_incomplete_step_with_no_duplicate_work() {
let (mut env, fx) = env_with_fixture("plain-make");
let c = queued_candidate(&env, "plain-make");
let first = pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap();
pipeline::run(&env.cfg, &env.db, &first).unwrap();
fx.bump(&[("pkgver", "1.0.1")]);
let c2 = queued_candidate(&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_before = aur_build::shims::called_with(&env.cfg.hooks.shim_dir.clone().unwrap(), "makepkg", "");
env.clear_publish_fault();
env.exec(
"UPDATE build SET state='leased', lease_owner='dead-worker',
lease_expires_at='2000-01-01T00:00:00Z' WHERE id=?1",
¶ms(vec![id.as_str()]),
);
let _clock = aur_build::time::virtualize();
let pool = dispatch::Pool::spawn(Arc::new(env.cfg.clone()), env.db.clone());
let state = wait_for(&env, &id, &[BuildState::Succeeded, BuildState::Failed]);
pool.shutdown();
assert_eq!(state, BuildState::Succeeded, "the resumed build did not finish");
assert_eq!(
aur_build::shims::called_with(&env.cfg.hooks.shim_dir.clone().unwrap(), "makepkg", ""),
builds_before,
"resuming must not rebuild"
);
let steps = builds::steps(&env.db, &id).unwrap();
let done = steps
.iter()
.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!(
env.query_i64("SELECT count(*) FROM release WHERE pkgver='1.0.1'", &[]),
Some(1),
"the resume published twice"
);
assert_eq!(db::index_version(&env.db, "plain-make").unwrap().as_deref(), Some("1.0.1-1"));
}
fn queued_candidate(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 wait_attempt(env: &Env, id: &str, n: i64) -> builds::BuildRow {
for _ in 0..15_000 {
if let Some(b) = builds::get(&env.db, id).unwrap() {
if b.attempt >= n && b.state == BuildState::Failed {
return b;
}
}
std::thread::sleep(std::time::Duration::from_millis(2));
}
panic!("build {id} never reached {n} attempts in state failed");
}
fn wait_for(env: &Env, id: &str, want: &[BuildState]) -> BuildState {
for _ in 0..15_000 {
if let Some(b) = builds::get(&env.db, id).unwrap() {
if want.contains(&b.state) {
return b.state;
}
}
std::thread::sleep(std::time::Duration::from_millis(2));
}
panic!("build {id} never reached {want:?}");
}
|