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
|
mod common;
use std::sync::Arc;
use aur_build::db::{self, builds, model::*};
use aur_build::error::{Error, Result};
use aur_build::pipeline::{self, Ctx};
use aur_build::sandbox::loopback::LoopbackBackend;
use aur_build::sandbox::{Exit, Sandbox, SandboxBackend, SandboxInfo, SandboxSpec};
use common::*;
#[derive(Clone, Copy, PartialEq, Eq)]
enum Fault {
DieMidRun,
TtlMidBuild,
NoCapacity,
EmptyWorld,
}
struct FakeBackend {
cfg: aur_build::config::Config,
fault: Fault,
}
impl SandboxBackend for FakeBackend {
fn acquire(&self, spec: &SandboxSpec) -> Result<Arc<dyn Sandbox>> {
if self.fault == Fault::NoCapacity {
return Err(Error::pool_exhausted("fake pool at quota"));
}
let inner = LoopbackBackend::new(&self.cfg).acquire(spec)?;
Ok(Arc::new(FakeSandbox { inner, fault: self.fault }))
}
fn status(&self) -> Result<Vec<SandboxInfo>> {
Ok(vec![SandboxInfo { id: "fake".into(), profile: "fake".into(), warm: true }])
}
}
struct FakeSandbox {
inner: std::sync::Arc<dyn Sandbox>,
fault: Fault,
}
impl FakeSandbox {
fn is_build(cmd: &aur_build::sandbox::Cmd) -> bool {
cmd.argv.first().is_some_and(|a| a.ends_with("makepkg"))
}
fn is_query(cmd: &aur_build::sandbox::Cmd) -> bool {
cmd.argv.get(1).is_some_and(|a| a == "-Q")
}
}
impl Sandbox for FakeSandbox {
fn id(&self) -> &str {
self.inner.id()
}
fn run(&self, cmd: &aur_build::sandbox::Cmd, on_log: &mut dyn FnMut(&[u8])) -> Result<Exit> {
match self.fault {
Fault::DieMidRun if Self::is_build(cmd) => {
Err(Error::other("sandbox vanished mid-run (connection reset)"))
}
Fault::TtlMidBuild if Self::is_build(cmd) => {
Err(Error::other("sandbox ttl expired during build"))
}
Fault::EmptyWorld if Self::is_query(cmd) => {
Ok(Exit { code: Some(0), stdout: Vec::new(), stderr: Vec::new() })
}
_ => self.inner.run(cmd, on_log),
}
}
fn put(&self, digest: &str, r: &mut dyn std::io::Read) -> Result<()> {
self.inner.put(digest, r)
}
fn get(&self, digest: &str) -> Result<Box<dyn std::io::Read>> {
self.inner.get(digest)
}
fn destroy(&self) -> Result<()> {
self.inner.destroy()
}
}
fn approve_and_enqueue(env: &Env, name: &str) -> String {
aur_build::watch::detect::check_pkgbase(&env.cfg, &env.db, name).unwrap();
let c = aur_build::review::candidates::pending_candidates(&env.db).unwrap().remove(0);
let dd = aur_build::review::diff::render_and_store(&env.db, &env.cfg, &c).unwrap();
aur_build::review::approve::approve(&env.db, "tester", c.id, &c.identity_hash, &dd, None).unwrap();
let c = aur_build::review::candidates::candidate_by_id(&env.db, c.id).unwrap().unwrap();
pipeline::enqueue_for_candidate(&env.cfg, &env.db, &c).unwrap()
}
fn run_with_fault(env: &Env, id: &str, fault: Fault) -> pipeline::Outcome {
let mut ctx = Ctx::new(&env.cfg, &env.db, id).unwrap();
ctx.backend = Arc::new(FakeBackend { cfg: env.cfg.clone(), fault });
pipeline::run_with(&ctx).unwrap()
}
#[test]
fn a_sandbox_that_dies_mid_run_expires_its_lease_and_the_requeue_finishes_the_build() {
let (env, _fx) = env_with_fixture("plain-make");
let id = approve_and_enqueue(&env, "plain-make");
match run_with_fault(&env, &id, Fault::DieMidRun) {
pipeline::Outcome::Failed { step, error, .. } => {
assert_eq!(step, "build");
assert!(error.contains("vanished"), "{error}");
}
other => panic!("{other:?}"),
}
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()]),
);
assert_eq!(builds::reap_expired(&env.db).unwrap(), 1);
assert!(matches!(
pipeline::run(&env.cfg, &env.db, &id).unwrap(),
pipeline::Outcome::Succeeded { .. }
));
assert_eq!(db::index_version(&env.db, "plain-make").unwrap().as_deref(), Some("1.0-1"));
}
#[test]
fn a_ttl_that_bites_mid_build_is_a_failed_step_not_a_lost_build() {
let (env, _fx) = env_with_fixture("plain-make");
let id = approve_and_enqueue(&env, "plain-make");
match run_with_fault(&env, &id, Fault::TtlMidBuild) {
pipeline::Outcome::Failed { step, error, transient } => {
assert_eq!(step, "build");
assert!(error.contains("ttl expired"), "{error}");
assert!(!transient, "a sandbox TTL is deterministic; do not retry-burn it");
}
other => panic!("{other:?}"),
}
assert!(db::accepted_release(&env.db, "plain-make").unwrap().is_none());
}
#[test]
fn a_full_pool_blocks_the_build_instead_of_breaking_it() {
let (env, _fx) = env_with_fixture("plain-make");
let id = approve_and_enqueue(&env, "plain-make");
match run_with_fault(&env, &id, Fault::NoCapacity) {
pipeline::Outcome::Blocked { on } => {
assert_eq!(on, vec!["sandbox-pool".to_string()]);
}
other => panic!("a quota is a wait, not a failure: {other:?}"),
}
let b = builds::get(&env.db, &id).unwrap().unwrap();
assert_eq!(
b.state,
BuildState::BlockedMissingDep,
"the build waits for capacity on the same blocked machinery it uses for missing deps, unharmed"
);
assert_eq!(
b.blocked_on_json.as_str(),
r#"["sandbox-pool"]"#,
"the block names what it waits on"
);
}
#[test]
fn an_incomplete_closure_fails_closed_as_a_world_mismatch() {
let (env, _plain, _client) = env_with_two_fixtures("plain-make", "dep-client");
let id = approve_and_enqueue(&env, "plain-make");
assert!(matches!(pipeline::run(&env.cfg, &env.db, &id).unwrap(), pipeline::Outcome::Succeeded { .. }));
let id2 = approve_and_enqueue(&env, "dep-client");
match run_with_fault(&env, &id2, Fault::EmptyWorld) {
pipeline::Outcome::Failed { step, error, transient } => {
assert_eq!(step, "chroot", "provisioning is where a lying world gets caught");
assert!(error.contains("world is missing plain-make"), "{error}");
assert!(!transient, "a mismatched world is fail-closed, never retried");
}
other => panic!("{other:?}"),
}
assert!(db::accepted_release(&env.db, "dep-client").unwrap().is_none());
}
|