42fa9554 Andy McCall 1 month ago ↶
1
2
3
4
5
use axum:: body:: Body;
6
use axum:: http:: { header, Request, StatusCode} ;
7
use http_body_util:: BodyExt;
8
use oxy_core:: protocol:: { open_remote, RemoteOptions} ;
9
use oxy_core:: repo:: Repo;
10
use oxygenhub:: config:: Config;
11
use oxygenhub:: repos:: RepoName;
12
use oxygenhub:: { db, Shared} ;
13
use std:: path:: Path;
14
use tower:: ServiceExt;
15
16
const ROOTFS : & str = " 36fc4e3238044e424219a2b9002bf61bbe1805fec14d52cf37e17fae77c9ff16" ;
17
18
fn hermetic ( ) {
19
static ONCE : std:: sync:: Once = std:: sync:: Once:: new( ) ;
20
static DIR : std:: sync:: OnceLock< tempfile:: TempDir> = std:: sync:: OnceLock:: new( ) ;
21
ONCE .call_once ( | | {
22
let d = tempfile:: tempdir( ) .unwrap ( ) ;
23
std:: env:: set_var( " OXY_HOME" , d.path ( ) ) ;
24
DIR .set ( d) .ok ( ) ;
25
} ) ;
26
}
27
28
fn write ( root : & Path, rel : & str , content : & str ) {
29
let p = root.join ( rel) ;
30
std:: fs:: create_dir_all( p.parent ( ) .unwrap ( ) ) .unwrap ( ) ;
31
std:: fs:: write( p, content) .unwrap ( ) ;
32
}
33
34
struct Hub {
35
app : Shared,
36
base : String,
37
_dir : tempfile:: TempDir,
38
}
39
40
async fn hub ( ) -> Hub {
41
hermetic ( ) ;
42
let dir = tempfile:: tempdir( ) .unwrap ( ) ;
43
let mut config = Config:: load( dir.path ( ) ) .unwrap ( ) ;
44
config.auth.push_tokens = vec! [ " site" .into ( ) ] ;
4d2e44d7 Andy McCall 1 month ago ↶
45
config.flow.runner_tokens = vec! [ " runner-secret" .into ( ) , " runner-two" .into ( ) , " runner-three" .into ( ) ] ;
42fa9554 Andy McCall 1 month ago ↶
46
config.flow.repos = vec! [ " andy/*" .into ( ) ] ;
10708ab8 Andy McCall 1 month ago ↶
47
config.registration.mode = " open" .into ( ) ;
42fa9554 Andy McCall 1 month ago ↶
48
let listener = tokio:: net:: TcpListener:: bind( " 127.0.0.1:0" ) .await.unwrap ( ) ;
49
let port = listener.local_addr ( ) .unwrap ( ) .port ( ) ;
50
config.server.base_url = format! ( " http://127.0.0.1:{port} " ) ;
51
let app = oxygenhub:: build( config) .await.unwrap ( ) ;
52
let router = oxygenhub:: router( app.clone ( ) ) ;
53
tokio:: spawn( async move { axum:: serve( listener, router) .await.unwrap ( ) } ) ;
54
Hub { app, base: format! ( " http://127.0.0.1:{port} " ) , _dir: dir }
55
}
56
57
async fn call ( app : & Shared, method : & str , path : & str , bearer : Option< & str > , json : Option< String> ) -> ( StatusCode, String) {
58
let mut req = Request:: builder( ) .method ( method) .uri ( path) ;
59
if let Some ( b) = bearer {
60
req = req.header ( header:: AUTHORIZATION , format! ( " Bearer {b} " ) ) ;
61
}
62
let body = match json {
63
Some ( j) => {
64
req = req.header ( header:: CONTENT_TYPE , " application/json" ) ;
65
Body:: from( j)
66
}
67
None => Body:: empty( ) ,
68
} ;
69
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req.body ( body) .unwrap ( ) ) .await.unwrap ( ) ;
70
let status = res.status ( ) ;
71
let bytes = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
72
( status, String :: from_utf8_lossy( & bytes) .into_owned ( ) )
73
}
74
736f6813 Andy McCall 1 month ago ↶
75
76
async fn head ( app : & Shared, path : & str ) -> ( StatusCode, String, String) {
77
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( Request:: get( path) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
78
let h = | k : header:: HeaderName| res.headers ( ) .get ( k) .and_then ( | v | v.to_str ( ) .ok ( ) ) .unwrap_or ( " " ) .to_string ( ) ;
79
( res.status ( ) , h ( header:: LOCATION ) , h ( header:: CONTENT_TYPE ) )
80
}
81
42fa9554 Andy McCall 1 month ago ↶
82
async fn claim ( app : & Shared) -> Option< serde_json:: Value> {
83
let ( st, body) =
84
call ( app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( r # "{"runner":"test-1","runtimes":["microvm"]}"# .into ( ) ) ) .await;
85
match st {
86
StatusCode:: NO_CONTENT => None ,
87
StatusCode:: OK => Some ( serde_json:: from_str( & body) .unwrap ( ) ) ,
88
other => panic! ( " claim: {other} {body}" ) ,
89
}
90
}
91
92
fn receipt ( job : & str , pipeline_hash : & str , exit : u32 ) -> serde_json:: Value {
93
serde_json:: json! ( {
94
" job_name" : job, " pipeline_hash" : pipeline_hash, " scope_tree_hash" : " 11" .repeat ( 32 ) ,
95
" inputs" : [ ] , " environment" : " 22" .repeat ( 32 ) , " outputs" : [ ] , " exit_code" : exit, " duration_ms" : 640
96
} )
97
}
98
99
async fn finish ( app : & Shared, id : i64 , body : serde_json:: Value) -> StatusCode {
100
call ( app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /finish" ) , Some ( " runner-secret" ) , Some ( body.to_string ( ) ) ) .await.0
101
}
102
103
async fn create ( app : & Shared, owner : & str , name : & str ) {
104
app.repos.create ( & RepoName:: parse( & format! ( " {owner} /{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
105
db:: repos:: create( & app.db, owner, name, " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
106
}
107
108
fn client ( url : & str , work : & Path) -> Repo {
109
let mut c = Repo:: init( work) .unwrap ( ) ;
110
c.config.set ( " remote.origin.url" , url) .unwrap ( ) ;
111
c.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
112
c.save_config ( ) .unwrap ( ) ;
113
c
114
}
115
116
fn pipeline ( extra : & str ) -> String {
117
format! (
118
" schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n job \" test\" {{ \n sandbox \" alpine\" \n depends-on \" build\" \n exec \" sh\" \" -eu\" \" test.sh\" \n }} \n job \" lint\" {{ \n sandbox \" alpine\" \n exec \" true\" \n }} \n {extra} "
119
)
120
}
121
122
async fn runs_of ( app : & Shared, owner : & str , name : & str ) -> Vec< db:: flow:: Run> {
123
let row = db:: repos:: get( & app.db, owner, name) .await.unwrap ( ) .unwrap ( ) ;
124
for _ in 0 .. 100 {
125
let r = db:: flow:: runs( & app.db, row.id, 10 , None ) .await.unwrap ( ) ;
126
if ! r.is_empty ( ) {
127
return r;
128
}
129
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
130
}
131
vec! [ ]
132
}
133
90326c96 Andy McCall 1 month ago ↶
134
135
async fn checks_when ( app : & Shared, repo_id : i64 , state : & str , n : usize ) -> Vec< db:: hooks:: Check> {
136
for _ in 0 .. 100 {
137
let c = db:: hooks:: checks_for_state( & app.db, repo_id, state) .await.unwrap ( ) ;
138
if c.len ( ) > = n {
139
return c;
140
}
141
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
142
}
143
db:: hooks:: checks_for_state( & app.db, repo_id, state) .await.unwrap ( )
144
}
145
42fa9554 Andy McCall 1 month ago ↶
146
# [ tokio ::test ( flavor = " multi_thread" ) ]
147
async fn a_push_runs_the_pipeline_through_a_runner ( ) {
148
let h = hub ( ) .await;
149
create ( & h.app, " andy" , " demo" ) .await;
150
create ( & h.app, " andy" , " secret" ) .await;
151
create ( & h.app, " other" , " plain" ) .await;
152
let url = format! ( " {} /andy/demo" , h.base) ;
153
let work = tempfile:: tempdir( ) .unwrap ( ) ;
154
let mut c = client ( & url, work.path ( ) ) ;
155
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
156
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
157
c.snapshot ( " test" ) .unwrap ( ) ;
158
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
159
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
160
161
162
let runs = runs_of ( & h.app, " andy" , " demo" ) .await;
163
assert_eq! ( runs.len ( ) , 1 ) ;
164
let run = & runs[ 0 ] ;
165
assert_eq! ( ( run.bookmark.as_str ( ) , run.status.as_str ( ) , run.actor.as_str ( ) ) , ( " main" , " queued" , " site token" ) ) ;
166
assert_eq! ( run.pipeline_hash.len ( ) , 64 ) ;
167
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
168
assert_eq! ( jobs.iter ( ) .map ( | j | ( j.name.as_str ( ) , j.depth) ) .collect:: < Vec< _ > > ( ) , [ ( " build" , 0 ) , ( " lint" , 0 ) , ( " test" , 1 ) ] ) ;
90326c96 Andy McCall 1 month ago ↶
169
170
let checks = checks_when ( & h.app, run.repo_id, & run.state, 3 ) .await;
42fa9554 Andy McCall 1 month ago ↶
171
assert_eq! (
172
checks.iter ( ) .map ( | c | ( c.name.as_str ( ) , c.status.as_str ( ) ) ) .collect:: < Vec< _ > > ( ) ,
173
[ ( " flow/build" , " queued" ) , ( " flow/lint" , " queued" ) , ( " flow/test" , " queued" ) ]
174
) ;
175
176
177
assert_eq! (
178
call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " site" ) , Some ( r # "{"runner":"x","runtimes":["microvm"]}"# .into ( ) ) ) .await.0 ,
179
StatusCode:: UNAUTHORIZED
180
) ;
181
assert_eq! (
182
call ( & h.app, " POST" , " /api/v1/flow/claim" , None , Some ( r # "{"runner":"x","runtimes":["microvm"]}"# .into ( ) ) ) .await.0 ,
183
StatusCode:: UNAUTHORIZED
184
) ;
185
186
let ( st, _ ) =
187
call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( r # "{"runner":"x","runtimes":["wasm"]}"# .into ( ) ) ) .await;
188
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
189
190
191
let a = claim ( & h.app) .await.unwrap ( ) ;
192
let b = claim ( & h.app) .await.unwrap ( ) ;
193
assert_eq! ( ( a[ " job" ] [ " name" ] .as_str ( ) , b[ " job" ] [ " name" ] .as_str ( ) ) , ( Some ( " build" ) , Some ( " lint" ) ) ) ;
194
assert! ( claim ( & h.app) .await.is_none ( ) , " test waits for build" ) ;
195
assert_eq! ( a[ " repo" ] , " andy/demo" ) ;
196
assert_eq! ( a[ " clone_url" ] , url.as_str ( ) ) ;
197
assert_eq! ( a[ " sandbox" ] [ " rootfs" ] , ROOTFS ) ;
198
assert_eq! ( a[ " job" ] [ " exec" ] , serde_json:: json! ( [ " make" ] ) ) ;
199
assert_eq! ( a[ " timeout_s" ] , 3600 ) ;
9a065bc0 Andy McCall 1 month ago ↶
200
201
assert_eq! (
202
( a[ " state" ] .as_str ( ) , a[ " bookmark" ] .as_str ( ) , a[ " run_number" ] .as_i64 ( ) ) ,
203
( Some ( run.state.as_str ( ) ) , Some ( " main" ) , Some ( run.number) )
204
) ;
42fa9554 Andy McCall 1 month ago ↶
205
let heads: Vec< String> = a[ " heads" ] .as_array ( ) .unwrap ( ) .iter ( ) .map ( | v | v.as_str ( ) .unwrap ( ) .to_string ( ) ) .collect ( ) ;
206
assert_eq! ( heads.join ( " +" ) , run.heads) ;
207
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
208
assert_eq! ( checks.iter ( ) .find ( | c | c.name = = " flow/build" ) .unwrap ( ) .status, " running" ) ;
209
210
211
let token = a[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
212
assert! ( token.starts_with ( " flowjob_" ) ) ;
e8fa5369 Andy McCall 23 days ago ↶
213
let opts = RemoteOptions { token: Some ( token.clone ( ) ) , .. Default :: default( ) } ;
42fa9554 Andy McCall 1 month ago ↶
214
open_remote ( & url, & opts) .unwrap ( ) .refs ( ) .unwrap ( ) ;
215
assert! ( open_remote ( & format! ( " {} /andy/secret" , h.base) , & opts) .unwrap ( ) .refs ( ) .is_err ( ) , " another repository" ) ;
e8fa5369 Andy McCall 23 days ago ↶
216
let bad = RemoteOptions { token: Some ( " flowjob_00" .into ( ) ) , .. Default :: default( ) } ;
42fa9554 Andy McCall 1 month ago ↶
217
assert! ( open_remote ( & url, & bad) .unwrap ( ) .refs ( ) .is_err ( ) , " an unknown job token" ) ;
218
219
c.config.set ( " remote.origin.token" , & token) .unwrap ( ) ;
220
c.save_config ( ) .unwrap ( ) ;
221
write ( work.path ( ) , " more.txt" , " x\n " ) ;
222
c.snapshot ( " test" ) .unwrap ( ) ;
223
c.commit ( " More" , & [ ] ) .unwrap ( ) ;
224
let pushed = c.push ( " origin" , false ) ;
225
assert! ( pushed.is_err ( ) | | pushed.unwrap ( ) .iter ( ) .all ( | r | ! r.ok) , " a job token cannot push" ) ;
226
c.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
227
c.save_config ( ) .unwrap ( ) ;
228
229
230
let a_id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
231
let b_id = b[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
232
let log = serde_json:: json! ( { " records" : [
233
{ " t" : 5 , " stream" : " flow" , " text" : " booting" } ,
234
{ " t" : 120 , " stream" : " out" , " text" : " cc -o demo demo.c" } ,
235
{ " t" : 130 , " stream" : " weird" , " text" : " x" .repeat ( 20_000 ) } ,
236
] } ) ;
237
assert_eq! (
238
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{a_id} /log" ) , Some ( " runner-secret" ) , Some ( log.to_string ( ) ) ) .await.0 ,
239
StatusCode:: NO_CONTENT
240
) ;
241
let lines = db:: flow:: log( & h.app.db, a_id, 0 , 100 ) .await.unwrap ( ) ;
242
assert_eq! ( lines.len ( ) , 3 ) ;
243
assert_eq! ( ( lines[ 2 ] .stream.as_str ( ) , lines[ 2 ] .text.len ( ) ) , ( " console" , 16 * 1024 ) , " streams are known ones, text is capped" ) ;
244
let ph = a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) ;
245
assert_eq! (
246
finish ( & h.app, a_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : receipt ( " lint" , ph, 0 ) } ) ) .await,
247
StatusCode:: BAD_REQUEST ,
248
" a receipt for another job"
249
) ;
250
assert_eq! (
251
finish ( & h.app, a_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : receipt ( " build" , ph, 0 ) } ) ) .await,
252
StatusCode:: NO_CONTENT
253
) ;
254
assert_eq! ( finish ( & h.app, a_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 } ) ) .await, StatusCode:: CONFLICT , " only once" ) ;
255
assert! ( open_remote ( & url, & opts) .unwrap ( ) .refs ( ) .is_err ( ) , " the token ends with its job" ) ;
256
assert_eq! (
257
finish ( & h.app, b_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 300 , " receipt" : receipt ( " lint" , ph, 0 ) } ) ) .await,
258
StatusCode:: NO_CONTENT
259
) ;
260
let t = claim ( & h.app) .await.unwrap ( ) ;
261
assert_eq! ( t[ " job" ] [ " name" ] , " test" ) ;
262
let t_id = t[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
263
assert_eq! (
264
finish ( & h.app, t_id, serde_json:: json! ( { " exit_code" : 3 , " duration_ms" : 500 , " receipt" : receipt ( " test" , ph, 3 ) } ) ) .await,
265
StatusCode:: NO_CONTENT
266
) ;
267
268
let run = db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) ;
269
assert_eq! ( run.status, " failed" ) ;
270
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
271
let build = jobs.iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) ;
272
assert_eq! ( ( build.status.as_str ( ) , build.exit_code, build.receipt_hash.len ( ) ) , ( " passed" , Some ( 0 ) , 64 ) ) ;
273
assert_eq! ( jobs.iter ( ) .find ( | j | j.name = = " test" ) .unwrap ( ) .status, " failed" ) ;
274
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
275
assert_eq! (
276
checks.iter ( ) .map ( | c | ( c.name.as_str ( ) , c.status.as_str ( ) ) ) .collect:: < Vec< _ > > ( ) ,
277
[ ( " flow/build" , " passed" ) , ( " flow/lint" , " passed" ) , ( " flow/test" , " failed" ) ]
278
) ;
a0d4bbfb Andy McCall 1 month ago ↶
279
assert! ( checks[ 2 ] .summary.contains ( " exit 3" ) & & checks[ 2 ] .url.ends_with ( & format! ( " /andy/demo/flow/{} " , run.number) ) ) ;
42fa9554 Andy McCall 1 month ago ↶
280
281
282
let ( st, list) = call ( & h.app, " GET" , " /andy/demo/flow" , None , None ) .await;
283
assert_eq! ( st, StatusCode:: OK ) ;
a0d4bbfb Andy McCall 1 month ago ↶
284
assert! ( list.contains ( & format! ( " /andy/demo/flow/{} " , run.number) ) & & list.contains ( " st-failed" ) , " {list}" ) ;
285
let ( st, page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
42fa9554 Andy McCall 1 month ago ↶
286
assert_eq! ( st, StatusCode:: OK ) ;
287
assert! ( page.contains ( " <svg class=\" flow-graph\" " ) , " graph" ) ;
288
assert_eq! ( page.matches ( " data-job-node=" ) .count ( ) , 3 ) ;
289
assert_eq! ( page.matches ( " <path class=\" edge" ) .count ( ) , 1 , " one dependency edge" ) ;
290
assert! ( page.contains ( " cc -o demo demo.c" ) , " log shown" ) ;
291
assert! ( ! page.contains ( " data-flow-live" ) , " a finished run does not poll" ) ;
a0d4bbfb Andy McCall 1 month ago ↶
292
let ( st, live) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} /live?after={a_id} :1" , run.number) , None , None ) .await;
42fa9554 Andy McCall 1 month ago ↶
293
assert_eq! ( st, StatusCode:: OK ) ;
294
let live: serde_json:: Value = serde_json:: from_str( & live) .unwrap ( ) ;
295
assert_eq! ( live[ " status" ] , " failed" ) ;
296
let build_live = live[ " jobs" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | j | j[ " name" ] = = " build" ) .unwrap ( ) .clone ( ) ;
297
assert_eq! ( build_live[ " lines" ] .as_array ( ) .unwrap ( ) .len ( ) , 2 , " lines after seq 1" ) ;
298
assert_eq! ( call ( & h.app, " GET" , " /andy/demo/flow/999" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
299
300
301
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
302
write ( work.path ( ) , " test.sh" , " echo changed\n " ) ;
303
c.snapshot ( " test" ) .unwrap ( ) ;
304
c.commit ( " Change the test" , & [ ] ) .unwrap ( ) ;
305
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
306
let mut second = None ;
307
for _ in 0 .. 100 {
308
let r = runs_of ( & h.app, " andy" , " demo" ) .await;
309
if r.len ( ) = = 2 {
310
second = Some ( r[ 0 ] .clone ( ) ) ;
311
break ;
312
}
313
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
314
}
315
let second = second.expect ( " a second run" ) ;
316
let x = claim ( & h.app) .await.unwrap ( ) ;
317
let y = claim ( & h.app) .await.unwrap ( ) ;
318
let ( build_id, lint_id) = if x[ " job" ] [ " name" ] = = " build" {
319
( x[ " job_id" ] .as_i64 ( ) .unwrap ( ) , y[ " job_id" ] .as_i64 ( ) .unwrap ( ) )
320
} else {
321
( y[ " job_id" ] .as_i64 ( ) .unwrap ( ) , x[ " job_id" ] .as_i64 ( ) .unwrap ( ) )
322
} ;
323
assert_eq! (
324
finish ( & h.app, build_id, serde_json:: json! ( { " exit_code" : null, " duration_ms" : 10 , " error" : " rootfs is not on this runner" } ) )
325
.await,
326
StatusCode:: NO_CONTENT
327
) ;
328
assert_eq! ( finish ( & h.app, lint_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 } ) ) .await, StatusCode:: NO_CONTENT ) ;
329
assert! ( claim ( & h.app) .await.is_none ( ) , " test was skipped" ) ;
330
let jobs = db:: flow:: jobs( & h.app.db, second.id) .await.unwrap ( ) ;
331
assert_eq! (
332
jobs.iter ( ) .map ( | j | ( j.name.as_str ( ) , j.status.as_str ( ) ) ) .collect:: < Vec< _ > > ( ) ,
333
[ ( " build" , " error" ) , ( " lint" , " passed" ) , ( " test" , " skipped" ) ]
334
) ;
335
assert_eq! ( db:: flow:: run_by_id( & h.app.db, second.id) .await.unwrap ( ) .unwrap ( ) .status, " failed" ) ;
336
337
338
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " job \" broken\" {\n sandbox \" missing\" \n exec \" x\" \n }\n " ) ) ;
339
c.snapshot ( " test" ) .unwrap ( ) ;
340
c.commit ( " Break the pipeline" , & [ ] ) .unwrap ( ) ;
341
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
342
let mut third = None ;
343
for _ in 0 .. 100 {
344
let r = runs_of ( & h.app, " andy" , " demo" ) .await;
345
if r.len ( ) = = 3 {
346
third = Some ( r[ 0 ] .clone ( ) ) ;
347
break ;
348
}
349
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
350
}
351
let third = third.expect ( " a third run" ) ;
352
assert_eq! ( third.status, " error" ) ;
353
assert! ( third.error.contains ( " line 18" ) & & third.error.contains ( " \" missing\" " ) , " {}" , third.error) ;
a0d4bbfb Andy McCall 1 month ago ↶
354
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , third.number) , None , None ) .await;
42fa9554 Andy McCall 1 month ago ↶
355
assert! ( page.contains ( " The pipeline is not valid" ) ) ;
356
736f6813 Andy McCall 1 month ago ↶
357
358
write ( work.path ( ) , " README.md" , " # Demo\n \n [](.flow/run)\n \n \n " ) ;
359
std:: fs:: create_dir_all( work.path ( ) .join ( " img" ) ) .unwrap ( ) ;
360
std:: fs:: write( work.path ( ) .join ( " img/logo.png" ) , b " \x89 PNG\r \n \x1a \n \0 \0 \0 \r IHDR\0 \0 \0 \x01 \0 \0 \0 \x01 \x08 \x06 \0 \0 \0 \x1f \x15 \xc4 \x89 " )
361
.unwrap ( ) ;
362
c.snapshot ( " test" ) .unwrap ( ) ;
363
c.commit ( " A README with a stamp" , & [ ] ) .unwrap ( ) ;
364
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
365
let mut fourth = None ;
366
for _ in 0 .. 100 {
367
let r = runs_of ( & h.app, " andy" , " demo" ) .await;
368
if r.len ( ) = = 4 {
369
fourth = Some ( r[ 0 ] .clone ( ) ) ;
370
break ;
371
}
372
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
373
}
374
let fourth = fourth.expect ( " a fourth run" ) ;
375
let ( _ , overview) = call ( & h.app, " GET" , " /andy/demo" , None , None ) .await;
376
assert! ( overview.contains ( " src=\" /andy/demo/raw/main/.flow/stamp\" " ) , " {overview}" ) ;
377
assert! ( overview.contains ( " href=\" /andy/demo/file/main/.flow/run\" " ) ) ;
378
assert! ( overview.contains ( " src=\" /andy/demo/raw/main/img/logo.png\" " ) , " images use the raw route" ) ;
379
let ( st, _ , ct) = head ( & h.app, " /andy/demo/raw/main/img/logo.png" ) .await;
380
assert_eq! ( ( st, ct.as_str ( ) ) , ( StatusCode:: OK , " image/png" ) ) ;
381
let ( st, loc, _ ) = head ( & h.app, " /andy/demo/raw/main/.flow/stamp" ) .await;
382
assert! ( st.is_redirection ( ) & & loc = = " /andy/demo/flow/stamp/main" , " {st} {loc}" ) ;
383
let ( st, loc, _ ) = head ( & h.app, " /andy/demo/flow/stamp/main" ) .await;
384
assert! ( st.is_redirection ( ) & & loc = = format! ( " /andy/demo/flow/stamp/{} " , fourth.heads) , " a bookmark's stamp names its state: {loc}" ) ;
385
let ( st, _ , ct) = head ( & h.app, & loc) .await;
386
assert_eq! ( ( st, ct.as_str ( ) ) , ( StatusCode:: OK , " image/svg+xml; charset=utf-8" ) ) ;
387
let ( _ , svg) = call ( & h.app, " GET" , & loc, None , None ) .await;
388
assert! ( svg.starts_with ( " <svg" ) & & svg.contains ( " error ·" ) & & svg.contains ( & fourth.heads[ .. 12 ] ) , " {svg}" ) ;
389
let ( _ , svg1) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/stamp/{} " , run.heads) , None , None ) .await;
390
assert! ( svg1.contains ( " failed ·" ) & & svg1.contains ( & format! ( " run #{} failed" , run.id) ) , " an older state keeps its own result: {svg1}" ) ;
391
assert! ( svg1.contains ( & build.receipt_hash) , " receipt hashes in the metadata" ) ;
392
assert_eq! ( svg1.matches ( " text-anchor=\" middle\" " ) .count ( ) , 3 , " one glyph per job" ) ;
393
let ( st, loc, _ ) = head ( & h.app, " /andy/demo/file/main/.flow/run" ) .await;
394
assert! ( st.is_redirection ( ) & & loc = = " /andy/demo/flow/at/main" , " {loc}" ) ;
395
let ( st, loc, _ ) = head ( & h.app, " /andy/demo/flow/at/main" ) .await;
396
assert! ( st.is_redirection ( ) & & loc = = format! ( " /andy/demo/flow/{} " , fourth.id) , " {loc}" ) ;
397
42fa9554 Andy McCall 1 month ago ↶
398
399
let work2 = tempfile:: tempdir( ) .unwrap ( ) ;
400
let c2 = client ( & format! ( " {} /other/plain" , h.base) , work2.path ( ) ) ;
401
write ( work2.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
402
c2.snapshot ( " test" ) .unwrap ( ) ;
403
c2.commit ( " Pipeline" , & [ ] ) .unwrap ( ) ;
404
assert! ( c2.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
405
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 500 ) ) .await;
406
let row = db:: repos:: get( & h.app.db, " other" , " plain" ) .await.unwrap ( ) .unwrap ( ) ;
407
assert! ( db:: flow:: runs( & h.app.db, row.id, 10 , None ) .await.unwrap ( ) .is_empty ( ) ) ;
408
let ( _ , page) = call ( & h.app, " GET" , " /other/plain/flow" , None , None ) .await;
409
assert! ( page.contains ( " not enabled for this repository" ) ) ;
736f6813 Andy McCall 1 month ago ↶
410
let ( _ , loc, _ ) = head ( & h.app, " /other/plain/flow/stamp/main" ) .await;
411
let ( _ , svg) = call ( & h.app, " GET" , & loc, None , None ) .await;
412
assert! ( svg.contains ( " no run ·" ) , " {svg}" ) ;
413
let ( _ , loc, _ ) = head ( & h.app, " /other/plain/flow/at/main" ) .await;
414
assert_eq! ( loc, " /other/plain/flow" ) ;
42fa9554 Andy McCall 1 month ago ↶
415
}
fd3db644 Andy McCall 1 month ago ↶
416
417
# [ tokio ::test ( flavor = " multi_thread" ) ]
418
async fn claims_carry_pinned_subrepos_and_the_job_token_reads_only_those ( ) {
419
let h = hub ( ) .await;
25091e5c Andy McCall 1 month ago ↶
420
for n in [ " base" , " lib" , " vault" , " other" , " app" ] {
fd3db644 Andy McCall 1 month ago ↶
421
create ( & h.app, " andy" , n) .await;
422
}
423
let push = | name : & str , file : & str | {
424
let work = tempfile:: tempdir( ) .unwrap ( ) ;
425
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
426
write ( work.path ( ) , file, " x\n " ) ;
427
c.snapshot ( " t" ) .unwrap ( ) ;
428
c.commit ( " first" , & [ ] ) .unwrap ( ) ;
429
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
430
let heads = c.bookmark_heads ( ) .unwrap ( ) ;
431
( work, heads)
432
} ;
25091e5c Andy McCall 1 month ago ↶
433
let ( _b, base_heads) = push ( " base" , " base.rs" ) ;
434
435
let lib_work = tempfile:: tempdir( ) .unwrap ( ) ;
436
let lib = client ( & format! ( " {} /andy/lib" , h.base) , lib_work.path ( ) ) ;
437
lib.submodule_add ( & format! ( " {} /andy/base" , h.base) , " deps/base" , Some ( " main" ) ) .unwrap ( ) ;
438
write ( lib_work.path ( ) , " lib.rs" , " x\n " ) ;
439
lib.snapshot ( " t" ) .unwrap ( ) ;
440
lib.commit ( " lib on base" , & [ ] ) .unwrap ( ) ;
441
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
442
let lib_heads = lib.bookmark_heads ( ) .unwrap ( ) ;
fd3db644 Andy McCall 1 month ago ↶
443
let ( _v, _ ) = push ( " vault" , " secret.rs" ) ;
444
let ( _o, _ ) = push ( " other" , " o.rs" ) ;
445
446
447
let work = tempfile:: tempdir( ) .unwrap ( ) ;
448
let app = client ( & format! ( " {} /andy/app" , h.base) , work.path ( ) ) ;
449
app.submodule_add ( & format! ( " {} /andy/lib" , h.base) , " deps/lib" , Some ( " main" ) ) .unwrap ( ) ;
a4f01d51 Andy McCall 1 month ago ↶
450
2bbe88a8 Andy McCall 1 month ago ↶
451
452
fd3db644 Andy McCall 1 month ago ↶
453
sqlx:: query( " UPDATE repositories SET visibility = 'private' WHERE name = 'vault'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
a4f01d51 Andy McCall 1 month ago ↶
454
let bare_work = tempfile:: tempdir( ) .unwrap ( ) ;
455
let mut bare = Repo:: init( bare_work.path ( ) ) .unwrap ( ) ;
456
bare.config.set ( " remote.origin.url" , & format! ( " {} /andy/bare" , h.base) ) .unwrap ( ) ;
457
bare.save_config ( ) .unwrap ( ) ;
458
assert! ( bare.submodule_add ( & format! ( " {} /andy/vault" , h.base) , " deps/vault" , Some ( " main" ) ) .is_err ( ) , " not without a token" ) ;
370035a4 Andy McCall 1 month ago ↶
459
app.submodule_add ( & format! ( " {} /andy/vault" , h.base) , " deps/vault" , Some ( " main" ) ) .unwrap ( ) ;
a4f01d51 Andy McCall 1 month ago ↶
460
let nested = Repo:: open( & work.path ( ) .join ( " deps/vault" ) ) .unwrap ( ) ;
2bbe88a8 Andy McCall 1 month ago ↶
461
assert! ( nested.config.remote_tokens.is_empty ( ) , " no token is copied into a subrepo" ) ;
462
nested.fetch ( " origin" ) .unwrap ( ) ;
370035a4 Andy McCall 1 month ago ↶
463
fd3db644 Andy McCall 1 month ago ↶
464
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
465
write ( work.path ( ) , " Makefile" , " all:\n " ) ;
466
app.snapshot ( " t" ) .unwrap ( ) ;
467
app.commit ( " app with deps" , & [ ] ) .unwrap ( ) ;
468
assert! ( app.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
469
assert! ( ! runs_of ( & h.app, " andy" , " app" ) .await.is_empty ( ) ) ;
470
471
let job = claim ( & h.app) .await.unwrap ( ) ;
472
let subs = job[ " subrepos" ] .as_array ( ) .cloned ( ) .unwrap_or_default ( ) ;
25091e5c Andy McCall 1 month ago ↶
473
assert_eq! ( subs.len ( ) , 2 , " lib and what lib pins; vault is private and the pusher has no account: {subs:?}" ) ;
474
assert_eq! ( subs[ 1 ] [ " path" ] , " deps/lib/deps/base" , " nested under its parent, after it" ) ;
475
assert_eq! ( subs[ 1 ] [ " heads" ] , serde_json:: json! ( base_heads.iter ( ) .map ( | h | h.to_hex ( ) ) .collect:: < Vec< _ > > ( ) ) ) ;
370035a4 Andy McCall 1 month ago ↶
476
let _ = std:: fs:: remove_dir_all( work.path ( ) .join ( " deps/vault2" ) ) ;
fd3db644 Andy McCall 1 month ago ↶
477
assert_eq! ( subs[ 0 ] [ " path" ] , " deps/lib" ) ;
478
assert_eq! ( subs[ 0 ] [ " repo" ] , " andy/lib" ) ;
479
assert_eq! ( subs[ 0 ] [ " heads" ] , serde_json:: json! ( lib_heads.iter ( ) .map ( | h | h.to_hex ( ) ) .collect:: < Vec< _ > > ( ) ) ) ;
480
assert! ( subs[ 0 ] [ " clone_url" ] .as_str ( ) .unwrap ( ) .ends_with ( " /andy/lib" ) ) ;
afd0be38 Andy McCall 1 month ago ↶
481
482
let job_id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
483
let lines = db:: flow:: log( & h.app.db, job_id, 0 , 100 ) .await.unwrap ( ) ;
484
assert! (
485
lines.iter ( ) .any ( | l | l.stream = = " flow" & & l.text.contains ( " subrepo deps/vault (andy/vault) is left out" ) ) ,
486
" {:?}" ,
487
lines.iter ( ) .map ( | l | & l.text) .collect:: < Vec< _ > > ( )
488
) ;
fd3db644 Andy McCall 1 month ago ↶
489
490
491
let token = job[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
492
let read = | name : & str | {
493
let opts = RemoteOptions { token: Some ( token.clone ( ) ) , .. Default :: default( ) } ;
494
open_remote ( & format! ( " {} /andy/{name} " , h.base) , & opts) .and_then ( | r | r.refs ( ) ) .is_ok ( )
495
} ;
25091e5c Andy McCall 1 month ago ↶
496
assert! ( read ( " app" ) & & read ( " lib" ) & & read ( " base" ) ) ;
fd3db644 Andy McCall 1 month ago ↶
497
assert! ( ! read ( " vault" ) , " not pinned for this pusher" ) ;
498
assert! ( ! read ( " other" ) , " public, but not the job's to read" ) ;
499
}
941abdd6 Andy McCall 1 month ago ↶
500
501
# [ tokio ::test ( flavor = " multi_thread" , worker_threads = 8 ) ]
502
async fn concurrent_log_appends_never_fail_on_a_locked_database ( ) {
503
let h = hub ( ) .await;
504
create ( & h.app, " andy" , " demo" ) .await;
505
let work = tempfile:: tempdir( ) .unwrap ( ) ;
506
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
507
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
508
write ( work.path ( ) , " Makefile" , " all:\n " ) ;
509
c.snapshot ( " t" ) .unwrap ( ) ;
510
c.commit ( " pipeline" , & [ ] ) .unwrap ( ) ;
511
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
512
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
513
let job = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) .remove ( 0 ) ;
514
515
516
let mut tasks = Vec :: new( ) ;
517
for w in 0 .. 16 {
518
let pool = h.app.db.clone ( ) ;
519
tasks.push ( tokio:: spawn( async move {
520
let records: Vec< flow_core:: api:: Record> =
521
( 0 .. 20 ) .map ( | i | flow_core:: api:: Record { t: i, stream: " out" .into ( ) , text: format! ( " writer {w} line {i} " ) } ) .collect ( ) ;
522
db:: flow:: append_log( & pool, job.id, & records, 100_000 ) .await
523
} ) ) ;
524
}
525
for t in tasks {
526
assert_eq! ( t.await.unwrap ( ) .unwrap ( ) , 20 , " every append succeeds" ) ;
527
}
528
let lines = db:: flow:: log( & h.app.db, job.id, 0 , 1_000 ) .await.unwrap ( ) ;
529
assert_eq! ( lines.len ( ) , 320 ) ;
530
let seqs: std:: collections:: BTreeSet< i64 > = lines.iter ( ) .map ( | l | l.seq ) .collect ( ) ;
531
assert_eq! ( seqs.len ( ) , 320 , " sequence numbers are unique" ) ;
532
}
9008bf6c Andy McCall 1 month ago ↶
533
534
535
# [ tokio ::test ( flavor = " multi_thread" ) ]
536
async fn a_repository_setting_turns_flow_on_or_off ( ) {
537
let h = hub ( ) .await;
538
create ( & h.app, " other" , " plain" ) .await;
539
create ( & h.app, " andy" , " quiet" ) .await;
a0d4bbfb Andy McCall 1 month ago ↶
540
541
create ( & h.app, " andy" , " first" ) .await;
542
{
543
let work = tempfile:: tempdir( ) .unwrap ( ) ;
544
let c = client ( & format! ( " {} /andy/first" , h.base) , work.path ( ) ) ;
545
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
546
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
547
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
548
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
549
assert_eq! ( runs_of ( & h.app, " andy" , " first" ) .await.len ( ) , 1 ) ;
550
}
9008bf6c Andy McCall 1 month ago ↶
551
let row = db:: repos:: get( & h.app.db, " other" , " plain" ) .await.unwrap ( ) .unwrap ( ) ;
552
db:: repos:: set_flow( & h.app.db, row.id, " on" ) .await.unwrap ( ) ;
553
let quiet = db:: repos:: get( & h.app.db, " andy" , " quiet" ) .await.unwrap ( ) .unwrap ( ) ;
554
db:: repos:: set_flow( & h.app.db, quiet.id, " off" ) .await.unwrap ( ) ;
555
for ( owner, name) in [ ( " other" , " plain" ) , ( " andy" , " quiet" ) ] {
556
let work = tempfile:: tempdir( ) .unwrap ( ) ;
557
let c = client ( & format! ( " {} /{owner} /{name} " , h.base) , work.path ( ) ) ;
558
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
559
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
560
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
561
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
562
}
a0d4bbfb Andy McCall 1 month ago ↶
563
let plain = runs_of ( & h.app, " other" , " plain" ) .await;
564
assert_eq! ( plain.len ( ) , 1 , " turned on" ) ;
bf3d0754 Andy McCall 1 month ago ↶
565
566
let a = claim ( & h.app) .await.unwrap ( ) ;
567
assert_eq! ( a[ " repo" ] , " andy/first" ) ;
568
let ( _ , page) = call ( & h.app, " GET" , " /other/plain/flow/1" , None , None ) .await;
569
assert! ( page.contains ( " Waiting for a runner: 3rd in line" ) & & page.contains ( " Running now: andy/first #1" ) , " {page}" ) ;
570
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/other/plain/flow/runs/1" , None , None ) .await;
571
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " queue_position" ] , 3 ) ;
572
let ( _ , list) = call ( & h.app, " GET" , " /other/plain/flow" , None , None ) .await;
573
assert! ( list.contains ( " queued, 3rd in line" ) , " {list}" ) ;
a0d4bbfb Andy McCall 1 month ago ↶
574
575
576
assert_eq! ( plain[ 0 ] .number, 1 ) ;
90326c96 Andy McCall 1 month ago ↶
577
let checks = checks_when ( & h.app, row.id, & plain[ 0 ] .state, 3 ) .await;
a0d4bbfb Andy McCall 1 month ago ↶
578
assert! ( checks.iter ( ) .all ( | c | c.url.ends_with ( " /other/plain/flow/1" ) ) , " {checks:?}" ) ;
579
let ( st, page) = call ( & h.app, " GET" , " /other/plain/flow/1" , None , None ) .await;
580
assert! ( st = = StatusCode:: OK & & page.contains ( " Run #1" ) , " {st}" ) ;
9008bf6c Andy McCall 1 month ago ↶
581
assert! ( runs_of ( & h.app, " andy" , " quiet" ) .await.is_empty ( ) , " turned off despite andy/*" ) ;
582
}
1edac4b6 Andy McCall 1 month ago ↶
583
584
585
586
# [ tokio ::test ( flavor = " multi_thread" ) ]
587
async fn runs_start_without_a_push_and_finished_runs_re_run ( ) {
588
let h = hub ( ) .await;
589
590
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
591
create ( & h.app, " andy" , " demo" ) .await;
592
let url = format! ( " {} /andy/demo" , h.base) ;
593
let work = tempfile:: tempdir( ) .unwrap ( ) ;
594
let c = client ( & url, work.path ( ) ) ;
595
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
596
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
597
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
598
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
599
let first = runs_of ( & h.app, " andy" , " demo" ) .await;
600
assert_eq! ( ( first.len ( ) , first[ 0 ] .trigger.as_str ( ) ) , ( 1 , " push" ) ) ;
601
602
603
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
604
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
605
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
606
assert_eq! ( v[ " number" ] , 2 ) ;
607
assert! ( v[ " url" ] .as_str ( ) .unwrap ( ) .ends_with ( " /andy/demo/flow/2" ) ) ;
608
let second = db:: flow:: run( & h.app.db, first[ 0 ] .repo_id, 2 ) .await.unwrap ( ) .unwrap ( ) ;
609
assert_eq! ( ( second.trigger.as_str ( ) , second.actor.as_str ( ) , second.state.as_str ( ) ) , ( " manual" , " andy" , first[ 0 ] .state.as_str ( ) ) ) ;
610
assert_eq! ( db:: flow:: jobs( & h.app.db, second.id) .await.unwrap ( ) .len ( ) , 3 ) ;
611
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/2" , None , None ) .await;
612
assert! ( page.contains ( " started by andy" ) , " the run page says what started it" ) ;
613
4f692533 Andy McCall 1 month ago ↶
614
615
let head = first[ 0 ] .head_list ( ) [ 0 ] .to_string ( ) ;
616
let ( st, body) =
617
call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( format! ( r # "{{ "change":"{head} "}} "# ) ) ) .await;
618
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
619
let on_change = db:: flow:: run( & h.app.db, first[ 0 ] .repo_id, 3 ) .await.unwrap ( ) .unwrap ( ) ;
620
assert_eq! (
621
( on_change.bookmark.as_str ( ) , on_change.heads.as_str ( ) , on_change.state.as_str ( ) ) ,
622
( " " , head.as_str ( ) , first[ 0 ] .state.as_str ( ) )
623
) ;
624
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/3" , None , None ) .await;
625
assert! ( page.contains ( " title=\" Run on one change, not a bookmark\" >change</span>" ) , " run page" ) ;
626
let ( st, body) =
627
call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( format! ( r # "{{ "change":"{} "}} "# , " ab" .repeat ( 32 ) ) ) )
628
.await;
629
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " no change" ) , " {st} {body}" ) ;
630
631
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow" , None , None ) .await;
632
assert! ( page.contains ( " Sign in with write access to run the pipeline." ) & & ! page.contains ( " primary-btn" ) ) ;
633
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/bookmarks" , None , None ) .await;
634
assert! ( ! page.contains ( " /flow/start" ) , " no run buttons for anonymous viewers" ) ;
635
1edac4b6 Andy McCall 1 month ago ↶
636
637
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs/1/rerun" , Some ( " site" ) , None ) .await;
638
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " still queued" ) , " {st} {body}" ) ;
639
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( r # "{"bookmark":"nope"}"# .into ( ) ) ) .await;
640
assert_eq! ( st, StatusCode:: CONFLICT ) ;
641
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs/99/rerun" , Some ( " site" ) , None ) .await;
642
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
643
644
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , None , Some ( " {}" .into ( ) ) ) .await;
645
assert_ne! ( st, StatusCode:: CREATED ) ;
646
647
648
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
649
for r in [ & first[ 0 ] , & second] {
650
for j in db:: flow:: jobs( & h.app.db, r.id) .await.unwrap ( ) {
651
sqlx:: query( " UPDATE flow_jobs SET status = 'passed' WHERE id = ?" ) .bind ( j.id) .execute ( & h.app.db) .await.unwrap ( ) ;
652
}
653
sqlx:: query( " UPDATE flow_runs SET status = 'passed', finished_ms = 1 WHERE id = ?" ) .bind ( r.id) .execute ( & h.app.db) .await.unwrap ( ) ;
654
}
655
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs/1/rerun" , Some ( " site" ) , None ) .await;
656
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
4f692533 Andy McCall 1 month ago ↶
657
let third = db:: flow:: run( & h.app.db, row.id, 4 ) .await.unwrap ( ) .unwrap ( ) ;
348f8c4b Andy McCall 1 month ago ↶
658
assert_eq! (
659
( third.trigger_label ( ) .as_str ( ) , third.status.as_str ( ) , third.state.as_str ( ) ) ,
660
( " rerun:1" , " queued" , first[ 0 ] .state.as_str ( ) )
661
) ;
c889911b Andy McCall 1 month ago ↶
662
assert_eq! ( third.trigger, " rerun:1:push" , " a rerun keeps the event it repeats" ) ;
1edac4b6 Andy McCall 1 month ago ↶
663
let checks = db:: hooks:: checks_for_state( & h.app.db, row.id, & third.state) .await.unwrap ( ) ;
664
assert! (
4f692533 Andy McCall 1 month ago ↶
665
checks.iter ( ) .all ( | c | c.status = = " queued" & & c.url.ends_with ( " /flow/4" ) ) ,
1edac4b6 Andy McCall 1 month ago ↶
666
" the re-run's checks replace the old ones: {checks:?}"
667
) ;
4f692533 Andy McCall 1 month ago ↶
668
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/4" , None , None ) .await;
1edac4b6 Andy McCall 1 month ago ↶
669
assert! ( page.contains ( " re-run of #1 by andy" ) ) ;
670
assert! ( ! page.contains ( " RE-RUN" ) , " anonymous viewers get no button" ) ;
671
0650d94e Andy McCall 26 days ago ↶
672
673
674
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( r # "{"jobs":["test"]}"# .into ( ) ) ) .await;
675
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
676
let n = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " number" ] .as_i64 ( ) .unwrap ( ) ;
677
let only = db:: flow:: run( & h.app.db, first[ 0 ] .repo_id, n) .await.unwrap ( ) .unwrap ( ) ;
678
let mut names: Vec< String> = db:: flow:: jobs( & h.app.db, only.id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
679
names.sort ( ) ;
680
assert_eq! ( names, [ " build" , " test" ] , " test and the job it depends on, not lint" ) ;
681
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( r # "{"jobs":["deploy"]}"# .into ( ) ) ) .await;
682
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " not in the pipeline" ) & & body.contains ( " build, lint, test" ) , " {st} {body}" ) ;
683
1edac4b6 Andy McCall 1 month ago ↶
684
685
db:: repos:: set_flow( & h.app.db, row.id, " off" ) .await.unwrap ( ) ;
686
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
687
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " not enabled" ) , " {body}" ) ;
4f692533 Andy McCall 1 month ago ↶
688
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow" , None , None ) .await;
689
assert! ( page.contains ( " Flow is off for this repository" ) , " and the Flow tab says so" ) ;
90326c96 Andy McCall 1 month ago ↶
690
}
691
692
693
694
# [ tokio ::test ( flavor = " multi_thread" ) ]
695
async fn runs_jobs_and_logs_are_in_the_api ( ) {
696
let h = hub ( ) .await;
697
create ( & h.app, " andy" , " demo" ) .await;
698
let url = format! ( " {} /andy/demo" , h.base) ;
699
let work = tempfile:: tempdir( ) .unwrap ( ) ;
700
let c = client ( & url, work.path ( ) ) ;
701
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
702
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
703
let head = c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
704
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
705
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
706
707
708
let a = claim ( & h.app) .await.unwrap ( ) ;
709
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
710
let recs =
711
serde_json:: json! ( { " records" : [ { " t" : 1 , " stream" : " stdout" , " text" : " one" } , { " t" : 3 , " stream" : " stderr" , " text" : " three" } ] } ) ;
712
let ( st, _ ) = call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( recs.to_string ( ) ) ) .await;
713
assert! ( st.is_success ( ) , " {st}" ) ;
714
715
let ( st, body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs" , None , None ) .await;
716
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
717
let runs: Vec< hub_api:: FlowRun> = serde_json:: from_str( & body) .unwrap ( ) ;
718
assert_eq! ( runs.len ( ) , 1 ) ;
719
assert_eq! ( ( runs[ 0 ] .number, runs[ 0 ] .state.as_str ( ) , runs[ 0 ] .trigger.as_str ( ) ) , ( 1 , run.state.as_str ( ) , " push" ) ) ;
720
assert! ( runs[ 0 ] .url.ends_with ( " /andy/demo/flow/1" ) & & runs[ 0 ] .jobs.is_empty ( ) ) ;
721
722
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , None , None ) .await;
723
let one: hub_api:: FlowRun = serde_json:: from_str( & body) .unwrap ( ) ;
724
let names: Vec< ( & str , & str ) > = one.jobs.iter ( ) .map ( | j | ( j.name.as_str ( ) , j.status.as_str ( ) ) ) .collect ( ) ;
725
assert_eq! ( names.len ( ) , 3 ) ;
726
let running = one.jobs.iter ( ) .find ( | j | j.status = = " running" ) .unwrap ( ) ;
727
assert_eq! ( running.name, a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) ) ;
728
assert! ( one.jobs.iter ( ) .any ( | j | j.name = = " test" & & j.depends_on = = [ " build" ] ) ) ;
729
730
let job = running.name.clone ( ) ;
731
let ( st, body) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/demo/flow/runs/1/jobs/{job} /log" ) , None , None ) .await;
732
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
733
let lines: Vec< hub_api:: FlowLogLine> = serde_json:: from_str( & body) .unwrap ( ) ;
734
assert! ( lines.iter ( ) .any ( | l | l.text = = " three" ) , " {body}" ) ;
735
let last = lines.last ( ) .unwrap ( ) .seq;
736
let ( _ , body) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/demo/flow/runs/1/jobs/{job} /log?after={last} " ) , None , None ) .await;
737
assert_eq! ( body, " []" , " nothing after the last line" ) ;
738
assert_eq! ( call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/9" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
739
assert_eq! ( call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1/jobs/nope/log" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
740
741
742
let by = | key : String| {
743
let app = h.app.clone ( ) ;
744
async move {
745
let ( st, body) = call ( & app, " GET" , & format! ( " /api/v1/repos/andy/demo/checks/{key} " ) , None , None ) .await;
746
assert_eq! ( st, StatusCode:: OK , " {key}: {body}" ) ;
747
serde_json:: from_str:: < Vec< hub_api:: Check> > ( & body) .unwrap ( ) .len ( )
748
}
749
} ;
750
checks_when ( & h.app, run.repo_id, & run.state, 3 ) .await;
751
assert_eq! ( by ( run.state.clone ( ) ) .await, 3 ) ;
752
assert_eq! ( by ( head.to_hex ( ) ) .await, 3 , " a single head names its state" ) ;
753
assert_eq! ( by ( " main" .into ( ) ) .await, 3 ) ;
754
let ( st, _ ) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/checks/no-such-bookmark" , None , None ) .await;
755
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
756
757
758
let ( st, body) = call ( & h.app, " GET" , " /api/v1/users/andy/repos" , None , None ) .await;
759
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
760
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
761
assert_eq! ( v[ " status" ] , 404 , " {body}" ) ;
1edac4b6 Andy McCall 1 month ago ↶
762
}
cc19697f Andy McCall 1 month ago ↶
763
764
async fn get_bytes ( app : & Shared, path : & str ) -> ( StatusCode, Vec< u8 > ) {
765
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( Request:: get( path) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
766
let status = res.status ( ) ;
767
( status, res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) )
768
}
769
770
# [ tokio ::test ( flavor = " multi_thread" ) ]
771
async fn signed_receipts_are_checked_kept_and_shown ( ) {
772
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectRef, ObjectType} ;
773
use oxy_core:: identity:: SecretKey;
774
use oxy_core:: objects:: StateObject;
775
use oxy_core:: receipt:: SignedReceipt;
776
let h = hub ( ) .await;
777
create ( & h.app, " andy" , " demo" ) .await;
778
let url = format! ( " {} /andy/demo" , h.base) ;
779
let work = tempfile:: tempdir( ) .unwrap ( ) ;
780
let c = client ( & url, work.path ( ) ) ;
781
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
782
c.snapshot ( " test" ) .unwrap ( ) ;
783
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
784
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
785
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
786
let heads: Vec< ChangeHash> = run.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
787
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads.clone ( ) ) .encode ( ) ) ;
788
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
789
let sk = SecretKey:: generate( ) ;
790
let runner = sk.identity ( " test-1 (OxygenFlow runner)" , " " , 1 ) ;
791
let sign = | r : & serde_json:: Value, state : ObjectRef, sk : & SecretKey, id : & oxy_core:: identity:: Identity| {
792
let r: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
793
let run_url = format! ( " {url} /flow/{} " , run.number) ;
794
hex:: encode( SignedReceipt:: sign( r.to_oxy ( ) , repo_id, state, & run_url, id, sk) .encode ( ) )
795
} ;
796
797
let a = claim ( & h.app) .await.unwrap ( ) ;
798
let b = claim ( & h.app) .await.unwrap ( ) ;
799
let ph = a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
800
let ( a_id, b_id) = ( a[ " job_id" ] .as_i64 ( ) .unwrap ( ) , b[ " job_id" ] .as_i64 ( ) .unwrap ( ) ) ;
801
802
let rb = receipt ( " build" , & ph, 0 ) ;
803
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : rb, " signed_receipt" : sign ( & rb, state, & sk, & runner) } ) ;
804
assert_eq! ( finish ( & h.app, a_id, body) .await, StatusCode:: NO_CONTENT ) ;
805
806
let other = SecretKey:: generate( ) ;
807
let rl = receipt ( " lint" , & ph, 0 ) ;
808
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 300 , " receipt" : rl,
809
" signed_receipt" : sign ( & rl, state, & other, & other.identity ( " mallory" , " " , 1 ) ) } ) ;
810
assert_eq! ( finish ( & h.app, b_id, body) .await, StatusCode:: NO_CONTENT ) ;
811
812
let t = claim ( & h.app) .await.unwrap ( ) ;
813
let rt = receipt ( " test" , & ph, 0 ) ;
814
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 500 , " receipt" : rt,
815
" signed_receipt" : sign ( & rt, ObjectRef( [ 9 ; 32 ] ) , & sk, & runner) } ) ;
816
assert_eq! ( finish ( & h.app, t[ " job_id" ] .as_i64 ( ) .unwrap ( ) , body) .await, StatusCode:: NO_CONTENT ) ;
817
818
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
819
let job = | n : & str | jobs.iter ( ) .find ( | j | j.name = = n ) .unwrap ( ) .clone ( ) ;
820
assert_eq! ( job ( " build" ) .status, " passed" ) ;
821
assert_eq! ( ( job ( " build" ) .receipt_attest.len ( ) , job ( " build" ) .receipt_signer.as_str ( ) ) , ( 64 , " test-1 (OxygenFlow runner)" ) ) ;
822
assert_eq! ( job ( " lint" ) .status, " passed" , " the job's result stands" ) ;
823
assert! ( job ( " lint" ) .receipt_attest.is_empty ( ) ) ;
824
assert! ( job ( " lint" ) .receipt_note.contains ( " not the key this runner signs with" ) , " {}" , job ( " lint" ) .receipt_note) ;
825
assert! ( job ( " test" ) .receipt_note.contains ( " another state" ) , " {}" , job ( " test" ) .receipt_note) ;
826
827
828
829
let kept = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .receipts ( & heads) .unwrap ( ) ;
830
assert_eq! ( kept.len ( ) , 1 ) ;
831
let attest = job ( " build" ) .receipt_attest;
832
let ( st, bytes) = get_bytes ( & h.app, & format! ( " /andy/demo/receipts/{attest} " ) ) .await;
833
assert_eq! ( st, StatusCode:: OK ) ;
834
let sr = SignedReceipt:: decode( & bytes) .unwrap ( ) ;
835
sr.verify ( ) .unwrap ( ) ;
836
assert_eq! ( ( sr.state ( ) , sr.receipt.job_name.as_str ( ) ) , ( state, " build" ) ) ;
837
assert_eq! ( get_bytes ( & h.app, & format! ( " /andy/demo/receipts/{} " , " 0" .repeat ( 64 ) ) ) .await.0 , StatusCode:: NOT_FOUND ) ;
838
839
840
841
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
842
assert! ( page.contains ( " signed receipt</a> by test-1" ) , " run page" ) ;
843
assert! ( page.contains ( " receipt not kept" ) , " run page" ) ;
844
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/change/{} " , heads[ 0 ] .to_hex ( ) ) , None , None ) .await;
845
assert! ( page.contains ( " <dt>Receipts</dt>" ) & & page.contains ( & format! ( " /andy/demo/receipts/{attest} " ) ) , " change page" ) ;
846
}
a3c8288b Andy McCall 1 month ago ↶
847
848
async fn upload ( app : & Shared, job : i64 , name : & str , bytes : & [u8 ]) -> ( StatusCode, String) {
849
let req = Request:: post( format! ( " /api/v1/flow/jobs/{job} /artifacts/{name} " ) )
850
.header ( header:: AUTHORIZATION , " Bearer runner-secret" )
851
.header ( header:: CONTENT_TYPE , " application/octet-stream" )
852
.body ( Body:: from( bytes.to_vec ( ) ) )
853
.unwrap ( ) ;
854
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
855
let status = res.status ( ) ;
856
( status, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
857
}
858
859
# [ tokio ::test ( flavor = " multi_thread" ) ]
860
async fn artifacts_are_kept_named_by_the_receipt_and_served ( ) {
861
let h = hub ( ) .await;
862
create ( & h.app, " andy" , " demo" ) .await;
863
let url = format! ( " {} /andy/demo" , h.base) ;
864
let work = tempfile:: tempdir( ) .unwrap ( ) ;
865
let c = client ( & url, work.path ( ) ) ;
866
write (
867
work.path ( ) ,
868
" .flow/pipeline.kdl" ,
869
& format! (
870
" schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n output {{ \n artifact \" tool\" {{ \n path \" /src/out/tool\" \n }} \n }} \n }} \n "
871
) ,
872
) ;
873
c.snapshot ( " test" ) .unwrap ( ) ;
874
c.commit ( " Build a tool" , & [ ] ) .unwrap ( ) ;
875
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
876
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
877
let a = claim ( & h.app) .await.unwrap ( ) ;
878
assert_eq! ( a[ " job" ] [ " outputs" ] , serde_json:: json! ( [ { " name" : " tool" , " path" : " /src/out/tool" } ] ) , " the runner is told" ) ;
879
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
880
881
let body: Vec< u8 > = ( 0 .. 300_000 u32 ) .map ( | i | ( i % 253 ) as u8 ) .collect ( ) ;
882
let ( st, text) = upload ( & h.app, id, " other" , & body) .await;
883
assert_eq! ( st, StatusCode:: BAD_REQUEST , " an undeclared artifact: {text}" ) ;
884
let ( st, text) = upload ( & h.app, id, " tool" , & body) .await;
885
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
886
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
887
assert_eq! ( blob, oxy_core:: repo:: Repo:: blob_ref( & body) .to_hex ( ) , " the same blob the runner computes" ) ;
888
889
890
let mut r = receipt ( " build" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
891
assert_eq! (
892
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : r.clone ( ) } ) ) .await,
893
StatusCode:: BAD_REQUEST
894
) ;
895
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
896
assert_eq! ( finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : r } ) ) .await, StatusCode:: NO_CONTENT ) ;
897
898
899
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
900
let link = format! ( " /andy/demo/flow/{} /artifacts/build/tool" , run.number) ;
901
assert! ( page.contains ( & link) , " run page lists the artifact" ) ;
902
let ( st, got) = get_bytes ( & h.app, & link) .await;
903
assert_eq! ( ( st, got = = body) , ( StatusCode:: OK , true ) ) ;
904
assert_eq! ( get_bytes ( & h.app, & format! ( " /andy/demo/flow/{} /artifacts/build/none" , run.number) ) .await.0 , StatusCode:: NOT_FOUND ) ;
10708ab8 Andy McCall 1 month ago ↶
905
45977501 Andy McCall 26 days ago ↶
906
907
908
{
909
let name = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
910
let mut hosted = oxy_core:: repo:: Repo:: open( & h.app.repos.dir ( & name) ) .unwrap ( ) ;
911
hosted.config.set ( " core.gc-grace" , " 0" ) .unwrap ( ) ;
912
hosted.save_config ( ) .unwrap ( ) ;
913
}
914
let done = oxygenhub:: maintenance:: gc_all( & h.app) .await;
915
assert! ( done.iter ( ) .all ( | d | ! d.failed) , " {done:?}" ) ;
916
let ( st, got) = get_bytes ( & h.app, & link) .await;
917
assert_eq! ( ( st, got = = body) , ( StatusCode:: OK , true ) , " the artifact survives gc" ) ;
918
10708ab8 Andy McCall 1 month ago ↶
919
920
921
let form = | f : String| {
922
Request:: post( " /register" ) .header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) .body ( Body:: from( f) ) .unwrap ( )
923
} ;
924
let res = oxygenhub:: router( h.app.clone ( ) )
925
.oneshot ( form ( " username=andy&email=a%40example.com&password=correct-horse-battery" .into ( ) ) )
926
.await
927
.unwrap ( ) ;
928
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
929
let with_cookie = | method : & str , path : & str , body : Option< String> | {
930
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
931
if body.is_some ( ) {
932
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
933
}
934
req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( )
935
} ;
936
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
937
db:: releases:: create( & h.app.db, row.id, " v1" , " One" , " " , false , None ) .await.unwrap ( ) ;
938
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( with_cookie ( " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None ) ) .await.unwrap ( ) ;
939
let page = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
940
let attach = format! ( " /andy/demo/flow/{} /artifacts/build/tool/attach" , run.number) ;
941
assert! ( page.contains ( & attach) , " a writer is offered the release" ) ;
942
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
943
let csrf = page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
944
let release_id = db:: releases:: list( & h.app.db, row.id) .await.unwrap ( ) [ 0 ] .id;
945
let res = oxygenhub:: router( h.app.clone ( ) )
946
.oneshot ( with_cookie ( " POST" , & attach, Some ( format! ( " _csrf={csrf} &release={release_id} " ) ) ) )
947
.await
948
.unwrap ( ) ;
949
assert_eq! ( res.status ( ) , StatusCode:: SEE_OTHER ) ;
950
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/releases/tag/v1" , None , None ) .await;
951
assert! (
952
page.contains ( " >tool</a>" ) & & page.contains ( & format! ( " built by <a href=\" /andy/demo/flow/{0} \" >run #{0} </a> build" , run.number) ) ,
953
" {page}"
954
) ;
955
let ( _ , api) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/releases" , None , None ) .await;
956
let api: serde_json:: Value = serde_json:: from_str( & api) .unwrap ( ) ;
957
let asset = & api[ 0 ] [ " assets" ] [ 0 ] ;
958
assert_eq! (
959
( asset[ " name" ] .as_str ( ) , asset[ " built_by" ] [ " run" ] .as_i64 ( ) , asset[ " built_by" ] [ " job" ] .as_str ( ) ) ,
960
( Some ( " tool" ) , Some ( run.number) , Some ( " build" ) )
961
) ;
962
assert! ( asset[ " built_by" ] [ " receipt" ] .is_null ( ) , " that receipt was not signed" ) ;
a3c8288b Andy McCall 1 month ago ↶
963
}
df940d75 Andy McCall 1 month ago ↶
964
d29070fa Andy McCall 1 month ago ↶
965
966
a36bab6b Andy McCall 26 days ago ↶
967
async fn upload_report_file ( app : & Shared, job : i64 , name : & str , path : & str , bytes : & [u8 ]) -> ( StatusCode, String) {
968
let req = Request:: post( format! ( " /api/v1/flow/jobs/{job} /reports/{name} /files" ) )
969
.header ( header:: AUTHORIZATION , " Bearer runner-secret" )
970
.header ( header:: CONTENT_TYPE , " application/octet-stream" )
971
.header ( " X-Flow-Report-Path" , path)
972
.body ( Body:: from( bytes.to_vec ( ) ) )
973
.unwrap ( ) ;
974
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
975
let status = res.status ( ) ;
976
( status, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
977
}
978
979
980
981
982
# [ tokio ::test ( flavor = " multi_thread" ) ]
983
async fn reports_are_kept_file_by_file_and_indexed ( ) {
984
let h = hub ( ) .await;
985
create ( & h.app, " andy" , " demo" ) .await;
986
let url = format! ( " {} /andy/demo" , h.base) ;
987
let work = tempfile:: tempdir( ) .unwrap ( ) ;
988
let c = client ( & url, work.path ( ) ) ;
989
write (
990
work.path ( ) ,
991
" .flow/pipeline.kdl" ,
992
& format! (
993
" schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" perf\" {{ \n sandbox \" alpine\" \n exec \" make\" \n output {{ \n artifact \" tool\" {{ \n path \" /src/out/tool\" \n }} \n report \" frame-timing\" {{ \n path \" /src/out/report\" \n when \" always\" \n }} \n }} \n }} \n "
994
) ,
995
) ;
996
c.snapshot ( " test" ) .unwrap ( ) ;
997
c.commit ( " Measure frames" , & [ ] ) .unwrap ( ) ;
998
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
999
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
1000
let a = claim ( & h.app) .await.unwrap ( ) ;
1001
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1002
1003
let toml = b " kind = \" performance\" \n title = \" Frame timing\" \n summary = \" Median 16.4 ms\" \n \n [metrics]\n median_ms = { value = 16.4, unit = \" ms\" , better = \" lower\" , fail_above = 20 }\n \n [context]\n target_fps = 30\n " ;
1004
let page = b " <p>frames</p>" .to_vec ( ) ;
1005
let data: Vec< u8 > = ( 0 .. 100_000 u32 ) .map ( | i | ( i % 241 ) as u8 ) .collect ( ) ;
c8df33a6 Andy McCall 26 days ago ↶
1006
let ( page_bytes, data_bytes) = ( page.clone ( ) , data.clone ( ) ) ;
a36bab6b Andy McCall 26 days ago ↶
1007
let files: [ ( & str , & [ u8 ] ) ; 3 ] = [ ( " data/frames.csv" , & data) , ( " index.html" , & page) , ( " report.toml" , toml) ] ;
1008
1009
assert_eq! ( upload_report_file ( & h.app, id, " tool" , " report.toml" , toml) .await.0 , StatusCode:: BAD_REQUEST , " not a report" ) ;
1010
assert_eq! ( upload_report_file ( & h.app, id, " frame-timing" , " ../x" , toml) .await.0 , StatusCode:: BAD_REQUEST , " not a clean path" ) ;
1011
let mut listed = vec! [ ] ;
1012
for ( path, bytes) in files {
1013
let ( st, text) = upload_report_file ( & h.app, id, " frame-timing" , path, bytes) .await;
1014
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
1015
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1016
assert_eq! ( blob, oxy_core:: repo:: Repo:: blob_ref( bytes) .to_hex ( ) ) ;
1017
listed.push ( serde_json:: json! ( { " path" : path, " blob" : blob, " size" : bytes.len ( ) } ) ) ;
1018
}
1019
let mut missing = listed.clone ( ) ;
1020
missing.push ( serde_json:: json! ( { " path" : " assets/x.js" , " blob" : " ab" .repeat ( 32 ) , " size" : 1 } ) ) ;
1021
let ( st, text) =
1022
upload ( & h.app, id, " frame-timing" , & serde_json:: to_vec( & serde_json:: json! ( { " version" : 1 , " files" : missing } ) ) .unwrap ( ) ) .await;
1023
assert_eq! ( st, StatusCode:: BAD_REQUEST , " a file never uploaded: {text}" ) ;
1024
assert! ( text.contains ( " assets/x.js" ) , " {text}" ) ;
1025
let manifest = serde_json:: to_vec( & serde_json:: json! ( { " version" : 1 , " files" : listed } ) ) .unwrap ( ) ;
1026
let ( st, text) = upload ( & h.app, id, " frame-timing" , & manifest) .await;
1027
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
1028
1029
let ( kind, title, summary, files_n, bytes) : ( String , String , String , i64 , i64 ) =
1030
sqlx:: query_as( " SELECT kind, title, summary, files, bytes FROM reports WHERE job_id = ? AND name = 'frame-timing'" )
1031
.bind ( id)
1032
.fetch_one ( & h.app.db)
1033
.await
1034
.unwrap ( ) ;
1035
assert_eq! ( ( kind.as_str ( ) , title.as_str ( ) , summary.as_str ( ) ) , ( " performance" , " Frame timing" , " Median 16.4 ms" ) ) ;
1036
assert_eq! ( ( files_n, bytes) , ( 3 , ( data.len ( ) + page.len ( ) + toml.len ( ) ) as i64 ) ) ;
1037
let rid: i64 = sqlx:: query_scalar( " SELECT id FROM reports WHERE job_id = ?" ) .bind ( id) .fetch_one ( & h.app.db) .await.unwrap ( ) ;
1038
let m = oxygenhub:: reports:: metrics( & h.app.db, rid) .await.unwrap ( ) ;
1039
assert_eq! ( ( m[ 0 ] .key.as_str ( ) , m[ 0 ] .value, m[ 0 ] .fail_above) , ( " median_ms" , 16. 4 , Some ( 20. 0 ) ) ) ;
1040
let log = db:: flow:: log( & h.app.db, id, 0 , 1000 ) .await.unwrap ( ) ;
1041
assert! ( log.iter ( ) .any ( | r | r.text.contains ( " report frame-timing: performance \" Frame timing\" , 1 metric" ) ) , " {log:?}" ) ;
c8df33a6 Andy McCall 26 days ago ↶
1042
1043
1044
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
1045
let c = checks.iter ( ) .find ( | c | c.name = = " report/frame-timing/median_ms" ) .expect ( " a check per threshold" ) ;
1046
assert_eq! ( c.status, " passed" , " {c:?}" ) ;
1047
assert! ( c.summary.contains ( " 16.4 ms" ) & & c.url.contains ( " /reports/frame-timing?id=" ) , " {c:?}" ) ;
1048
1049
1050
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo" , None , None ) .await;
1051
assert! ( page.contains ( " >Reports</a>" ) , " the tab" ) ;
1052
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/reports" , None , None ) .await;
1053
assert! ( st = = StatusCode:: OK & & page.contains ( " Frame timing" ) & & page.contains ( " median_ms" ) , " {st}" ) ;
1054
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/reports/frame-timing" , None , None ) .await;
1055
assert! ( st = = StatusCode:: OK & & page.contains ( " fails above 20" ) & & page.contains ( " sandbox=\" allow-scripts\" " ) , " {st}" ) ;
1056
let src = page.split ( " class=\" rep-frame\" src=\" " ) .nth ( 1 ) .unwrap ( ) .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1057
assert! ( src.ends_with ( " /index.html" ) , " {src}" ) ;
1058
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( Request:: get( & src) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
1059
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
1060
let csp = res.headers ( ) [ header:: CONTENT_SECURITY_POLICY ] .to_str ( ) .unwrap ( ) .to_string ( ) ;
1061
assert! ( csp.starts_with ( " sandbox allow-scripts;" ) & & csp.contains ( " default-src 'none'" ) , " {csp}" ) ;
d74ce22f Andy McCall 21 days ago ↶
1062
1063
assert! (
1064
res.headers ( ) .get ( header:: X_FRAME_OPTIONS ) .is_none ( ) & & res.headers ( ) .get ( header:: CONTENT_SECURITY_POLICY_REPORT_ONLY ) .is_none ( )
1065
) ;
1066
assert_eq! ( res.headers ( ) [ header:: X_CONTENT_TYPE_OPTIONS ] , " nosniff" ) ;
c8df33a6 Andy McCall 26 days ago ↶
1067
assert_eq! ( res.headers ( ) [ header:: ACCESS_CONTROL_ALLOW_ORIGIN ] , " *" ) ;
f3e5e2bc Andy McCall 26 days ago ↶
1068
1069
let served = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ;
1070
assert! ( served.starts_with ( & page_bytes) , " the report's page" ) ;
1071
assert! ( String :: from_utf8_lossy( & served) .contains ( " /static/report-frame.js" ) , " with the frame script" ) ;
1072
let ( st, _ ) = get_bytes ( & h.app, " /static/report.css" ) .await;
1073
assert_eq! ( st, StatusCode:: OK , " the stylesheet a report may link" ) ;
c8df33a6 Andy McCall 26 days ago ↶
1074
let data = src.replace ( " /index.html" , " /data/frames.csv" ) ;
1075
assert_eq! ( get_bytes ( & h.app, & data) .await, ( StatusCode:: OK , data_bytes.clone ( ) ) ) ;
1076
95b525b0 Andy McCall 25 days ago ↶
1077
1078
1079
let first: i64 = sqlx:: query_scalar( " SELECT id FROM reports WHERE name = 'frame-timing'" ) .fetch_one ( & h.app.db) .await.unwrap ( ) ;
1080
let second: i64 = sqlx:: query_scalar(
1081
" INSERT INTO reports (repo_id, name, kind, title, summary, context, run_id, job_id, run_number, job, bookmark, state, manifest, files, bytes, created_ms) \
1082
SELECT repo_id, name, kind, title, 'Median 18.0 ms', context, run_id, NULL, 2, job, bookmark, state, manifest, files, bytes, created_ms + 1000 FROM reports WHERE id = ? RETURNING id" ,
1083
)
1084
.bind ( first)
1085
.fetch_one ( & h.app.db)
1086
.await
1087
.unwrap ( ) ;
1088
sqlx:: query(
1089
" INSERT INTO report_metrics (report_id, key, value, unit, better, fail_above) VALUES (?, 'median_ms', 18.0, 'ms', 'lower', 20)" ,
1090
)
1091
.bind ( second)
1092
.execute ( & h.app.db)
1093
.await
1094
.unwrap ( ) ;
1095
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/reports/frame-timing" , None , None ) .await;
1096
assert_eq! ( page.matches ( " data-tl-stop" ) .count ( ) , 2 , " a stop per run" ) ;
1097
assert! ( page.contains ( " Median 18.0 ms" ) , " the newest run is shown" ) ;
1098
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/reports/frame-timing?id={first} &compare={second} &play=3" ) , None , None ) .await;
1099
assert! ( page.contains ( " This run against run #2" ) & & page.contains ( " -1.6" ) , " every metric against the other run" ) ;
1100
let stop = page.split ( " data-tl-stop" ) .next ( ) .unwrap_or ( " " ) .rsplit ( " href=" ) .next ( ) .unwrap_or ( " " ) .to_string ( ) ;
1101
assert! ( page.contains ( " data-play=\" 3\" " ) , " playing" ) ;
1102
assert! ( stop.contains ( " play" ) & & stop.contains ( ' 3' ) , " playing carries on from stop to stop: {stop}" ) ;
1103
assert! ( page.contains ( " data-report-runs=" ) , " the frame interface knows the runs" ) ;
c8df33a6 Andy McCall 26 days ago ↶
1104
let forged = src.replacen ( " /files/" , " /files/9" , 1 ) ;
1105
assert_eq! ( get_bytes ( & h.app, & forged) .await.0 , StatusCode:: NOT_FOUND ) ;
1106
assert_eq! ( get_bytes ( & h.app, & src.replace ( " /index.html" , " /secret.txt" ) ) .await.0 , StatusCode:: NOT_FOUND ) ;
a36bab6b Andy McCall 26 days ago ↶
1107
let _ = run;
1108
1109
1110
{
1111
let name = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
1112
let mut hosted = oxy_core:: repo:: Repo:: open( & h.app.repos.dir ( & name) ) .unwrap ( ) ;
1113
hosted.config.set ( " core.gc-grace" , " 0" ) .unwrap ( ) ;
1114
hosted.save_config ( ) .unwrap ( ) ;
1115
}
1116
let done = oxygenhub:: maintenance:: gc_all( & h.app) .await;
1117
assert! ( done.iter ( ) .all ( | d | ! d.failed) , " {done:?}" ) ;
1118
let hosted = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
1119
for ( path, bytes) in files {
1120
assert_eq! ( hosted.read_blob ( & oxy_core:: repo:: Repo:: blob_ref( bytes) ) .unwrap ( ) , bytes, " {path} survives gc" ) ;
1121
}
1122
}
1123
d29070fa Andy McCall 1 month ago ↶
1124
# [ tokio ::test ( flavor = " multi_thread" ) ]
1125
async fn a_failing_job_keeps_an_artifact_outside_its_receipt ( ) {
1126
let h = hub ( ) .await;
1127
create ( & h.app, " andy" , " demo" ) .await;
1128
let url = format! ( " {} /andy/demo" , h.base) ;
1129
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1130
let c = client ( & url, work.path ( ) ) ;
1131
write (
1132
work.path ( ) ,
1133
" .flow/pipeline.kdl" ,
1134
& format! (
336c953a Andy McCall 1 month ago ↶
1135
" schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" test\" {{ \n sandbox \" alpine\" \n exec \" make\" \" test\" \n output {{ \n artifact \" junit\" {{ \n path \" /src/out/junit.xml\" \n when \" always\" \n }} \n }} \n }} \n "
d29070fa Andy McCall 1 month ago ↶
1136
) ,
1137
) ;
1138
c.snapshot ( " test" ) .unwrap ( ) ;
1139
c.commit ( " Test" , & [ ] ) .unwrap ( ) ;
1140
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1141
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
1142
let a = claim ( & h.app) .await.unwrap ( ) ;
336c953a Andy McCall 1 month ago ↶
1143
assert_eq! ( a[ " job" ] [ " outputs" ] [ 0 ] [ " always" ] , true , " the runner is told to keep it from a failing job" ) ;
d29070fa Andy McCall 1 month ago ↶
1144
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
0b203bbf Andy McCall 1 month ago ↶
1145
1146
let xml = br # "<testsuites><testsuite name="pytest"><testcase classname="tests.test_io" name="test_read" time="0.5"/><testcase classname="tests.test_io" name="test_write" time="1.5"><failure message="disk full">E OSError: disk full</failure></testcase></testsuite></testsuites>"# ;
1147
let ( st, text) = upload ( & h.app, id, " junit" , xml) .await;
d29070fa Andy McCall 1 month ago ↶
1148
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
1149
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1150
let mut r = receipt ( " test" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 1 ) ;
1151
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
1152
assert_eq! (
1153
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 1 , " duration_ms" : 700 , " receipt" : r.clone ( ) } ) ) .await,
1154
StatusCode:: BAD_REQUEST ,
1155
" a failing receipt names no outputs"
1156
) ;
1157
r[ " outputs" ] = serde_json:: json! ( [ ] ) ;
1158
assert_eq! ( finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 1 , " duration_ms" : 700 , " receipt" : r } ) ) .await, StatusCode:: NO_CONTENT ) ;
1159
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
1160
assert! ( page.contains ( & format! ( " /andy/demo/flow/{} /artifacts/test/junit" , run.number) ) , " listed" ) ;
1161
assert! ( page.contains ( " not in its receipt" ) , " marked" ) ;
0b203bbf Andy McCall 1 month ago ↶
1162
assert! ( page.contains ( " 2 tests: 1 failed" ) , " counts beside the job" ) ;
1163
assert! ( page.contains ( " tests.test_io::test_write" ) & & page.contains ( " disk full" ) & & page.contains ( " OSError" ) , " the failure" ) ;
1164
assert! ( page.contains ( " tests: 2 tests: 1 failed in 2.0 s" ) , " said in the job's log" ) ;
d29070fa Andy McCall 1 month ago ↶
1165
}
1166
df940d75 Andy McCall 1 month ago ↶
1167
1168
1169
1170
# [ tokio ::test ( flavor = " multi_thread" ) ]
1171
async fn a_release_is_audited_from_authors_to_receipts ( ) {
1172
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectType} ;
1173
use oxy_core:: identity:: SecretKey;
1174
use oxy_core:: objects:: StateObject;
1175
use oxy_core:: receipt:: SignedReceipt;
1176
let h = hub ( ) .await;
1177
create ( & h.app, " andy" , " demo" ) .await;
1178
let url = format! ( " {} /andy/demo" , h.base) ;
1179
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1180
let mut c = client ( & url, work.path ( ) ) ;
1181
c.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
1182
write (
1183
work.path ( ) ,
1184
" .flow/pipeline.kdl" ,
1185
& format! (
1186
" schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n output {{ \n artifact \" tool\" {{ \n path \" /src/out/tool\" \n }} \n }} \n }} \n "
1187
) ,
1188
) ;
1189
c.snapshot ( " test" ) .unwrap ( ) ;
1190
c.commit ( " Build a tool" , & [ ] ) .unwrap ( ) ;
1191
let heads = c.bookmark_heads ( ) .unwrap ( ) ;
1192
c.tag_create ( " v1" , & heads, " first" ) .unwrap ( ) ;
1193
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1194
1195
1196
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
1197
let a = claim ( & h.app) .await.unwrap ( ) ;
1198
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1199
let body: Vec< u8 > = b " #!/bin/sh\n echo tool\n " .to_vec ( ) ;
1200
let ( _ , text) = upload ( & h.app, id, " tool" , & body) .await;
1201
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1202
let mut r = receipt ( " build" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
1203
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
1204
let heads_run: Vec< ChangeHash> = run.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
1205
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads_run) .encode ( ) ) ;
1206
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
1207
let sk = SecretKey:: generate( ) ;
1208
let runner = sk.identity ( " test-1 (OxygenFlow runner)" , " " , 1 ) ;
1209
let fr: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
1210
let signed = hex:: encode( SignedReceipt:: sign( fr.to_oxy ( ) , repo_id, state, & format! ( " {url} /flow/1" ) , & runner, & sk) .encode ( ) ) ;
1211
let done = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 900 , " receipt" : r, " signed_receipt" : signed } ) ;
1212
assert_eq! ( finish ( & h.app, id, done) .await, StatusCode:: NO_CONTENT ) ;
1213
1214
1215
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
1216
let rel = db:: releases:: create( & h.app.db, row.id, " v1" , " One" , " " , false , None ) .await.unwrap ( ) ;
1217
oxygenhub:: web:: releases:: store_asset( & h.app, rel.id, " tool" , " " , & body) .await.unwrap ( ) ;
1218
oxygenhub:: web:: releases:: store_asset( & h.app, rel.id, " notes.txt" , " " , b " by hand\n " ) .await.unwrap ( ) ;
1219
1220
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/releases/trust/v1" , None , None ) .await;
1221
assert_eq! ( st, StatusCode:: OK ) ;
1222
for want in [
1223
" Tag v1 is signed" ,
1224
" The one change in the release is authenticated" ,
1225
" tool: signed receipt from run #1 build" ,
1226
" a runner key of this hub" ,
1227
" tool: built from the tagged state" ,
1228
" tool: pinned environment" ,
1229
" notes.txt: no build found" ,
1230
" <svg class=\" trust-graph\" " ,
bf5101d3 Andy McCall 1 month ago ↶
1231
1232
1233
" Tag signed by" ,
1234
" 1 of 1 changes signed or sealed" ,
1235
" 1 of 2 assets built and attested by OxygenFlow" ,
1236
" attested build" ,
1237
" uploaded by hand" ,
1238
" receipt verified" ,
1239
" class=\" shackle\" " ,
1240
" <title>notes.txt:" ,
df940d75 Andy McCall 1 month ago ↶
1241
] {
1242
assert! ( page.contains ( want) , " missing {want:?}" ) ;
1243
}
bf5101d3 Andy McCall 1 month ago ↶
1244
let svg = & page[ page.find ( " <svg class=\" trust-graph\" " ) .unwrap ( ) .. ] ;
1245
assert! ( svg[ .. 200 ] .contains ( " width=\" " ) & & svg[ .. 200 ] .contains ( " height=\" " ) , " the graph has its own size: {}" , & svg[ .. 200 ] ) ;
df940d75 Andy McCall 1 month ago ↶
1246
let ( _ , releases) = call ( & h.app, " GET" , " /andy/demo/releases" , None , None ) .await;
1247
assert! ( releases.contains ( " /andy/demo/releases/trust/v1" ) , " the release links its audit" ) ;
1248
1249
1250
let ( st, json) = call ( & h.app, " GET" , " /andy/demo/releases/trust/v1?format=json" , None , None ) .await;
1251
assert_eq! ( st, StatusCode:: OK ) ;
1252
let t: serde_json:: Value = serde_json:: from_str( & json) .unwrap ( ) ;
1253
assert_eq! ( ( t[ " tag" ] .as_str ( ) , t[ " tag_signature" ] .as_str ( ) , t[ " changes" ] .as_i64 ( ) ) , ( Some ( " v1" ) , Some ( " valid" ) , Some ( 1 ) ) ) ;
1254
assert_eq! ( t[ " authors" ] [ 0 ] [ " who" ] [ " name" ] , " Andy" ) ;
1255
let tool = t[ " assets" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | a | a[ " name" ] = = " tool" ) .unwrap ( ) ;
1256
let hex_receipt = tool[ " build" ] [ " signed_receipt" ] .as_str ( ) .unwrap ( ) ;
1257
let sr = SignedReceipt:: decode( & hex:: decode( hex_receipt) .unwrap ( ) ) .unwrap ( ) ;
1258
sr.verify ( ) .unwrap ( ) ;
1259
assert_eq! ( sr.receipt.outputs[ 0 ] .to_hex ( ) , blob) ;
1260
let audit = t[ " audit" ] .as_array ( ) .unwrap ( ) ;
1261
assert! ( audit.iter ( ) .any ( | c | c[ " verdict" ] = = " warn" & & c[ " title" ] .as_str ( ) .unwrap ( ) .starts_with ( " notes.txt" ) ) ) ;
1262
assert! ( ! audit.iter ( ) .any ( | c | c[ " verdict" ] = = " fail" ) , " {audit:?}" ) ;
1263
1264
let ( st, api) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/releases/trust/v1" , None , None ) .await;
1265
assert_eq! ( st, StatusCode:: OK ) ;
1266
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & api) .unwrap ( ) [ " assets" ] .as_array ( ) .unwrap ( ) .len ( ) , 2 ) ;
1267
assert_eq! ( call ( & h.app, " GET" , " /api/v1/repos/andy/demo/releases/trust/v9" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
1268
assert_eq! ( call ( & h.app, " GET" , " /andy/demo/releases/trust/v9" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
1269
}
b6b7e4cf Andy McCall 1 month ago ↶
1270
1271
1272
1273
1274
# [ tokio ::test ( flavor = " multi_thread" ) ]
1275
async fn runners_register_show_their_history_and_are_drained_and_removed ( ) {
1276
use oxy_core:: identity:: SecretKey;
1277
let h = hub ( ) .await;
1278
create ( & h.app, " andy" , " demo" ) .await;
1279
1280
let res = oxygenhub:: router( h.app.clone ( ) )
1281
.oneshot (
1282
Request:: post( " /register" )
1283
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
1284
.body ( Body:: from( " username=carol&email=carol%40example.com&password=correct-horse-battery" ) )
1285
.unwrap ( ) ,
1286
)
1287
.await
1288
.unwrap ( ) ;
1289
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1290
let admin = db:: accounts:: account_by_name( & h.app.db, " carol" ) .await.unwrap ( ) .unwrap ( ) ;
1291
db:: accounts:: set_admin( & h.app.db, admin.id, true ) .await.unwrap ( ) ;
1292
let web = | method : & 'static str , path : String, body : Option< String> | {
1293
let ( app, cookie) = ( h.app.clone ( ) , cookie.clone ( ) ) ;
1294
async move {
1295
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie) ;
1296
if body.is_some ( ) {
1297
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
1298
}
1299
let res = oxygenhub:: router( app) .oneshot ( req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ) .await.unwrap ( ) ;
1300
let st = res.status ( ) ;
1301
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
1302
}
1303
} ;
1304
let csrf_of = | page : & str | {
1305
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
1306
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
1307
} ;
1308
let ( st, page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1309
assert_eq! ( st, StatusCode:: OK ) ;
1310
assert! ( page.contains ( " No runners yet" ) ) ;
1311
assert_eq! ( call ( & h.app, " GET" , " /admin/runners" , None , None ) .await.0 , StatusCode:: NOT_FOUND , " administrators only" ) ;
1312
1313
1314
let ( _ , page) = web ( " POST" , " /admin/runners/token" .into ( ) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1315
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
1316
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
1317
assert! ( token.starts_with ( " flowreg_" ) , " {token}" ) ;
1318
1319
1320
let sk = SecretKey:: generate( ) ;
1321
let identity = sk.identity ( " plex (OxygenFlow runner)" , " " , 1 ) ;
1322
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ " big-disk" ] , " max_jobs" : 2 , " memory_mib" : 16384 , " cpus" : 4 } ) ;
1323
let reg = serde_json:: json! ( { " token" : token, " name" : " plex" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1324
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1325
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1326
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1327
assert! ( credential.starts_with ( " flowrunner_" ) ) ;
1328
assert_eq! (
1329
call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await.0 ,
1330
StatusCode:: FORBIDDEN ,
1331
" used once"
1332
) ;
1333
1334
1335
let claim_as = | who : String| {
1336
let app = h.app.clone ( ) ;
1337
let info = info.clone ( ) ;
1338
async move {
1339
let body = serde_json:: json! ( { " runner" : " ignored" , " runtimes" : [ " microvm" ] , " info" : info } ) .to_string ( ) ;
1340
call ( & app, " POST" , " /api/v1/flow/claim" , Some ( & who) , Some ( body) ) .await
1341
}
1342
} ;
1343
assert_eq! ( claim_as ( credential.clone ( ) ) .await.0 , StatusCode:: NO_CONTENT , " nothing queued" ) ;
1344
assert_eq! ( claim ( & h.app) .await, None ) ;
1345
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1346
assert! (
1347
page.contains ( " >plex</strong>" ) & & page.contains ( " st-online" ) & & page.contains ( " x86_64 · linux" ) & & page.contains ( " big-disk" ) ,
1348
" {page}"
1349
) ;
1350
assert! ( page.contains ( " >test-1</strong>" ) & & page.contains ( " hub.toml" ) , " the configured runner, by its claimed name" ) ;
1351
let plex = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " plex" ) .unwrap ( ) ;
1352
assert_eq! ( plex.key, identity.hash ( ) .to_hex ( ) , " bound at registration" ) ;
1353
1354
1355
let url = format! ( " {} /andy/demo" , h.base) ;
1356
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1357
let c = client ( & url, work.path ( ) ) ;
1358
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1359
c.snapshot ( " test" ) .unwrap ( ) ;
1360
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1361
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1362
runs_of ( & h.app, " andy" , " demo" ) .await;
1363
let ( st, body) = claim_as ( credential.clone ( ) ) .await;
1364
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1365
let job: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1366
let ( _ , page) = web ( " GET" , format! ( " /admin/runners/{} " , plex.id) , None ) .await;
1367
assert! (
1368
page.contains ( " andy/demo" ) & & page.contains ( job[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) ) & & page.contains ( " Running now</dt><dd>1 job" ) ,
1369
" {page}"
1370
) ;
b66e70b7 Andy McCall 1 month ago ↶
1371
1372
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1373
assert! ( page.contains ( " <h2>Queue</h2>" ) & & page.contains ( " <td>plex</td>" ) , " {page}" ) ;
1374
let ( _ , tokens) = web ( " GET" , " /settings/tokens" .into ( ) , None ) .await;
1375
let ( _ , made) = web ( " POST" , " /settings/tokens" .into ( ) , Some ( format! ( " _csrf={} &name=admin&scopes=user%3Aread" , csrf_of ( & tokens) ) ) ) .await;
1376
let i = made.find ( " oxyhub_" ) .unwrap ( ) ;
1377
let admin_token: String = made[ i.. ] .chars ( ) .take_while ( | c | c.is_ascii_alphanumeric ( ) | | * c = = ' _' ) .collect ( ) ;
1378
let ( st, body) = call ( & h.app, " GET" , " /api/v1/runners" , Some ( & admin_token) , None ) .await;
1379
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1380
let listed: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1381
let plex_row = listed.as_array ( ) .unwrap ( ) .iter ( ) .find ( | r | r[ " name" ] = = " plex" ) .unwrap ( ) ;
1382
assert_eq! ( ( plex_row[ " running" ] .as_i64 ( ) , plex_row[ " arch" ] .as_str ( ) ) , ( Some ( 1 ) , Some ( " x86_64" ) ) ) ;
7f5f6c05 Andy McCall 1 month ago ↶
1383
1384
let ( _ , page) = web ( " GET" , format! ( " /admin/runners/{} " , plex.id) , None ) .await;
1385
let ( st, _ ) = web (
1386
" POST" ,
1387
format! ( " /admin/runners/{} /limits" , plex.id) ,
1388
Some ( format! ( " _csrf={} &labels=gpu%2C+plex&timeout_minutes=5" , csrf_of ( & page) ) ) ,
1389
)
1390
.await;
1391
assert_eq! ( st, StatusCode:: SEE_OTHER ) ;
1392
let ( _ , page) = web ( " GET" , format! ( " /admin/runners/{} " , plex.id) , None ) .await;
1393
assert! ( page.contains ( " >gpu<" ) & & page.contains ( " 5 min for its jobs" ) , " {page}" ) ;
1394
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/runners" , Some ( & admin_token) , None ) .await;
1395
let listed: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1396
let plex_row = listed.as_array ( ) .unwrap ( ) .iter ( ) .find ( | r | r[ " name" ] = = " plex" ) .unwrap ( ) ;
1397
assert! ( plex_row[ " labels" ] .as_array ( ) .unwrap ( ) .iter ( ) .any ( | l | l = = " gpu" ) , " {plex_row}" ) ;
1398
let ( st, _ ) = web (
1399
" POST" ,
1400
format! ( " /admin/runners/{} /limits" , plex.id) ,
1401
Some ( format! ( " _csrf={} &labels=bad%20label%21&timeout_minutes=" , csrf_of ( & page) ) ) ,
1402
)
1403
.await;
1404
assert_eq! ( st, StatusCode:: BAD_REQUEST , " a label with a space or punctuation is refused" ) ;
b66e70b7 Andy McCall 1 month ago ↶
1405
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/runners/queue" , Some ( & admin_token) , None ) .await;
1406
let queue: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1407
assert! ( queue.as_array ( ) .unwrap ( ) .iter ( ) .any ( | q | q[ " status" ] = = " running" & & q[ " runner" ] = = " plex" ) , " {queue}" ) ;
1408
let ( _ , body) = call ( & h.app, " GET" , & format! ( " /api/v1/runners/{} /jobs" , plex.id) , Some ( & admin_token) , None ) .await;
1409
let jobs: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1410
assert_eq! ( jobs[ 0 ] [ " repo" ] , " andy/demo" , " {jobs}" ) ;
1411
assert_eq! ( call ( & h.app, " GET" , " /api/v1/runners" , None , None ) .await.0 , StatusCode:: NOT_FOUND , " administrators only" ) ;
b6b7e4cf Andy McCall 1 month ago ↶
1412
1413
1414
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1415
let csrf = csrf_of ( & page) ;
1416
web ( " POST" , format! ( " /admin/runners/{} /state" , plex.id) , Some ( format! ( " _csrf={csrf} &state=draining" ) ) ) .await;
1417
assert_eq! ( claim_as ( credential.clone ( ) ) .await.0 , StatusCode:: NO_CONTENT , " a draining runner takes nothing" ) ;
1418
let ( _ , page) = web ( " POST" , format! ( " /admin/runners/{} /remove" , plex.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1419
assert! ( page.contains ( " is running jobs: drain it first" ) , " {page}" ) ;
1420
1421
let id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1422
let done = call (
1423
& h.app,
1424
" POST" ,
1425
& format! ( " /api/v1/flow/jobs/{id} /finish" ) ,
1426
Some ( & credential) ,
1427
Some ( r # "{"exit_code":0,"duration_ms":5}"# .into ( ) ) ,
1428
)
1429
.await;
1430
assert_eq! ( done.0 , StatusCode:: NO_CONTENT ) ;
1431
web ( " POST" , format! ( " /admin/runners/{} /state" , plex.id) , Some ( format! ( " _csrf={csrf} &state=disabled" ) ) ) .await;
1432
assert_eq! ( claim_as ( credential.clone ( ) ) .await.0 , StatusCode:: FORBIDDEN ) ;
1433
let ( _ , page) = web ( " POST" , format! ( " /admin/runners/{} /remove" , plex.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1434
assert! ( page.contains ( " Removed plex." ) , " {page}" ) ;
1435
assert_eq! ( claim_as ( credential) .await.0 , StatusCode:: UNAUTHORIZED ) ;
1436
}
74be9796 Andy McCall 1 month ago ↶
1437
1438
1439
1440
# [ tokio ::test ( flavor = " multi_thread" ) ]
1441
async fn scoped_runners_take_only_their_scope ( ) {
1442
use oxy_core:: identity:: SecretKey;
1443
let h = hub ( ) .await;
1444
create ( & h.app, " andy" , " demo" ) .await;
1445
create ( & h.app, " andy" , " other" ) .await;
1446
let register = | scope : db:: runners:: Scope, name : & 'static str | {
1447
let app = h.app.clone ( ) ;
1448
async move {
1449
let token = format! ( " flowreg_{name} " ) ;
1450
db:: runners:: add_registration( & app.db, & blake3:: hash( token.as_bytes ( ) ) .to_hex ( ) , " admin" , & scope) .await.unwrap ( ) ;
1451
let identity = SecretKey:: generate( ) .identity ( name, " " , 1 ) ;
1452
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
1453
let reg = serde_json:: json! ( { " token" : token, " name" : name, " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1454
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1455
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1456
serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( )
1457
}
1458
} ;
1459
let demo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
1460
let only_demo = register ( db:: runners:: Scope { owner: " andy" .into ( ) , repo_id: Some ( demo.id) } , " demo-runner" ) .await;
1461
let carols = register ( db:: runners:: Scope { owner: " carol" .into ( ) , repo_id: None } , " carol-runner" ) .await;
1462
let andys = register ( db:: runners:: Scope { owner: " andy" .into ( ) , repo_id: None } , " andy-runner" ) .await;
1463
let claim_as = | who : String| {
1464
let app = h.app.clone ( ) ;
1465
async move {
1466
let body = serde_json:: json! ( { " runner" : " x" , " runtimes" : [ " microvm" ] } ) .to_string ( ) ;
1467
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( & who) , Some ( body) ) .await;
1468
( st = = StatusCode:: OK ) .then ( | | serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " repo" ] .as_str ( ) .unwrap ( ) .to_string ( ) )
1469
}
1470
} ;
1471
1472
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1473
let c = client ( & format! ( " {} /andy/other" , h.base) , work.path ( ) ) ;
1474
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1475
c.snapshot ( " t" ) .unwrap ( ) ;
1476
c.commit ( " pipeline" , & [ ] ) .unwrap ( ) ;
1477
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1478
assert! ( ! runs_of ( & h.app, " andy" , " other" ) .await.is_empty ( ) ) ;
1479
assert_eq! ( claim_as ( only_demo.clone ( ) ) .await, None , " nothing of andy/demo is queued" ) ;
1480
assert_eq! ( claim_as ( carols) .await, None , " nothing of carol's" ) ;
1481
assert_eq! ( claim_as ( andys) .await.as_deref ( ) , Some ( " andy/other" ) , " the owner's runner takes its repositories' jobs" ) ;
1482
1483
assert_eq! ( claim ( & h.app) .await.unwrap ( ) [ " repo" ] , " andy/other" ) ;
1484
assert_eq! ( claim_as ( only_demo) .await, None , " still nothing of its own" ) ;
1485
}
7974adb0 Andy McCall 1 month ago ↶
1486
1487
1488
1489
1490
# [ tokio ::test ( flavor = " multi_thread" ) ]
1491
async fn repository_admins_add_and_remove_their_own_runners ( ) {
1492
use oxy_core:: identity:: SecretKey;
1493
let h = hub ( ) .await;
1494
let res = oxygenhub:: router( h.app.clone ( ) )
1495
.oneshot (
1496
Request:: post( " /register" )
1497
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
1498
.body ( Body:: from( " username=andy&email=andy%40example.com&password=correct-horse-battery" ) )
1499
.unwrap ( ) ,
1500
)
1501
.await
1502
.unwrap ( ) ;
1503
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1504
create ( & h.app, " andy" , " demo" ) .await;
1505
let demo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
1506
let web = | method : & 'static str , path : String, body : Option< String> | {
1507
let ( app, cookie) = ( h.app.clone ( ) , cookie.clone ( ) ) ;
1508
async move {
1509
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie) ;
1510
if body.is_some ( ) {
1511
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
1512
}
1513
let res = oxygenhub:: router( app) .oneshot ( req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ) .await.unwrap ( ) ;
1514
let st = res.status ( ) ;
1515
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
1516
}
1517
} ;
1518
let csrf_of = | page : & str | {
1519
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
1520
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
1521
} ;
1522
let ( st, page) = web ( " GET" , " /andy/demo/settings" .into ( ) , None ) .await;
1523
assert_eq! ( st, StatusCode:: OK ) ;
1524
assert! ( page.contains ( " <h2>Runners</h2>" ) , " the owner sees the section" ) ;
1525
let ( _ , page) = web ( " POST" , " /andy/demo/settings/runners/token" .into ( ) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1526
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
1527
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
1528
let identity = SecretKey:: generate( ) .identity ( " mine" , " " , 1 ) ;
1529
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
1530
let reg = serde_json:: json! ( { " token" : token, " name" : " mine" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1531
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1532
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1533
let mine = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " mine" ) .unwrap ( ) ;
1534
assert_eq! ( mine.scope ( ) , db:: runners:: Scope { owner: " andy" .into ( ) , repo_id: Some ( demo.id) } ) ;
1535
1536
claim ( & h.app) .await;
1537
let site = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " test-1" ) .unwrap ( ) ;
1538
let ( _ , page) = web ( " GET" , " /andy/demo/settings" .into ( ) , None ) .await;
1539
assert! ( page.contains ( " <strong>mine</strong>" ) & & page.contains ( " this repository" ) , " {page}" ) ;
1540
assert! ( page.contains ( " <strong>test-1</strong>" ) & & page.contains ( " the whole site" ) , " {page}" ) ;
1541
let csrf = csrf_of ( & page) ;
1542
let ( st, _ ) = web ( " POST" , format! ( " /andy/demo/settings/runners/{} /remove" , site.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1543
assert_eq! ( st, StatusCode:: NOT_FOUND , " not the repository's own" ) ;
1544
let ( _ , page) = web ( " POST" , format! ( " /andy/demo/settings/runners/{} /remove" , mine.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1545
assert! ( page.contains ( " mine removed" ) , " {page}" ) ;
1546
assert! ( db:: runners:: get( & h.app.db, mine.id) .await.unwrap ( ) .is_none ( ) ) ;
1547
1548
1549
let ( _ , page) = web ( " GET" , " /orgs/new" .into ( ) , None ) .await;
1550
web ( " POST" , " /orgs/new" .into ( ) , Some ( format! ( " _csrf={} &name=acme&display_name=Acme" , csrf_of ( & page) ) ) ) .await;
1551
let ( st, page) = web ( " GET" , " /orgs/acme/settings" .into ( ) , None ) .await;
1552
assert_eq! ( st, StatusCode:: OK ) ;
1553
let ( _ , page) = web ( " POST" , " /orgs/acme/settings/runners/token" .into ( ) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1554
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
1555
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
1556
let identity = SecretKey:: generate( ) .identity ( " acme-runner" , " " , 1 ) ;
1557
let reg = serde_json:: json! ( { " token" : token, " name" : " acme-runner" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1558
assert_eq! ( call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await.0 , StatusCode:: OK ) ;
1559
let acme = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " acme-runner" ) .unwrap ( ) ;
1560
assert_eq! ( acme.scope ( ) , db:: runners:: Scope { owner: " acme" .into ( ) , repo_id: None } ) ;
1561
let ( _ , page) = web ( " GET" , " /orgs/acme/settings" .into ( ) , None ) .await;
1562
assert! ( page.contains ( " <strong>acme-runner</strong>" ) & & page.contains ( " the organisation's repositories" ) , " {page}" ) ;
1563
let ( _ , page) = web ( " POST" , format! ( " /orgs/acme/settings/runners/{} /remove" , acme.id) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1564
assert! ( page.contains ( " acme-runner removed" ) , " {page}" ) ;
1565
}
c4c87f48 Andy McCall 1 month ago ↶
1566
1567
1568
1569
# [ tokio ::test ( flavor = " multi_thread" ) ]
1570
async fn jobs_that_ask_for_labels_wait_for_a_runner_with_them ( ) {
1571
use oxy_core:: identity:: SecretKey;
1572
let h = hub ( ) .await;
1573
create ( & h.app, " andy" , " demo" ) .await;
1574
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1575
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
1576
let cross = " job \" cross\" {\n sandbox \" alpine\" \n runs-on \" riscv64\" \n exec \" true\" \n }\n " ;
1577
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( cross) ) ;
1578
c.snapshot ( " t" ) .unwrap ( ) ;
1579
c.commit ( " pipeline" , & [ ] ) .unwrap ( ) ;
1580
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1581
assert! ( ! runs_of ( & h.app, " andy" , " demo" ) .await.is_empty ( ) ) ;
1582
1583
let mut taken = Vec :: new( ) ;
1584
while let Some ( j) = claim ( & h.app) .await {
1585
taken.push ( j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
1586
}
1587
assert! ( ! taken.contains ( & " cross" .to_string ( ) ) & & taken.contains ( & " lint" .to_string ( ) ) , " {taken:?}" ) ;
1588
let queue = db:: flow:: queue( & h.app.db) .await.unwrap ( ) ;
1589
let waiting = queue.iter ( ) .find ( | q | q.job = = " cross" ) .unwrap ( ) ;
1590
assert_eq! ( ( waiting.status.as_str ( ) , waiting.needs ( ) ) , ( " queued" , vec! [ " riscv64" .to_string ( ) ] ) ) ;
1591
1592
let token = " flowreg_riscv" ;
1593
db:: runners:: add_registration( & h.app.db, & blake3:: hash( token.as_bytes ( ) ) .to_hex ( ) , " admin" , & Default :: default( ) ) .await.unwrap ( ) ;
1594
let identity = SecretKey:: generate( ) .identity ( " visionfive" , " " , 1 ) ;
1595
let info = serde_json:: json! ( { " arch" : " riscv64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ " riscv64" ] , " max_jobs" : 1 } ) ;
1596
let reg = serde_json:: json! ( { " token" : token, " name" : " visionfive" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1597
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1598
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1599
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1600
let claim_body = serde_json:: json! ( { " runner" : " x" , " runtimes" : [ " microvm" ] , " info" : info } ) .to_string ( ) ;
1601
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( & credential) , Some ( claim_body) ) .await;
1602
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1603
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job" ] [ " name" ] , " cross" ) ;
1604
}
070a7b64 Andy McCall 1 month ago ↶
1605
1606
1607
1608
1609
1610
# [ tokio ::test ( flavor = " multi_thread" ) ]
1611
async fn a_job_no_runner_reports_is_ended_after_the_time_limit ( ) {
1612
let h = hub ( ) .await;
1613
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1614
create ( & h.app, " andy" , " lost" ) .await;
1615
let url = format! ( " {} /andy/lost" , h.base) ;
1616
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1617
let c = client ( & url, work.path ( ) ) ;
1618
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1619
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1620
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1621
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1622
assert_eq! ( runs_of ( & h.app, " andy" , " lost" ) .await.len ( ) , 1 ) ;
1623
let job = claim ( & h.app) .await.expect ( " a job to claim" ) ;
1624
let id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1625
1626
1627
assert_eq! ( oxygenhub:: flow:: reap_lost_jobs( & h.app) .await.unwrap ( ) , 0 ) ;
1628
1629
let old = db:: now_ms( ) - 24 * 3600 * 1000 ;
1630
sqlx:: query( " UPDATE flow_jobs SET started_ms = ? WHERE id = ?" ) .bind ( old) .bind ( id) .execute ( & h.app.db) .await.unwrap ( ) ;
1631
assert_eq! ( oxygenhub:: flow:: reap_lost_jobs( & h.app) .await.unwrap ( ) , 1 ) ;
1632
let j = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
1633
assert_eq! ( j.status, " error" ) ;
1634
assert! ( j.error.starts_with ( " lost:" ) , " {}" , j.error) ;
1635
let run = & runs_of ( & h.app, " andy" , " lost" ) .await[ 0 ] ;
1636
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
1637
assert! ( jobs.iter ( ) .all ( | j | j.status ! = " running" ) , " nothing left running" ) ;
1638
1639
assert_ne! ( finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 } ) ) .await, StatusCode:: NO_CONTENT ) ;
1640
assert_eq! ( db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .status, " error" ) ;
1641
}
b0612e7d Andy McCall 1 month ago ↶
1642
1643
1644
1645
# [ tokio ::test ( flavor = " multi_thread" ) ]
1646
async fn a_runner_is_offered_only_jobs_whose_image_it_holds ( ) {
1647
let h = hub ( ) .await;
1648
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1649
create ( & h.app, " andy" , " images" ) .await;
1650
let url = format! ( " {} /andy/images" , h.base) ;
1651
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1652
let c = client ( & url, work.path ( ) ) ;
1653
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1654
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1655
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1656
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1657
assert_eq! ( runs_of ( & h.app, " andy" , " images" ) .await.len ( ) , 1 ) ;
1658
let claim_with = | images : Vec< String> | {
1659
let body = serde_json:: json! ( { " runner" : " test-1" , " runtimes" : [ " microvm" ] , " images" : images } ) .to_string ( ) ;
1660
let app = h.app.clone ( ) ;
1661
async move {
1662
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body) ) .await;
1663
( st, body)
1664
}
1665
} ;
1666
let ( st, _ ) = claim_with ( vec! [ " ab" .repeat ( 32 ) ] ) .await;
1667
assert_eq! ( st, StatusCode:: NO_CONTENT , " no job for a runner without the image" ) ;
1668
let jobs = db:: flow:: jobs( & h.app.db, runs_of ( & h.app, " andy" , " images" ) .await[ 0 ] .id) .await.unwrap ( ) ;
1669
let rootfs: serde_json:: Value = serde_json:: from_str( & jobs[ 0 ] .sandbox) .unwrap ( ) ;
1670
let rootfs = rootfs[ " rootfs" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1671
let ( st, body) = claim_with ( vec! [ " ab" .repeat ( 32 ) , rootfs] ) .await;
1672
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1673
}
eef677c4 Andy McCall 1 month ago ↶
1674
1675
1676
1677
1678
# [ tokio ::test ( flavor = " multi_thread" ) ]
1679
async fn a_job_its_runner_no_longer_holds_is_ended_at_its_next_claim ( ) {
1680
let h = hub ( ) .await;
1681
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1682
create ( & h.app, " andy" , " held" ) .await;
1683
let url = format! ( " {} /andy/held" , h.base) ;
1684
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1685
let c = client ( & url, work.path ( ) ) ;
1686
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1687
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1688
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1689
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1690
assert_eq! ( runs_of ( & h.app, " andy" , " held" ) .await.len ( ) , 1 ) ;
1691
let id = claim ( & h.app) .await.expect ( " a job to claim" ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1692
let claim_holding = | holding : Vec< i64 > | {
1693
let body = serde_json:: json! ( { " runner" : " test-1" , " runtimes" : [ " microvm" ] , " holding" : holding } ) .to_string ( ) ;
1694
let app = h.app.clone ( ) ;
1695
async move { call ( & app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body) ) .await.0 }
1696
} ;
1697
claim_holding ( vec! [ id] ) .await;
1698
assert_eq! ( db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .status, " running" , " a held job stays" ) ;
1699
claim_holding ( vec! [ ] ) .await;
1700
let j = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
1701
assert_eq! ( j.status, " error" ) ;
1702
assert! ( j.error.starts_with ( " lost:" ) & & j.error.contains ( " no longer holds" ) , " {}" , j.error) ;
1703
let run = & runs_of ( & h.app, " andy" , " held" ) .await[ 0 ] ;
1704
assert! ( db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) .iter ( ) .all ( | j | j.status ! = " running" ) , " the run settles" ) ;
1705
}
d7ce41ac Andy McCall 1 month ago ↶
1706
1707
1708
1709
1710
# [ tokio ::test ( flavor = " multi_thread" ) ]
1711
async fn a_scoped_job_runs_only_when_a_push_touches_its_paths ( ) {
1712
let h = hub ( ) .await;
1713
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1714
create ( & h.app, " andy" , " scoped" ) .await;
1715
let url = format! ( " {} /andy/scoped" , h.base) ;
1716
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1717
let c = client ( & url, work.path ( ) ) ;
1718
let extra = " job \" dist\" {\n sandbox \" alpine\" \n scope \" Cargo.toml\" \" packaging/**\" \n exec \" true\" \n }\n " ;
1719
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
1720
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1721
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.0\" \n " ) ;
1722
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
1723
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1724
let names = | app : Shared, n : usize | async move {
1725
let mut runs = Vec :: new( ) ;
1726
for _ in 0 .. 100 {
1727
runs = runs_of ( & app, " andy" , " scoped" ) .await;
1728
if runs.len ( ) > = n {
1729
break ;
1730
}
1731
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
1732
}
1733
assert_eq! ( runs.len ( ) , n, " runs" ) ;
1734
let mut j: Vec< String> = db:: flow:: jobs( & app.db, runs[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
1735
j.sort ( ) ;
745d225d Andy McCall 1 month ago ↶
1736
1737
sqlx:: query( " UPDATE flow_runs SET status = 'passed' WHERE id = ?" ) .bind ( runs[ 0 ] .id) .execute ( & app.db) .await.unwrap ( ) ;
d7ce41ac Andy McCall 1 month ago ↶
1738
j
1739
} ;
1740
assert_eq! ( names ( h.app.clone ( ) , 1 ) .await, [ " build" , " dist" , " lint" , " test" ] , " the first run has every job" ) ;
1741
write ( work.path ( ) , " test.sh" , " echo still ok\n " ) ;
1742
c.commit ( " Edit the test" , & [ ] ) .unwrap ( ) ;
1743
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1744
assert_eq! ( names ( h.app.clone ( ) , 2 ) .await, [ " build" , " lint" , " test" ] , " dist is out of scope" ) ;
1745
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.1\" \n " ) ;
1746
c.commit ( " Release 0.1.1" , & [ ] ) .unwrap ( ) ;
1747
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1748
assert_eq! ( names ( h.app.clone ( ) , 3 ) .await, [ " build" , " dist" , " lint" , " test" ] , " a version bump runs dist" ) ;
1749
}
5dead1f4 Andy McCall 1 month ago ↶
1750
745d225d Andy McCall 1 month ago ↶
1751
1752
1753
1754
# [ tokio ::test ( flavor = " multi_thread" ) ]
1755
async fn runs_are_cancelled_by_hand_or_by_a_newer_push ( ) {
1756
let h = hub ( ) .await;
1757
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1758
create ( & h.app, " andy" , " stop" ) .await;
1759
let url = format! ( " {} /andy/stop" , h.base) ;
1760
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1761
let c = client ( & url, work.path ( ) ) ;
1762
let extra = " job \" dist\" {\n sandbox \" alpine\" \n scope \" Cargo.toml\" \n exec \" true\" \n }\n " ;
1763
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
1764
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1765
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.0\" \n " ) ;
1766
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
1767
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1768
let wait_runs = | app : Shared, n : usize | async move {
1769
for _ in 0 .. 100 {
1770
let runs = runs_of ( & app, " andy" , " stop" ) .await;
1771
if runs.len ( ) > = n {
1772
return runs;
1773
}
1774
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
1775
}
1776
panic! ( " no run {n}" ) ;
1777
} ;
1778
let first = wait_runs ( h.app.clone ( ) , 1 ) .await;
1779
sqlx:: query( " UPDATE flow_runs SET status = 'passed' WHERE id = ?" ) .bind ( first[ 0 ] .id) .execute ( & h.app.db) .await.unwrap ( ) ;
1780
sqlx:: query( " UPDATE flow_jobs SET status = 'passed' WHERE run_id = ?" ) .bind ( first[ 0 ] .id) .execute ( & h.app.db) .await.unwrap ( ) ;
1781
1782
1783
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.1\" \n " ) ;
1784
c.commit ( " Release 0.1.1" , & [ ] ) .unwrap ( ) ;
1785
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1786
wait_runs ( h.app.clone ( ) , 2 ) .await;
1787
let job = claim ( & h.app) .await.expect ( " a job of run 2" ) ;
1788
let id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1789
write ( work.path ( ) , " test.sh" , " echo still ok\n " ) ;
1790
c.commit ( " Edit the test" , & [ ] ) .unwrap ( ) ;
1791
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1792
let runs = wait_runs ( h.app.clone ( ) , 3 ) .await;
1793
let third = & runs[ 0 ] ;
1794
let mut second = runs[ 1 ] .clone ( ) ;
1795
for _ in 0 .. 100 {
1796
second = db:: flow:: run_by_id( & h.app.db, runs[ 1 ] .id) .await.unwrap ( ) .unwrap ( ) ;
1797
if ! second.live ( ) {
1798
break ;
1799
}
1800
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
1801
}
1802
assert_eq! ( second.status, " cancelled" , " a newer push supersedes a run still going" ) ;
1803
let jobs = db:: flow:: jobs( & h.app.db, second.id) .await.unwrap ( ) ;
1804
assert! ( jobs.iter ( ) .all ( | j | j.status = = " cancelled" & & j.error = = " superseded by run #3" ) , " {jobs:?}" ) ;
1805
let mut names: Vec< String> = db:: flow:: jobs( & h.app.db, third.id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
1806
names.sort ( ) ;
1807
assert_eq! ( names, [ " build" , " dist" , " lint" , " test" ] , " run 3 also checks what run 2 would have: the version bump" ) ;
1808
let mut checks = vec! [ ] ;
1809
for _ in 0 .. 100 {
1810
checks = db:: hooks:: checks_for_state( & h.app.db, second.repo_id, & second.state) .await.unwrap ( ) ;
1811
if checks.iter ( ) .all ( | c | c.status = = " cancelled" ) {
1812
break ;
1813
}
1814
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
1815
}
1816
assert! ( checks.iter ( ) .all ( | c | c.status = = " cancelled" ) , " {checks:?}" ) ;
1817
1818
let ( st, _ ) =
1819
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( r # "{"records":[]}"# .into ( ) ) ) .await;
1820
assert_eq! ( st, StatusCode:: CONFLICT ) ;
1821
1822
1823
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/stop/flow/runs/3/cancel" , Some ( " site" ) , None ) .await;
1824
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1825
let third = db:: flow:: run_by_id( & h.app.db, third.id) .await.unwrap ( ) .unwrap ( ) ;
1826
assert_eq! ( third.status, " cancelled" ) ;
1827
assert! ( db:: flow:: jobs( & h.app.db, third.id) .await.unwrap ( ) .iter ( ) .all ( | j | j.error = = " cancelled by andy" ) ) ;
1828
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/stop/flow/runs/3/cancel" , Some ( " site" ) , None ) .await;
1829
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " already cancelled" ) , " {st} {body}" ) ;
1830
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/stop/flow/runs/3/cancel" , None , None ) .await;
1831
assert_ne! ( st, StatusCode:: OK , " anonymous callers cannot cancel" ) ;
1832
let ( _ , page) = call ( & h.app, " GET" , " /andy/stop/flow/3" , None , None ) .await;
1833
assert! ( page.contains ( " ⊘" ) & & ! page.contains ( " CANCEL</button>" ) , " a cancelled run shows so, with no button" ) ;
1834
}
1835
5dead1f4 Andy McCall 1 month ago ↶
1836
1837
1838
1839
# [ tokio ::test ( flavor = " multi_thread" ) ]
1840
async fn a_job_runs_under_its_own_time_limit_within_the_sites ( ) {
1841
let h = hub ( ) .await;
1842
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1843
db:: accounts:: set_setting( & h.app.db, oxygenhub:: flow:: SETTING_DEFAULT , " 30" ) .await.unwrap ( ) ;
1844
db:: accounts:: set_setting( & h.app.db, oxygenhub:: flow:: SETTING_MAX , " 120" ) .await.unwrap ( ) ;
1845
create ( & h.app, " andy" , " limits" ) .await;
1846
let url = format! ( " {} /andy/limits" , h.base) ;
1847
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1848
let c = client ( & url, work.path ( ) ) ;
1849
let extra = " job \" slow\" {\n sandbox \" alpine\" \n timeout \" 90m\" \n exec \" true\" \n }\n job \" huge\" {\n sandbox \" alpine\" \n timeout \" 5h\" \n exec \" true\" \n }\n " ;
1850
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
1851
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1852
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1853
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1854
assert_eq! ( runs_of ( & h.app, " andy" , " limits" ) .await.len ( ) , 1 ) ;
1855
let mut limits = std:: collections:: HashMap:: new( ) ;
1856
let mut ids = std:: collections:: HashMap:: new( ) ;
1857
while let Some ( j) = claim ( & h.app) .await {
1858
let name = j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1859
limits.insert ( name.clone ( ) , j[ " timeout_s" ] .as_u64 ( ) .unwrap ( ) ) ;
1860
ids.insert ( name, j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ) ;
1861
}
1862
assert_eq! ( limits[ " slow" ] , 5400 , " its own" ) ;
1863
assert_eq! ( limits[ " huge" ] , 7200 , " cut to the site's maximum" ) ;
1864
assert_eq! ( limits[ " build" ] , 1800 , " the site's default" ) ;
1865
1866
let started = db:: now_ms( ) - 40 * 60 * 1000 ;
1867
sqlx:: query( " UPDATE flow_jobs SET started_ms = ?" ) .bind ( started) .execute ( & h.app.db) .await.unwrap ( ) ;
1868
oxygenhub:: flow:: reap_lost_jobs( & h.app) .await.unwrap ( ) ;
1869
let status = | id : i64 | {
1870
let app = h.app.clone ( ) ;
1871
async move { db:: flow:: job( & app.db, id) .await.unwrap ( ) .unwrap ( ) .status }
1872
} ;
1873
assert_eq! ( status ( ids[ " build" ] ) .await, " error" , " past its 30 minutes" ) ;
1874
assert_eq! ( status ( ids[ " slow" ] ) .await, " running" , " within its 90" ) ;
1875
assert_eq! ( status ( ids[ " huge" ] ) .await, " running" , " within its 120" ) ;
1876
}
c889911b Andy McCall 1 month ago ↶
1877
1878
1879
1880
1881
# [ tokio ::test ( flavor = " multi_thread" ) ]
1882
async fn on_decides_which_events_start_a_job ( ) {
1883
let h = hub ( ) .await;
1884
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1885
create ( & h.app, " andy" , " events" ) .await;
1886
let url = format! ( " {} /andy/events" , h.base) ;
1887
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1888
let c = client ( & url, work.path ( ) ) ;
1889
let text = format! (
1890
" on {{ \n push \" main\" \n manual\n }} \n {} job \" dist\" {{ \n sandbox \" alpine\" \n on {{ \n tag \" v*\" \n }} \n exec \" true\" \n }} \n " ,
1891
pipeline ( " " )
1892
) ;
1893
1894
let text = text.replacen (
1895
" on {\n push \" main\" \n manual\n }\n schema-version 1\n " ,
1896
" schema-version 1\n on {\n push \" main\" \n manual\n }\n " ,
1897
1 ,
1898
) ;
1899
write ( work.path ( ) , " .flow/pipeline.kdl" , & text) ;
1900
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1901
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
1902
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1903
let jobs_of_latest = | app : Shared, n : usize | async move {
1904
let mut runs = Vec :: new( ) ;
1905
for _ in 0 .. 100 {
1906
runs = runs_of ( & app, " andy" , " events" ) .await;
1907
if runs.len ( ) > = n {
1908
break ;
1909
}
1910
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
1911
}
1912
assert_eq! ( runs.len ( ) , n, " runs" ) ;
1913
let mut j: Vec< String> = db:: flow:: jobs( & app.db, runs[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
1914
j.sort ( ) ;
1915
( runs[ 0 ] .clone ( ) , j)
1916
} ;
1917
let ( _ , j) = jobs_of_latest ( h.app.clone ( ) , 1 ) .await;
1918
assert_eq! ( j, [ " build" , " lint" , " test" ] , " a push to main: no dist" ) ;
1919
1920
let heads = c.bookmark_heads ( ) .unwrap ( ) ;
1921
c.tag_create ( " v1.0.0" , & heads, " first" ) .unwrap ( ) ;
1922
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1923
let ( run, j) = jobs_of_latest ( h.app.clone ( ) , 2 ) .await;
1924
assert_eq! ( j, [ " dist" ] , " a tag runs only what names it" ) ;
1925
assert_eq! ( run.trigger, " tag:v1.0.0" ) ;
1926
1927
let row = db:: repos:: get( & h.app.db, " andy" , " events" ) .await.unwrap ( ) .unwrap ( ) ;
1928
db:: flow:: finish(
1929
& h.app.db,
1930
db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) [ 0 ] .id,
1931
db:: flow:: Outcome { status: " passed" , exit_code: Some ( 0 ) , error: " " , receipt: " " , receipt_hash: " " } ,
1932
)
1933
.await
1934
.unwrap ( ) ;
1935
let run = db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) ;
1936
oxygenhub:: flow:: rerun( & h.app, & row, & run, " andy" ) .await.unwrap ( ) ;
1937
let ( _ , j) = jobs_of_latest ( h.app.clone ( ) , 3 ) .await;
1938
assert_eq! ( j, [ " dist" ] , " the rerun is the tag's run again" ) ;
1939
1940
c.bookmark_create ( " develop" , None ) .unwrap ( ) ;
1941
assert! ( c.push_bookmark ( " origin" , " develop" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1942
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
1943
assert_eq! ( runs_of ( & h.app, " andy" , " events" ) .await.len ( ) , 3 , " no run for develop" ) ;
348f8c4b Andy McCall 1 month ago ↶
1944
}
1945
1946
1947
1948
# [ tokio ::test ( flavor = " multi_thread" ) ]
1949
async fn scheduled_jobs_run_when_due ( ) {
1950
let h = hub ( ) .await;
1951
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1952
create ( & h.app, " andy" , " nightly" ) .await;
1953
let url = format! ( " {} /andy/nightly" , h.base) ;
1954
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1955
let c = client ( & url, work.path ( ) ) ;
1956
1957
let extra = " job \" nightly\" {\n sandbox \" alpine\" \n on {\n schedule \" 17 4 29 2 *\" \n }\n exec \" true\" \n }\n " ;
1958
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
1959
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1960
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
1961
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1962
assert_eq! ( runs_of ( & h.app, " andy" , " nightly" ) .await.len ( ) , 1 ) ;
1963
let first = runs_of ( & h.app, " andy" , " nightly" ) .await;
1964
let names: Vec< String> = db:: flow:: jobs( & h.app.db, first[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
1965
assert! ( ! names.contains ( & " nightly" .to_string ( ) ) , " a push does not run a scheduled job" ) ;
1966
use flow_core:: pipeline:: When;
1967
let not_yet = When { minute: 16 , hour: 4 , day: 29 , month: 2 , weekday: 4 } ;
1968
assert_eq! ( oxygenhub:: flow:: run_due( & h.app, not_yet) .await, 0 ) ;
1969
let due = When { minute: 17 , .. not_yet } ;
1970
assert_eq! ( oxygenhub:: flow:: run_due( & h.app, due) .await, 1 ) ;
1971
let runs = runs_of ( & h.app, " andy" , " nightly" ) .await;
1972
assert_eq! ( runs.len ( ) , 2 ) ;
1973
assert_eq! ( runs[ 0 ] .trigger, " schedule" ) ;
1974
let names: Vec< String> = db:: flow:: jobs( & h.app.db, runs[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
1975
assert_eq! ( names, [ " nightly" ] ) ;
1976
assert_eq! ( runs[ 0 ] .bookmark, " main" , " on the default bookmark" ) ;
c889911b Andy McCall 1 month ago ↶
1977
}
2a83f530 Andy McCall 1 month ago ↶
1978
1979
# [ tokio ::test ( flavor = " multi_thread" ) ]
1980
async fn variables_from_three_levels_reach_a_job_inner_overriding_outer ( ) {
1981
use oxygenhub:: levels:: Scope;
1982
use oxygenhub:: variables;
1983
let h = hub ( ) .await;
1984
let app = & h.app;
1985
1986
let res = oxygenhub:: router( app.clone ( ) )
1987
.oneshot (
1988
Request:: post( " /register" )
1989
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
1990
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery" ) )
1991
.unwrap ( ) ,
1992
)
1993
.await
1994
.unwrap ( ) ;
1995
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1996
let andy = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
1997
db:: accounts:: create_org( & app.db, " acme" , " Acme" , andy.id) .await.unwrap ( ) ;
1998
create ( app, " acme" , " app" ) .await;
1999
let page = | path : & str , form : Option< String> | {
2000
let mut req =
2001
Request:: builder( ) .method ( if form.is_some ( ) { " POST" } else { " GET" } ) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
2002
if form.is_some ( ) {
2003
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
2004
}
2005
let req = req.body ( Body:: from( form.unwrap_or_default ( ) ) ) .unwrap ( ) ;
2006
let app = app.clone ( ) ;
2007
async move {
2008
let res = oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) ;
2009
let status = res.status ( ) ;
2010
let body = String :: from_utf8( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ) .unwrap ( ) ;
2011
( status, body)
2012
}
2013
} ;
2014
let csrf_of = | page : & str | {
2015
let i = page.find ( " name=\" _csrf\" value=\" " ) .expect ( " csrf field" ) + " name=\" _csrf\" value=\" " .len ( ) ;
2016
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
2017
} ;
2018
2019
2020
2021
variables:: set( & app.db, Scope:: Site, " CC" , " gcc" ) .await.unwrap ( ) ;
2022
variables:: set( & app.db, Scope:: Site, " LANG" , " C" ) .await.unwrap ( ) ;
2023
let org = db:: accounts:: org_by_name( & app.db, " acme" ) .await.unwrap ( ) .unwrap ( ) ;
2024
variables:: set( & app.db, Scope:: Org( org.id) , " CC" , " clang" ) .await.unwrap ( ) ;
2025
let ( st, settings) = page ( " /acme/app/settings" , None ) .await;
2026
assert_eq! ( st, StatusCode:: OK , " {settings}" ) ;
2027
assert! ( settings.contains ( " Inherited" ) & & settings.contains ( " the acme organisation" ) & & settings.contains ( " overridden" ) , " {settings}" ) ;
2028
let csrf = csrf_of ( & settings) ;
2029
let ( _ , body) = page ( " /acme/app/settings/variables" , Some ( format! ( " _csrf={csrf} &action=set&name=RUST_LOG&value=debug" ) ) ) .await;
2030
assert! ( body.contains ( " RUST_LOG set." ) & & body.contains ( " <code>debug</code>" ) , " {body}" ) ;
2031
let ( _ , body) = page ( " /acme/app/settings/variables" , Some ( format! ( " _csrf={csrf} &action=set&name=FLOW_REPO&value=x" ) ) ) .await;
2032
assert! ( body.contains ( " not FLOW_" ) , " {body}" ) ;
2033
let repo = db:: repos:: get( & app.db, " acme" , " app" ) .await.unwrap ( ) .unwrap ( ) ;
2034
db:: repos:: set_flow( & app.db, repo.id, " on" ) .await.unwrap ( ) ;
2035
let env = variables:: env_for_repo( & app.db, & repo) .await.unwrap ( ) ;
2036
assert_eq! (
2037
env.iter ( ) .map ( | ( k , v ) | ( k.as_str ( ) , v.as_str ( ) ) ) .collect:: < Vec< _ > > ( ) ,
2038
[ ( " CC" , " clang" ) , ( " LANG" , " C" ) , ( " RUST_LOG" , " debug" ) ] ,
2039
" the organisation's CC wins over the site's"
2040
) ;
2041
2042
2043
let url = format! ( " {} /acme/app" , h.base) ;
2044
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2045
let c = client ( & url, work.path ( ) ) ;
2046
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
2047
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2048
c.snapshot ( " test" ) .unwrap ( ) ;
2049
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2050
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2051
let runs = runs_of ( app, " acme" , " app" ) .await;
2052
assert_eq! ( runs.len ( ) , 1 , " {runs:?}" ) ;
2053
let got = claim ( app) .await.unwrap_or_else ( | | panic! ( " a job: {:?}" , runs[ 0 ] ) ) ;
2054
assert_eq! ( got[ " env" ] , serde_json:: json! ( { " CC" : " clang" , " LANG" : " C" , " RUST_LOG" : " debug" } ) , " {got}" ) ;
2055
2056
2057
let ( _ , admin) = page ( " /admin" , None ) .await;
2058
assert! ( admin.contains ( " Variables" ) & & admin.contains ( " <code>gcc</code>" ) , " {admin}" ) ;
2059
let ( _ , body) = page ( " /admin/variables" , Some ( format! ( " _csrf={} &action=delete&name=LANG" , csrf_of ( & admin) ) ) ) .await;
2060
assert! ( body.contains ( " LANG removed." ) , " {body}" ) ;
2061
let got = claim ( app) .await.expect ( " another job" ) ;
2062
assert_eq! ( got[ " env" ] , serde_json:: json! ( { " CC" : " clang" , " RUST_LOG" : " debug" } ) , " {got}" ) ;
2063
}
4ac01a73 Andy McCall 1 month ago ↶
2064
2065
# [ tokio ::test ( flavor = " multi_thread" ) ]
2066
async fn secrets_a_job_declares_come_with_the_claim_and_never_reach_the_log ( ) {
2067
let h = hub ( ) .await;
2068
let app = & h.app;
2069
let res = oxygenhub:: router( app.clone ( ) )
2070
.oneshot (
2071
Request:: post( " /register" )
2072
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
2073
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery" ) )
2074
.unwrap ( ) ,
2075
)
2076
.await
2077
.unwrap ( ) ;
2078
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
2079
let page = | path : & str , form : Option< String> | {
2080
let mut req =
2081
Request:: builder( ) .method ( if form.is_some ( ) { " POST" } else { " GET" } ) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
2082
if form.is_some ( ) {
2083
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
2084
}
2085
let req = req.body ( Body:: from( form.unwrap_or_default ( ) ) ) .unwrap ( ) ;
2086
let app = app.clone ( ) ;
2087
async move {
2088
let res = oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) ;
2089
let status = res.status ( ) ;
2090
let body = String :: from_utf8( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ) .unwrap ( ) ;
2091
( status, body)
2092
}
2093
} ;
2094
let csrf_of = | page : & str | {
2095
let i = page.find ( " name=\" _csrf\" value=\" " ) .expect ( " csrf field" ) + " name=\" _csrf\" value=\" " .len ( ) ;
2096
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
2097
} ;
2098
create ( app, " andy" , " demo" ) .await;
2099
2100
2101
let ( _ , admin) = page ( " /admin" , None ) .await;
2102
let csrf = csrf_of ( & admin) ;
2103
let ( _ , body) = page ( " /admin/secrets" , Some ( format! ( " _csrf={csrf} &action=set&name=deploy-token&value=site-level-value" ) ) ) .await;
2104
assert! ( body.contains ( " deploy-token set." ) & & ! body.contains ( " site-level-value" ) , " {body}" ) ;
2105
let ( _ , body) = page ( " /admin/secrets" , Some ( format! ( " _csrf={csrf} &action=set&name=bad+name&value=x" ) ) ) .await;
2106
assert! ( body.contains ( " name is 1 to 64 of" ) , " {body}" ) ;
2107
let ( _ , settings) = page ( " /andy/demo/settings" , None ) .await;
2108
assert! (
2109
settings.contains ( " Secrets" ) & & settings.contains ( " <code>deploy-token</code>" ) & & settings.contains ( " the site</td>" ) ,
2110
" {settings}"
2111
) ;
2112
let csrf_r = csrf_of ( & settings) ;
2113
let ( _ , body) =
2114
page ( " /andy/demo/settings/secrets" , Some ( format! ( " _csrf={csrf_r} &action=set&name=deploy-token&value=hunter22-repo-value" ) ) ) .await;
2115
assert! ( body.contains ( " deploy-token set." ) & & body.contains ( " overridden" ) & & ! body.contains ( " hunter22" ) , " {body}" ) ;
2116
let repo = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
2117
assert! (
2118
! String :: from_utf8_lossy( & oxygenhub:: secrets:: list( & app.db, oxygenhub:: levels:: Scope:: Repo( repo.id) ) .await.unwrap ( ) [ 0 ] .value)
2119
.contains ( " hunter22" ) ,
2120
" sealed at rest"
2121
) ;
2122
2123
2124
let url = format! ( " {} /andy/demo" , h.base) ;
2125
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2126
let c = client ( & url, work.path ( ) ) ;
2127
write (
2128
work.path ( ) ,
2129
" .flow/pipeline.kdl" ,
2130
& pipeline ( " job \" deploy\" {\n sandbox \" alpine\" \n secret \" deploy-token\" {\n mount-path \" /run/secrets/token\" \n }\n secret \" missing\" {\n mount-path \" /run/secrets/missing\" \n }\n exec \" deploy\" \n }\n " ) ,
2131
) ;
2132
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2133
c.snapshot ( " test" ) .unwrap ( ) ;
2134
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2135
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2136
let runs = runs_of ( app, " andy" , " demo" ) .await;
2137
let jobs = match runs.first ( ) {
2138
Some ( r) => db:: flow:: jobs( & app.db, r.id) .await.unwrap ( ) .iter ( ) .map ( | j | format! ( " {} :{} " , j.name, j.status) ) .collect:: < Vec< _ > > ( ) ,
2139
None => vec! [ ] ,
2140
} ;
2141
let mut deploy = None ;
2142
for _ in 0 .. 50 {
2143
let Some ( got) = claim ( app) .await else {
2144
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
2145
continue ;
2146
} ;
2147
if got[ " job" ] [ " name" ] = = " deploy" {
2148
deploy = Some ( got) ;
2149
break ;
2150
}
2151
assert_eq! ( got[ " secrets" ] , serde_json:: Value:: Null, " {got}" ) ;
2152
}
2153
let got = deploy.unwrap_or_else ( | | panic! ( " the deploy job: runs {runs:?} jobs {jobs:?}" ) ) ;
2154
assert_eq! (
2155
got[ " secrets" ] ,
2156
serde_json:: json! ( [ { " name" : " deploy-token" , " path" : " /run/secrets/token" , " value" : " hunter22-repo-value" } ] ) ,
2157
" the repository's value, the missing one left out: {got}"
2158
) ;
2159
let id = got[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2160
let log = db:: flow:: log( & app.db, id, 0 , 100 ) .await.unwrap ( ) ;
2161
assert! ( log.iter ( ) .any ( | l | l.stream = = " flow" & & l.text.contains ( " secret \" missing\" is not set" ) ) , " {log:?}" ) ;
2162
2163
2164
let records = serde_json:: json! ( { " records" : [
2165
{ " t" : 1 , " stream" : " out" , " text" : " token=hunter22-repo-value" } ,
2166
{ " t" : 2 , " stream" : " err" , " text" : " b64 aHVudGVyMjItcmVwby12YWx1ZQ== end" } ,
2167
] } ) ;
2168
let ( st, _ ) = call ( app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( records.to_string ( ) ) ) .await;
2169
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
2170
let log = db:: flow:: log( & app.db, id, 0 , 100 ) .await.unwrap ( ) ;
2171
let texts: Vec< & str > = log.iter ( ) .map ( | l | l.text.as_str ( ) ) .collect ( ) ;
2172
assert! ( texts.contains ( & " token=***" ) & & texts.contains ( & " b64 *** end" ) , " {texts:?}" ) ;
2173
assert! ( ! texts.iter ( ) .any ( | t | t.contains ( " hunter22" ) ) , " {texts:?}" ) ;
2174
}
d1727c57 Andy McCall 1 month ago ↶
2175
2176
# [ tokio ::test ( flavor = " multi_thread" ) ]
2177
async fn a_job_s_identity_is_a_signed_token_the_discovery_document_lets_anyone_verify ( ) {
2178
let h = hub ( ) .await;
2179
let app = & h.app;
2180
let ( st, disc) = call ( app, " GET" , " /.well-known/openid-configuration" , None , None ) .await;
2181
assert_eq! ( st, StatusCode:: OK , " {disc}" ) ;
2182
let disc: serde_json:: Value = serde_json:: from_str( & disc) .unwrap ( ) ;
2183
assert_eq! ( disc[ " issuer" ] , h.base.as_str ( ) ) ;
2184
assert_eq! ( disc[ " jwks_uri" ] , format! ( " {} /.well-known/jwks.json" , h.base) ) ;
2185
let ( st, jwks) = call ( app, " GET" , " /.well-known/jwks.json" , None , None ) .await;
2186
assert_eq! ( st, StatusCode:: OK , " {jwks}" ) ;
2187
let jwks: serde_json:: Value = serde_json:: from_str( & jwks) .unwrap ( ) ;
2188
assert_eq! ( jwks[ " keys" ] [ 0 ] [ " alg" ] , " RS256" ) ;
2189
let kid = jwks[ " keys" ] [ 0 ] [ " kid" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
2190
2191
create ( app, " andy" , " demo" ) .await;
2192
let url = format! ( " {} /andy/demo" , h.base) ;
2193
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2194
let c = client ( & url, work.path ( ) ) ;
2195
write (
2196
work.path ( ) ,
2197
" .flow/pipeline.kdl" ,
2198
& pipeline ( " job \" deploy\" {\n sandbox \" alpine\" \n identity \" sts.amazonaws.com\" {\n mount-path \" /run/secrets/aws-token\" \n }\n exec \" deploy\" \n }\n " ) ,
2199
) ;
2200
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2201
c.snapshot ( " test" ) .unwrap ( ) ;
2202
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2203
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2204
let mut deploy = None ;
2205
for _ in 0 .. 50 {
2206
let Some ( got) = claim ( app) .await else {
2207
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
2208
continue ;
2209
} ;
2210
if got[ " job" ] [ " name" ] = = " deploy" {
2211
deploy = Some ( got) ;
2212
break ;
2213
}
2214
}
2215
let got = deploy.expect ( " the deploy job" ) ;
2216
let secrets = got[ " secrets" ] .as_array ( ) .expect ( " secrets" ) ;
2217
assert_eq! ( secrets.len ( ) , 1 ) ;
2218
assert_eq! (
2219
( secrets[ 0 ] [ " name" ] .as_str ( ) , secrets[ 0 ] [ " path" ] .as_str ( ) ) ,
2220
( Some ( " identity:sts.amazonaws.com" ) , Some ( " /run/secrets/aws-token" ) )
2221
) ;
2222
let token = secrets[ 0 ] [ " value" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
2223
let issuer = app.oidc ( ) .await.unwrap ( ) ;
2224
assert_eq! ( issuer.kid, kid, " the key the JWKS publishes signed it" ) ;
2225
let claims = issuer.verify ( & token, " sts.amazonaws.com" , db:: now_ms( ) ) .expect ( " verifies" ) ;
2226
assert_eq! ( claims[ " sub" ] , " repo:andy/demo:bookmark:main" ) ;
2227
assert_eq! ( claims[ " repository" ] , " andy/demo" ) ;
2228
assert_eq! ( claims[ " job" ] , " deploy" ) ;
2229
assert_eq! ( claims[ " run_number" ] , 1 ) ;
2230
assert_eq! ( claims[ " runner" ] , " test-1" ) ;
2231
assert_eq! ( claims[ " iss" ] , h.base.as_str ( ) ) ;
2232
assert! ( claims[ " state" ] .as_str ( ) .unwrap ( ) .len ( ) = = 64 ) ;
2233
2234
let id = got[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2235
let records = serde_json:: json! ( { " records" : [ { " t" : 1 , " stream" : " out" , " text" : format! ( " TOKEN={token} end" ) } ] } ) ;
2236
let ( st, _ ) = call ( app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( records.to_string ( ) ) ) .await;
2237
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
2238
let log = db:: flow:: log( & app.db, id, 0 , 100 ) .await.unwrap ( ) ;
2239
assert! ( log.iter ( ) .any ( | l | l.text = = " TOKEN=*** end" ) , " {log:?}" ) ;
2240
}
efd29a00 Andy McCall 1 month ago ↶
2241
2242
# [ tokio ::test ( flavor = " multi_thread" ) ]
2243
async fn jobs_are_offered_only_to_runners_of_their_architecture ( ) {
2244
let h = hub ( ) .await;
2245
let app = & h.app;
2246
create ( app, " andy" , " demo" ) .await;
2247
let url = format! ( " {} /andy/demo" , h.base) ;
2248
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2249
let c = client ( & url, work.path ( ) ) ;
2250
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " sandbox \" board\" {\n rootfs \" cas://blob/{ROOTFS}\" \n arch \" riscv64\" \n }\n job \" flash\" {\n sandbox \" board\" \n exec \" flash\" \n }\n " ) .replace ( " {ROOTFS}" , & " ab" .repeat ( 32 ) ) ) ;
2251
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2252
c.snapshot ( " test" ) .unwrap ( ) ;
2253
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2254
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2255
let claim_as = | arch : & 'static str , archs : serde_json:: Value| {
2256
let app = app.clone ( ) ;
2257
async move {
2258
let body = serde_json:: json! ( {
2259
" runner" : format! ( " {arch} -1" ) , " runtimes" : [ " microvm" ] ,
2260
" info" : { " arch" : arch, " os" : " linux" , " version" : " t" , " labels" : [ ] , " max_jobs" : 1 } ,
2261
" image_archs" : archs,
2262
} ) ;
2263
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body.to_string ( ) ) ) .await;
2264
match st {
2265
StatusCode:: NO_CONTENT => None ,
2266
StatusCode:: OK => {
2267
Some ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) )
2268
}
2269
other => panic! ( " claim: {other} {body}" ) ,
2270
}
2271
}
2272
} ;
2273
2274
2275
let rootfs = pipeline ( " " ) .split ( " cas://blob/" ) .nth ( 1 ) .unwrap ( ) [ .. 64 ] .to_string ( ) ;
2276
for _ in 0 .. 50 {
2277
if ! runs_of ( app, " andy" , " demo" ) .await.is_empty ( ) {
2278
break ;
2279
}
2280
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
2281
}
2282
let runs = runs_of ( app, " andy" , " demo" ) .await;
2283
assert_eq! ( runs.len ( ) , 1 , " {runs:?}" ) ;
2284
let mut taken = vec! [ ] ;
2285
while let Some ( j) = claim_as ( " x86_64" , serde_json:: json! ( { rootfs.clone ( ) : " x86_64" } ) ) .await {
2286
taken.push ( j) ;
2287
}
2288
assert_eq! ( taken, [ " build" , " lint" ] , " test waits for build; flash is for riscv64" ) ;
2289
2290
assert_eq! ( claim_as ( " riscv64" , serde_json:: json! ( { } ) ) .await.as_deref ( ) , Some ( " flash" ) ) ;
2291
assert_eq! ( claim_as ( " riscv64" , serde_json:: json! ( { } ) ) .await, None ) ;
2292
let ( _ , queue) = call ( app, " GET" , " /admin/runners" , None , None ) .await;
2293
let _ = queue;
2294
}
4d2e44d7 Andy McCall 1 month ago ↶
2295
2296
2297
2298
2299
2300
# [ tokio ::test ( flavor = " multi_thread" ) ]
2301
async fn a_declined_job_goes_to_another_runner_or_ends_unplaceable ( ) {
2302
let h = hub ( ) .await;
2303
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2304
create ( & h.app, " andy" , " declines" ) .await;
2305
let url = format! ( " {} /andy/declines" , h.base) ;
2306
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2307
let c = client ( & url, work.path ( ) ) ;
2308
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
2309
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2310
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2311
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2312
let run = runs_of ( & h.app, " andy" , " declines" ) .await[ 0 ] .clone ( ) ;
2313
let claim_as = | token : & 'static str , name : & 'static str | {
2314
let app = h.app.clone ( ) ;
2315
async move {
2316
let body = serde_json:: json! ( { " runner" : name, " runtimes" : [ " microvm" ] } ) .to_string ( ) ;
2317
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( token) , Some ( body) ) .await;
2318
( st = = StatusCode:: OK ) .then ( | | serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) )
2319
}
2320
} ;
2321
let decline = | token : & 'static str , id : i64 , reason : & 'static str | {
2322
let app = h.app.clone ( ) ;
2323
async move {
2324
let body = serde_json:: json! ( { " reason" : reason } ) .to_string ( ) ;
2325
call ( & app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /decline" ) , Some ( token) , Some ( body) ) .await.0
2326
}
2327
} ;
2328
let job = claim_as ( " runner-secret" , " earth-1" ) .await.expect ( " a job" ) ;
2329
2330
assert_eq! ( decline ( " runner-two" , job, " not mine" ) .await, StatusCode:: CONFLICT , " only the runner it runs on may decline it" ) ;
2331
assert_eq! ( decline ( " runner-secret" , job, " the MicroVM tier needs /dev/kvm" ) .await, StatusCode:: NO_CONTENT ) ;
2332
let j = db:: flow:: job( & h.app.db, job) .await.unwrap ( ) .unwrap ( ) ;
2333
assert_eq! ( ( j.status.as_str ( ) , j.runner.as_str ( ) ) , ( " queued" , " " ) ) ;
2334
assert_eq! ( j.waiting, " declined by earth-1: the MicroVM tier needs /dev/kvm; waiting for another runner" ) ;
2335
2336
let next = claim_as ( " runner-secret" , " earth-1" ) .await.expect ( " the other ready job" ) ;
2337
assert_ne! ( next, job) ;
2338
let page = call ( & h.app, " GET" , & format! ( " /andy/declines/flow/{} " , run.number) , None , None ) .await.1 ;
2339
assert! ( page.contains ( " Waiting: declined by earth-1: the MicroVM tier needs /dev/kvm" ) , " {page}" ) ;
2340
assert_eq! ( claim_as ( " runner-two" , " plex-1" ) .await, Some ( job) , " another runner takes it" ) ;
2341
assert_eq! ( decline ( " runner-two" , job, " rootfs 36fc4e323804 is not on this runner" ) .await, StatusCode:: NO_CONTENT ) ;
2342
assert_eq! ( claim_as ( " runner-three" , " narvi" ) .await, Some ( job) ) ;
2343
assert_eq! ( decline ( " runner-three" , job, " rootfs 36fc4e323804 is for x86_64; this runner is riscv64" ) .await, StatusCode:: NO_CONTENT ) ;
2344
let j = db:: flow:: job( & h.app.db, job) .await.unwrap ( ) .unwrap ( ) ;
2345
assert_eq! ( j.status, " error" ) ;
2346
assert_eq! (
2347
j.error,
2348
" unplaceable: declined by 3 runners: earth-1: the MicroVM tier needs /dev/kvm; plex-1: rootfs 36fc4e323804 is not on this runner; narvi: rootfs 36fc4e323804 is for x86_64; this runner is riscv64"
2349
) ;
2350
let r = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " plex-1" ) .unwrap ( ) ;
2351
let declined = db:: flow:: declines_of_runner( & h.app.db, r.id, 10 ) .await.unwrap ( ) ;
2352
assert_eq! ( declined.len ( ) , 1 ) ;
2353
assert_eq! ( ( declined[ 0 ] .job_id, declined[ 0 ] .reason.as_str ( ) ) , ( job, " rootfs 36fc4e323804 is not on this runner" ) ) ;
2354
assert_eq! ( r.tier_list ( ) , vec! [ " microvm" ] ) ;
2355
}
554ccacf Andy McCall 1 month ago ↶
2356
2357
2358
2359
2360
# [ tokio ::test ( flavor = " multi_thread" ) ]
2361
async fn a_coverage_artifact_becomes_a_coverage_report ( ) {
2362
let h = hub ( ) .await;
2363
create ( & h.app, " andy" , " cov" ) .await;
2364
let url = format! ( " {} /andy/cov" , h.base) ;
2365
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2366
let c = client ( & url, work.path ( ) ) ;
2367
write (
2368
work.path ( ) ,
2369
" .flow/pipeline.kdl" ,
2370
& format! (
2371
" schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" coverage\" {{ \n sandbox \" alpine\" \n exec \" make\" \" coverage\" \n output {{ \n artifact \" coverage\" {{ \n path \" /src/lcov.info\" \n }} \n }} \n }} \n "
2372
) ,
2373
) ;
2374
write ( work.path ( ) , " src/lib.rs" , " pub fn add(a: u32, b: u32) -> u32 {\n a + b\n }\n \n pub fn unused() {\n todo!()\n }\n " ) ;
2375
c.snapshot ( " test" ) .unwrap ( ) ;
2376
c.commit ( " Code and coverage" , & [ ] ) .unwrap ( ) ;
2377
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2378
let run = runs_of ( & h.app, " andy" , " cov" ) .await.remove ( 0 ) ;
c8df33a6 Andy McCall 26 days ago ↶
2379
assert! ( ! call ( & h.app, " GET" , " /andy/cov" , None , None ) .await.1. contains ( " >Reports</a>" ) , " no tab before a report" ) ;
554ccacf Andy McCall 1 month ago ↶
2380
let id = claim ( & h.app) .await.unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2381
2382
let ( st, text) = upload ( & h.app, id, " coverage" , b " this is not a report" ) .await;
2383
assert_eq! ( st, StatusCode:: OK , " a bad report never fails the upload: {text}" ) ;
2384
let lcov = " TN:\n SF:/src/src/lib.rs\n FN:1,add\n FN:5,unused\n FNDA:3,add\n FNDA:0,unused\n DA:1,3\n DA:2,3\n DA:5,0\n DA:6,0\n end_of_record\n \
2385
SF:/src/src/gone.rs\n DA:1,1\n end_of_record\n SF:/usr/lib/rustlib/x.rs\n DA:1,1\n end_of_record\n " ;
2386
let ( st, text) = upload ( & h.app, id, " coverage" , lcov.as_bytes ( ) ) .await;
2387
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
2388
let log: Vec< String> = db:: flow:: log( & h.app.db, id, 0 , 50 ) .await.unwrap ( ) .into_iter ( ) .map ( | l | l.text ) .collect ( ) ;
2389
assert! ( log.iter ( ) .any ( | l | l.contains ( " coverage: this is not an LCOV report" ) ) , " {log:?}" ) ;
2390
assert! (
2391
log.iter ( ) .any ( | l | l = = " coverage: 50.0% of lines (2 of 4) in 1 files (2 named files are not in this state and were left out)" ) ,
2392
" {log:?}"
2393
) ;
2394
2395
let ( st, page) = call ( & h.app, " GET" , " /andy/cov/coverage" , None , None ) .await;
2396
assert_eq! ( st, StatusCode:: OK ) ;
c987c865 Andy McCall 1 month ago ↶
2397
assert! (
c8df33a6 Andy McCall 26 days ago ↶
2398
page.contains ( " >Reports</a>" )
c987c865 Andy McCall 1 month ago ↶
2399
& & page.contains ( " 50.0%" )
2400
& & page.contains ( " <strong>2</strong> of <strong>4</strong> relevant lines covered" ) ,
2401
" {page}"
2402
) ;
554ccacf Andy McCall 1 month ago ↶
2403
assert! ( page.contains ( " 1 of 2" ) & & page.contains ( " /andy/cov/coverage/" ) & & page.contains ( " /file/src/lib.rs" ) , " {page}" ) ;
2404
let report = crate_report ( & h.app, run.id) .await;
2405
let ( st, file) = call ( & h.app, " GET" , & format! ( " /andy/cov/coverage/{report} /file/src/lib.rs" ) , None , None ) .await;
2406
assert_eq! ( st, StatusCode:: OK , " {file}" ) ;
2407
assert_eq! ( file.matches ( " class=\" cov-hit\" " ) .count ( ) , 2 , " {file}" ) ;
2408
assert_eq! ( file.matches ( " class=\" cov-miss\" " ) .count ( ) , 2 , " {file}" ) ;
2409
assert! ( file.contains ( " 3×" ) , " {file}" ) ;
2410
assert_eq! ( call ( & h.app, " GET" , & format! ( " /andy/cov/coverage/{report} /file/src/gone.rs" ) , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
2411
let ( _ , badge) = call ( & h.app, " GET" , " /andy/cov/coverage/badge.svg" , None , None ) .await;
2412
assert! ( badge.contains ( " coverage: 50.0%" ) , " {badge}" ) ;
2413
let ( _ , run_page) = call ( & h.app, " GET" , & format! ( " /andy/cov/flow/{} " , run.number) , None , None ) .await;
2414
assert! ( run_page.contains ( " coverage 50.0%" ) , " {run_page}" ) ;
617c2840 Andy McCall 1 month ago ↶
2415
2416
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/cov/coverage" , None , None ) .await;
2417
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
2418
assert_eq! ( ( list[ 0 ] [ " percent" ] .as_f64 ( ) , list[ 0 ] [ " format" ] .as_str ( ) ) , ( Some ( 50. 0 ) , Some ( " lcov" ) ) , " {list}" ) ;
2419
let ( _ , one) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/cov/coverage/{report} " ) , None , None ) .await;
2420
let one: serde_json:: Value = serde_json:: from_str( & one) .unwrap ( ) ;
2421
assert_eq! ( ( one[ " files" ] [ 0 ] [ " path" ] .as_str ( ) , one[ " files" ] [ 0 ] [ " lines_hit" ] .as_i64 ( ) ) , ( Some ( " src/lib.rs" ) , Some ( 2 ) ) , " {one}" ) ;
7edaff01 Andy McCall 1 month ago ↶
2422
2423
2424
2425
let row = db:: repos:: get( & h.app.db, " andy" , " cov" ) .await.unwrap ( ) .unwrap ( ) ;
2426
let check = | checks : Vec< db:: hooks:: Check> | checks.into_iter ( ) .find ( | c | c.name = = " coverage" ) .expect ( " a coverage check" ) ;
2427
let c = check ( db:: hooks:: checks_for_state( & h.app.db, row.id, & run.state) .await.unwrap ( ) ) ;
2428
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " passed" , " 50.0%" ) ) ;
2429
assert! ( c.url.ends_with ( & format! ( " /andy/cov/coverage?report={report} " ) ) , " {}" , c.url) ;
2430
oxygenhub:: coverage:: set_rules( & h.app.db, row.id, oxygenhub:: coverage:: Rules { min: Some ( 60. 0 ) , max_drop: None } ) .await.unwrap ( ) ;
2431
assert_eq! ( upload ( & h.app, id, " coverage" , lcov.as_bytes ( ) ) .await.0 , StatusCode:: OK ) ;
2432
let c = check ( db:: hooks:: checks_for_state( & h.app.db, row.id, & run.state) .await.unwrap ( ) ) ;
2433
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " failed" , " 50.0%: below the minimum of 60%" ) ) ;
554ccacf Andy McCall 1 month ago ↶
2434
}
2435
2436
async fn crate_report ( app : & Shared, run_id : i64 ) -> i64 {
2437
oxygenhub:: coverage:: for_run( & app.db, run_id) .await.unwrap ( ) [ 0 ] .id
2438
}
36e5a9c1 Andy McCall 1 month ago ↶
2439
2440
2441
2442
# [ tokio ::test ( flavor = " multi_thread" ) ]
2443
async fn junit_reports_make_a_tests_tab ( ) {
2444
let h = hub ( ) .await;
2445
create ( & h.app, " andy" , " t" ) .await;
2446
let url = format! ( " {} /andy/t" , h.base) ;
2447
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2448
let c = client ( & url, work.path ( ) ) ;
2449
write (
2450
work.path ( ) ,
2451
" .flow/pipeline.kdl" ,
2452
& format! (
2453
" schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" test\" {{ \n sandbox \" alpine\" \n exec \" make\" \" test\" \n output {{ \n artifact \" junit\" {{ \n path \" /src/junit.xml\" \n }} \n }} \n }} \n "
2454
) ,
2455
) ;
c8df33a6 Andy McCall 26 days ago ↶
2456
assert! ( ! call ( & h.app, " GET" , " /andy/t" , None , None ) .await.1. contains ( " >Reports</a>" ) ) ;
36e5a9c1 Andy McCall 1 month ago ↶
2457
let case = | name : & str , ok : bool , secs : & str | {
2458
if ok {
2459
format! ( r # "<testcase classname="t.suite" name="{name} " time="{secs} "/>"# )
2460
} else {
2461
format! (
2462
r # "<testcase classname="t.suite" name="{name} " time="{secs} "><failure message="{name} broke">AssertionError</failure></testcase>"#
2463
)
2464
}
2465
} ;
2466
let runs = [
2467
vec! [ case ( " a" , true , " 0.1" ) , case ( " b" , true , " 2.5" ) , case ( " c" , true , " 0.2" ) ] ,
2468
vec! [ case ( " a" , true , " 0.1" ) , case ( " b" , false , " 2.6" ) , case ( " c" , true , " 0.2" ) , case ( " d" , true , " 0.3" ) ] ,
2469
vec! [ case ( " a" , true , " 0.1" ) , case ( " b" , true , " 2.4" ) , case ( " c" , true , " 0.2" ) , case ( " d" , true , " 0.3" ) ] ,
2470
] ;
2471
for ( i, cases) in runs.iter ( ) .enumerate ( ) {
2472
write ( work.path ( ) , " n.txt" , & i.to_string ( ) ) ;
2473
c.snapshot ( " test" ) .unwrap ( ) ;
2474
c.commit ( & format! ( " Run {i} " ) , & [ ] ) .unwrap ( ) ;
2475
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2476
2477
let mut a = None ;
2478
for _ in 0 .. 100 {
2479
a = claim ( & h.app) .await;
2480
if a.is_some ( ) {
2481
break ;
2482
}
2483
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2484
}
2485
let a = a.expect ( " a job to claim" ) ;
2486
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2487
let xml = format! ( " <testsuites><testsuite name=\" t\" >{} </testsuite></testsuites>" , cases.concat ( ) ) ;
2488
assert_eq! ( upload ( & h.app, id, " junit" , xml.as_bytes ( ) ) .await.0 , StatusCode:: OK ) ;
2489
let failed = cases.iter ( ) .any ( | c | c.contains ( " <failure" ) ) ;
2490
let mut r = receipt ( " test" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , failed as u32 ) ;
2491
if ! failed {
2492
r[ " outputs" ] = serde_json:: json! ( [ oxy_core:: repo:: Repo:: blob_ref( xml.as_bytes ( ) ) .to_hex ( ) ] ) ;
2493
}
2494
let code = failed as i32 ;
2495
assert_eq! (
2496
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : code, " duration_ms" : 700 , " receipt" : r } ) ) .await,
2497
StatusCode:: NO_CONTENT
2498
) ;
2499
}
2500
let ( st, page) = call ( & h.app, " GET" , " /andy/t/tests" , None , None ) .await;
2501
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
2502
if let Some ( out) = std:: env:: var_os( " OXYHUB_DUMP_TESTS_PAGE" ) {
2503
std:: fs:: write( out, & page) .unwrap ( ) ;
2504
}
c8df33a6 Andy McCall 26 days ago ↶
2505
assert! ( page.contains ( " >Reports</a>" ) , " the tab" ) ;
36e5a9c1 Andy McCall 1 month ago ↶
2506
assert! ( page.contains ( " 100%" ) & & page.contains ( " of 4 tests passed" ) , " the latest run passes" ) ;
2507
assert! ( page.contains ( " 1 fixed" ) & & page.contains ( " t.suite::b" ) , " b fixed against the run before" ) ;
2508
assert! ( page.contains ( " 1 ✕" ) & & page.contains ( " 2 ✓" ) , " b is flaky: {page}" ) ;
2509
assert! ( page.contains ( " <svg class=\" cov-chart\" " ) , " charts" ) ;
2510
2511
let slow = page.find ( " Slowest tests" ) .unwrap ( ) ;
2512
assert! ( page[ slow.. ] .find ( " t.suite::b" ) .unwrap ( ) < page[ slow.. ] .find ( " t.suite::d" ) .unwrap ( ) ) ;
2513
2514
let row = db:: repos:: get( & h.app.db, " andy" , " t" ) .await.unwrap ( ) .unwrap ( ) ;
2515
let second = oxygenhub:: junit:: on_bookmark( & h.app.db, row.id, " main" , 10 ) .await.unwrap ( ) [ 1 ] .id;
2516
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/t/tests?report={second} " ) , None , None ) .await;
2517
assert! ( page.contains ( " 1 failing" ) & & page.contains ( " Newly failing" ) & & page.contains ( " b broke" ) , " {page}" ) ;
2518
assert! ( page.contains ( " 1 new tests" ) , " d is new" ) ;
617c2840 Andy McCall 1 month ago ↶
2519
2520
2521
2522
let ( st, list) = call ( & h.app, " GET" , " /api/v1/repos/andy/t/tests" , None , None ) .await;
2523
assert_eq! ( st, StatusCode:: OK , " {list}" ) ;
2524
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
2525
assert_eq! ( list.as_array ( ) .unwrap ( ) .len ( ) , 3 ) ;
2526
assert_eq! ( ( list[ 0 ] [ " tests" ] .as_i64 ( ) , list[ 1 ] [ " failed" ] .as_i64 ( ) ) , ( Some ( 4 ) , Some ( 1 ) ) ) ;
2527
let ( _ , one) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/t/tests/{second} ?status=failed" ) , None , None ) .await;
2528
let one: serde_json:: Value = serde_json:: from_str( & one) .unwrap ( ) ;
2529
assert_eq! ( one[ " cases" ] .as_array ( ) .unwrap ( ) .len ( ) , 1 ) ;
2530
assert_eq! ( ( one[ " cases" ] [ 0 ] [ " name" ] .as_str ( ) , one[ " cases" ] [ 0 ] [ " message" ] .as_str ( ) ) , ( Some ( " b" ) , Some ( " b broke" ) ) ) ;
2531
let ( _ , all) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/t/tests/{second} " ) , None , None ) .await;
2532
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & all) .unwrap ( ) [ " cases" ] .as_array ( ) .unwrap ( ) .len ( ) , 4 ) ;
2533
assert_eq! ( call ( & h.app, " GET" , " /api/v1/repos/andy/t/tests/99999" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
3d5c825a Andy McCall 1 month ago ↶
2534
2535
2536
let reports = oxygenhub:: junit:: on_bookmark( & h.app.db, row.id, " main" , 10 ) .await.unwrap ( ) ;
2537
let check = | state : String| {
2538
let app = h.app.clone ( ) ;
2539
async move { db:: hooks:: checks_for_state( & app.db, row.id, & state) .await.unwrap ( ) .into_iter ( ) .find ( | c | c.name = = " tests" ) .unwrap ( ) }
2540
} ;
2541
let c = check ( reports[ 1 ] .state.clone ( ) ) .await;
2542
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " failed" , " 1 failing; 1 new against main: t.suite::b" ) ) ;
2543
let c = check ( reports[ 0 ] .state.clone ( ) ) .await;
2544
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " passed" , " 4 passed" ) ) ;
2545
2546
2547
let ( _ , tab) = call ( & h.app, " GET" , " /andy/t/tests" , None , None ) .await;
2548
assert! (
2549
tab.contains ( " /andy/t/tests/case?classname=t.suite&name=b" ) | | tab.contains ( " /andy/t/tests/case?classname=t.suite&name=b" ) ,
2550
" linked"
2551
) ;
2552
let ( st, hist) = call ( & h.app, " GET" , " /andy/t/tests/case?classname=t.suite&name=b" , None , None ) .await;
2553
assert_eq! ( st, StatusCode:: OK , " {hist}" ) ;
2554
assert! ( hist.contains ( " 2 passed" ) & & hist.contains ( " 1 failed" ) & & hist.contains ( " b broke" ) , " {hist}" ) ;
2555
assert_eq! ( call ( & h.app, " GET" , " /andy/t/tests/case?classname=t.suite&name=nope" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
36e5a9c1 Andy McCall 1 month ago ↶
2556
}
67040b15 Andy McCall 26 days ago ↶
2557
2558
2559
2560
# [ tokio ::test ( flavor = " multi_thread" ) ]
2561
async fn a_job_no_runner_can_take_says_why ( ) {
2562
let h = hub ( ) .await;
2563
create ( & h.app, " andy" , " demo" ) .await;
2564
let url = format! ( " {} /andy/demo" , h.base) ;
2565
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2566
let c = client ( & url, work.path ( ) ) ;
2567
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
2568
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2569
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2570
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2571
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
2572
2573
2574
let r = db:: runners:: configured( & h.app.db, & blake3:: hash( b " elsewhere" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
2575
db:: runners:: seen( & h.app.db, r.id, None , None ) .await.unwrap ( ) ;
2576
let other = " ab" .repeat ( 32 ) ;
92a797b2 Andy McCall 26 days ago ↶
2577
db:: runners:: set_offer( & h.app.db, r.id, & [ " microvm" .to_string ( ) ] , Some ( & [ other] ) , false , " p" ) .await.unwrap ( ) ;
67040b15 Andy McCall 26 days ago ↶
2578
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
2579
2580
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
2581
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
2582
let build = v[ " jobs" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | j | j[ " name" ] = = " build" ) .unwrap ( ) .clone ( ) ;
2583
let why = build[ " waiting" ] .as_str ( ) .unwrap_or_default ( ) .to_string ( ) ;
2584
assert! ( why.contains ( " earth-9 does not hold image" ) & & why.contains ( & ROOTFS [ .. 12 ] ) , " {body}" ) ;
2585
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/1" , None , None ) .await;
2586
assert! ( page.contains ( " data-waiting" ) & & page.contains ( " does not hold image" ) , " the run page says it" ) ;
2587
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
2588
let check = checks.iter ( ) .find ( | c | c.name = = " flow/build" ) .unwrap ( ) ;
2589
assert! ( check.summary.contains ( " does not hold image" ) , " {check:?}" ) ;
2590
}
92a797b2 Andy McCall 26 days ago ↶
2591
2592
async fn put_bytes ( app : & Shared, path : & str , bearer : & str , bytes : Vec< u8 > ) -> ( StatusCode, String) {
2593
let req = Request:: put( path)
2594
.header ( header:: AUTHORIZATION , format! ( " Bearer {bearer} " ) )
2595
.header ( header:: CONTENT_TYPE , " application/octet-stream" )
2596
.body ( Body:: from( bytes) )
2597
.unwrap ( ) ;
2598
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
2599
let st = res.status ( ) ;
2600
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
2601
}
2602
2603
2604
2605
2606
2607
# [ tokio ::test ( flavor = " multi_thread" ) ]
2608
async fn images_are_sent_by_chunk_and_offered_to_runners_that_fetch ( ) {
2609
let h = hub ( ) .await;
2610
2611
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2612
2613
let mut image: Vec< u8 > = ( 0 .. 400_000 u32 ) .map ( | i | ( i % 251 ) as u8 ) .collect ( ) ;
2614
image.extend ( std:: iter:: repeat_n( 0 u8 , 2 < < 20 ) ) ;
2615
image.extend ( ( 0 .. 100_000 u32 ) .map ( | i | ( i % 239 ) as u8 ) ) ;
2616
let mut chunks = std:: collections:: HashMap:: new( ) ;
2617
let m = jet:: images:: scan( & image[ .. ] , Some ( " x86_64" .into ( ) ) , | c , b | {
2618
chunks.insert ( c.hash ( ) .to_string ( ) , b.to_vec ( ) ) ;
2619
Ok ( ( ) )
2620
} )
2621
.unwrap ( ) ;
2622
let hash = m.hash.clone ( ) ;
2623
2624
2625
let ask = serde_json:: json! ( { " chunks" : m.needed ( ) } ) .to_string ( ) ;
2626
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/images/missing" , Some ( " site" ) , Some ( ask.clone ( ) ) ) .await;
2627
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
2628
let missing: Vec< String> = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " missing" ]
2629
.as_array ( )
2630
.unwrap ( )
2631
.iter ( )
2632
.map ( | v | v.as_str ( ) .unwrap ( ) .to_string ( ) )
2633
.collect ( ) ;
2634
assert_eq! ( missing.len ( ) , m.needed ( ) .len ( ) ) ;
2635
let manifest = serde_json:: to_string( & m) .unwrap ( ) ;
2636
let ( st, body) = call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " site" ) , Some ( manifest.clone ( ) ) ) .await;
2637
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " was not sent" ) , " {st} {body}" ) ;
2638
2639
let first = & missing[ 0 ] ;
2640
assert_eq! ( put_bytes ( & h.app, & format! ( " /api/v1/flow/images/chunks/{first} " ) , " site" , vec! [ 1 , 2 , 3 ] ) .await.0 , StatusCode:: BAD_REQUEST ) ;
2641
assert_ne! (
2642
put_bytes ( & h.app, & format! ( " /api/v1/flow/images/chunks/{first} " ) , " runner-secret" , chunks[ first] .clone ( ) ) .await.0 ,
2643
StatusCode:: NO_CONTENT
2644
) ;
2645
for c in & missing {
2646
let ( st, body) = put_bytes ( & h.app, & format! ( " /api/v1/flow/images/chunks/{c} " ) , " site" , chunks[ c] .clone ( ) ) .await;
2647
assert_eq! ( st, StatusCode:: NO_CONTENT , " {body}" ) ;
2648
}
2649
let ( _ , body) = call ( & h.app, " POST" , " /api/v1/flow/images/missing" , Some ( " site" ) , Some ( ask) ) .await;
2650
assert! ( body.contains ( " \" missing\" :[]" ) , " {body}" ) ;
2651
2652
let mut wrong = m.clone ( ) ;
2653
wrong.hash = " ab" .repeat ( 32 ) ;
2654
let ( st, _ ) =
2655
call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{} " , wrong.hash) , Some ( " site" ) , Some ( serde_json:: to_string( & wrong) .unwrap ( ) ) )
2656
.await;
2657
assert_eq! ( st, StatusCode:: CONFLICT , " chunks that make another image" ) ;
2658
let ( st, body) = call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " site" ) , Some ( manifest) ) .await;
2659
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
2660
2661
2662
let ( st, body) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " runner-secret" ) , None ) .await;
2663
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
2664
let got: jet:: images:: Manifest = serde_json:: from_str( & body) .unwrap ( ) ;
2665
assert_eq! ( got, m) ;
2666
let dir = tempfile:: tempdir( ) .unwrap ( ) ;
2667
let out = dir.path ( ) .join ( " img.ext4" ) ;
2668
let mut fetched = Vec :: new( ) ;
2669
for c in got.needed ( ) {
2670
let req = Request:: get( format! ( " /api/v1/flow/images/chunks/{c} " ) )
2671
.header ( header:: AUTHORIZATION , " Bearer runner-secret" )
2672
.body ( Body:: empty( ) )
2673
.unwrap ( ) ;
2674
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
2675
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
2676
let bytes = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ;
2677
fetched.push ( ( c.to_string ( ) , bytes) ) ;
2678
}
2679
jet:: images:: assemble( & got, & out, | c | Ok ( fetched.iter ( ) .find ( | ( h , _) | h = = c.hash ( ) ) .unwrap ( ) .1. clone ( ) ) ) .unwrap ( ) ;
2680
assert_eq! ( std:: fs:: read( & out) .unwrap ( ) , image) ;
2681
assert_eq! (
2682
call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{} " , " cd" .repeat ( 32 ) ) , Some ( " runner-secret" ) , None ) .await.0 ,
2683
StatusCode:: NOT_FOUND
2684
) ;
2685
assert_ne! ( call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{hash} " ) , None , None ) .await.0 , StatusCode:: OK , " not anonymous" ) ;
2686
2687
2688
2689
create ( & h.app, " andy" , " demo" ) .await;
2690
let url = format! ( " {} /andy/demo" , h.base) ;
2691
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2692
let c = client ( & url, work.path ( ) ) ;
2693
write (
2694
work.path ( ) ,
2695
" .flow/pipeline.kdl" ,
2696
& format! ( " schema-version 1\n sandbox \" img\" {{ \n rootfs \" cas://blob/{hash} \" \n }} \n job \" build\" {{ \n sandbox \" img\" \n exec \" make\" \n }} \n " ) ,
2697
) ;
2698
c.commit ( " Build on the image" , & [ ] ) .unwrap ( ) ;
2699
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2700
runs_of ( & h.app, " andy" , " demo" ) .await;
1c8276b9 Andy McCall 25 days ago ↶
2701
2702
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
2703
let run1 = db:: flow:: run( & h.app.db, row.id, 1 ) .await.unwrap ( ) .unwrap ( ) ;
2704
let checks = checks_when ( & h.app, row.id, & run1.state, 1 ) .await;
2705
assert! ( checks.iter ( ) .all ( | c | c.name ! = " pipeline/images" ) , " {checks:?}" ) ;
92a797b2 Andy McCall 26 days ago ↶
2706
let ask = | fetches : bool | format! ( r # "{{ "runner":"test-1","runtimes":["microvm"],"images":[],"fetches_images":{fetches} }} "# ) ;
2707
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( false ) ) ) .await;
2708
assert_eq! ( st, StatusCode:: NO_CONTENT , " it lacks the image and does not fetch" ) ;
2709
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
2710
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
2711
assert! ( body.contains ( " does not hold image" ) , " {body}" ) ;
2712
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( true ) ) ) .await;
2713
assert_eq! ( st, StatusCode:: OK , " it fetches, and the hub holds the image: {body}" ) ;
2714
}
40dd3f86 Andy McCall 25 days ago ↶
2715
2716
2717
2718
2719
# [ tokio ::test ( flavor = " multi_thread" ) ]
2720
async fn coverage_uploaded_from_a_workstation_says_so ( ) {
2721
let h = hub ( ) .await;
2722
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2723
create ( & h.app, " andy" , " vk" ) .await;
2724
let url = format! ( " {} /andy/vk" , h.base) ;
2725
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2726
let c = client ( & url, work.path ( ) ) ;
2727
write ( work.path ( ) , " src/game.c" , " int a;\n int b;\n int c;\n int d;\n " ) ;
2728
c.commit ( " Game" , & [ ] ) .unwrap ( ) ;
2729
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2730
2731
let lcov = " SF:src/game.c\n DA:1,3\n DA:2,1\n DA:3,0\n DA:4,5\n end_of_record\n SF:reference/rom.asm\n DA:1,1\n end_of_record\n " ;
2732
let body = serde_json:: json! ( { " bookmark" : " main" , " content" : lcov } ) .to_string ( ) ;
2733
let ( st, out) = call ( & h.app, " POST" , " /api/v1/repos/andy/vk/coverage" , Some ( " site" ) , Some ( body) ) .await;
2734
assert_eq! ( st, StatusCode:: CREATED , " {out}" ) ;
2735
let v: serde_json:: Value = serde_json:: from_str( & out) .unwrap ( ) ;
2736
assert_eq! ( ( v[ " uploaded_by" ] .as_str ( ) , v[ " lines_hit" ] .as_i64 ( ) , v[ " lines_found" ] .as_i64 ( ) ) , ( Some ( " andy" ) , Some ( 3 ) , Some ( 4 ) ) ) ;
2737
assert_eq! ( v[ " dropped" ] , 1 , " a file outside the state is left out" ) ;
2738
assert_eq! ( v[ " bookmark" ] , " main" ) ;
2739
2740
let ( _ , page) = call ( & h.app, " GET" , " /andy/vk/coverage" , None , None ) .await;
2741
assert! ( page.contains ( " uploaded by andy" ) & & page.contains ( " 75.0%" ) , " the Coverage page says where it came from" ) ;
2742
let ( _ , page) = call ( & h.app, " GET" , " /andy/vk/reports" , None , None ) .await;
2743
assert! ( page.contains ( " uploaded by andy" ) , " and the Reports card" ) ;
2744
let row = db:: repos:: get( & h.app.db, " andy" , " vk" ) .await.unwrap ( ) .unwrap ( ) ;
2745
let state = v[ " state" ] .as_str ( ) .unwrap ( ) ;
2746
let checks = db:: hooks:: checks_for_state( & h.app.db, row.id, state) .await.unwrap ( ) ;
2747
assert! ( checks.iter ( ) .all ( | c | c.name ! = " coverage" ) , " an upload sets no check: {checks:?}" ) ;
2748
2749
2750
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/vk/coverage" , None , Some ( r # "{"content":"x"}"# .into ( ) ) ) .await;
2751
assert_ne! ( st, StatusCode:: CREATED ) ;
2752
let ( st, _ ) = call (
2753
& h.app,
2754
" POST" ,
2755
" /api/v1/repos/andy/vk/coverage" ,
2756
Some ( " site" ) ,
2757
Some ( r # "{"bookmark":"nope","content":"SF:a\nend_of_record\n"}"# .into ( ) ) ,
2758
)
2759
.await;
2760
assert_eq! ( st, StatusCode:: BAD_REQUEST ) ;
2761
}
2fb70d01 Andy McCall 25 days ago ↶
2762
2763
2764
# [ tokio ::test ( flavor = " multi_thread" ) ]
2765
async fn a_runners_facts_are_kept_from_its_claim ( ) {
2766
let h = hub ( ) .await;
2767
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2768
let body = r # "{"runner":"test-1","runtimes":["microvm"],"facts":{"free_disk_mib":42000,"load_x100":112,"speed":100,"speed_class":3,"kernel":"7.0.0"}}"# ;
2769
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body.into ( ) ) ) .await;
2770
assert_eq! ( st, StatusCode:: NO_CONTENT , " nothing to run, but the facts are kept" ) ;
2771
let ( st, out) = call ( & h.app, " GET" , " /api/v1/runners" , Some ( " site" ) , None ) .await;
2772
assert_eq! ( st, StatusCode:: OK , " {out}" ) ;
2773
let v: serde_json:: Value = serde_json:: from_str( & out) .unwrap ( ) ;
2774
let r = & v[ 0 ] ;
2775
assert_eq! (
2776
( r[ " speed" ] .as_i64 ( ) , r[ " speed_class" ] .as_i64 ( ) , r[ " free_disk_mib" ] .as_i64 ( ) , r[ " load_x100" ] .as_i64 ( ) ) ,
2777
( Some ( 100 ) , Some ( 3 ) , Some ( 42000 ) , Some ( 112 ) )
2778
) ;
2779
let row = db:: runners:: list( & h.app.db) .await.unwrap ( ) .remove ( 0 ) ;
2780
assert_eq! ( row.facts_line ( ) , " speed 100 (class 3) · load 1.12 · 41 GiB free" ) ;
2781
}
44a3d2eb Andy McCall 25 days ago ↶
2782
2783
2784
2785
# [ tokio ::test ( flavor = " multi_thread" ) ]
2786
async fn a_jobs_next_run_needs_the_disk_its_last_took ( ) {
2787
let h = hub ( ) .await;
2788
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2789
create ( & h.app, " andy" , " demo" ) .await;
2790
let url = format! ( " {} /andy/demo" , h.base) ;
2791
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2792
let c = client ( & url, work.path ( ) ) ;
2793
write (
2794
work.path ( ) ,
2795
" .flow/pipeline.kdl" ,
2796
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
2797
) ;
2798
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
2799
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2800
runs_of ( & h.app, " andy" , " demo" ) .await;
2801
let claim_with = | free : u64 | format! ( r # "{{ "runner":"test-1","runtimes":["microvm"],"facts":{{ "free_disk_mib":{free} }} }} "# ) ;
2802
2803
2804
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( claim_with ( 500 ) ) ) .await;
2805
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
2806
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2807
let done = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " peak_disk_mib" : 8000 , " peak_memory_mib" : 900 } ) ;
2808
assert_eq! ( finish ( & h.app, id, done) .await, StatusCode:: NO_CONTENT ) ;
2809
let job = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
2810
assert_eq! ( ( job.peak_disk_mib, job.peak_memory_mib) , ( Some ( 8000 ) , Some ( 900 ) ) ) ;
2811
2812
2813
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
2814
assert_eq! ( st, StatusCode:: CREATED ) ;
2815
let repo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
2816
let run2 = db:: flow:: run( & h.app.db, repo.id, 2 ) .await.unwrap ( ) .unwrap ( ) ;
2817
let next = db:: flow:: jobs( & h.app.db, run2.id) .await.unwrap ( ) .remove ( 0 ) ;
2818
assert_eq! ( next.need_disk_mib, Some ( 8000 + 2000 + 64 ) ) ;
2819
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( claim_with ( 9000 ) ) ) .await;
2820
assert_eq! ( st, StatusCode:: NO_CONTENT , " 9000 MiB free is not enough" ) ;
2821
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
2822
let waiting = db:: flow:: job( & h.app.db, next.id) .await.unwrap ( ) .unwrap ( ) .waiting;
2823
assert! ( waiting.contains ( " 8.8 GiB free; the job needs ~9.8 GiB" ) , " {waiting}" ) ;
2824
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( claim_with ( 20_000 ) ) ) .await;
2825
assert_eq! ( st, StatusCode:: OK , " a roomier runner takes it: {body}" ) ;
2826
}
501d8b26 Andy McCall 25 days ago ↶
2827
2828
2829
2830
2831
# [ tokio ::test ( flavor = " multi_thread" ) ]
2832
async fn a_job_waits_briefly_for_a_runner_that_holds_its_image ( ) {
2833
let h = hub ( ) .await;
2834
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2835
2836
db:: runners:: configured( & h.app.db, & blake3:: hash( b " other-secret" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
2837
let ask = | images : & str | {
2838
format! (
2839
r # "{{ "runner":"r","runtimes":["microvm"],"images":[{images} ],"fetches_images":true,"facts":{{ "speed":100,"speed_class":3,"free_disk_mib":50000}} }} "#
2840
)
2841
} ;
2842
let holder = ask ( & format! ( " \" {ROOTFS} \" " ) ) ;
2843
2844
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder.clone ( ) ) ) .await;
2845
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
2846
2847
sqlx:: query( " INSERT INTO jet_images (hash, size, arch, chunks, uploader, created_ms) VALUES (?, 1, 'x86_64', 1, 'andy', 0)" )
2848
.bind ( ROOTFS )
2849
.execute ( & h.app.db)
2850
.await
2851
.unwrap ( ) ;
2852
2853
create ( & h.app, " andy" , " demo" ) .await;
2854
let url = format! ( " {} /andy/demo" , h.base) ;
2855
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2856
let c = client ( & url, work.path ( ) ) ;
2857
write (
2858
work.path ( ) ,
2859
" .flow/pipeline.kdl" ,
2860
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
2861
) ;
2862
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
2863
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2864
runs_of ( & h.app, " andy" , " demo" ) .await;
2865
2866
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( " " ) ) ) .await;
2867
assert_eq! ( st, StatusCode:: NO_CONTENT , " held for the runner that holds the image" ) ;
2868
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder) ) .await;
2869
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
2870
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2871
let placed = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .placed;
2872
assert! ( placed.starts_with ( " holds the image, idle, speed 100 (class 3)" ) , " {placed}" ) ;
2873
assert! ( placed.contains ( " held for a better runner" ) , " {placed}" ) ;
2874
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
2875
assert! ( body.contains ( " \" placed\" :\" holds the image" ) , " {body}" ) ;
2876
}
20b8fe85 Andy McCall 25 days ago ↶
2877
4fa0da12 Andy McCall 24 days ago ↶
2878
2879
2880
# [ tokio ::test ( flavor = " multi_thread" ) ]
2881
async fn a_job_waits_briefly_for_the_runner_holding_its_build_cache ( ) {
2882
let h = hub ( ) .await;
2883
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2884
db:: runners:: configured( & h.app.db, & blake3:: hash( b " other-secret" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
2885
let slot = flow_core:: api:: cache_slot( " andy/demo" , " build" ) ;
2886
let ask = | caches : & str | {
2887
format! (
2888
r # "{{ "runner":"r","runtimes":["microvm"],"images":["{ROOTFS} "],"fetches_images":true,"caches":[{caches} ],"facts":{{ "speed":100,"speed_class":3,"free_disk_mib":50000}} }} "#
2889
)
2890
} ;
2891
let holder = ask ( & format! ( " \" {slot} \" " ) ) ;
2892
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder.clone ( ) ) ) .await;
2893
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
2894
2895
create ( & h.app, " andy" , " demo" ) .await;
2896
let url = format! ( " {} /andy/demo" , h.base) ;
2897
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2898
let c = client ( & url, work.path ( ) ) ;
2899
write (
2900
work.path ( ) ,
2901
" .flow/pipeline.kdl" ,
2902
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
2903
) ;
2904
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
2905
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2906
runs_of ( & h.app, " andy" , " demo" ) .await;
2907
2908
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( " " ) ) ) .await;
2909
assert_eq! ( st, StatusCode:: NO_CONTENT , " held for the runner holding the build cache" ) ;
2910
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder) ) .await;
2911
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
2912
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2913
let placed = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .placed;
2914
assert! ( placed.starts_with ( " holds its build cache, holds the image" ) , " {placed}" ) ;
2915
}
2916
c2be148c Andy McCall 23 days ago ↶
2917
2918
2919
# [ tokio ::test ( flavor = " multi_thread" ) ]
2920
async fn a_job_waits_briefly_for_the_fresher_build_cache ( ) {
2921
let h = hub ( ) .await;
2922
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2923
db:: runners:: configured( & h.app.db, & blake3:: hash( b " other-secret" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
2924
let slot = flow_core:: api:: cache_slot( " andy/demo" , " build" ) ;
2925
let now = db:: now_ms( ) ;
2926
let ask = | kept : i64 | {
2927
format! (
2928
r # "{{ "runner":"r","runtimes":["microvm"],"images":["{ROOTFS} "],"fetches_images":true,"caches":["{slot} "],"cache_times":{{ "{slot} ":{kept} }} ,"facts":{{ "speed":100,"speed_class":3,"free_disk_mib":50000}} }} "#
2929
)
2930
} ;
2931
2932
let fresh = ask ( now - 60_000 ) ;
2933
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( fresh.clone ( ) ) ) .await;
2934
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
2935
2936
create ( & h.app, " andy" , " demo" ) .await;
2937
let url = format! ( " {} /andy/demo" , h.base) ;
2938
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2939
let c = client ( & url, work.path ( ) ) ;
2940
write (
2941
work.path ( ) ,
2942
" .flow/pipeline.kdl" ,
2943
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
2944
) ;
2945
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
2946
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2947
runs_of ( & h.app, " andy" , " demo" ) .await;
2948
2949
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( now - 86_400_000 ) ) ) .await;
2950
assert_eq! ( st, StatusCode:: NO_CONTENT , " held for the fresher copy" ) ;
2951
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( fresh) ) .await;
2952
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
2953
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2954
let placed = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .placed;
2955
assert! ( placed.starts_with ( " holds its build cache (kept 1 min ago), holds the image" ) , " {placed}" ) ;
2956
}
2957
20b8fe85 Andy McCall 25 days ago ↶
2958
2959
2960
2961
# [ tokio ::test ( flavor = " multi_thread" ) ]
2962
async fn report_files_expire_under_retention ( ) {
2963
let h = hub ( ) .await;
2964
create ( & h.app, " andy" , " demo" ) .await;
2965
let repo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
2966
let add = | bookmark : & 'static str , at : i64 | {
2967
let db = h.app.db.clone ( ) ;
2968
async move {
2969
let id: i64 = sqlx:: query_scalar(
2970
" INSERT INTO reports (repo_id, name, kind, title, summary, context, job, bookmark, state, files, bytes, created_ms) \
2971
VALUES (?, 'perf', 'performance', 'Perf', '', '{}', 'perf', ?, 's', 1, 1, ?) RETURNING id" ,
2972
)
2973
.bind ( repo.id)
2974
.bind ( bookmark)
2975
.bind ( at)
2976
.fetch_one ( & db)
2977
.await
2978
.unwrap ( ) ;
2979
sqlx:: query( " INSERT INTO report_files (report_id, path, blob, size) VALUES (?, 'report.toml', ?, 1)" )
2980
.bind ( id)
2981
.bind ( format! ( " {:064x}" , id) )
2982
.execute ( & db)
2983
.await
2984
.unwrap ( ) ;
2985
sqlx:: query( " INSERT INTO report_metrics (report_id, key, value, unit, better) VALUES (?, 'ms', 1, '', '')" )
2986
.bind ( id)
2987
.execute ( & db)
2988
.await
2989
.unwrap ( ) ;
2990
id
2991
}
2992
} ;
2993
let main_old = add ( " main" , 1 ) .await;
2994
add ( " main" , 2 ) .await;
2995
let mut feature = Vec :: new( ) ;
2996
for at in 0 .. 52 {
2997
feature.push ( add ( " feature" , 10 + at) .await) ;
2998
}
2999
assert_eq! ( oxygenhub:: reports:: expire( & h.app.db) .await.unwrap ( ) , 2 , " the two oldest feature runs" ) ;
3000
let files = | id : i64 | {
3001
let db = h.app.db.clone ( ) ;
3002
async move {
3003
sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM report_files WHERE report_id = ?" ) .bind ( id) .fetch_one ( & db) .await.unwrap ( )
3004
}
3005
} ;
3006
assert_eq! ( ( files ( feature[ 0 ] ) .await, files ( feature[ 1 ] ) .await, files ( feature[ 2 ] ) .await) , ( 0 , 0 , 1 ) ) ;
3007
assert_eq! ( files ( main_old) .await, 1 , " the default bookmark keeps every run" ) ;
3008
assert_eq! ( oxygenhub:: reports:: metrics( & h.app.db, feature[ 0 ] ) .await.unwrap ( ) .len ( ) , 1 , " metrics are kept" ) ;
3009
assert_eq! ( oxygenhub:: reports:: expire( & h.app.db) .await.unwrap ( ) , 0 , " once" ) ;
3010
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/reports/perf?id={} " , feature[ 0 ] ) , None , None ) .await;
3011
assert! ( page.contains ( " files have expired" ) , " the page says so" ) ;
1c8276b9 Andy McCall 25 days ago ↶
3012
}
3013
3014
3015
3016
3017
# [ tokio ::test ( flavor = " multi_thread" ) ]
3018
async fn a_pipeline_pinning_an_image_nobody_holds_fails_a_check ( ) {
3019
let h = hub ( ) .await;
3020
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3021
3022
let none = r # "{"runner":"test-1","runtimes":["microvm"],"images":[]}"# ;
3023
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( none.into ( ) ) ) .await;
3024
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
3025
create ( & h.app, " andy" , " demo" ) .await;
3026
let url = format! ( " {} /andy/demo" , h.base) ;
3027
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3028
let c = client ( & url, work.path ( ) ) ;
3029
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3030
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3031
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
3032
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3033
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
3034
let checks = checks_when ( & h.app, run.repo_id, & run.state, 4 ) .await;
3035
let check = checks.iter ( ) .find ( | c | c.name = = " pipeline/images" ) .expect ( " an images check" ) ;
3036
assert! ( check.status = = " failed" & & check.summary.contains ( & ROOTFS [ .. 12 ] ) & & check.summary.contains ( " flow rootfs push" ) , " {check:?}" ) ;
3037
3038
let holds = format! ( r # "{{ "runner":"test-1","runtimes":["microvm"],"images":["{ROOTFS} "]}} "# ) ;
3039
call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( holds) ) .await;
3040
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
3041
assert_eq! ( st, StatusCode:: CREATED ) ;
3042
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
3043
let check = checks.iter ( ) .find ( | c | c.name = = " pipeline/images" ) .unwrap ( ) ;
3044
assert_eq! ( check.status, " passed" , " {check:?}" ) ;
20b8fe85 Andy McCall 25 days ago ↶
3045
}
a46d15e7 Andy McCall 25 days ago ↶
3046
3047
3048
3049
3050
# [ tokio ::test ( flavor = " multi_thread" ) ]
3051
async fn the_tracker_names_peers_in_the_same_site_and_signs_a_ticket ( ) {
3052
let h = hub ( ) .await;
3053
let app = & h.app;
3054
let image = " ab" .repeat ( 32 ) ;
3055
let claim_as = | token : & 'static str , name : & 'static str , body : serde_json:: Value| async move {
3056
let mut b = serde_json:: json! ( { " runner" : name, " runtimes" : [ " microvm" ] } ) ;
3057
b.as_object_mut ( ) .unwrap ( ) .extend ( body.as_object ( ) .unwrap ( ) .clone ( ) ) ;
3058
let ( st, out) = call ( app, " POST" , " /api/v1/flow/claim" , Some ( token) , Some ( b.to_string ( ) ) ) .await;
3059
assert_eq! ( st, StatusCode:: NO_CONTENT , " {out}" ) ;
3060
} ;
3061
let announce = | token : & 'static str , state : & 'static str | {
3062
let body = serde_json:: json! ( { " image" : image, " state" : state } ) .to_string ( ) ;
3063
async move { call ( app, " POST" , " /api/v1/flow/swarm/announce" , Some ( token) , Some ( body) ) .await }
3064
} ;
3065
6adcd68b Andy McCall 25 days ago ↶
3066
claim_as (
3067
" runner-secret" ,
3068
" a" ,
3069
serde_json:: json! ( { " images" : [ image] , " swarm" : { " addr" : " 10.0.0.1:7401" , " site" : " lab" , " cert" : " 11" .repeat ( 32 ) } } ) ,
3070
)
3071
.await;
3072
claim_as (
3073
" runner-two" ,
3074
" b" ,
3075
serde_json:: json! ( { " images" : [ ] , " swarm" : { " addr" : " 10.0.0.2:7401" , " site" : " lab" , " cert" : " 22" .repeat ( 32 ) } } ) ,
3076
)
3077
.await;
3078
claim_as (
3079
" runner-three" ,
3080
" c" ,
3081
serde_json:: json! ( { " images" : [ ] , " swarm" : { " addr" : " 10.0.0.3:7401" , " site" : " lab" , " cert" : " 33" .repeat ( 32 ) } } ) ,
3082
)
3083
.await;
a46d15e7 Andy McCall 25 days ago ↶
3084
3085
let ( st, body) = announce ( " runner-two" , " fetching" ) .await;
3086
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3087
let a: flow_core:: api:: Announced = serde_json:: from_str( & body) .unwrap ( ) ;
3088
let names: Vec< ( & str , bool ) > = a.peers.iter ( ) .map ( | p | ( p.addr.as_str ( ) , p.complete) ) .collect ( ) ;
3089
assert_eq! ( names, vec! [ ( " 10.0.0.1:7401" , true ) ] , " only A holds it so far" ) ;
6adcd68b Andy McCall 25 days ago ↶
3090
assert_eq! ( a.peers[ 0 ] .cert, " 11" .repeat ( 32 ) , " with the fingerprint peers check it by" ) ;
a46d15e7 Andy McCall 25 days ago ↶
3091
let t = jet:: swarm:: Ticket:: open( & a.ticket, & app.hub_key.identity, oxygenhub:: db:: now_ms( ) as u64 ) .unwrap ( ) ;
3092
assert_eq! ( ( t.name.as_str ( ) , t.site.as_str ( ) ) , ( " b" , " lab" ) ) ;
3093
3094
3095
let ( _ , body) = announce ( " runner-three" , " fetching" ) .await;
3096
let a: flow_core:: api:: Announced = serde_json:: from_str( & body) .unwrap ( ) ;
3097
let mut peers: Vec< ( & str , bool ) > = a.peers.iter ( ) .map ( | p | ( p.addr.as_str ( ) , p.complete) ) .collect ( ) ;
3098
peers.sort ( ) ;
3099
assert_eq! ( peers, vec! [ ( " 10.0.0.1:7401" , true ) , ( " 10.0.0.2:7401" , false ) ] ) ;
3100
3101
3102
announce ( " runner-two" , " done" ) .await;
6adcd68b Andy McCall 25 days ago ↶
3103
claim_as (
3104
" runner-three" ,
3105
" c" ,
3106
serde_json:: json! ( { " images" : [ ] , " swarm" : { " addr" : " 10.0.0.3:7401" , " site" : " elsewhere" , " cert" : " 33" .repeat ( 32 ) } } ) ,
3107
)
3108
.await;
a46d15e7 Andy McCall 25 days ago ↶
3109
let ( _ , body) = announce ( " runner-three" , " fetching" ) .await;
3110
let a: flow_core:: api:: Announced = serde_json:: from_str( & body) .unwrap ( ) ;
3111
assert! ( a.peers.is_empty ( ) , " {:?}" , a.peers) ;
6adcd68b Andy McCall 25 days ago ↶
3112
3113
3114
claim_as ( " runner-secret" , " a" , serde_json:: json! ( { " images" : [ image] , " swarm" : { " addr" : " 10.0.0.1:7401" , " site" : " elsewhere" } } ) )
3115
.await;
3116
let ( st, _ ) = announce ( " runner-secret" , " fetching" ) .await;
3117
assert_eq! ( st, StatusCode:: CONFLICT ) ;
a46d15e7 Andy McCall 25 days ago ↶
3118
3119
3120
claim_as ( " runner-two" , " b" , serde_json:: json! ( { " images" : [ ] } ) ) .await;
3121
let ( st, body) = announce ( " runner-two" , " fetching" ) .await;
3122
assert_eq! ( st, StatusCode:: CONFLICT ) ;
3123
assert! ( body.contains ( " [runner.swarm]" ) , " {body}" ) ;
3124
let ( st, _ ) =
3125
call ( app, " POST" , " /api/v1/flow/swarm/announce" , Some ( " runner-secret" ) , Some ( r # "{"image":"nope","state":"fetching"}"# .into ( ) ) )
3126
.await;
3127
assert_eq! ( st, StatusCode:: BAD_REQUEST ) ;
3128
let ( st, _ ) = call (
3129
app,
3130
" POST" ,
3131
" /api/v1/flow/swarm/announce" ,
3132
None ,
3133
Some ( serde_json:: json! ( { " image" : image, " state" : " fetching" } ) .to_string ( ) ) ,
3134
)
3135
.await;
3136
assert_eq! ( st, StatusCode:: UNAUTHORIZED ) ;
3137
}
6ca0b971 Andy McCall 24 days ago ↶
3138
3139
3140
# [ tokio ::test ( flavor = " multi_thread" ) ]
3141
async fn the_images_page_says_where_each_image_is_and_what_uses_it ( ) {
3142
let h = hub ( ) .await;
3143
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3144
create ( & h.app, " andy" , " demo" ) .await;
3145
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3146
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
3147
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3148
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3149
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
3150
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3151
3152
let held = " cd" .repeat ( 32 ) ;
3153
let kept = " ef" .repeat ( 32 ) ;
3154
let body = serde_json:: json! ( { " runner" : " earth-1" , " runtimes" : [ " wasm" ] , " images" : [ held] , " swarm" : { " addr" : " 10.0.0.1:7401" , " site" : " home" , " cert" : " 11" .repeat ( 32 ) } } ) ;
3155
let ( st, out) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body.to_string ( ) ) ) .await;
3156
assert! ( st = = StatusCode:: NO_CONTENT | | st = = StatusCode:: OK , " {st} {out}" ) ;
3157
sqlx:: query( " INSERT INTO jet_images (hash, size, arch, chunks, uploader, created_ms) VALUES (?, 1048576, 'x86_64', 9, 'andy', 1)" )
3158
.bind ( & kept)
3159
.execute ( & h.app.db)
3160
.await
3161
.unwrap ( ) ;
3162
3163
let res = oxygenhub:: router( h.app.clone ( ) )
3164
.oneshot (
3165
Request:: post( " /register" )
3166
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
3167
.body ( Body:: from( " username=carol&email=carol%40example.com&password=correct-horse-battery" ) )
3168
.unwrap ( ) ,
3169
)
3170
.await
3171
.unwrap ( ) ;
3172
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
3173
let carol = db:: accounts:: account_by_name( & h.app.db, " carol" ) .await.unwrap ( ) .unwrap ( ) ;
3174
db:: accounts:: set_admin( & h.app.db, carol.id, true ) .await.unwrap ( ) ;
3175
let res = oxygenhub:: router( h.app.clone ( ) )
3176
.oneshot ( Request:: get( " /admin/runners/images" ) .header ( header:: COOKIE , cookie) .body ( Body:: empty( ) ) .unwrap ( ) )
3177
.await
3178
.unwrap ( ) ;
3179
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
3180
let page = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
3181
let row = | hash : & str | {
3182
let at = page.find ( & format! ( " title=\" {hash} \" " ) ) .unwrap_or_else ( | | panic! ( " {hash} is not on the page" ) ) ;
3183
let end = page[ at.. ] .find ( " </tr>" ) .unwrap ( ) ;
3184
page[ at.. at + end] .to_string ( )
3185
} ;
3186
let pinned = row ( ROOTFS ) ;
3187
assert! ( pinned.contains ( " nowhere" ) & & pinned.contains ( " andy/demo" ) & & pinned.contains ( " 3 jobs" ) , " {pinned}" ) ;
3188
let on_runner = row ( & held) ;
3189
assert! ( on_runner.contains ( " img-holds" ) & & on_runner.contains ( " earth-1" ) & & ! on_runner.contains ( " nowhere" ) , " {on_runner}" ) ;
3190
let on_hub = row ( & kept) ;
3191
assert! ( on_hub.contains ( " img-hub" ) & & on_hub.contains ( " 1.0 MiB" ) & & on_hub.contains ( " x86_64" ) , " {on_hub}" ) ;
3192
assert! ( page.contains ( " earth-1 (home)" ) , " the swarm is named" ) ;
6246d1e5 Andy McCall 23 days ago ↶
3193
3194
assert! ( page.contains ( r # "{"images":"# ) & & page.contains ( r # ""scene":"swarm""# ) & & page.contains ( r # ""earth-1""# ) , " the swarm's data" ) ;
6ca0b971 Andy McCall 24 days ago ↶
3195
assert_eq! ( call ( & h.app, " GET" , " /admin/runners/images" , None , None ) .await.0 , StatusCode:: NOT_FOUND , " administrators only" ) ;
3196
}
f677d627 Andy McCall 24 days ago ↶
3197
3198
3199
3200
3201
# [ tokio ::test ( flavor = " multi_thread" ) ]
3202
async fn consumers_of_a_subrepo_are_indexed_with_how_far_behind_they_are ( ) {
3203
let h = hub ( ) .await;
e7158350 Andy McCall 24 days ago ↶
3204
3205
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
f677d627 Andy McCall 24 days ago ↶
3206
create ( & h.app, " andy" , " api" ) .await;
3207
create ( & h.app, " andy" , " game" ) .await;
3208
let api_work = tempfile:: tempdir( ) .unwrap ( ) ;
3209
let api = client ( & format! ( " {} /andy/api" , h.base) , api_work.path ( ) ) ;
3210
write ( api_work.path ( ) , " api.h" , " v1\n " ) ;
3211
api.snapshot ( " t" ) .unwrap ( ) ;
3212
api.commit ( " api v1" , & [ ] ) .unwrap ( ) ;
3213
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3214
3215
let game_work = tempfile:: tempdir( ) .unwrap ( ) ;
3216
let game = client ( & format! ( " {} /andy/game" , h.base) , game_work.path ( ) ) ;
3217
game.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
3218
write ( game_work.path ( ) , " game.c" , " int main;\n " ) ;
3219
game.snapshot ( " t" ) .unwrap ( ) ;
3220
game.commit ( " game on api v1" , & [ ] ) .unwrap ( ) ;
3221
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3222
3223
let consumers = | | async {
3224
let ( st, body) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , Some ( " site" ) , None ) .await;
3225
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3226
serde_json:: from_str:: < Vec< serde_json:: Value> > ( & body) .unwrap ( )
3227
} ;
3228
3229
3230
let settle = | want : & 'static ( dyn Fn( & [serde_json:: Value]) -> bool + Sync) | {
3231
let consumers = & consumers;
3232
async move {
3233
for _ in 0 .. 100 {
3234
let list = consumers ( ) .await;
3235
if want ( & list) {
3236
return list;
3237
}
3238
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3239
}
3240
consumers ( ) .await
3241
}
3242
} ;
3243
let list = settle ( & | l| ! l.is_empty ( ) ) .await;
3244
assert_eq! ( list.len ( ) , 1 , " {list:?}" ) ;
3245
assert_eq! (
3246
( list[ 0 ] [ " name" ] .as_str ( ) , list[ 0 ] [ " path" ] .as_str ( ) , list[ 0 ] [ " bookmark" ] .as_str ( ) ) ,
3247
( Some ( " game" ) , Some ( " deps/api" ) , Some ( " main" ) )
3248
) ;
3249
assert_eq! ( list[ 0 ] [ " behind" ] , 0 ) ;
3250
3251
3252
write ( api_work.path ( ) , " api.c" , " v2\n " ) ;
3253
api.snapshot ( " t" ) .unwrap ( ) ;
3254
api.commit ( " api v2" , & [ ] ) .unwrap ( ) ;
3255
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3256
assert_eq! ( settle ( & | l| l.first ( ) .is_some_and ( | c | c[ " behind" ] = = 1 ) ) .await[ 0 ] [ " behind" ] , 1 ) ;
3257
let ( _ , page) = call ( & h.app, " GET" , " /andy/api" , None , None ) .await;
c9aacc0e Andy McCall 24 days ago ↶
3258
assert! ( page.contains ( " Diffuses to" ) & & page.contains ( " andy/game" ) & & page.contains ( " 1 behind" ) , " the page shows it" ) ;
f677d627 Andy McCall 24 days ago ↶
3259
3260
3261
sqlx:: query( " UPDATE repositories SET visibility = 'private' WHERE name = 'game'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
3262
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , None , None ) .await;
3263
assert_eq! ( body.trim ( ) , " []" ) ;
3264
let ( _ , page) = call ( & h.app, " GET" , " /andy/api" , None , None ) .await;
3265
assert! ( ! page.contains ( " andy/game" ) ) ;
3266
sqlx:: query( " UPDATE repositories SET visibility = 'public' WHERE name = 'game'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
e7158350 Andy McCall 24 days ago ↶
3267
1d634f69 Andy McCall 24 days ago ↶
3268
3269
3270
let game_row = db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
3271
let api_row = db:: repos:: get( & h.app.db, " andy" , " api" ) .await.unwrap ( ) .unwrap ( ) ;
3272
let api_heads = api.bookmark_heads ( ) .unwrap ( ) ;
3273
let bumped = oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, api_heads.clone ( ) ) .await.unwrap ( ) ;
3274
let oxygenhub:: diffusion:: Bumped:: Moved { bookmark, heads, .. } = bumped else { panic! ( " {bumped:?}" ) } ;
3275
assert_eq! ( bookmark, " diffusion/deps/api" ) ;
3276
game.fetch ( " origin" ) .unwrap ( ) ;
3277
let bump_state = game.load_state ( & heads) .unwrap ( ) .materialize ( ) ;
3278
let pin = match & bump_state.files[ " deps/api" ] .content {
3279
oxy_core:: state:: FileContent:: Opaque { blobs } => oxy_core:: subrepo:: Pin:: decode( & game.read_blob ( & blobs[ 0 ] ) .unwrap ( ) ) .unwrap ( ) ,
3280
other => panic! ( " {other:?}" ) ,
3281
} ;
3282
assert! (
3283
matches! ( & pin, oxy_core:: subrepo:: Pin:: Oxy { heads, .. } if { let mut a = heads.clone ( ) ; a.sort ( ) ; let mut b = api_heads.clone ( ) ; b.sort ( ) ; a = = b } ) ,
3284
" {pin:?}"
3285
) ;
3286
assert! ( bump_state.files.contains_key ( " game.c" ) , " the rest of the game is as it was" ) ;
9aaa88b2 Andy McCall 24 days ago ↶
3287
assert! ( matches! (
1d634f69 Andy McCall 24 days ago ↶
3288
oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, api_heads.clone ( ) ) .await.unwrap ( ) ,
9aaa88b2 Andy McCall 24 days ago ↶
3289
oxygenhub:: diffusion:: Bumped:: Already { .. }
3290
) ) ;
7a14bdae Andy McCall 24 days ago ↶
3291
3292
3293
1d634f69 Andy McCall 24 days ago ↶
3294
write ( api_work.path ( ) , " other.c" , " v3\n " ) ;
3295
api.snapshot ( " t" ) .unwrap ( ) ;
3296
let v3 = api.commit ( " api v3, on its own" , & [ ] ) .unwrap ( ) ;
3297
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3298
let only_v3 = api.load_state ( & [ v3] ) .unwrap ( ) .heads ( ) .to_vec ( ) ;
7a14bdae Andy McCall 24 days ago ↶
3299
let combined = oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, only_v3) .await.unwrap ( ) ;
3300
let oxygenhub:: diffusion:: Bumped:: Moved { heads: ch, .. } = combined else { panic! ( " {combined:?}" ) } ;
3301
game.fetch ( " origin" ) .unwrap ( ) ;
3302
let st = game.load_state ( & ch) .unwrap ( ) .materialize ( ) ;
3303
let pin = match & st.files[ " deps/api" ] .content {
3304
oxy_core:: state:: FileContent:: Opaque { blobs } => oxy_core:: subrepo:: Pin:: decode( & game.read_blob ( & blobs[ 0 ] ) .unwrap ( ) ) .unwrap ( ) ,
3305
other => panic! ( " {other:?}" ) ,
3306
} ;
3307
let oxy_core:: subrepo:: Pin:: Oxy { heads: pinned, .. } = pin else { panic! ( ) } ;
3308
let all = api.load_state ( & pinned) .unwrap ( ) ;
3309
3310
assert! ( all.contains ( & v3) & & all.len ( ) = = 2 , " v1 and v3, {} changes" , all.len ( ) ) ;
1d634f69 Andy McCall 24 days ago ↶
3311
3312
let newer = api.bookmark_heads ( ) .unwrap ( ) ;
3313
let again = oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, newer) .await.unwrap ( ) ;
3314
assert! ( matches! ( again, oxygenhub:: diffusion:: Bumped:: Moved { ref bookmark, .. } if bookmark = = " diffusion/deps/api" ) , " {again:?}" ) ;
3315
e7158350 Andy McCall 24 days ago ↶
3316
3317
let put =
3318
| body : & 'static str , who : Option< & 'static str > | call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , who, Some ( body.into ( ) ) ) ;
3319
let ( st, body) = put ( r # "{"follow":"green"}"# , Some ( " site" ) ) .await;
3320
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3321
assert! ( body.contains ( " \" follow\" :\" green\" " ) & & body.contains ( " \" from\" :\" setting\" " ) , " {body}" ) ;
3322
assert_eq! ( put ( r # "{"follow":"sometimes"}"# , Some ( " site" ) ) .await.0 , StatusCode:: BAD_REQUEST ) ;
3323
assert_ne! ( put ( r # "{"follow":"any"}"# , None ) .await.0 , StatusCode:: OK , " not without a token" ) ;
3324
let ( _ , body) = put ( r # "{"follow":"default"}"# , Some ( " site" ) ) .await;
3325
assert! ( body.contains ( " \" follow\" :\" off\" " ) & & body.contains ( " \" from\" :\" site\" " ) , " {body}" ) ;
f677d627 Andy McCall 24 days ago ↶
3326
3327
3328
game.submodule_remove ( " deps/api" ) .unwrap ( ) ;
3329
std:: fs:: remove_dir_all( game_work.path ( ) .join ( " deps/api" ) ) .unwrap ( ) ;
3330
game.snapshot ( " t" ) .unwrap ( ) ;
3331
game.commit ( " game without api" , & [ ] ) .unwrap ( ) ;
3332
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3333
let left = settle ( & | l| l.is_empty ( ) ) .await;
3334
assert! ( left.is_empty ( ) , " {left:?}" ) ;
3335
}
a7e6e8e6 Andy McCall 24 days ago ↶
3336
3337
3338
3339
3340
# [ tokio ::test ( flavor = " multi_thread" ) ]
3341
async fn diffusion_proposes_a_bump_when_the_upstream_moves ( ) {
3342
let h = hub ( ) .await;
3343
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3344
create ( & h.app, " andy" , " api" ) .await;
3345
create ( & h.app, " andy" , " game" ) .await;
180dc43a Andy McCall 24 days ago ↶
3346
3347
let seen: std:: sync:: Mutex< Vec< serde_json:: Value> > = std:: sync:: Mutex:: new( Vec :: new( ) ) ;
3348
let seen = & seen;
3349
let hub_app = & h.app;
3350
let pass_all = move | | async move {
3351
let h_app = hub_app;
a7e6e8e6 Andy McCall 24 days ago ↶
3352
for _ in 0 .. 20 {
180dc43a Andy McCall 24 days ago ↶
3353
let Some ( a) = claim ( h_app) .await else {
a7e6e8e6 Andy McCall 24 days ago ↶
3354
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
180dc43a Andy McCall 24 days ago ↶
3355
if ! jobs_left ( h_app) .await {
a7e6e8e6 Andy McCall 24 days ago ↶
3356
break ;
3357
}
3358
continue ;
3359
} ;
180dc43a Andy McCall 24 days ago ↶
3360
seen.lock ( ) .unwrap ( ) .push ( a.clone ( ) ) ;
a7e6e8e6 Andy McCall 24 days ago ↶
3361
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3362
let ( name, ph) = ( a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
3363
assert_eq! (
180dc43a Andy McCall 24 days ago ↶
3364
finish ( h_app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, 0 ) } ) ) .await,
a7e6e8e6 Andy McCall 24 days ago ↶
3365
StatusCode:: NO_CONTENT
3366
) ;
3367
}
3368
} ;
3369
let api_work = tempfile:: tempdir( ) .unwrap ( ) ;
3370
let api = client ( & format! ( " {} /andy/api" , h.base) , api_work.path ( ) ) ;
3371
write ( api_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3372
write ( api_work.path ( ) , " test.sh" , " echo ok\n " ) ;
3373
write ( api_work.path ( ) , " api.h" , " v1\n " ) ;
3374
api.snapshot ( " t" ) .unwrap ( ) ;
3375
api.commit ( " api v1" , & [ ] ) .unwrap ( ) ;
3376
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3377
pass_all ( ) .await;
3378
3379
let game_work = tempfile:: tempdir( ) .unwrap ( ) ;
3380
let game = client ( & format! ( " {} /andy/game" , h.base) , game_work.path ( ) ) ;
3381
game.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
3382
write ( game_work.path ( ) , " game.c" , " int main;\n " ) ;
f01cb865 Andy McCall 24 days ago ↶
3383
write ( game_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3384
write ( game_work.path ( ) , " test.sh" , " echo ok\n " ) ;
a7e6e8e6 Andy McCall 24 days ago ↶
3385
game.snapshot ( " t" ) .unwrap ( ) ;
3386
game.commit ( " game on api v1" , & [ ] ) .unwrap ( ) ;
3387
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3388
let game_row = db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
3389
let bump = | | async {
3390
for _ in 0 .. 100 {
3391
let open = db:: proposals:: list( & h.app.db, game_row.id, & [ " open" ] ) .await.unwrap ( ) ;
3392
if let Some ( p) = open.into_iter ( ) .find ( | p | p.source_bookmark = = " diffusion/deps/api" ) {
3393
return Some ( p) ;
3394
}
3395
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3396
}
3397
None
3398
} ;
3399
3400
3401
write ( api_work.path ( ) , " a2.c" , " v2\n " ) ;
3402
api.snapshot ( " t" ) .unwrap ( ) ;
3403
api.commit ( " api v2: faster sprites" , & [ ] ) .unwrap ( ) ;
3404
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3405
pass_all ( ) .await;
3406
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
3407
assert! ( db:: proposals:: list( & h.app.db, game_row.id, & [ ] ) .await.unwrap ( ) .is_empty ( ) , " off proposes nothing" ) ;
3408
3776b70d Andy McCall 24 days ago ↶
3409
3410
a7e6e8e6 Andy McCall 24 days ago ↶
3411
let put = | body : & 'static str | call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , Some ( body.into ( ) ) ) ;
3412
assert_eq! ( put ( r # "{"follow":"any"}"# ) .await.0 , StatusCode:: OK ) ;
3776b70d Andy McCall 24 days ago ↶
3413
let first = bump ( ) .await.expect ( " caught up when turned on" ) ;
3414
assert! ( first.title.contains ( " 1 change" ) & & first.body.contains ( " api v2: faster sprites" ) , " {:?}" , first.title) ;
f01cb865 Andy McCall 24 days ago ↶
3415
3416
let mut ran = false ;
3417
for _ in 0 .. 100 {
3418
if runs_of ( & h.app, " andy" , " game" ) .await.iter ( ) .any ( | r | r.bookmark = = " diffusion/deps/api" ) {
3419
ran = true ;
3420
break ;
3421
}
3422
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3423
}
3424
assert! ( ran, " a run of the bump" ) ;
3425
pass_all ( ) .await;
a7e6e8e6 Andy McCall 24 days ago ↶
3426
write ( api_work.path ( ) , " a3.c" , " v3\n " ) ;
3427
api.snapshot ( " t" ) .unwrap ( ) ;
3428
api.commit ( " api v3: save slots past ten" , & [ ] ) .unwrap ( ) ;
3429
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3776b70d Andy McCall 24 days ago ↶
3430
let mut p = first.clone ( ) ;
3431
for _ in 0 .. 100 {
3432
p = bump ( ) .await.unwrap ( ) ;
3433
if p.body.contains ( " api v3" ) {
3434
break ;
3435
}
3436
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3437
}
3438
assert_eq! ( p.number, first.number, " the same proposal" ) ;
a7e6e8e6 Andy McCall 24 days ago ↶
3439
assert_eq! ( ( p.author.as_str ( ) , p.target.as_str ( ) ) , ( " diffusion" , " main" ) ) ;
3440
assert! ( p.title.contains ( " deps/api" ) & & p.title.contains ( " andy/api" ) & & p.title.contains ( " 2 changes" ) , " {}" , p.title) ;
3441
assert! ( p.body.contains ( " api v2: faster sprites" ) & & p.body.contains ( " api v3: save slots past ten" ) , " {}" , p.body) ;
3442
let labels = db:: issues:: proposal_labels( & h.app.db, p.id) .await.unwrap ( ) ;
3443
assert! ( labels.iter ( ) .any ( | l | l.name = = " dependencies" ) ) ;
3444
pass_all ( ) .await;
3445
3446
3447
3448
assert_eq! ( put ( r # "{"follow":"green"}"# ) .await.0 , StatusCode:: OK ) ;
3449
write ( api_work.path ( ) , " a4.c" , " v4\n " ) ;
3450
api.snapshot ( " t" ) .unwrap ( ) ;
3451
api.commit ( " api v4: rsm_set cheaper" , & [ ] ) .unwrap ( ) ;
3452
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3453
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
3454
assert! ( ! bump ( ) .await.unwrap ( ) .body.contains ( " api v4" ) , " not before its run passes" ) ;
3455
pass_all ( ) .await;
3456
let mut updated = None ;
3457
for _ in 0 .. 100 {
3458
let p2 = bump ( ) .await.unwrap ( ) ;
3459
if p2.body.contains ( " api v4" ) {
3460
updated = Some ( p2) ;
3461
break ;
3462
}
3463
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3464
}
3465
let p2 = updated.expect ( " brought up to date once the run passed" ) ;
3466
assert_eq! ( p2.number, p.number, " the same proposal" ) ;
3467
assert! ( p2.title.contains ( " 3 changes" ) , " {}" , p2.title) ;
3468
assert_eq! ( db:: proposals:: list( & h.app.db, game_row.id, & [ ] ) .await.unwrap ( ) .len ( ) , 1 , " never a pile of bumps" ) ;
3776b70d Andy McCall 24 days ago ↶
3469
757195ad Andy McCall 24 days ago ↶
3470
3471
3472
pass_all ( ) .await;
3473
let ( st, page) = call ( & h.app, " GET" , " /andy/api/diffusion" , None , None ) .await;
3474
assert_eq! ( st, StatusCode:: OK ) ;
c9aacc0e Andy McCall 24 days ago ↶
3475
assert! ( page.contains ( " Diffuses to" ) & & page.contains ( " andy/game" ) & & page.contains ( & format! ( " #{} " , p2.number) ) , " the bump is listed" ) ;
757195ad Andy McCall 24 days ago ↶
3476
assert! ( page.contains ( " df-green" ) , " a passing run of a checked upstream is green" ) ;
6246d1e5 Andy McCall 23 days ago ↶
3477
3478
assert! (
3479
page.contains ( r # ""scene":"ripples""# ) & & page.contains ( r # "["andy/game","# ) & & page.contains ( r # ""repo":"andy/api""# ) ,
3480
" the ripples' data"
3481
) ;
757195ad Andy McCall 24 days ago ↶
3482
let ( _ , page) = call ( & h.app, " GET" , " /andy/game/diffusion" , None , None ) .await;
c9aacc0e Andy McCall 24 days ago ↶
3483
assert! ( page.contains ( " Takes from" ) & & page.contains ( " andy/api" ) ) ;
757195ad Andy McCall 24 days ago ↶
3484
let ( _ , overview) = call ( & h.app, " GET" , " /andy/game" , None , None ) .await;
c9aacc0e Andy McCall 24 days ago ↶
3485
assert! ( overview.contains ( " Takes from" ) & & overview.contains ( " >Diffusion</a>" ) , " the tab and the panel" ) ;
eb2a3502 Andy McCall 24 days ago ↶
3486
3487
let ( st, body) = call ( & h.app, " GET" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , None ) .await;
3488
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3489
let d: hub_api:: Diffusion = serde_json:: from_str( & body) .unwrap ( ) ;
3490
assert! ( d.diffuses_to.is_empty ( ) , " nothing pins the game" ) ;
3491
let pin = d.takes_from.first ( ) .expect ( " the game takes from the api" ) ;
3492
assert_eq! ( ( pin.upstream.as_str ( ) , pin.consumer.as_str ( ) ) , ( " andy/api" , " andy/game" ) ) ;
3493
assert_eq! ( pin.bump, Some ( p2.number) ) ;
3494
assert_eq! ( pin.upstream_state.tone, " green" , " {body}" ) ;
3495
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/diffusion" , Some ( " site" ) , None ) .await;
3496
let d: hub_api:: Diffusion = serde_json:: from_str( & body) .unwrap ( ) ;
3497
assert! ( d.takes_from.is_empty ( ) & & d.diffuses_to.iter ( ) .any ( | p | p.consumer = = " andy/game" ) , " {body}" ) ;
757195ad Andy McCall 24 days ago ↶
3498
let ( st, _ ) = call ( & h.app, " GET" , " /admin/diffusion" , None , None ) .await;
3499
assert_eq! ( st, StatusCode:: NOT_FOUND , " administrators only" ) ;
3500
3501
3502
3503
let ( st, body) =
3504
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/game/proposals/{} /land" , p2.number) , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
3505
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3506
let mut behind = serde_json:: Value:: Null;
3507
for _ in 0 .. 100 {
3508
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , Some ( " site" ) , None ) .await;
3509
let list: Vec< serde_json:: Value> = serde_json:: from_str( & list) .unwrap ( ) ;
3510
behind = list.iter ( ) .find ( | c | c[ " name" ] = = " game" ) .map ( | c | c[ " behind" ] .clone ( ) ) .unwrap_or_default ( ) ;
3511
if behind = = 0 {
3512
break ;
3513
}
3514
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3515
}
3516
assert_eq! ( behind, 0 , " the index followed the landing" ) ;
3517
180dc43a Andy McCall 24 days ago ↶
3518
3519
3520
sqlx:: query( " UPDATE repositories SET visibility = 'private' WHERE name = 'api'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
3521
seen.lock ( ) .unwrap ( ) .clear ( ) ;
3522
write ( api_work.path ( ) , " a6.c" , " v6\n " ) ;
3523
api.snapshot ( " t" ) .unwrap ( ) ;
3524
api.commit ( " api v6" , & [ ] ) .unwrap ( ) ;
3525
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4fa0da12 Andy McCall 24 days ago ↶
3526
3527
3528
let mut bump_job = None ;
3529
for _ in 0 .. 100 {
3530
pass_all ( ) .await;
3531
bump_job = seen.lock ( ) .unwrap ( ) .iter ( ) .find ( | c | c[ " repo" ] = = " andy/game" & & c[ " bookmark" ] = = " diffusion/deps/api" ) .cloned ( ) ;
3532
if bump_job.is_some ( ) {
3533
break ;
3534
}
3535
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
3536
}
3537
let bump_job = bump_job.expect ( " the bump's job ran" ) ;
180dc43a Andy McCall 24 days ago ↶
3538
let subs: Vec< & str > = bump_job[ " subrepos" ] .as_array ( ) .unwrap ( ) .iter ( ) .filter_map ( | s | s[ " path" ] .as_str ( ) ) .collect ( ) ;
3539
assert_eq! ( subs, vec! [ " deps/api" ] , " {bump_job}" ) ;
3540
3776b70d Andy McCall 24 days ago ↶
3541
3542
3543
create ( & h.app, " andy" , " game2" ) .await;
3544
let g2_work = tempfile:: tempdir( ) .unwrap ( ) ;
3545
let g2 = client ( & format! ( " {} /andy/game2" , h.base) , g2_work.path ( ) ) ;
3546
g2.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
3547
g2.snapshot ( " t" ) .unwrap ( ) ;
3548
g2.commit ( " game2 on api" , & [ ] ) .unwrap ( ) ;
3549
assert! ( g2.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3550
3551
write ( api_work.path ( ) , " a5.c" , " v5\n " ) ;
3552
api.snapshot ( " t" ) .unwrap ( ) ;
3553
api.commit ( " api v5" , & [ ] ) .unwrap ( ) ;
7e01a766 Andy McCall 21 days ago ↶
3554
let api_row = db:: repos:: get( & h.app.db, " andy" , " api" ) .await.unwrap ( ) .unwrap ( ) ;
3555
let runs_before = db:: flow:: runs( & h.app.db, api_row.id, 100 , None ) .await.unwrap ( ) .len ( ) ;
3776b70d Andy McCall 24 days ago ↶
3556
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
7e01a766 Andy McCall 21 days ago ↶
3557
3558
3559
for _ in 0 .. 300 {
3560
if db:: flow:: runs( & h.app.db, api_row.id, 100 , None ) .await.unwrap ( ) .len ( ) > runs_before {
3561
break ;
3562
}
3563
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 20 ) ) .await;
3564
}
3776b70d Andy McCall 24 days ago ↶
3565
pass_all ( ) .await;
3566
let g2_row = db:: repos:: get( & h.app.db, " andy" , " game2" ) .await.unwrap ( ) .unwrap ( ) ;
3567
for _ in 0 .. 100 {
3568
if sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = ?" )
3569
.bind ( g2_row.id)
3570
.fetch_one ( & h.app.db)
3571
.await
3572
.unwrap ( )
3573
> 0
3574
{
3575
break ;
3576
}
3577
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
3578
}
3579
assert! ( db:: proposals:: list( & h.app.db, g2_row.id, & [ ] ) .await.unwrap ( ) .is_empty ( ) , " off until turned on" ) ;
3580
let ( st, _ ) = call ( & h.app, " PUT" , " /api/v1/repos/andy/game2/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green"}"# .into ( ) ) ) .await;
3581
assert_eq! ( st, StatusCode:: OK ) ;
3582
let mut caught = None ;
3583
for _ in 0 .. 100 {
3584
if let Some ( p) = db:: proposals:: list( & h.app.db, g2_row.id, & [ " open" ] ) .await.unwrap ( ) .into_iter ( ) .next ( ) {
3585
caught = Some ( p) ;
3586
break ;
3587
}
3588
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3589
}
3590
let caught = caught.expect ( " bumped as soon as it was turned on" ) ;
3591
assert! ( caught.body.contains ( " api v5" ) & & caught.title.contains ( " 1 change" ) , " {:?} {:?}" , caught.title, caught.body) ;
a7e6e8e6 Andy McCall 24 days ago ↶
3592
}
3593
3594
3595
async fn jobs_left ( app : & Shared) -> bool {
3596
sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM flow_jobs WHERE status IN ('queued', 'running')" )
3597
.fetch_one ( & app.db)
3598
.await
3599
.unwrap_or ( 0 )
3600
> 0
9aaa88b2 Andy McCall 24 days ago ↶
3601
}
3602
3603
3604
3605
# [ tokio ::test ( flavor = " multi_thread" ) ]
3606
async fn bumps_from_an_upstream_without_checks_are_marked_unverified ( ) {
3607
let h = hub ( ) .await;
3608
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3609
create ( & h.app, " andy" , " lib" ) .await;
3610
create ( & h.app, " andy" , " app" ) .await;
3611
let lib_work = tempfile:: tempdir( ) .unwrap ( ) ;
3612
let lib = client ( & format! ( " {} /andy/lib" , h.base) , lib_work.path ( ) ) ;
3613
write ( lib_work.path ( ) , " lib.h" , " v1\n " ) ;
3614
lib.snapshot ( " t" ) .unwrap ( ) ;
3615
lib.commit ( " lib v1" , & [ ] ) .unwrap ( ) ;
3616
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3617
let app_work = tempfile:: tempdir( ) .unwrap ( ) ;
3618
let app = client ( & format! ( " {} /andy/app" , h.base) , app_work.path ( ) ) ;
3619
app.submodule_add ( & format! ( " {} /andy/lib" , h.base) , " deps/lib" , Some ( " main" ) ) .unwrap ( ) ;
c4e432da Andy McCall 24 days ago ↶
3620
3621
let modules = std:: fs:: read_to_string( app_work.path ( ) .join ( " .oxymodules" ) ) .unwrap ( ) ;
3622
write ( app_work.path ( ) , " .oxymodules" , & format! ( " {modules} watch = lib.c\n " ) ) ;
9aaa88b2 Andy McCall 24 days ago ↶
3623
app.snapshot ( " t" ) .unwrap ( ) ;
3624
app.commit ( " app on lib" , & [ ] ) .unwrap ( ) ;
3625
assert! ( app.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3626
let app_row = db:: repos:: get( & h.app.db, " andy" , " app" ) .await.unwrap ( ) .unwrap ( ) ;
3627
for _ in 0 .. 100 {
3628
if sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = ?" )
3629
.bind ( app_row.id)
3630
.fetch_one ( & h.app.db)
3631
.await
3632
.unwrap ( )
3633
> 0
3634
{
3635
break ;
3636
}
3637
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
3638
}
3639
let ( st, _ ) = call ( & h.app, " PUT" , " /api/v1/repos/andy/app/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green"}"# .into ( ) ) ) .await;
3640
assert_eq! ( st, StatusCode:: OK ) ;
3641
write ( lib_work.path ( ) , " lib.c" , " v2\n " ) ;
3642
lib.snapshot ( " t" ) .unwrap ( ) ;
3643
lib.commit ( " lib v2" , & [ ] ) .unwrap ( ) ;
3644
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3645
let mut found = None ;
3646
for _ in 0 .. 100 {
3647
if let Some ( p) = db:: proposals:: list( & h.app.db, app_row.id, & [ " open" ] ) .await.unwrap ( ) .into_iter ( ) .next ( ) {
3648
found = Some ( p) ;
3649
break ;
3650
}
3651
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3652
}
3653
let p = found.expect ( " a bump although the upstream never goes green" ) ;
3654
assert! ( p.body.contains ( " Upstream unverified" ) , " {}" , p.body) ;
c4e432da Andy McCall 24 days ago ↶
3655
assert! ( p.body.contains ( " Touches what you watch" ) & & p.body.contains ( " touches `lib.c`" ) & & p.body.contains ( " ⚑" ) , " {}" , p.body) ;
9aaa88b2 Andy McCall 24 days ago ↶
3656
let mut labels = Vec :: new( ) ;
3657
for _ in 0 .. 100 {
3658
labels = db:: issues:: proposal_labels( & h.app.db, p.id) .await.unwrap ( ) .into_iter ( ) .map ( | l | l.name ) .collect:: < Vec< String> > ( ) ;
3659
if labels.len ( ) = = 2 {
3660
break ;
3661
}
3662
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
3663
}
3664
assert! ( labels.contains ( & " upstream unverified" .to_string ( ) ) & & labels.contains ( & " dependencies" .to_string ( ) ) , " {labels:?}" ) ;
a7e6e8e6 Andy McCall 24 days ago ↶
3665
}
d943160d Andy McCall 24 days ago ↶
3666
3667
3668
3669
# [ tokio ::test ( flavor = " multi_thread" ) ]
3670
async fn a_moved_target_runs_its_open_proposals_again ( ) {
3671
let h = hub ( ) .await;
3672
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3673
create ( & h.app, " andy" , " demo" ) .await;
3674
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3675
let mut c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
3676
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3677
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3678
c.snapshot ( " t" ) .unwrap ( ) ;
3679
let base = c.commit ( " base" , & [ ] ) .unwrap ( ) ;
3680
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3681
3682
for ( b, file) in [ ( " fix-a" , " a.txt" ) , ( " fix-b" , " b.txt" ) ] {
3683
c.bookmark_create ( b, Some ( & [ base] ) ) .unwrap ( ) ;
3684
c.bookmark_switch ( b, true ) .unwrap ( ) ;
3685
write ( work.path ( ) , file, " x\n " ) ;
3686
c.snapshot ( " t" ) .unwrap ( ) ;
3687
c.commit ( & format! ( " add {file} " ) , & [ ] ) .unwrap ( ) ;
3688
assert! ( c.push_bookmark ( " origin" , b, false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3689
let body = serde_json:: json! ( { " title" : b, " target" : " main" , " source" : b } ) .to_string ( ) ;
3690
let ( st, out) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/proposals" , Some ( " site" ) , Some ( body) ) .await;
3691
assert_eq! ( st, StatusCode:: CREATED , " {out}" ) ;
3692
}
3693
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
3694
let props = db:: proposals:: list( & h.app.db, row.id, & [ " open" ] ) .await.unwrap ( ) ;
3695
let a = props.iter ( ) .find ( | p | p.source_bookmark = = " fix-a" ) .unwrap ( ) .number;
3696
let b = props.iter ( ) .find ( | p | p.source_bookmark = = " fix-b" ) .unwrap ( ) .number;
3697
3698
let ( st, out) = call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/demo/proposals/{a} /land" ) , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
3699
assert_eq! ( st, StatusCode:: OK , " {out}" ) ;
3700
let label = format! ( " proposal/{b} " ) ;
3701
let mut found = None ;
3702
for _ in 0 .. 100 {
3703
if let Some ( r) = runs_of ( & h.app, " andy" , " demo" ) .await.into_iter ( ) .find ( | r | r.bookmark = = label ) {
3704
found = Some ( r) ;
3705
break ;
3706
}
3707
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3708
}
3709
let run = found.expect ( " a run of fix-b's new projected state" ) ;
3710
let projected = db:: proposals:: get( & h.app.db, row.id, b) .await.unwrap ( ) .unwrap ( ) ;
3711
assert_eq! ( ( run.trigger.as_str ( ) , run.state.as_str ( ) ) , ( " proposal" , projected.projected_state.as_str ( ) ) , " on its projected state" ) ;
3712
3713
let before = runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) ;
3714
oxygenhub:: web:: proposals:: settle_after_push( & h.app, & row, " main" , " andy" ) .await;
3715
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , before, " not again for the same state" ) ;
3716
}
93501da2 Andy McCall 23 days ago ↶
3717
3718
3719
3720
async fn send_image ( app : & Shared, image : & [u8 ]) -> ( String, serde_json:: Value) {
3721
let mut chunks = std:: collections:: HashMap:: new( ) ;
3722
let m = jet:: images:: scan( image, None , | c , b | {
3723
chunks.insert ( c.hash ( ) .to_string ( ) , b.to_vec ( ) ) ;
3724
Ok ( ( ) )
3725
} )
3726
.unwrap ( ) ;
3727
for c in m.needed ( ) {
3728
let ( st, body) = put_bytes ( app, & format! ( " /api/v1/flow/images/chunks/{c} " ) , " site" , chunks[ c] .clone ( ) ) .await;
3729
assert_eq! ( st, StatusCode:: NO_CONTENT , " {body}" ) ;
3730
}
3731
let ( st, body) =
3732
call ( app, " PUT" , & format! ( " /api/v1/flow/images/{} " , m.hash) , Some ( " site" ) , Some ( serde_json:: to_string( & m) .unwrap ( ) ) ) .await;
3733
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
3734
( m.hash.clone ( ) , serde_json:: from_str( & body) .unwrap ( ) )
3735
}
3736
3737
3738
3739
3740
3741
# [ tokio ::test ( flavor = " multi_thread" ) ]
3742
async fn an_image_names_its_own_architecture_and_a_pass_teaches_the_rest ( ) {
3743
use oxy_core:: identity:: SecretKey;
3744
let h = hub ( ) .await;
3745
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3746
3747
let dir = tempfile:: tempdir( ) .unwrap ( ) ;
3748
let tree = dir.path ( ) .join ( " tree" ) ;
3749
std:: fs:: create_dir_all( tree.join ( " bin" ) ) .unwrap ( ) ;
3750
let mut elf = vec! [ 0 u8 ; 64 ] ;
3751
elf[ .. 7 ] .copy_from_slice ( b " \x7f ELF\x02 \x01 \x01 " ) ;
3752
elf[ 18 .. 20 ] .copy_from_slice ( & 0x3E u16 .to_le_bytes ( ) ) ;
3753
std:: fs:: write( tree.join ( " bin/sh" ) , & elf) .unwrap ( ) ;
3754
let img = dir.path ( ) .join ( " rootfs.ext4" ) ;
3755
let made = std:: process:: Command:: new( " mkfs.ext4" ) .args ( [ " -q" , " -F" , " -d" ] ) .arg ( & tree) .arg ( & img) .arg ( " 8M" ) .status ( ) ;
3756
if ! made.is_ok_and ( | s | s.success ( ) ) {
3757
eprintln! ( " mkfs.ext4 not found: skipped" ) ;
3758
return ;
3759
}
3760
let ( x86, answer) = send_image ( & h.app, & std:: fs:: read( & img) .unwrap ( ) ) .await;
3761
assert_eq! ( answer[ " arch" ] , " x86_64" , " read from its shell: {answer}" ) ;
3762
let ( _ , body) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{x86} " ) , Some ( " runner-secret" ) , None ) .await;
3763
assert_eq! ( serde_json:: from_str:: < jet:: images:: Manifest> ( & body) .unwrap ( ) .arch.as_deref ( ) , Some ( " x86_64" ) , " runners fetch it named" ) ;
3764
3765
let blob: Vec< u8 > = ( 0 .. 300_000 u32 ) .map ( | i | ( i % 241 ) as u8 ) .collect ( ) ;
3766
let ( unread, answer) = send_image ( & h.app, & blob) .await;
3767
assert! ( answer[ " arch" ] .is_null ( ) , " {answer}" ) ;
3768
let archs = db:: flow:: image_archs( & h.app.db) .await.unwrap ( ) ;
3769
assert_eq! ( ( archs.get ( & x86) .map ( String :: as_str) , archs.get ( & unread) ) , ( Some ( " x86_64" ) , None ) ) ;
3770
3771
sqlx:: query( " UPDATE jet_images SET arch = ''" ) .execute ( & h.app.db) .await.unwrap ( ) ;
3772
sqlx:: query( " DELETE FROM flow_images" ) .execute ( & h.app.db) .await.unwrap ( ) ;
3773
assert_eq! ( oxygenhub:: images:: learn_archs( & h.app) .await.unwrap ( ) , 1 ) ;
3774
assert_eq! ( db:: flow:: image_archs( & h.app.db) .await.unwrap ( ) .get ( & x86) .map ( String :: as_str) , Some ( " x86_64" ) ) ;
3775
3776
3777
create ( & h.app, " andy" , " arch" ) .await;
3778
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3779
let c = client ( & format! ( " {} /andy/arch" , h.base) , work.path ( ) ) ;
3780
let kdl = format! (
3781
" schema-version 1\n sandbox \" unread\" {{ \n rootfs \" cas://blob/{unread} \" \n }} \n sandbox \" x86\" {{ \n rootfs \" cas://blob/{x86} \" \n }} \n \
3782
job \" first\" {{ \n sandbox \" unread\" \n exec \" make\" \n }} \n job \" second\" {{ \n sandbox \" x86\" \n exec \" make\" \n depends-on \" first\" \n }} \n \
3783
job \" third\" {{ \n sandbox \" unread\" \n exec \" make\" \n depends-on \" first\" \n }} \n "
3784
) ;
3785
write ( work.path ( ) , " .flow/pipeline.kdl" , & kdl) ;
3786
c.commit ( " Jobs on two images" , & [ ] ) .unwrap ( ) ;
3787
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3788
runs_of ( & h.app, " andy" , " arch" ) .await;
3789
3790
3791
let x86_info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
3792
let x86_claim =
3793
serde_json:: json! ( { " runner" : " earth" , " runtimes" : [ " microvm" ] , " images" : [ ] , " fetches_images" : true , " info" : x86_info } ) ;
3794
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( x86_claim.to_string ( ) ) ) .await;
3795
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3796
let first: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
3797
assert_eq! ( first[ " job" ] [ " name" ] , " first" ) ;
3798
let ph = first[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) ;
3799
let id = first[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3800
assert_eq! (
3801
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : receipt ( " first" , ph, 0 ) } ) ) .await,
3802
StatusCode:: NO_CONTENT
3803
) ;
3804
assert_eq! ( db:: flow:: image_archs( & h.app.db) .await.unwrap ( ) .get ( & unread) .map ( String :: as_str) , Some ( " x86_64" ) , " learned" ) ;
3805
3806
3807
let token = " flowreg_narvi" ;
3808
db:: runners:: add_registration( & h.app.db, & blake3:: hash( token.as_bytes ( ) ) .to_hex ( ) , " admin" , & Default :: default( ) ) .await.unwrap ( ) ;
3809
let identity = SecretKey:: generate( ) .identity ( " narvi" , " " , 1 ) ;
3810
let info = serde_json:: json! ( { " arch" : " riscv64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
3811
let reg = serde_json:: json! ( { " token" : token, " name" : " narvi" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
3812
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
3813
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3814
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
3815
let narvi =
3816
serde_json:: json! ( { " runner" : " narvi" , " runtimes" : [ " microvm" ] , " images" : [ ] , " fetches_images" : true , " info" : info } ) .to_string ( ) ;
3817
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( & credential) , Some ( narvi) ) .await;
3818
assert_eq! ( st, StatusCode:: NO_CONTENT , " the second job's image is x86_64 and the third's learned so: {body}" ) ;
3819
for _ in 0 .. 2 {
3820
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( x86_claim.to_string ( ) ) ) .await;
3821
assert_eq! ( st, StatusCode:: OK , " both go to x86_64: {body}" ) ;
3822
}
3823
}
f4d6ef41 Andy McCall 23 days ago ↶
3824
3825
3826
3827
3828
3829
# [ tokio ::test ( flavor = " multi_thread" ) ]
3830
async fn a_consumer_that_chose_auto_lands_green_bumps_itself ( ) {
3831
let h = hub ( ) .await;
3832
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3833
create ( & h.app, " andy" , " api" ) .await;
3834
create ( & h.app, " andy" , " game" ) .await;
3835
let hub_app = & h.app;
3836
3837
let pass_all = move | | async move {
3838
for _ in 0 .. 20 {
3839
let Some ( a) = claim ( hub_app) .await else {
3840
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
3841
if ! jobs_left ( hub_app) .await {
3842
break ;
3843
}
3844
continue ;
3845
} ;
3846
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3847
let ( name, ph) = ( a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
3848
assert_eq! (
3849
finish ( hub_app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, 0 ) } ) ) .await,
3850
StatusCode:: NO_CONTENT
3851
) ;
3852
}
3853
} ;
3854
let api_work = tempfile:: tempdir( ) .unwrap ( ) ;
3855
let api = client ( & format! ( " {} /andy/api" , h.base) , api_work.path ( ) ) ;
3856
write ( api_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3857
write ( api_work.path ( ) , " test.sh" , " echo ok\n " ) ;
3858
api.snapshot ( " t" ) .unwrap ( ) ;
3859
api.commit ( " api v1" , & [ ] ) .unwrap ( ) ;
3860
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3861
pass_all ( ) .await;
3862
let game_work = tempfile:: tempdir( ) .unwrap ( ) ;
3863
let game = client ( & format! ( " {} /andy/game" , h.base) , game_work.path ( ) ) ;
3864
game.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
3865
write ( game_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3866
write ( game_work.path ( ) , " test.sh" , " echo ok\n " ) ;
3867
game.snapshot ( " t" ) .unwrap ( ) ;
3868
game.commit ( " game on api v1" , & [ ] ) .unwrap ( ) ;
3869
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3870
pass_all ( ) .await;
3871
let game_row = db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
3872
for _ in 0 .. 100 {
3873
let n: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = ?" )
3874
.bind ( game_row.id)
3875
.fetch_one ( & h.app.db)
3876
.await
3877
.unwrap ( ) ;
3878
if n > 0 {
3879
break ;
3880
}
3881
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
3882
}
3883
let ( st, body) =
3884
call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green","land":"auto"}"# .into ( ) ) ) .await;
3885
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3886
assert! ( body.contains ( r # ""land":"auto""# ) , " {body}" ) ;
3887
let ( st, _ ) =
3888
call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green","land":"soon"}"# .into ( ) ) ) .await;
3889
assert_eq! ( st, StatusCode:: BAD_REQUEST ) ;
3890
3891
3892
3893
write ( api_work.path ( ) , " a2.c" , " v2\n " ) ;
3894
api.snapshot ( " t" ) .unwrap ( ) ;
3895
api.commit ( " api v2: faster sprites" , & [ ] ) .unwrap ( ) ;
3896
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3897
let mut landed = None ;
3898
for _ in 0 .. 100 {
3899
pass_all ( ) .await;
3900
let all = db:: proposals:: list( & h.app.db, game_row.id, & [ ] ) .await.unwrap ( ) ;
3901
if let Some ( p) = all.into_iter ( ) .find ( | p | p.source_bookmark = = " diffusion/deps/api" & & p.state = = " landed" ) {
3902
landed = Some ( p) ;
3903
break ;
3904
}
3905
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3906
}
3907
let p = landed.expect ( " landed by itself once both halves were green" ) ;
3908
assert! ( p.body.contains ( " lands its bumps itself" ) , " {}" , p.body) ;
3909
let events = db:: proposals:: events( & h.app.db, p.id) .await.unwrap ( ) ;
3910
let landing = events.iter ( ) .find ( | e | e.event = = " landed" ) .expect ( " a landing on the timeline" ) ;
3911
assert_eq! ( landing.actor, " diffusion" ) ;
3912
assert! ( landing.detail.contains ( " Diffusion, both halves green" ) , " {}" , landing.detail) ;
3913
3914
let mut behind = serde_json:: Value:: Null;
3915
for _ in 0 .. 100 {
3916
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , Some ( " site" ) , None ) .await;
3917
let list: Vec< serde_json:: Value> = serde_json:: from_str( & list) .unwrap ( ) ;
3918
behind = list.iter ( ) .find ( | c | c[ " name" ] = = " game" ) .map ( | c | c[ " behind" ] .clone ( ) ) .unwrap_or_default ( ) ;
3919
if behind = = 0 {
3920
break ;
3921
}
3922
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3923
}
3924
assert_eq! ( behind, 0 , " main moved to the bump" ) ;
3925
3926
3927
3928
create ( & h.app, " andy" , " lib" ) .await;
3929
create ( & h.app, " andy" , " app" ) .await;
3930
let lib_work = tempfile:: tempdir( ) .unwrap ( ) ;
3931
let lib = client ( & format! ( " {} /andy/lib" , h.base) , lib_work.path ( ) ) ;
3932
write ( lib_work.path ( ) , " lib.h" , " v1\n " ) ;
3933
lib.snapshot ( " t" ) .unwrap ( ) ;
3934
lib.commit ( " lib v1" , & [ ] ) .unwrap ( ) ;
3935
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3936
let app_work = tempfile:: tempdir( ) .unwrap ( ) ;
3937
let app = client ( & format! ( " {} /andy/app" , h.base) , app_work.path ( ) ) ;
3938
app.submodule_add ( & format! ( " {} /andy/lib" , h.base) , " deps/lib" , Some ( " main" ) ) .unwrap ( ) ;
3939
let modules = std:: fs:: read_to_string( app_work.path ( ) .join ( " .oxymodules" ) ) .unwrap ( ) ;
3940
write ( app_work.path ( ) , " .oxymodules" , & format! ( " {modules} follow = green\n land = auto\n " ) ) ;
3941
write ( app_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3942
write ( app_work.path ( ) , " test.sh" , " echo ok\n " ) ;
3943
app.snapshot ( " t" ) .unwrap ( ) ;
3944
app.commit ( " app on lib" , & [ ] ) .unwrap ( ) ;
3945
assert! ( app.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3946
pass_all ( ) .await;
3947
let app_row = db:: repos:: get( & h.app.db, " andy" , " app" ) .await.unwrap ( ) .unwrap ( ) ;
3948
for _ in 0 .. 100 {
3949
let n: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = ?" )
3950
.bind ( app_row.id)
3951
.fetch_one ( & h.app.db)
3952
.await
3953
.unwrap ( ) ;
3954
if n > 0 {
3955
break ;
3956
}
3957
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
3958
}
3959
write ( lib_work.path ( ) , " lib.c" , " v2\n " ) ;
3960
lib.snapshot ( " t" ) .unwrap ( ) ;
3961
lib.commit ( " lib v2" , & [ ] ) .unwrap ( ) ;
3962
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3963
let mut passed = false ;
3964
for _ in 0 .. 100 {
3965
pass_all ( ) .await;
3966
let s: Option< String> = sqlx:: query_scalar(
3967
" SELECT status FROM flow_runs WHERE repo_id = ? AND bookmark = 'diffusion/deps/lib' ORDER BY id DESC LIMIT 1" ,
3968
)
3969
.bind ( app_row.id)
3970
.fetch_optional ( & h.app.db)
3971
.await
3972
.unwrap ( ) ;
3973
if s.as_deref ( ) = = Some ( " passed" ) {
3974
passed = true ;
3975
break ;
3976
}
3977
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3978
}
3979
assert! ( passed, " the app's run on the bump passed" ) ;
3980
assert_eq! ( oxygenhub:: diffusion:: auto_land( & h.app, & app_row, " deps/lib" ) .await, Err ( oxygenhub:: diffusion:: Held:: Unverified) ) ;
3981
let open = db:: proposals:: list( & h.app.db, app_row.id, & [ " open" ] ) .await.unwrap ( ) ;
3982
assert! ( open.iter ( ) .any ( | p | p.source_bookmark = = " diffusion/deps/lib" ) , " amber waits for a person" ) ;
3983
}
e8fa5369 Andy McCall 23 days ago ↶
3984
3985
3986
3987
3988
3989
# [ tokio ::test ( flavor = " multi_thread" ) ]
3990
async fn one_job_reruns_with_what_depends_on_it ( ) {
3991
let h = hub ( ) .await;
3992
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3993
create ( & h.app, " andy" , " flaky" ) .await;
3994
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3995
let c = client ( & format! ( " {} /andy/flaky" , h.base) , work.path ( ) ) ;
3996
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3997
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3998
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
3999
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4000
let run = runs_of ( & h.app, " andy" , " flaky" ) .await[ 0 ] .clone ( ) ;
4001
4002
let ( st, _ ) =
4003
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/test/rerun" , run.number) , Some ( " site" ) , None ) .await;
4004
assert_eq! ( st, StatusCode:: CONFLICT , " not while the run is live" ) ;
4005
while let Some ( j) = claim ( & h.app) .await {
4006
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4007
let ( name, ph) = ( j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4008
let exit = if name = = " test" { 1 } else { 0 } ;
4009
let log = serde_json:: json! ( { " records" : [ { " t" : 1 , " stream" : " out" , " text" : format! ( " {name} ran" ) } ] } ) ;
4010
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( log.to_string ( ) ) ) .await;
4011
assert_eq! (
4012
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : exit, " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, exit as u32 ) } ) )
4013
.await,
4014
StatusCode:: NO_CONTENT
4015
) ;
4016
}
4017
let status = | n : & 'static str | {
4018
let ( app, run_id) = ( h.app.clone ( ) , run.id) ;
4019
async move { db:: flow:: jobs( & app.db, run_id) .await.unwrap ( ) .into_iter ( ) .find ( | j | j.name = = n ) .unwrap ( ) }
4020
} ;
4021
assert_eq! ( db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) .status, " failed" ) ;
4022
let failed_test = status ( " test" ) .await;
4023
4024
4025
let ( st, body) =
4026
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/test/rerun" , run.number) , Some ( " site" ) , None ) .await;
4027
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4028
assert! ( body.contains ( r # ""queued":1"# ) , " {body}" ) ;
4029
assert_eq! (
4030
( status ( " build" ) .await.status, status ( " lint" ) .await.status, status ( " test" ) .await.status) ,
4031
( " passed" .into ( ) , " passed" .into ( ) , " queued" .into ( ) )
4032
) ;
4033
assert! ( db:: flow:: log( & h.app.db, failed_test.id, 0 , 100 ) .await.unwrap ( ) .is_empty ( ) , " its old log is gone" ) ;
4034
assert! ( db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) .live ( ) ) ;
4035
let checks = checks_when ( & h.app, run.repo_id, & run.state, 3 ) .await;
4036
assert_eq! ( checks.iter ( ) .find ( | c | c.name = = " flow/test" ) .map ( | c | c.status.as_str ( ) ) , Some ( " queued" ) ) ;
4037
let j = claim ( & h.app) .await.expect ( " the test, queued again" ) ;
4038
assert_eq! ( j[ " job" ] [ " name" ] , " test" ) ;
4039
let ph = j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
4040
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4041
assert_eq! (
4042
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( " test" , & ph, 0 ) } ) ) .await,
4043
StatusCode:: NO_CONTENT
4044
) ;
4045
assert_eq! ( db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) .status, " passed" , " the run is whole again" ) ;
bff9106e Andy McCall 23 days ago ↶
4046
4047
let andy_id = db:: accounts:: account_by_name( & h.app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
4048
let kinds: Vec< String> = db:: notify:: inbox( & h.app.db, andy_id, 20 ) .await.unwrap ( ) .into_iter ( ) .map ( | n | n.kind ) .collect ( ) ;
4049
assert! ( kinds.contains ( & " flow.failed" .to_string ( ) ) & & kinds.contains ( & " flow.fixed" .to_string ( ) ) , " {kinds:?}" ) ;
e8fa5369 Andy McCall 23 days ago ↶
4050
4051
4052
let ( _ , body) =
4053
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/build/rerun" , run.number) , Some ( " site" ) , None ) .await;
4054
assert! ( body.contains ( r # ""queued":2"# ) , " {body}" ) ;
4055
assert_eq! (
4056
( status ( " build" ) .await.status, status ( " lint" ) .await.status, status ( " test" ) .await.status) ,
4057
( " queued" .into ( ) , " passed" .into ( ) , " queued" .into ( ) )
4058
) ;
4059
let ( st, _ ) =
4060
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/deploy/rerun" , run.number) , Some ( " site" ) , None ) .await;
4061
assert_eq! ( st, StatusCode:: CONFLICT , " live again, and no such job either way" ) ;
4062
}
4a4400c5 Andy McCall 23 days ago ↶
4063
4064
4065
4066
4067
4068
# [ tokio ::test ( flavor = " multi_thread" ) ]
4069
async fn failed_runs_are_told_and_an_erroring_runner_is_drained ( ) {
4070
let h = hub ( ) .await;
4071
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4072
let bob = db:: accounts:: create_account( & h.app.db, " bob" , " [email protected] " , " x" , " salt" , false ) .await.unwrap ( ) ;
4073
create ( & h.app, " andy" , " alerts" ) .await;
4074
let row = db:: repos:: get( & h.app.db, " andy" , " alerts" ) .await.unwrap ( ) .unwrap ( ) ;
4075
db:: notify:: set_watch( & h.app.db, bob.id, row.id, true ) .await.unwrap ( ) ;
4076
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4077
let c = client ( & format! ( " {} /andy/alerts" , h.base) , work.path ( ) ) ;
4078
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4079
write ( work.path ( ) , " test.sh" , " exit 1\n " ) ;
4080
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
4081
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4082
let run = runs_of ( & h.app, " andy" , " alerts" ) .await[ 0 ] .clone ( ) ;
4083
sqlx:: query( " UPDATE flow_runs SET actor = 'andy' WHERE id = ?" ) .bind ( run.id) .execute ( & h.app.db) .await.unwrap ( ) ;
4084
while let Some ( j) = claim ( & h.app) .await {
4085
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4086
let ( name, ph) = ( j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4087
let exit = if name = = " test" { 1 } else { 0 } ;
4088
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : exit, " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, exit as u32 ) } ) ) .await;
4089
}
4090
let told = | id : i64 | {
4091
let db = h.app.db.clone ( ) ;
4092
async move { db:: notify:: inbox( & db, id, 20 ) .await.unwrap ( ) .into_iter ( ) .filter ( | n | n.kind = = " flow.failed" ) .collect:: < Vec< _ > > ( ) }
4093
} ;
4094
let ( for_andy, for_bob) = ( told ( andy.id) .await, told ( bob.id) .await) ;
4095
assert_eq! ( ( for_andy.len ( ) , for_bob.len ( ) ) , ( 1 , 1 ) , " the pusher and a watcher" ) ;
4096
assert! ( for_andy[ 0 ] .title.contains ( & format! ( " run #{} on main failed (test)" , run.number) ) , " {}" , for_andy[ 0 ] .title) ;
4097
assert_eq! ( for_andy[ 0 ] .url, format! ( " /andy/alerts/flow/{} " , run.number) ) ;
4098
4099
4100
let runner_id = | | {
4101
let db = h.app.db.clone ( ) ;
4102
async move { sqlx:: query_scalar:: < _ , i64 > ( " SELECT id FROM flow_runners WHERE name = 'test-1'" ) .fetch_one ( & db) .await.unwrap ( ) }
4103
} ;
4104
let mut errors = 0 ;
4105
while errors < 5 {
4106
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/alerts/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
4107
assert_eq! ( st, StatusCode:: CREATED ) ;
4108
while let Some ( j) = claim ( & h.app) .await {
4109
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4110
let st = finish (
4111
& h.app,
4112
id,
4113
serde_json:: json! ( { " exit_code" : null, " duration_ms" : 10 , " error" : " the microVM did not start: /dev/kvm busy" } ) ,
4114
)
4115
.await;
4116
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
4117
errors + = 1 ;
4118
if errors = = 5 {
4119
break ;
4120
}
4121
}
4122
}
4123
let state = | id : i64 | {
4124
let db = h.app.db.clone ( ) ;
4125
async move { db:: runners:: get( & db, id) .await.unwrap ( ) .unwrap ( ) .state }
4126
} ;
4127
let id = runner_id ( ) .await;
4128
assert_eq! ( state ( id) .await, " draining" ) ;
4129
let drained: Vec< _ > =
4130
db:: notify:: inbox( & h.app.db, andy.id, 50 ) .await.unwrap ( ) .into_iter ( ) .filter ( | n | n.kind = = " flow.runner_drained" ) .collect ( ) ;
4131
assert_eq! ( drained.len ( ) , 1 , " the administrator is told" ) ;
4132
assert! ( drained[ 0 ] .title.contains ( " /dev/kvm busy" ) , " {}" , drained[ 0 ] .title) ;
4133
assert! (
4134
! told ( bob.id) .await.is_empty ( )
4135
& & db:: notify:: inbox( & h.app.db, bob.id, 50 ) .await.unwrap ( ) .iter ( ) .all ( | n | n.kind ! = " flow.runner_drained" )
4136
) ;
4137
4138
db:: runners:: set_state( & h.app.db, id, " active" ) .await.unwrap ( ) ;
4139
assert_eq! ( db:: runners:: erroring( & h.app.db, id, 5 ) .await.unwrap ( ) , None ) ;
4140
}
1870e4fa Andy McCall 21 days ago ↶
4141
4142
4143
4144
# [ tokio ::test ( flavor = " multi_thread" ) ]
4145
async fn owners_run_flow_on_their_own_runners_only ( ) {
4146
use oxy_core:: identity:: SecretKey;
4147
let h = hub ( ) .await;
4148
let register = | name : & 'static str | {
4149
let app = h.app.clone ( ) ;
4150
async move {
4151
let body = format! ( " username={name} &email={name} %40example.com&password=correct-horse-battery" ) ;
4152
let res = oxygenhub:: router( app)
4153
.oneshot (
4154
Request:: post( " /register" )
4155
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
4156
.body ( Body:: from( body) )
4157
.unwrap ( ) ,
4158
)
4159
.await
4160
.unwrap ( ) ;
4161
res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( )
4162
}
4163
} ;
4164
register ( " andy" ) .await;
4165
let bob = register ( " bob" ) .await;
4166
create ( & h.app, " bob" , " game" ) .await;
4167
let web = | method : & 'static str , path : & str , body : Option< String> | {
4168
let ( app, cookie, path) = ( h.app.clone ( ) , bob.clone ( ) , path.to_string ( ) ) ;
4169
async move {
4170
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie) ;
4171
if body.is_some ( ) {
4172
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
4173
}
4174
let res = oxygenhub:: router( app) .oneshot ( req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ) .await.unwrap ( ) ;
4175
String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( )
4176
}
4177
} ;
4178
let csrf_of = | page : & str | {
4179
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
4180
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
4181
} ;
4182
4183
let page = web ( " GET" , " /bob/game/settings" , None ) .await;
4184
assert! ( page.contains ( " register a runner for this repository" ) & & ! page.contains ( " value=\" own\" " ) , " {page}" ) ;
4185
let page = web ( " POST" , " /bob/game/settings/flow" , Some ( format! ( " _csrf={} &flow=own" , csrf_of ( & page) ) ) ) .await;
4186
assert! ( page.contains ( " Register a runner for this repository" ) , " {page}" ) ;
4187
4188
let page = web ( " GET" , " /bob/game/settings" , None ) .await;
4189
let page = web ( " POST" , " /bob/game/settings/runners/token" , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
4190
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
4191
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
4192
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
4193
let identity = SecretKey:: generate( ) .identity ( " bobs" , " " , 1 ) ;
4194
let reg = serde_json:: json! ( { " token" : token, " name" : " bobs" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
4195
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
4196
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4197
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
4198
4199
let page = web ( " GET" , " /bob/game/settings" , None ) .await;
4200
assert! ( page.contains ( " value=\" own\" " ) & & ! page.contains ( " value=\" on\" " ) , " {page}" ) ;
4201
let page = web ( " POST" , " /bob/game/settings/flow" , Some ( format! ( " _csrf={} &flow=on" , csrf_of ( & page) ) ) ) .await;
4202
assert! ( page.contains ( " Only site administrators let the site's runners" ) , " {page}" ) ;
4203
let page = web ( " POST" , " /bob/game/settings/flow" , Some ( format! ( " _csrf={} &flow=own" , csrf_of ( & page) ) ) ) .await;
4204
assert! ( page.contains ( " value=\" own\" checked" ) & & page.contains ( " start runs" ) , " {page}" ) ;
4205
assert_eq! ( db:: repos:: get( & h.app.db, " bob" , " game" ) .await.unwrap ( ) .unwrap ( ) .flow, " own" ) ;
4206
4207
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4208
let c = client ( & format! ( " {} /bob/game" , h.base) , work.path ( ) ) ;
4209
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4210
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
4211
c.snapshot ( " t" ) .unwrap ( ) ;
4212
c.commit ( " A pipeline" , & [ ] ) .unwrap ( ) ;
4213
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4214
assert_eq! ( runs_of ( & h.app, " bob" , " game" ) .await.len ( ) , 1 , " a run started" ) ;
4215
assert! ( claim ( & h.app) .await.is_none ( ) , " the site's runner is not offered it" ) ;
4216
let body = serde_json:: json! ( { " runner" : " ignored" , " runtimes" : [ " microvm" ] , " info" : info } ) .to_string ( ) ;
4217
let ( st, job) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( & credential) , Some ( body) ) .await;
4218
assert_eq! ( st, StatusCode:: OK , " {job}" ) ;
4219
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & job) .unwrap ( ) [ " repo" ] , " bob/game" ) ;
4220
}
cd437c4f Andy McCall 21 days ago ↶
4221
4222
# [ tokio ::test ( flavor = " multi_thread" ) ]
4223
async fn a_job_takes_the_artifact_its_producer_kept ( ) {
4224
let h = hub ( ) .await;
4225
create ( & h.app, " andy" , " demo" ) .await;
4226
let url = format! ( " {} /andy/demo" , h.base) ;
4227
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4228
let c = client ( & url, work.path ( ) ) ;
4229
write (
4230
work.path ( ) ,
4231
" .flow/pipeline.kdl" ,
4232
& format! (
4233
" schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" plan\" {{ \n sandbox \" alpine\" \n exec \" plan\" \n output {{ \n artifact \" tfplan\" {{ \n path \" /src/out/tfplan\" \n }} \n }} \n }} \n job \" apply\" {{ \n sandbox \" alpine\" \n depends-on \" plan\" \n input {{ \n artifact \" tfplan\" from \" plan\" \n path \" /src/tfplan\" \n }} \n exec \" apply\" \n }} \n "
4234
) ,
4235
) ;
4236
c.snapshot ( " test" ) .unwrap ( ) ;
4237
c.commit ( " Plan, then apply that plan" , & [ ] ) .unwrap ( ) ;
4238
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4239
4240
4241
let runs = runs_of ( & h.app, " andy" , " demo" ) .await;
4242
assert_eq! ( runs.len ( ) , 1 , " a run" ) ;
4243
let jobs = db:: flow:: jobs( & h.app.db, runs[ 0 ] .id) .await.unwrap ( ) ;
4244
assert! ( ! jobs.is_empty ( ) , " jobs: {:?}" , runs[ 0 ] ) ;
4245
let p = claim ( & h.app)
4246
.await
4247
.unwrap_or_else ( | | panic! ( " nothing to claim: {:?}" , jobs.iter ( ) .map ( | j | ( & j.name, & j.status, & j.error) ) .collect:: < Vec< _ > > ( ) ) ) ;
4248
assert_eq! ( p[ " job" ] [ " name" ] , " plan" ) ;
4249
let plan_id = p[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4250
let plan = b " the reviewed plan" .to_vec ( ) ;
4251
let ( st, text) = upload ( & h.app, plan_id, " tfplan" , & plan) .await;
4252
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
4253
let blob = oxy_core:: repo:: Repo:: blob_ref( & plan) .to_hex ( ) ;
4254
let mut r = receipt ( " plan" , p[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
4255
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
4256
assert_eq! (
4257
finish ( & h.app, plan_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : r } ) ) .await,
4258
StatusCode:: NO_CONTENT
4259
) ;
4260
4261
4262
4263
let a = claim ( & h.app) .await.unwrap ( ) ;
4264
assert_eq! ( a[ " job" ] [ " name" ] , " apply" ) ;
4265
let apply_id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4266
assert_eq! (
4267
a[ " inputs" ] ,
4268
serde_json:: json! ( [ { " artifact" : " tfplan" , " from" : " plan" , " path" : " /src/tfplan" , " blob" : blob } ] ) ,
4269
" the claim names the producer's blob"
4270
) ;
4271
let req = Request:: get( format! ( " /api/v1/flow/jobs/{apply_id} /inputs/0" ) ) .header ( header:: AUTHORIZATION , " Bearer runner-secret" ) ;
4272
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( req.body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
4273
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
4274
assert_eq! ( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) , plan) ;
4275
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/jobs/{apply_id} /inputs/1" ) , Some ( " runner-secret" ) , None ) .await;
4276
assert_eq! ( st, StatusCode:: NOT_FOUND , " no second input" ) ;
4277
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/jobs/{apply_id} /inputs/0" ) , None , None ) .await;
4278
assert_eq! ( st, StatusCode:: UNAUTHORIZED , " runners only" ) ;
4279
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/jobs/{plan_id} /inputs/0" ) , Some ( " runner-secret" ) , None ) .await;
4280
assert_eq! ( st, StatusCode:: CONFLICT , " only a running job's" ) ;
4281
}
417184c7 Andy McCall 21 days ago ↶
4282
4283
# [ tokio ::test ( flavor = " multi_thread" ) ]
4284
async fn a_job_waits_for_approval_and_is_approved_rejected_or_expires ( ) {
4285
let h = hub ( ) .await;
4286
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4287
let bob = db:: accounts:: create_account( & h.app.db, " bob" , " [email protected] " , " x" , " salt" , false ) .await.unwrap ( ) ;
4288
let carol = db:: accounts:: create_account( & h.app.db, " carol" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4289
create ( & h.app, " andy" , " gated" ) .await;
4290
let row = db:: repos:: get( & h.app.db, " andy" , " gated" ) .await.unwrap ( ) .unwrap ( ) ;
4291
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4292
let c = client ( & format! ( " {} /andy/gated" , h.base) , work.path ( ) ) ;
4293
write (
4294
work.path ( ) ,
4295
" .flow/pipeline.kdl" ,
4296
& format! (
47e37402 Andy McCall 21 days ago ↶
4297
" schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" plan\" {{ \n sandbox \" alpine\" \n exec \" plan\" \n }} \n job \" apply\" {{ \n sandbox \" alpine\" \n depends-on \" plan\" \n approval {{ \n by \" maintain\" \n others-only #true\n sealed #false\n }} \n environment \" prod\" \n identity \" https://sts.example\" {{ \n mount-path \" /run/secrets/cloud\" \n }} \n exec \" apply\" \n }} \n "
417184c7 Andy McCall 21 days ago ↶
4298
) ,
4299
) ;
4300
let mut n = 0 ;
4301
let mut push = | | {
4302
n + = 1 ;
4303
write ( work.path ( ) , " n.txt" , & format! ( " {n} \n " ) ) ;
4304
c.snapshot ( " test" ) .unwrap ( ) ;
4305
c.commit ( & format! ( " Push {n} " ) , & [ ] ) .unwrap ( ) ;
4306
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4307
} ;
4308
4309
let plan_passes = | number : i64 | {
4310
let app = h.app.clone ( ) ;
4311
let row = row.clone ( ) ;
4312
async move {
4313
let mut run = None ;
4314
for _ in 0 .. 100 {
4315
if let Some ( r) = db:: flow:: run( & app.db, row.id, number) .await.unwrap ( ) {
4316
run = Some ( r) ;
4317
break ;
4318
}
4319
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4320
}
4321
let run = run.expect ( " the run" ) ;
4322
sqlx:: query( " UPDATE flow_runs SET actor = 'andy' WHERE id = ?" ) .bind ( run.id) .execute ( & app.db) .await.unwrap ( ) ;
4323
let j = claim ( & app) .await.expect ( " plan is claimed" ) ;
4324
assert_eq! ( j[ " job" ] [ " name" ] , " plan" ) ;
4325
let ( id, ph) = ( j[ " job_id" ] .as_i64 ( ) .unwrap ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4326
finish ( & app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( " plan" , & ph, 0 ) } ) ) .await;
4327
db:: flow:: run( & app.db, row.id, number) .await.unwrap ( ) .unwrap ( )
4328
}
4329
} ;
4330
4331
4332
push ( ) ;
4333
let run = runs_of ( & h.app, " andy" , " gated" ) .await[ 0 ] .clone ( ) ;
7e01a766 Andy McCall 21 days ago ↶
4334
4335
let heard = | topic : String| match h.app.events.live.since ( 0 , & [ topic] ) {
4336
oxygenhub:: events:: Replay:: Events( e) => e.into_iter ( ) .map ( | e | e.kind ) .collect:: < Vec< _ > > ( ) ,
4337
oxygenhub:: events:: Replay:: Reload => vec! [ " reload" .into ( ) ] ,
4338
} ;
4339
assert! ( heard ( oxygenhub:: events:: topic:: repo( row.id) ) .contains ( & " push" .to_string ( ) ) , " the push is on the repository's topic" ) ;
417184c7 Andy McCall 21 days ago ↶
4340
sqlx:: query( " UPDATE flow_runs SET actor = 'andy' WHERE id = ?" ) .bind ( run.id) .execute ( & h.app.db) .await.unwrap ( ) ;
4341
let early = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & carol, true ) .await.unwrap_err ( ) ;
4342
assert! ( early.contains ( " waits for plan first" ) , " {early}" ) ;
4343
let run = plan_passes ( run.number) .await;
4344
assert! ( claim ( & h.app) .await.is_none ( ) , " apply waits for a person, so no runner gets it" ) ;
7e01a766 Andy McCall 21 days ago ↶
4345
assert! ( heard ( oxygenhub:: events:: topic:: run( run.id) ) .contains ( & " jobs" .to_string ( ) ) , " the run's topic hears its jobs" ) ;
dc59d1b5 Andy McCall 21 days ago ↶
4346
4347
4348
let told = | id : i64 | {
4349
let db = h.app.db.clone ( ) ;
4350
async move { db:: notify:: inbox( & db, id, 20 ) .await.unwrap ( ) .into_iter ( ) .filter ( | n | n.kind = = " flow.approval" ) .count ( ) }
4351
} ;
4352
assert_eq! ( ( told ( carol.id) .await, told ( andy.id) .await, told ( bob.id) .await) , ( 1 , 0 , 0 ) ) ;
417184c7 Andy McCall 21 days ago ↶
4353
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/gated/flow/{} " , run.number) , None , None ) .await;
4354
assert! ( page.contains ( " waits for approval by someone with the <em>maintain</em> role, other than andy" ) , " the run page says so" ) ;
4355
let own = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & andy, true ) .await.unwrap_err ( ) ;
4356
assert! ( own.contains ( " someone other than andy" ) , " {own}" ) ;
4357
let no_role = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & bob, true ) .await.unwrap_err ( ) ;
4358
assert! ( no_role.contains ( " takes the maintain role" ) , " {no_role}" ) ;
4359
assert_eq! ( oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & carol, true ) .await.unwrap ( ) , " apply: approved by carol" ) ;
dc59d1b5 Andy McCall 21 days ago ↶
4360
assert_eq! ( told ( carol.id) .await, 1 , " told once" ) ;
417184c7 Andy McCall 21 days ago ↶
4361
let j = claim ( & h.app) .await.expect ( " approved, it runs" ) ;
4362
assert_eq! ( j[ " job" ] [ " name" ] , " apply" ) ;
b644e175 Andy McCall 21 days ago ↶
4363
4364
let token =
4365
j[ " secrets" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | s | s[ " path" ] = = " /run/secrets/cloud" ) .unwrap ( ) [ " value" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
4366
let claims = h.app.oidc ( ) .await.unwrap ( ) .verify ( & token, " https://sts.example" , db:: now_ms( ) ) .expect ( " a valid token" ) ;
4367
assert_eq! ( claims[ " environment" ] , " prod" ) ;
417184c7 Andy McCall 21 days ago ↶
4368
let ( id, ph) = ( j[ " job_id" ] .as_i64 ( ) .unwrap ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4369
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( " apply" , & ph, 0 ) } ) ) .await;
4370
let audit = db:: accounts:: audit_rows( & h.app.db, 50 ) .await.unwrap ( ) ;
4371
assert! ( audit.iter ( ) .any ( | r | r.action = = " flow.approved" & & r.actor = = " carol" & & r.target.ends_with ( " #1 apply" ) ) , " audited" ) ;
4372
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/gated/flow/{} " , run.number) , None , None ) .await;
4373
assert! ( page.contains ( " approved by carol" ) , " the run says who" ) ;
b644e175 Andy McCall 21 days ago ↶
4374
let ( _ , page) = call ( & h.app, " GET" , " /andy/gated/deployments" , None , None ) .await;
4375
assert! ( page.contains ( " <strong>prod</strong>" ) & & page.contains ( " >#1</a>" ) & & page.contains ( " <td>carol</td>" ) , " the Deployments page" ) ;
4376
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/gated/deployments" , Some ( " site" ) , None ) .await;
4377
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
4378
assert_eq! ( ( list[ 0 ] [ " environment" ] .as_str ( ) , list[ 0 ] [ " approved_by" ] .as_str ( ) ) , ( Some ( " prod" ) , Some ( " carol" ) ) ) ;
417184c7 Andy McCall 21 days ago ↶
4379
4380
4381
push ( ) ;
4382
let run = plan_passes ( 2 ) .await;
4383
assert_eq! ( oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & carol, false ) .await.unwrap ( ) , " apply: rejected by carol" ) ;
4384
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
4385
let apply = jobs.iter ( ) .find ( | j | j.name = = " apply" ) .unwrap ( ) ;
4386
assert_eq! ( ( apply.status.as_str ( ) , apply.error.as_str ( ) ) , ( " cancelled" , " rejected by carol" ) ) ;
4387
assert! ( claim ( & h.app) .await.is_none ( ) ) ;
4388
4389
4390
push ( ) ;
4391
let run = plan_passes ( 3 ) .await;
4392
assert_eq! ( oxygenhub:: flow:: expire_approvals( & h.app, db:: now_ms( ) + 3_600_000 ) .await.unwrap ( ) , 0 , " not yet" ) ;
4393
assert_eq! ( oxygenhub:: flow:: expire_approvals( & h.app, db:: now_ms( ) + 25 * 3_600_000 ) .await.unwrap ( ) , 1 ) ;
4394
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
4395
let apply = jobs.iter ( ) .find ( | j | j.name = = " apply" ) .unwrap ( ) ;
4396
assert_eq! ( ( apply.status.as_str ( ) , apply.error.as_str ( ) ) , ( " cancelled" , " no approval within 24h" ) ) ;
4397
}
67be8ac0 Andy McCall 21 days ago ↶
4398
4399
# [ tokio ::test ( flavor = " multi_thread" ) ]
4400
async fn live_streams_only_topics_the_viewer_may_hear_and_replays ( ) {
4401
let h = hub ( ) .await;
4402
create ( & h.app, " andy" , " open" ) .await;
4403
h.app.repos.create ( & RepoName:: parse( " andy/shut" ) .unwrap ( ) , None ) .unwrap ( ) ;
4404
db:: repos:: create( & h.app.db, " andy" , " shut" , " " , hub_api:: Visibility:: Private) .await.unwrap ( ) ;
4405
let open = db:: repos:: get( & h.app.db, " andy" , " open" ) .await.unwrap ( ) .unwrap ( ) ;
4406
let shut = db:: repos:: get( & h.app.db, " andy" , " shut" ) .await.unwrap ( ) .unwrap ( ) ;
4407
let live = & h.app.events.live;
4408
let first = live.publish ( & oxygenhub:: events:: topic:: repo( open.id) , " push" ) ;
4409
live.publish ( & oxygenhub:: events:: topic:: repo( shut.id) , " push" ) ;
4410
live.publish ( & oxygenhub:: events:: topic:: proposal( open.id, 3 ) , " review" ) ;
4411
4412
4413
let read = | path : String, last : Option< u64 > | {
4414
let app = h.app.clone ( ) ;
4415
async move {
4416
let mut req = Request:: get( & path) ;
4417
if let Some ( l) = last {
4418
req = req.header ( " last-event-id" , l.to_string ( ) ) ;
4419
}
4420
let res = oxygenhub:: router( app) .oneshot ( req.body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
4421
let status = res.status ( ) ;
4422
let mut body = res.into_body ( ) ;
4423
let mut text = String :: new( ) ;
4424
while let Ok ( Some ( Ok ( frame) ) ) = tokio:: time:: timeout( std:: time:: Duration:: from_millis( 300 ) , body.frame ( ) ) .await {
4425
if let Some ( d) = frame.data_ref ( ) {
4426
text.push_str ( & String :: from_utf8_lossy( d) ) ;
4427
}
4428
}
4429
( status, text)
4430
}
4431
} ;
4432
let topics = format! ( " /live?topics=repo:{} ,repo:{} /proposal:3" , open.id, open.id) ;
4433
4434
let ( st, text) = read ( topics.clone ( ) , Some ( first.id) ) .await;
4435
assert_eq! ( st, StatusCode:: OK ) ;
4436
assert! ( text.contains ( " event: change" ) & & text.contains ( & format! ( " \" topic\" :\" repo:{} /proposal:3\" " , open.id) ) , " {text}" ) ;
4437
assert! ( ! text.contains ( & format! ( " \" topic\" :\" repo:{} \" " , shut.id) ) , " never another topic: {text}" ) ;
4438
assert! ( ! text.contains ( & format! ( " id: {} \n " , first.id) ) , " not what was already seen: {text}" ) ;
4439
4440
let ( st, _ ) = read ( format! ( " /live?topics=repo:{} ,repo:{} " , open.id, shut.id) , None ) .await;
4441
assert_eq! ( st, StatusCode:: FORBIDDEN ) ;
4442
assert_eq! ( read ( " /live?topics=user:1" .into ( ) , None ) .await.0 , StatusCode:: FORBIDDEN ) ;
4443
assert_eq! ( read ( " /live?topics=nonsense" .into ( ) , None ) .await.0 , StatusCode:: BAD_REQUEST ) ;
4444
assert_eq! ( read ( " /live?topics=" .into ( ) , None ) .await.0 , StatusCode:: BAD_REQUEST ) ;
4445
4446
for _ in 0 .. oxygenhub:: events:: REPLAY {
4447
live.publish ( " repo:999999" , " push" ) ;
4448
}
4449
let ( _ , text) = read ( topics, Some ( first.id) ) .await;
4450
assert! ( text.contains ( " event: reload" ) , " {text}" ) ;
4451
}
47e37402 Andy McCall 21 days ago ↶
4452
4453
# [ tokio ::test ( flavor = " multi_thread" ) ]
4454
async fn a_sealed_approval_releases_exactly_the_reviewed_plan ( ) {
4455
use oxy_core:: approval:: { SignedApproval, Statement} ;
4456
use oxy_core:: hash:: ObjectRef;
4457
use oxy_core:: identity:: SecretKey;
4458
let h = hub ( ) .await;
4459
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4460
let carol = db:: accounts:: create_account( & h.app.db, " carol" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4461
let sk = SecretKey:: generate( ) ;
4462
let carol_id = sk.identity ( " Carol" , " [email protected] " , 1 ) ;
4463
db:: accounts:: link_identity( & h.app.db, carol.id, & carol_id.hash ( ) .to_hex ( ) , " Carol" , " [email protected] " , & carol_id.encode ( ) )
4464
.await
4465
.unwrap ( ) ;
4466
create ( & h.app, " andy" , " infra" ) .await;
4467
let row = db:: repos:: get( & h.app.db, " andy" , " infra" ) .await.unwrap ( ) .unwrap ( ) ;
4468
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4469
let c = client ( & format! ( " {} /andy/infra" , h.base) , work.path ( ) ) ;
4470
write (
4471
work.path ( ) ,
4472
" .flow/pipeline.kdl" ,
4473
& format! (
4474
" schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" plan\" {{ \n sandbox \" alpine\" \n exec \" plan\" \n output {{ \n artifact \" tfplan\" {{ \n path \" /src/out/tfplan\" \n }} \n }} \n }} \n job \" apply\" {{ \n sandbox \" alpine\" \n depends-on \" plan\" \n environment \" prod\" \n approval\n input {{ \n artifact \" tfplan\" from \" plan\" \n path \" /src/tfplan\" \n }} \n identity \" https://sts.example\" {{ \n mount-path \" /run/secrets/cloud\" \n }} \n exec \" apply\" \n }} \n "
4475
) ,
4476
) ;
4477
c.snapshot ( " test" ) .unwrap ( ) ;
4478
c.commit ( " Plan, then apply" , & [ ] ) .unwrap ( ) ;
4479
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4480
let run = runs_of ( & h.app, " andy" , " infra" ) .await[ 0 ] .clone ( ) ;
4481
let p = claim ( & h.app) .await.unwrap ( ) ;
4482
let plan_id = p[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4483
let plan = b " the reviewed plan" .to_vec ( ) ;
4484
upload ( & h.app, plan_id, " tfplan" , & plan) .await;
4485
let mut r = receipt ( " plan" , p[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
4486
r[ " outputs" ] = serde_json:: json! ( [ oxy_core:: repo:: Repo:: blob_ref( & plan) .to_hex ( ) ] ) ;
4487
finish ( & h.app, plan_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : r } ) ) .await;
4488
let run = db:: flow:: run( & h.app.db, row.id, run.number) .await.unwrap ( ) .unwrap ( ) ;
4489
4490
4491
let plain = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & carol, true ) .await.unwrap_err ( ) ;
4492
assert! ( plain.contains ( & format! ( " oxy seal --flow {} /andy/infra/flow/{} --job apply" , h.base, run.number) ) , " {plain}" ) ;
4493
4494
let ( st, body) =
4495
call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/infra/flow/runs/{} /jobs/apply/approval" , run.number) , Some ( " site" ) , None ) .await;
4496
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4497
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
4498
assert_eq! ( v[ " environment" ] , " prod" ) ;
4499
assert_eq! ( v[ " inputs" ] , serde_json:: json! ( [ oxy_core:: repo:: Repo:: blob_ref( & plan) .to_hex ( ) ] ) , " the reviewed plan" ) ;
4500
let mut repo = [ 0 u8 ; 16 ] ;
4501
repo.copy_from_slice ( & hex:: decode( v[ " repo" ] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ) ;
4502
let oref = | k : & str | ObjectRef:: from_hex( v[ k] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ;
4503
let statement = Statement {
4504
repo,
4505
run: v[ " run" ] .as_str ( ) .unwrap ( ) .into ( ) ,
4506
job: " apply" .into ( ) ,
4507
job_hash: oref ( " job_hash" ) ,
4508
state: oref ( " state" ) ,
4509
inputs: v[ " inputs" ] .as_array ( ) .unwrap ( ) .iter ( ) .map ( | i | ObjectRef:: from_hex( i.as_str ( ) .unwrap ( ) ) .unwrap ( ) ) .collect ( ) ,
4510
environment: " prod" .into ( ) ,
4511
approve: true ,
4512
} ;
4513
let seal = | s : & Statement, key : & SecretKey, id : & oxy_core:: identity:: Identity| {
4514
let body = serde_json:: json! ( { " signed" : hex:: encode( SignedApproval:: sign( s, id, key) .encode ( ) ) } ) .to_string ( ) ;
4515
let app = h.app.clone ( ) ;
4516
let path = format! ( " /api/v1/repos/andy/infra/flow/runs/{} /jobs/apply/seal" , run.number) ;
4517
async move { call ( & app, " POST" , & path, Some ( " site" ) , Some ( body) ) .await }
4518
} ;
4519
4520
let other_plan = Statement { inputs: vec! [ ObjectRef( [ 9 ; 32 ] ) ] , .. statement.clone ( ) } ;
4521
let ( st, body) = seal ( & other_plan, & sk, & carol_id) .await;
4522
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " approves something else" ) , " {st} {body}" ) ;
4523
let agent = SecretKey:: generate( ) ;
4524
let agent_id = agent.identity ( " Claude Code" , " [email protected] " , 1 ) ;
4525
let ( st, body) = seal ( & statement, & agent, & agent_id) .await;
4526
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " not linked to an account" ) , " {st} {body}" ) ;
4527
assert! ( claim ( & h.app) .await.is_none ( ) , " still waiting" ) ;
4528
4529
4530
let ( st, body) = seal ( & statement, & sk, & carol_id) .await;
4531
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4532
assert! ( body.contains ( " approved by carol, sealed" ) , " {body}" ) ;
4533
let name = oxygenhub:: repos:: RepoName:: parse( " andy/infra" ) .unwrap ( ) ;
4534
let state = statement.state;
4535
let kept = h.app.repos.read ( & name, move | r, _ | Ok ( r.store ( ) .approvals_for ( & state) ? ) ) .await.unwrap ( ) ;
4536
assert_eq! ( kept.len ( ) , 1 , " the approval is in the repository" ) ;
4537
let j = claim ( & h.app) .await.expect ( " released" ) ;
4538
assert_eq! ( j[ " job" ] [ " name" ] , " apply" ) ;
4539
let token =
4540
j[ " secrets" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | s | s[ " path" ] = = " /run/secrets/cloud" ) .unwrap ( ) [ " value" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
4541
let claims = h.app.oidc ( ) .await.unwrap ( ) .verify ( & token, " https://sts.example" , db:: now_ms( ) ) .unwrap ( ) ;
4542
assert_eq! ( ( claims[ " environment" ] .as_str ( ) , claims[ " approved_by" ] .as_str ( ) ) , ( Some ( " prod" ) , Some ( " carol" ) ) ) ;
4543
assert_eq! ( claims[ " approval" ] .as_str ( ) , Some ( kept[ 0 ] .to_hex ( ) .as_str ( ) ) ) ;
4544
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/infra/flow/{} " , run.number) , None , None ) .await;
4545
assert! ( page.contains ( " sealed with their key" ) , " the run page says so" ) ;
4546
}