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 ( ) ;
c8684d44 Andy McCall 20 days ago
44
45
config.registration.setup_code = " test-setup" .into ( ) ;
42fa9554 Andy McCall 1 month ago ↶
46
config.auth.push_tokens = vec! [ " site" .into ( ) ] ;
4d2e44d7 Andy McCall 1 month ago ↶
47
config.flow.runner_tokens = vec! [ " runner-secret" .into ( ) , " runner-two" .into ( ) , " runner-three" .into ( ) ] ;
42fa9554 Andy McCall 1 month ago ↶
48
config.flow.repos = vec! [ " andy/*" .into ( ) ] ;
10708ab8 Andy McCall 1 month ago ↶
49
config.registration.mode = " open" .into ( ) ;
42fa9554 Andy McCall 1 month ago ↶
50
let listener = tokio:: net:: TcpListener:: bind( " 127.0.0.1:0" ) .await.unwrap ( ) ;
51
let port = listener.local_addr ( ) .unwrap ( ) .port ( ) ;
52
config.server.base_url = format! ( " http://127.0.0.1:{port} " ) ;
53
let app = oxygenhub:: build( config) .await.unwrap ( ) ;
54
let router = oxygenhub:: router( app.clone ( ) ) ;
55
tokio:: spawn( async move { axum:: serve( listener, router) .await.unwrap ( ) } ) ;
56
Hub { app, base: format! ( " http://127.0.0.1:{port} " ) , _dir: dir }
57
}
58
59
async fn call ( app : & Shared, method : & str , path : & str , bearer : Option< & str > , json : Option< String> ) -> ( StatusCode, String) {
60
let mut req = Request:: builder( ) .method ( method) .uri ( path) ;
61
if let Some ( b) = bearer {
62
req = req.header ( header:: AUTHORIZATION , format! ( " Bearer {b} " ) ) ;
63
}
64
let body = match json {
65
Some ( j) => {
66
req = req.header ( header:: CONTENT_TYPE , " application/json" ) ;
67
Body:: from( j)
68
}
69
None => Body:: empty( ) ,
70
} ;
71
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req.body ( body) .unwrap ( ) ) .await.unwrap ( ) ;
72
let status = res.status ( ) ;
73
let bytes = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
74
( status, String :: from_utf8_lossy( & bytes) .into_owned ( ) )
75
}
76
736f6813 Andy McCall 1 month ago ↶
77
78
async fn head ( app : & Shared, path : & str ) -> ( StatusCode, String, String) {
79
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( Request:: get( path) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
80
let h = | k : header:: HeaderName| res.headers ( ) .get ( k) .and_then ( | v | v.to_str ( ) .ok ( ) ) .unwrap_or ( " " ) .to_string ( ) ;
81
( res.status ( ) , h ( header:: LOCATION ) , h ( header:: CONTENT_TYPE ) )
82
}
83
42fa9554 Andy McCall 1 month ago ↶
84
async fn claim ( app : & Shared) -> Option< serde_json:: Value> {
85
let ( st, body) =
86
call ( app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( r # "{"runner":"test-1","runtimes":["microvm"]}"# .into ( ) ) ) .await;
87
match st {
88
StatusCode:: NO_CONTENT => None ,
89
StatusCode:: OK => Some ( serde_json:: from_str( & body) .unwrap ( ) ) ,
90
other => panic! ( " claim: {other} {body}" ) ,
91
}
92
}
93
94
fn receipt ( job : & str , pipeline_hash : & str , exit : u32 ) -> serde_json:: Value {
95
serde_json:: json! ( {
96
" job_name" : job, " pipeline_hash" : pipeline_hash, " scope_tree_hash" : " 11" .repeat ( 32 ) ,
97
" inputs" : [ ] , " environment" : " 22" .repeat ( 32 ) , " outputs" : [ ] , " exit_code" : exit, " duration_ms" : 640
98
} )
99
}
100
101
async fn finish ( app : & Shared, id : i64 , body : serde_json:: Value) -> StatusCode {
102
call ( app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /finish" ) , Some ( " runner-secret" ) , Some ( body.to_string ( ) ) ) .await.0
103
}
104
105
async fn create ( app : & Shared, owner : & str , name : & str ) {
106
app.repos.create ( & RepoName:: parse( & format! ( " {owner} /{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
107
db:: repos:: create( & app.db, owner, name, " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
108
}
109
110
fn client ( url : & str , work : & Path) -> Repo {
111
let mut c = Repo:: init( work) .unwrap ( ) ;
112
c.config.set ( " remote.origin.url" , url) .unwrap ( ) ;
113
c.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
114
c.save_config ( ) .unwrap ( ) ;
115
c
116
}
117
118
fn pipeline ( extra : & str ) -> String {
119
format! (
120
" 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} "
121
)
122
}
123
124
async fn runs_of ( app : & Shared, owner : & str , name : & str ) -> Vec< db:: flow:: Run> {
125
let row = db:: repos:: get( & app.db, owner, name) .await.unwrap ( ) .unwrap ( ) ;
126
for _ in 0 .. 100 {
127
let r = db:: flow:: runs( & app.db, row.id, 10 , None ) .await.unwrap ( ) ;
128
if ! r.is_empty ( ) {
129
return r;
130
}
131
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
132
}
133
vec! [ ]
134
}
135
90326c96 Andy McCall 1 month ago ↶
136
137
async fn checks_when ( app : & Shared, repo_id : i64 , state : & str , n : usize ) -> Vec< db:: hooks:: Check> {
138
for _ in 0 .. 100 {
139
let c = db:: hooks:: checks_for_state( & app.db, repo_id, state) .await.unwrap ( ) ;
140
if c.len ( ) > = n {
141
return c;
142
}
143
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
144
}
145
db:: hooks:: checks_for_state( & app.db, repo_id, state) .await.unwrap ( )
146
}
147
42fa9554 Andy McCall 1 month ago ↶
148
# [ tokio ::test ( flavor = " multi_thread" ) ]
149
async fn a_push_runs_the_pipeline_through_a_runner ( ) {
150
let h = hub ( ) .await;
151
create ( & h.app, " andy" , " demo" ) .await;
152
create ( & h.app, " andy" , " secret" ) .await;
153
create ( & h.app, " other" , " plain" ) .await;
154
let url = format! ( " {} /andy/demo" , h.base) ;
155
let work = tempfile:: tempdir( ) .unwrap ( ) ;
156
let mut c = client ( & url, work.path ( ) ) ;
157
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
158
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
159
c.snapshot ( " test" ) .unwrap ( ) ;
160
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
161
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
162
163
164
let runs = runs_of ( & h.app, " andy" , " demo" ) .await;
165
assert_eq! ( runs.len ( ) , 1 ) ;
166
let run = & runs[ 0 ] ;
167
assert_eq! ( ( run.bookmark.as_str ( ) , run.status.as_str ( ) , run.actor.as_str ( ) ) , ( " main" , " queued" , " site token" ) ) ;
168
assert_eq! ( run.pipeline_hash.len ( ) , 64 ) ;
169
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
170
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 ↶
171
172
let checks = checks_when ( & h.app, run.repo_id, & run.state, 3 ) .await;
42fa9554 Andy McCall 1 month ago ↶
173
assert_eq! (
174
checks.iter ( ) .map ( | c | ( c.name.as_str ( ) , c.status.as_str ( ) ) ) .collect:: < Vec< _ > > ( ) ,
175
[ ( " flow/build" , " queued" ) , ( " flow/lint" , " queued" ) , ( " flow/test" , " queued" ) ]
176
) ;
177
178
179
assert_eq! (
180
call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " site" ) , Some ( r # "{"runner":"x","runtimes":["microvm"]}"# .into ( ) ) ) .await.0 ,
181
StatusCode:: UNAUTHORIZED
182
) ;
183
assert_eq! (
184
call ( & h.app, " POST" , " /api/v1/flow/claim" , None , Some ( r # "{"runner":"x","runtimes":["microvm"]}"# .into ( ) ) ) .await.0 ,
185
StatusCode:: UNAUTHORIZED
186
) ;
187
188
let ( st, _ ) =
189
call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( r # "{"runner":"x","runtimes":["wasm"]}"# .into ( ) ) ) .await;
190
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
191
192
193
let a = claim ( & h.app) .await.unwrap ( ) ;
194
let b = claim ( & h.app) .await.unwrap ( ) ;
195
assert_eq! ( ( a[ " job" ] [ " name" ] .as_str ( ) , b[ " job" ] [ " name" ] .as_str ( ) ) , ( Some ( " build" ) , Some ( " lint" ) ) ) ;
196
assert! ( claim ( & h.app) .await.is_none ( ) , " test waits for build" ) ;
197
assert_eq! ( a[ " repo" ] , " andy/demo" ) ;
198
assert_eq! ( a[ " clone_url" ] , url.as_str ( ) ) ;
199
assert_eq! ( a[ " sandbox" ] [ " rootfs" ] , ROOTFS ) ;
200
assert_eq! ( a[ " job" ] [ " exec" ] , serde_json:: json! ( [ " make" ] ) ) ;
201
assert_eq! ( a[ " timeout_s" ] , 3600 ) ;
9a065bc0 Andy McCall 1 month ago ↶
202
203
assert_eq! (
204
( a[ " state" ] .as_str ( ) , a[ " bookmark" ] .as_str ( ) , a[ " run_number" ] .as_i64 ( ) ) ,
205
( Some ( run.state.as_str ( ) ) , Some ( " main" ) , Some ( run.number) )
206
) ;
42fa9554 Andy McCall 1 month ago ↶
207
let heads: Vec< String> = a[ " heads" ] .as_array ( ) .unwrap ( ) .iter ( ) .map ( | v | v.as_str ( ) .unwrap ( ) .to_string ( ) ) .collect ( ) ;
208
assert_eq! ( heads.join ( " +" ) , run.heads) ;
209
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
210
assert_eq! ( checks.iter ( ) .find ( | c | c.name = = " flow/build" ) .unwrap ( ) .status, " running" ) ;
211
212
213
let token = a[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
214
assert! ( token.starts_with ( " flowjob_" ) ) ;
e8fa5369 Andy McCall 23 days ago ↶
215
let opts = RemoteOptions { token: Some ( token.clone ( ) ) , .. Default :: default( ) } ;
42fa9554 Andy McCall 1 month ago ↶
216
open_remote ( & url, & opts) .unwrap ( ) .refs ( ) .unwrap ( ) ;
217
assert! ( open_remote ( & format! ( " {} /andy/secret" , h.base) , & opts) .unwrap ( ) .refs ( ) .is_err ( ) , " another repository" ) ;
e8fa5369 Andy McCall 23 days ago ↶
218
let bad = RemoteOptions { token: Some ( " flowjob_00" .into ( ) ) , .. Default :: default( ) } ;
42fa9554 Andy McCall 1 month ago ↶
219
assert! ( open_remote ( & url, & bad) .unwrap ( ) .refs ( ) .is_err ( ) , " an unknown job token" ) ;
220
221
c.config.set ( " remote.origin.token" , & token) .unwrap ( ) ;
222
c.save_config ( ) .unwrap ( ) ;
223
write ( work.path ( ) , " more.txt" , " x\n " ) ;
224
c.snapshot ( " test" ) .unwrap ( ) ;
225
c.commit ( " More" , & [ ] ) .unwrap ( ) ;
226
let pushed = c.push ( " origin" , false ) ;
227
assert! ( pushed.is_err ( ) | | pushed.unwrap ( ) .iter ( ) .all ( | r | ! r.ok) , " a job token cannot push" ) ;
228
c.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
229
c.save_config ( ) .unwrap ( ) ;
230
231
232
let a_id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
233
let b_id = b[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
234
let log = serde_json:: json! ( { " records" : [
235
{ " t" : 5 , " stream" : " flow" , " text" : " booting" } ,
236
{ " t" : 120 , " stream" : " out" , " text" : " cc -o demo demo.c" } ,
237
{ " t" : 130 , " stream" : " weird" , " text" : " x" .repeat ( 20_000 ) } ,
238
] } ) ;
239
assert_eq! (
240
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{a_id} /log" ) , Some ( " runner-secret" ) , Some ( log.to_string ( ) ) ) .await.0 ,
241
StatusCode:: NO_CONTENT
242
) ;
243
let lines = db:: flow:: log( & h.app.db, a_id, 0 , 100 ) .await.unwrap ( ) ;
244
assert_eq! ( lines.len ( ) , 3 ) ;
245
assert_eq! ( ( lines[ 2 ] .stream.as_str ( ) , lines[ 2 ] .text.len ( ) ) , ( " console" , 16 * 1024 ) , " streams are known ones, text is capped" ) ;
246
let ph = a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) ;
247
assert_eq! (
248
finish ( & h.app, a_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : receipt ( " lint" , ph, 0 ) } ) ) .await,
249
StatusCode:: BAD_REQUEST ,
250
" a receipt for another job"
251
) ;
252
assert_eq! (
253
finish ( & h.app, a_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : receipt ( " build" , ph, 0 ) } ) ) .await,
254
StatusCode:: NO_CONTENT
255
) ;
256
assert_eq! ( finish ( & h.app, a_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 } ) ) .await, StatusCode:: CONFLICT , " only once" ) ;
257
assert! ( open_remote ( & url, & opts) .unwrap ( ) .refs ( ) .is_err ( ) , " the token ends with its job" ) ;
258
assert_eq! (
259
finish ( & h.app, b_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 300 , " receipt" : receipt ( " lint" , ph, 0 ) } ) ) .await,
260
StatusCode:: NO_CONTENT
261
) ;
262
let t = claim ( & h.app) .await.unwrap ( ) ;
263
assert_eq! ( t[ " job" ] [ " name" ] , " test" ) ;
264
let t_id = t[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
265
assert_eq! (
266
finish ( & h.app, t_id, serde_json:: json! ( { " exit_code" : 3 , " duration_ms" : 500 , " receipt" : receipt ( " test" , ph, 3 ) } ) ) .await,
267
StatusCode:: NO_CONTENT
268
) ;
269
270
let run = db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) ;
271
assert_eq! ( run.status, " failed" ) ;
272
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
273
let build = jobs.iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) ;
274
assert_eq! ( ( build.status.as_str ( ) , build.exit_code, build.receipt_hash.len ( ) ) , ( " passed" , Some ( 0 ) , 64 ) ) ;
275
assert_eq! ( jobs.iter ( ) .find ( | j | j.name = = " test" ) .unwrap ( ) .status, " failed" ) ;
276
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
277
assert_eq! (
278
checks.iter ( ) .map ( | c | ( c.name.as_str ( ) , c.status.as_str ( ) ) ) .collect:: < Vec< _ > > ( ) ,
279
[ ( " flow/build" , " passed" ) , ( " flow/lint" , " passed" ) , ( " flow/test" , " failed" ) ]
280
) ;
a0d4bbfb Andy McCall 1 month ago ↶
281
assert! ( checks[ 2 ] .summary.contains ( " exit 3" ) & & checks[ 2 ] .url.ends_with ( & format! ( " /andy/demo/flow/{} " , run.number) ) ) ;
42fa9554 Andy McCall 1 month ago ↶
282
283
284
let ( st, list) = call ( & h.app, " GET" , " /andy/demo/flow" , None , None ) .await;
285
assert_eq! ( st, StatusCode:: OK ) ;
a0d4bbfb Andy McCall 1 month ago ↶
286
assert! ( list.contains ( & format! ( " /andy/demo/flow/{} " , run.number) ) & & list.contains ( " st-failed" ) , " {list}" ) ;
287
let ( st, page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
42fa9554 Andy McCall 1 month ago ↶
288
assert_eq! ( st, StatusCode:: OK ) ;
289
assert! ( page.contains ( " <svg class=\" flow-graph\" " ) , " graph" ) ;
290
assert_eq! ( page.matches ( " data-job-node=" ) .count ( ) , 3 ) ;
291
assert_eq! ( page.matches ( " <path class=\" edge" ) .count ( ) , 1 , " one dependency edge" ) ;
292
assert! ( page.contains ( " cc -o demo demo.c" ) , " log shown" ) ;
293
assert! ( ! page.contains ( " data-flow-live" ) , " a finished run does not poll" ) ;
a0d4bbfb Andy McCall 1 month ago ↶
294
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 ↶
295
assert_eq! ( st, StatusCode:: OK ) ;
296
let live: serde_json:: Value = serde_json:: from_str( & live) .unwrap ( ) ;
297
assert_eq! ( live[ " status" ] , " failed" ) ;
298
let build_live = live[ " jobs" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | j | j[ " name" ] = = " build" ) .unwrap ( ) .clone ( ) ;
299
assert_eq! ( build_live[ " lines" ] .as_array ( ) .unwrap ( ) .len ( ) , 2 , " lines after seq 1" ) ;
300
assert_eq! ( call ( & h.app, " GET" , " /andy/demo/flow/999" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
301
302
303
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
304
write ( work.path ( ) , " test.sh" , " echo changed\n " ) ;
305
c.snapshot ( " test" ) .unwrap ( ) ;
306
c.commit ( " Change the test" , & [ ] ) .unwrap ( ) ;
307
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
308
let mut second = None ;
309
for _ in 0 .. 100 {
310
let r = runs_of ( & h.app, " andy" , " demo" ) .await;
311
if r.len ( ) = = 2 {
312
second = Some ( r[ 0 ] .clone ( ) ) ;
313
break ;
314
}
315
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
316
}
317
let second = second.expect ( " a second run" ) ;
318
let x = claim ( & h.app) .await.unwrap ( ) ;
319
let y = claim ( & h.app) .await.unwrap ( ) ;
320
let ( build_id, lint_id) = if x[ " job" ] [ " name" ] = = " build" {
321
( x[ " job_id" ] .as_i64 ( ) .unwrap ( ) , y[ " job_id" ] .as_i64 ( ) .unwrap ( ) )
322
} else {
323
( y[ " job_id" ] .as_i64 ( ) .unwrap ( ) , x[ " job_id" ] .as_i64 ( ) .unwrap ( ) )
324
} ;
325
assert_eq! (
326
finish ( & h.app, build_id, serde_json:: json! ( { " exit_code" : null, " duration_ms" : 10 , " error" : " rootfs is not on this runner" } ) )
327
.await,
328
StatusCode:: NO_CONTENT
329
) ;
330
assert_eq! ( finish ( & h.app, lint_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 } ) ) .await, StatusCode:: NO_CONTENT ) ;
331
assert! ( claim ( & h.app) .await.is_none ( ) , " test was skipped" ) ;
332
let jobs = db:: flow:: jobs( & h.app.db, second.id) .await.unwrap ( ) ;
333
assert_eq! (
334
jobs.iter ( ) .map ( | j | ( j.name.as_str ( ) , j.status.as_str ( ) ) ) .collect:: < Vec< _ > > ( ) ,
335
[ ( " build" , " error" ) , ( " lint" , " passed" ) , ( " test" , " skipped" ) ]
336
) ;
337
assert_eq! ( db:: flow:: run_by_id( & h.app.db, second.id) .await.unwrap ( ) .unwrap ( ) .status, " failed" ) ;
338
339
340
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " job \" broken\" {\n sandbox \" missing\" \n exec \" x\" \n }\n " ) ) ;
341
c.snapshot ( " test" ) .unwrap ( ) ;
342
c.commit ( " Break the pipeline" , & [ ] ) .unwrap ( ) ;
343
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
344
let mut third = None ;
345
for _ in 0 .. 100 {
346
let r = runs_of ( & h.app, " andy" , " demo" ) .await;
347
if r.len ( ) = = 3 {
348
third = Some ( r[ 0 ] .clone ( ) ) ;
349
break ;
350
}
351
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
352
}
353
let third = third.expect ( " a third run" ) ;
354
assert_eq! ( third.status, " error" ) ;
355
assert! ( third.error.contains ( " line 18" ) & & third.error.contains ( " \" missing\" " ) , " {}" , third.error) ;
a0d4bbfb Andy McCall 1 month ago ↶
356
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , third.number) , None , None ) .await;
42fa9554 Andy McCall 1 month ago ↶
357
assert! ( page.contains ( " The pipeline is not valid" ) ) ;
358
736f6813 Andy McCall 1 month ago ↶
359
360
write ( work.path ( ) , " README.md" , " # Demo\n \n [](.flow/run)\n \n \n " ) ;
361
std:: fs:: create_dir_all( work.path ( ) .join ( " img" ) ) .unwrap ( ) ;
362
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 " )
363
.unwrap ( ) ;
364
c.snapshot ( " test" ) .unwrap ( ) ;
365
c.commit ( " A README with a stamp" , & [ ] ) .unwrap ( ) ;
366
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
367
let mut fourth = None ;
368
for _ in 0 .. 100 {
369
let r = runs_of ( & h.app, " andy" , " demo" ) .await;
370
if r.len ( ) = = 4 {
371
fourth = Some ( r[ 0 ] .clone ( ) ) ;
372
break ;
373
}
374
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
375
}
376
let fourth = fourth.expect ( " a fourth run" ) ;
377
let ( _ , overview) = call ( & h.app, " GET" , " /andy/demo" , None , None ) .await;
378
assert! ( overview.contains ( " src=\" /andy/demo/raw/main/.flow/stamp\" " ) , " {overview}" ) ;
379
assert! ( overview.contains ( " href=\" /andy/demo/file/main/.flow/run\" " ) ) ;
380
assert! ( overview.contains ( " src=\" /andy/demo/raw/main/img/logo.png\" " ) , " images use the raw route" ) ;
381
let ( st, _ , ct) = head ( & h.app, " /andy/demo/raw/main/img/logo.png" ) .await;
382
assert_eq! ( ( st, ct.as_str ( ) ) , ( StatusCode:: OK , " image/png" ) ) ;
383
let ( st, loc, _ ) = head ( & h.app, " /andy/demo/raw/main/.flow/stamp" ) .await;
384
assert! ( st.is_redirection ( ) & & loc = = " /andy/demo/flow/stamp/main" , " {st} {loc}" ) ;
385
let ( st, loc, _ ) = head ( & h.app, " /andy/demo/flow/stamp/main" ) .await;
386
assert! ( st.is_redirection ( ) & & loc = = format! ( " /andy/demo/flow/stamp/{} " , fourth.heads) , " a bookmark's stamp names its state: {loc}" ) ;
387
let ( st, _ , ct) = head ( & h.app, & loc) .await;
388
assert_eq! ( ( st, ct.as_str ( ) ) , ( StatusCode:: OK , " image/svg+xml; charset=utf-8" ) ) ;
389
let ( _ , svg) = call ( & h.app, " GET" , & loc, None , None ) .await;
390
assert! ( svg.starts_with ( " <svg" ) & & svg.contains ( " error ·" ) & & svg.contains ( & fourth.heads[ .. 12 ] ) , " {svg}" ) ;
391
let ( _ , svg1) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/stamp/{} " , run.heads) , None , None ) .await;
392
assert! ( svg1.contains ( " failed ·" ) & & svg1.contains ( & format! ( " run #{} failed" , run.id) ) , " an older state keeps its own result: {svg1}" ) ;
393
assert! ( svg1.contains ( & build.receipt_hash) , " receipt hashes in the metadata" ) ;
394
assert_eq! ( svg1.matches ( " text-anchor=\" middle\" " ) .count ( ) , 3 , " one glyph per job" ) ;
395
let ( st, loc, _ ) = head ( & h.app, " /andy/demo/file/main/.flow/run" ) .await;
396
assert! ( st.is_redirection ( ) & & loc = = " /andy/demo/flow/at/main" , " {loc}" ) ;
397
let ( st, loc, _ ) = head ( & h.app, " /andy/demo/flow/at/main" ) .await;
398
assert! ( st.is_redirection ( ) & & loc = = format! ( " /andy/demo/flow/{} " , fourth.id) , " {loc}" ) ;
399
42fa9554 Andy McCall 1 month ago ↶
400
401
let work2 = tempfile:: tempdir( ) .unwrap ( ) ;
402
let c2 = client ( & format! ( " {} /other/plain" , h.base) , work2.path ( ) ) ;
403
write ( work2.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
404
c2.snapshot ( " test" ) .unwrap ( ) ;
405
c2.commit ( " Pipeline" , & [ ] ) .unwrap ( ) ;
406
assert! ( c2.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
407
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 500 ) ) .await;
408
let row = db:: repos:: get( & h.app.db, " other" , " plain" ) .await.unwrap ( ) .unwrap ( ) ;
409
assert! ( db:: flow:: runs( & h.app.db, row.id, 10 , None ) .await.unwrap ( ) .is_empty ( ) ) ;
410
let ( _ , page) = call ( & h.app, " GET" , " /other/plain/flow" , None , None ) .await;
411
assert! ( page.contains ( " not enabled for this repository" ) ) ;
736f6813 Andy McCall 1 month ago ↶
412
let ( _ , loc, _ ) = head ( & h.app, " /other/plain/flow/stamp/main" ) .await;
413
let ( _ , svg) = call ( & h.app, " GET" , & loc, None , None ) .await;
414
assert! ( svg.contains ( " no run ·" ) , " {svg}" ) ;
415
let ( _ , loc, _ ) = head ( & h.app, " /other/plain/flow/at/main" ) .await;
416
assert_eq! ( loc, " /other/plain/flow" ) ;
42fa9554 Andy McCall 1 month ago ↶
417
}
fd3db644 Andy McCall 1 month ago ↶
418
419
# [ tokio ::test ( flavor = " multi_thread" ) ]
420
async fn claims_carry_pinned_subrepos_and_the_job_token_reads_only_those ( ) {
421
let h = hub ( ) .await;
25091e5c Andy McCall 1 month ago ↶
422
for n in [ " base" , " lib" , " vault" , " other" , " app" ] {
fd3db644 Andy McCall 1 month ago ↶
423
create ( & h.app, " andy" , n) .await;
424
}
425
let push = | name : & str , file : & str | {
426
let work = tempfile:: tempdir( ) .unwrap ( ) ;
427
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
428
write ( work.path ( ) , file, " x\n " ) ;
429
c.snapshot ( " t" ) .unwrap ( ) ;
430
c.commit ( " first" , & [ ] ) .unwrap ( ) ;
431
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
432
let heads = c.bookmark_heads ( ) .unwrap ( ) ;
433
( work, heads)
434
} ;
25091e5c Andy McCall 1 month ago ↶
435
let ( _b, base_heads) = push ( " base" , " base.rs" ) ;
436
437
let lib_work = tempfile:: tempdir( ) .unwrap ( ) ;
438
let lib = client ( & format! ( " {} /andy/lib" , h.base) , lib_work.path ( ) ) ;
439
lib.submodule_add ( & format! ( " {} /andy/base" , h.base) , " deps/base" , Some ( " main" ) ) .unwrap ( ) ;
440
write ( lib_work.path ( ) , " lib.rs" , " x\n " ) ;
441
lib.snapshot ( " t" ) .unwrap ( ) ;
442
lib.commit ( " lib on base" , & [ ] ) .unwrap ( ) ;
443
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
444
let lib_heads = lib.bookmark_heads ( ) .unwrap ( ) ;
fd3db644 Andy McCall 1 month ago ↶
445
let ( _v, _ ) = push ( " vault" , " secret.rs" ) ;
446
let ( _o, _ ) = push ( " other" , " o.rs" ) ;
447
448
449
let work = tempfile:: tempdir( ) .unwrap ( ) ;
450
let app = client ( & format! ( " {} /andy/app" , h.base) , work.path ( ) ) ;
451
app.submodule_add ( & format! ( " {} /andy/lib" , h.base) , " deps/lib" , Some ( " main" ) ) .unwrap ( ) ;
a4f01d51 Andy McCall 1 month ago ↶
452
2bbe88a8 Andy McCall 1 month ago ↶
453
454
fd3db644 Andy McCall 1 month ago ↶
455
sqlx:: query( " UPDATE repositories SET visibility = 'private' WHERE name = 'vault'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
a4f01d51 Andy McCall 1 month ago ↶
456
let bare_work = tempfile:: tempdir( ) .unwrap ( ) ;
457
let mut bare = Repo:: init( bare_work.path ( ) ) .unwrap ( ) ;
458
bare.config.set ( " remote.origin.url" , & format! ( " {} /andy/bare" , h.base) ) .unwrap ( ) ;
459
bare.save_config ( ) .unwrap ( ) ;
460
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 ↶
461
app.submodule_add ( & format! ( " {} /andy/vault" , h.base) , " deps/vault" , Some ( " main" ) ) .unwrap ( ) ;
a4f01d51 Andy McCall 1 month ago ↶
462
let nested = Repo:: open( & work.path ( ) .join ( " deps/vault" ) ) .unwrap ( ) ;
2bbe88a8 Andy McCall 1 month ago ↶
463
assert! ( nested.config.remote_tokens.is_empty ( ) , " no token is copied into a subrepo" ) ;
464
nested.fetch ( " origin" ) .unwrap ( ) ;
370035a4 Andy McCall 1 month ago ↶
465
fd3db644 Andy McCall 1 month ago ↶
466
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
467
write ( work.path ( ) , " Makefile" , " all:\n " ) ;
468
app.snapshot ( " t" ) .unwrap ( ) ;
469
app.commit ( " app with deps" , & [ ] ) .unwrap ( ) ;
470
assert! ( app.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
471
assert! ( ! runs_of ( & h.app, " andy" , " app" ) .await.is_empty ( ) ) ;
472
473
let job = claim ( & h.app) .await.unwrap ( ) ;
474
let subs = job[ " subrepos" ] .as_array ( ) .cloned ( ) .unwrap_or_default ( ) ;
25091e5c Andy McCall 1 month ago ↶
475
assert_eq! ( subs.len ( ) , 2 , " lib and what lib pins; vault is private and the pusher has no account: {subs:?}" ) ;
476
assert_eq! ( subs[ 1 ] [ " path" ] , " deps/lib/deps/base" , " nested under its parent, after it" ) ;
477
assert_eq! ( subs[ 1 ] [ " heads" ] , serde_json:: json! ( base_heads.iter ( ) .map ( | h | h.to_hex ( ) ) .collect:: < Vec< _ > > ( ) ) ) ;
370035a4 Andy McCall 1 month ago ↶
478
let _ = std:: fs:: remove_dir_all( work.path ( ) .join ( " deps/vault2" ) ) ;
fd3db644 Andy McCall 1 month ago ↶
479
assert_eq! ( subs[ 0 ] [ " path" ] , " deps/lib" ) ;
480
assert_eq! ( subs[ 0 ] [ " repo" ] , " andy/lib" ) ;
481
assert_eq! ( subs[ 0 ] [ " heads" ] , serde_json:: json! ( lib_heads.iter ( ) .map ( | h | h.to_hex ( ) ) .collect:: < Vec< _ > > ( ) ) ) ;
482
assert! ( subs[ 0 ] [ " clone_url" ] .as_str ( ) .unwrap ( ) .ends_with ( " /andy/lib" ) ) ;
afd0be38 Andy McCall 1 month ago ↶
483
484
let job_id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
485
let lines = db:: flow:: log( & h.app.db, job_id, 0 , 100 ) .await.unwrap ( ) ;
486
assert! (
487
lines.iter ( ) .any ( | l | l.stream = = " flow" & & l.text.contains ( " subrepo deps/vault (andy/vault) is left out" ) ) ,
488
" {:?}" ,
489
lines.iter ( ) .map ( | l | & l.text) .collect:: < Vec< _ > > ( )
490
) ;
fd3db644 Andy McCall 1 month ago ↶
491
492
493
let token = job[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
494
let read = | name : & str | {
495
let opts = RemoteOptions { token: Some ( token.clone ( ) ) , .. Default :: default( ) } ;
496
open_remote ( & format! ( " {} /andy/{name} " , h.base) , & opts) .and_then ( | r | r.refs ( ) ) .is_ok ( )
497
} ;
25091e5c Andy McCall 1 month ago ↶
498
assert! ( read ( " app" ) & & read ( " lib" ) & & read ( " base" ) ) ;
fd3db644 Andy McCall 1 month ago ↶
499
assert! ( ! read ( " vault" ) , " not pinned for this pusher" ) ;
500
assert! ( ! read ( " other" ) , " public, but not the job's to read" ) ;
501
}
941abdd6 Andy McCall 1 month ago ↶
502
503
# [ tokio ::test ( flavor = " multi_thread" , worker_threads = 8 ) ]
504
async fn concurrent_log_appends_never_fail_on_a_locked_database ( ) {
505
let h = hub ( ) .await;
506
create ( & h.app, " andy" , " demo" ) .await;
507
let work = tempfile:: tempdir( ) .unwrap ( ) ;
508
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
509
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
510
write ( work.path ( ) , " Makefile" , " all:\n " ) ;
511
c.snapshot ( " t" ) .unwrap ( ) ;
512
c.commit ( " pipeline" , & [ ] ) .unwrap ( ) ;
513
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
514
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
515
let job = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) .remove ( 0 ) ;
516
517
518
let mut tasks = Vec :: new( ) ;
519
for w in 0 .. 16 {
520
let pool = h.app.db.clone ( ) ;
521
tasks.push ( tokio:: spawn( async move {
522
let records: Vec< flow_core:: api:: Record> =
523
( 0 .. 20 ) .map ( | i | flow_core:: api:: Record { t: i, stream: " out" .into ( ) , text: format! ( " writer {w} line {i} " ) } ) .collect ( ) ;
524
db:: flow:: append_log( & pool, job.id, & records, 100_000 ) .await
525
} ) ) ;
526
}
527
for t in tasks {
528
assert_eq! ( t.await.unwrap ( ) .unwrap ( ) , 20 , " every append succeeds" ) ;
529
}
530
let lines = db:: flow:: log( & h.app.db, job.id, 0 , 1_000 ) .await.unwrap ( ) ;
531
assert_eq! ( lines.len ( ) , 320 ) ;
532
let seqs: std:: collections:: BTreeSet< i64 > = lines.iter ( ) .map ( | l | l.seq ) .collect ( ) ;
533
assert_eq! ( seqs.len ( ) , 320 , " sequence numbers are unique" ) ;
534
}
9008bf6c Andy McCall 1 month ago ↶
535
536
537
# [ tokio ::test ( flavor = " multi_thread" ) ]
538
async fn a_repository_setting_turns_flow_on_or_off ( ) {
539
let h = hub ( ) .await;
540
create ( & h.app, " other" , " plain" ) .await;
541
create ( & h.app, " andy" , " quiet" ) .await;
a0d4bbfb Andy McCall 1 month ago ↶
542
543
create ( & h.app, " andy" , " first" ) .await;
544
{
545
let work = tempfile:: tempdir( ) .unwrap ( ) ;
546
let c = client ( & format! ( " {} /andy/first" , h.base) , work.path ( ) ) ;
547
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
548
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
549
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
550
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
551
assert_eq! ( runs_of ( & h.app, " andy" , " first" ) .await.len ( ) , 1 ) ;
552
}
9008bf6c Andy McCall 1 month ago ↶
553
let row = db:: repos:: get( & h.app.db, " other" , " plain" ) .await.unwrap ( ) .unwrap ( ) ;
554
db:: repos:: set_flow( & h.app.db, row.id, " on" ) .await.unwrap ( ) ;
555
let quiet = db:: repos:: get( & h.app.db, " andy" , " quiet" ) .await.unwrap ( ) .unwrap ( ) ;
556
db:: repos:: set_flow( & h.app.db, quiet.id, " off" ) .await.unwrap ( ) ;
557
for ( owner, name) in [ ( " other" , " plain" ) , ( " andy" , " quiet" ) ] {
558
let work = tempfile:: tempdir( ) .unwrap ( ) ;
559
let c = client ( & format! ( " {} /{owner} /{name} " , h.base) , work.path ( ) ) ;
560
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
561
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
562
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
563
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
564
}
a0d4bbfb Andy McCall 1 month ago ↶
565
let plain = runs_of ( & h.app, " other" , " plain" ) .await;
566
assert_eq! ( plain.len ( ) , 1 , " turned on" ) ;
55673c65 Andy McCall 20 days ago ↶
567
568
bf3d0754 Andy McCall 1 month ago ↶
569
let a = claim ( & h.app) .await.unwrap ( ) ;
570
assert_eq! ( a[ " repo" ] , " andy/first" ) ;
571
let ( _ , page) = call ( & h.app, " GET" , " /other/plain/flow/1" , None , None ) .await;
55673c65 Andy McCall 20 days ago ↶
572
assert! ( page.contains ( " Waiting for a runner: 1st in line" ) & & page.contains ( " Running now: andy/first #1" ) , " {page}" ) ;
bf3d0754 Andy McCall 1 month ago ↶
573
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/other/plain/flow/runs/1" , None , None ) .await;
55673c65 Andy McCall 20 days ago ↶
574
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " queue_position" ] , 1 ) ;
bf3d0754 Andy McCall 1 month ago ↶
575
let ( _ , list) = call ( & h.app, " GET" , " /other/plain/flow" , None , None ) .await;
55673c65 Andy McCall 20 days ago ↶
576
assert! ( list.contains ( " queued, 1st in line" ) , " {list}" ) ;
a0d4bbfb Andy McCall 1 month ago ↶
577
578
579
assert_eq! ( plain[ 0 ] .number, 1 ) ;
90326c96 Andy McCall 1 month ago ↶
580
let checks = checks_when ( & h.app, row.id, & plain[ 0 ] .state, 3 ) .await;
a0d4bbfb Andy McCall 1 month ago ↶
581
assert! ( checks.iter ( ) .all ( | c | c.url.ends_with ( " /other/plain/flow/1" ) ) , " {checks:?}" ) ;
582
let ( st, page) = call ( & h.app, " GET" , " /other/plain/flow/1" , None , None ) .await;
583
assert! ( st = = StatusCode:: OK & & page.contains ( " Run #1" ) , " {st}" ) ;
9008bf6c Andy McCall 1 month ago ↶
584
assert! ( runs_of ( & h.app, " andy" , " quiet" ) .await.is_empty ( ) , " turned off despite andy/*" ) ;
585
}
1edac4b6 Andy McCall 1 month ago ↶
586
587
588
589
# [ tokio ::test ( flavor = " multi_thread" ) ]
590
async fn runs_start_without_a_push_and_finished_runs_re_run ( ) {
591
let h = hub ( ) .await;
592
593
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
594
create ( & h.app, " andy" , " demo" ) .await;
595
let url = format! ( " {} /andy/demo" , h.base) ;
596
let work = tempfile:: tempdir( ) .unwrap ( ) ;
597
let c = client ( & url, work.path ( ) ) ;
598
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
599
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
600
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
601
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
602
let first = runs_of ( & h.app, " andy" , " demo" ) .await;
603
assert_eq! ( ( first.len ( ) , first[ 0 ] .trigger.as_str ( ) ) , ( 1 , " push" ) ) ;
604
605
606
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
607
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
608
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
609
assert_eq! ( v[ " number" ] , 2 ) ;
610
assert! ( v[ " url" ] .as_str ( ) .unwrap ( ) .ends_with ( " /andy/demo/flow/2" ) ) ;
611
let second = db:: flow:: run( & h.app.db, first[ 0 ] .repo_id, 2 ) .await.unwrap ( ) .unwrap ( ) ;
612
assert_eq! ( ( second.trigger.as_str ( ) , second.actor.as_str ( ) , second.state.as_str ( ) ) , ( " manual" , " andy" , first[ 0 ] .state.as_str ( ) ) ) ;
613
assert_eq! ( db:: flow:: jobs( & h.app.db, second.id) .await.unwrap ( ) .len ( ) , 3 ) ;
614
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/2" , None , None ) .await;
615
assert! ( page.contains ( " started by andy" ) , " the run page says what started it" ) ;
616
4f692533 Andy McCall 1 month ago ↶
617
618
let head = first[ 0 ] .head_list ( ) [ 0 ] .to_string ( ) ;
619
let ( st, body) =
620
call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( format! ( r # "{{ "change":"{head} "}} "# ) ) ) .await;
621
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
622
let on_change = db:: flow:: run( & h.app.db, first[ 0 ] .repo_id, 3 ) .await.unwrap ( ) .unwrap ( ) ;
623
assert_eq! (
624
( on_change.bookmark.as_str ( ) , on_change.heads.as_str ( ) , on_change.state.as_str ( ) ) ,
625
( " " , head.as_str ( ) , first[ 0 ] .state.as_str ( ) )
626
) ;
627
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/3" , None , None ) .await;
628
assert! ( page.contains ( " title=\" Run on one change, not a bookmark\" >change</span>" ) , " run page" ) ;
629
let ( st, body) =
630
call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( format! ( r # "{{ "change":"{} "}} "# , " ab" .repeat ( 32 ) ) ) )
631
.await;
632
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " no change" ) , " {st} {body}" ) ;
633
634
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow" , None , None ) .await;
635
assert! ( page.contains ( " Sign in with write access to run the pipeline." ) & & ! page.contains ( " primary-btn" ) ) ;
636
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/bookmarks" , None , None ) .await;
637
assert! ( ! page.contains ( " /flow/start" ) , " no run buttons for anonymous viewers" ) ;
638
1edac4b6 Andy McCall 1 month ago ↶
639
640
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs/1/rerun" , Some ( " site" ) , None ) .await;
641
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " still queued" ) , " {st} {body}" ) ;
642
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( r # "{"bookmark":"nope"}"# .into ( ) ) ) .await;
643
assert_eq! ( st, StatusCode:: CONFLICT ) ;
644
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs/99/rerun" , Some ( " site" ) , None ) .await;
645
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
646
647
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , None , Some ( " {}" .into ( ) ) ) .await;
648
assert_ne! ( st, StatusCode:: CREATED ) ;
649
650
651
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
652
for r in [ & first[ 0 ] , & second] {
653
for j in db:: flow:: jobs( & h.app.db, r.id) .await.unwrap ( ) {
3b1bc27f Andy McCall 20 days ago
654
sqlx:: query( " UPDATE flow_jobs SET status = 'passed' WHERE id = $1" ) .bind ( j.id) .execute ( & h.app.db) .await.unwrap ( ) ;
1edac4b6 Andy McCall 1 month ago ↶
655
}
3b1bc27f Andy McCall 20 days ago
656
sqlx:: query( " UPDATE flow_runs SET status = 'passed', finished_ms = 1 WHERE id = $1" ) .bind ( r.id) .execute ( & h.app.db) .await.unwrap ( ) ;
1edac4b6 Andy McCall 1 month ago ↶
657
}
658
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs/1/rerun" , Some ( " site" ) , None ) .await;
659
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
4f692533 Andy McCall 1 month ago ↶
660
let third = db:: flow:: run( & h.app.db, row.id, 4 ) .await.unwrap ( ) .unwrap ( ) ;
348f8c4b Andy McCall 1 month ago ↶
661
assert_eq! (
662
( third.trigger_label ( ) .as_str ( ) , third.status.as_str ( ) , third.state.as_str ( ) ) ,
663
( " rerun:1" , " queued" , first[ 0 ] .state.as_str ( ) )
664
) ;
c889911b Andy McCall 1 month ago ↶
665
assert_eq! ( third.trigger, " rerun:1:push" , " a rerun keeps the event it repeats" ) ;
1edac4b6 Andy McCall 1 month ago ↶
666
let checks = db:: hooks:: checks_for_state( & h.app.db, row.id, & third.state) .await.unwrap ( ) ;
667
assert! (
4f692533 Andy McCall 1 month ago ↶
668
checks.iter ( ) .all ( | c | c.status = = " queued" & & c.url.ends_with ( " /flow/4" ) ) ,
1edac4b6 Andy McCall 1 month ago ↶
669
" the re-run's checks replace the old ones: {checks:?}"
670
) ;
4f692533 Andy McCall 1 month ago ↶
671
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/4" , None , None ) .await;
1edac4b6 Andy McCall 1 month ago ↶
672
assert! ( page.contains ( " re-run of #1 by andy" ) ) ;
673
assert! ( ! page.contains ( " RE-RUN" ) , " anonymous viewers get no button" ) ;
674
0650d94e Andy McCall 25 days ago ↶
675
676
677
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( r # "{"jobs":["test"]}"# .into ( ) ) ) .await;
678
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
679
let n = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " number" ] .as_i64 ( ) .unwrap ( ) ;
680
let only = db:: flow:: run( & h.app.db, first[ 0 ] .repo_id, n) .await.unwrap ( ) .unwrap ( ) ;
681
let mut names: Vec< String> = db:: flow:: jobs( & h.app.db, only.id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
682
names.sort ( ) ;
683
assert_eq! ( names, [ " build" , " test" ] , " test and the job it depends on, not lint" ) ;
684
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( r # "{"jobs":["deploy"]}"# .into ( ) ) ) .await;
685
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " not in the pipeline" ) & & body.contains ( " build, lint, test" ) , " {st} {body}" ) ;
686
1edac4b6 Andy McCall 1 month ago ↶
687
688
db:: repos:: set_flow( & h.app.db, row.id, " off" ) .await.unwrap ( ) ;
689
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
690
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " not enabled" ) , " {body}" ) ;
4f692533 Andy McCall 1 month ago ↶
691
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow" , None , None ) .await;
692
assert! ( page.contains ( " Flow is off for this repository" ) , " and the Flow tab says so" ) ;
90326c96 Andy McCall 1 month ago ↶
693
}
694
695
696
697
# [ tokio ::test ( flavor = " multi_thread" ) ]
698
async fn runs_jobs_and_logs_are_in_the_api ( ) {
699
let h = hub ( ) .await;
700
create ( & h.app, " andy" , " demo" ) .await;
701
let url = format! ( " {} /andy/demo" , h.base) ;
702
let work = tempfile:: tempdir( ) .unwrap ( ) ;
703
let c = client ( & url, work.path ( ) ) ;
704
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
705
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
706
let head = c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
707
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
708
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
709
710
711
let a = claim ( & h.app) .await.unwrap ( ) ;
712
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
713
let recs =
714
serde_json:: json! ( { " records" : [ { " t" : 1 , " stream" : " stdout" , " text" : " one" } , { " t" : 3 , " stream" : " stderr" , " text" : " three" } ] } ) ;
715
let ( st, _ ) = call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( recs.to_string ( ) ) ) .await;
716
assert! ( st.is_success ( ) , " {st}" ) ;
717
718
let ( st, body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs" , None , None ) .await;
719
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
720
let runs: Vec< hub_api:: FlowRun> = serde_json:: from_str( & body) .unwrap ( ) ;
721
assert_eq! ( runs.len ( ) , 1 ) ;
722
assert_eq! ( ( runs[ 0 ] .number, runs[ 0 ] .state.as_str ( ) , runs[ 0 ] .trigger.as_str ( ) ) , ( 1 , run.state.as_str ( ) , " push" ) ) ;
723
assert! ( runs[ 0 ] .url.ends_with ( " /andy/demo/flow/1" ) & & runs[ 0 ] .jobs.is_empty ( ) ) ;
724
725
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , None , None ) .await;
726
let one: hub_api:: FlowRun = serde_json:: from_str( & body) .unwrap ( ) ;
727
let names: Vec< ( & str , & str ) > = one.jobs.iter ( ) .map ( | j | ( j.name.as_str ( ) , j.status.as_str ( ) ) ) .collect ( ) ;
728
assert_eq! ( names.len ( ) , 3 ) ;
729
let running = one.jobs.iter ( ) .find ( | j | j.status = = " running" ) .unwrap ( ) ;
730
assert_eq! ( running.name, a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) ) ;
731
assert! ( one.jobs.iter ( ) .any ( | j | j.name = = " test" & & j.depends_on = = [ " build" ] ) ) ;
732
733
let job = running.name.clone ( ) ;
734
let ( st, body) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/demo/flow/runs/1/jobs/{job} /log" ) , None , None ) .await;
735
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
736
let lines: Vec< hub_api:: FlowLogLine> = serde_json:: from_str( & body) .unwrap ( ) ;
737
assert! ( lines.iter ( ) .any ( | l | l.text = = " three" ) , " {body}" ) ;
738
let last = lines.last ( ) .unwrap ( ) .seq;
739
let ( _ , body) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/demo/flow/runs/1/jobs/{job} /log?after={last} " ) , None , None ) .await;
740
assert_eq! ( body, " []" , " nothing after the last line" ) ;
741
assert_eq! ( call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/9" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
742
assert_eq! ( call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1/jobs/nope/log" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
743
744
745
let by = | key : String| {
746
let app = h.app.clone ( ) ;
747
async move {
748
let ( st, body) = call ( & app, " GET" , & format! ( " /api/v1/repos/andy/demo/checks/{key} " ) , None , None ) .await;
749
assert_eq! ( st, StatusCode:: OK , " {key}: {body}" ) ;
750
serde_json:: from_str:: < Vec< hub_api:: Check> > ( & body) .unwrap ( ) .len ( )
751
}
752
} ;
753
checks_when ( & h.app, run.repo_id, & run.state, 3 ) .await;
754
assert_eq! ( by ( run.state.clone ( ) ) .await, 3 ) ;
755
assert_eq! ( by ( head.to_hex ( ) ) .await, 3 , " a single head names its state" ) ;
756
assert_eq! ( by ( " main" .into ( ) ) .await, 3 ) ;
757
let ( st, _ ) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/checks/no-such-bookmark" , None , None ) .await;
758
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
759
760
761
let ( st, body) = call ( & h.app, " GET" , " /api/v1/users/andy/repos" , None , None ) .await;
762
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
763
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
764
assert_eq! ( v[ " status" ] , 404 , " {body}" ) ;
1edac4b6 Andy McCall 1 month ago ↶
765
}
cc19697f Andy McCall 1 month ago ↶
766
767
async fn get_bytes ( app : & Shared, path : & str ) -> ( StatusCode, Vec< u8 > ) {
768
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( Request:: get( path) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
769
let status = res.status ( ) ;
770
( status, res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) )
771
}
772
773
# [ tokio ::test ( flavor = " multi_thread" ) ]
774
async fn signed_receipts_are_checked_kept_and_shown ( ) {
775
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectRef, ObjectType} ;
776
use oxy_core:: identity:: SecretKey;
777
use oxy_core:: objects:: StateObject;
778
use oxy_core:: receipt:: SignedReceipt;
779
let h = hub ( ) .await;
780
create ( & h.app, " andy" , " demo" ) .await;
781
let url = format! ( " {} /andy/demo" , h.base) ;
782
let work = tempfile:: tempdir( ) .unwrap ( ) ;
783
let c = client ( & url, work.path ( ) ) ;
784
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
785
c.snapshot ( " test" ) .unwrap ( ) ;
786
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
787
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
788
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
789
let heads: Vec< ChangeHash> = run.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
790
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads.clone ( ) ) .encode ( ) ) ;
791
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
792
let sk = SecretKey:: generate( ) ;
793
let runner = sk.identity ( " test-1 (OxygenFlow runner)" , " " , 1 ) ;
794
let sign = | r : & serde_json:: Value, state : ObjectRef, sk : & SecretKey, id : & oxy_core:: identity:: Identity| {
795
let r: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
796
let run_url = format! ( " {url} /flow/{} " , run.number) ;
797
hex:: encode( SignedReceipt:: sign( r.to_oxy ( ) , repo_id, state, & run_url, id, sk) .encode ( ) )
798
} ;
799
800
let a = claim ( & h.app) .await.unwrap ( ) ;
801
let b = claim ( & h.app) .await.unwrap ( ) ;
802
let ph = a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
803
let ( a_id, b_id) = ( a[ " job_id" ] .as_i64 ( ) .unwrap ( ) , b[ " job_id" ] .as_i64 ( ) .unwrap ( ) ) ;
804
805
let rb = receipt ( " build" , & ph, 0 ) ;
806
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : rb, " signed_receipt" : sign ( & rb, state, & sk, & runner) } ) ;
807
assert_eq! ( finish ( & h.app, a_id, body) .await, StatusCode:: NO_CONTENT ) ;
808
809
let other = SecretKey:: generate( ) ;
810
let rl = receipt ( " lint" , & ph, 0 ) ;
811
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 300 , " receipt" : rl,
812
" signed_receipt" : sign ( & rl, state, & other, & other.identity ( " mallory" , " " , 1 ) ) } ) ;
813
assert_eq! ( finish ( & h.app, b_id, body) .await, StatusCode:: NO_CONTENT ) ;
814
815
let t = claim ( & h.app) .await.unwrap ( ) ;
816
let rt = receipt ( " test" , & ph, 0 ) ;
817
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 500 , " receipt" : rt,
818
" signed_receipt" : sign ( & rt, ObjectRef( [ 9 ; 32 ] ) , & sk, & runner) } ) ;
819
assert_eq! ( finish ( & h.app, t[ " job_id" ] .as_i64 ( ) .unwrap ( ) , body) .await, StatusCode:: NO_CONTENT ) ;
820
821
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
822
let job = | n : & str | jobs.iter ( ) .find ( | j | j.name = = n ) .unwrap ( ) .clone ( ) ;
823
assert_eq! ( job ( " build" ) .status, " passed" ) ;
824
assert_eq! ( ( job ( " build" ) .receipt_attest.len ( ) , job ( " build" ) .receipt_signer.as_str ( ) ) , ( 64 , " test-1 (OxygenFlow runner)" ) ) ;
825
assert_eq! ( job ( " lint" ) .status, " passed" , " the job's result stands" ) ;
826
assert! ( job ( " lint" ) .receipt_attest.is_empty ( ) ) ;
827
assert! ( job ( " lint" ) .receipt_note.contains ( " not the key this runner signs with" ) , " {}" , job ( " lint" ) .receipt_note) ;
828
assert! ( job ( " test" ) .receipt_note.contains ( " another state" ) , " {}" , job ( " test" ) .receipt_note) ;
829
830
831
832
let kept = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .receipts ( & heads) .unwrap ( ) ;
833
assert_eq! ( kept.len ( ) , 1 ) ;
834
let attest = job ( " build" ) .receipt_attest;
835
let ( st, bytes) = get_bytes ( & h.app, & format! ( " /andy/demo/receipts/{attest} " ) ) .await;
836
assert_eq! ( st, StatusCode:: OK ) ;
837
let sr = SignedReceipt:: decode( & bytes) .unwrap ( ) ;
838
sr.verify ( ) .unwrap ( ) ;
839
assert_eq! ( ( sr.state ( ) , sr.receipt.job_name.as_str ( ) ) , ( state, " build" ) ) ;
840
assert_eq! ( get_bytes ( & h.app, & format! ( " /andy/demo/receipts/{} " , " 0" .repeat ( 64 ) ) ) .await.0 , StatusCode:: NOT_FOUND ) ;
841
842
843
844
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
845
assert! ( page.contains ( " signed receipt</a> by test-1" ) , " run page" ) ;
846
assert! ( page.contains ( " receipt not kept" ) , " run page" ) ;
847
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/change/{} " , heads[ 0 ] .to_hex ( ) ) , None , None ) .await;
848
assert! ( page.contains ( " <dt>Receipts</dt>" ) & & page.contains ( & format! ( " /andy/demo/receipts/{attest} " ) ) , " change page" ) ;
849
}
a3c8288b Andy McCall 1 month ago ↶
850
851
async fn upload ( app : & Shared, job : i64 , name : & str , bytes : & [u8 ]) -> ( StatusCode, String) {
852
let req = Request:: post( format! ( " /api/v1/flow/jobs/{job} /artifacts/{name} " ) )
853
.header ( header:: AUTHORIZATION , " Bearer runner-secret" )
854
.header ( header:: CONTENT_TYPE , " application/octet-stream" )
855
.body ( Body:: from( bytes.to_vec ( ) ) )
856
.unwrap ( ) ;
857
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
858
let status = res.status ( ) ;
859
( status, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
860
}
861
862
# [ tokio ::test ( flavor = " multi_thread" ) ]
863
async fn artifacts_are_kept_named_by_the_receipt_and_served ( ) {
864
let h = hub ( ) .await;
865
create ( & h.app, " andy" , " demo" ) .await;
866
let url = format! ( " {} /andy/demo" , h.base) ;
867
let work = tempfile:: tempdir( ) .unwrap ( ) ;
868
let c = client ( & url, work.path ( ) ) ;
869
write (
870
work.path ( ) ,
871
" .flow/pipeline.kdl" ,
872
& format! (
873
" 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 "
874
) ,
875
) ;
876
c.snapshot ( " test" ) .unwrap ( ) ;
877
c.commit ( " Build a tool" , & [ ] ) .unwrap ( ) ;
878
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
879
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
880
let a = claim ( & h.app) .await.unwrap ( ) ;
881
assert_eq! ( a[ " job" ] [ " outputs" ] , serde_json:: json! ( [ { " name" : " tool" , " path" : " /src/out/tool" } ] ) , " the runner is told" ) ;
882
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
883
884
let body: Vec< u8 > = ( 0 .. 300_000 u32 ) .map ( | i | ( i % 253 ) as u8 ) .collect ( ) ;
885
let ( st, text) = upload ( & h.app, id, " other" , & body) .await;
886
assert_eq! ( st, StatusCode:: BAD_REQUEST , " an undeclared artifact: {text}" ) ;
887
let ( st, text) = upload ( & h.app, id, " tool" , & body) .await;
888
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
889
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
890
assert_eq! ( blob, oxy_core:: repo:: Repo:: blob_ref( & body) .to_hex ( ) , " the same blob the runner computes" ) ;
891
892
893
let mut r = receipt ( " build" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
894
assert_eq! (
895
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : r.clone ( ) } ) ) .await,
896
StatusCode:: BAD_REQUEST
897
) ;
898
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
899
assert_eq! ( finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : r } ) ) .await, StatusCode:: NO_CONTENT ) ;
900
901
902
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
903
let link = format! ( " /andy/demo/flow/{} /artifacts/build/tool" , run.number) ;
904
assert! ( page.contains ( & link) , " run page lists the artifact" ) ;
16ee5d19 Andy McCall 18 days ago ↶
905
906
907
let ( _ , list) = call ( & h.app, " GET" , " /andy/demo/artifacts" , None , None ) .await;
908
assert! ( list.contains ( & link) & & list.contains ( & format! ( " #{} </a>" , run.number) ) , " {list}" ) ;
909
assert! ( list.contains ( " <summary class=\" on\" >Artifacts</summary>" ) & & list.contains ( " href=\" /andy/demo/images\" " ) , " {list}" ) ;
a3c8288b Andy McCall 1 month ago ↶
910
let ( st, got) = get_bytes ( & h.app, & link) .await;
911
assert_eq! ( ( st, got = = body) , ( StatusCode:: OK , true ) ) ;
912
assert_eq! ( get_bytes ( & h.app, & format! ( " /andy/demo/flow/{} /artifacts/build/none" , run.number) ) .await.0 , StatusCode:: NOT_FOUND ) ;
076c7a7c Andy McCall 20 days ago ↶
913
914
915
let prov = format! ( " {link} /provenance" ) ;
916
assert_eq! ( get_bytes ( & h.app, & prov) .await.0 , StatusCode:: BAD_REQUEST , " no kept receipt, no provenance" ) ;
917
sqlx:: query( " UPDATE flow_jobs SET receipt_attest = 'a1b2', receipt_signer = 'test-1 runner' WHERE id = $1" )
918
.bind ( id)
919
.execute ( & h.app.db)
920
.await
921
.unwrap ( ) ;
922
let ( st, env) = get_bytes ( & h.app, & prov) .await;
923
assert_eq! ( st, StatusCode:: OK ) ;
924
let env: serde_json:: Value = serde_json:: from_slice( & env) .unwrap ( ) ;
925
assert_eq! ( env[ " payloadType" ] , " application/vnd.in-toto+json" ) ;
926
let unb64 = | t : & str | -> Vec< u8 > {
927
let val = | c : u8 | match c {
928
b ' A' .. = b ' Z' => c - b ' A' ,
929
b ' a' .. = b ' z' => c - b ' a' + 26 ,
930
b ' 0' .. = b ' 9' => c - b ' 0' + 52 ,
931
b ' +' => 62 ,
932
_ => 63 ,
933
} ;
934
let clean: Vec< u8 > = t.bytes ( ) .filter ( | c | * c ! = b ' =' ) .collect ( ) ;
935
let mut out = Vec :: new( ) ;
936
for chunk in clean.chunks ( 4 ) {
937
let n = chunk.iter ( ) .enumerate ( ) .fold ( 0 u32 , | acc , ( i , c ) | acc | ( val ( * c) as u32 ) < < ( 18 - 6 * i) ) ;
938
for i in 0 .. chunk.len ( ) - 1 {
939
out.push ( ( n > > ( 16 - 8 * i) ) as u8 ) ;
940
}
941
}
942
out
943
} ;
944
let payload = unb64 ( env[ " payload" ] .as_str ( ) .unwrap ( ) ) ;
945
let sig = unb64 ( env[ " signatures" ] [ 0 ] [ " sig" ] .as_str ( ) .unwrap ( ) ) ;
946
let signed = oxygenhub:: provenance:: pae( " application/vnd.in-toto+json" , & payload) ;
947
ring:: signature:: UnparsedPublicKey:: new( & ring:: signature:: ED25519 , h.app.hub_key.identity.ed25519_pk)
948
.verify ( & signed, & sig)
949
.expect ( " the hub's key signed the envelope" ) ;
950
let statement: serde_json:: Value = serde_json:: from_slice( & payload) .unwrap ( ) ;
951
use sha2:: Digest;
952
assert_eq! ( statement[ " subject" ] [ 0 ] [ " digest" ] [ " sha256" ] , hex:: encode( sha2:: Sha256:: digest( & body) ) ) ;
953
assert_eq! ( statement[ " predicateType" ] , " https://slsa.dev/provenance/v1" ) ;
954
assert_eq! ( statement[ " predicate" ] [ " buildDefinition" ] [ " externalParameters" ] [ " job" ] , " build" ) ;
955
assert_eq! ( statement[ " predicate" ] [ " runDetails" ] [ " byproducts" ] [ 0 ] [ " digest" ] [ " oxygen-attestation" ] , " a1b2" ) ;
956
let ( _ , pem) = get_bytes ( & h.app, " /.well-known/oxyhub-signing.pem" ) .await;
957
let pem = String :: from_utf8( pem) .unwrap ( ) ;
958
let der = unb64 ( pem.lines ( ) .filter ( | l | ! l.starts_with ( " -----" ) ) .collect:: < String> ( ) .as_str ( ) ) ;
959
assert_eq! ( & der[ der.len ( ) - 32 .. ] , & h.app.hub_key.identity.ed25519_pk[ .. ] , " the PEM holds the hub's key" ) ;
960
sqlx:: query( " UPDATE flow_jobs SET receipt_attest = '', receipt_signer = '' WHERE id = $1" ) .bind ( id) .execute ( & h.app.db) .await.unwrap ( ) ;
10708ab8 Andy McCall 1 month ago ↶
961
45977501 Andy McCall 26 days ago ↶
962
963
964
{
965
let name = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
966
let mut hosted = oxy_core:: repo:: Repo:: open( & h.app.repos.dir ( & name) ) .unwrap ( ) ;
967
hosted.config.set ( " core.gc-grace" , " 0" ) .unwrap ( ) ;
968
hosted.save_config ( ) .unwrap ( ) ;
969
}
970
let done = oxygenhub:: maintenance:: gc_all( & h.app) .await;
971
assert! ( done.iter ( ) .all ( | d | ! d.failed) , " {done:?}" ) ;
972
let ( st, got) = get_bytes ( & h.app, & link) .await;
973
assert_eq! ( ( st, got = = body) , ( StatusCode:: OK , true ) , " the artifact survives gc" ) ;
974
10708ab8 Andy McCall 1 month ago ↶
975
976
977
let form = | f : String| {
978
Request:: post( " /register" ) .header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) .body ( Body:: from( f) ) .unwrap ( )
979
} ;
980
let res = oxygenhub:: router( h.app.clone ( ) )
c8684d44 Andy McCall 20 days ago
981
.oneshot ( form ( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" .into ( ) ) )
10708ab8 Andy McCall 1 month ago ↶
982
.await
983
.unwrap ( ) ;
984
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
985
let with_cookie = | method : & str , path : & str , body : Option< String> | {
986
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
987
if body.is_some ( ) {
988
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
989
}
990
req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( )
991
} ;
992
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
993
db:: releases:: create( & h.app.db, row.id, " v1" , " One" , " " , false , None ) .await.unwrap ( ) ;
994
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( with_cookie ( " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None ) ) .await.unwrap ( ) ;
995
let page = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
996
let attach = format! ( " /andy/demo/flow/{} /artifacts/build/tool/attach" , run.number) ;
997
assert! ( page.contains ( & attach) , " a writer is offered the release" ) ;
998
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
999
let csrf = page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1000
let release_id = db:: releases:: list( & h.app.db, row.id) .await.unwrap ( ) [ 0 ] .id;
1001
let res = oxygenhub:: router( h.app.clone ( ) )
1002
.oneshot ( with_cookie ( " POST" , & attach, Some ( format! ( " _csrf={csrf} &release={release_id} " ) ) ) )
1003
.await
1004
.unwrap ( ) ;
1005
assert_eq! ( res.status ( ) , StatusCode:: SEE_OTHER ) ;
1006
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/releases/tag/v1" , None , None ) .await;
1007
assert! (
1008
page.contains ( " >tool</a>" ) & & page.contains ( & format! ( " built by <a href=\" /andy/demo/flow/{0} \" >run #{0} </a> build" , run.number) ) ,
1009
" {page}"
1010
) ;
1011
let ( _ , api) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/releases" , None , None ) .await;
1012
let api: serde_json:: Value = serde_json:: from_str( & api) .unwrap ( ) ;
1013
let asset = & api[ 0 ] [ " assets" ] [ 0 ] ;
1014
assert_eq! (
1015
( asset[ " name" ] .as_str ( ) , asset[ " built_by" ] [ " run" ] .as_i64 ( ) , asset[ " built_by" ] [ " job" ] .as_str ( ) ) ,
1016
( Some ( " tool" ) , Some ( run.number) , Some ( " build" ) )
1017
) ;
1018
assert! ( asset[ " built_by" ] [ " receipt" ] .is_null ( ) , " that receipt was not signed" ) ;
a3c8288b Andy McCall 1 month ago ↶
1019
}
df940d75 Andy McCall 1 month ago ↶
1020
d29070fa Andy McCall 1 month ago ↶
1021
1022
a36bab6b Andy McCall 26 days ago ↶
1023
async fn upload_report_file ( app : & Shared, job : i64 , name : & str , path : & str , bytes : & [u8 ]) -> ( StatusCode, String) {
1024
let req = Request:: post( format! ( " /api/v1/flow/jobs/{job} /reports/{name} /files" ) )
1025
.header ( header:: AUTHORIZATION , " Bearer runner-secret" )
1026
.header ( header:: CONTENT_TYPE , " application/octet-stream" )
1027
.header ( " X-Flow-Report-Path" , path)
1028
.body ( Body:: from( bytes.to_vec ( ) ) )
1029
.unwrap ( ) ;
1030
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
1031
let status = res.status ( ) ;
1032
( status, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
1033
}
1034
1035
1036
1037
1038
# [ tokio ::test ( flavor = " multi_thread" ) ]
1039
async fn reports_are_kept_file_by_file_and_indexed ( ) {
1040
let h = hub ( ) .await;
1041
create ( & h.app, " andy" , " demo" ) .await;
1042
let url = format! ( " {} /andy/demo" , h.base) ;
1043
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1044
let c = client ( & url, work.path ( ) ) ;
1045
write (
1046
work.path ( ) ,
1047
" .flow/pipeline.kdl" ,
1048
& format! (
1049
" 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 "
1050
) ,
1051
) ;
1052
c.snapshot ( " test" ) .unwrap ( ) ;
1053
c.commit ( " Measure frames" , & [ ] ) .unwrap ( ) ;
1054
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1055
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
1056
let a = claim ( & h.app) .await.unwrap ( ) ;
1057
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1058
1059
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 " ;
1060
let page = b " <p>frames</p>" .to_vec ( ) ;
1061
let data: Vec< u8 > = ( 0 .. 100_000 u32 ) .map ( | i | ( i % 241 ) as u8 ) .collect ( ) ;
c8df33a6 Andy McCall 25 days ago ↶
1062
let ( page_bytes, data_bytes) = ( page.clone ( ) , data.clone ( ) ) ;
a36bab6b Andy McCall 26 days ago ↶
1063
let files: [ ( & str , & [ u8 ] ) ; 3 ] = [ ( " data/frames.csv" , & data) , ( " index.html" , & page) , ( " report.toml" , toml) ] ;
1064
1065
assert_eq! ( upload_report_file ( & h.app, id, " tool" , " report.toml" , toml) .await.0 , StatusCode:: BAD_REQUEST , " not a report" ) ;
1066
assert_eq! ( upload_report_file ( & h.app, id, " frame-timing" , " ../x" , toml) .await.0 , StatusCode:: BAD_REQUEST , " not a clean path" ) ;
1067
let mut listed = vec! [ ] ;
1068
for ( path, bytes) in files {
1069
let ( st, text) = upload_report_file ( & h.app, id, " frame-timing" , path, bytes) .await;
1070
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
1071
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1072
assert_eq! ( blob, oxy_core:: repo:: Repo:: blob_ref( bytes) .to_hex ( ) ) ;
1073
listed.push ( serde_json:: json! ( { " path" : path, " blob" : blob, " size" : bytes.len ( ) } ) ) ;
1074
}
1075
let mut missing = listed.clone ( ) ;
1076
missing.push ( serde_json:: json! ( { " path" : " assets/x.js" , " blob" : " ab" .repeat ( 32 ) , " size" : 1 } ) ) ;
1077
let ( st, text) =
1078
upload ( & h.app, id, " frame-timing" , & serde_json:: to_vec( & serde_json:: json! ( { " version" : 1 , " files" : missing } ) ) .unwrap ( ) ) .await;
1079
assert_eq! ( st, StatusCode:: BAD_REQUEST , " a file never uploaded: {text}" ) ;
1080
assert! ( text.contains ( " assets/x.js" ) , " {text}" ) ;
1081
let manifest = serde_json:: to_vec( & serde_json:: json! ( { " version" : 1 , " files" : listed } ) ) .unwrap ( ) ;
1082
let ( st, text) = upload ( & h.app, id, " frame-timing" , & manifest) .await;
1083
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
1084
1085
let ( kind, title, summary, files_n, bytes) : ( String , String , String , i64 , i64 ) =
3b1bc27f Andy McCall 20 days ago
1086
sqlx:: query_as( " SELECT kind, title, summary, files, bytes FROM reports WHERE job_id = $1 AND name = 'frame-timing'" )
a36bab6b Andy McCall 26 days ago ↶
1087
.bind ( id)
1088
.fetch_one ( & h.app.db)
1089
.await
1090
.unwrap ( ) ;
1091
assert_eq! ( ( kind.as_str ( ) , title.as_str ( ) , summary.as_str ( ) ) , ( " performance" , " Frame timing" , " Median 16.4 ms" ) ) ;
1092
assert_eq! ( ( files_n, bytes) , ( 3 , ( data.len ( ) + page.len ( ) + toml.len ( ) ) as i64 ) ) ;
3b1bc27f Andy McCall 20 days ago
1093
let rid: i64 = sqlx:: query_scalar( " SELECT id FROM reports WHERE job_id = $1" ) .bind ( id) .fetch_one ( & h.app.db) .await.unwrap ( ) ;
a36bab6b Andy McCall 26 days ago ↶
1094
let m = oxygenhub:: reports:: metrics( & h.app.db, rid) .await.unwrap ( ) ;
1095
assert_eq! ( ( m[ 0 ] .key.as_str ( ) , m[ 0 ] .value, m[ 0 ] .fail_above) , ( " median_ms" , 16. 4 , Some ( 20. 0 ) ) ) ;
1096
let log = db:: flow:: log( & h.app.db, id, 0 , 1000 ) .await.unwrap ( ) ;
1097
assert! ( log.iter ( ) .any ( | r | r.text.contains ( " report frame-timing: performance \" Frame timing\" , 1 metric" ) ) , " {log:?}" ) ;
c8df33a6 Andy McCall 25 days ago ↶
1098
1099
1100
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
1101
let c = checks.iter ( ) .find ( | c | c.name = = " report/frame-timing/median_ms" ) .expect ( " a check per threshold" ) ;
1102
assert_eq! ( c.status, " passed" , " {c:?}" ) ;
1103
assert! ( c.summary.contains ( " 16.4 ms" ) & & c.url.contains ( " /reports/frame-timing?id=" ) , " {c:?}" ) ;
1104
1105
1106
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo" , None , None ) .await;
1107
assert! ( page.contains ( " >Reports</a>" ) , " the tab" ) ;
1108
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/reports" , None , None ) .await;
1109
assert! ( st = = StatusCode:: OK & & page.contains ( " Frame timing" ) & & page.contains ( " median_ms" ) , " {st}" ) ;
1110
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/reports/frame-timing" , None , None ) .await;
1111
assert! ( st = = StatusCode:: OK & & page.contains ( " fails above 20" ) & & page.contains ( " sandbox=\" allow-scripts\" " ) , " {st}" ) ;
1112
let src = page.split ( " class=\" rep-frame\" src=\" " ) .nth ( 1 ) .unwrap ( ) .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1113
assert! ( src.ends_with ( " /index.html" ) , " {src}" ) ;
1114
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( Request:: get( & src) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
1115
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
1116
let csp = res.headers ( ) [ header:: CONTENT_SECURITY_POLICY ] .to_str ( ) .unwrap ( ) .to_string ( ) ;
1117
assert! ( csp.starts_with ( " sandbox allow-scripts;" ) & & csp.contains ( " default-src 'none'" ) , " {csp}" ) ;
d74ce22f Andy McCall 21 days ago ↶
1118
1119
assert! (
1120
res.headers ( ) .get ( header:: X_FRAME_OPTIONS ) .is_none ( ) & & res.headers ( ) .get ( header:: CONTENT_SECURITY_POLICY_REPORT_ONLY ) .is_none ( )
1121
) ;
1122
assert_eq! ( res.headers ( ) [ header:: X_CONTENT_TYPE_OPTIONS ] , " nosniff" ) ;
c8df33a6 Andy McCall 25 days ago ↶
1123
assert_eq! ( res.headers ( ) [ header:: ACCESS_CONTROL_ALLOW_ORIGIN ] , " *" ) ;
f3e5e2bc Andy McCall 25 days ago ↶
1124
1125
let served = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ;
1126
assert! ( served.starts_with ( & page_bytes) , " the report's page" ) ;
1127
assert! ( String :: from_utf8_lossy( & served) .contains ( " /static/report-frame.js" ) , " with the frame script" ) ;
1128
let ( st, _ ) = get_bytes ( & h.app, " /static/report.css" ) .await;
1129
assert_eq! ( st, StatusCode:: OK , " the stylesheet a report may link" ) ;
c8df33a6 Andy McCall 25 days ago ↶
1130
let data = src.replace ( " /index.html" , " /data/frames.csv" ) ;
1131
assert_eq! ( get_bytes ( & h.app, & data) .await, ( StatusCode:: OK , data_bytes.clone ( ) ) ) ;
1132
95b525b0 Andy McCall 25 days ago ↶
1133
1134
1135
let first: i64 = sqlx:: query_scalar( " SELECT id FROM reports WHERE name = 'frame-timing'" ) .fetch_one ( & h.app.db) .await.unwrap ( ) ;
1136
let second: i64 = sqlx:: query_scalar(
1137
" INSERT INTO reports (repo_id, name, kind, title, summary, context, run_id, job_id, run_number, job, bookmark, state, manifest, files, bytes, created_ms) \
3b1bc27f Andy McCall 20 days ago
1138
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 = $1 RETURNING id" ,
95b525b0 Andy McCall 25 days ago ↶
1139
)
1140
.bind ( first)
1141
.fetch_one ( & h.app.db)
1142
.await
1143
.unwrap ( ) ;
1144
sqlx:: query(
3b1bc27f Andy McCall 20 days ago
1145
" INSERT INTO report_metrics (report_id, key, value, unit, better, fail_above) VALUES ($1, 'median_ms', 18.0, 'ms', 'lower', 20)" ,
95b525b0 Andy McCall 25 days ago ↶
1146
)
1147
.bind ( second)
1148
.execute ( & h.app.db)
1149
.await
1150
.unwrap ( ) ;
1151
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/reports/frame-timing" , None , None ) .await;
1152
assert_eq! ( page.matches ( " data-tl-stop" ) .count ( ) , 2 , " a stop per run" ) ;
1153
assert! ( page.contains ( " Median 18.0 ms" ) , " the newest run is shown" ) ;
1154
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/reports/frame-timing?id={first} &compare={second} &play=3" ) , None , None ) .await;
1155
assert! ( page.contains ( " This run against run #2" ) & & page.contains ( " -1.6" ) , " every metric against the other run" ) ;
1156
let stop = page.split ( " data-tl-stop" ) .next ( ) .unwrap_or ( " " ) .rsplit ( " href=" ) .next ( ) .unwrap_or ( " " ) .to_string ( ) ;
1157
assert! ( page.contains ( " data-play=\" 3\" " ) , " playing" ) ;
1158
assert! ( stop.contains ( " play" ) & & stop.contains ( ' 3' ) , " playing carries on from stop to stop: {stop}" ) ;
1159
assert! ( page.contains ( " data-report-runs=" ) , " the frame interface knows the runs" ) ;
c8df33a6 Andy McCall 25 days ago ↶
1160
let forged = src.replacen ( " /files/" , " /files/9" , 1 ) ;
1161
assert_eq! ( get_bytes ( & h.app, & forged) .await.0 , StatusCode:: NOT_FOUND ) ;
1162
assert_eq! ( get_bytes ( & h.app, & src.replace ( " /index.html" , " /secret.txt" ) ) .await.0 , StatusCode:: NOT_FOUND ) ;
a36bab6b Andy McCall 26 days ago ↶
1163
let _ = run;
1164
1165
1166
{
1167
let name = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
1168
let mut hosted = oxy_core:: repo:: Repo:: open( & h.app.repos.dir ( & name) ) .unwrap ( ) ;
1169
hosted.config.set ( " core.gc-grace" , " 0" ) .unwrap ( ) ;
1170
hosted.save_config ( ) .unwrap ( ) ;
1171
}
1172
let done = oxygenhub:: maintenance:: gc_all( & h.app) .await;
1173
assert! ( done.iter ( ) .all ( | d | ! d.failed) , " {done:?}" ) ;
1174
let hosted = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
1175
for ( path, bytes) in files {
1176
assert_eq! ( hosted.read_blob ( & oxy_core:: repo:: Repo:: blob_ref( bytes) ) .unwrap ( ) , bytes, " {path} survives gc" ) ;
1177
}
1178
}
1179
d29070fa Andy McCall 1 month ago ↶
1180
# [ tokio ::test ( flavor = " multi_thread" ) ]
1181
async fn a_failing_job_keeps_an_artifact_outside_its_receipt ( ) {
1182
let h = hub ( ) .await;
1183
create ( & h.app, " andy" , " demo" ) .await;
1184
let url = format! ( " {} /andy/demo" , h.base) ;
1185
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1186
let c = client ( & url, work.path ( ) ) ;
1187
write (
1188
work.path ( ) ,
1189
" .flow/pipeline.kdl" ,
1190
& format! (
336c953a Andy McCall 1 month ago ↶
1191
" 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 ↶
1192
) ,
1193
) ;
1194
c.snapshot ( " test" ) .unwrap ( ) ;
1195
c.commit ( " Test" , & [ ] ) .unwrap ( ) ;
1196
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1197
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
1198
let a = claim ( & h.app) .await.unwrap ( ) ;
336c953a Andy McCall 1 month ago ↶
1199
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 ↶
1200
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
0b203bbf Andy McCall 1 month ago ↶
1201
1202
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>"# ;
1203
let ( st, text) = upload ( & h.app, id, " junit" , xml) .await;
d29070fa Andy McCall 1 month ago ↶
1204
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
1205
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1206
let mut r = receipt ( " test" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 1 ) ;
1207
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
1208
assert_eq! (
1209
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 1 , " duration_ms" : 700 , " receipt" : r.clone ( ) } ) ) .await,
1210
StatusCode:: BAD_REQUEST ,
1211
" a failing receipt names no outputs"
1212
) ;
1213
r[ " outputs" ] = serde_json:: json! ( [ ] ) ;
1214
assert_eq! ( finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 1 , " duration_ms" : 700 , " receipt" : r } ) ) .await, StatusCode:: NO_CONTENT ) ;
1215
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
1216
assert! ( page.contains ( & format! ( " /andy/demo/flow/{} /artifacts/test/junit" , run.number) ) , " listed" ) ;
1217
assert! ( page.contains ( " not in its receipt" ) , " marked" ) ;
0b203bbf Andy McCall 1 month ago ↶
1218
assert! ( page.contains ( " 2 tests: 1 failed" ) , " counts beside the job" ) ;
1219
assert! ( page.contains ( " tests.test_io::test_write" ) & & page.contains ( " disk full" ) & & page.contains ( " OSError" ) , " the failure" ) ;
1220
assert! ( page.contains ( " tests: 2 tests: 1 failed in 2.0 s" ) , " said in the job's log" ) ;
d29070fa Andy McCall 1 month ago ↶
1221
}
1222
df940d75 Andy McCall 1 month ago ↶
1223
1224
1225
1226
# [ tokio ::test ( flavor = " multi_thread" ) ]
1227
async fn a_release_is_audited_from_authors_to_receipts ( ) {
1228
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectType} ;
1229
use oxy_core:: identity:: SecretKey;
1230
use oxy_core:: objects:: StateObject;
1231
use oxy_core:: receipt:: SignedReceipt;
1232
let h = hub ( ) .await;
1233
create ( & h.app, " andy" , " demo" ) .await;
1234
let url = format! ( " {} /andy/demo" , h.base) ;
1235
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1236
let mut c = client ( & url, work.path ( ) ) ;
1237
c.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
1238
write (
1239
work.path ( ) ,
1240
" .flow/pipeline.kdl" ,
1241
& format! (
1242
" 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 "
1243
) ,
1244
) ;
1245
c.snapshot ( " test" ) .unwrap ( ) ;
1246
c.commit ( " Build a tool" , & [ ] ) .unwrap ( ) ;
1247
let heads = c.bookmark_heads ( ) .unwrap ( ) ;
1248
c.tag_create ( " v1" , & heads, " first" ) .unwrap ( ) ;
1249
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1250
1251
1252
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
1253
let a = claim ( & h.app) .await.unwrap ( ) ;
1254
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1255
let body: Vec< u8 > = b " #!/bin/sh\n echo tool\n " .to_vec ( ) ;
1256
let ( _ , text) = upload ( & h.app, id, " tool" , & body) .await;
1257
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1258
let mut r = receipt ( " build" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
1259
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
1260
let heads_run: Vec< ChangeHash> = run.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
1261
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads_run) .encode ( ) ) ;
1262
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
1263
let sk = SecretKey:: generate( ) ;
1264
let runner = sk.identity ( " test-1 (OxygenFlow runner)" , " " , 1 ) ;
1265
let fr: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
1266
let signed = hex:: encode( SignedReceipt:: sign( fr.to_oxy ( ) , repo_id, state, & format! ( " {url} /flow/1" ) , & runner, & sk) .encode ( ) ) ;
1267
let done = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 900 , " receipt" : r, " signed_receipt" : signed } ) ;
1268
assert_eq! ( finish ( & h.app, id, done) .await, StatusCode:: NO_CONTENT ) ;
1269
1270
1271
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
1272
let rel = db:: releases:: create( & h.app.db, row.id, " v1" , " One" , " " , false , None ) .await.unwrap ( ) ;
1273
oxygenhub:: web:: releases:: store_asset( & h.app, rel.id, " tool" , " " , & body) .await.unwrap ( ) ;
1274
oxygenhub:: web:: releases:: store_asset( & h.app, rel.id, " notes.txt" , " " , b " by hand\n " ) .await.unwrap ( ) ;
1275
1276
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/releases/trust/v1" , None , None ) .await;
1277
assert_eq! ( st, StatusCode:: OK ) ;
1278
for want in [
1279
" Tag v1 is signed" ,
1280
" The one change in the release is authenticated" ,
1281
" tool: signed receipt from run #1 build" ,
1282
" a runner key of this hub" ,
1283
" tool: built from the tagged state" ,
1284
" tool: pinned environment" ,
1285
" notes.txt: no build found" ,
1286
" <svg class=\" trust-graph\" " ,
bf5101d3 Andy McCall 1 month ago ↶
1287
1288
1289
" Tag signed by" ,
1290
" 1 of 1 changes signed or sealed" ,
1291
" 1 of 2 assets built and attested by OxygenFlow" ,
1292
" attested build" ,
1293
" uploaded by hand" ,
1294
" receipt verified" ,
1295
" class=\" shackle\" " ,
1296
" <title>notes.txt:" ,
df940d75 Andy McCall 1 month ago ↶
1297
] {
1298
assert! ( page.contains ( want) , " missing {want:?}" ) ;
1299
}
bf5101d3 Andy McCall 1 month ago ↶
1300
let svg = & page[ page.find ( " <svg class=\" trust-graph\" " ) .unwrap ( ) .. ] ;
1301
assert! ( svg[ .. 200 ] .contains ( " width=\" " ) & & svg[ .. 200 ] .contains ( " height=\" " ) , " the graph has its own size: {}" , & svg[ .. 200 ] ) ;
df940d75 Andy McCall 1 month ago ↶
1302
let ( _ , releases) = call ( & h.app, " GET" , " /andy/demo/releases" , None , None ) .await;
1303
assert! ( releases.contains ( " /andy/demo/releases/trust/v1" ) , " the release links its audit" ) ;
1304
1305
1306
let ( st, json) = call ( & h.app, " GET" , " /andy/demo/releases/trust/v1?format=json" , None , None ) .await;
1307
assert_eq! ( st, StatusCode:: OK ) ;
1308
let t: serde_json:: Value = serde_json:: from_str( & json) .unwrap ( ) ;
1309
assert_eq! ( ( t[ " tag" ] .as_str ( ) , t[ " tag_signature" ] .as_str ( ) , t[ " changes" ] .as_i64 ( ) ) , ( Some ( " v1" ) , Some ( " valid" ) , Some ( 1 ) ) ) ;
1310
assert_eq! ( t[ " authors" ] [ 0 ] [ " who" ] [ " name" ] , " Andy" ) ;
1311
let tool = t[ " assets" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | a | a[ " name" ] = = " tool" ) .unwrap ( ) ;
1312
let hex_receipt = tool[ " build" ] [ " signed_receipt" ] .as_str ( ) .unwrap ( ) ;
1313
let sr = SignedReceipt:: decode( & hex:: decode( hex_receipt) .unwrap ( ) ) .unwrap ( ) ;
1314
sr.verify ( ) .unwrap ( ) ;
1315
assert_eq! ( sr.receipt.outputs[ 0 ] .to_hex ( ) , blob) ;
1316
let audit = t[ " audit" ] .as_array ( ) .unwrap ( ) ;
1317
assert! ( audit.iter ( ) .any ( | c | c[ " verdict" ] = = " warn" & & c[ " title" ] .as_str ( ) .unwrap ( ) .starts_with ( " notes.txt" ) ) ) ;
1318
assert! ( ! audit.iter ( ) .any ( | c | c[ " verdict" ] = = " fail" ) , " {audit:?}" ) ;
1319
1320
let ( st, api) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/releases/trust/v1" , None , None ) .await;
1321
assert_eq! ( st, StatusCode:: OK ) ;
1322
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & api) .unwrap ( ) [ " assets" ] .as_array ( ) .unwrap ( ) .len ( ) , 2 ) ;
1323
assert_eq! ( call ( & h.app, " GET" , " /api/v1/repos/andy/demo/releases/trust/v9" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
1324
assert_eq! ( call ( & h.app, " GET" , " /andy/demo/releases/trust/v9" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
1325
}
b6b7e4cf Andy McCall 1 month ago ↶
1326
1327
1328
1329
1330
# [ tokio ::test ( flavor = " multi_thread" ) ]
1331
async fn runners_register_show_their_history_and_are_drained_and_removed ( ) {
1332
use oxy_core:: identity:: SecretKey;
1333
let h = hub ( ) .await;
1334
create ( & h.app, " andy" , " demo" ) .await;
1335
1336
let res = oxygenhub:: router( h.app.clone ( ) )
1337
.oneshot (
1338
Request:: post( " /register" )
1339
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 20 days ago
1340
.body ( Body:: from( " username=carol&email=carol%40example.com&password=correct-horse-battery&setup=test-setup" ) )
b6b7e4cf Andy McCall 1 month ago ↶
1341
.unwrap ( ) ,
1342
)
1343
.await
1344
.unwrap ( ) ;
1345
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1346
let admin = db:: accounts:: account_by_name( & h.app.db, " carol" ) .await.unwrap ( ) .unwrap ( ) ;
1347
db:: accounts:: set_admin( & h.app.db, admin.id, true ) .await.unwrap ( ) ;
1348
let web = | method : & 'static str , path : String, body : Option< String> | {
1349
let ( app, cookie) = ( h.app.clone ( ) , cookie.clone ( ) ) ;
1350
async move {
1351
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie) ;
1352
if body.is_some ( ) {
1353
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
1354
}
1355
let res = oxygenhub:: router( app) .oneshot ( req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ) .await.unwrap ( ) ;
1356
let st = res.status ( ) ;
1357
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
1358
}
1359
} ;
1360
let csrf_of = | page : & str | {
1361
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
1362
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
1363
} ;
1364
let ( st, page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1365
assert_eq! ( st, StatusCode:: OK ) ;
1366
assert! ( page.contains ( " No runners yet" ) ) ;
1367
assert_eq! ( call ( & h.app, " GET" , " /admin/runners" , None , None ) .await.0 , StatusCode:: NOT_FOUND , " administrators only" ) ;
1368
1369
1370
let ( _ , page) = web ( " POST" , " /admin/runners/token" .into ( ) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1371
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
1372
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
1373
assert! ( token.starts_with ( " flowreg_" ) , " {token}" ) ;
1374
1375
1376
let sk = SecretKey:: generate( ) ;
1377
let identity = sk.identity ( " plex (OxygenFlow runner)" , " " , 1 ) ;
1378
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 } ) ;
1379
let reg = serde_json:: json! ( { " token" : token, " name" : " plex" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1380
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1381
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1382
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1383
assert! ( credential.starts_with ( " flowrunner_" ) ) ;
1384
assert_eq! (
1385
call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await.0 ,
1386
StatusCode:: FORBIDDEN ,
1387
" used once"
1388
) ;
1389
1390
1391
let claim_as = | who : String| {
1392
let app = h.app.clone ( ) ;
1393
let info = info.clone ( ) ;
1394
async move {
1395
let body = serde_json:: json! ( { " runner" : " ignored" , " runtimes" : [ " microvm" ] , " info" : info } ) .to_string ( ) ;
1396
call ( & app, " POST" , " /api/v1/flow/claim" , Some ( & who) , Some ( body) ) .await
1397
}
1398
} ;
1399
assert_eq! ( claim_as ( credential.clone ( ) ) .await.0 , StatusCode:: NO_CONTENT , " nothing queued" ) ;
1400
assert_eq! ( claim ( & h.app) .await, None ) ;
1401
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1402
assert! (
1403
page.contains ( " >plex</strong>" ) & & page.contains ( " st-online" ) & & page.contains ( " x86_64 · linux" ) & & page.contains ( " big-disk" ) ,
1404
" {page}"
1405
) ;
1406
assert! ( page.contains ( " >test-1</strong>" ) & & page.contains ( " hub.toml" ) , " the configured runner, by its claimed name" ) ;
1407
let plex = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " plex" ) .unwrap ( ) ;
1408
assert_eq! ( plex.key, identity.hash ( ) .to_hex ( ) , " bound at registration" ) ;
1409
1410
1411
let url = format! ( " {} /andy/demo" , h.base) ;
1412
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1413
let c = client ( & url, work.path ( ) ) ;
1414
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1415
c.snapshot ( " test" ) .unwrap ( ) ;
1416
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1417
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1418
runs_of ( & h.app, " andy" , " demo" ) .await;
1419
let ( st, body) = claim_as ( credential.clone ( ) ) .await;
1420
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1421
let job: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1422
let ( _ , page) = web ( " GET" , format! ( " /admin/runners/{} " , plex.id) , None ) .await;
1423
assert! (
1424
page.contains ( " andy/demo" ) & & page.contains ( job[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) ) & & page.contains ( " Running now</dt><dd>1 job" ) ,
1425
" {page}"
1426
) ;
b66e70b7 Andy McCall 1 month ago ↶
1427
1428
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
e9b9619c Andy McCall 20 days ago ↶
1429
assert! ( page.contains ( " id=\" queue\" " ) & & page.contains ( " >plex</a></td>" ) , " {page}" ) ;
1430
1431
1432
assert! ( page.contains ( " running 1 of " ) & & page.contains ( " /admin/runners?runner=plex#queue" ) , " {page}" ) ;
1433
let ( _ , only) = web ( " GET" , " /admin/runners?runner=plex" .into ( ) , None ) .await;
1434
assert! ( only.contains ( " >plex</a></td>" ) & & only.contains ( " of " ) & & only.contains ( " >all</a>" ) , " {only}" ) ;
1435
let ( _ , none) = web ( " GET" , " /admin/runners?runner=nobody&status=running" .into ( ) , None ) .await;
1436
assert! ( none.contains ( " Nothing matches." ) , " {none}" ) ;
1437
1438
sqlx:: query( " UPDATE flow_runners SET last_seen_ms = 0 WHERE id = $1" ) .bind ( plex.id) .execute ( & h.app.db) .await.unwrap ( ) ;
1439
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1440
assert! ( page.contains ( " st-offline" ) , " quiet for long: offline" ) ;
1441
assert_eq! ( call ( & h.app, " POST" , " /api/v1/flow/runners/heartbeat" , Some ( & credential) , None ) .await.0 , StatusCode:: NO_CONTENT ) ;
1442
assert_eq! ( call ( & h.app, " POST" , " /api/v1/flow/runners/heartbeat" , Some ( " flowrunner_nope" ) , None ) .await.0 , StatusCode:: UNAUTHORIZED ) ;
1443
let plex_now = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " plex" ) .unwrap ( ) ;
1444
assert_eq! ( plex_now.connectivity ( ) , " online" ) ;
b66e70b7 Andy McCall 1 month ago ↶
1445
let ( _ , tokens) = web ( " GET" , " /settings/tokens" .into ( ) , None ) .await;
1446
let ( _ , made) = web ( " POST" , " /settings/tokens" .into ( ) , Some ( format! ( " _csrf={} &name=admin&scopes=user%3Aread" , csrf_of ( & tokens) ) ) ) .await;
1447
let i = made.find ( " oxyhub_" ) .unwrap ( ) ;
1448
let admin_token: String = made[ i.. ] .chars ( ) .take_while ( | c | c.is_ascii_alphanumeric ( ) | | * c = = ' _' ) .collect ( ) ;
1449
let ( st, body) = call ( & h.app, " GET" , " /api/v1/runners" , Some ( & admin_token) , None ) .await;
1450
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1451
let listed: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1452
let plex_row = listed.as_array ( ) .unwrap ( ) .iter ( ) .find ( | r | r[ " name" ] = = " plex" ) .unwrap ( ) ;
1453
assert_eq! ( ( plex_row[ " running" ] .as_i64 ( ) , plex_row[ " arch" ] .as_str ( ) ) , ( Some ( 1 ) , Some ( " x86_64" ) ) ) ;
7f5f6c05 Andy McCall 1 month ago ↶
1454
1455
let ( _ , page) = web ( " GET" , format! ( " /admin/runners/{} " , plex.id) , None ) .await;
1456
let ( st, _ ) = web (
1457
" POST" ,
1458
format! ( " /admin/runners/{} /limits" , plex.id) ,
1459
Some ( format! ( " _csrf={} &labels=gpu%2C+plex&timeout_minutes=5" , csrf_of ( & page) ) ) ,
1460
)
1461
.await;
1462
assert_eq! ( st, StatusCode:: SEE_OTHER ) ;
1463
let ( _ , page) = web ( " GET" , format! ( " /admin/runners/{} " , plex.id) , None ) .await;
1464
assert! ( page.contains ( " >gpu<" ) & & page.contains ( " 5 min for its jobs" ) , " {page}" ) ;
1465
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/runners" , Some ( & admin_token) , None ) .await;
1466
let listed: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1467
let plex_row = listed.as_array ( ) .unwrap ( ) .iter ( ) .find ( | r | r[ " name" ] = = " plex" ) .unwrap ( ) ;
1468
assert! ( plex_row[ " labels" ] .as_array ( ) .unwrap ( ) .iter ( ) .any ( | l | l = = " gpu" ) , " {plex_row}" ) ;
1469
let ( st, _ ) = web (
1470
" POST" ,
1471
format! ( " /admin/runners/{} /limits" , plex.id) ,
1472
Some ( format! ( " _csrf={} &labels=bad%20label%21&timeout_minutes=" , csrf_of ( & page) ) ) ,
1473
)
1474
.await;
1475
assert_eq! ( st, StatusCode:: BAD_REQUEST , " a label with a space or punctuation is refused" ) ;
b66e70b7 Andy McCall 1 month ago ↶
1476
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/runners/queue" , Some ( & admin_token) , None ) .await;
1477
let queue: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1478
assert! ( queue.as_array ( ) .unwrap ( ) .iter ( ) .any ( | q | q[ " status" ] = = " running" & & q[ " runner" ] = = " plex" ) , " {queue}" ) ;
1479
let ( _ , body) = call ( & h.app, " GET" , & format! ( " /api/v1/runners/{} /jobs" , plex.id) , Some ( & admin_token) , None ) .await;
1480
let jobs: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1481
assert_eq! ( jobs[ 0 ] [ " repo" ] , " andy/demo" , " {jobs}" ) ;
1482
assert_eq! ( call ( & h.app, " GET" , " /api/v1/runners" , None , None ) .await.0 , StatusCode:: NOT_FOUND , " administrators only" ) ;
b6b7e4cf Andy McCall 1 month ago ↶
1483
1484
1485
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1486
let csrf = csrf_of ( & page) ;
1487
web ( " POST" , format! ( " /admin/runners/{} /state" , plex.id) , Some ( format! ( " _csrf={csrf} &state=draining" ) ) ) .await;
1488
assert_eq! ( claim_as ( credential.clone ( ) ) .await.0 , StatusCode:: NO_CONTENT , " a draining runner takes nothing" ) ;
1489
let ( _ , page) = web ( " POST" , format! ( " /admin/runners/{} /remove" , plex.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1490
assert! ( page.contains ( " is running jobs: drain it first" ) , " {page}" ) ;
1491
1492
let id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1493
let done = call (
1494
& h.app,
1495
" POST" ,
1496
& format! ( " /api/v1/flow/jobs/{id} /finish" ) ,
1497
Some ( & credential) ,
1498
Some ( r # "{"exit_code":0,"duration_ms":5}"# .into ( ) ) ,
1499
)
1500
.await;
1501
assert_eq! ( done.0 , StatusCode:: NO_CONTENT ) ;
1502
web ( " POST" , format! ( " /admin/runners/{} /state" , plex.id) , Some ( format! ( " _csrf={csrf} &state=disabled" ) ) ) .await;
1503
assert_eq! ( claim_as ( credential.clone ( ) ) .await.0 , StatusCode:: FORBIDDEN ) ;
1504
let ( _ , page) = web ( " POST" , format! ( " /admin/runners/{} /remove" , plex.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1505
assert! ( page.contains ( " Removed plex." ) , " {page}" ) ;
1506
assert_eq! ( claim_as ( credential) .await.0 , StatusCode:: UNAUTHORIZED ) ;
1507
}
74be9796 Andy McCall 1 month ago ↶
1508
1509
1510
1511
# [ tokio ::test ( flavor = " multi_thread" ) ]
1512
async fn scoped_runners_take_only_their_scope ( ) {
1513
use oxy_core:: identity:: SecretKey;
1514
let h = hub ( ) .await;
1515
create ( & h.app, " andy" , " demo" ) .await;
1516
create ( & h.app, " andy" , " other" ) .await;
1517
let register = | scope : db:: runners:: Scope, name : & 'static str | {
1518
let app = h.app.clone ( ) ;
1519
async move {
1520
let token = format! ( " flowreg_{name} " ) ;
1521
db:: runners:: add_registration( & app.db, & blake3:: hash( token.as_bytes ( ) ) .to_hex ( ) , " admin" , & scope) .await.unwrap ( ) ;
1522
let identity = SecretKey:: generate( ) .identity ( name, " " , 1 ) ;
1523
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
1524
let reg = serde_json:: json! ( { " token" : token, " name" : name, " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1525
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1526
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1527
serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( )
1528
}
1529
} ;
1530
let demo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
1531
let only_demo = register ( db:: runners:: Scope { owner: " andy" .into ( ) , repo_id: Some ( demo.id) } , " demo-runner" ) .await;
1532
let carols = register ( db:: runners:: Scope { owner: " carol" .into ( ) , repo_id: None } , " carol-runner" ) .await;
1533
let andys = register ( db:: runners:: Scope { owner: " andy" .into ( ) , repo_id: None } , " andy-runner" ) .await;
1534
let claim_as = | who : String| {
1535
let app = h.app.clone ( ) ;
1536
async move {
1537
let body = serde_json:: json! ( { " runner" : " x" , " runtimes" : [ " microvm" ] } ) .to_string ( ) ;
1538
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( & who) , Some ( body) ) .await;
1539
( st = = StatusCode:: OK ) .then ( | | serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " repo" ] .as_str ( ) .unwrap ( ) .to_string ( ) )
1540
}
1541
} ;
1542
1543
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1544
let c = client ( & format! ( " {} /andy/other" , h.base) , work.path ( ) ) ;
1545
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1546
c.snapshot ( " t" ) .unwrap ( ) ;
1547
c.commit ( " pipeline" , & [ ] ) .unwrap ( ) ;
1548
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1549
assert! ( ! runs_of ( & h.app, " andy" , " other" ) .await.is_empty ( ) ) ;
1550
assert_eq! ( claim_as ( only_demo.clone ( ) ) .await, None , " nothing of andy/demo is queued" ) ;
1551
assert_eq! ( claim_as ( carols) .await, None , " nothing of carol's" ) ;
1552
assert_eq! ( claim_as ( andys) .await.as_deref ( ) , Some ( " andy/other" ) , " the owner's runner takes its repositories' jobs" ) ;
1553
1554
assert_eq! ( claim ( & h.app) .await.unwrap ( ) [ " repo" ] , " andy/other" ) ;
1555
assert_eq! ( claim_as ( only_demo) .await, None , " still nothing of its own" ) ;
1556
}
7974adb0 Andy McCall 1 month ago ↶
1557
5714347a Andy McCall 11 days ago ↶
1558
1559
1560
1561
# [ tokio ::test ( flavor = " multi_thread" ) ]
1562
async fn a_user_registers_a_runner_for_their_own_repositories ( ) {
1563
use oxy_core:: identity:: SecretKey;
1564
let h = hub ( ) .await;
1565
create ( & h.app, " andy" , " demo" ) .await;
1566
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " " , " salt" , false ) .await.unwrap ( ) ;
1567
let pat = " oxyhub_phone_test_token" ;
1568
db:: accounts:: create_token( & h.app.db, andy.id, " phone" , & oxygenhub:: auth:: token_hash( pat) , " repo:read,repo:write" , " " , None )
1569
.await
1570
.unwrap ( ) ;
1571
let read_only = " oxyhub_read_only_token" ;
1572
db:: accounts:: create_token( & h.app.db, andy.id, " ro" , & oxygenhub:: auth:: token_hash( read_only) , " repo:read" , " " , None ) .await.unwrap ( ) ;
1573
assert_eq! ( call ( & h.app, " POST" , " /api/v1/user/runner-token" , Some ( read_only) , None ) .await.0 , StatusCode:: FORBIDDEN ) ;
1574
assert_eq! ( call ( & h.app, " POST" , " /api/v1/user/runner-token" , None , None ) .await.0 , StatusCode:: UNAUTHORIZED ) ;
1575
let ( st, body) = call ( & h.app, " POST" , " /api/v1/user/runner-token" , Some ( pat) , None ) .await;
1576
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
1577
let got: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1578
let token = got[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1579
assert! ( token.starts_with ( " flowreg_" ) & & got[ " owner" ] = = " andy" , " {body}" ) ;
1580
let identity = SecretKey:: generate( ) .identity ( " andy's phone" , " " , 1 ) ;
1581
let info = serde_json:: json! ( { " arch" : " aarch64" , " os" : " android" , " version" : " 0.3.1" , " labels" : [ " android" ] , " max_jobs" : 1 } ) ;
1582
let reg = serde_json:: json! ( { " token" : token, " name" : " pixel" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1583
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1584
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1585
let phone = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " pixel" ) .unwrap ( ) ;
1586
assert_eq! ( ( phone.scope_owner.as_str ( ) , phone.scope_repo_id) , ( " andy" , None ) , " andy's repositories, all of them, and no one else's" ) ;
1587
}
1588
7974adb0 Andy McCall 1 month ago ↶
1589
1590
1591
1592
# [ tokio ::test ( flavor = " multi_thread" ) ]
1593
async fn repository_admins_add_and_remove_their_own_runners ( ) {
1594
use oxy_core:: identity:: SecretKey;
1595
let h = hub ( ) .await;
1596
let res = oxygenhub:: router( h.app.clone ( ) )
1597
.oneshot (
1598
Request:: post( " /register" )
1599
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 20 days ago
1600
.body ( Body:: from( " username=andy&email=andy%40example.com&password=correct-horse-battery&setup=test-setup" ) )
7974adb0 Andy McCall 1 month ago ↶
1601
.unwrap ( ) ,
1602
)
1603
.await
1604
.unwrap ( ) ;
1605
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1606
create ( & h.app, " andy" , " demo" ) .await;
1607
let demo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
1608
let web = | method : & 'static str , path : String, body : Option< String> | {
1609
let ( app, cookie) = ( h.app.clone ( ) , cookie.clone ( ) ) ;
1610
async move {
1611
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie) ;
1612
if body.is_some ( ) {
1613
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
1614
}
1615
let res = oxygenhub:: router( app) .oneshot ( req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ) .await.unwrap ( ) ;
1616
let st = res.status ( ) ;
1617
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
1618
}
1619
} ;
1620
let csrf_of = | page : & str | {
1621
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
1622
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
1623
} ;
c7067c7b Andy McCall 16 days ago
1624
let ( st, page) = web ( " GET" , " /andy/demo/settings/runners" .into ( ) , None ) .await;
7974adb0 Andy McCall 1 month ago ↶
1625
assert_eq! ( st, StatusCode:: OK ) ;
c7067c7b Andy McCall 16 days ago
1626
assert! ( page.contains ( " <h2>Runners</h2>" ) , " the owner sees the page" ) ;
7974adb0 Andy McCall 1 month ago ↶
1627
let ( _ , page) = web ( " POST" , " /andy/demo/settings/runners/token" .into ( ) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1628
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
1629
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
1630
let identity = SecretKey:: generate( ) .identity ( " mine" , " " , 1 ) ;
1631
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
1632
let reg = serde_json:: json! ( { " token" : token, " name" : " mine" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1633
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1634
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1635
let mine = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " mine" ) .unwrap ( ) ;
1636
assert_eq! ( mine.scope ( ) , db:: runners:: Scope { owner: " andy" .into ( ) , repo_id: Some ( demo.id) } ) ;
1637
1638
claim ( & h.app) .await;
1639
let site = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " test-1" ) .unwrap ( ) ;
c7067c7b Andy McCall 16 days ago
1640
let ( _ , page) = web ( " GET" , " /andy/demo/settings/runners" .into ( ) , None ) .await;
7974adb0 Andy McCall 1 month ago ↶
1641
assert! ( page.contains ( " <strong>mine</strong>" ) & & page.contains ( " this repository" ) , " {page}" ) ;
1642
assert! ( page.contains ( " <strong>test-1</strong>" ) & & page.contains ( " the whole site" ) , " {page}" ) ;
1643
let csrf = csrf_of ( & page) ;
1644
let ( st, _ ) = web ( " POST" , format! ( " /andy/demo/settings/runners/{} /remove" , site.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1645
assert_eq! ( st, StatusCode:: NOT_FOUND , " not the repository's own" ) ;
c7067c7b Andy McCall 16 days ago
1646
let ( st, _ ) = web ( " POST" , format! ( " /andy/demo/settings/runners/{} /remove" , mine.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1647
assert_eq! ( st, StatusCode:: SEE_OTHER , " back to the Runners page" ) ;
1648
let ( _ , page) = web ( " GET" , " /andy/demo/settings/runners?notice=x" .into ( ) , None ) .await;
1649
assert! ( ! page.contains ( " <strong>mine</strong>" ) , " {page}" ) ;
7974adb0 Andy McCall 1 month ago ↶
1650
assert! ( db:: runners:: get( & h.app.db, mine.id) .await.unwrap ( ) .is_none ( ) ) ;
1651
1652
1653
let ( _ , page) = web ( " GET" , " /orgs/new" .into ( ) , None ) .await;
1654
web ( " POST" , " /orgs/new" .into ( ) , Some ( format! ( " _csrf={} &name=acme&display_name=Acme" , csrf_of ( & page) ) ) ) .await;
1655
let ( st, page) = web ( " GET" , " /orgs/acme/settings" .into ( ) , None ) .await;
1656
assert_eq! ( st, StatusCode:: OK ) ;
1657
let ( _ , page) = web ( " POST" , " /orgs/acme/settings/runners/token" .into ( ) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1658
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
1659
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
1660
let identity = SecretKey:: generate( ) .identity ( " acme-runner" , " " , 1 ) ;
1661
let reg = serde_json:: json! ( { " token" : token, " name" : " acme-runner" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1662
assert_eq! ( call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await.0 , StatusCode:: OK ) ;
1663
let acme = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " acme-runner" ) .unwrap ( ) ;
1664
assert_eq! ( acme.scope ( ) , db:: runners:: Scope { owner: " acme" .into ( ) , repo_id: None } ) ;
1665
let ( _ , page) = web ( " GET" , " /orgs/acme/settings" .into ( ) , None ) .await;
1666
assert! ( page.contains ( " <strong>acme-runner</strong>" ) & & page.contains ( " the organisation's repositories" ) , " {page}" ) ;
1667
let ( _ , page) = web ( " POST" , format! ( " /orgs/acme/settings/runners/{} /remove" , acme.id) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1668
assert! ( page.contains ( " acme-runner removed" ) , " {page}" ) ;
1669
}
c4c87f48 Andy McCall 1 month ago ↶
1670
1671
1672
1673
# [ tokio ::test ( flavor = " multi_thread" ) ]
1674
async fn jobs_that_ask_for_labels_wait_for_a_runner_with_them ( ) {
1675
use oxy_core:: identity:: SecretKey;
1676
let h = hub ( ) .await;
1677
create ( & h.app, " andy" , " demo" ) .await;
1678
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1679
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
1680
let cross = " job \" cross\" {\n sandbox \" alpine\" \n runs-on \" riscv64\" \n exec \" true\" \n }\n " ;
1681
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( cross) ) ;
1682
c.snapshot ( " t" ) .unwrap ( ) ;
1683
c.commit ( " pipeline" , & [ ] ) .unwrap ( ) ;
1684
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1685
assert! ( ! runs_of ( & h.app, " andy" , " demo" ) .await.is_empty ( ) ) ;
1686
1687
let mut taken = Vec :: new( ) ;
1688
while let Some ( j) = claim ( & h.app) .await {
1689
taken.push ( j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
1690
}
1691
assert! ( ! taken.contains ( & " cross" .to_string ( ) ) & & taken.contains ( & " lint" .to_string ( ) ) , " {taken:?}" ) ;
1692
let queue = db:: flow:: queue( & h.app.db) .await.unwrap ( ) ;
1693
let waiting = queue.iter ( ) .find ( | q | q.job = = " cross" ) .unwrap ( ) ;
1694
assert_eq! ( ( waiting.status.as_str ( ) , waiting.needs ( ) ) , ( " queued" , vec! [ " riscv64" .to_string ( ) ] ) ) ;
1695
1696
let token = " flowreg_riscv" ;
1697
db:: runners:: add_registration( & h.app.db, & blake3:: hash( token.as_bytes ( ) ) .to_hex ( ) , " admin" , & Default :: default( ) ) .await.unwrap ( ) ;
1698
let identity = SecretKey:: generate( ) .identity ( " visionfive" , " " , 1 ) ;
1699
let info = serde_json:: json! ( { " arch" : " riscv64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ " riscv64" ] , " max_jobs" : 1 } ) ;
1700
let reg = serde_json:: json! ( { " token" : token, " name" : " visionfive" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1701
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1702
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1703
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1704
let claim_body = serde_json:: json! ( { " runner" : " x" , " runtimes" : [ " microvm" ] , " info" : info } ) .to_string ( ) ;
1705
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( & credential) , Some ( claim_body) ) .await;
1706
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1707
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job" ] [ " name" ] , " cross" ) ;
1708
}
070a7b64 Andy McCall 1 month ago ↶
1709
1710
1711
1712
1713
1714
# [ tokio ::test ( flavor = " multi_thread" ) ]
1715
async fn a_job_no_runner_reports_is_ended_after_the_time_limit ( ) {
1716
let h = hub ( ) .await;
1717
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1718
create ( & h.app, " andy" , " lost" ) .await;
1719
let url = format! ( " {} /andy/lost" , h.base) ;
1720
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1721
let c = client ( & url, work.path ( ) ) ;
1722
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1723
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1724
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1725
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1726
assert_eq! ( runs_of ( & h.app, " andy" , " lost" ) .await.len ( ) , 1 ) ;
1727
let job = claim ( & h.app) .await.expect ( " a job to claim" ) ;
1728
let id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1729
1730
1731
assert_eq! ( oxygenhub:: flow:: reap_lost_jobs( & h.app) .await.unwrap ( ) , 0 ) ;
1732
1733
let old = db:: now_ms( ) - 24 * 3600 * 1000 ;
3b1bc27f Andy McCall 20 days ago
1734
sqlx:: query( " UPDATE flow_jobs SET started_ms = $1 WHERE id = $2" ) .bind ( old) .bind ( id) .execute ( & h.app.db) .await.unwrap ( ) ;
070a7b64 Andy McCall 1 month ago ↶
1735
assert_eq! ( oxygenhub:: flow:: reap_lost_jobs( & h.app) .await.unwrap ( ) , 1 ) ;
1736
let j = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
1737
assert_eq! ( j.status, " error" ) ;
1738
assert! ( j.error.starts_with ( " lost:" ) , " {}" , j.error) ;
1739
let run = & runs_of ( & h.app, " andy" , " lost" ) .await[ 0 ] ;
1740
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
1741
assert! ( jobs.iter ( ) .all ( | j | j.status ! = " running" ) , " nothing left running" ) ;
1742
1743
assert_ne! ( finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 } ) ) .await, StatusCode:: NO_CONTENT ) ;
1744
assert_eq! ( db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .status, " error" ) ;
1745
}
b0612e7d Andy McCall 1 month ago ↶
1746
1747
1748
1749
# [ tokio ::test ( flavor = " multi_thread" ) ]
1750
async fn a_runner_is_offered_only_jobs_whose_image_it_holds ( ) {
1751
let h = hub ( ) .await;
1752
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1753
create ( & h.app, " andy" , " images" ) .await;
1754
let url = format! ( " {} /andy/images" , h.base) ;
1755
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1756
let c = client ( & url, work.path ( ) ) ;
1757
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1758
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1759
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1760
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1761
assert_eq! ( runs_of ( & h.app, " andy" , " images" ) .await.len ( ) , 1 ) ;
1762
let claim_with = | images : Vec< String> | {
1763
let body = serde_json:: json! ( { " runner" : " test-1" , " runtimes" : [ " microvm" ] , " images" : images } ) .to_string ( ) ;
1764
let app = h.app.clone ( ) ;
1765
async move {
1766
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body) ) .await;
1767
( st, body)
1768
}
1769
} ;
1770
let ( st, _ ) = claim_with ( vec! [ " ab" .repeat ( 32 ) ] ) .await;
1771
assert_eq! ( st, StatusCode:: NO_CONTENT , " no job for a runner without the image" ) ;
1772
let jobs = db:: flow:: jobs( & h.app.db, runs_of ( & h.app, " andy" , " images" ) .await[ 0 ] .id) .await.unwrap ( ) ;
1773
let rootfs: serde_json:: Value = serde_json:: from_str( & jobs[ 0 ] .sandbox) .unwrap ( ) ;
1774
let rootfs = rootfs[ " rootfs" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1775
let ( st, body) = claim_with ( vec! [ " ab" .repeat ( 32 ) , rootfs] ) .await;
1776
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1777
}
eef677c4 Andy McCall 1 month ago ↶
1778
1779
1780
1781
1782
# [ tokio ::test ( flavor = " multi_thread" ) ]
1783
async fn a_job_its_runner_no_longer_holds_is_ended_at_its_next_claim ( ) {
1784
let h = hub ( ) .await;
1785
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1786
create ( & h.app, " andy" , " held" ) .await;
1787
let url = format! ( " {} /andy/held" , h.base) ;
1788
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1789
let c = client ( & url, work.path ( ) ) ;
1790
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1791
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1792
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1793
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1794
assert_eq! ( runs_of ( & h.app, " andy" , " held" ) .await.len ( ) , 1 ) ;
1795
let id = claim ( & h.app) .await.expect ( " a job to claim" ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1796
let claim_holding = | holding : Vec< i64 > | {
1797
let body = serde_json:: json! ( { " runner" : " test-1" , " runtimes" : [ " microvm" ] , " holding" : holding } ) .to_string ( ) ;
1798
let app = h.app.clone ( ) ;
1799
async move { call ( & app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body) ) .await.0 }
1800
} ;
1801
claim_holding ( vec! [ id] ) .await;
1802
assert_eq! ( db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .status, " running" , " a held job stays" ) ;
1803
claim_holding ( vec! [ ] ) .await;
1804
let j = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
1805
assert_eq! ( j.status, " error" ) ;
1806
assert! ( j.error.starts_with ( " lost:" ) & & j.error.contains ( " no longer holds" ) , " {}" , j.error) ;
1807
let run = & runs_of ( & h.app, " andy" , " held" ) .await[ 0 ] ;
1808
assert! ( db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) .iter ( ) .all ( | j | j.status ! = " running" ) , " the run settles" ) ;
1809
}
d7ce41ac Andy McCall 1 month ago ↶
1810
1811
1812
1813
1814
# [ tokio ::test ( flavor = " multi_thread" ) ]
1815
async fn a_scoped_job_runs_only_when_a_push_touches_its_paths ( ) {
1816
let h = hub ( ) .await;
1817
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1818
create ( & h.app, " andy" , " scoped" ) .await;
1819
let url = format! ( " {} /andy/scoped" , h.base) ;
1820
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1821
let c = client ( & url, work.path ( ) ) ;
1822
let extra = " job \" dist\" {\n sandbox \" alpine\" \n scope \" Cargo.toml\" \" packaging/**\" \n exec \" true\" \n }\n " ;
1823
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
1824
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1825
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.0\" \n " ) ;
1826
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
1827
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1828
let names = | app : Shared, n : usize | async move {
1829
let mut runs = Vec :: new( ) ;
1830
for _ in 0 .. 100 {
1831
runs = runs_of ( & app, " andy" , " scoped" ) .await;
1832
if runs.len ( ) > = n {
1833
break ;
1834
}
1835
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
1836
}
1837
assert_eq! ( runs.len ( ) , n, " runs" ) ;
1838
let mut j: Vec< String> = db:: flow:: jobs( & app.db, runs[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
1839
j.sort ( ) ;
745d225d Andy McCall 1 month ago ↶
1840
3b1bc27f Andy McCall 20 days ago
1841
sqlx:: query( " UPDATE flow_runs SET status = 'passed' WHERE id = $1" ) .bind ( runs[ 0 ] .id) .execute ( & app.db) .await.unwrap ( ) ;
d7ce41ac Andy McCall 1 month ago ↶
1842
j
1843
} ;
1844
assert_eq! ( names ( h.app.clone ( ) , 1 ) .await, [ " build" , " dist" , " lint" , " test" ] , " the first run has every job" ) ;
1845
write ( work.path ( ) , " test.sh" , " echo still ok\n " ) ;
1846
c.commit ( " Edit the test" , & [ ] ) .unwrap ( ) ;
1847
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1848
assert_eq! ( names ( h.app.clone ( ) , 2 ) .await, [ " build" , " lint" , " test" ] , " dist is out of scope" ) ;
1849
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.1\" \n " ) ;
1850
c.commit ( " Release 0.1.1" , & [ ] ) .unwrap ( ) ;
1851
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1852
assert_eq! ( names ( h.app.clone ( ) , 3 ) .await, [ " build" , " dist" , " lint" , " test" ] , " a version bump runs dist" ) ;
1853
}
5dead1f4 Andy McCall 1 month ago ↶
1854
367f34b8 Andy McCall 16 days ago ↶
1855
1856
1857
1858
# [ tokio ::test ( flavor = " multi_thread" ) ]
1859
async fn a_push_can_skip_its_run ( ) {
1860
let h = hub ( ) .await;
1861
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1862
create ( & h.app, " andy" , " skip" ) .await;
1863
let url = format! ( " {} /andy/skip" , h.base) ;
1864
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1865
let c = client ( & url, work.path ( ) ) ;
1866
let extra = " job \" dist\" {\n sandbox \" alpine\" \n scope \" Cargo.toml\" \n exec \" true\" \n }\n " ;
1867
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
1868
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1869
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.0\" \n " ) ;
1870
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
1871
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1872
let newest = | app : Shared, n : usize | async move {
1873
for _ in 0 .. 100 {
1874
let runs = runs_of ( & app, " andy" , " skip" ) .await;
1875
if runs.len ( ) > = n {
1876
assert_eq! ( runs.len ( ) , n, " runs" ) ;
1877
let r = runs[ 0 ] .clone ( ) ;
1878
let mut jobs: Vec< String> = db:: flow:: jobs( & app.db, r.id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
1879
jobs.sort ( ) ;
1880
if r.status ! = " skipped" {
1881
sqlx:: query( " UPDATE flow_runs SET status = 'passed' WHERE id = $1" ) .bind ( r.id) .execute ( & app.db) .await.unwrap ( ) ;
1882
}
1883
return ( r, jobs) ;
1884
}
1885
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
1886
}
1887
panic! ( " no run {n}" ) ;
1888
} ;
1889
assert_eq! ( newest ( h.app.clone ( ) , 1 ) .await.1 , [ " build" , " dist" , " lint" , " test" ] ) ;
1890
1891
1892
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.1\" \n " ) ;
1893
c.commit ( " Bump the version [Skip CI]" , & [ ] ) .unwrap ( ) ;
1894
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1895
let ( run, jobs) = newest ( h.app.clone ( ) , 2 ) .await;
1896
assert_eq! ( run.status, " skipped" ) ;
1897
assert! ( jobs.is_empty ( ) , " {jobs:?}" ) ;
1898
assert! ( run.error.starts_with ( " [skip ci] in change " ) & & run.error.contains ( " , pushed by " ) , " {}" , run.error) ;
1899
assert! ( run.finished_ms.is_some ( ) ) ;
1900
let ( _ , page) = call ( & h.app, " GET" , " /andy/skip/flow/2" , None , None ) .await;
1901
assert! ( page.contains ( " the push asked to skip this run" ) , " {page}" ) ;
1902
1903
1904
1905
write ( work.path ( ) , " test.sh" , " echo still ok\n " ) ;
1906
c.commit ( " Edit the test" , & [ ] ) .unwrap ( ) ;
1907
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1908
assert_eq! ( newest ( h.app.clone ( ) , 3 ) .await.1 , [ " build" , " dist" , " lint" , " test" ] , " the skipped bump still runs dist" ) ;
1909
1910
1911
let row = db:: repos:: get( & h.app.db, " andy" , " skip" ) .await.unwrap ( ) .unwrap ( ) ;
1912
db:: repos:: set_flow_skip( & h.app.db, row.id, " others" ) .await.unwrap ( ) ;
1913
write ( work.path ( ) , " test.sh" , " echo ok again\n " ) ;
1914
c.commit ( " Docs only [skip flow]" , & [ ] ) .unwrap ( ) ;
1915
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1916
let ( run, jobs) = newest ( h.app.clone ( ) , 4 ) .await;
1917
assert_ne! ( run.status, " skipped" ) ;
1918
assert_eq! ( jobs, [ " build" , " lint" , " test" ] ) ;
1919
let row = db:: repos:: get( & h.app.db, " andy" , " skip" ) .await.unwrap ( ) .unwrap ( ) ;
1920
assert! ( oxygenhub:: flow:: skip_allowed( & row, " feature" ) & & ! oxygenhub:: flow:: skip_allowed( & row, " main" ) ) ;
1921
db:: repos:: set_flow_skip( & h.app.db, row.id, " never" ) .await.unwrap ( ) ;
1922
let row = db:: repos:: get( & h.app.db, " andy" , " skip" ) .await.unwrap ( ) .unwrap ( ) ;
1923
assert! ( ! oxygenhub:: flow:: skip_allowed( & row, " feature" ) ) ;
1924
}
1925
5588bb36 Andy McCall 16 days ago ↶
1926
1927
1928
1929
# [ tokio ::test ( flavor = " multi_thread" ) ]
1930
async fn a_push_reuses_a_pass_on_the_same_state ( ) {
1931
let h = hub ( ) .await;
1932
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1933
create ( & h.app, " andy" , " reuse" ) .await;
1934
let url = format! ( " {} /andy/reuse" , h.base) ;
1935
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1936
let c = client ( & url, work.path ( ) ) ;
1937
let gated = " job \" deploy\" {\n sandbox \" alpine\" \n depends-on \" test\" \n approval {\n by \" maintain\" \n }\n exec \" true\" \n }\n " ;
1938
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( gated) ) ;
1939
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1940
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
1941
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1942
let first = runs_of ( & h.app, " andy" , " reuse" ) .await.remove ( 0 ) ;
1943
1944
sqlx:: query( " UPDATE flow_jobs SET status = 'passed', exit_code = 0, receipt_hash = 'r-' || name WHERE run_id = $1" )
1945
.bind ( first.id)
1946
.execute ( & h.app.db)
1947
.await
1948
.unwrap ( ) ;
1949
sqlx:: query( " UPDATE flow_runs SET status = 'passed' WHERE id = $1" ) .bind ( first.id) .execute ( & h.app.db) .await.unwrap ( ) ;
1950
let heads: Vec< oxy_core:: hash:: ChangeHash> = first.head_list ( ) .into_iter ( ) .filter_map ( oxy_core:: hash:: ChangeHash:: from_hex) .collect ( ) ;
1951
let row = db:: repos:: get( & h.app.db, " andy" , " reuse" ) .await.unwrap ( ) .unwrap ( ) ;
1952
assert! ( row.flow_reuse, " on by default" ) ;
1953
let again = | row : db:: repos:: RepoRow| {
1954
let ( app, state, heads) = ( h.app.clone ( ) , first.state.clone ( ) , heads.clone ( ) ) ;
1955
async move {
1956
let n =
1957
oxygenhub:: flow:: start_run_scoped( & app, & row, " main" , & state, & heads, " andy" , " push" , None ) .await.unwrap ( ) .expect ( " a run" ) ;
1958
let run = db:: flow:: run( & app.db, row.id, n) .await.unwrap ( ) .unwrap ( ) ;
1959
let jobs = db:: flow:: jobs( & app.db, run.id) .await.unwrap ( ) ;
1960
( run, jobs)
1961
}
1962
} ;
1963
let ( run, jobs) = again ( row.clone ( ) ) .await;
1964
let by = | name : & str | jobs.iter ( ) .find ( | j | j.name = = name ) .unwrap ( ) .clone ( ) ;
1965
for name in [ " build" , " test" , " lint" ] {
1966
let j = by ( name) ;
1967
assert_eq! ( ( j.status.as_str ( ) , j.reused_from ( ) ) , ( " passed" , Some ( first.number) ) , " {name}: {j:?}" ) ;
1968
assert_eq! ( j.receipt_hash, format! ( " r-{name} " ) , " the pass's receipt stands for it" ) ;
1969
}
1970
let deploy = by ( " deploy" ) ;
1971
assert! ( deploy.status = = " queued" & & deploy.reused_from ( ) .is_none ( ) , " a gated job runs again: {deploy:?}" ) ;
1972
assert_eq! ( run.status, " queued" , " the run waits for the gated job" ) ;
1973
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/reuse/flow/{} " , run.number) , None , None ) .await;
1974
assert! ( page.contains ( " data-reused" ) & & page.contains ( & format! ( " /andy/reuse/flow/{} " , first.number) ) , " {page}" ) ;
1975
1976
let checks = db:: hooks:: checks_for_state( & h.app.db, row.id, & first.state) .await.unwrap ( ) ;
1977
assert! (
1978
checks.iter ( ) .any ( | c | c.name = = " flow/build" & & c.status = = " passed" & & c.summary.contains ( " reused" ) ) ,
1979
" a reused pass reports its check: {checks:?}"
1980
) ;
1981
1982
1983
db:: repos:: set_flow_reuse( & h.app.db, row.id, false ) .await.unwrap ( ) ;
1984
let row = db:: repos:: get( & h.app.db, " andy" , " reuse" ) .await.unwrap ( ) .unwrap ( ) ;
1985
let ( _ , jobs) = again ( row) .await;
1986
assert! ( jobs.iter ( ) .all ( | j | j.status = = " queued" & & j.reused_from ( ) .is_none ( ) ) , " {jobs:?}" ) ;
1987
}
1988
367f34b8 Andy McCall 16 days ago ↶
1989
# [ test ]
1990
fn skip_markers_are_the_forges_spellings_in_any_case ( ) {
1991
use oxygenhub:: flow:: skip_marker;
1992
assert_eq! ( skip_marker ( " Fix typo [skip flow]" ) , Some ( " [skip flow]" ) ) ;
1993
assert_eq! ( skip_marker ( " [CI SKIP] docs" ) , Some ( " [ci skip]" ) ) ;
1994
assert_eq! ( skip_marker ( " Docs\n \n [Skip CI]" ) , Some ( " [skip ci]" ) ) ;
1995
assert_eq! ( skip_marker ( " skip ci without brackets" ) , None ) ;
3b3a51af Andy McCall 16 days ago ↶
1996
1997
assert_eq! ( skip_marker ( " Document the `[skip flow]` marker" ) , None ) ;
1998
assert_eq! ( skip_marker ( " Example:\n \n ```\n git commit -m 'x [skip ci]'\n ```\n " ) , None ) ;
1999
assert_eq! ( skip_marker ( " `code` then [skip ci]" ) , Some ( " [skip ci]" ) ) ;
2000
assert_eq! ( skip_marker ( " an odd ` backtick [skip ci]" ) , Some ( " [skip ci]" ) ) ;
367f34b8 Andy McCall 16 days ago ↶
2001
}
2002
745d225d Andy McCall 1 month ago ↶
2003
2004
2005
2006
# [ tokio ::test ( flavor = " multi_thread" ) ]
2007
async fn runs_are_cancelled_by_hand_or_by_a_newer_push ( ) {
2008
let h = hub ( ) .await;
2009
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2010
create ( & h.app, " andy" , " stop" ) .await;
2011
let url = format! ( " {} /andy/stop" , h.base) ;
2012
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2013
let c = client ( & url, work.path ( ) ) ;
2014
let extra = " job \" dist\" {\n sandbox \" alpine\" \n scope \" Cargo.toml\" \n exec \" true\" \n }\n " ;
2015
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
2016
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2017
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.0\" \n " ) ;
2018
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
2019
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2020
let wait_runs = | app : Shared, n : usize | async move {
2021
for _ in 0 .. 100 {
2022
let runs = runs_of ( & app, " andy" , " stop" ) .await;
2023
if runs.len ( ) > = n {
2024
return runs;
2025
}
2026
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2027
}
2028
panic! ( " no run {n}" ) ;
2029
} ;
2030
let first = wait_runs ( h.app.clone ( ) , 1 ) .await;
3b1bc27f Andy McCall 20 days ago
2031
sqlx:: query( " UPDATE flow_runs SET status = 'passed' WHERE id = $1" ) .bind ( first[ 0 ] .id) .execute ( & h.app.db) .await.unwrap ( ) ;
2032
sqlx:: query( " UPDATE flow_jobs SET status = 'passed' WHERE run_id = $1" ) .bind ( first[ 0 ] .id) .execute ( & h.app.db) .await.unwrap ( ) ;
745d225d Andy McCall 1 month ago ↶
2033
2034
2035
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.1\" \n " ) ;
2036
c.commit ( " Release 0.1.1" , & [ ] ) .unwrap ( ) ;
2037
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2038
wait_runs ( h.app.clone ( ) , 2 ) .await;
2039
let job = claim ( & h.app) .await.expect ( " a job of run 2" ) ;
2040
let id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2041
write ( work.path ( ) , " test.sh" , " echo still ok\n " ) ;
2042
c.commit ( " Edit the test" , & [ ] ) .unwrap ( ) ;
2043
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2044
let runs = wait_runs ( h.app.clone ( ) , 3 ) .await;
2045
let third = & runs[ 0 ] ;
2046
let mut second = runs[ 1 ] .clone ( ) ;
2047
for _ in 0 .. 100 {
2048
second = db:: flow:: run_by_id( & h.app.db, runs[ 1 ] .id) .await.unwrap ( ) .unwrap ( ) ;
2049
if ! second.live ( ) {
2050
break ;
2051
}
2052
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2053
}
2054
assert_eq! ( second.status, " cancelled" , " a newer push supersedes a run still going" ) ;
2055
let jobs = db:: flow:: jobs( & h.app.db, second.id) .await.unwrap ( ) ;
2056
assert! ( jobs.iter ( ) .all ( | j | j.status = = " cancelled" & & j.error = = " superseded by run #3" ) , " {jobs:?}" ) ;
2057
let mut names: Vec< String> = db:: flow:: jobs( & h.app.db, third.id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
2058
names.sort ( ) ;
2059
assert_eq! ( names, [ " build" , " dist" , " lint" , " test" ] , " run 3 also checks what run 2 would have: the version bump" ) ;
2060
let mut checks = vec! [ ] ;
2061
for _ in 0 .. 100 {
2062
checks = db:: hooks:: checks_for_state( & h.app.db, second.repo_id, & second.state) .await.unwrap ( ) ;
2063
if checks.iter ( ) .all ( | c | c.status = = " cancelled" ) {
2064
break ;
2065
}
2066
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2067
}
2068
assert! ( checks.iter ( ) .all ( | c | c.status = = " cancelled" ) , " {checks:?}" ) ;
2069
2070
let ( st, _ ) =
2071
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( r # "{"records":[]}"# .into ( ) ) ) .await;
2072
assert_eq! ( st, StatusCode:: CONFLICT ) ;
2073
2074
2075
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/stop/flow/runs/3/cancel" , Some ( " site" ) , None ) .await;
2076
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
2077
let third = db:: flow:: run_by_id( & h.app.db, third.id) .await.unwrap ( ) .unwrap ( ) ;
2078
assert_eq! ( third.status, " cancelled" ) ;
2079
assert! ( db:: flow:: jobs( & h.app.db, third.id) .await.unwrap ( ) .iter ( ) .all ( | j | j.error = = " cancelled by andy" ) ) ;
2080
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/stop/flow/runs/3/cancel" , Some ( " site" ) , None ) .await;
2081
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " already cancelled" ) , " {st} {body}" ) ;
2082
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/stop/flow/runs/3/cancel" , None , None ) .await;
2083
assert_ne! ( st, StatusCode:: OK , " anonymous callers cannot cancel" ) ;
2084
let ( _ , page) = call ( & h.app, " GET" , " /andy/stop/flow/3" , None , None ) .await;
2085
assert! ( page.contains ( " ⊘" ) & & ! page.contains ( " CANCEL</button>" ) , " a cancelled run shows so, with no button" ) ;
2086
}
2087
5dead1f4 Andy McCall 1 month ago ↶
2088
2089
2090
2091
# [ tokio ::test ( flavor = " multi_thread" ) ]
2092
async fn a_job_runs_under_its_own_time_limit_within_the_sites ( ) {
2093
let h = hub ( ) .await;
2094
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2095
db:: accounts:: set_setting( & h.app.db, oxygenhub:: flow:: SETTING_DEFAULT , " 30" ) .await.unwrap ( ) ;
2096
db:: accounts:: set_setting( & h.app.db, oxygenhub:: flow:: SETTING_MAX , " 120" ) .await.unwrap ( ) ;
2097
create ( & h.app, " andy" , " limits" ) .await;
2098
let url = format! ( " {} /andy/limits" , h.base) ;
2099
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2100
let c = client ( & url, work.path ( ) ) ;
2101
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 " ;
2102
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
2103
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2104
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2105
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2106
assert_eq! ( runs_of ( & h.app, " andy" , " limits" ) .await.len ( ) , 1 ) ;
2107
let mut limits = std:: collections:: HashMap:: new( ) ;
2108
let mut ids = std:: collections:: HashMap:: new( ) ;
2109
while let Some ( j) = claim ( & h.app) .await {
2110
let name = j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
2111
limits.insert ( name.clone ( ) , j[ " timeout_s" ] .as_u64 ( ) .unwrap ( ) ) ;
2112
ids.insert ( name, j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ) ;
2113
}
2114
assert_eq! ( limits[ " slow" ] , 5400 , " its own" ) ;
2115
assert_eq! ( limits[ " huge" ] , 7200 , " cut to the site's maximum" ) ;
2116
assert_eq! ( limits[ " build" ] , 1800 , " the site's default" ) ;
2117
2118
let started = db:: now_ms( ) - 40 * 60 * 1000 ;
3b1bc27f Andy McCall 20 days ago
2119
sqlx:: query( " UPDATE flow_jobs SET started_ms = $1" ) .bind ( started) .execute ( & h.app.db) .await.unwrap ( ) ;
5dead1f4 Andy McCall 1 month ago ↶
2120
oxygenhub:: flow:: reap_lost_jobs( & h.app) .await.unwrap ( ) ;
2121
let status = | id : i64 | {
2122
let app = h.app.clone ( ) ;
2123
async move { db:: flow:: job( & app.db, id) .await.unwrap ( ) .unwrap ( ) .status }
2124
} ;
2125
assert_eq! ( status ( ids[ " build" ] ) .await, " error" , " past its 30 minutes" ) ;
2126
assert_eq! ( status ( ids[ " slow" ] ) .await, " running" , " within its 90" ) ;
2127
assert_eq! ( status ( ids[ " huge" ] ) .await, " running" , " within its 120" ) ;
2128
}
c889911b Andy McCall 1 month ago ↶
2129
2130
2131
2132
2133
# [ tokio ::test ( flavor = " multi_thread" ) ]
2134
async fn on_decides_which_events_start_a_job ( ) {
2135
let h = hub ( ) .await;
2136
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2137
create ( & h.app, " andy" , " events" ) .await;
2138
let url = format! ( " {} /andy/events" , h.base) ;
2139
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2140
let c = client ( & url, work.path ( ) ) ;
2141
let text = format! (
2142
" on {{ \n push \" main\" \n manual\n }} \n {} job \" dist\" {{ \n sandbox \" alpine\" \n on {{ \n tag \" v*\" \n }} \n exec \" true\" \n }} \n " ,
2143
pipeline ( " " )
2144
) ;
2145
2146
let text = text.replacen (
2147
" on {\n push \" main\" \n manual\n }\n schema-version 1\n " ,
2148
" schema-version 1\n on {\n push \" main\" \n manual\n }\n " ,
2149
1 ,
2150
) ;
2151
write ( work.path ( ) , " .flow/pipeline.kdl" , & text) ;
2152
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2153
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
2154
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2155
let jobs_of_latest = | app : Shared, n : usize | async move {
2156
let mut runs = Vec :: new( ) ;
2157
for _ in 0 .. 100 {
2158
runs = runs_of ( & app, " andy" , " events" ) .await;
2159
if runs.len ( ) > = n {
2160
break ;
2161
}
2162
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2163
}
2164
assert_eq! ( runs.len ( ) , n, " runs" ) ;
2165
let mut j: Vec< String> = db:: flow:: jobs( & app.db, runs[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
2166
j.sort ( ) ;
2167
( runs[ 0 ] .clone ( ) , j)
2168
} ;
2169
let ( _ , j) = jobs_of_latest ( h.app.clone ( ) , 1 ) .await;
2170
assert_eq! ( j, [ " build" , " lint" , " test" ] , " a push to main: no dist" ) ;
2171
2172
let heads = c.bookmark_heads ( ) .unwrap ( ) ;
2173
c.tag_create ( " v1.0.0" , & heads, " first" ) .unwrap ( ) ;
2174
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2175
let ( run, j) = jobs_of_latest ( h.app.clone ( ) , 2 ) .await;
2176
assert_eq! ( j, [ " dist" ] , " a tag runs only what names it" ) ;
2177
assert_eq! ( run.trigger, " tag:v1.0.0" ) ;
2178
2179
let row = db:: repos:: get( & h.app.db, " andy" , " events" ) .await.unwrap ( ) .unwrap ( ) ;
2180
db:: flow:: finish(
2181
& h.app.db,
2182
db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) [ 0 ] .id,
2183
db:: flow:: Outcome { status: " passed" , exit_code: Some ( 0 ) , error: " " , receipt: " " , receipt_hash: " " } ,
2184
)
2185
.await
2186
.unwrap ( ) ;
2187
let run = db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) ;
2188
oxygenhub:: flow:: rerun( & h.app, & row, & run, " andy" ) .await.unwrap ( ) ;
2189
let ( _ , j) = jobs_of_latest ( h.app.clone ( ) , 3 ) .await;
2190
assert_eq! ( j, [ " dist" ] , " the rerun is the tag's run again" ) ;
2191
2192
c.bookmark_create ( " develop" , None ) .unwrap ( ) ;
2193
assert! ( c.push_bookmark ( " origin" , " develop" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2194
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
2195
assert_eq! ( runs_of ( & h.app, " andy" , " events" ) .await.len ( ) , 3 , " no run for develop" ) ;
348f8c4b Andy McCall 1 month ago ↶
2196
}
2197
2198
2199
2200
# [ tokio ::test ( flavor = " multi_thread" ) ]
2201
async fn scheduled_jobs_run_when_due ( ) {
2202
let h = hub ( ) .await;
2203
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2204
create ( & h.app, " andy" , " nightly" ) .await;
2205
let url = format! ( " {} /andy/nightly" , h.base) ;
2206
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2207
let c = client ( & url, work.path ( ) ) ;
2208
2209
let extra = " job \" nightly\" {\n sandbox \" alpine\" \n on {\n schedule \" 17 4 29 2 *\" \n }\n exec \" true\" \n }\n " ;
2210
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
2211
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2212
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
2213
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2214
assert_eq! ( runs_of ( & h.app, " andy" , " nightly" ) .await.len ( ) , 1 ) ;
2215
let first = runs_of ( & h.app, " andy" , " nightly" ) .await;
2216
let names: Vec< String> = db:: flow:: jobs( & h.app.db, first[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
2217
assert! ( ! names.contains ( & " nightly" .to_string ( ) ) , " a push does not run a scheduled job" ) ;
2218
use flow_core:: pipeline:: When;
2219
let not_yet = When { minute: 16 , hour: 4 , day: 29 , month: 2 , weekday: 4 } ;
2220
assert_eq! ( oxygenhub:: flow:: run_due( & h.app, not_yet) .await, 0 ) ;
2221
let due = When { minute: 17 , .. not_yet } ;
2222
assert_eq! ( oxygenhub:: flow:: run_due( & h.app, due) .await, 1 ) ;
2223
let runs = runs_of ( & h.app, " andy" , " nightly" ) .await;
2224
assert_eq! ( runs.len ( ) , 2 ) ;
2225
assert_eq! ( runs[ 0 ] .trigger, " schedule" ) ;
2226
let names: Vec< String> = db:: flow:: jobs( & h.app.db, runs[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
2227
assert_eq! ( names, [ " nightly" ] ) ;
2228
assert_eq! ( runs[ 0 ] .bookmark, " main" , " on the default bookmark" ) ;
5f3fa151 Andy McCall 11 days ago ↶
2229
2230
2231
write ( work.path ( ) , " .flow/pipeline.kdl" , " schema-version 1\n " ) ;
2232
c.commit ( " No jobs" , & [ ] ) .unwrap ( ) ;
2233
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2234
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
2235
let runs = runs_of ( & h.app, " andy" , " nightly" ) .await;
2236
assert_eq! ( runs.len ( ) , 3 ) ;
2237
assert! ( runs[ 0 ] .error.contains ( " at least one job" ) , " {}" , runs[ 0 ] .error) ;
2238
assert_eq! ( oxygenhub:: flow:: run_due( & h.app, due) .await, 0 ) ;
2239
assert_eq! ( oxygenhub:: flow:: run_due( & h.app, not_yet) .await, 0 ) ;
2240
assert_eq! ( runs_of ( & h.app, " andy" , " nightly" ) .await.len ( ) , 3 , " no scheduled runs of a broken pipeline" ) ;
c889911b Andy McCall 1 month ago ↶
2241
}
2a83f530 Andy McCall 1 month ago ↶
2242
2243
# [ tokio ::test ( flavor = " multi_thread" ) ]
2244
async fn variables_from_three_levels_reach_a_job_inner_overriding_outer ( ) {
2245
use oxygenhub:: levels:: Scope;
2246
use oxygenhub:: variables;
2247
let h = hub ( ) .await;
2248
let app = & h.app;
2249
2250
let res = oxygenhub:: router( app.clone ( ) )
2251
.oneshot (
2252
Request:: post( " /register" )
2253
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 20 days ago
2254
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
2a83f530 Andy McCall 1 month ago ↶
2255
.unwrap ( ) ,
2256
)
2257
.await
2258
.unwrap ( ) ;
2259
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
2260
let andy = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
2261
db:: accounts:: create_org( & app.db, " acme" , " Acme" , andy.id) .await.unwrap ( ) ;
2262
create ( app, " acme" , " app" ) .await;
2263
let page = | path : & str , form : Option< String> | {
2264
let mut req =
2265
Request:: builder( ) .method ( if form.is_some ( ) { " POST" } else { " GET" } ) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
2266
if form.is_some ( ) {
2267
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
2268
}
2269
let req = req.body ( Body:: from( form.unwrap_or_default ( ) ) ) .unwrap ( ) ;
c7067c7b Andy McCall 16 days ago
2270
let ( app, cookie) = ( app.clone ( ) , cookie.clone ( ) ) ;
2a83f530 Andy McCall 1 month ago ↶
2271
async move {
c7067c7b Andy McCall 16 days ago
2272
let mut res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
2273
2274
let back =
2275
res.headers ( ) .get ( header:: LOCATION ) .and_then ( | v | v.to_str ( ) .ok ( ) ) .filter ( | l | l.contains ( " /settings/" ) ) .map ( String :: from) ;
2276
if let Some ( to) = back {
2277
let req = Request:: get( to) .header ( header:: COOKIE , cookie) .body ( Body:: empty( ) ) .unwrap ( ) ;
2278
res = oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) ;
2279
}
2a83f530 Andy McCall 1 month ago ↶
2280
let status = res.status ( ) ;
2281
let body = String :: from_utf8( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ) .unwrap ( ) ;
2282
( status, body)
2283
}
2284
} ;
2285
let csrf_of = | page : & str | {
2286
let i = page.find ( " name=\" _csrf\" value=\" " ) .expect ( " csrf field" ) + " name=\" _csrf\" value=\" " .len ( ) ;
2287
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
2288
} ;
2289
2290
2291
2292
variables:: set( & app.db, Scope:: Site, " CC" , " gcc" ) .await.unwrap ( ) ;
2293
variables:: set( & app.db, Scope:: Site, " LANG" , " C" ) .await.unwrap ( ) ;
2294
let org = db:: accounts:: org_by_name( & app.db, " acme" ) .await.unwrap ( ) .unwrap ( ) ;
2295
variables:: set( & app.db, Scope:: Org( org.id) , " CC" , " clang" ) .await.unwrap ( ) ;
c7067c7b Andy McCall 16 days ago
2296
let ( st, settings) = page ( " /acme/app/settings/variables" , None ) .await;
2a83f530 Andy McCall 1 month ago ↶
2297
assert_eq! ( st, StatusCode:: OK , " {settings}" ) ;
2298
assert! ( settings.contains ( " Inherited" ) & & settings.contains ( " the acme organisation" ) & & settings.contains ( " overridden" ) , " {settings}" ) ;
2299
let csrf = csrf_of ( & settings) ;
2300
let ( _ , body) = page ( " /acme/app/settings/variables" , Some ( format! ( " _csrf={csrf} &action=set&name=RUST_LOG&value=debug" ) ) ) .await;
2301
assert! ( body.contains ( " RUST_LOG set." ) & & body.contains ( " <code>debug</code>" ) , " {body}" ) ;
2302
let ( _ , body) = page ( " /acme/app/settings/variables" , Some ( format! ( " _csrf={csrf} &action=set&name=FLOW_REPO&value=x" ) ) ) .await;
2303
assert! ( body.contains ( " not FLOW_" ) , " {body}" ) ;
2304
let repo = db:: repos:: get( & app.db, " acme" , " app" ) .await.unwrap ( ) .unwrap ( ) ;
2305
db:: repos:: set_flow( & app.db, repo.id, " on" ) .await.unwrap ( ) ;
2306
let env = variables:: env_for_repo( & app.db, & repo) .await.unwrap ( ) ;
2307
assert_eq! (
2308
env.iter ( ) .map ( | ( k , v ) | ( k.as_str ( ) , v.as_str ( ) ) ) .collect:: < Vec< _ > > ( ) ,
2309
[ ( " CC" , " clang" ) , ( " LANG" , " C" ) , ( " RUST_LOG" , " debug" ) ] ,
2310
" the organisation's CC wins over the site's"
2311
) ;
2312
2313
2314
let url = format! ( " {} /acme/app" , h.base) ;
2315
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2316
let c = client ( & url, work.path ( ) ) ;
2317
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
2318
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2319
c.snapshot ( " test" ) .unwrap ( ) ;
2320
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2321
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2322
let runs = runs_of ( app, " acme" , " app" ) .await;
2323
assert_eq! ( runs.len ( ) , 1 , " {runs:?}" ) ;
2324
let got = claim ( app) .await.unwrap_or_else ( | | panic! ( " a job: {:?}" , runs[ 0 ] ) ) ;
2325
assert_eq! ( got[ " env" ] , serde_json:: json! ( { " CC" : " clang" , " LANG" : " C" , " RUST_LOG" : " debug" } ) , " {got}" ) ;
2326
2327
737de0fa Andy McCall 16 days ago ↶
2328
let ( _ , admin) = page ( " /admin/variables" , None ) .await;
2a83f530 Andy McCall 1 month ago ↶
2329
assert! ( admin.contains ( " Variables" ) & & admin.contains ( " <code>gcc</code>" ) , " {admin}" ) ;
2330
let ( _ , body) = page ( " /admin/variables" , Some ( format! ( " _csrf={} &action=delete&name=LANG" , csrf_of ( & admin) ) ) ) .await;
2331
assert! ( body.contains ( " LANG removed." ) , " {body}" ) ;
2332
let got = claim ( app) .await.expect ( " another job" ) ;
2333
assert_eq! ( got[ " env" ] , serde_json:: json! ( { " CC" : " clang" , " RUST_LOG" : " debug" } ) , " {got}" ) ;
2334
}
4ac01a73 Andy McCall 1 month ago ↶
2335
2336
# [ tokio ::test ( flavor = " multi_thread" ) ]
2337
async fn secrets_a_job_declares_come_with_the_claim_and_never_reach_the_log ( ) {
2338
let h = hub ( ) .await;
2339
let app = & h.app;
2340
let res = oxygenhub:: router( app.clone ( ) )
2341
.oneshot (
2342
Request:: post( " /register" )
2343
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 20 days ago
2344
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
4ac01a73 Andy McCall 1 month ago ↶
2345
.unwrap ( ) ,
2346
)
2347
.await
2348
.unwrap ( ) ;
2349
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
2350
let page = | path : & str , form : Option< String> | {
2351
let mut req =
2352
Request:: builder( ) .method ( if form.is_some ( ) { " POST" } else { " GET" } ) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
2353
if form.is_some ( ) {
2354
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
2355
}
2356
let req = req.body ( Body:: from( form.unwrap_or_default ( ) ) ) .unwrap ( ) ;
c7067c7b Andy McCall 16 days ago
2357
let ( app, cookie) = ( app.clone ( ) , cookie.clone ( ) ) ;
4ac01a73 Andy McCall 1 month ago ↶
2358
async move {
c7067c7b Andy McCall 16 days ago
2359
let mut res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
2360
2361
let back =
2362
res.headers ( ) .get ( header:: LOCATION ) .and_then ( | v | v.to_str ( ) .ok ( ) ) .filter ( | l | l.contains ( " /settings/" ) ) .map ( String :: from) ;
2363
if let Some ( to) = back {
2364
let req = Request:: get( to) .header ( header:: COOKIE , cookie) .body ( Body:: empty( ) ) .unwrap ( ) ;
2365
res = oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) ;
2366
}
4ac01a73 Andy McCall 1 month ago ↶
2367
let status = res.status ( ) ;
2368
let body = String :: from_utf8( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ) .unwrap ( ) ;
2369
( status, body)
2370
}
2371
} ;
2372
let csrf_of = | page : & str | {
2373
let i = page.find ( " name=\" _csrf\" value=\" " ) .expect ( " csrf field" ) + " name=\" _csrf\" value=\" " .len ( ) ;
2374
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
2375
} ;
2376
create ( app, " andy" , " demo" ) .await;
2377
2378
2379
let ( _ , admin) = page ( " /admin" , None ) .await;
2380
let csrf = csrf_of ( & admin) ;
2381
let ( _ , body) = page ( " /admin/secrets" , Some ( format! ( " _csrf={csrf} &action=set&name=deploy-token&value=site-level-value" ) ) ) .await;
2382
assert! ( body.contains ( " deploy-token set." ) & & ! body.contains ( " site-level-value" ) , " {body}" ) ;
2383
let ( _ , body) = page ( " /admin/secrets" , Some ( format! ( " _csrf={csrf} &action=set&name=bad+name&value=x" ) ) ) .await;
2384
assert! ( body.contains ( " name is 1 to 64 of" ) , " {body}" ) ;
c7067c7b Andy McCall 16 days ago
2385
let ( _ , settings) = page ( " /andy/demo/settings/secrets" , None ) .await;
4ac01a73 Andy McCall 1 month ago ↶
2386
assert! (
2387
settings.contains ( " Secrets" ) & & settings.contains ( " <code>deploy-token</code>" ) & & settings.contains ( " the site</td>" ) ,
2388
" {settings}"
2389
) ;
2390
let csrf_r = csrf_of ( & settings) ;
2391
let ( _ , body) =
2392
page ( " /andy/demo/settings/secrets" , Some ( format! ( " _csrf={csrf_r} &action=set&name=deploy-token&value=hunter22-repo-value" ) ) ) .await;
2393
assert! ( body.contains ( " deploy-token set." ) & & body.contains ( " overridden" ) & & ! body.contains ( " hunter22" ) , " {body}" ) ;
2394
let repo = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
2395
assert! (
2396
! String :: from_utf8_lossy( & oxygenhub:: secrets:: list( & app.db, oxygenhub:: levels:: Scope:: Repo( repo.id) ) .await.unwrap ( ) [ 0 ] .value)
2397
.contains ( " hunter22" ) ,
2398
" sealed at rest"
2399
) ;
2400
2401
2402
let url = format! ( " {} /andy/demo" , h.base) ;
2403
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2404
let c = client ( & url, work.path ( ) ) ;
2405
write (
2406
work.path ( ) ,
2407
" .flow/pipeline.kdl" ,
2408
& 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 " ) ,
2409
) ;
2410
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2411
c.snapshot ( " test" ) .unwrap ( ) ;
2412
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2413
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2414
let runs = runs_of ( app, " andy" , " demo" ) .await;
2415
let jobs = match runs.first ( ) {
2416
Some ( r) => db:: flow:: jobs( & app.db, r.id) .await.unwrap ( ) .iter ( ) .map ( | j | format! ( " {} :{} " , j.name, j.status) ) .collect:: < Vec< _ > > ( ) ,
2417
None => vec! [ ] ,
2418
} ;
2419
let mut deploy = None ;
2420
for _ in 0 .. 50 {
2421
let Some ( got) = claim ( app) .await else {
2422
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
2423
continue ;
2424
} ;
2425
if got[ " job" ] [ " name" ] = = " deploy" {
2426
deploy = Some ( got) ;
2427
break ;
2428
}
2429
assert_eq! ( got[ " secrets" ] , serde_json:: Value:: Null, " {got}" ) ;
2430
}
2431
let got = deploy.unwrap_or_else ( | | panic! ( " the deploy job: runs {runs:?} jobs {jobs:?}" ) ) ;
2432
assert_eq! (
2433
got[ " secrets" ] ,
2434
serde_json:: json! ( [ { " name" : " deploy-token" , " path" : " /run/secrets/token" , " value" : " hunter22-repo-value" } ] ) ,
2435
" the repository's value, the missing one left out: {got}"
2436
) ;
2437
let id = got[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2438
let log = db:: flow:: log( & app.db, id, 0 , 100 ) .await.unwrap ( ) ;
2439
assert! ( log.iter ( ) .any ( | l | l.stream = = " flow" & & l.text.contains ( " secret \" missing\" is not set" ) ) , " {log:?}" ) ;
2440
2441
2442
let records = serde_json:: json! ( { " records" : [
2443
{ " t" : 1 , " stream" : " out" , " text" : " token=hunter22-repo-value" } ,
2444
{ " t" : 2 , " stream" : " err" , " text" : " b64 aHVudGVyMjItcmVwby12YWx1ZQ== end" } ,
2445
] } ) ;
2446
let ( st, _ ) = call ( app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( records.to_string ( ) ) ) .await;
2447
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
2448
let log = db:: flow:: log( & app.db, id, 0 , 100 ) .await.unwrap ( ) ;
2449
let texts: Vec< & str > = log.iter ( ) .map ( | l | l.text.as_str ( ) ) .collect ( ) ;
2450
assert! ( texts.contains ( & " token=***" ) & & texts.contains ( & " b64 *** end" ) , " {texts:?}" ) ;
2451
assert! ( ! texts.iter ( ) .any ( | t | t.contains ( " hunter22" ) ) , " {texts:?}" ) ;
2452
}
d1727c57 Andy McCall 1 month ago ↶
2453
2454
# [ tokio ::test ( flavor = " multi_thread" ) ]
2455
async fn a_job_s_identity_is_a_signed_token_the_discovery_document_lets_anyone_verify ( ) {
2456
let h = hub ( ) .await;
2457
let app = & h.app;
2458
let ( st, disc) = call ( app, " GET" , " /.well-known/openid-configuration" , None , None ) .await;
2459
assert_eq! ( st, StatusCode:: OK , " {disc}" ) ;
2460
let disc: serde_json:: Value = serde_json:: from_str( & disc) .unwrap ( ) ;
2461
assert_eq! ( disc[ " issuer" ] , h.base.as_str ( ) ) ;
2462
assert_eq! ( disc[ " jwks_uri" ] , format! ( " {} /.well-known/jwks.json" , h.base) ) ;
2463
let ( st, jwks) = call ( app, " GET" , " /.well-known/jwks.json" , None , None ) .await;
2464
assert_eq! ( st, StatusCode:: OK , " {jwks}" ) ;
2465
let jwks: serde_json:: Value = serde_json:: from_str( & jwks) .unwrap ( ) ;
2466
assert_eq! ( jwks[ " keys" ] [ 0 ] [ " alg" ] , " RS256" ) ;
2467
let kid = jwks[ " keys" ] [ 0 ] [ " kid" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
2468
2469
create ( app, " andy" , " demo" ) .await;
2470
let url = format! ( " {} /andy/demo" , h.base) ;
2471
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2472
let c = client ( & url, work.path ( ) ) ;
2473
write (
2474
work.path ( ) ,
2475
" .flow/pipeline.kdl" ,
2476
& pipeline ( " job \" deploy\" {\n sandbox \" alpine\" \n identity \" sts.amazonaws.com\" {\n mount-path \" /run/secrets/aws-token\" \n }\n exec \" deploy\" \n }\n " ) ,
2477
) ;
2478
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2479
c.snapshot ( " test" ) .unwrap ( ) ;
2480
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2481
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2482
let mut deploy = None ;
2483
for _ in 0 .. 50 {
2484
let Some ( got) = claim ( app) .await else {
2485
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
2486
continue ;
2487
} ;
2488
if got[ " job" ] [ " name" ] = = " deploy" {
2489
deploy = Some ( got) ;
2490
break ;
2491
}
2492
}
2493
let got = deploy.expect ( " the deploy job" ) ;
2494
let secrets = got[ " secrets" ] .as_array ( ) .expect ( " secrets" ) ;
2495
assert_eq! ( secrets.len ( ) , 1 ) ;
2496
assert_eq! (
2497
( secrets[ 0 ] [ " name" ] .as_str ( ) , secrets[ 0 ] [ " path" ] .as_str ( ) ) ,
2498
( Some ( " identity:sts.amazonaws.com" ) , Some ( " /run/secrets/aws-token" ) )
2499
) ;
2500
let token = secrets[ 0 ] [ " value" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
2501
let issuer = app.oidc ( ) .await.unwrap ( ) ;
2502
assert_eq! ( issuer.kid, kid, " the key the JWKS publishes signed it" ) ;
2503
let claims = issuer.verify ( & token, " sts.amazonaws.com" , db:: now_ms( ) ) .expect ( " verifies" ) ;
2504
assert_eq! ( claims[ " sub" ] , " repo:andy/demo:bookmark:main" ) ;
2505
assert_eq! ( claims[ " repository" ] , " andy/demo" ) ;
2506
assert_eq! ( claims[ " job" ] , " deploy" ) ;
2507
assert_eq! ( claims[ " run_number" ] , 1 ) ;
2508
assert_eq! ( claims[ " runner" ] , " test-1" ) ;
2509
assert_eq! ( claims[ " iss" ] , h.base.as_str ( ) ) ;
2510
assert! ( claims[ " state" ] .as_str ( ) .unwrap ( ) .len ( ) = = 64 ) ;
2511
2512
let id = got[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2513
let records = serde_json:: json! ( { " records" : [ { " t" : 1 , " stream" : " out" , " text" : format! ( " TOKEN={token} end" ) } ] } ) ;
2514
let ( st, _ ) = call ( app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( records.to_string ( ) ) ) .await;
2515
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
2516
let log = db:: flow:: log( & app.db, id, 0 , 100 ) .await.unwrap ( ) ;
2517
assert! ( log.iter ( ) .any ( | l | l.text = = " TOKEN=*** end" ) , " {log:?}" ) ;
2518
}
efd29a00 Andy McCall 1 month ago ↶
2519
2520
# [ tokio ::test ( flavor = " multi_thread" ) ]
2521
async fn jobs_are_offered_only_to_runners_of_their_architecture ( ) {
2522
let h = hub ( ) .await;
2523
let app = & h.app;
2524
create ( app, " andy" , " demo" ) .await;
2525
let url = format! ( " {} /andy/demo" , h.base) ;
2526
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2527
let c = client ( & url, work.path ( ) ) ;
2528
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 ) ) ) ;
2529
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2530
c.snapshot ( " test" ) .unwrap ( ) ;
2531
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2532
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2533
let claim_as = | arch : & 'static str , archs : serde_json:: Value| {
2534
let app = app.clone ( ) ;
2535
async move {
2536
let body = serde_json:: json! ( {
2537
" runner" : format! ( " {arch} -1" ) , " runtimes" : [ " microvm" ] ,
2538
" info" : { " arch" : arch, " os" : " linux" , " version" : " t" , " labels" : [ ] , " max_jobs" : 1 } ,
2539
" image_archs" : archs,
2540
} ) ;
2541
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body.to_string ( ) ) ) .await;
2542
match st {
2543
StatusCode:: NO_CONTENT => None ,
2544
StatusCode:: OK => {
2545
Some ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) )
2546
}
2547
other => panic! ( " claim: {other} {body}" ) ,
2548
}
2549
}
2550
} ;
2551
2552
2553
let rootfs = pipeline ( " " ) .split ( " cas://blob/" ) .nth ( 1 ) .unwrap ( ) [ .. 64 ] .to_string ( ) ;
2554
for _ in 0 .. 50 {
2555
if ! runs_of ( app, " andy" , " demo" ) .await.is_empty ( ) {
2556
break ;
2557
}
2558
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
2559
}
2560
let runs = runs_of ( app, " andy" , " demo" ) .await;
2561
assert_eq! ( runs.len ( ) , 1 , " {runs:?}" ) ;
2562
let mut taken = vec! [ ] ;
2563
while let Some ( j) = claim_as ( " x86_64" , serde_json:: json! ( { rootfs.clone ( ) : " x86_64" } ) ) .await {
2564
taken.push ( j) ;
2565
}
2566
assert_eq! ( taken, [ " build" , " lint" ] , " test waits for build; flash is for riscv64" ) ;
2567
2568
assert_eq! ( claim_as ( " riscv64" , serde_json:: json! ( { } ) ) .await.as_deref ( ) , Some ( " flash" ) ) ;
2569
assert_eq! ( claim_as ( " riscv64" , serde_json:: json! ( { } ) ) .await, None ) ;
2570
let ( _ , queue) = call ( app, " GET" , " /admin/runners" , None , None ) .await;
2571
let _ = queue;
2572
}
4d2e44d7 Andy McCall 1 month ago ↶
2573
2574
2575
2576
2577
2578
# [ tokio ::test ( flavor = " multi_thread" ) ]
2579
async fn a_declined_job_goes_to_another_runner_or_ends_unplaceable ( ) {
2580
let h = hub ( ) .await;
2581
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2582
create ( & h.app, " andy" , " declines" ) .await;
2583
let url = format! ( " {} /andy/declines" , h.base) ;
2584
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2585
let c = client ( & url, work.path ( ) ) ;
2586
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
2587
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2588
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2589
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2590
let run = runs_of ( & h.app, " andy" , " declines" ) .await[ 0 ] .clone ( ) ;
2591
let claim_as = | token : & 'static str , name : & 'static str | {
2592
let app = h.app.clone ( ) ;
2593
async move {
2594
let body = serde_json:: json! ( { " runner" : name, " runtimes" : [ " microvm" ] } ) .to_string ( ) ;
2595
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( token) , Some ( body) ) .await;
2596
( st = = StatusCode:: OK ) .then ( | | serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) )
2597
}
2598
} ;
2599
let decline = | token : & 'static str , id : i64 , reason : & 'static str | {
2600
let app = h.app.clone ( ) ;
2601
async move {
2602
let body = serde_json:: json! ( { " reason" : reason } ) .to_string ( ) ;
2603
call ( & app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /decline" ) , Some ( token) , Some ( body) ) .await.0
2604
}
2605
} ;
2606
let job = claim_as ( " runner-secret" , " earth-1" ) .await.expect ( " a job" ) ;
2607
2608
assert_eq! ( decline ( " runner-two" , job, " not mine" ) .await, StatusCode:: CONFLICT , " only the runner it runs on may decline it" ) ;
2609
assert_eq! ( decline ( " runner-secret" , job, " the MicroVM tier needs /dev/kvm" ) .await, StatusCode:: NO_CONTENT ) ;
2610
let j = db:: flow:: job( & h.app.db, job) .await.unwrap ( ) .unwrap ( ) ;
2611
assert_eq! ( ( j.status.as_str ( ) , j.runner.as_str ( ) ) , ( " queued" , " " ) ) ;
2612
assert_eq! ( j.waiting, " declined by earth-1: the MicroVM tier needs /dev/kvm; waiting for another runner" ) ;
2613
2614
let next = claim_as ( " runner-secret" , " earth-1" ) .await.expect ( " the other ready job" ) ;
2615
assert_ne! ( next, job) ;
2616
let page = call ( & h.app, " GET" , & format! ( " /andy/declines/flow/{} " , run.number) , None , None ) .await.1 ;
2617
assert! ( page.contains ( " Waiting: declined by earth-1: the MicroVM tier needs /dev/kvm" ) , " {page}" ) ;
2618
assert_eq! ( claim_as ( " runner-two" , " plex-1" ) .await, Some ( job) , " another runner takes it" ) ;
2619
assert_eq! ( decline ( " runner-two" , job, " rootfs 36fc4e323804 is not on this runner" ) .await, StatusCode:: NO_CONTENT ) ;
2620
assert_eq! ( claim_as ( " runner-three" , " narvi" ) .await, Some ( job) ) ;
2621
assert_eq! ( decline ( " runner-three" , job, " rootfs 36fc4e323804 is for x86_64; this runner is riscv64" ) .await, StatusCode:: NO_CONTENT ) ;
2622
let j = db:: flow:: job( & h.app.db, job) .await.unwrap ( ) .unwrap ( ) ;
2623
assert_eq! ( j.status, " error" ) ;
2624
assert_eq! (
2625
j.error,
2626
" 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"
2627
) ;
2628
let r = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " plex-1" ) .unwrap ( ) ;
2629
let declined = db:: flow:: declines_of_runner( & h.app.db, r.id, 10 ) .await.unwrap ( ) ;
2630
assert_eq! ( declined.len ( ) , 1 ) ;
2631
assert_eq! ( ( declined[ 0 ] .job_id, declined[ 0 ] .reason.as_str ( ) ) , ( job, " rootfs 36fc4e323804 is not on this runner" ) ) ;
2632
assert_eq! ( r.tier_list ( ) , vec! [ " microvm" ] ) ;
2633
}
554ccacf Andy McCall 1 month ago ↶
2634
ab4df6aa Andy McCall 11 days ago ↶
2635
2636
2637
2638
2639
# [ tokio ::test ( flavor = " multi_thread" ) ]
2640
async fn a_drained_job_goes_back_ahead_of_new_work ( ) {
2641
let h = hub ( ) .await;
2642
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2643
create ( & h.app, " andy" , " drains" ) .await;
2644
let url = format! ( " {} /andy/drains" , h.base) ;
2645
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2646
let c = client ( & url, work.path ( ) ) ;
e4203e14 Andy McCall 11 days ago ↶
2647
let bulk = " job \" bulk\" {\n sandbox \" alpine\" \n priority \" low\" \n checkpoint \" /src/state\" \n exec \" true\" \n }\n " ;
ab4df6aa Andy McCall 11 days ago ↶
2648
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( bulk) ) ;
2649
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2650
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2651
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2652
runs_of ( & h.app, " andy" , " drains" ) .await;
2653
let claim_as = | token : & 'static str , name : & 'static str | {
2654
let app = h.app.clone ( ) ;
2655
async move {
2656
let body = serde_json:: json! ( { " runner" : name, " runtimes" : [ " microvm" ] } ) .to_string ( ) ;
2657
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( token) , Some ( body) ) .await;
2658
( st = = StatusCode:: OK ) .then ( | | serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) )
2659
}
2660
} ;
2661
let release = | token : & 'static str , id : i64 , reason : & 'static str | {
2662
let app = h.app.clone ( ) ;
2663
async move {
2664
let body = serde_json:: json! ( { " reason" : reason } ) .to_string ( ) ;
2665
call ( & app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /release" ) , Some ( token) , Some ( body) ) .await.0
2666
}
2667
} ;
2668
let name_of = | id : i64 | {
2669
let app = h.app.clone ( ) ;
2670
async move { db:: flow:: job( & app.db, id) .await.unwrap ( ) .unwrap ( ) .name }
2671
} ;
2672
let build = claim_as ( " runner-secret" , " earth-1" ) .await.unwrap ( ) ;
2673
let lint = claim_as ( " runner-two" , " plex-1" ) .await.unwrap ( ) ;
2674
let low = claim_as ( " runner-three" , " phone" ) .await.unwrap ( ) ;
2675
assert_eq! ( ( name_of ( build) .await, name_of ( lint) .await, name_of ( low) .await) , ( " build" .into ( ) , " lint" .into ( ) , " bulk" .into ( ) ) ) ;
2676
2677
let line = serde_json:: json! ( { " records" : [ { " t" : 1200 , " stream" : " stdout" , " text" : " step 1 of 30" } ] } ) .to_string ( ) ;
2678
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{low} /log" ) , Some ( " runner-three" ) , Some ( line) ) .await;
2679
assert_eq! ( finish ( & h.app, build, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 } ) ) .await, StatusCode:: NO_CONTENT ) ;
e4203e14 Andy McCall 11 days ago ↶
2680
2681
let ( st, body) = call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{lint} /checkpoint" ) , Some ( " runner-two" ) , Some ( " x" .into ( ) ) ) .await;
2682
assert_eq! ( st, StatusCode:: BAD_REQUEST , " lint declares no checkpoint: {body}" ) ;
2683
let ( st, body) =
2684
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{low} /checkpoint" ) , Some ( " runner-three" ) , Some ( " step 32" .into ( ) ) ) .await;
2685
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
2686
let saved = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
ab4df6aa Andy McCall 11 days ago ↶
2687
assert_eq! ( release ( " runner-two" , low, " off Wi-Fi" ) .await, StatusCode:: CONFLICT , " only the runner that holds it" ) ;
2688
assert_eq! ( release ( " runner-three" , low, " off Wi-Fi" ) .await, StatusCode:: NO_CONTENT ) ;
2689
let j = db:: flow:: job( & h.app.db, low) .await.unwrap ( ) .unwrap ( ) ;
2690
assert_eq! ( ( j.status.as_str ( ) , j.runner.as_str ( ) , j.handbacks) , ( " queued" , " " , 1 ) ) ;
2691
let log: Vec< String> = db:: flow:: log( & h.app.db, low, 0 , 50 ) .await.unwrap ( ) .into_iter ( ) .map ( | l | l.text ) .collect ( ) ;
2692
assert_eq! ( log, [ " step 1 of 30" , " ── handed back by phone: off Wi-Fi; queued again ahead of new work" ] ) ;
2693
assert! ( db:: flow:: declines_of_job( & h.app.db, low) .await.unwrap ( ) .is_empty ( ) , " not a decline" ) ;
2694
2695
e4203e14 Andy McCall 11 days ago ↶
2696
let body = serde_json:: json! ( { " runner" : " phone" , " runtimes" : [ " microvm" ] } ) .to_string ( ) ;
2697
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-three" ) , Some ( body) ) .await;
2698
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
2699
let again: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
2700
assert_eq! ( ( again[ " job_id" ] .as_i64 ( ) , again[ " checkpoint" ] .as_str ( ) ) , ( Some ( low) , Some ( saved.as_str ( ) ) ) , " it resumes: {again}" ) ;
2701
let ( st, bytes) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/jobs/{low} /checkpoint" ) , Some ( " runner-three" ) , None ) .await;
2702
assert_eq! ( ( st, bytes.as_str ( ) ) , ( StatusCode:: OK , " step 32" ) ) ;
2703
assert! ( db:: flow:: artifacts( & h.app.db, low) .await.unwrap ( ) .is_empty ( ) , " a checkpoint is no artifact of the job" ) ;
ab4df6aa Andy McCall 11 days ago ↶
2704
assert_eq! ( claim_as ( " runner-secret" , " earth-1" ) .await.map ( | id | id ! = low ) , Some ( true ) , " then new work" ) ;
2705
2706
2707
let phone = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " phone" ) .unwrap ( ) ;
2708
sqlx:: query( " UPDATE flow_runners SET last_seen_ms = $1 WHERE id = $2" )
2709
.bind ( db:: now_ms( ) - oxygenhub:: flow:: LEASE_MS - 1000 )
2710
.bind ( phone.id)
2711
.execute ( & h.app.db)
2712
.await
2713
.unwrap ( ) ;
2714
assert_eq! ( oxygenhub:: flow:: requeue_lapsed( & h.app) .await.unwrap ( ) , 1 ) ;
2715
let j = db:: flow:: job( & h.app.db, low) .await.unwrap ( ) .unwrap ( ) ;
2716
assert_eq! ( ( j.status.as_str ( ) , j.handbacks) , ( " queued" , 2 ) ) ;
2717
let log: Vec< String> = db:: flow:: log( & h.app.db, low, 0 , 50 ) .await.unwrap ( ) .into_iter ( ) .map ( | l | l.text ) .collect ( ) ;
2718
assert_eq! ( log.last ( ) .unwrap ( ) , " ── the runner phone has not been heard from for 3 minutes; queued again ahead of new work" ) ;
2719
assert_eq! ( claim_as ( " runner-two" , " plex-1" ) .await, Some ( low) , " another runner takes it" ) ;
2720
}
2721
3b650652 Andy McCall 11 days ago ↶
2722
2723
2724
2725
# [ tokio ::test ( flavor = " multi_thread" ) ]
2726
async fn a_claim_carries_the_job_s_expected_time ( ) {
2727
let h = hub ( ) .await;
2728
create ( & h.app, " andy" , " eta" ) .await;
2729
let url = format! ( " {} /andy/eta" , h.base) ;
2730
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2731
let c = client ( & url, work.path ( ) ) ;
2732
let one = " schema-version 1\n sandbox \" alpine\" {\n rootfs \" cas://blob/ROOTFS\" \n }\n job \" build\" {\n sandbox \" alpine\" \n exec \" make\" \n }\n " .replace ( " ROOTFS" , ROOTFS ) ;
2733
write ( work.path ( ) , " .flow/pipeline.kdl" , & one) ;
2734
c.commit ( " One job" , & [ ] ) .unwrap ( ) ;
2735
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2736
runs_of ( & h.app, " andy" , " eta" ) .await;
2737
let first = claim ( & h.app) .await.expect ( " a job" ) ;
2738
assert! ( first.get ( " expected_ms" ) .is_none ( ) , " never passed: no estimate, {first}" ) ;
2739
let id = first[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2740
assert_eq! ( finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 } ) ) .await, StatusCode:: NO_CONTENT ) ;
2741
2742
sqlx:: query( " UPDATE flow_jobs SET started_ms = finished_ms - 42000 WHERE id = $1" ) .bind ( id) .execute ( & h.app.db) .await.unwrap ( ) ;
2743
write ( work.path ( ) , " README" , " again\n " ) ;
2744
c.commit ( " Again" , & [ ] ) .unwrap ( ) ;
2745
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2746
let mut next = None ;
2747
for _ in 0 .. 100 {
2748
if let Some ( j) = claim ( & h.app) .await {
2749
next = Some ( j) ;
2750
break ;
2751
}
2752
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2753
}
2754
let next = next.expect ( " the second run's job" ) ;
2755
assert_eq! ( next[ " expected_ms" ] , 42_000 , " {next}" ) ;
2756
}
2757
554ccacf Andy McCall 1 month ago ↶
2758
2759
2760
2761
# [ tokio ::test ( flavor = " multi_thread" ) ]
2762
async fn a_coverage_artifact_becomes_a_coverage_report ( ) {
2763
let h = hub ( ) .await;
2764
create ( & h.app, " andy" , " cov" ) .await;
2765
let url = format! ( " {} /andy/cov" , h.base) ;
2766
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2767
let c = client ( & url, work.path ( ) ) ;
2768
write (
2769
work.path ( ) ,
2770
" .flow/pipeline.kdl" ,
2771
& format! (
2772
" 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 "
2773
) ,
2774
) ;
2775
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 " ) ;
2776
c.snapshot ( " test" ) .unwrap ( ) ;
2777
c.commit ( " Code and coverage" , & [ ] ) .unwrap ( ) ;
2778
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2779
let run = runs_of ( & h.app, " andy" , " cov" ) .await.remove ( 0 ) ;
c8df33a6 Andy McCall 25 days ago ↶
2780
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 ↶
2781
let id = claim ( & h.app) .await.unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2782
2783
let ( st, text) = upload ( & h.app, id, " coverage" , b " this is not a report" ) .await;
2784
assert_eq! ( st, StatusCode:: OK , " a bad report never fails the upload: {text}" ) ;
2785
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 \
2786
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 " ;
2787
let ( st, text) = upload ( & h.app, id, " coverage" , lcov.as_bytes ( ) ) .await;
2788
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
2789
let log: Vec< String> = db:: flow:: log( & h.app.db, id, 0 , 50 ) .await.unwrap ( ) .into_iter ( ) .map ( | l | l.text ) .collect ( ) ;
2790
assert! ( log.iter ( ) .any ( | l | l.contains ( " coverage: this is not an LCOV report" ) ) , " {log:?}" ) ;
2791
assert! (
2792
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)" ) ,
2793
" {log:?}"
2794
) ;
2795
2796
let ( st, page) = call ( & h.app, " GET" , " /andy/cov/coverage" , None , None ) .await;
2797
assert_eq! ( st, StatusCode:: OK ) ;
c987c865 Andy McCall 1 month ago ↶
2798
assert! (
c8df33a6 Andy McCall 25 days ago ↶
2799
page.contains ( " >Reports</a>" )
c987c865 Andy McCall 1 month ago ↶
2800
& & page.contains ( " 50.0%" )
2801
& & page.contains ( " <strong>2</strong> of <strong>4</strong> relevant lines covered" ) ,
2802
" {page}"
2803
) ;
554ccacf Andy McCall 1 month ago ↶
2804
assert! ( page.contains ( " 1 of 2" ) & & page.contains ( " /andy/cov/coverage/" ) & & page.contains ( " /file/src/lib.rs" ) , " {page}" ) ;
2805
let report = crate_report ( & h.app, run.id) .await;
2806
let ( st, file) = call ( & h.app, " GET" , & format! ( " /andy/cov/coverage/{report} /file/src/lib.rs" ) , None , None ) .await;
2807
assert_eq! ( st, StatusCode:: OK , " {file}" ) ;
2808
assert_eq! ( file.matches ( " class=\" cov-hit\" " ) .count ( ) , 2 , " {file}" ) ;
2809
assert_eq! ( file.matches ( " class=\" cov-miss\" " ) .count ( ) , 2 , " {file}" ) ;
2810
assert! ( file.contains ( " 3×" ) , " {file}" ) ;
2811
assert_eq! ( call ( & h.app, " GET" , & format! ( " /andy/cov/coverage/{report} /file/src/gone.rs" ) , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
2812
let ( _ , badge) = call ( & h.app, " GET" , " /andy/cov/coverage/badge.svg" , None , None ) .await;
2813
assert! ( badge.contains ( " coverage: 50.0%" ) , " {badge}" ) ;
2814
let ( _ , run_page) = call ( & h.app, " GET" , & format! ( " /andy/cov/flow/{} " , run.number) , None , None ) .await;
2815
assert! ( run_page.contains ( " coverage 50.0%" ) , " {run_page}" ) ;
617c2840 Andy McCall 1 month ago ↶
2816
2817
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/cov/coverage" , None , None ) .await;
2818
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
2819
assert_eq! ( ( list[ 0 ] [ " percent" ] .as_f64 ( ) , list[ 0 ] [ " format" ] .as_str ( ) ) , ( Some ( 50. 0 ) , Some ( " lcov" ) ) , " {list}" ) ;
2820
let ( _ , one) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/cov/coverage/{report} " ) , None , None ) .await;
2821
let one: serde_json:: Value = serde_json:: from_str( & one) .unwrap ( ) ;
2822
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 ↶
2823
2824
2825
2826
let row = db:: repos:: get( & h.app.db, " andy" , " cov" ) .await.unwrap ( ) .unwrap ( ) ;
2827
let check = | checks : Vec< db:: hooks:: Check> | checks.into_iter ( ) .find ( | c | c.name = = " coverage" ) .expect ( " a coverage check" ) ;
2828
let c = check ( db:: hooks:: checks_for_state( & h.app.db, row.id, & run.state) .await.unwrap ( ) ) ;
2829
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " passed" , " 50.0%" ) ) ;
2830
assert! ( c.url.ends_with ( & format! ( " /andy/cov/coverage?report={report} " ) ) , " {}" , c.url) ;
2831
oxygenhub:: coverage:: set_rules( & h.app.db, row.id, oxygenhub:: coverage:: Rules { min: Some ( 60. 0 ) , max_drop: None } ) .await.unwrap ( ) ;
2832
assert_eq! ( upload ( & h.app, id, " coverage" , lcov.as_bytes ( ) ) .await.0 , StatusCode:: OK ) ;
2833
let c = check ( db:: hooks:: checks_for_state( & h.app.db, row.id, & run.state) .await.unwrap ( ) ) ;
2834
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " failed" , " 50.0%: below the minimum of 60%" ) ) ;
554ccacf Andy McCall 1 month ago ↶
2835
}
2836
2837
async fn crate_report ( app : & Shared, run_id : i64 ) -> i64 {
2838
oxygenhub:: coverage:: for_run( & app.db, run_id) .await.unwrap ( ) [ 0 ] .id
2839
}
36e5a9c1 Andy McCall 1 month ago ↶
2840
2841
2842
2843
# [ tokio ::test ( flavor = " multi_thread" ) ]
2844
async fn junit_reports_make_a_tests_tab ( ) {
2845
let h = hub ( ) .await;
2846
create ( & h.app, " andy" , " t" ) .await;
2847
let url = format! ( " {} /andy/t" , h.base) ;
2848
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2849
let c = client ( & url, work.path ( ) ) ;
2850
write (
2851
work.path ( ) ,
2852
" .flow/pipeline.kdl" ,
2853
& format! (
2854
" 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 "
2855
) ,
2856
) ;
c8df33a6 Andy McCall 25 days ago ↶
2857
assert! ( ! call ( & h.app, " GET" , " /andy/t" , None , None ) .await.1. contains ( " >Reports</a>" ) ) ;
36e5a9c1 Andy McCall 1 month ago ↶
2858
let case = | name : & str , ok : bool , secs : & str | {
2859
if ok {
2860
format! ( r # "<testcase classname="t.suite" name="{name} " time="{secs} "/>"# )
2861
} else {
2862
format! (
2863
r # "<testcase classname="t.suite" name="{name} " time="{secs} "><failure message="{name} broke">AssertionError</failure></testcase>"#
2864
)
2865
}
2866
} ;
2867
let runs = [
2868
vec! [ case ( " a" , true , " 0.1" ) , case ( " b" , true , " 2.5" ) , case ( " c" , true , " 0.2" ) ] ,
2869
vec! [ case ( " a" , true , " 0.1" ) , case ( " b" , false , " 2.6" ) , case ( " c" , true , " 0.2" ) , case ( " d" , true , " 0.3" ) ] ,
2870
vec! [ case ( " a" , true , " 0.1" ) , case ( " b" , true , " 2.4" ) , case ( " c" , true , " 0.2" ) , case ( " d" , true , " 0.3" ) ] ,
2871
] ;
2872
for ( i, cases) in runs.iter ( ) .enumerate ( ) {
2873
write ( work.path ( ) , " n.txt" , & i.to_string ( ) ) ;
2874
c.snapshot ( " test" ) .unwrap ( ) ;
2875
c.commit ( & format! ( " Run {i} " ) , & [ ] ) .unwrap ( ) ;
2876
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2877
2878
let mut a = None ;
2879
for _ in 0 .. 100 {
2880
a = claim ( & h.app) .await;
2881
if a.is_some ( ) {
2882
break ;
2883
}
2884
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2885
}
2886
let a = a.expect ( " a job to claim" ) ;
2887
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2888
let xml = format! ( " <testsuites><testsuite name=\" t\" >{} </testsuite></testsuites>" , cases.concat ( ) ) ;
2889
assert_eq! ( upload ( & h.app, id, " junit" , xml.as_bytes ( ) ) .await.0 , StatusCode:: OK ) ;
2890
let failed = cases.iter ( ) .any ( | c | c.contains ( " <failure" ) ) ;
2891
let mut r = receipt ( " test" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , failed as u32 ) ;
2892
if ! failed {
2893
r[ " outputs" ] = serde_json:: json! ( [ oxy_core:: repo:: Repo:: blob_ref( xml.as_bytes ( ) ) .to_hex ( ) ] ) ;
2894
}
2895
let code = failed as i32 ;
2896
assert_eq! (
2897
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : code, " duration_ms" : 700 , " receipt" : r } ) ) .await,
2898
StatusCode:: NO_CONTENT
2899
) ;
2900
}
2901
let ( st, page) = call ( & h.app, " GET" , " /andy/t/tests" , None , None ) .await;
2902
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
2903
if let Some ( out) = std:: env:: var_os( " OXYHUB_DUMP_TESTS_PAGE" ) {
2904
std:: fs:: write( out, & page) .unwrap ( ) ;
2905
}
c8df33a6 Andy McCall 25 days ago ↶
2906
assert! ( page.contains ( " >Reports</a>" ) , " the tab" ) ;
36e5a9c1 Andy McCall 1 month ago ↶
2907
assert! ( page.contains ( " 100%" ) & & page.contains ( " of 4 tests passed" ) , " the latest run passes" ) ;
2908
assert! ( page.contains ( " 1 fixed" ) & & page.contains ( " t.suite::b" ) , " b fixed against the run before" ) ;
2909
assert! ( page.contains ( " 1 ✕" ) & & page.contains ( " 2 ✓" ) , " b is flaky: {page}" ) ;
2910
assert! ( page.contains ( " <svg class=\" cov-chart\" " ) , " charts" ) ;
2911
2912
let slow = page.find ( " Slowest tests" ) .unwrap ( ) ;
2913
assert! ( page[ slow.. ] .find ( " t.suite::b" ) .unwrap ( ) < page[ slow.. ] .find ( " t.suite::d" ) .unwrap ( ) ) ;
2914
2915
let row = db:: repos:: get( & h.app.db, " andy" , " t" ) .await.unwrap ( ) .unwrap ( ) ;
2916
let second = oxygenhub:: junit:: on_bookmark( & h.app.db, row.id, " main" , 10 ) .await.unwrap ( ) [ 1 ] .id;
2917
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/t/tests?report={second} " ) , None , None ) .await;
2918
assert! ( page.contains ( " 1 failing" ) & & page.contains ( " Newly failing" ) & & page.contains ( " b broke" ) , " {page}" ) ;
2919
assert! ( page.contains ( " 1 new tests" ) , " d is new" ) ;
617c2840 Andy McCall 1 month ago ↶
2920
2921
2922
2923
let ( st, list) = call ( & h.app, " GET" , " /api/v1/repos/andy/t/tests" , None , None ) .await;
2924
assert_eq! ( st, StatusCode:: OK , " {list}" ) ;
2925
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
2926
assert_eq! ( list.as_array ( ) .unwrap ( ) .len ( ) , 3 ) ;
2927
assert_eq! ( ( list[ 0 ] [ " tests" ] .as_i64 ( ) , list[ 1 ] [ " failed" ] .as_i64 ( ) ) , ( Some ( 4 ) , Some ( 1 ) ) ) ;
2928
let ( _ , one) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/t/tests/{second} ?status=failed" ) , None , None ) .await;
2929
let one: serde_json:: Value = serde_json:: from_str( & one) .unwrap ( ) ;
2930
assert_eq! ( one[ " cases" ] .as_array ( ) .unwrap ( ) .len ( ) , 1 ) ;
2931
assert_eq! ( ( one[ " cases" ] [ 0 ] [ " name" ] .as_str ( ) , one[ " cases" ] [ 0 ] [ " message" ] .as_str ( ) ) , ( Some ( " b" ) , Some ( " b broke" ) ) ) ;
2932
let ( _ , all) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/t/tests/{second} " ) , None , None ) .await;
2933
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & all) .unwrap ( ) [ " cases" ] .as_array ( ) .unwrap ( ) .len ( ) , 4 ) ;
2934
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 ↶
2935
2936
2937
let reports = oxygenhub:: junit:: on_bookmark( & h.app.db, row.id, " main" , 10 ) .await.unwrap ( ) ;
2938
let check = | state : String| {
2939
let app = h.app.clone ( ) ;
2940
async move { db:: hooks:: checks_for_state( & app.db, row.id, & state) .await.unwrap ( ) .into_iter ( ) .find ( | c | c.name = = " tests" ) .unwrap ( ) }
2941
} ;
2942
let c = check ( reports[ 1 ] .state.clone ( ) ) .await;
2943
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " failed" , " 1 failing; 1 new against main: t.suite::b" ) ) ;
2944
let c = check ( reports[ 0 ] .state.clone ( ) ) .await;
2945
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " passed" , " 4 passed" ) ) ;
2946
2947
2948
let ( _ , tab) = call ( & h.app, " GET" , " /andy/t/tests" , None , None ) .await;
2949
assert! (
2950
tab.contains ( " /andy/t/tests/case?classname=t.suite&name=b" ) | | tab.contains ( " /andy/t/tests/case?classname=t.suite&name=b" ) ,
2951
" linked"
2952
) ;
2953
let ( st, hist) = call ( & h.app, " GET" , " /andy/t/tests/case?classname=t.suite&name=b" , None , None ) .await;
2954
assert_eq! ( st, StatusCode:: OK , " {hist}" ) ;
2955
assert! ( hist.contains ( " 2 passed" ) & & hist.contains ( " 1 failed" ) & & hist.contains ( " b broke" ) , " {hist}" ) ;
2956
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 ↶
2957
}
67040b15 Andy McCall 25 days ago ↶
2958
2959
2960
2961
# [ tokio ::test ( flavor = " multi_thread" ) ]
2962
async fn a_job_no_runner_can_take_says_why ( ) {
2963
let h = hub ( ) .await;
2964
create ( & h.app, " andy" , " demo" ) .await;
2965
let url = format! ( " {} /andy/demo" , h.base) ;
2966
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2967
let c = client ( & url, work.path ( ) ) ;
2968
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
2969
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2970
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2971
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2972
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
2973
2974
2975
let r = db:: runners:: configured( & h.app.db, & blake3:: hash( b " elsewhere" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
2976
db:: runners:: seen( & h.app.db, r.id, None , None ) .await.unwrap ( ) ;
2977
let other = " ab" .repeat ( 32 ) ;
92a797b2 Andy McCall 25 days ago ↶
2978
db:: runners:: set_offer( & h.app.db, r.id, & [ " microvm" .to_string ( ) ] , Some ( & [ other] ) , false , " p" ) .await.unwrap ( ) ;
67040b15 Andy McCall 25 days ago ↶
2979
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
2980
2981
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
2982
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
2983
let build = v[ " jobs" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | j | j[ " name" ] = = " build" ) .unwrap ( ) .clone ( ) ;
2984
let why = build[ " waiting" ] .as_str ( ) .unwrap_or_default ( ) .to_string ( ) ;
2985
assert! ( why.contains ( " earth-9 does not hold image" ) & & why.contains ( & ROOTFS [ .. 12 ] ) , " {body}" ) ;
2986
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/1" , None , None ) .await;
2987
assert! ( page.contains ( " data-waiting" ) & & page.contains ( " does not hold image" ) , " the run page says it" ) ;
13b27cb2 Andy McCall 18 days ago ↶
2988
2989
2990
2991
let mut summary = String :: new( ) ;
2992
for _ in 0 .. 40 {
2993
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
2994
summary = checks.iter ( ) .find ( | c | c.name = = " flow/build" ) .unwrap ( ) .summary.clone ( ) ;
2995
if summary.contains ( " does not hold image" ) {
2996
break ;
2997
}
2998
let build = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) .into_iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) ;
2999
db:: flow:: set_waiting( & h.app.db, build.id, " " ) .await.unwrap ( ) ;
3000
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3001
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3002
}
3003
assert! ( summary.contains ( " does not hold image" ) , " {summary}" ) ;
67040b15 Andy McCall 25 days ago ↶
3004
}
92a797b2 Andy McCall 25 days ago ↶
3005
58cbf424 Andy McCall 18 days ago ↶
3006
3007
3008
3009
# [ tokio ::test ( flavor = " multi_thread" ) ]
3010
async fn a_job_waiting_for_a_busy_runner_says_so ( ) {
3011
let h = hub ( ) .await;
3012
create ( & h.app, " andy" , " demo" ) .await;
3013
let url = format! ( " {} /andy/demo" , h.base) ;
3014
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3015
let c = client ( & url, work.path ( ) ) ;
3016
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3017
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3018
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
3019
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3020
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
3021
3022
let r = db:: runners:: configured( & h.app.db, & blake3:: hash( b " busy" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
3023
db:: runners:: seen( & h.app.db, r.id, None , None ) .await.unwrap ( ) ;
3024
db:: runners:: set_offer( & h.app.db, r.id, & [ " microvm" .to_string ( ) ] , Some ( & [ ROOTFS .to_string ( ) ] ) , false , " p" ) .await.unwrap ( ) ;
3025
sqlx:: query(
3026
" UPDATE flow_jobs SET status = 'running', runner_id = $1, runner = 'earth-9', started_ms = $2 WHERE run_id = $3 AND name = 'lint'" ,
3027
)
3028
.bind ( r.id)
3029
.bind ( db:: now_ms( ) )
3030
.bind ( run.id)
3031
.execute ( & h.app.db)
3032
.await
3033
.unwrap ( ) ;
3034
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3035
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
3036
let build = jobs.iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) ;
3037
assert! ( build.waiting.contains ( " the runners that can take it are busy: earth-9 busy (1 of 1)" ) , " {}" , build.waiting) ;
3038
3039
sqlx:: query( " UPDATE flow_runners SET max_jobs = 2 WHERE id = $1" ) .bind ( r.id) .execute ( & h.app.db) .await.unwrap ( ) ;
3040
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3041
let build = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) .into_iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) ;
3042
assert! ( build.waiting.contains ( " earth-9 has a free slot (1 of 2) but not the memory or disk for it now" ) , " {}" , build.waiting) ;
3043
}
3044
794ff94f Andy McCall 12 days ago ↶
3045
3046
3047
# [ tokio ::test ( flavor = " multi_thread" ) ]
3048
async fn an_expedited_run_moves_the_queue_at_once ( ) {
3049
let h = hub ( ) .await;
3050
for name in [ " first" , " urgent" ] {
3051
create ( & h.app, " andy" , name) .await;
3052
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3053
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
3054
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3055
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3056
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
3057
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3058
3059
assert_eq! ( runs_of ( & h.app, " andy" , name) .await.len ( ) , 1 ) ;
3060
}
3061
3062
let r = db:: runners:: configured( & h.app.db, & blake3:: hash( b " busy" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
3063
db:: runners:: seen( & h.app.db, r.id, None , None ) .await.unwrap ( ) ;
3064
db:: runners:: set_offer( & h.app.db, r.id, & [ " microvm" .to_string ( ) ] , Some ( & [ ROOTFS .to_string ( ) ] ) , false , " p" ) .await.unwrap ( ) ;
3065
let first = runs_of ( & h.app, " andy" , " first" ) .await.remove ( 0 ) ;
3066
sqlx:: query(
3067
" UPDATE flow_jobs SET status = 'running', runner_id = $1, runner = 'earth-9', started_ms = $2 WHERE run_id = $3 AND name = 'lint'" ,
3068
)
3069
.bind ( r.id)
3070
.bind ( db:: now_ms( ) )
3071
.bind ( first.id)
3072
.execute ( & h.app.db)
3073
.await
3074
.unwrap ( ) ;
3075
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3076
let db = h.app.db.clone ( ) ;
3077
let build = | run : i64 | {
3078
let db = db.clone ( ) ;
3079
async move { db:: flow:: jobs( & db, run) .await.unwrap ( ) .into_iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) }
3080
} ;
3081
let urgent = runs_of ( & h.app, " andy" , " urgent" ) .await.remove ( 0 ) ;
3082
assert! ( build ( urgent.id) .await.waiting.contains ( " 1 ready job comes first" ) , " {}" , build ( urgent.id) .await.waiting) ;
3083
3084
3085
let res = oxygenhub:: router( h.app.clone ( ) )
3086
.oneshot (
3087
Request:: post( " /register" )
3088
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
3089
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
3090
.unwrap ( ) ,
3091
)
3092
.await
3093
.unwrap ( ) ;
3094
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
3095
let page = oxygenhub:: router( h.app.clone ( ) )
3096
.oneshot ( Request:: get( " /andy/urgent/flow/1" ) .header ( header:: COOKIE , cookie.clone ( ) ) .body ( Body:: empty( ) ) .unwrap ( ) )
3097
.await
3098
.unwrap ( ) ;
3099
let page = String :: from_utf8_lossy( & page.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
3100
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
3101
let csrf = page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
3102
let res = oxygenhub:: router( h.app.clone ( ) )
3103
.oneshot (
3104
Request:: post( " /andy/urgent/flow/1/expedite" )
3105
.header ( header:: COOKIE , cookie.clone ( ) )
3106
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
3107
.body ( Body:: from( format! ( " _csrf={csrf} &reason=incident" ) ) )
3108
.unwrap ( ) ,
3109
)
3110
.await
3111
.unwrap ( ) ;
3112
assert! ( res.status ( ) .is_redirection ( ) , " {}" , res.status ( ) ) ;
3113
assert! ( ! build ( urgent.id) .await.waiting.contains ( " come" ) , " first now: {}" , build ( urgent.id) .await.waiting) ;
3114
let w = build ( first.id) .await.waiting;
3115
assert! ( w.contains ( " 2 ready jobs come first (after expedited run andy/urgent #1)" ) , " {w}" ) ;
3116
}
3117
92a797b2 Andy McCall 25 days ago ↶
3118
async fn put_bytes ( app : & Shared, path : & str , bearer : & str , bytes : Vec< u8 > ) -> ( StatusCode, String) {
3119
let req = Request:: put( path)
3120
.header ( header:: AUTHORIZATION , format! ( " Bearer {bearer} " ) )
3121
.header ( header:: CONTENT_TYPE , " application/octet-stream" )
3122
.body ( Body:: from( bytes) )
3123
.unwrap ( ) ;
3124
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
3125
let st = res.status ( ) ;
3126
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
3127
}
3128
3129
3130
3131
3132
3133
# [ tokio ::test ( flavor = " multi_thread" ) ]
3134
async fn images_are_sent_by_chunk_and_offered_to_runners_that_fetch ( ) {
3135
let h = hub ( ) .await;
3136
3137
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3138
3139
let mut image: Vec< u8 > = ( 0 .. 400_000 u32 ) .map ( | i | ( i % 251 ) as u8 ) .collect ( ) ;
3140
image.extend ( std:: iter:: repeat_n( 0 u8 , 2 < < 20 ) ) ;
3141
image.extend ( ( 0 .. 100_000 u32 ) .map ( | i | ( i % 239 ) as u8 ) ) ;
3142
let mut chunks = std:: collections:: HashMap:: new( ) ;
3143
let m = jet:: images:: scan( & image[ .. ] , Some ( " x86_64" .into ( ) ) , | c , b | {
3144
chunks.insert ( c.hash ( ) .to_string ( ) , b.to_vec ( ) ) ;
3145
Ok ( ( ) )
3146
} )
3147
.unwrap ( ) ;
3148
let hash = m.hash.clone ( ) ;
3149
3150
3151
let ask = serde_json:: json! ( { " chunks" : m.needed ( ) } ) .to_string ( ) ;
3152
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/images/missing" , Some ( " site" ) , Some ( ask.clone ( ) ) ) .await;
3153
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3154
let missing: Vec< String> = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " missing" ]
3155
.as_array ( )
3156
.unwrap ( )
3157
.iter ( )
3158
.map ( | v | v.as_str ( ) .unwrap ( ) .to_string ( ) )
3159
.collect ( ) ;
3160
assert_eq! ( missing.len ( ) , m.needed ( ) .len ( ) ) ;
3161
let manifest = serde_json:: to_string( & m) .unwrap ( ) ;
3162
let ( st, body) = call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " site" ) , Some ( manifest.clone ( ) ) ) .await;
3163
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " was not sent" ) , " {st} {body}" ) ;
3164
3165
let first = & missing[ 0 ] ;
3166
assert_eq! ( put_bytes ( & h.app, & format! ( " /api/v1/flow/images/chunks/{first} " ) , " site" , vec! [ 1 , 2 , 3 ] ) .await.0 , StatusCode:: BAD_REQUEST ) ;
3167
assert_ne! (
3168
put_bytes ( & h.app, & format! ( " /api/v1/flow/images/chunks/{first} " ) , " runner-secret" , chunks[ first] .clone ( ) ) .await.0 ,
3169
StatusCode:: NO_CONTENT
3170
) ;
3171
for c in & missing {
3172
let ( st, body) = put_bytes ( & h.app, & format! ( " /api/v1/flow/images/chunks/{c} " ) , " site" , chunks[ c] .clone ( ) ) .await;
3173
assert_eq! ( st, StatusCode:: NO_CONTENT , " {body}" ) ;
3174
}
3175
let ( _ , body) = call ( & h.app, " POST" , " /api/v1/flow/images/missing" , Some ( " site" ) , Some ( ask) ) .await;
3176
assert! ( body.contains ( " \" missing\" :[]" ) , " {body}" ) ;
750579ec Andy McCall 24 days ago ↶
3177
3178
let many: Vec< String> = ( 0 .. 60_000 u32 ) .map ( | i | blake3:: hash( & i.to_le_bytes ( ) ) .to_hex ( ) .to_string ( ) ) .collect ( ) ;
3179
let ( st, body) =
3180
call ( & h.app, " POST" , " /api/v1/flow/images/missing" , Some ( " site" ) , Some ( serde_json:: json! ( { " chunks" : many } ) .to_string ( ) ) ) .await;
3181
assert_eq! ( st, StatusCode:: OK , " {}" , & body[ .. body.len ( ) .min ( 200 ) ] ) ;
3182
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " missing" ] .as_array ( ) .unwrap ( ) .len ( ) , 60_000 ) ;
92a797b2 Andy McCall 25 days ago ↶
3183
3184
let mut wrong = m.clone ( ) ;
3185
wrong.hash = " ab" .repeat ( 32 ) ;
3186
let ( st, _ ) =
3187
call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{} " , wrong.hash) , Some ( " site" ) , Some ( serde_json:: to_string( & wrong) .unwrap ( ) ) )
3188
.await;
3189
assert_eq! ( st, StatusCode:: CONFLICT , " chunks that make another image" ) ;
3190
let ( st, body) = call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " site" ) , Some ( manifest) ) .await;
3191
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
3192
3193
3194
let ( st, body) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " runner-secret" ) , None ) .await;
3195
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3196
let got: jet:: images:: Manifest = serde_json:: from_str( & body) .unwrap ( ) ;
3197
assert_eq! ( got, m) ;
3198
let dir = tempfile:: tempdir( ) .unwrap ( ) ;
3199
let out = dir.path ( ) .join ( " img.ext4" ) ;
3200
let mut fetched = Vec :: new( ) ;
3201
for c in got.needed ( ) {
3202
let req = Request:: get( format! ( " /api/v1/flow/images/chunks/{c} " ) )
3203
.header ( header:: AUTHORIZATION , " Bearer runner-secret" )
3204
.body ( Body:: empty( ) )
3205
.unwrap ( ) ;
3206
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
3207
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
3208
let bytes = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ;
3209
fetched.push ( ( c.to_string ( ) , bytes) ) ;
3210
}
3211
jet:: images:: assemble( & got, & out, | c | Ok ( fetched.iter ( ) .find ( | ( h , _) | h = = c.hash ( ) ) .unwrap ( ) .1. clone ( ) ) ) .unwrap ( ) ;
3212
assert_eq! ( std:: fs:: read( & out) .unwrap ( ) , image) ;
3213
assert_eq! (
3214
call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{} " , " cd" .repeat ( 32 ) ) , Some ( " runner-secret" ) , None ) .await.0 ,
3215
StatusCode:: NOT_FOUND
3216
) ;
3217
assert_ne! ( call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{hash} " ) , None , None ) .await.0 , StatusCode:: OK , " not anonymous" ) ;
3218
3219
3220
3221
create ( & h.app, " andy" , " demo" ) .await;
3222
let url = format! ( " {} /andy/demo" , h.base) ;
3223
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3224
let c = client ( & url, work.path ( ) ) ;
3225
write (
3226
work.path ( ) ,
3227
" .flow/pipeline.kdl" ,
3228
& format! ( " schema-version 1\n sandbox \" img\" {{ \n rootfs \" cas://blob/{hash} \" \n }} \n job \" build\" {{ \n sandbox \" img\" \n exec \" make\" \n }} \n " ) ,
3229
) ;
3230
c.commit ( " Build on the image" , & [ ] ) .unwrap ( ) ;
3231
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3232
runs_of ( & h.app, " andy" , " demo" ) .await;
1c8276b9 Andy McCall 25 days ago ↶
3233
3234
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
3235
let run1 = db:: flow:: run( & h.app.db, row.id, 1 ) .await.unwrap ( ) .unwrap ( ) ;
3236
let checks = checks_when ( & h.app, row.id, & run1.state, 1 ) .await;
3237
assert! ( checks.iter ( ) .all ( | c | c.name ! = " pipeline/images" ) , " {checks:?}" ) ;
92a797b2 Andy McCall 25 days ago ↶
3238
let ask = | fetches : bool | format! ( r # "{{ "runner":"test-1","runtimes":["microvm"],"images":[],"fetches_images":{fetches} }} "# ) ;
3239
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( false ) ) ) .await;
3240
assert_eq! ( st, StatusCode:: NO_CONTENT , " it lacks the image and does not fetch" ) ;
3241
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3242
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
3243
assert! ( body.contains ( " does not hold image" ) , " {body}" ) ;
3244
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( true ) ) ) .await;
3245
assert_eq! ( st, StatusCode:: OK , " it fetches, and the hub holds the image: {body}" ) ;
7829b7c7 Andy McCall 25 days ago ↶
3246
3247
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
3248
assert! ( ! body.contains ( " waiting" ) , " a running job keeps no waiting reason: {body}" ) ;
92a797b2 Andy McCall 25 days ago ↶
3249
}
40dd3f86 Andy McCall 25 days ago ↶
3250
3251
3252
3253
3254
# [ tokio ::test ( flavor = " multi_thread" ) ]
3255
async fn coverage_uploaded_from_a_workstation_says_so ( ) {
3256
let h = hub ( ) .await;
3257
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3258
create ( & h.app, " andy" , " vk" ) .await;
3259
let url = format! ( " {} /andy/vk" , h.base) ;
3260
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3261
let c = client ( & url, work.path ( ) ) ;
3262
write ( work.path ( ) , " src/game.c" , " int a;\n int b;\n int c;\n int d;\n " ) ;
3263
c.commit ( " Game" , & [ ] ) .unwrap ( ) ;
3264
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3265
3266
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 " ;
3267
let body = serde_json:: json! ( { " bookmark" : " main" , " content" : lcov } ) .to_string ( ) ;
3268
let ( st, out) = call ( & h.app, " POST" , " /api/v1/repos/andy/vk/coverage" , Some ( " site" ) , Some ( body) ) .await;
3269
assert_eq! ( st, StatusCode:: CREATED , " {out}" ) ;
3270
let v: serde_json:: Value = serde_json:: from_str( & out) .unwrap ( ) ;
3271
assert_eq! ( ( v[ " uploaded_by" ] .as_str ( ) , v[ " lines_hit" ] .as_i64 ( ) , v[ " lines_found" ] .as_i64 ( ) ) , ( Some ( " andy" ) , Some ( 3 ) , Some ( 4 ) ) ) ;
3272
assert_eq! ( v[ " dropped" ] , 1 , " a file outside the state is left out" ) ;
3273
assert_eq! ( v[ " bookmark" ] , " main" ) ;
3274
3275
let ( _ , page) = call ( & h.app, " GET" , " /andy/vk/coverage" , None , None ) .await;
3276
assert! ( page.contains ( " uploaded by andy" ) & & page.contains ( " 75.0%" ) , " the Coverage page says where it came from" ) ;
3277
let ( _ , page) = call ( & h.app, " GET" , " /andy/vk/reports" , None , None ) .await;
3278
assert! ( page.contains ( " uploaded by andy" ) , " and the Reports card" ) ;
3279
let row = db:: repos:: get( & h.app.db, " andy" , " vk" ) .await.unwrap ( ) .unwrap ( ) ;
3280
let state = v[ " state" ] .as_str ( ) .unwrap ( ) ;
3281
let checks = db:: hooks:: checks_for_state( & h.app.db, row.id, state) .await.unwrap ( ) ;
3282
assert! ( checks.iter ( ) .all ( | c | c.name ! = " coverage" ) , " an upload sets no check: {checks:?}" ) ;
3283
3284
3285
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/vk/coverage" , None , Some ( r # "{"content":"x"}"# .into ( ) ) ) .await;
3286
assert_ne! ( st, StatusCode:: CREATED ) ;
3287
let ( st, _ ) = call (
3288
& h.app,
3289
" POST" ,
3290
" /api/v1/repos/andy/vk/coverage" ,
3291
Some ( " site" ) ,
3292
Some ( r # "{"bookmark":"nope","content":"SF:a\nend_of_record\n"}"# .into ( ) ) ,
3293
)
3294
.await;
3295
assert_eq! ( st, StatusCode:: BAD_REQUEST ) ;
3296
}
2fb70d01 Andy McCall 25 days ago ↶
3297
3298
3299
# [ tokio ::test ( flavor = " multi_thread" ) ]
3300
async fn a_runners_facts_are_kept_from_its_claim ( ) {
3301
let h = hub ( ) .await;
3302
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3303
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"}}"# ;
3304
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body.into ( ) ) ) .await;
3305
assert_eq! ( st, StatusCode:: NO_CONTENT , " nothing to run, but the facts are kept" ) ;
3306
let ( st, out) = call ( & h.app, " GET" , " /api/v1/runners" , Some ( " site" ) , None ) .await;
3307
assert_eq! ( st, StatusCode:: OK , " {out}" ) ;
3308
let v: serde_json:: Value = serde_json:: from_str( & out) .unwrap ( ) ;
3309
let r = & v[ 0 ] ;
3310
assert_eq! (
3311
( r[ " speed" ] .as_i64 ( ) , r[ " speed_class" ] .as_i64 ( ) , r[ " free_disk_mib" ] .as_i64 ( ) , r[ " load_x100" ] .as_i64 ( ) ) ,
3312
( Some ( 100 ) , Some ( 3 ) , Some ( 42000 ) , Some ( 112 ) )
3313
) ;
3314
let row = db:: runners:: list( & h.app.db) .await.unwrap ( ) .remove ( 0 ) ;
3315
assert_eq! ( row.facts_line ( ) , " speed 100 (class 3) · load 1.12 · 41 GiB free" ) ;
3316
}
44a3d2eb Andy McCall 25 days ago ↶
3317
3318
3319
3320
# [ tokio ::test ( flavor = " multi_thread" ) ]
3321
async fn a_jobs_next_run_needs_the_disk_its_last_took ( ) {
3322
let h = hub ( ) .await;
3323
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3324
create ( & h.app, " andy" , " demo" ) .await;
3325
let url = format! ( " {} /andy/demo" , h.base) ;
3326
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3327
let c = client ( & url, work.path ( ) ) ;
3328
write (
3329
work.path ( ) ,
3330
" .flow/pipeline.kdl" ,
3331
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
3332
) ;
3333
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
3334
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3335
runs_of ( & h.app, " andy" , " demo" ) .await;
3336
let claim_with = | free : u64 | format! ( r # "{{ "runner":"test-1","runtimes":["microvm"],"facts":{{ "free_disk_mib":{free} }} }} "# ) ;
3337
3338
3339
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( claim_with ( 500 ) ) ) .await;
3340
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3341
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3342
let done = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " peak_disk_mib" : 8000 , " peak_memory_mib" : 900 } ) ;
3343
assert_eq! ( finish ( & h.app, id, done) .await, StatusCode:: NO_CONTENT ) ;
3344
let job = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
3345
assert_eq! ( ( job.peak_disk_mib, job.peak_memory_mib) , ( Some ( 8000 ) , Some ( 900 ) ) ) ;
3346
3347
3348
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
3349
assert_eq! ( st, StatusCode:: CREATED ) ;
3350
let repo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
3351
let run2 = db:: flow:: run( & h.app.db, repo.id, 2 ) .await.unwrap ( ) .unwrap ( ) ;
3352
let next = db:: flow:: jobs( & h.app.db, run2.id) .await.unwrap ( ) .remove ( 0 ) ;
3353
assert_eq! ( next.need_disk_mib, Some ( 8000 + 2000 + 64 ) ) ;
3354
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( claim_with ( 9000 ) ) ) .await;
3355
assert_eq! ( st, StatusCode:: NO_CONTENT , " 9000 MiB free is not enough" ) ;
3356
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3357
let waiting = db:: flow:: job( & h.app.db, next.id) .await.unwrap ( ) .unwrap ( ) .waiting;
3358
assert! ( waiting.contains ( " 8.8 GiB free; the job needs ~9.8 GiB" ) , " {waiting}" ) ;
3359
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( claim_with ( 20_000 ) ) ) .await;
3360
assert_eq! ( st, StatusCode:: OK , " a roomier runner takes it: {body}" ) ;
3361
}
501d8b26 Andy McCall 25 days ago ↶
3362
3363
3364
3365
3366
# [ tokio ::test ( flavor = " multi_thread" ) ]
3367
async fn a_job_waits_briefly_for_a_runner_that_holds_its_image ( ) {
3368
let h = hub ( ) .await;
3369
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3370
3371
db:: runners:: configured( & h.app.db, & blake3:: hash( b " other-secret" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
3372
let ask = | images : & str | {
3373
format! (
3374
r # "{{ "runner":"r","runtimes":["microvm"],"images":[{images} ],"fetches_images":true,"facts":{{ "speed":100,"speed_class":3,"free_disk_mib":50000}} }} "#
3375
)
3376
} ;
3377
let holder = ask ( & format! ( " \" {ROOTFS} \" " ) ) ;
3378
3379
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder.clone ( ) ) ) .await;
3380
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
3381
3b1bc27f Andy McCall 20 days ago
3382
sqlx:: query( " INSERT INTO jet_images (hash, size, arch, chunks, uploader, created_ms) VALUES ($1, 1, 'x86_64', 1, 'andy', 0)" )
501d8b26 Andy McCall 25 days ago ↶
3383
.bind ( ROOTFS )
3384
.execute ( & h.app.db)
3385
.await
3386
.unwrap ( ) ;
3387
3388
create ( & h.app, " andy" , " demo" ) .await;
3389
let url = format! ( " {} /andy/demo" , h.base) ;
3390
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3391
let c = client ( & url, work.path ( ) ) ;
3392
write (
3393
work.path ( ) ,
3394
" .flow/pipeline.kdl" ,
3395
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
3396
) ;
3397
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
3398
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3399
runs_of ( & h.app, " andy" , " demo" ) .await;
3400
3401
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( " " ) ) ) .await;
3402
assert_eq! ( st, StatusCode:: NO_CONTENT , " held for the runner that holds the image" ) ;
3403
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder) ) .await;
3404
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3405
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3406
let placed = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .placed;
3407
assert! ( placed.starts_with ( " holds the image, idle, speed 100 (class 3)" ) , " {placed}" ) ;
3408
assert! ( placed.contains ( " held for a better runner" ) , " {placed}" ) ;
3409
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
3410
assert! ( body.contains ( " \" placed\" :\" holds the image" ) , " {body}" ) ;
3411
}
20b8fe85 Andy McCall 25 days ago ↶
3412
4fa0da12 Andy McCall 23 days ago ↶
3413
3414
3415
# [ tokio ::test ( flavor = " multi_thread" ) ]
3416
async fn a_job_waits_briefly_for_the_runner_holding_its_build_cache ( ) {
3417
let h = hub ( ) .await;
3418
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3419
db:: runners:: configured( & h.app.db, & blake3:: hash( b " other-secret" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
3420
let slot = flow_core:: api:: cache_slot( " andy/demo" , " build" ) ;
3421
let ask = | caches : & str | {
3422
format! (
3423
r # "{{ "runner":"r","runtimes":["microvm"],"images":["{ROOTFS} "],"fetches_images":true,"caches":[{caches} ],"facts":{{ "speed":100,"speed_class":3,"free_disk_mib":50000}} }} "#
3424
)
3425
} ;
3426
let holder = ask ( & format! ( " \" {slot} \" " ) ) ;
3427
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder.clone ( ) ) ) .await;
3428
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
3429
3430
create ( & h.app, " andy" , " demo" ) .await;
3431
let url = format! ( " {} /andy/demo" , h.base) ;
3432
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3433
let c = client ( & url, work.path ( ) ) ;
3434
write (
3435
work.path ( ) ,
3436
" .flow/pipeline.kdl" ,
3437
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
3438
) ;
3439
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
3440
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3441
runs_of ( & h.app, " andy" , " demo" ) .await;
3442
3443
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( " " ) ) ) .await;
3444
assert_eq! ( st, StatusCode:: NO_CONTENT , " held for the runner holding the build cache" ) ;
3445
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder) ) .await;
3446
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3447
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3448
let placed = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .placed;
3449
assert! ( placed.starts_with ( " holds its build cache, holds the image" ) , " {placed}" ) ;
3450
}
3451
c2be148c Andy McCall 23 days ago ↶
3452
3453
3454
# [ tokio ::test ( flavor = " multi_thread" ) ]
3455
async fn a_job_waits_briefly_for_the_fresher_build_cache ( ) {
3456
let h = hub ( ) .await;
3457
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3458
db:: runners:: configured( & h.app.db, & blake3:: hash( b " other-secret" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
3459
let slot = flow_core:: api:: cache_slot( " andy/demo" , " build" ) ;
3460
let now = db:: now_ms( ) ;
3461
let ask = | kept : i64 | {
3462
format! (
3463
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}} }} "#
3464
)
3465
} ;
3466
3467
let fresh = ask ( now - 60_000 ) ;
3468
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( fresh.clone ( ) ) ) .await;
3469
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
3470
3471
create ( & h.app, " andy" , " demo" ) .await;
3472
let url = format! ( " {} /andy/demo" , h.base) ;
3473
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3474
let c = client ( & url, work.path ( ) ) ;
3475
write (
3476
work.path ( ) ,
3477
" .flow/pipeline.kdl" ,
3478
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
3479
) ;
3480
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
3481
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3482
runs_of ( & h.app, " andy" , " demo" ) .await;
3483
3484
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( now - 86_400_000 ) ) ) .await;
3485
assert_eq! ( st, StatusCode:: NO_CONTENT , " held for the fresher copy" ) ;
3486
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( fresh) ) .await;
3487
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3488
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3489
let placed = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .placed;
3490
assert! ( placed.starts_with ( " holds its build cache (kept 1 min ago), holds the image" ) , " {placed}" ) ;
3491
}
3492
20b8fe85 Andy McCall 25 days ago ↶
3493
3494
3495
3496
# [ tokio ::test ( flavor = " multi_thread" ) ]
3497
async fn report_files_expire_under_retention ( ) {
3498
let h = hub ( ) .await;
3499
create ( & h.app, " andy" , " demo" ) .await;
3500
let repo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
3501
let add = | bookmark : & 'static str , at : i64 | {
3502
let db = h.app.db.clone ( ) ;
3503
async move {
3504
let id: i64 = sqlx:: query_scalar(
3505
" INSERT INTO reports (repo_id, name, kind, title, summary, context, job, bookmark, state, files, bytes, created_ms) \
3b1bc27f Andy McCall 20 days ago
3506
VALUES ($1, 'perf', 'performance', 'Perf', '', '{}', 'perf', $2, 's', 1, 1, $3) RETURNING id" ,
20b8fe85 Andy McCall 25 days ago ↶
3507
)
3508
.bind ( repo.id)
3509
.bind ( bookmark)
3510
.bind ( at)
3511
.fetch_one ( & db)
3512
.await
3513
.unwrap ( ) ;
3b1bc27f Andy McCall 20 days ago
3514
sqlx:: query( " INSERT INTO report_files (report_id, path, blob, size) VALUES ($1, 'report.toml', $2, 1)" )
20b8fe85 Andy McCall 25 days ago ↶
3515
.bind ( id)
3516
.bind ( format! ( " {:064x}" , id) )
3517
.execute ( & db)
3518
.await
3519
.unwrap ( ) ;
3b1bc27f Andy McCall 20 days ago
3520
sqlx:: query( " INSERT INTO report_metrics (report_id, key, value, unit, better) VALUES ($1, 'ms', 1, '', '')" )
20b8fe85 Andy McCall 25 days ago ↶
3521
.bind ( id)
3522
.execute ( & db)
3523
.await
3524
.unwrap ( ) ;
3525
id
3526
}
3527
} ;
3528
let main_old = add ( " main" , 1 ) .await;
3529
add ( " main" , 2 ) .await;
3530
let mut feature = Vec :: new( ) ;
3531
for at in 0 .. 52 {
3532
feature.push ( add ( " feature" , 10 + at) .await) ;
3533
}
3534
assert_eq! ( oxygenhub:: reports:: expire( & h.app.db) .await.unwrap ( ) , 2 , " the two oldest feature runs" ) ;
3535
let files = | id : i64 | {
3536
let db = h.app.db.clone ( ) ;
3537
async move {
3b1bc27f Andy McCall 20 days ago
3538
sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM report_files WHERE report_id = $1" ) .bind ( id) .fetch_one ( & db) .await.unwrap ( )
20b8fe85 Andy McCall 25 days ago ↶
3539
}
3540
} ;
3541
assert_eq! ( ( files ( feature[ 0 ] ) .await, files ( feature[ 1 ] ) .await, files ( feature[ 2 ] ) .await) , ( 0 , 0 , 1 ) ) ;
3542
assert_eq! ( files ( main_old) .await, 1 , " the default bookmark keeps every run" ) ;
3543
assert_eq! ( oxygenhub:: reports:: metrics( & h.app.db, feature[ 0 ] ) .await.unwrap ( ) .len ( ) , 1 , " metrics are kept" ) ;
3544
assert_eq! ( oxygenhub:: reports:: expire( & h.app.db) .await.unwrap ( ) , 0 , " once" ) ;
3545
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/reports/perf?id={} " , feature[ 0 ] ) , None , None ) .await;
3546
assert! ( page.contains ( " files have expired" ) , " the page says so" ) ;
1c8276b9 Andy McCall 25 days ago ↶
3547
}
3548
3549
3550
3551
3552
# [ tokio ::test ( flavor = " multi_thread" ) ]
3553
async fn a_pipeline_pinning_an_image_nobody_holds_fails_a_check ( ) {
3554
let h = hub ( ) .await;
3555
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3556
3557
let none = r # "{"runner":"test-1","runtimes":["microvm"],"images":[]}"# ;
3558
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( none.into ( ) ) ) .await;
3559
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
3560
create ( & h.app, " andy" , " demo" ) .await;
3561
let url = format! ( " {} /andy/demo" , h.base) ;
3562
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3563
let c = client ( & url, work.path ( ) ) ;
3564
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3565
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3566
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
3567
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3568
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
3569
let checks = checks_when ( & h.app, run.repo_id, & run.state, 4 ) .await;
3570
let check = checks.iter ( ) .find ( | c | c.name = = " pipeline/images" ) .expect ( " an images check" ) ;
3571
assert! ( check.status = = " failed" & & check.summary.contains ( & ROOTFS [ .. 12 ] ) & & check.summary.contains ( " flow rootfs push" ) , " {check:?}" ) ;
3572
3573
let holds = format! ( r # "{{ "runner":"test-1","runtimes":["microvm"],"images":["{ROOTFS} "]}} "# ) ;
3574
call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( holds) ) .await;
3575
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
3576
assert_eq! ( st, StatusCode:: CREATED ) ;
3577
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
3578
let check = checks.iter ( ) .find ( | c | c.name = = " pipeline/images" ) .unwrap ( ) ;
3579
assert_eq! ( check.status, " passed" , " {check:?}" ) ;
20b8fe85 Andy McCall 25 days ago ↶
3580
}
a46d15e7 Andy McCall 24 days ago ↶
3581
3582
3583
3584
3585
# [ tokio ::test ( flavor = " multi_thread" ) ]
3586
async fn the_tracker_names_peers_in_the_same_site_and_signs_a_ticket ( ) {
3587
let h = hub ( ) .await;
3588
let app = & h.app;
3589
let image = " ab" .repeat ( 32 ) ;
3590
let claim_as = | token : & 'static str , name : & 'static str , body : serde_json:: Value| async move {
3591
let mut b = serde_json:: json! ( { " runner" : name, " runtimes" : [ " microvm" ] } ) ;
3592
b.as_object_mut ( ) .unwrap ( ) .extend ( body.as_object ( ) .unwrap ( ) .clone ( ) ) ;
3593
let ( st, out) = call ( app, " POST" , " /api/v1/flow/claim" , Some ( token) , Some ( b.to_string ( ) ) ) .await;
3594
assert_eq! ( st, StatusCode:: NO_CONTENT , " {out}" ) ;
3595
} ;
3596
let announce = | token : & 'static str , state : & 'static str | {
3597
let body = serde_json:: json! ( { " image" : image, " state" : state } ) .to_string ( ) ;
3598
async move { call ( app, " POST" , " /api/v1/flow/swarm/announce" , Some ( token) , Some ( body) ) .await }
3599
} ;
3600
6adcd68b Andy McCall 24 days ago ↶
3601
claim_as (
3602
" runner-secret" ,
3603
" a" ,
3604
serde_json:: json! ( { " images" : [ image] , " swarm" : { " addr" : " 10.0.0.1:7401" , " site" : " lab" , " cert" : " 11" .repeat ( 32 ) } } ) ,
3605
)
3606
.await;
3607
claim_as (
3608
" runner-two" ,
3609
" b" ,
3610
serde_json:: json! ( { " images" : [ ] , " swarm" : { " addr" : " 10.0.0.2:7401" , " site" : " lab" , " cert" : " 22" .repeat ( 32 ) } } ) ,
3611
)
3612
.await;
3613
claim_as (
3614
" runner-three" ,
3615
" c" ,
3616
serde_json:: json! ( { " images" : [ ] , " swarm" : { " addr" : " 10.0.0.3:7401" , " site" : " lab" , " cert" : " 33" .repeat ( 32 ) } } ) ,
3617
)
3618
.await;
a46d15e7 Andy McCall 24 days ago ↶
3619
3620
let ( st, body) = announce ( " runner-two" , " fetching" ) .await;
3621
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3622
let a: flow_core:: api:: Announced = serde_json:: from_str( & body) .unwrap ( ) ;
3623
let names: Vec< ( & str , bool ) > = a.peers.iter ( ) .map ( | p | ( p.addr.as_str ( ) , p.complete) ) .collect ( ) ;
3624
assert_eq! ( names, vec! [ ( " 10.0.0.1:7401" , true ) ] , " only A holds it so far" ) ;
6adcd68b Andy McCall 24 days ago ↶
3625
assert_eq! ( a.peers[ 0 ] .cert, " 11" .repeat ( 32 ) , " with the fingerprint peers check it by" ) ;
a46d15e7 Andy McCall 24 days ago ↶
3626
let t = jet:: swarm:: Ticket:: open( & a.ticket, & app.hub_key.identity, oxygenhub:: db:: now_ms( ) as u64 ) .unwrap ( ) ;
3627
assert_eq! ( ( t.name.as_str ( ) , t.site.as_str ( ) ) , ( " b" , " lab" ) ) ;
3628
3629
3630
let ( _ , body) = announce ( " runner-three" , " fetching" ) .await;
3631
let a: flow_core:: api:: Announced = serde_json:: from_str( & body) .unwrap ( ) ;
3632
let mut peers: Vec< ( & str , bool ) > = a.peers.iter ( ) .map ( | p | ( p.addr.as_str ( ) , p.complete) ) .collect ( ) ;
3633
peers.sort ( ) ;
3634
assert_eq! ( peers, vec! [ ( " 10.0.0.1:7401" , true ) , ( " 10.0.0.2:7401" , false ) ] ) ;
3635
3636
3637
announce ( " runner-two" , " done" ) .await;
6adcd68b Andy McCall 24 days ago ↶
3638
claim_as (
3639
" runner-three" ,
3640
" c" ,
3641
serde_json:: json! ( { " images" : [ ] , " swarm" : { " addr" : " 10.0.0.3:7401" , " site" : " elsewhere" , " cert" : " 33" .repeat ( 32 ) } } ) ,
3642
)
3643
.await;
a46d15e7 Andy McCall 24 days ago ↶
3644
let ( _ , body) = announce ( " runner-three" , " fetching" ) .await;
3645
let a: flow_core:: api:: Announced = serde_json:: from_str( & body) .unwrap ( ) ;
3646
assert! ( a.peers.is_empty ( ) , " {:?}" , a.peers) ;
6adcd68b Andy McCall 24 days ago ↶
3647
3648
3649
claim_as ( " runner-secret" , " a" , serde_json:: json! ( { " images" : [ image] , " swarm" : { " addr" : " 10.0.0.1:7401" , " site" : " elsewhere" } } ) )
3650
.await;
3651
let ( st, _ ) = announce ( " runner-secret" , " fetching" ) .await;
3652
assert_eq! ( st, StatusCode:: CONFLICT ) ;
a46d15e7 Andy McCall 24 days ago ↶
3653
3654
3655
claim_as ( " runner-two" , " b" , serde_json:: json! ( { " images" : [ ] } ) ) .await;
3656
let ( st, body) = announce ( " runner-two" , " fetching" ) .await;
3657
assert_eq! ( st, StatusCode:: CONFLICT ) ;
3658
assert! ( body.contains ( " [runner.swarm]" ) , " {body}" ) ;
3659
let ( st, _ ) =
3660
call ( app, " POST" , " /api/v1/flow/swarm/announce" , Some ( " runner-secret" ) , Some ( r # "{"image":"nope","state":"fetching"}"# .into ( ) ) )
3661
.await;
3662
assert_eq! ( st, StatusCode:: BAD_REQUEST ) ;
3663
let ( st, _ ) = call (
3664
app,
3665
" POST" ,
3666
" /api/v1/flow/swarm/announce" ,
3667
None ,
3668
Some ( serde_json:: json! ( { " image" : image, " state" : " fetching" } ) .to_string ( ) ) ,
3669
)
3670
.await;
3671
assert_eq! ( st, StatusCode:: UNAUTHORIZED ) ;
3672
}
6ca0b971 Andy McCall 24 days ago ↶
3673
3674
3675
# [ tokio ::test ( flavor = " multi_thread" ) ]
3676
async fn the_images_page_says_where_each_image_is_and_what_uses_it ( ) {
3677
let h = hub ( ) .await;
3678
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3679
create ( & h.app, " andy" , " demo" ) .await;
3680
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3681
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
3682
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3683
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3684
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
3685
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3686
3687
let held = " cd" .repeat ( 32 ) ;
3688
let kept = " ef" .repeat ( 32 ) ;
3689
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 ) } } ) ;
3690
let ( st, out) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body.to_string ( ) ) ) .await;
3691
assert! ( st = = StatusCode:: NO_CONTENT | | st = = StatusCode:: OK , " {st} {out}" ) ;
3b1bc27f Andy McCall 20 days ago
3692
sqlx:: query( " INSERT INTO jet_images (hash, size, arch, chunks, uploader, created_ms) VALUES ($1, 1048576, 'x86_64', 9, 'andy', 1)" )
6ca0b971 Andy McCall 24 days ago ↶
3693
.bind ( & kept)
3694
.execute ( & h.app.db)
3695
.await
3696
.unwrap ( ) ;
3697
3698
let res = oxygenhub:: router( h.app.clone ( ) )
3699
.oneshot (
3700
Request:: post( " /register" )
3701
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 20 days ago
3702
.body ( Body:: from( " username=carol&email=carol%40example.com&password=correct-horse-battery&setup=test-setup" ) )
6ca0b971 Andy McCall 24 days ago ↶
3703
.unwrap ( ) ,
3704
)
3705
.await
3706
.unwrap ( ) ;
3707
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
3708
let carol = db:: accounts:: account_by_name( & h.app.db, " carol" ) .await.unwrap ( ) .unwrap ( ) ;
3709
db:: accounts:: set_admin( & h.app.db, carol.id, true ) .await.unwrap ( ) ;
3710
let res = oxygenhub:: router( h.app.clone ( ) )
3711
.oneshot ( Request:: get( " /admin/runners/images" ) .header ( header:: COOKIE , cookie) .body ( Body:: empty( ) ) .unwrap ( ) )
3712
.await
3713
.unwrap ( ) ;
3714
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
3715
let page = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
3716
let row = | hash : & str | {
3717
let at = page.find ( & format! ( " title=\" {hash} \" " ) ) .unwrap_or_else ( | | panic! ( " {hash} is not on the page" ) ) ;
3718
let end = page[ at.. ] .find ( " </tr>" ) .unwrap ( ) ;
3719
page[ at.. at + end] .to_string ( )
3720
} ;
3721
let pinned = row ( ROOTFS ) ;
3722
assert! ( pinned.contains ( " nowhere" ) & & pinned.contains ( " andy/demo" ) & & pinned.contains ( " 3 jobs" ) , " {pinned}" ) ;
3723
let on_runner = row ( & held) ;
3724
assert! ( on_runner.contains ( " img-holds" ) & & on_runner.contains ( " earth-1" ) & & ! on_runner.contains ( " nowhere" ) , " {on_runner}" ) ;
3725
let on_hub = row ( & kept) ;
3726
assert! ( on_hub.contains ( " img-hub" ) & & on_hub.contains ( " 1.0 MiB" ) & & on_hub.contains ( " x86_64" ) , " {on_hub}" ) ;
3727
assert! ( page.contains ( " earth-1 (home)" ) , " the swarm is named" ) ;
6246d1e5 Andy McCall 23 days ago ↶
3728
3729
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 ↶
3730
assert_eq! ( call ( & h.app, " GET" , " /admin/runners/images" , None , None ) .await.0 , StatusCode:: NOT_FOUND , " administrators only" ) ;
3731
}
f677d627 Andy McCall 24 days ago ↶
3732
3733
3734
3735
3736
# [ tokio ::test ( flavor = " multi_thread" ) ]
3737
async fn consumers_of_a_subrepo_are_indexed_with_how_far_behind_they_are ( ) {
3738
let h = hub ( ) .await;
e7158350 Andy McCall 24 days ago ↶
3739
3740
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
f677d627 Andy McCall 24 days ago ↶
3741
create ( & h.app, " andy" , " api" ) .await;
3742
create ( & h.app, " andy" , " game" ) .await;
3743
let api_work = tempfile:: tempdir( ) .unwrap ( ) ;
3744
let api = client ( & format! ( " {} /andy/api" , h.base) , api_work.path ( ) ) ;
3745
write ( api_work.path ( ) , " api.h" , " v1\n " ) ;
3746
api.snapshot ( " t" ) .unwrap ( ) ;
3747
api.commit ( " api v1" , & [ ] ) .unwrap ( ) ;
3748
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3749
3750
let game_work = tempfile:: tempdir( ) .unwrap ( ) ;
3751
let game = client ( & format! ( " {} /andy/game" , h.base) , game_work.path ( ) ) ;
3752
game.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
3753
write ( game_work.path ( ) , " game.c" , " int main;\n " ) ;
3754
game.snapshot ( " t" ) .unwrap ( ) ;
3755
game.commit ( " game on api v1" , & [ ] ) .unwrap ( ) ;
3756
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3757
3758
let consumers = | | async {
3759
let ( st, body) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , Some ( " site" ) , None ) .await;
3760
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3761
serde_json:: from_str:: < Vec< serde_json:: Value> > ( & body) .unwrap ( )
3762
} ;
3763
3764
3765
let settle = | want : & 'static ( dyn Fn( & [serde_json:: Value]) -> bool + Sync) | {
3766
let consumers = & consumers;
3767
async move {
3768
for _ in 0 .. 100 {
3769
let list = consumers ( ) .await;
3770
if want ( & list) {
3771
return list;
3772
}
3773
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3774
}
3775
consumers ( ) .await
3776
}
3777
} ;
3778
let list = settle ( & | l| ! l.is_empty ( ) ) .await;
3779
assert_eq! ( list.len ( ) , 1 , " {list:?}" ) ;
3780
assert_eq! (
3781
( list[ 0 ] [ " name" ] .as_str ( ) , list[ 0 ] [ " path" ] .as_str ( ) , list[ 0 ] [ " bookmark" ] .as_str ( ) ) ,
3782
( Some ( " game" ) , Some ( " deps/api" ) , Some ( " main" ) )
3783
) ;
3784
assert_eq! ( list[ 0 ] [ " behind" ] , 0 ) ;
3785
3786
3787
write ( api_work.path ( ) , " api.c" , " v2\n " ) ;
3788
api.snapshot ( " t" ) .unwrap ( ) ;
3789
api.commit ( " api v2" , & [ ] ) .unwrap ( ) ;
3790
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3791
assert_eq! ( settle ( & | l| l.first ( ) .is_some_and ( | c | c[ " behind" ] = = 1 ) ) .await[ 0 ] [ " behind" ] , 1 ) ;
3792
let ( _ , page) = call ( & h.app, " GET" , " /andy/api" , None , None ) .await;
c9aacc0e Andy McCall 23 days ago ↶
3793
assert! ( page.contains ( " Diffuses to" ) & & page.contains ( " andy/game" ) & & page.contains ( " 1 behind" ) , " the page shows it" ) ;
f677d627 Andy McCall 24 days ago ↶
3794
3795
3796
sqlx:: query( " UPDATE repositories SET visibility = 'private' WHERE name = 'game'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
3797
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , None , None ) .await;
3798
assert_eq! ( body.trim ( ) , " []" ) ;
3799
let ( _ , page) = call ( & h.app, " GET" , " /andy/api" , None , None ) .await;
3800
assert! ( ! page.contains ( " andy/game" ) ) ;
3801
sqlx:: query( " UPDATE repositories SET visibility = 'public' WHERE name = 'game'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
e7158350 Andy McCall 24 days ago ↶
3802
1d634f69 Andy McCall 24 days ago ↶
3803
3804
3805
let game_row = db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
3806
let api_row = db:: repos:: get( & h.app.db, " andy" , " api" ) .await.unwrap ( ) .unwrap ( ) ;
3807
let api_heads = api.bookmark_heads ( ) .unwrap ( ) ;
3808
let bumped = oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, api_heads.clone ( ) ) .await.unwrap ( ) ;
3809
let oxygenhub:: diffusion:: Bumped:: Moved { bookmark, heads, .. } = bumped else { panic! ( " {bumped:?}" ) } ;
3810
assert_eq! ( bookmark, " diffusion/deps/api" ) ;
3811
game.fetch ( " origin" ) .unwrap ( ) ;
3812
let bump_state = game.load_state ( & heads) .unwrap ( ) .materialize ( ) ;
3813
let pin = match & bump_state.files[ " deps/api" ] .content {
3814
oxy_core:: state:: FileContent:: Opaque { blobs } => oxy_core:: subrepo:: Pin:: decode( & game.read_blob ( & blobs[ 0 ] ) .unwrap ( ) ) .unwrap ( ) ,
3815
other => panic! ( " {other:?}" ) ,
3816
} ;
3817
assert! (
3818
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 } ) ,
3819
" {pin:?}"
3820
) ;
3821
assert! ( bump_state.files.contains_key ( " game.c" ) , " the rest of the game is as it was" ) ;
9aaa88b2 Andy McCall 24 days ago ↶
3822
assert! ( matches! (
1d634f69 Andy McCall 24 days ago ↶
3823
oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, api_heads.clone ( ) ) .await.unwrap ( ) ,
9aaa88b2 Andy McCall 24 days ago ↶
3824
oxygenhub:: diffusion:: Bumped:: Already { .. }
3825
) ) ;
7a14bdae Andy McCall 24 days ago ↶
3826
3827
3828
1d634f69 Andy McCall 24 days ago ↶
3829
write ( api_work.path ( ) , " other.c" , " v3\n " ) ;
3830
api.snapshot ( " t" ) .unwrap ( ) ;
3831
let v3 = api.commit ( " api v3, on its own" , & [ ] ) .unwrap ( ) ;
3832
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3833
let only_v3 = api.load_state ( & [ v3] ) .unwrap ( ) .heads ( ) .to_vec ( ) ;
7a14bdae Andy McCall 24 days ago ↶
3834
let combined = oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, only_v3) .await.unwrap ( ) ;
3835
let oxygenhub:: diffusion:: Bumped:: Moved { heads: ch, .. } = combined else { panic! ( " {combined:?}" ) } ;
3836
game.fetch ( " origin" ) .unwrap ( ) ;
3837
let st = game.load_state ( & ch) .unwrap ( ) .materialize ( ) ;
3838
let pin = match & st.files[ " deps/api" ] .content {
3839
oxy_core:: state:: FileContent:: Opaque { blobs } => oxy_core:: subrepo:: Pin:: decode( & game.read_blob ( & blobs[ 0 ] ) .unwrap ( ) ) .unwrap ( ) ,
3840
other => panic! ( " {other:?}" ) ,
3841
} ;
3842
let oxy_core:: subrepo:: Pin:: Oxy { heads: pinned, .. } = pin else { panic! ( ) } ;
3843
let all = api.load_state ( & pinned) .unwrap ( ) ;
3844
3845
assert! ( all.contains ( & v3) & & all.len ( ) = = 2 , " v1 and v3, {} changes" , all.len ( ) ) ;
1d634f69 Andy McCall 24 days ago ↶
3846
3847
let newer = api.bookmark_heads ( ) .unwrap ( ) ;
3848
let again = oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, newer) .await.unwrap ( ) ;
3849
assert! ( matches! ( again, oxygenhub:: diffusion:: Bumped:: Moved { ref bookmark, .. } if bookmark = = " diffusion/deps/api" ) , " {again:?}" ) ;
3850
e7158350 Andy McCall 24 days ago ↶
3851
3852
let put =
3853
| body : & 'static str , who : Option< & 'static str > | call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , who, Some ( body.into ( ) ) ) ;
3854
let ( st, body) = put ( r # "{"follow":"green"}"# , Some ( " site" ) ) .await;
3855
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3856
assert! ( body.contains ( " \" follow\" :\" green\" " ) & & body.contains ( " \" from\" :\" setting\" " ) , " {body}" ) ;
3857
assert_eq! ( put ( r # "{"follow":"sometimes"}"# , Some ( " site" ) ) .await.0 , StatusCode:: BAD_REQUEST ) ;
3858
assert_ne! ( put ( r # "{"follow":"any"}"# , None ) .await.0 , StatusCode:: OK , " not without a token" ) ;
3859
let ( _ , body) = put ( r # "{"follow":"default"}"# , Some ( " site" ) ) .await;
3860
assert! ( body.contains ( " \" follow\" :\" off\" " ) & & body.contains ( " \" from\" :\" site\" " ) , " {body}" ) ;
f677d627 Andy McCall 24 days ago ↶
3861
3862
3863
game.submodule_remove ( " deps/api" ) .unwrap ( ) ;
3864
std:: fs:: remove_dir_all( game_work.path ( ) .join ( " deps/api" ) ) .unwrap ( ) ;
3865
game.snapshot ( " t" ) .unwrap ( ) ;
3866
game.commit ( " game without api" , & [ ] ) .unwrap ( ) ;
3867
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3868
let left = settle ( & | l| l.is_empty ( ) ) .await;
3869
assert! ( left.is_empty ( ) , " {left:?}" ) ;
3870
}
a7e6e8e6 Andy McCall 24 days ago ↶
3871
3872
3873
3874
3875
# [ tokio ::test ( flavor = " multi_thread" ) ]
3876
async fn diffusion_proposes_a_bump_when_the_upstream_moves ( ) {
3877
let h = hub ( ) .await;
3878
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3879
create ( & h.app, " andy" , " api" ) .await;
3880
create ( & h.app, " andy" , " game" ) .await;
180dc43a Andy McCall 23 days ago ↶
3881
3882
let seen: std:: sync:: Mutex< Vec< serde_json:: Value> > = std:: sync:: Mutex:: new( Vec :: new( ) ) ;
3883
let seen = & seen;
3884
let hub_app = & h.app;
3885
let pass_all = move | | async move {
3886
let h_app = hub_app;
a7e6e8e6 Andy McCall 24 days ago ↶
3887
for _ in 0 .. 20 {
180dc43a Andy McCall 23 days ago ↶
3888
let Some ( a) = claim ( h_app) .await else {
a7e6e8e6 Andy McCall 24 days ago ↶
3889
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
180dc43a Andy McCall 23 days ago ↶
3890
if ! jobs_left ( h_app) .await {
a7e6e8e6 Andy McCall 24 days ago ↶
3891
break ;
3892
}
3893
continue ;
3894
} ;
180dc43a Andy McCall 23 days ago ↶
3895
seen.lock ( ) .unwrap ( ) .push ( a.clone ( ) ) ;
a7e6e8e6 Andy McCall 24 days ago ↶
3896
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3897
let ( name, ph) = ( a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
f7bcee49 Andy McCall 22 days ago ↶
3898
let st = finish ( h_app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, 0 ) } ) ) .await;
3899
3900
3901
3902
if st = = StatusCode:: CONFLICT {
3903
let job = db:: flow:: job( & h_app.db, id) .await.unwrap ( ) .unwrap ( ) ;
3904
assert_eq! ( job.status, " cancelled" , " refused only because a newer run cancelled it" ) ;
3905
continue ;
3906
}
3907
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
a7e6e8e6 Andy McCall 24 days ago ↶
3908
}
3909
} ;
3910
let api_work = tempfile:: tempdir( ) .unwrap ( ) ;
3911
let api = client ( & format! ( " {} /andy/api" , h.base) , api_work.path ( ) ) ;
3912
write ( api_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3913
write ( api_work.path ( ) , " test.sh" , " echo ok\n " ) ;
3914
write ( api_work.path ( ) , " api.h" , " v1\n " ) ;
3915
api.snapshot ( " t" ) .unwrap ( ) ;
3916
api.commit ( " api v1" , & [ ] ) .unwrap ( ) ;
3917
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3918
pass_all ( ) .await;
3919
3920
let game_work = tempfile:: tempdir( ) .unwrap ( ) ;
3921
let game = client ( & format! ( " {} /andy/game" , h.base) , game_work.path ( ) ) ;
3922
game.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
3923
write ( game_work.path ( ) , " game.c" , " int main;\n " ) ;
f01cb865 Andy McCall 24 days ago ↶
3924
write ( game_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3925
write ( game_work.path ( ) , " test.sh" , " echo ok\n " ) ;
a7e6e8e6 Andy McCall 24 days ago ↶
3926
game.snapshot ( " t" ) .unwrap ( ) ;
3927
game.commit ( " game on api v1" , & [ ] ) .unwrap ( ) ;
3928
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3929
let game_row = db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
3930
let bump = | | async {
3931
for _ in 0 .. 100 {
3932
let open = db:: proposals:: list( & h.app.db, game_row.id, & [ " open" ] ) .await.unwrap ( ) ;
3933
if let Some ( p) = open.into_iter ( ) .find ( | p | p.source_bookmark = = " diffusion/deps/api" ) {
3934
return Some ( p) ;
3935
}
3936
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3937
}
3938
None
3939
} ;
3940
3941
3942
write ( api_work.path ( ) , " a2.c" , " v2\n " ) ;
3943
api.snapshot ( " t" ) .unwrap ( ) ;
3944
api.commit ( " api v2: faster sprites" , & [ ] ) .unwrap ( ) ;
3945
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3946
pass_all ( ) .await;
3947
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
3948
assert! ( db:: proposals:: list( & h.app.db, game_row.id, & [ ] ) .await.unwrap ( ) .is_empty ( ) , " off proposes nothing" ) ;
3949
3776b70d Andy McCall 24 days ago ↶
3950
3951
a7e6e8e6 Andy McCall 24 days ago ↶
3952
let put = | body : & 'static str | call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , Some ( body.into ( ) ) ) ;
3953
assert_eq! ( put ( r # "{"follow":"any"}"# ) .await.0 , StatusCode:: OK ) ;
3776b70d Andy McCall 24 days ago ↶
3954
let first = bump ( ) .await.expect ( " caught up when turned on" ) ;
3955
assert! ( first.title.contains ( " 1 change" ) & & first.body.contains ( " api v2: faster sprites" ) , " {:?}" , first.title) ;
f01cb865 Andy McCall 24 days ago ↶
3956
3957
let mut ran = false ;
3958
for _ in 0 .. 100 {
3959
if runs_of ( & h.app, " andy" , " game" ) .await.iter ( ) .any ( | r | r.bookmark = = " diffusion/deps/api" ) {
3960
ran = true ;
3961
break ;
3962
}
3963
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3964
}
3965
assert! ( ran, " a run of the bump" ) ;
3966
pass_all ( ) .await;
a7e6e8e6 Andy McCall 24 days ago ↶
3967
write ( api_work.path ( ) , " a3.c" , " v3\n " ) ;
3968
api.snapshot ( " t" ) .unwrap ( ) ;
3969
api.commit ( " api v3: save slots past ten" , & [ ] ) .unwrap ( ) ;
3970
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3776b70d Andy McCall 24 days ago ↶
3971
let mut p = first.clone ( ) ;
3972
for _ in 0 .. 100 {
3973
p = bump ( ) .await.unwrap ( ) ;
3974
if p.body.contains ( " api v3" ) {
3975
break ;
3976
}
3977
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3978
}
3979
assert_eq! ( p.number, first.number, " the same proposal" ) ;
a7e6e8e6 Andy McCall 24 days ago ↶
3980
assert_eq! ( ( p.author.as_str ( ) , p.target.as_str ( ) ) , ( " diffusion" , " main" ) ) ;
3981
assert! ( p.title.contains ( " deps/api" ) & & p.title.contains ( " andy/api" ) & & p.title.contains ( " 2 changes" ) , " {}" , p.title) ;
3982
assert! ( p.body.contains ( " api v2: faster sprites" ) & & p.body.contains ( " api v3: save slots past ten" ) , " {}" , p.body) ;
3983
let labels = db:: issues:: proposal_labels( & h.app.db, p.id) .await.unwrap ( ) ;
3984
assert! ( labels.iter ( ) .any ( | l | l.name = = " dependencies" ) ) ;
3985
pass_all ( ) .await;
3986
3987
3988
3989
assert_eq! ( put ( r # "{"follow":"green"}"# ) .await.0 , StatusCode:: OK ) ;
3990
write ( api_work.path ( ) , " a4.c" , " v4\n " ) ;
3991
api.snapshot ( " t" ) .unwrap ( ) ;
3992
api.commit ( " api v4: rsm_set cheaper" , & [ ] ) .unwrap ( ) ;
3993
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3994
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
3995
assert! ( ! bump ( ) .await.unwrap ( ) .body.contains ( " api v4" ) , " not before its run passes" ) ;
3996
pass_all ( ) .await;
3997
let mut updated = None ;
3998
for _ in 0 .. 100 {
3999
let p2 = bump ( ) .await.unwrap ( ) ;
4000
if p2.body.contains ( " api v4" ) {
4001
updated = Some ( p2) ;
4002
break ;
4003
}
4004
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4005
}
4006
let p2 = updated.expect ( " brought up to date once the run passed" ) ;
4007
assert_eq! ( p2.number, p.number, " the same proposal" ) ;
4008
assert! ( p2.title.contains ( " 3 changes" ) , " {}" , p2.title) ;
4009
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 ↶
4010
757195ad Andy McCall 23 days ago ↶
4011
4012
4013
pass_all ( ) .await;
4014
let ( st, page) = call ( & h.app, " GET" , " /andy/api/diffusion" , None , None ) .await;
4015
assert_eq! ( st, StatusCode:: OK ) ;
c9aacc0e Andy McCall 23 days ago ↶
4016
assert! ( page.contains ( " Diffuses to" ) & & page.contains ( " andy/game" ) & & page.contains ( & format! ( " #{} " , p2.number) ) , " the bump is listed" ) ;
757195ad Andy McCall 23 days ago ↶
4017
assert! ( page.contains ( " df-green" ) , " a passing run of a checked upstream is green" ) ;
6246d1e5 Andy McCall 23 days ago ↶
4018
4019
assert! (
4020
page.contains ( r # ""scene":"ripples""# ) & & page.contains ( r # "["andy/game","# ) & & page.contains ( r # ""repo":"andy/api""# ) ,
4021
" the ripples' data"
4022
) ;
757195ad Andy McCall 23 days ago ↶
4023
let ( _ , page) = call ( & h.app, " GET" , " /andy/game/diffusion" , None , None ) .await;
c9aacc0e Andy McCall 23 days ago ↶
4024
assert! ( page.contains ( " Takes from" ) & & page.contains ( " andy/api" ) ) ;
757195ad Andy McCall 23 days ago ↶
4025
let ( _ , overview) = call ( & h.app, " GET" , " /andy/game" , None , None ) .await;
c9aacc0e Andy McCall 23 days ago ↶
4026
assert! ( overview.contains ( " Takes from" ) & & overview.contains ( " >Diffusion</a>" ) , " the tab and the panel" ) ;
eb2a3502 Andy McCall 23 days ago ↶
4027
4028
let ( st, body) = call ( & h.app, " GET" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , None ) .await;
4029
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4030
let d: hub_api:: Diffusion = serde_json:: from_str( & body) .unwrap ( ) ;
4031
assert! ( d.diffuses_to.is_empty ( ) , " nothing pins the game" ) ;
4032
let pin = d.takes_from.first ( ) .expect ( " the game takes from the api" ) ;
4033
assert_eq! ( ( pin.upstream.as_str ( ) , pin.consumer.as_str ( ) ) , ( " andy/api" , " andy/game" ) ) ;
4034
assert_eq! ( pin.bump, Some ( p2.number) ) ;
4035
assert_eq! ( pin.upstream_state.tone, " green" , " {body}" ) ;
4036
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/diffusion" , Some ( " site" ) , None ) .await;
4037
let d: hub_api:: Diffusion = serde_json:: from_str( & body) .unwrap ( ) ;
4038
assert! ( d.takes_from.is_empty ( ) & & d.diffuses_to.iter ( ) .any ( | p | p.consumer = = " andy/game" ) , " {body}" ) ;
757195ad Andy McCall 23 days ago ↶
4039
let ( st, _ ) = call ( & h.app, " GET" , " /admin/diffusion" , None , None ) .await;
4040
assert_eq! ( st, StatusCode:: NOT_FOUND , " administrators only" ) ;
4041
4042
5d54cf74 Andy McCall 21 days ago ↶
4043
4044
4045
let game = oxygenhub:: db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
4046
oxygenhub:: diffusion:: fail_next_records( & h.app, game.id, 1 ) ;
757195ad Andy McCall 23 days ago ↶
4047
let ( st, body) =
4048
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/game/proposals/{} /land" , p2.number) , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
4049
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4050
let mut behind = serde_json:: Value:: Null;
4051
for _ in 0 .. 100 {
4052
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , Some ( " site" ) , None ) .await;
4053
let list: Vec< serde_json:: Value> = serde_json:: from_str( & list) .unwrap ( ) ;
4054
behind = list.iter ( ) .find ( | c | c[ " name" ] = = " game" ) .map ( | c | c[ " behind" ] .clone ( ) ) .unwrap_or_default ( ) ;
4055
if behind = = 0 {
4056
break ;
4057
}
4058
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4059
}
4060
assert_eq! ( behind, 0 , " the index followed the landing" ) ;
5d54cf74 Andy McCall 21 days ago ↶
4061
assert_eq! ( oxygenhub:: diffusion:: forced_left( & h.app, game.id) , 0 , " the first write did fail" ) ;
0e04dfc2 Andy McCall 21 days ago ↶
4062
4063
4064
4065
let pins_of = | | async {
3b1bc27f Andy McCall 20 days ago
4066
sqlx:: query_scalar:: < _ , String> ( " SELECT heads FROM diffusion_pins WHERE consumer_id = $1" )
0e04dfc2 Andy McCall 21 days ago ↶
4067
.bind ( game.id)
4068
.fetch_one ( & h.app.db)
4069
.await
4070
.unwrap ( )
4071
} ;
4072
let right = pins_of ( ) .await;
3b1bc27f Andy McCall 20 days ago
4073
sqlx:: query( " UPDATE diffusion_pins SET heads = 'stale' WHERE consumer_id = $1" ) .bind ( game.id) .execute ( & h.app.db) .await.unwrap ( ) ;
4074
sqlx:: query( " DELETE FROM diffusion_indexed WHERE consumer_id = $1" ) .bind ( game.id) .execute ( & h.app.db) .await.unwrap ( ) ;
0e04dfc2 Andy McCall 21 days ago ↶
4075
let r = oxygenhub:: diffusion:: reconcile( & h.app) .await;
4076
assert! ( r.reindexed > = 1 , " {r:?}" ) ;
4077
assert_eq! ( pins_of ( ) .await, right, " the pin read again from the game's bookmark" ) ;
4078
assert_eq! ( oxygenhub:: diffusion:: reconcile( & h.app) .await.reindexed, 0 , " nothing left to mend" ) ;
757195ad Andy McCall 23 days ago ↶
4079
180dc43a Andy McCall 23 days ago ↶
4080
4081
4082
sqlx:: query( " UPDATE repositories SET visibility = 'private' WHERE name = 'api'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
4083
seen.lock ( ) .unwrap ( ) .clear ( ) ;
4084
write ( api_work.path ( ) , " a6.c" , " v6\n " ) ;
4085
api.snapshot ( " t" ) .unwrap ( ) ;
4086
api.commit ( " api v6" , & [ ] ) .unwrap ( ) ;
4087
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4fa0da12 Andy McCall 23 days ago ↶
4088
4089
4090
let mut bump_job = None ;
4091
for _ in 0 .. 100 {
4092
pass_all ( ) .await;
4093
bump_job = seen.lock ( ) .unwrap ( ) .iter ( ) .find ( | c | c[ " repo" ] = = " andy/game" & & c[ " bookmark" ] = = " diffusion/deps/api" ) .cloned ( ) ;
4094
if bump_job.is_some ( ) {
4095
break ;
4096
}
4097
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
4098
}
4099
let bump_job = bump_job.expect ( " the bump's job ran" ) ;
180dc43a Andy McCall 23 days ago ↶
4100
let subs: Vec< & str > = bump_job[ " subrepos" ] .as_array ( ) .unwrap ( ) .iter ( ) .filter_map ( | s | s[ " path" ] .as_str ( ) ) .collect ( ) ;
4101
assert_eq! ( subs, vec! [ " deps/api" ] , " {bump_job}" ) ;
4102
3776b70d Andy McCall 24 days ago ↶
4103
4104
4105
create ( & h.app, " andy" , " game2" ) .await;
4106
let g2_work = tempfile:: tempdir( ) .unwrap ( ) ;
4107
let g2 = client ( & format! ( " {} /andy/game2" , h.base) , g2_work.path ( ) ) ;
4108
g2.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
4109
g2.snapshot ( " t" ) .unwrap ( ) ;
4110
g2.commit ( " game2 on api" , & [ ] ) .unwrap ( ) ;
4111
assert! ( g2.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4112
4113
write ( api_work.path ( ) , " a5.c" , " v5\n " ) ;
4114
api.snapshot ( " t" ) .unwrap ( ) ;
4115
api.commit ( " api v5" , & [ ] ) .unwrap ( ) ;
7e01a766 Andy McCall 21 days ago ↶
4116
let api_row = db:: repos:: get( & h.app.db, " andy" , " api" ) .await.unwrap ( ) .unwrap ( ) ;
4117
let runs_before = db:: flow:: runs( & h.app.db, api_row.id, 100 , None ) .await.unwrap ( ) .len ( ) ;
3776b70d Andy McCall 24 days ago ↶
4118
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
7e01a766 Andy McCall 21 days ago ↶
4119
4120
4121
for _ in 0 .. 300 {
4122
if db:: flow:: runs( & h.app.db, api_row.id, 100 , None ) .await.unwrap ( ) .len ( ) > runs_before {
4123
break ;
4124
}
4125
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 20 ) ) .await;
4126
}
3776b70d Andy McCall 24 days ago ↶
4127
pass_all ( ) .await;
4128
let g2_row = db:: repos:: get( & h.app.db, " andy" , " game2" ) .await.unwrap ( ) .unwrap ( ) ;
4129
for _ in 0 .. 100 {
3b1bc27f Andy McCall 20 days ago
4130
if sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = $1" )
3776b70d Andy McCall 24 days ago ↶
4131
.bind ( g2_row.id)
4132
.fetch_one ( & h.app.db)
4133
.await
4134
.unwrap ( )
4135
> 0
4136
{
4137
break ;
4138
}
4139
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4140
}
4141
assert! ( db:: proposals:: list( & h.app.db, g2_row.id, & [ ] ) .await.unwrap ( ) .is_empty ( ) , " off until turned on" ) ;
4142
let ( st, _ ) = call ( & h.app, " PUT" , " /api/v1/repos/andy/game2/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green"}"# .into ( ) ) ) .await;
4143
assert_eq! ( st, StatusCode:: OK ) ;
4144
let mut caught = None ;
5d54cf74 Andy McCall 21 days ago ↶
4145
4146
for _ in 0 .. 300 {
3776b70d Andy McCall 24 days ago ↶
4147
if let Some ( p) = db:: proposals:: list( & h.app.db, g2_row.id, & [ " open" ] ) .await.unwrap ( ) .into_iter ( ) .next ( ) {
4148
caught = Some ( p) ;
4149
break ;
4150
}
4151
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4152
}
4153
let caught = caught.expect ( " bumped as soon as it was turned on" ) ;
4154
assert! ( caught.body.contains ( " api v5" ) & & caught.title.contains ( " 1 change" ) , " {:?} {:?}" , caught.title, caught.body) ;
0e04dfc2 Andy McCall 21 days ago ↶
4155
3b1bc27f Andy McCall 20 days ago
4156
sqlx:: query( " UPDATE proposals SET state = 'closed' WHERE id = $1" ) .bind ( caught.id) .execute ( & h.app.db) .await.unwrap ( ) ;
62cdf34d Andy McCall 21 days ago ↶
4157
4158
0e04dfc2 Andy McCall 21 days ago ↶
4159
let r = oxygenhub:: diffusion:: reconcile( & h.app) .await;
62cdf34d Andy McCall 21 days ago ↶
4160
assert_eq! ( r.caught_up, 0 , " the reconciler respects the closed bump: {r:?}" ) ;
a7e6e8e6 Andy McCall 24 days ago ↶
4161
}
4162
4163
4164
async fn jobs_left ( app : & Shared) -> bool {
4165
sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM flow_jobs WHERE status IN ('queued', 'running')" )
4166
.fetch_one ( & app.db)
4167
.await
4168
.unwrap_or ( 0 )
4169
> 0
9aaa88b2 Andy McCall 24 days ago ↶
4170
}
4171
4172
4173
4174
# [ tokio ::test ( flavor = " multi_thread" ) ]
4175
async fn bumps_from_an_upstream_without_checks_are_marked_unverified ( ) {
4176
let h = hub ( ) .await;
4177
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4178
create ( & h.app, " andy" , " lib" ) .await;
4179
create ( & h.app, " andy" , " app" ) .await;
4180
let lib_work = tempfile:: tempdir( ) .unwrap ( ) ;
4181
let lib = client ( & format! ( " {} /andy/lib" , h.base) , lib_work.path ( ) ) ;
4182
write ( lib_work.path ( ) , " lib.h" , " v1\n " ) ;
4183
lib.snapshot ( " t" ) .unwrap ( ) ;
4184
lib.commit ( " lib v1" , & [ ] ) .unwrap ( ) ;
4185
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4186
let app_work = tempfile:: tempdir( ) .unwrap ( ) ;
4187
let app = client ( & format! ( " {} /andy/app" , h.base) , app_work.path ( ) ) ;
4188
app.submodule_add ( & format! ( " {} /andy/lib" , h.base) , " deps/lib" , Some ( " main" ) ) .unwrap ( ) ;
c4e432da Andy McCall 24 days ago ↶
4189
4190
let modules = std:: fs:: read_to_string( app_work.path ( ) .join ( " .oxymodules" ) ) .unwrap ( ) ;
4191
write ( app_work.path ( ) , " .oxymodules" , & format! ( " {modules} watch = lib.c\n " ) ) ;
9aaa88b2 Andy McCall 24 days ago ↶
4192
app.snapshot ( " t" ) .unwrap ( ) ;
4193
app.commit ( " app on lib" , & [ ] ) .unwrap ( ) ;
4194
assert! ( app.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4195
let app_row = db:: repos:: get( & h.app.db, " andy" , " app" ) .await.unwrap ( ) .unwrap ( ) ;
4196
for _ in 0 .. 100 {
3b1bc27f Andy McCall 20 days ago
4197
if sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = $1" )
9aaa88b2 Andy McCall 24 days ago ↶
4198
.bind ( app_row.id)
4199
.fetch_one ( & h.app.db)
4200
.await
4201
.unwrap ( )
4202
> 0
4203
{
4204
break ;
4205
}
4206
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4207
}
4208
let ( st, _ ) = call ( & h.app, " PUT" , " /api/v1/repos/andy/app/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green"}"# .into ( ) ) ) .await;
4209
assert_eq! ( st, StatusCode:: OK ) ;
4210
write ( lib_work.path ( ) , " lib.c" , " v2\n " ) ;
4211
lib.snapshot ( " t" ) .unwrap ( ) ;
4212
lib.commit ( " lib v2" , & [ ] ) .unwrap ( ) ;
4213
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4214
let mut found = None ;
4215
for _ in 0 .. 100 {
4216
if let Some ( p) = db:: proposals:: list( & h.app.db, app_row.id, & [ " open" ] ) .await.unwrap ( ) .into_iter ( ) .next ( ) {
4217
found = Some ( p) ;
4218
break ;
4219
}
4220
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4221
}
4222
let p = found.expect ( " a bump although the upstream never goes green" ) ;
4223
assert! ( p.body.contains ( " Upstream unverified" ) , " {}" , p.body) ;
c4e432da Andy McCall 24 days ago ↶
4224
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 ↶
4225
let mut labels = Vec :: new( ) ;
4226
for _ in 0 .. 100 {
4227
labels = db:: issues:: proposal_labels( & h.app.db, p.id) .await.unwrap ( ) .into_iter ( ) .map ( | l | l.name ) .collect:: < Vec< String> > ( ) ;
4228
if labels.len ( ) = = 2 {
4229
break ;
4230
}
4231
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4232
}
4233
assert! ( labels.contains ( & " upstream unverified" .to_string ( ) ) & & labels.contains ( & " dependencies" .to_string ( ) ) , " {labels:?}" ) ;
a7e6e8e6 Andy McCall 24 days ago ↶
4234
}
d943160d Andy McCall 23 days ago ↶
4235
4236
4237
4238
# [ tokio ::test ( flavor = " multi_thread" ) ]
4239
async fn a_moved_target_runs_its_open_proposals_again ( ) {
4240
let h = hub ( ) .await;
4241
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4242
create ( & h.app, " andy" , " demo" ) .await;
4243
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4244
let mut c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
4245
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4246
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
4247
c.snapshot ( " t" ) .unwrap ( ) ;
4248
let base = c.commit ( " base" , & [ ] ) .unwrap ( ) ;
4249
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4250
4251
for ( b, file) in [ ( " fix-a" , " a.txt" ) , ( " fix-b" , " b.txt" ) ] {
4252
c.bookmark_create ( b, Some ( & [ base] ) ) .unwrap ( ) ;
4253
c.bookmark_switch ( b, true ) .unwrap ( ) ;
4254
write ( work.path ( ) , file, " x\n " ) ;
4255
c.snapshot ( " t" ) .unwrap ( ) ;
4256
c.commit ( & format! ( " add {file} " ) , & [ ] ) .unwrap ( ) ;
4257
assert! ( c.push_bookmark ( " origin" , b, false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4258
let body = serde_json:: json! ( { " title" : b, " target" : " main" , " source" : b } ) .to_string ( ) ;
4259
let ( st, out) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/proposals" , Some ( " site" ) , Some ( body) ) .await;
4260
assert_eq! ( st, StatusCode:: CREATED , " {out}" ) ;
4261
}
4262
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
4263
let props = db:: proposals:: list( & h.app.db, row.id, & [ " open" ] ) .await.unwrap ( ) ;
4264
let a = props.iter ( ) .find ( | p | p.source_bookmark = = " fix-a" ) .unwrap ( ) .number;
4265
let b = props.iter ( ) .find ( | p | p.source_bookmark = = " fix-b" ) .unwrap ( ) .number;
4266
4267
let ( st, out) = call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/demo/proposals/{a} /land" ) , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
4268
assert_eq! ( st, StatusCode:: OK , " {out}" ) ;
4269
let label = format! ( " proposal/{b} " ) ;
4270
let mut found = None ;
4271
for _ in 0 .. 100 {
4272
if let Some ( r) = runs_of ( & h.app, " andy" , " demo" ) .await.into_iter ( ) .find ( | r | r.bookmark = = label ) {
4273
found = Some ( r) ;
4274
break ;
4275
}
4276
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4277
}
4278
let run = found.expect ( " a run of fix-b's new projected state" ) ;
4279
let projected = db:: proposals:: get( & h.app.db, row.id, b) .await.unwrap ( ) .unwrap ( ) ;
4280
assert_eq! ( ( run.trigger.as_str ( ) , run.state.as_str ( ) ) , ( " proposal" , projected.projected_state.as_str ( ) ) , " on its projected state" ) ;
4281
4282
let before = runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) ;
4283
oxygenhub:: web:: proposals:: settle_after_push( & h.app, & row, " main" , " andy" ) .await;
4284
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , before, " not again for the same state" ) ;
4285
}
93501da2 Andy McCall 23 days ago ↶
4286
4287
4288
4289
async fn send_image ( app : & Shared, image : & [u8 ]) -> ( String, serde_json:: Value) {
4290
let mut chunks = std:: collections:: HashMap:: new( ) ;
4291
let m = jet:: images:: scan( image, None , | c , b | {
4292
chunks.insert ( c.hash ( ) .to_string ( ) , b.to_vec ( ) ) ;
4293
Ok ( ( ) )
4294
} )
4295
.unwrap ( ) ;
4296
for c in m.needed ( ) {
4297
let ( st, body) = put_bytes ( app, & format! ( " /api/v1/flow/images/chunks/{c} " ) , " site" , chunks[ c] .clone ( ) ) .await;
4298
assert_eq! ( st, StatusCode:: NO_CONTENT , " {body}" ) ;
4299
}
4300
let ( st, body) =
4301
call ( app, " PUT" , & format! ( " /api/v1/flow/images/{} " , m.hash) , Some ( " site" ) , Some ( serde_json:: to_string( & m) .unwrap ( ) ) ) .await;
4302
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
4303
( m.hash.clone ( ) , serde_json:: from_str( & body) .unwrap ( ) )
4304
}
4305
4306
4307
4308
4309
4310
# [ tokio ::test ( flavor = " multi_thread" ) ]
4311
async fn an_image_names_its_own_architecture_and_a_pass_teaches_the_rest ( ) {
4312
use oxy_core:: identity:: SecretKey;
4313
let h = hub ( ) .await;
4314
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4315
4316
let dir = tempfile:: tempdir( ) .unwrap ( ) ;
4317
let tree = dir.path ( ) .join ( " tree" ) ;
4318
std:: fs:: create_dir_all( tree.join ( " bin" ) ) .unwrap ( ) ;
4319
let mut elf = vec! [ 0 u8 ; 64 ] ;
4320
elf[ .. 7 ] .copy_from_slice ( b " \x7f ELF\x02 \x01 \x01 " ) ;
4321
elf[ 18 .. 20 ] .copy_from_slice ( & 0x3E u16 .to_le_bytes ( ) ) ;
4322
std:: fs:: write( tree.join ( " bin/sh" ) , & elf) .unwrap ( ) ;
4323
let img = dir.path ( ) .join ( " rootfs.ext4" ) ;
4324
let made = std:: process:: Command:: new( " mkfs.ext4" ) .args ( [ " -q" , " -F" , " -d" ] ) .arg ( & tree) .arg ( & img) .arg ( " 8M" ) .status ( ) ;
4325
if ! made.is_ok_and ( | s | s.success ( ) ) {
4326
eprintln! ( " mkfs.ext4 not found: skipped" ) ;
4327
return ;
4328
}
4329
let ( x86, answer) = send_image ( & h.app, & std:: fs:: read( & img) .unwrap ( ) ) .await;
4330
assert_eq! ( answer[ " arch" ] , " x86_64" , " read from its shell: {answer}" ) ;
4331
let ( _ , body) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{x86} " ) , Some ( " runner-secret" ) , None ) .await;
4332
assert_eq! ( serde_json:: from_str:: < jet:: images:: Manifest> ( & body) .unwrap ( ) .arch.as_deref ( ) , Some ( " x86_64" ) , " runners fetch it named" ) ;
4333
4334
let blob: Vec< u8 > = ( 0 .. 300_000 u32 ) .map ( | i | ( i % 241 ) as u8 ) .collect ( ) ;
4335
let ( unread, answer) = send_image ( & h.app, & blob) .await;
4336
assert! ( answer[ " arch" ] .is_null ( ) , " {answer}" ) ;
4337
let archs = db:: flow:: image_archs( & h.app.db) .await.unwrap ( ) ;
4338
assert_eq! ( ( archs.get ( & x86) .map ( String :: as_str) , archs.get ( & unread) ) , ( Some ( " x86_64" ) , None ) ) ;
4339
4340
sqlx:: query( " UPDATE jet_images SET arch = ''" ) .execute ( & h.app.db) .await.unwrap ( ) ;
4341
sqlx:: query( " DELETE FROM flow_images" ) .execute ( & h.app.db) .await.unwrap ( ) ;
4342
assert_eq! ( oxygenhub:: images:: learn_archs( & h.app) .await.unwrap ( ) , 1 ) ;
4343
assert_eq! ( db:: flow:: image_archs( & h.app.db) .await.unwrap ( ) .get ( & x86) .map ( String :: as_str) , Some ( " x86_64" ) ) ;
4344
4345
4346
create ( & h.app, " andy" , " arch" ) .await;
4347
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4348
let c = client ( & format! ( " {} /andy/arch" , h.base) , work.path ( ) ) ;
4349
let kdl = format! (
4350
" schema-version 1\n sandbox \" unread\" {{ \n rootfs \" cas://blob/{unread} \" \n }} \n sandbox \" x86\" {{ \n rootfs \" cas://blob/{x86} \" \n }} \n \
4351
job \" first\" {{ \n sandbox \" unread\" \n exec \" make\" \n }} \n job \" second\" {{ \n sandbox \" x86\" \n exec \" make\" \n depends-on \" first\" \n }} \n \
4352
job \" third\" {{ \n sandbox \" unread\" \n exec \" make\" \n depends-on \" first\" \n }} \n "
4353
) ;
4354
write ( work.path ( ) , " .flow/pipeline.kdl" , & kdl) ;
4355
c.commit ( " Jobs on two images" , & [ ] ) .unwrap ( ) ;
4356
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4357
runs_of ( & h.app, " andy" , " arch" ) .await;
4358
4359
4360
let x86_info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
4361
let x86_claim =
4362
serde_json:: json! ( { " runner" : " earth" , " runtimes" : [ " microvm" ] , " images" : [ ] , " fetches_images" : true , " info" : x86_info } ) ;
4363
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( x86_claim.to_string ( ) ) ) .await;
4364
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4365
let first: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
4366
assert_eq! ( first[ " job" ] [ " name" ] , " first" ) ;
4367
let ph = first[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) ;
4368
let id = first[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4369
assert_eq! (
4370
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : receipt ( " first" , ph, 0 ) } ) ) .await,
4371
StatusCode:: NO_CONTENT
4372
) ;
4373
assert_eq! ( db:: flow:: image_archs( & h.app.db) .await.unwrap ( ) .get ( & unread) .map ( String :: as_str) , Some ( " x86_64" ) , " learned" ) ;
4374
4375
4376
let token = " flowreg_narvi" ;
4377
db:: runners:: add_registration( & h.app.db, & blake3:: hash( token.as_bytes ( ) ) .to_hex ( ) , " admin" , & Default :: default( ) ) .await.unwrap ( ) ;
4378
let identity = SecretKey:: generate( ) .identity ( " narvi" , " " , 1 ) ;
4379
let info = serde_json:: json! ( { " arch" : " riscv64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
4380
let reg = serde_json:: json! ( { " token" : token, " name" : " narvi" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
4381
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
4382
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4383
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
4384
let narvi =
4385
serde_json:: json! ( { " runner" : " narvi" , " runtimes" : [ " microvm" ] , " images" : [ ] , " fetches_images" : true , " info" : info } ) .to_string ( ) ;
4386
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( & credential) , Some ( narvi) ) .await;
4387
assert_eq! ( st, StatusCode:: NO_CONTENT , " the second job's image is x86_64 and the third's learned so: {body}" ) ;
4388
for _ in 0 .. 2 {
4389
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( x86_claim.to_string ( ) ) ) .await;
4390
assert_eq! ( st, StatusCode:: OK , " both go to x86_64: {body}" ) ;
4391
}
4392
}
f4d6ef41 Andy McCall 23 days ago ↶
4393
4394
4395
4396
4397
4398
# [ tokio ::test ( flavor = " multi_thread" ) ]
4399
async fn a_consumer_that_chose_auto_lands_green_bumps_itself ( ) {
4400
let h = hub ( ) .await;
4401
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4402
create ( & h.app, " andy" , " api" ) .await;
4403
create ( & h.app, " andy" , " game" ) .await;
4404
let hub_app = & h.app;
4405
4406
let pass_all = move | | async move {
4407
for _ in 0 .. 20 {
4408
let Some ( a) = claim ( hub_app) .await else {
4409
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4410
if ! jobs_left ( hub_app) .await {
4411
break ;
4412
}
4413
continue ;
4414
} ;
4415
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4416
let ( name, ph) = ( a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4417
assert_eq! (
4418
finish ( hub_app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, 0 ) } ) ) .await,
4419
StatusCode:: NO_CONTENT
4420
) ;
4421
}
4422
} ;
4423
let api_work = tempfile:: tempdir( ) .unwrap ( ) ;
4424
let api = client ( & format! ( " {} /andy/api" , h.base) , api_work.path ( ) ) ;
4425
write ( api_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4426
write ( api_work.path ( ) , " test.sh" , " echo ok\n " ) ;
4427
api.snapshot ( " t" ) .unwrap ( ) ;
4428
api.commit ( " api v1" , & [ ] ) .unwrap ( ) ;
4429
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4430
pass_all ( ) .await;
4431
let game_work = tempfile:: tempdir( ) .unwrap ( ) ;
4432
let game = client ( & format! ( " {} /andy/game" , h.base) , game_work.path ( ) ) ;
4433
game.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
4434
write ( game_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4435
write ( game_work.path ( ) , " test.sh" , " echo ok\n " ) ;
4436
game.snapshot ( " t" ) .unwrap ( ) ;
4437
game.commit ( " game on api v1" , & [ ] ) .unwrap ( ) ;
4438
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4439
pass_all ( ) .await;
4440
let game_row = db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
4441
for _ in 0 .. 100 {
3b1bc27f Andy McCall 20 days ago
4442
let n: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = $1" )
f4d6ef41 Andy McCall 23 days ago ↶
4443
.bind ( game_row.id)
4444
.fetch_one ( & h.app.db)
4445
.await
4446
.unwrap ( ) ;
4447
if n > 0 {
4448
break ;
4449
}
4450
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4451
}
4452
let ( st, body) =
4453
call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green","land":"auto"}"# .into ( ) ) ) .await;
4454
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4455
assert! ( body.contains ( r # ""land":"auto""# ) , " {body}" ) ;
4456
let ( st, _ ) =
4457
call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green","land":"soon"}"# .into ( ) ) ) .await;
4458
assert_eq! ( st, StatusCode:: BAD_REQUEST ) ;
4459
4460
4461
4462
write ( api_work.path ( ) , " a2.c" , " v2\n " ) ;
4463
api.snapshot ( " t" ) .unwrap ( ) ;
4464
api.commit ( " api v2: faster sprites" , & [ ] ) .unwrap ( ) ;
4465
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4466
let mut landed = None ;
4467
for _ in 0 .. 100 {
4468
pass_all ( ) .await;
4469
let all = db:: proposals:: list( & h.app.db, game_row.id, & [ ] ) .await.unwrap ( ) ;
4470
if let Some ( p) = all.into_iter ( ) .find ( | p | p.source_bookmark = = " diffusion/deps/api" & & p.state = = " landed" ) {
4471
landed = Some ( p) ;
4472
break ;
4473
}
4474
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4475
}
4476
let p = landed.expect ( " landed by itself once both halves were green" ) ;
4477
assert! ( p.body.contains ( " lands its bumps itself" ) , " {}" , p.body) ;
4478
let events = db:: proposals:: events( & h.app.db, p.id) .await.unwrap ( ) ;
4479
let landing = events.iter ( ) .find ( | e | e.event = = " landed" ) .expect ( " a landing on the timeline" ) ;
4480
assert_eq! ( landing.actor, " diffusion" ) ;
4481
assert! ( landing.detail.contains ( " Diffusion, both halves green" ) , " {}" , landing.detail) ;
4482
4483
let mut behind = serde_json:: Value:: Null;
4484
for _ in 0 .. 100 {
4485
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , Some ( " site" ) , None ) .await;
4486
let list: Vec< serde_json:: Value> = serde_json:: from_str( & list) .unwrap ( ) ;
4487
behind = list.iter ( ) .find ( | c | c[ " name" ] = = " game" ) .map ( | c | c[ " behind" ] .clone ( ) ) .unwrap_or_default ( ) ;
4488
if behind = = 0 {
4489
break ;
4490
}
4491
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4492
}
4493
assert_eq! ( behind, 0 , " main moved to the bump" ) ;
4494
4495
4496
4497
create ( & h.app, " andy" , " lib" ) .await;
4498
create ( & h.app, " andy" , " app" ) .await;
4499
let lib_work = tempfile:: tempdir( ) .unwrap ( ) ;
4500
let lib = client ( & format! ( " {} /andy/lib" , h.base) , lib_work.path ( ) ) ;
4501
write ( lib_work.path ( ) , " lib.h" , " v1\n " ) ;
4502
lib.snapshot ( " t" ) .unwrap ( ) ;
4503
lib.commit ( " lib v1" , & [ ] ) .unwrap ( ) ;
4504
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4505
let app_work = tempfile:: tempdir( ) .unwrap ( ) ;
4506
let app = client ( & format! ( " {} /andy/app" , h.base) , app_work.path ( ) ) ;
4507
app.submodule_add ( & format! ( " {} /andy/lib" , h.base) , " deps/lib" , Some ( " main" ) ) .unwrap ( ) ;
4508
let modules = std:: fs:: read_to_string( app_work.path ( ) .join ( " .oxymodules" ) ) .unwrap ( ) ;
4509
write ( app_work.path ( ) , " .oxymodules" , & format! ( " {modules} follow = green\n land = auto\n " ) ) ;
4510
write ( app_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4511
write ( app_work.path ( ) , " test.sh" , " echo ok\n " ) ;
4512
app.snapshot ( " t" ) .unwrap ( ) ;
4513
app.commit ( " app on lib" , & [ ] ) .unwrap ( ) ;
4514
assert! ( app.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4515
pass_all ( ) .await;
4516
let app_row = db:: repos:: get( & h.app.db, " andy" , " app" ) .await.unwrap ( ) .unwrap ( ) ;
4517
for _ in 0 .. 100 {
3b1bc27f Andy McCall 20 days ago
4518
let n: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = $1" )
f4d6ef41 Andy McCall 23 days ago ↶
4519
.bind ( app_row.id)
4520
.fetch_one ( & h.app.db)
4521
.await
4522
.unwrap ( ) ;
4523
if n > 0 {
4524
break ;
4525
}
4526
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4527
}
4528
write ( lib_work.path ( ) , " lib.c" , " v2\n " ) ;
4529
lib.snapshot ( " t" ) .unwrap ( ) ;
4530
lib.commit ( " lib v2" , & [ ] ) .unwrap ( ) ;
4531
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4532
let mut passed = false ;
4533
for _ in 0 .. 100 {
4534
pass_all ( ) .await;
4535
let s: Option< String> = sqlx:: query_scalar(
3b1bc27f Andy McCall 20 days ago
4536
" SELECT status FROM flow_runs WHERE repo_id = $1 AND bookmark = 'diffusion/deps/lib' ORDER BY id DESC LIMIT 1" ,
f4d6ef41 Andy McCall 23 days ago ↶
4537
)
4538
.bind ( app_row.id)
4539
.fetch_optional ( & h.app.db)
4540
.await
4541
.unwrap ( ) ;
4542
if s.as_deref ( ) = = Some ( " passed" ) {
4543
passed = true ;
4544
break ;
4545
}
4546
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4547
}
4548
assert! ( passed, " the app's run on the bump passed" ) ;
4549
assert_eq! ( oxygenhub:: diffusion:: auto_land( & h.app, & app_row, " deps/lib" ) .await, Err ( oxygenhub:: diffusion:: Held:: Unverified) ) ;
4550
let open = db:: proposals:: list( & h.app.db, app_row.id, & [ " open" ] ) .await.unwrap ( ) ;
4551
assert! ( open.iter ( ) .any ( | p | p.source_bookmark = = " diffusion/deps/lib" ) , " amber waits for a person" ) ;
4552
}
e8fa5369 Andy McCall 23 days ago ↶
4553
4554
4555
4556
4557
4558
# [ tokio ::test ( flavor = " multi_thread" ) ]
4559
async fn one_job_reruns_with_what_depends_on_it ( ) {
4560
let h = hub ( ) .await;
4561
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4562
create ( & h.app, " andy" , " flaky" ) .await;
4563
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4564
let c = client ( & format! ( " {} /andy/flaky" , h.base) , work.path ( ) ) ;
4565
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4566
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
4567
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
4568
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4569
let run = runs_of ( & h.app, " andy" , " flaky" ) .await[ 0 ] .clone ( ) ;
4570
4571
let ( st, _ ) =
4572
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/test/rerun" , run.number) , Some ( " site" ) , None ) .await;
4573
assert_eq! ( st, StatusCode:: CONFLICT , " not while the run is live" ) ;
4574
while let Some ( j) = claim ( & h.app) .await {
4575
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4576
let ( name, ph) = ( j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4577
let exit = if name = = " test" { 1 } else { 0 } ;
4578
let log = serde_json:: json! ( { " records" : [ { " t" : 1 , " stream" : " out" , " text" : format! ( " {name} ran" ) } ] } ) ;
4579
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( log.to_string ( ) ) ) .await;
4580
assert_eq! (
4581
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : exit, " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, exit as u32 ) } ) )
4582
.await,
4583
StatusCode:: NO_CONTENT
4584
) ;
4585
}
4586
let status = | n : & 'static str | {
4587
let ( app, run_id) = ( h.app.clone ( ) , run.id) ;
4588
async move { db:: flow:: jobs( & app.db, run_id) .await.unwrap ( ) .into_iter ( ) .find ( | j | j.name = = n ) .unwrap ( ) }
4589
} ;
4590
assert_eq! ( db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) .status, " failed" ) ;
4591
let failed_test = status ( " test" ) .await;
4592
4593
4594
let ( st, body) =
4595
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/test/rerun" , run.number) , Some ( " site" ) , None ) .await;
4596
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4597
assert! ( body.contains ( r # ""queued":1"# ) , " {body}" ) ;
4598
assert_eq! (
4599
( status ( " build" ) .await.status, status ( " lint" ) .await.status, status ( " test" ) .await.status) ,
4600
( " passed" .into ( ) , " passed" .into ( ) , " queued" .into ( ) )
4601
) ;
4602
assert! ( db:: flow:: log( & h.app.db, failed_test.id, 0 , 100 ) .await.unwrap ( ) .is_empty ( ) , " its old log is gone" ) ;
4603
assert! ( db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) .live ( ) ) ;
4604
let checks = checks_when ( & h.app, run.repo_id, & run.state, 3 ) .await;
4605
assert_eq! ( checks.iter ( ) .find ( | c | c.name = = " flow/test" ) .map ( | c | c.status.as_str ( ) ) , Some ( " queued" ) ) ;
4606
let j = claim ( & h.app) .await.expect ( " the test, queued again" ) ;
4607
assert_eq! ( j[ " job" ] [ " name" ] , " test" ) ;
4608
let ph = j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
4609
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4610
assert_eq! (
4611
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( " test" , & ph, 0 ) } ) ) .await,
4612
StatusCode:: NO_CONTENT
4613
) ;
4614
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 22 days ago ↶
4615
4616
let andy_id = db:: accounts:: account_by_name( & h.app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
4617
let kinds: Vec< String> = db:: notify:: inbox( & h.app.db, andy_id, 20 ) .await.unwrap ( ) .into_iter ( ) .map ( | n | n.kind ) .collect ( ) ;
4618
assert! ( kinds.contains ( & " flow.failed" .to_string ( ) ) & & kinds.contains ( & " flow.fixed" .to_string ( ) ) , " {kinds:?}" ) ;
e8fa5369 Andy McCall 23 days ago ↶
4619
4620
4621
let ( _ , body) =
4622
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/build/rerun" , run.number) , Some ( " site" ) , None ) .await;
4623
assert! ( body.contains ( r # ""queued":2"# ) , " {body}" ) ;
4624
assert_eq! (
4625
( status ( " build" ) .await.status, status ( " lint" ) .await.status, status ( " test" ) .await.status) ,
4626
( " queued" .into ( ) , " passed" .into ( ) , " queued" .into ( ) )
4627
) ;
4628
let ( st, _ ) =
4629
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/deploy/rerun" , run.number) , Some ( " site" ) , None ) .await;
4630
assert_eq! ( st, StatusCode:: CONFLICT , " live again, and no such job either way" ) ;
4631
}
4a4400c5 Andy McCall 23 days ago ↶
4632
4633
4634
4635
4636
4637
# [ tokio ::test ( flavor = " multi_thread" ) ]
4638
async fn failed_runs_are_told_and_an_erroring_runner_is_drained ( ) {
4639
let h = hub ( ) .await;
4640
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4641
let bob = db:: accounts:: create_account( & h.app.db, " bob" , " [email protected] " , " x" , " salt" , false ) .await.unwrap ( ) ;
4642
create ( & h.app, " andy" , " alerts" ) .await;
4643
let row = db:: repos:: get( & h.app.db, " andy" , " alerts" ) .await.unwrap ( ) .unwrap ( ) ;
4644
db:: notify:: set_watch( & h.app.db, bob.id, row.id, true ) .await.unwrap ( ) ;
4645
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4646
let c = client ( & format! ( " {} /andy/alerts" , h.base) , work.path ( ) ) ;
4647
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4648
write ( work.path ( ) , " test.sh" , " exit 1\n " ) ;
4649
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
4650
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4651
let run = runs_of ( & h.app, " andy" , " alerts" ) .await[ 0 ] .clone ( ) ;
3b1bc27f Andy McCall 20 days ago
4652
sqlx:: query( " UPDATE flow_runs SET actor = 'andy' WHERE id = $1" ) .bind ( run.id) .execute ( & h.app.db) .await.unwrap ( ) ;
4a4400c5 Andy McCall 23 days ago ↶
4653
while let Some ( j) = claim ( & h.app) .await {
4654
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4655
let ( name, ph) = ( j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4656
let exit = if name = = " test" { 1 } else { 0 } ;
4657
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : exit, " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, exit as u32 ) } ) ) .await;
4658
}
4659
let told = | id : i64 | {
4660
let db = h.app.db.clone ( ) ;
4661
async move { db:: notify:: inbox( & db, id, 20 ) .await.unwrap ( ) .into_iter ( ) .filter ( | n | n.kind = = " flow.failed" ) .collect:: < Vec< _ > > ( ) }
4662
} ;
4663
let ( for_andy, for_bob) = ( told ( andy.id) .await, told ( bob.id) .await) ;
4664
assert_eq! ( ( for_andy.len ( ) , for_bob.len ( ) ) , ( 1 , 1 ) , " the pusher and a watcher" ) ;
4665
assert! ( for_andy[ 0 ] .title.contains ( & format! ( " run #{} on main failed (test)" , run.number) ) , " {}" , for_andy[ 0 ] .title) ;
4666
assert_eq! ( for_andy[ 0 ] .url, format! ( " /andy/alerts/flow/{} " , run.number) ) ;
4667
4668
4669
let runner_id = | | {
4670
let db = h.app.db.clone ( ) ;
4671
async move { sqlx:: query_scalar:: < _ , i64 > ( " SELECT id FROM flow_runners WHERE name = 'test-1'" ) .fetch_one ( & db) .await.unwrap ( ) }
4672
} ;
4673
let mut errors = 0 ;
4674
while errors < 5 {
4675
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/alerts/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
4676
assert_eq! ( st, StatusCode:: CREATED ) ;
4677
while let Some ( j) = claim ( & h.app) .await {
4678
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4679
let st = finish (
4680
& h.app,
4681
id,
4682
serde_json:: json! ( { " exit_code" : null, " duration_ms" : 10 , " error" : " the microVM did not start: /dev/kvm busy" } ) ,
4683
)
4684
.await;
4685
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
4686
errors + = 1 ;
4687
if errors = = 5 {
4688
break ;
4689
}
4690
}
4691
}
4692
let state = | id : i64 | {
4693
let db = h.app.db.clone ( ) ;
4694
async move { db:: runners:: get( & db, id) .await.unwrap ( ) .unwrap ( ) .state }
4695
} ;
4696
let id = runner_id ( ) .await;
4697
assert_eq! ( state ( id) .await, " draining" ) ;
4698
let drained: Vec< _ > =
4699
db:: notify:: inbox( & h.app.db, andy.id, 50 ) .await.unwrap ( ) .into_iter ( ) .filter ( | n | n.kind = = " flow.runner_drained" ) .collect ( ) ;
4700
assert_eq! ( drained.len ( ) , 1 , " the administrator is told" ) ;
4701
assert! ( drained[ 0 ] .title.contains ( " /dev/kvm busy" ) , " {}" , drained[ 0 ] .title) ;
4702
assert! (
4703
! told ( bob.id) .await.is_empty ( )
4704
& & db:: notify:: inbox( & h.app.db, bob.id, 50 ) .await.unwrap ( ) .iter ( ) .all ( | n | n.kind ! = " flow.runner_drained" )
4705
) ;
4706
4707
db:: runners:: set_state( & h.app.db, id, " active" ) .await.unwrap ( ) ;
4708
assert_eq! ( db:: runners:: erroring( & h.app.db, id, 5 ) .await.unwrap ( ) , None ) ;
f7bcee49 Andy McCall 22 days ago ↶
4709
}
4710
4711
4712
4713
# [ tokio ::test ( flavor = " multi_thread" ) ]
4714
async fn a_landed_proposal_runs_its_targets_pipeline ( ) {
4715
let h = hub ( ) .await;
4716
create ( & h.app, " andy" , " demo" ) .await;
4717
let url = format! ( " {} /andy/demo" , h.base) ;
4718
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4719
let mut c = client ( & url, work.path ( ) ) ;
4720
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4721
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
4722
c.snapshot ( " test" ) .unwrap ( ) ;
4723
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
4724
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4725
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , 1 ) ;
4726
c.bookmark_create ( " feature" , None ) .unwrap ( ) ;
4727
c.bookmark_switch ( " feature" , false ) .unwrap ( ) ;
4728
write ( work.path ( ) , " b.txt" , " more\n " ) ;
4729
c.snapshot ( " test" ) .unwrap ( ) ;
4730
c.commit ( " More" , & [ ] ) .unwrap ( ) ;
4731
assert! ( c.push_bookmark ( " origin" , " feature" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4732
let runs_before =
4733
db:: flow:: runs( & h.app.db, db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) .id, 10 , None ) .await.unwrap ( ) .len ( ) ;
4734
4735
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4736
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
4737
db:: proposals:: create(
4738
& h.app.db,
4739
db:: proposals:: NewProposal {
4740
repo_id: row.id,
4741
title: " More" ,
4742
body: " " ,
4743
author_id: Some ( andy.id) ,
4744
author: " andy" ,
4745
draft: false ,
4746
target: " main" ,
4747
source_kind: " bookmark" ,
4748
source_repo_id: Some ( row.id) ,
4749
source_bookmark: " feature" ,
4750
source_changes: " " ,
4751
} ,
4752
)
4753
.await
4754
.unwrap ( ) ;
4755
let repo = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
2a8b6216 Andy McCall 18 days ago ↶
4756
let state = oxygenhub:: web:: proposals:: land_as( & h.app, & repo, & row, & andy, 1 , " web" , false , None ) .await.ok ( ) .expect ( " landed" ) ;
f7bcee49 Andy McCall 22 days ago ↶
4757
let mut runs = vec! [ ] ;
4758
for _ in 0 .. 100 {
4759
runs = db:: flow:: runs( & h.app.db, row.id, 10 , None ) .await.unwrap ( ) ;
4760
if runs.len ( ) > runs_before {
4761
break ;
4762
}
4763
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4764
}
4765
let landed = runs.iter ( ) .find ( | r | r.bookmark = = " main" & & r.state = = state ) .expect ( " a run of the landed state on main" ) ;
4766
assert_eq! ( ( landed.trigger.as_str ( ) , landed.actor.as_str ( ) ) , ( " push" , " andy" ) ) ;
4a4400c5 Andy McCall 23 days ago ↶
4767
}
1870e4fa Andy McCall 21 days ago ↶
4768
4769
4770
4771
# [ tokio ::test ( flavor = " multi_thread" ) ]
4772
async fn owners_run_flow_on_their_own_runners_only ( ) {
4773
use oxy_core:: identity:: SecretKey;
4774
let h = hub ( ) .await;
4775
let register = | name : & 'static str | {
4776
let app = h.app.clone ( ) ;
4777
async move {
c8684d44 Andy McCall 20 days ago
4778
let body = format! ( " username={name} &email={name} %40example.com&password=correct-horse-battery&setup=test-setup" ) ;
1870e4fa Andy McCall 21 days ago ↶
4779
let res = oxygenhub:: router( app)
4780
.oneshot (
4781
Request:: post( " /register" )
4782
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
4783
.body ( Body:: from( body) )
4784
.unwrap ( ) ,
4785
)
4786
.await
4787
.unwrap ( ) ;
4788
res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( )
4789
}
4790
} ;
4791
register ( " andy" ) .await;
4792
let bob = register ( " bob" ) .await;
4793
create ( & h.app, " bob" , " game" ) .await;
4794
let web = | method : & 'static str , path : & str , body : Option< String> | {
4795
let ( app, cookie, path) = ( h.app.clone ( ) , bob.clone ( ) , path.to_string ( ) ) ;
4796
async move {
c7067c7b Andy McCall 16 days ago
4797
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
1870e4fa Andy McCall 21 days ago ↶
4798
if body.is_some ( ) {
4799
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
4800
}
c7067c7b Andy McCall 16 days ago
4801
let req = req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ;
4802
let mut res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
4803
4804
let back =
4805
res.headers ( ) .get ( header:: LOCATION ) .and_then ( | v | v.to_str ( ) .ok ( ) ) .filter ( | l | l.contains ( " /settings/" ) ) .map ( String :: from) ;
4806
if let Some ( to) = back {
4807
let req = Request:: get( to) .header ( header:: COOKIE , cookie) .body ( Body:: empty( ) ) .unwrap ( ) ;
4808
res = oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) ;
4809
}
1870e4fa Andy McCall 21 days ago ↶
4810
String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( )
4811
}
4812
} ;
4813
let csrf_of = | page : & str | {
4814
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
4815
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
4816
} ;
4817
c7067c7b Andy McCall 16 days ago
4818
let page = web ( " GET" , " /bob/game/settings/flow" , None ) .await;
1870e4fa Andy McCall 21 days ago ↶
4819
assert! ( page.contains ( " register a runner for this repository" ) & & ! page.contains ( " value=\" own\" " ) , " {page}" ) ;
4820
let page = web ( " POST" , " /bob/game/settings/flow" , Some ( format! ( " _csrf={} &flow=own" , csrf_of ( & page) ) ) ) .await;
4821
assert! ( page.contains ( " Register a runner for this repository" ) , " {page}" ) ;
4822
4823
let page = web ( " GET" , " /bob/game/settings" , None ) .await;
4824
let page = web ( " POST" , " /bob/game/settings/runners/token" , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
4825
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
4826
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
4827
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
4828
let identity = SecretKey:: generate( ) .identity ( " bobs" , " " , 1 ) ;
4829
let reg = serde_json:: json! ( { " token" : token, " name" : " bobs" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
4830
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
4831
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4832
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
4833
c7067c7b Andy McCall 16 days ago
4834
let page = web ( " GET" , " /bob/game/settings/flow" , None ) .await;
1870e4fa Andy McCall 21 days ago ↶
4835
assert! ( page.contains ( " value=\" own\" " ) & & ! page.contains ( " value=\" on\" " ) , " {page}" ) ;
4836
let page = web ( " POST" , " /bob/game/settings/flow" , Some ( format! ( " _csrf={} &flow=on" , csrf_of ( & page) ) ) ) .await;
4837
assert! ( page.contains ( " Only site administrators let the site's runners" ) , " {page}" ) ;
4838
let page = web ( " POST" , " /bob/game/settings/flow" , Some ( format! ( " _csrf={} &flow=own" , csrf_of ( & page) ) ) ) .await;
4839
assert! ( page.contains ( " value=\" own\" checked" ) & & page.contains ( " start runs" ) , " {page}" ) ;
4840
assert_eq! ( db:: repos:: get( & h.app.db, " bob" , " game" ) .await.unwrap ( ) .unwrap ( ) .flow, " own" ) ;
4841
4842
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4843
let c = client ( & format! ( " {} /bob/game" , h.base) , work.path ( ) ) ;
4844
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4845
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
4846
c.snapshot ( " t" ) .unwrap ( ) ;
4847
c.commit ( " A pipeline" , & [ ] ) .unwrap ( ) ;
4848
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4849
assert_eq! ( runs_of ( & h.app, " bob" , " game" ) .await.len ( ) , 1 , " a run started" ) ;
4850
assert! ( claim ( & h.app) .await.is_none ( ) , " the site's runner is not offered it" ) ;
4851
let body = serde_json:: json! ( { " runner" : " ignored" , " runtimes" : [ " microvm" ] , " info" : info } ) .to_string ( ) ;
4852
let ( st, job) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( & credential) , Some ( body) ) .await;
4853
assert_eq! ( st, StatusCode:: OK , " {job}" ) ;
4854
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & job) .unwrap ( ) [ " repo" ] , " bob/game" ) ;
4855
}
cd437c4f Andy McCall 21 days ago ↶
4856
4857
# [ tokio ::test ( flavor = " multi_thread" ) ]
4858
async fn a_job_takes_the_artifact_its_producer_kept ( ) {
4859
let h = hub ( ) .await;
4860
create ( & h.app, " andy" , " demo" ) .await;
4861
let url = format! ( " {} /andy/demo" , h.base) ;
4862
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4863
let c = client ( & url, work.path ( ) ) ;
4864
write (
4865
work.path ( ) ,
4866
" .flow/pipeline.kdl" ,
4867
& format! (
4868
" 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 "
4869
) ,
4870
) ;
4871
c.snapshot ( " test" ) .unwrap ( ) ;
4872
c.commit ( " Plan, then apply that plan" , & [ ] ) .unwrap ( ) ;
4873
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4874
4875
4876
let runs = runs_of ( & h.app, " andy" , " demo" ) .await;
4877
assert_eq! ( runs.len ( ) , 1 , " a run" ) ;
4878
let jobs = db:: flow:: jobs( & h.app.db, runs[ 0 ] .id) .await.unwrap ( ) ;
4879
assert! ( ! jobs.is_empty ( ) , " jobs: {:?}" , runs[ 0 ] ) ;
4880
let p = claim ( & h.app)
4881
.await
4882
.unwrap_or_else ( | | panic! ( " nothing to claim: {:?}" , jobs.iter ( ) .map ( | j | ( & j.name, & j.status, & j.error) ) .collect:: < Vec< _ > > ( ) ) ) ;
4883
assert_eq! ( p[ " job" ] [ " name" ] , " plan" ) ;
4884
let plan_id = p[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4885
let plan = b " the reviewed plan" .to_vec ( ) ;
4886
let ( st, text) = upload ( & h.app, plan_id, " tfplan" , & plan) .await;
4887
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
4888
let blob = oxy_core:: repo:: Repo:: blob_ref( & plan) .to_hex ( ) ;
4889
let mut r = receipt ( " plan" , p[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
4890
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
4891
assert_eq! (
4892
finish ( & h.app, plan_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : r } ) ) .await,
4893
StatusCode:: NO_CONTENT
4894
) ;
4895
4896
4897
4898
let a = claim ( & h.app) .await.unwrap ( ) ;
4899
assert_eq! ( a[ " job" ] [ " name" ] , " apply" ) ;
4900
let apply_id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4901
assert_eq! (
4902
a[ " inputs" ] ,
4903
serde_json:: json! ( [ { " artifact" : " tfplan" , " from" : " plan" , " path" : " /src/tfplan" , " blob" : blob } ] ) ,
4904
" the claim names the producer's blob"
4905
) ;
4906
let req = Request:: get( format! ( " /api/v1/flow/jobs/{apply_id} /inputs/0" ) ) .header ( header:: AUTHORIZATION , " Bearer runner-secret" ) ;
4907
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( req.body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
4908
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
4909
assert_eq! ( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) , plan) ;
4910
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/jobs/{apply_id} /inputs/1" ) , Some ( " runner-secret" ) , None ) .await;
4911
assert_eq! ( st, StatusCode:: NOT_FOUND , " no second input" ) ;
4912
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/jobs/{apply_id} /inputs/0" ) , None , None ) .await;
4913
assert_eq! ( st, StatusCode:: UNAUTHORIZED , " runners only" ) ;
4914
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/jobs/{plan_id} /inputs/0" ) , Some ( " runner-secret" ) , None ) .await;
4915
assert_eq! ( st, StatusCode:: CONFLICT , " only a running job's" ) ;
4916
}
417184c7 Andy McCall 21 days ago ↶
4917
4918
# [ tokio ::test ( flavor = " multi_thread" ) ]
4919
async fn a_job_waits_for_approval_and_is_approved_rejected_or_expires ( ) {
4920
let h = hub ( ) .await;
4921
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4922
let bob = db:: accounts:: create_account( & h.app.db, " bob" , " [email protected] " , " x" , " salt" , false ) .await.unwrap ( ) ;
4923
let carol = db:: accounts:: create_account( & h.app.db, " carol" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4924
create ( & h.app, " andy" , " gated" ) .await;
4925
let row = db:: repos:: get( & h.app.db, " andy" , " gated" ) .await.unwrap ( ) .unwrap ( ) ;
4926
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4927
let c = client ( & format! ( " {} /andy/gated" , h.base) , work.path ( ) ) ;
4928
write (
4929
work.path ( ) ,
4930
" .flow/pipeline.kdl" ,
4931
& format! (
47e37402 Andy McCall 20 days ago ↶
4932
" 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 ↶
4933
) ,
4934
) ;
4935
let mut n = 0 ;
4936
let mut push = | | {
4937
n + = 1 ;
4938
write ( work.path ( ) , " n.txt" , & format! ( " {n} \n " ) ) ;
4939
c.snapshot ( " test" ) .unwrap ( ) ;
4940
c.commit ( & format! ( " Push {n} " ) , & [ ] ) .unwrap ( ) ;
4941
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4942
} ;
4943
4944
let plan_passes = | number : i64 | {
4945
let app = h.app.clone ( ) ;
4946
let row = row.clone ( ) ;
4947
async move {
4948
let mut run = None ;
4949
for _ in 0 .. 100 {
4950
if let Some ( r) = db:: flow:: run( & app.db, row.id, number) .await.unwrap ( ) {
4951
run = Some ( r) ;
4952
break ;
4953
}
4954
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4955
}
4956
let run = run.expect ( " the run" ) ;
3b1bc27f Andy McCall 20 days ago
4957
sqlx:: query( " UPDATE flow_runs SET actor = 'andy' WHERE id = $1" ) .bind ( run.id) .execute ( & app.db) .await.unwrap ( ) ;
417184c7 Andy McCall 21 days ago ↶
4958
let j = claim ( & app) .await.expect ( " plan is claimed" ) ;
4959
assert_eq! ( j[ " job" ] [ " name" ] , " plan" ) ;
4960
let ( id, ph) = ( j[ " job_id" ] .as_i64 ( ) .unwrap ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4961
finish ( & app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( " plan" , & ph, 0 ) } ) ) .await;
4962
db:: flow:: run( & app.db, row.id, number) .await.unwrap ( ) .unwrap ( )
4963
}
4964
} ;
4965
4966
4967
push ( ) ;
4968
let run = runs_of ( & h.app, " andy" , " gated" ) .await[ 0 ] .clone ( ) ;
7e01a766 Andy McCall 21 days ago ↶
4969
33d98147 Andy McCall 17 days ago
4970
let heard = | topic : String| match h.app.bus.live.since ( 0 , & [ topic] ) {
7e01a766 Andy McCall 21 days ago ↶
4971
oxygenhub:: events:: Replay:: Events( e) => e.into_iter ( ) .map ( | e | e.kind ) .collect:: < Vec< _ > > ( ) ,
4972
oxygenhub:: events:: Replay:: Reload => vec! [ " reload" .into ( ) ] ,
4973
} ;
4974
assert! ( heard ( oxygenhub:: events:: topic:: repo( row.id) ) .contains ( & " push" .to_string ( ) ) , " the push is on the repository's topic" ) ;
3b1bc27f Andy McCall 20 days ago
4975
sqlx:: query( " UPDATE flow_runs SET actor = 'andy' WHERE id = $1" ) .bind ( run.id) .execute ( & h.app.db) .await.unwrap ( ) ;
05f1a85a Andy McCall 19 days ago ↶
4976
let early = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & carol, true , None ) .await.unwrap_err ( ) ;
417184c7 Andy McCall 21 days ago ↶
4977
assert! ( early.contains ( " waits for plan first" ) , " {early}" ) ;
4978
let run = plan_passes ( run.number) .await;
4979
assert! ( claim ( & h.app) .await.is_none ( ) , " apply waits for a person, so no runner gets it" ) ;
7e01a766 Andy McCall 21 days ago ↶
4980
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 ↶
4981
4982
4983
let told = | id : i64 | {
4984
let db = h.app.db.clone ( ) ;
4985
async move { db:: notify:: inbox( & db, id, 20 ) .await.unwrap ( ) .into_iter ( ) .filter ( | n | n.kind = = " flow.approval" ) .count ( ) }
4986
} ;
4987
assert_eq! ( ( told ( carol.id) .await, told ( andy.id) .await, told ( bob.id) .await) , ( 1 , 0 , 0 ) ) ;
417184c7 Andy McCall 21 days ago ↶
4988
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/gated/flow/{} " , run.number) , None , None ) .await;
4989
assert! ( page.contains ( " waits for approval by someone with the <em>maintain</em> role, other than andy" ) , " the run page says so" ) ;
05f1a85a Andy McCall 19 days ago ↶
4990
let own = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & andy, true , None ) .await.unwrap_err ( ) ;
417184c7 Andy McCall 21 days ago ↶
4991
assert! ( own.contains ( " someone other than andy" ) , " {own}" ) ;
05f1a85a Andy McCall 19 days ago ↶
4992
let no_role = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & bob, true , None ) .await.unwrap_err ( ) ;
417184c7 Andy McCall 21 days ago ↶
4993
assert! ( no_role.contains ( " takes the maintain role" ) , " {no_role}" ) ;
05f1a85a Andy McCall 19 days ago ↶
4994
assert_eq! ( oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & carol, true , None ) .await.unwrap ( ) , " apply: approved by carol" ) ;
dc59d1b5 Andy McCall 21 days ago ↶
4995
assert_eq! ( told ( carol.id) .await, 1 , " told once" ) ;
417184c7 Andy McCall 21 days ago ↶
4996
let j = claim ( & h.app) .await.expect ( " approved, it runs" ) ;
4997
assert_eq! ( j[ " job" ] [ " name" ] , " apply" ) ;
b644e175 Andy McCall 21 days ago ↶
4998
4999
let token =
5000
j[ " secrets" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | s | s[ " path" ] = = " /run/secrets/cloud" ) .unwrap ( ) [ " value" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5001
let claims = h.app.oidc ( ) .await.unwrap ( ) .verify ( & token, " https://sts.example" , db:: now_ms( ) ) .expect ( " a valid token" ) ;
5002
assert_eq! ( claims[ " environment" ] , " prod" ) ;
417184c7 Andy McCall 21 days ago ↶
5003
let ( id, ph) = ( j[ " job_id" ] .as_i64 ( ) .unwrap ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
5004
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( " apply" , & ph, 0 ) } ) ) .await;
5005
let audit = db:: accounts:: audit_rows( & h.app.db, 50 ) .await.unwrap ( ) ;
5006
assert! ( audit.iter ( ) .any ( | r | r.action = = " flow.approved" & & r.actor = = " carol" & & r.target.ends_with ( " #1 apply" ) ) , " audited" ) ;
5007
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/gated/flow/{} " , run.number) , None , None ) .await;
5008
assert! ( page.contains ( " approved by carol" ) , " the run says who" ) ;
b644e175 Andy McCall 21 days ago ↶
5009
let ( _ , page) = call ( & h.app, " GET" , " /andy/gated/deployments" , None , None ) .await;
5010
assert! ( page.contains ( " <strong>prod</strong>" ) & & page.contains ( " >#1</a>" ) & & page.contains ( " <td>carol</td>" ) , " the Deployments page" ) ;
5011
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/gated/deployments" , Some ( " site" ) , None ) .await;
5012
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
5013
assert_eq! ( ( list[ 0 ] [ " environment" ] .as_str ( ) , list[ 0 ] [ " approved_by" ] .as_str ( ) ) , ( Some ( " prod" ) , Some ( " carol" ) ) ) ;
417184c7 Andy McCall 21 days ago ↶
5014
05f1a85a Andy McCall 19 days ago ↶
5015
5016
5017
417184c7 Andy McCall 21 days ago ↶
5018
push ( ) ;
5019
let run = plan_passes ( 2 ) .await;
05f1a85a Andy McCall 19 days ago ↶
5020
let ( secret, hash) = oxygenhub:: auth:: new_token( ) ;
5021
let tok = db:: accounts:: create_token( & h.app.db, carol.id, " claude-code" , & hash, " repo:write" , " " , None ) .await.unwrap ( ) ;
5022
db:: accounts:: set_token_agent( & h.app.db, carol.id, tok.id, true ) .await.unwrap ( ) ;
4a68f106 Andy McCall 18 days ago ↶
5023
5024
05f1a85a Andy McCall 19 days ago ↶
5025
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/gated/flow/runs/2/jobs/apply/reject" , Some ( & secret) , Some ( " {}" .into ( ) ) ) .await;
4a68f106 Andy McCall 18 days ago ↶
5026
assert_eq! ( st, StatusCode:: CONFLICT , " {body}" ) ;
5027
assert! ( body.contains ( " decided by a person here" ) & & body.contains ( " Settings › OxygenFlow" ) , " {body}" ) ;
5028
let gated = db:: repos:: get( & h.app.db, " andy" , " gated" ) .await.unwrap ( ) .unwrap ( ) ;
5029
db:: repos:: set_agents_decide_gates( & h.app.db, gated.id, true ) .await.unwrap ( ) ;
5030
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/gated/flow/runs/2/jobs/apply/reject" , Some ( & secret) , Some ( " {}" .into ( ) ) ) .await;
05f1a85a Andy McCall 19 days ago ↶
5031
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5032
assert! ( body.contains ( " rejected by carol via claude-code" ) , " {body}" ) ;
417184c7 Andy McCall 21 days ago ↶
5033
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
5034
let apply = jobs.iter ( ) .find ( | j | j.name = = " apply" ) .unwrap ( ) ;
05f1a85a Andy McCall 19 days ago ↶
5035
assert_eq! ( ( apply.status.as_str ( ) , apply.error.as_str ( ) ) , ( " cancelled" , " rejected by carol via claude-code" ) ) ;
5036
assert_eq! ( ( apply.approved_by.as_str ( ) , apply.approved_via.as_str ( ) ) , ( " carol" , " claude-code" ) ) ;
5037
let audit = db:: accounts:: audit_rows( & h.app.db, 50 ) .await.unwrap ( ) ;
5038
assert! (
5039
audit.iter ( ) .any ( | r | r.action = = " flow.rejected" & & r.actor = = " carol" & & r.detail.contains ( " via claude-code" ) ) ,
5040
" audited with the agent"
5041
) ;
417184c7 Andy McCall 21 days ago ↶
5042
assert! ( claim ( & h.app) .await.is_none ( ) ) ;
5043
5044
5045
push ( ) ;
5046
let run = plan_passes ( 3 ) .await;
5047
assert_eq! ( oxygenhub:: flow:: expire_approvals( & h.app, db:: now_ms( ) + 3_600_000 ) .await.unwrap ( ) , 0 , " not yet" ) ;
5048
assert_eq! ( oxygenhub:: flow:: expire_approvals( & h.app, db:: now_ms( ) + 25 * 3_600_000 ) .await.unwrap ( ) , 1 ) ;
5049
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
5050
let apply = jobs.iter ( ) .find ( | j | j.name = = " apply" ) .unwrap ( ) ;
5051
assert_eq! ( ( apply.status.as_str ( ) , apply.error.as_str ( ) ) , ( " cancelled" , " no approval within 24h" ) ) ;
5052
}
67be8ac0 Andy McCall 21 days ago ↶
5053
5054
# [ tokio ::test ( flavor = " multi_thread" ) ]
5055
async fn live_streams_only_topics_the_viewer_may_hear_and_replays ( ) {
5056
let h = hub ( ) .await;
5057
create ( & h.app, " andy" , " open" ) .await;
5058
h.app.repos.create ( & RepoName:: parse( " andy/shut" ) .unwrap ( ) , None ) .unwrap ( ) ;
5059
db:: repos:: create( & h.app.db, " andy" , " shut" , " " , hub_api:: Visibility:: Private) .await.unwrap ( ) ;
5060
let open = db:: repos:: get( & h.app.db, " andy" , " open" ) .await.unwrap ( ) .unwrap ( ) ;
5061
let shut = db:: repos:: get( & h.app.db, " andy" , " shut" ) .await.unwrap ( ) .unwrap ( ) ;
33d98147 Andy McCall 17 days ago
5062
let live = & h.app.bus.live;
67be8ac0 Andy McCall 21 days ago ↶
5063
let first = live.publish ( & oxygenhub:: events:: topic:: repo( open.id) , " push" ) ;
5064
live.publish ( & oxygenhub:: events:: topic:: repo( shut.id) , " push" ) ;
5065
live.publish ( & oxygenhub:: events:: topic:: proposal( open.id, 3 ) , " review" ) ;
5066
5067
5068
let read = | path : String, last : Option< u64 > | {
5069
let app = h.app.clone ( ) ;
5070
async move {
5071
let mut req = Request:: get( & path) ;
5072
if let Some ( l) = last {
5073
req = req.header ( " last-event-id" , l.to_string ( ) ) ;
5074
}
5075
let res = oxygenhub:: router( app) .oneshot ( req.body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
5076
let status = res.status ( ) ;
5077
let mut body = res.into_body ( ) ;
5078
let mut text = String :: new( ) ;
5079
while let Ok ( Some ( Ok ( frame) ) ) = tokio:: time:: timeout( std:: time:: Duration:: from_millis( 300 ) , body.frame ( ) ) .await {
5080
if let Some ( d) = frame.data_ref ( ) {
5081
text.push_str ( & String :: from_utf8_lossy( d) ) ;
5082
}
5083
}
5084
( status, text)
5085
}
5086
} ;
5087
let topics = format! ( " /live?topics=repo:{} ,repo:{} /proposal:3" , open.id, open.id) ;
5088
5089
let ( st, text) = read ( topics.clone ( ) , Some ( first.id) ) .await;
5090
assert_eq! ( st, StatusCode:: OK ) ;
5091
assert! ( text.contains ( " event: change" ) & & text.contains ( & format! ( " \" topic\" :\" repo:{} /proposal:3\" " , open.id) ) , " {text}" ) ;
5092
assert! ( ! text.contains ( & format! ( " \" topic\" :\" repo:{} \" " , shut.id) ) , " never another topic: {text}" ) ;
5093
assert! ( ! text.contains ( & format! ( " id: {} \n " , first.id) ) , " not what was already seen: {text}" ) ;
5094
5095
let ( st, _ ) = read ( format! ( " /live?topics=repo:{} ,repo:{} " , open.id, shut.id) , None ) .await;
5096
assert_eq! ( st, StatusCode:: FORBIDDEN ) ;
5097
assert_eq! ( read ( " /live?topics=user:1" .into ( ) , None ) .await.0 , StatusCode:: FORBIDDEN ) ;
5098
assert_eq! ( read ( " /live?topics=nonsense" .into ( ) , None ) .await.0 , StatusCode:: BAD_REQUEST ) ;
5099
assert_eq! ( read ( " /live?topics=" .into ( ) , None ) .await.0 , StatusCode:: BAD_REQUEST ) ;
5100
5101
for _ in 0 .. oxygenhub:: events:: REPLAY {
5102
live.publish ( " repo:999999" , " push" ) ;
5103
}
5104
let ( _ , text) = read ( topics, Some ( first.id) ) .await;
5105
assert! ( text.contains ( " event: reload" ) , " {text}" ) ;
5106
}
47e37402 Andy McCall 20 days ago ↶
5107
5108
# [ tokio ::test ( flavor = " multi_thread" ) ]
5109
async fn a_sealed_approval_releases_exactly_the_reviewed_plan ( ) {
5110
use oxy_core:: approval:: { SignedApproval, Statement} ;
5111
use oxy_core:: hash:: ObjectRef;
5112
use oxy_core:: identity:: SecretKey;
5113
let h = hub ( ) .await;
5114
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
5115
let carol = db:: accounts:: create_account( & h.app.db, " carol" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
5116
let sk = SecretKey:: generate( ) ;
5117
let carol_id = sk.identity ( " Carol" , " [email protected] " , 1 ) ;
5118
db:: accounts:: link_identity( & h.app.db, carol.id, & carol_id.hash ( ) .to_hex ( ) , " Carol" , " [email protected] " , & carol_id.encode ( ) )
5119
.await
5120
.unwrap ( ) ;
5121
create ( & h.app, " andy" , " infra" ) .await;
5122
let row = db:: repos:: get( & h.app.db, " andy" , " infra" ) .await.unwrap ( ) .unwrap ( ) ;
5123
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5124
let c = client ( & format! ( " {} /andy/infra" , h.base) , work.path ( ) ) ;
5125
write (
5126
work.path ( ) ,
5127
" .flow/pipeline.kdl" ,
5128
& format! (
5129
" 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 "
5130
) ,
5131
) ;
5132
c.snapshot ( " test" ) .unwrap ( ) ;
5133
c.commit ( " Plan, then apply" , & [ ] ) .unwrap ( ) ;
5134
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5135
let run = runs_of ( & h.app, " andy" , " infra" ) .await[ 0 ] .clone ( ) ;
5136
let p = claim ( & h.app) .await.unwrap ( ) ;
5137
let plan_id = p[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
5138
let plan = b " the reviewed plan" .to_vec ( ) ;
5139
upload ( & h.app, plan_id, " tfplan" , & plan) .await;
5140
let mut r = receipt ( " plan" , p[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
5141
r[ " outputs" ] = serde_json:: json! ( [ oxy_core:: repo:: Repo:: blob_ref( & plan) .to_hex ( ) ] ) ;
5142
finish ( & h.app, plan_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : r } ) ) .await;
5143
let run = db:: flow:: run( & h.app.db, row.id, run.number) .await.unwrap ( ) .unwrap ( ) ;
5144
5145
05f1a85a Andy McCall 19 days ago ↶
5146
let plain = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & carol, true , None ) .await.unwrap_err ( ) ;
47e37402 Andy McCall 20 days ago ↶
5147
assert! ( plain.contains ( & format! ( " oxy seal --flow {} /andy/infra/flow/{} --job apply" , h.base, run.number) ) , " {plain}" ) ;
5148
5149
let ( st, body) =
5150
call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/infra/flow/runs/{} /jobs/apply/approval" , run.number) , Some ( " site" ) , None ) .await;
5151
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5152
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
5153
assert_eq! ( v[ " environment" ] , " prod" ) ;
5154
assert_eq! ( v[ " inputs" ] , serde_json:: json! ( [ oxy_core:: repo:: Repo:: blob_ref( & plan) .to_hex ( ) ] ) , " the reviewed plan" ) ;
5155
let mut repo = [ 0 u8 ; 16 ] ;
5156
repo.copy_from_slice ( & hex:: decode( v[ " repo" ] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ) ;
5157
let oref = | k : & str | ObjectRef:: from_hex( v[ k] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ;
5158
let statement = Statement {
5159
repo,
5160
run: v[ " run" ] .as_str ( ) .unwrap ( ) .into ( ) ,
5161
job: " apply" .into ( ) ,
5162
job_hash: oref ( " job_hash" ) ,
5163
state: oref ( " state" ) ,
5164
inputs: v[ " inputs" ] .as_array ( ) .unwrap ( ) .iter ( ) .map ( | i | ObjectRef:: from_hex( i.as_str ( ) .unwrap ( ) ) .unwrap ( ) ) .collect ( ) ,
5165
environment: " prod" .into ( ) ,
5166
approve: true ,
5167
} ;
5168
let seal = | s : & Statement, key : & SecretKey, id : & oxy_core:: identity:: Identity| {
5169
let body = serde_json:: json! ( { " signed" : hex:: encode( SignedApproval:: sign( s, id, key) .encode ( ) ) } ) .to_string ( ) ;
5170
let app = h.app.clone ( ) ;
5171
let path = format! ( " /api/v1/repos/andy/infra/flow/runs/{} /jobs/apply/seal" , run.number) ;
5172
async move { call ( & app, " POST" , & path, Some ( " site" ) , Some ( body) ) .await }
5173
} ;
5174
5175
let other_plan = Statement { inputs: vec! [ ObjectRef( [ 9 ; 32 ] ) ] , .. statement.clone ( ) } ;
5176
let ( st, body) = seal ( & other_plan, & sk, & carol_id) .await;
5177
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " approves something else" ) , " {st} {body}" ) ;
5178
let agent = SecretKey:: generate( ) ;
5179
let agent_id = agent.identity ( " Claude Code" , " [email protected] " , 1 ) ;
5180
let ( st, body) = seal ( & statement, & agent, & agent_id) .await;
5181
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " not linked to an account" ) , " {st} {body}" ) ;
5182
assert! ( claim ( & h.app) .await.is_none ( ) , " still waiting" ) ;
5183
5184
5185
let ( st, body) = seal ( & statement, & sk, & carol_id) .await;
5186
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5187
assert! ( body.contains ( " approved by carol, sealed" ) , " {body}" ) ;
5188
let name = oxygenhub:: repos:: RepoName:: parse( " andy/infra" ) .unwrap ( ) ;
5189
let state = statement.state;
5190
let kept = h.app.repos.read ( & name, move | r, _ | Ok ( r.store ( ) .approvals_for ( & state) ? ) ) .await.unwrap ( ) ;
5191
assert_eq! ( kept.len ( ) , 1 , " the approval is in the repository" ) ;
5192
let j = claim ( & h.app) .await.expect ( " released" ) ;
5193
assert_eq! ( j[ " job" ] [ " name" ] , " apply" ) ;
5194
let token =
5195
j[ " secrets" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | s | s[ " path" ] = = " /run/secrets/cloud" ) .unwrap ( ) [ " value" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5196
let claims = h.app.oidc ( ) .await.unwrap ( ) .verify ( & token, " https://sts.example" , db:: now_ms( ) ) .unwrap ( ) ;
5197
assert_eq! ( ( claims[ " environment" ] .as_str ( ) , claims[ " approved_by" ] .as_str ( ) ) , ( Some ( " prod" ) , Some ( " carol" ) ) ) ;
5198
assert_eq! ( claims[ " approval" ] .as_str ( ) , Some ( kept[ 0 ] .to_hex ( ) .as_str ( ) ) ) ;
5199
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/infra/flow/{} " , run.number) , None , None ) .await;
5200
assert! ( page.contains ( " sealed with their key" ) , " the run page says so" ) ;
5201
}
55673c65 Andy McCall 20 days ago ↶
5202
5203
5204
5205
# [ tokio ::test ( flavor = " multi_thread" ) ]
5206
async fn repositories_take_turns_on_the_runners ( ) {
5207
let h = hub ( ) .await;
5208
for name in [ " busy" , " quiet" ] {
5209
create ( & h.app, " andy" , name) .await;
5210
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5211
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
5212
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5213
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5214
c.snapshot ( " test" ) .unwrap ( ) ;
5215
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5216
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5217
assert_eq! ( runs_of ( & h.app, " andy" , name) .await.len ( ) , 1 ) ;
5218
}
5219
5220
5221
let order: Vec< String> = {
5222
let mut out = Vec :: new( ) ;
5223
for _ in 0 .. 3 {
5224
let c = claim ( & h.app) .await.expect ( " a job to claim" ) ;
5225
out.push ( c[ " repo" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
5226
}
5227
out
5228
} ;
5229
assert_eq! ( order, [ " andy/busy" , " andy/quiet" , " andy/busy" ] ) ;
5230
}
107eaba5 Andy McCall 20 days ago ↶
5231
53a0fb2e Andy McCall 19 days ago ↶
5232
5233
5234
5235
5236
# [ tokio ::test ( flavor = " multi_thread" ) ]
5237
async fn a_pipeline_lowers_its_jobs_and_expediting_still_wins ( ) {
5238
let h = hub ( ) .await;
5239
for ( name, extra) in [ ( " nightly" , " priority \" low\" \n " ) , ( " everyday" , " " ) ] {
5240
create ( & h.app, " andy" , name) .await;
5241
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5242
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
5243
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
5244
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5245
c.snapshot ( " test" ) .unwrap ( ) ;
5246
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5247
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5248
assert_eq! ( runs_of ( & h.app, " andy" , name) .await.len ( ) , 1 ) ;
5249
}
5250
let nightly = db:: repos:: get( & h.app.db, " andy" , " nightly" ) .await.unwrap ( ) .unwrap ( ) ;
5251
db:: repos:: set_flow_priority( & h.app.db, nightly.id, " high" ) .await.unwrap ( ) ;
5252
let q = db:: flow:: queue( & h.app.db) .await.unwrap ( ) ;
5253
assert! (
5254
q.iter ( ) .filter ( | e | e.name = = " nightly" ) .all ( | e | e.priority ( ) = = " low" & & e.priority_note = = " pipeline: low" ) ,
5255
" {:?}" ,
5256
q.iter ( ) .map ( | e | ( & e.name, e.priority ( ) , & e.priority_note) ) .collect:: < Vec< _ > > ( )
5257
) ;
5258
let repo_of = | c : serde_json:: Value| c[ " repo" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5259
5260
5261
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/everyday" ) ;
5262
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/everyday" ) ;
5263
let run = & runs_of ( & h.app, " andy" , " nightly" ) .await[ 0 ] ;
5264
assert! ( db:: flow:: expedite( & h.app.db, run.id, " expedited by andy: needed now" ) .await.unwrap ( ) ) ;
5265
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/nightly" , " an expedited run is urgent whatever its jobs say" ) ;
5266
}
5267
107eaba5 Andy McCall 20 days ago ↶
5268
5269
5270
# [ tokio ::test ( flavor = " multi_thread" ) ]
5271
async fn priority_then_turns_and_an_expedited_run_first ( ) {
5272
let h = hub ( ) .await;
5273
for name in [ " early" , " later" ] {
5274
create ( & h.app, " andy" , name) .await;
5275
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5276
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
5277
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5278
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5279
c.snapshot ( " test" ) .unwrap ( ) ;
5280
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5281
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5282
assert_eq! ( runs_of ( & h.app, " andy" , name) .await.len ( ) , 1 ) ;
5283
}
5284
let early = db:: repos:: get( & h.app.db, " andy" , " early" ) .await.unwrap ( ) .unwrap ( ) ;
5285
db:: repos:: set_flow_priority( & h.app.db, early.id, " low" ) .await.unwrap ( ) ;
5286
5287
let repo_of = | c : serde_json:: Value| c[ " repo" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5288
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/later" ) ;
5289
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/later" ) ;
5290
5291
let run = & runs_of ( & h.app, " andy" , " early" ) .await[ 0 ] ;
5292
assert! ( db:: flow:: expedite( & h.app.db, run.id, " expedited by andy: an incident" ) .await.unwrap ( ) ) ;
5293
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/early" ) ;
5294
let q = db:: flow:: queue( & h.app.db) .await.unwrap ( ) ;
5295
assert! (
5296
q.iter ( ) .any ( | e | e.name = = " early" & & e.priority ( ) = = " urgent" & & e.priority_note.contains ( " an incident" ) ) ,
5297
" {:?}" ,
5298
q.iter ( ) .map ( | e | ( & e.name, e.priority ( ) ) ) .collect:: < Vec< _ > > ( )
5299
) ;
5300
5301
db:: accounts:: set_setting( & h.app.db, " flow_priority" , " high" ) .await.unwrap ( ) ;
5302
let q = db:: flow:: queue( & h.app.db) .await.unwrap ( ) ;
5303
assert! ( q.iter ( ) .filter ( | e | e.name = = " later" ) .all ( | e | e.priority ( ) = = " high" ) ) ;
5304
}
026a76d2 Andy McCall 19 days ago ↶
5305
5306
5307
5308
5309
5310
5311
# [ tokio ::test ( flavor = " multi_thread" ) ]
5312
async fn a_running_job_gets_a_short_certificate_for_a_fresh_key ( ) {
5313
use oxy_core:: cert:: { certify_challenge, SignedCertificate} ;
5314
use oxy_core:: identity:: { Scope, SecretKey} ;
5315
let h = hub ( ) .await;
5316
create ( & h.app, " andy" , " demo" ) .await;
5317
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5318
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
5319
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5320
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5321
c.snapshot ( " test" ) .unwrap ( ) ;
5322
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5323
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5324
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , 1 ) ;
5325
let job = claim ( & h.app) .await.expect ( " a job" ) ;
5326
let token = job[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5327
let job_id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
5328
5329
let sk = SecretKey:: generate( ) ;
5330
let id = sk.identity ( " job" , " [email protected] " , 1 ) ;
5331
let ask = | credential : & str , signed_for : & str | {
5332
let proof = sk.sign_challenge ( & id.hash ( ) , & certify_challenge ( signed_for) ) .unwrap ( ) ;
5333
let body = serde_json:: json! ( {
5334
" identity" : oxy_core:: dsse:: base64( & id.encode ( ) ) ,
5335
" proof" : oxy_core:: dsse:: base64( & oxy_core:: cbor:: encode( & proof.to_value ( ) ) ) ,
5336
" scope" : " changes,refs" ,
5337
} ) ;
5338
let ( credential, app) = ( credential.to_string ( ) , h.app.clone ( ) ) ;
5339
async move { call ( & app, " POST" , " /api/v1/keyless/certify" , Some ( & credential) , Some ( body.to_string ( ) ) ) .await }
5340
} ;
5341
let ( st, body) = ask ( & token, & token) .await;
5342
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5343
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
5344
let sc = SignedCertificate:: decode( & oxy_core:: dsse:: base64_decode( v[ " certificate" ] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ) .unwrap ( ) ;
5345
assert! ( sc.verify ( & h.app.hub_key.identity) .is_ok ( ) , " signed with the hub's key" ) ;
5346
assert_eq! ( sc.cert.key_id ( ) , id.hash ( ) ) ;
5347
assert_eq! ( sc.cert.subject, " repo:andy/demo:bookmark:main" ) ;
5348
assert_eq! ( sc.cert.claim ( " repository" ) , Some ( " andy/demo" ) ) ;
5349
assert! ( sc.cert.claim ( " job" ) .is_some ( ) & & sc.cert.claim ( " run" ) = = Some ( " 1" ) ) ;
5350
assert_eq! ( sc.cert.scope, Scope:: CHANGES | Scope:: REFS ) ;
5351
assert! ( sc.cert.expires - sc.cert.not_before < = 10 * 60_000 + 30_000 , " ten minutes" ) ;
5352
5353
5354
let ( st, body) = ask ( & token, " flowjob_someone_else" ) .await;
5355
assert_eq! ( st, StatusCode:: UNAUTHORIZED , " {body}" ) ;
5356
assert! ( body.contains ( " the proof does not show" ) , " {body}" ) ;
5357
5358
5359
5360
let issuer = h.app.oidc ( ) .await.unwrap ( ) ;
5361
let now = oxygenhub:: db:: now_ms( ) ;
5362
let aud = oxygenhub:: keyless:: audience( h.app.config.base_url ( ) ) ;
5363
let good = issuer.mint_for ( h.app.config.base_url ( ) , " agent:claude@andy" , & aud, now, 600 ) ;
5364
let ( st, body) = ask ( & good, & good) .await;
5365
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5366
assert! ( body.contains ( " \" subject\" :\" agent:claude@andy\" " ) , " {body}" ) ;
5367
let other = issuer.mint_for ( h.app.config.base_url ( ) , " agent:claude@andy" , " sts.amazonaws.com" , now, 600 ) ;
5368
let ( st, _ ) = ask ( & other, & other) .await;
5369
assert_eq! ( st, StatusCode:: UNAUTHORIZED ) ;
5370
5371
5372
sqlx:: query( " UPDATE flow_jobs SET status = 'passed' WHERE id = $1" ) .bind ( job_id) .execute ( & h.app.db) .await.unwrap ( ) ;
5373
let ( st, body) = ask ( & token, & token) .await;
5374
assert_eq! ( st, StatusCode:: UNAUTHORIZED , " {body}" ) ;
5375
}
2c31915f Andy McCall 19 days ago ↶
5376
5377
5378
5379
5380
5381
# [ tokio ::test ( flavor = " multi_thread" ) ]
5382
async fn a_receipt_signed_by_a_certified_key_is_kept ( ) {
5383
use oxy_core:: cert:: { certify_challenge, SignedCertificate} ;
5384
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectType} ;
5385
use oxy_core:: identity:: SecretKey;
5386
use oxy_core:: objects:: StateObject;
5387
use oxy_core:: receipt:: SignedReceipt;
5388
let h = hub ( ) .await;
5389
create ( & h.app, " andy" , " demo" ) .await;
5390
let url = format! ( " {} /andy/demo" , h.base) ;
5391
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5392
let c = client ( & url, work.path ( ) ) ;
5393
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5394
c.snapshot ( " test" ) .unwrap ( ) ;
5395
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5396
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5397
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
5398
let heads: Vec< ChangeHash> = run.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
5399
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads.clone ( ) ) .encode ( ) ) ;
5400
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
5401
5402
5403
5404
let certify = | token : String| {
5405
let app = h.app.clone ( ) ;
5406
async move {
5407
let sk = SecretKey:: generate( ) ;
5408
let id = sk.identity ( " test-1 job" , " " , 1 ) ;
5409
let proof = sk.sign_challenge ( & id.hash ( ) , & certify_challenge ( & token) ) .unwrap ( ) ;
5410
let body = serde_json:: json! ( {
5411
" identity" : oxy_core:: dsse:: base64( & id.encode ( ) ) ,
5412
" proof" : oxy_core:: dsse:: base64( & oxy_core:: cbor:: encode( & proof.to_value ( ) ) ) ,
5413
" scope" : " seal" ,
5414
} ) ;
5415
let ( st, body) = call ( & app, " POST" , " /api/v1/keyless/certify" , Some ( & token) , Some ( body.to_string ( ) ) ) .await;
5416
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5417
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
5418
let cert = SignedCertificate:: decode( & oxy_core:: dsse:: base64_decode( v[ " certificate" ] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ) .unwrap ( ) ;
5419
( sk, id, cert)
5420
}
5421
} ;
5422
let sign = | r : & serde_json:: Value,
5423
sk: & SecretKey,
5424
id: & oxy_core:: identity:: Identity,
5425
cert: SignedCertificate,
5426
issuer: oxy_core:: identity:: Identity| {
5427
let r: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
5428
let run_url = format! ( " {url} /flow/{} " , run.number) ;
5429
hex:: encode( SignedReceipt:: sign( r.to_oxy ( ) , repo_id, state, & run_url, id, sk) .with_certificate ( cert, issuer) .encode ( ) )
5430
} ;
5431
let hub_id = h.app.hub_key.identity.clone ( ) ;
5432
5433
let a = claim ( & h.app) .await.unwrap ( ) ;
5434
let b = claim ( & h.app) .await.unwrap ( ) ;
5435
let ph = a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5436
let ( a_name, b_name) = ( a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , b[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
5437
let ( a_id, b_id) = ( a[ " job_id" ] .as_i64 ( ) .unwrap ( ) , b[ " job_id" ] .as_i64 ( ) .unwrap ( ) ) ;
5438
let ( sk_a, id_a, cert_a) = certify ( a[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) .await;
5439
5440
5441
let rb = receipt ( & b_name, & ph, 0 ) ;
5442
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 300 , " receipt" : rb, " signed_receipt" : sign ( & rb, & sk_a, & id_a, cert_a.clone ( ) , hub_id.clone ( ) ) } ) ;
5443
assert_eq! ( finish ( & h.app, b_id, body) .await, StatusCode:: NO_CONTENT ) ;
5444
5445
let ra = receipt ( & a_name, & ph, 0 ) ;
5446
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : ra, " signed_receipt" : sign ( & ra, & sk_a, & id_a, cert_a, hub_id.clone ( ) ) } ) ;
5447
assert_eq! ( finish ( & h.app, a_id, body) .await, StatusCode:: NO_CONTENT ) ;
5448
5449
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
5450
let job = | n : & str | jobs.iter ( ) .find ( | j | j.name = = n ) .unwrap ( ) .clone ( ) ;
5451
assert_eq! ( job ( & a_name) .receipt_attest.len ( ) , 64 , " {}" , job ( & a_name) .receipt_note) ;
5452
assert! ( job ( & a_name) .receipt_signer.ends_with ( " (short-lived key)" ) , " {}" , job ( & a_name) .receipt_signer) ;
5453
assert! ( job ( & b_name) .receipt_attest.is_empty ( ) ) ;
5454
assert! ( job ( & b_name) .receipt_note.contains ( " another run or job" ) , " {}" , job ( & b_name) .receipt_note) ;
5455
5456
let kept = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .receipts ( & heads) .unwrap ( ) ;
5457
assert_eq! ( kept.len ( ) , 1 ) ;
5458
assert! ( kept[ 0 ] .problem.is_none ( ) & & kept[ 0 ] .signed.certificate.is_some ( ) ) ;
5459
5460
5461
let t = claim ( & h.app) .await.unwrap ( ) ;
5462
let t_name = t[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5463
let fake_sk = SecretKey:: generate( ) ;
5464
let fake = fake_sk.identity ( " not the hub" , " " , 1 ) ;
5465
let ( sk_t , id_t , mut cert_t ) = certify ( t[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) .await;
5466
cert_t .cert.issuer = fake.hash ( ) ;
5467
let cert_t = SignedCertificate:: sign( cert_t .cert, & fake, & fake_sk) ;
5468
let rt = receipt ( & t_name, & ph, 0 ) ;
5469
let body =
5470
serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 500 , " receipt" : rt, " signed_receipt" : sign ( & rt, & sk_t , & id_t , cert_t , fake) } ) ;
5471
assert_eq! ( finish ( & h.app, t[ " job_id" ] .as_i64 ( ) .unwrap ( ) , body) .await, StatusCode:: NO_CONTENT ) ;
5472
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
5473
let t_job = jobs.iter ( ) .find ( | j | j.name = = t_name ) .unwrap ( ) ;
5474
assert! ( t_job.receipt_note.contains ( " not issued by this hub" ) , " {}" , t_job.receipt_note) ;
5475
}
3eb491e9 Andy McCall 19 days ago ↶
5476
24472413 Andy McCall 19 days ago ↶
5477
5478
5479
5480
5481
# [ tokio ::test ( flavor = " multi_thread" ) ]
5482
async fn a_release_built_by_a_certified_key_is_attested ( ) {
5483
use oxy_core:: cert:: { certify_challenge, SignedCertificate} ;
5484
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectType} ;
5485
use oxy_core:: identity:: SecretKey;
5486
use oxy_core:: objects:: StateObject;
5487
use oxy_core:: receipt:: SignedReceipt;
5488
let h = hub ( ) .await;
5489
create ( & h.app, " andy" , " demo" ) .await;
5490
let url = format! ( " {} /andy/demo" , h.base) ;
5491
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5492
let mut c = client ( & url, work.path ( ) ) ;
5493
c.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
5494
write (
5495
work.path ( ) ,
5496
" .flow/pipeline.kdl" ,
5497
& format! (
5498
" 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 "
5499
) ,
5500
) ;
5501
c.snapshot ( " test" ) .unwrap ( ) ;
5502
c.commit ( " Build a tool" , & [ ] ) .unwrap ( ) ;
5503
let heads = c.bookmark_heads ( ) .unwrap ( ) ;
5504
c.tag_create ( " v1" , & heads, " first" ) .unwrap ( ) ;
5505
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5506
5507
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
5508
let a = claim ( & h.app) .await.unwrap ( ) ;
5509
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
5510
let token = a[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5511
let body: Vec< u8 > = b " #!/bin/sh\n echo tool\n " .to_vec ( ) ;
5512
let ( _ , text) = upload ( & h.app, id, " tool" , & body) .await;
5513
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5514
let mut r = receipt ( " build" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
5515
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
5516
let heads_run: Vec< ChangeHash> = run.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
5517
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads_run) .encode ( ) ) ;
5518
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
5519
5520
let sk = SecretKey:: generate( ) ;
5521
let key = sk.identity ( " test-1 job" , " " , 1 ) ;
5522
let proof = sk.sign_challenge ( & key.hash ( ) , & certify_challenge ( & token) ) .unwrap ( ) ;
5523
let ask = serde_json:: json! ( {
5524
" identity" : oxy_core:: dsse:: base64( & key.encode ( ) ) ,
5525
" proof" : oxy_core:: dsse:: base64( & oxy_core:: cbor:: encode( & proof.to_value ( ) ) ) ,
5526
" scope" : " seal" ,
5527
} ) ;
5528
let ( st, answer) = call ( & h.app, " POST" , " /api/v1/keyless/certify" , Some ( & token) , Some ( ask.to_string ( ) ) ) .await;
5529
assert_eq! ( st, StatusCode:: OK , " {answer}" ) ;
5530
let v: serde_json:: Value = serde_json:: from_str( & answer) .unwrap ( ) ;
5531
let cert = SignedCertificate:: decode( & oxy_core:: dsse:: base64_decode( v[ " certificate" ] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ) .unwrap ( ) ;
5532
let fr: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
5533
let signed = hex:: encode(
5534
SignedReceipt:: sign( fr.to_oxy ( ) , repo_id, state, & format! ( " {url} /flow/{} " , run.number) , & key, & sk)
5535
.with_certificate ( cert, h.app.hub_key.identity.clone ( ) )
5536
.encode ( ) ,
5537
) ;
5538
let done = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 900 , " receipt" : r, " signed_receipt" : signed } ) ;
5539
assert_eq! ( finish ( & h.app, id, done) .await, StatusCode:: NO_CONTENT ) ;
5540
5541
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
5542
let rel = db:: releases:: create( & h.app.db, row.id, " v1" , " One" , " " , false , None ) .await.unwrap ( ) ;
5543
oxygenhub:: web:: releases:: store_asset( & h.app, rel.id, " tool" , " " , & body) .await.unwrap ( ) ;
5544
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/releases/trust/v1" , None , None ) .await;
5545
assert_eq! ( st, StatusCode:: OK ) ;
5546
for want in [
5547
" tool: signed receipt from run #1 build" ,
5548
" a short-lived key this hub certified for repo:andy/demo:bookmark:main" ,
5549
" 1 of 1 assets built and attested by OxygenFlow" ,
5550
] {
5551
assert! ( page.contains ( want) , " missing {want:?}" ) ;
5552
}
5553
assert! ( ! page.contains ( " not one this hub" ) , " not an unknown key" ) ;
5554
}
5555
965b950f Andy McCall 19 days ago ↶
5556
5557
5558
5559
5560
# [ tokio ::test ( flavor = " multi_thread" ) ]
5561
async fn a_push_is_logged_and_the_log_checks_out ( ) {
5562
use oxy_core:: tlog:: { self , Checkpoint, Entry, Hash, Note, Tile} ;
5563
let h = hub ( ) .await;
5564
create ( & h.app, " andy" , " demo" ) .await;
5565
let url = format! ( " {} /andy/demo" , h.base) ;
5566
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5567
let mut c = client ( & url, work.path ( ) ) ;
5568
c.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
5569
write ( work.path ( ) , " a.txt" , " a\n " ) ;
5570
c.commit ( " a" , & [ ] ) .unwrap ( ) ;
5571
c.policy_init ( ) .unwrap ( ) ;
5572
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5573
let host = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
5574
let ( attest, _ ) = host.latest_ref_attest ( " main" ) .unwrap ( ) .unwrap ( ) ;
5575
5576
5577
let ( _ , sk) = oxygenhub:: keys:: load_secret( & h.app.config.keys_dir ( ) ) .unwrap ( ) ;
5578
h.app.tlog.refresh_checkpoint ( & h.app.hub_key.identity, & sk) .unwrap ( ) ;
5579
let ( st, note) = call ( & h.app, " GET" , " /log/checkpoint" , None , None ) .await;
5580
assert_eq! ( st, StatusCode:: OK , " {note}" ) ;
5581
let origin = oxygenhub:: tlog:: origin_of( h.app.config.base_url ( ) ) ;
5582
let cp = Checkpoint:: verify( & Note:: parse( & note) .unwrap ( ) , & origin, & h.app.hub_key.identity) .unwrap ( ) ;
5583
assert! ( cp.size > = 3 , " ref, seal and policy: {}" , cp.size) ;
5584
5585
5586
let bundle_path = Tile { level: 0 , n: 0 , width: cp.size as u32 } .entries_path ( ) ;
5587
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /log/{bundle_path} " ) , None , None ) .await;
5588
assert_eq! ( st, StatusCode:: OK ) ;
5589
let res =
5590
oxygenhub:: router( h.app.clone ( ) ) .oneshot ( Request:: get( format! ( " /log/{bundle_path} " ) ) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
5591
assert_eq! ( res.headers ( ) [ header:: CACHE_CONTROL ] , " public, max-age=5" , " a partial tile changes" ) ;
5592
let bytes = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
5593
let entries: Vec< Entry> = tlog:: decode_bundle( & bytes) .unwrap ( ) .iter ( ) .map ( | b | Entry:: decode( b) .unwrap ( ) ) .collect ( ) ;
5594
let kinds: Vec< & str > = entries.iter ( ) .map ( | e | e.kind.as_str ( ) ) .collect ( ) ;
5595
assert! ( kinds.contains ( & " ref" ) & & kinds.contains ( & " seal" ) & & kinds.contains ( & " policy" ) , " {kinds:?}" ) ;
5596
let r = entries.iter ( ) .find ( | e | e.kind = = " ref" ) .unwrap ( ) ;
5597
assert_eq! ( r.object, attest) ;
5598
assert_eq! ( r.repo, c.id ( ) .unwrap ( ) ) ;
5599
5600
5601
5602
let mut stored: Vec< Hash> = Vec :: new( ) ;
5603
for ( n, e) in entries.iter ( ) .enumerate ( ) {
5604
stored.extend ( tlog:: stored_hashes_for_leaf( n as u64 , e.leaf_hash ( ) , & stored) .unwrap ( ) ) ;
5605
}
5606
assert_eq! ( tlog:: tree_hash( cp.size, & stored) .unwrap ( ) , cp.root) ;
5607
let t = Tile { level: 0 , n: 0 , width: cp.size as u32 } ;
5608
let res =
5609
oxygenhub:: router( h.app.clone ( ) ) .oneshot ( Request:: get( format! ( " /log/{} " , t.path ( ) ) ) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
5610
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
5611
let data = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
5612
assert_eq! ( data.to_vec ( ) , t.read_data ( & stored) .unwrap ( ) ) ;
5613
let ( st, _ ) = call ( & h.app, " GET" , " /log/tile/0/000.p/200" , None , None ) .await;
5614
assert_eq! ( st, StatusCode:: NOT_FOUND , " not held yet" ) ;
5615
5616
5617
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5618
assert_eq! ( h.app.tlog.size ( ) , cp.size) ;
5619
let ( st, page) = call ( & h.app, " GET" , " /log" , None , None ) .await;
5620
assert_eq! ( st, StatusCode:: OK ) ;
5621
assert! ( page.contains ( " Transparency log" ) & & page.contains ( " andy/demo" ) & & page.contains ( & origin) , " {page}" ) ;
5622
}
5623
e5b97ac3 Andy McCall 19 days ago ↶
5624
5625
5626
5627
5628
5629
# [ tokio ::test ( flavor = " multi_thread" ) ]
5630
async fn a_fetch_checks_the_hub_s_log_and_refuses_a_fork ( ) {
5631
use oxy_core:: tlog:: { Checkpoint, Note} ;
5632
let h = hub ( ) .await;
5633
create ( & h.app, " andy" , " demo" ) .await;
5634
let url = format! ( " {} /andy/demo" , h.base) ;
5635
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5636
let mut a = client ( & url, & work.path ( ) .join ( " a" ) ) ;
5637
a.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
5638
let ( _ , sk) = oxygenhub:: keys:: load_secret( & h.app.config.keys_dir ( ) ) .unwrap ( ) ;
5639
let hub_id = h.app.hub_key.identity.clone ( ) ;
5640
let checkpoint = | | h.app.tlog.refresh_checkpoint ( & hub_id, & sk) .unwrap ( ) ;
5641
let mut n = 0 ;
5642
let mut push = | a : & Repo| {
5643
n + = 1 ;
5644
write ( & work.path ( ) .join ( " a" ) , " a.txt" , & format! ( " {n} \n " ) ) ;
5645
a.commit ( & format! ( " change {n} " ) , & [ ] ) .unwrap ( ) ;
5646
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5647
} ;
5648
push ( & a) ;
5649
checkpoint ( ) ;
5650
5651
let home = work.path ( ) .join ( " home-b" ) ;
5652
let b = Repo:: clone( & url, & work.path ( ) .join ( " b" ) ) .unwrap ( ) .with_home ( home.clone ( ) ) ;
5653
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5654
assert! ( report.log_notes.is_empty ( ) , " {:?}" , report.log_notes) ;
5655
let pin = std:: fs:: read_dir( home.join ( " tlog" ) ) .unwrap ( ) .next ( ) .unwrap ( ) .unwrap ( ) .path ( ) ;
5656
let seen = | p : & std:: path:: Path| {
5657
let t = std:: fs:: read_to_string( p) .unwrap ( ) ;
5658
Checkpoint:: parse( & Note:: parse( t.split_once ( ' \n ' ) .unwrap ( ) .1 ) .unwrap ( ) .text) .unwrap ( )
5659
} ;
5660
let first = seen ( & pin) ;
5661
5662
5663
push ( & a) ;
5664
checkpoint ( ) ;
5665
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5666
assert! ( report.log_notes.is_empty ( ) , " {:?}" , report.log_notes) ;
5667
assert! ( seen ( & pin) .size > first.size) ;
5668
5669
5670
push ( & a) ;
5671
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5672
assert! ( report.log_notes.iter ( ) .any ( | n | n.contains ( " not in its transparency log yet" ) ) , " {:?}" , report.log_notes) ;
5673
7dbb41b0 Andy McCall 19 days ago ↶
5674
5675
5676
let text = std:: fs:: read_to_string( & pin) .unwrap ( ) ;
5677
let other_sk = oxy_core:: identity:: SecretKey:: generate( ) ;
5678
let other = other_sk.identity ( " another hub" , " [email protected] " , 1 ) ;
5679
let note = text.split_once ( ' \n ' ) .unwrap ( ) .1 ;
5680
std:: fs:: write( & pin, format! ( " {} \n {note} " , oxy_core:: dsse:: base64( & other.encode ( ) ) ) ) .unwrap ( ) ;
5681
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5682
assert! ( report.log_notes.iter ( ) .any ( | n | n.contains ( " signed by another key" ) ) , " {:?}" , report.log_notes) ;
5683
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5684
assert! ( ! report.log_notes.iter ( ) .any ( | n | n.contains ( " another key" ) ) , " pinned now: {:?}" , report.log_notes) ;
5685
e5b97ac3 Andy McCall 19 days ago ↶
5686
5687
5688
let size = seen ( & pin) .size;
5689
let forged = Checkpoint { origin: oxygenhub:: tlog:: origin_of( h.app.config.base_url ( ) ) , size, root: [ 7 ; 32 ] , extensions: vec! [ ] } ;
5690
let text = std:: fs:: read_to_string( & pin) .unwrap ( ) ;
5691
let key = text.split_once ( ' \n ' ) .unwrap ( ) .0 ;
5692
std:: fs:: write( & pin, format! ( " {key} \n {} " , forged.sign ( & hub_id, & sk) .unwrap ( ) .encode ( ) ) ) .unwrap ( ) ;
5693
checkpoint ( ) ;
5694
let err = b.fetch ( " origin" ) .unwrap_err ( ) .to_string ( ) ;
5695
assert! ( err.contains ( " showed two histories" ) & & err.contains ( " does not extend" ) , " {err}" ) ;
5696
}
5697
4f40eed0 Andy McCall 19 days ago ↶
5698
5699
5700
5701
5702
5703
# [ tokio ::test ( flavor = " multi_thread" ) ]
5704
async fn a_hub_witnesses_another_hub_s_log ( ) {
5705
use oxy_core:: tlog:: { AddCheckpoint, Checkpoint, Note} ;
5706
let log = hub ( ) .await;
5707
let witness = hub ( ) .await;
5708
let origin = oxygenhub:: tlog:: origin_of( log.app.config.base_url ( ) ) ;
5709
witness.app.tlog.add_witness_for ( & origin, log.app.hub_key.identity.clone ( ) ) ;
5710
let name = witness.app.tlog.witness_name ( ) .to_string ( ) ;
5711
log.app.tlog.set_witnesses ( vec! [ oxygenhub:: tlog:: Peer {
5712
name: name.clone ( ) ,
5713
url: format! ( " {} /log/witness/add-checkpoint" , witness.base) ,
5714
identity: witness.app.hub_key.identity.clone ( ) ,
5715
} ] ) ;
5716
create ( & log.app, " andy" , " demo" ) .await;
5717
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5718
let mut c = client ( & format! ( " {} /andy/demo" , log.base) , work.path ( ) ) ;
5719
c.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
5720
let ( _ , sk) = oxygenhub:: keys:: load_secret( & log.app.config.keys_dir ( ) ) .unwrap ( ) ;
5721
let log_id = log.app.hub_key.identity.clone ( ) ;
5722
let wid = witness.app.hub_key.identity.clone ( ) ;
5723
for n in 0 .. 2 {
5724
write ( work.path ( ) , " a.txt" , & format! ( " {n} \n " ) ) ;
5725
c.commit ( & format! ( " change {n} " ) , & [ ] ) .unwrap ( ) ;
5726
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5727
log.app.tlog.refresh_checkpoint ( & log_id, & sk) .unwrap ( ) ;
5728
oxygenhub:: tlog:: collect( & log.app) .await;
5729
let note = Note:: parse( & log.app.tlog.checkpoint ( ) .unwrap ( ) ) .unwrap ( ) ;
5730
assert! ( note.cosigned_by ( & name, & wid) .is_ok ( ) , " round {n}: {}" , note.encode ( ) ) ;
5731
5732
let ( _ , served) = call ( & log.app, " GET" , " /log/checkpoint" , None , None ) .await;
5733
assert! ( Note:: parse( & served) .unwrap ( ) .cosigned_by ( & name, & wid) .is_ok ( ) ) ;
5734
}
5735
let cp = Checkpoint:: parse( & Note:: parse( & log.app.tlog.checkpoint ( ) .unwrap ( ) ) .unwrap ( ) .text) .unwrap ( ) ;
13b27cb2 Andy McCall 18 days ago ↶
5736
5737
5738
5739
5740
let ( _ , listed) = call ( & log.app, " GET" , " /log/witnesses" , None , None ) .await;
5741
assert! ( listed.starts_with ( & format! ( " {name} " ) ) , " {listed}" ) ;
5742
let url = format! ( " {} /andy/demo" , log.base) ;
5743
let refs = tokio:: task:: spawn_blocking( move | | {
5744
let opts = RemoteOptions { token: Some ( " site" .into ( ) ) , .. Default :: default( ) } ;
5745
open_remote ( & url, & opts) .unwrap ( ) .refs ( ) .unwrap ( )
5746
} )
5747
.await
5748
.unwrap ( ) ;
5749
assert_eq! ( refs.witnesses.len ( ) , 1 ) ;
5750
assert_eq! ( refs.witnesses[ 0 ] .0 , name) ;
5751
let main = refs.refs.iter ( ) .find ( | r | r.name = = " main" ) .expect ( " main" ) ;
5752
let logged = main.logged.as_ref ( ) .expect ( " logged" ) ;
5753
assert! ( ! main.integration.is_empty ( ) , " a witnessed checkpoint holds it" ) ;
5754
let cosigned: Vec< ( & str , & [[u8 ; 32]]) > = main.integration.iter ( ) .map ( | i | ( i.note.as_str ( ) , i.proof.as_slice ( ) ) ) .collect ( ) ;
5755
let at = oxy_core:: tlog:: integrated_time( & logged.entry, logged.index, & cosigned, & origin, & log_id, & [ ( name.clone ( ) , wid.clone ( ) ) ] )
5756
.expect ( " an integrated time" ) ;
5757
assert_eq! ( at.witness, name) ;
5758
assert! ( at.size > logged.index) ;
4f40eed0 Andy McCall 19 days ago ↶
5759
5760
let ask = | req : AddCheckpoint| {
5761
let app = witness.app.clone ( ) ;
5762
async move { call ( & app, " POST" , " /log/witness/add-checkpoint" , None , Some ( req.encode ( ) ) ) .await }
5763
} ;
5764
5765
let forked = Checkpoint { root: [ 7 ; 32 ] , .. cp.clone ( ) } .sign ( & log_id, & sk) .unwrap ( ) .encode ( ) ;
5766
let ( st, body) = ask ( AddCheckpoint { old_size: cp.size, proof: vec! [ ] , note: forked.clone ( ) } ) .await;
5767
assert_eq! ( st, StatusCode:: UNPROCESSABLE_ENTITY , " {body}" ) ;
5768
5769
let ( st, body) = ask ( AddCheckpoint { old_size: 1 , proof: vec! [ ] , note: forked } ) .await;
5770
assert_eq! ( ( st, body.trim ( ) ) , ( StatusCode:: CONFLICT , cp.size.to_string ( ) .as_str ( ) ) ) ;
5771
5772
let stranger = oxy_core:: identity:: SecretKey:: generate( ) ;
5773
let sid = stranger.identity ( " elsewhere" , " [email protected] " , 1 ) ;
5774
let other = Checkpoint { origin: " elsewhere.example/log" .into ( ) , .. cp.clone ( ) } .sign ( & sid, & stranger) .unwrap ( ) .encode ( ) ;
5775
let ( st, _ ) = ask ( AddCheckpoint { old_size: 0 , proof: vec! [ ] , note: other } ) .await;
5776
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
5777
}
5778
8dcaeeb8 Andy McCall 19 days ago ↶
5779
5780
5781
5782
5783
# [ tokio ::test ( flavor = " multi_thread" ) ]
5784
async fn a_job_keeps_its_terraform_state_on_the_hub ( ) {
5785
let h = hub ( ) .await;
5786
create ( & h.app, " andy" , " infra" ) .await;
5787
create ( & h.app, " andy" , " other" ) .await;
5788
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5789
let c = client ( & format! ( " {} /andy/infra" , h.base) , work.path ( ) ) ;
5790
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5791
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5792
c.snapshot ( " test" ) .unwrap ( ) ;
5793
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5794
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5795
assert_eq! ( runs_of ( & h.app, " andy" , " infra" ) .await.len ( ) , 1 ) ;
5796
let job = claim ( & h.app) .await.expect ( " a job" ) ;
5797
let token = job[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5798
let basic = format! ( " Basic {} " , oxy_core:: dsse:: base64( format! ( " flow:{token} " ) .as_bytes ( ) ) ) ;
5799
let path = " /api/v1/repos/andy/infra/terraform/state/prod" ;
5800
let send = | method : & str , uri : String, auth : Option< String> , body : & str | {
5801
let mut req = Request:: builder( ) .method ( method) .uri ( uri) ;
5802
if let Some ( a) = auth {
5803
req = req.header ( header:: AUTHORIZATION , a) ;
5804
}
5805
let req = req.body ( Body:: from( body.to_string ( ) ) ) .unwrap ( ) ;
5806
let app = h.app.clone ( ) ;
5807
async move {
5808
let res = oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) ;
5809
let st = res.status ( ) ;
5810
let b = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
5811
( st, String :: from_utf8_lossy( & b) .into_owned ( ) )
5812
}
5813
} ;
5814
let lock = r # "{"ID":"lock-1","Operation":"OperationTypeApply","Who":"flow@runner","Version":"1.8.0"}"# ;
5815
5816
assert_eq! ( send ( " GET" , path.into ( ) , None , " " ) .await.0 , StatusCode:: UNAUTHORIZED ) ;
5817
assert_eq! ( send ( " GET" , path.into ( ) , Some ( basic.clone ( ) ) , " " ) .await.0 , StatusCode:: NO_CONTENT ) ;
5818
5819
let ( st, body) = send ( " LOCK" , path.into ( ) , Some ( basic.clone ( ) ) , lock) .await;
5820
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5821
let other_lock = r # "{"ID":"lock-2","Operation":"OperationTypePlan"}"# ;
5822
let ( st, held) = send ( " LOCK" , path.into ( ) , Some ( basic.clone ( ) ) , other_lock) .await;
5823
assert_eq! ( st, StatusCode:: LOCKED ) ;
5824
assert! ( held.contains ( " lock-1" ) , " the holder's lock info: {held}" ) ;
5825
let state1 = r # "{"version":4,"serial":1,"lineage":"l","resources":[{"secret":"hunter2"}]}"# ;
5826
assert_eq! ( send ( " POST" , path.into ( ) , Some ( basic.clone ( ) ) , state1) .await.0 , StatusCode:: LOCKED , " not under its lock" ) ;
5827
assert_eq! ( send ( " POST" , format! ( " {path} ?ID=lock-1" ) , Some ( basic.clone ( ) ) , state1) .await.0 , StatusCode:: OK ) ;
5828
assert_eq! ( send ( " UNLOCK" , path.into ( ) , Some ( basic.clone ( ) ) , other_lock) .await.0 , StatusCode:: CONFLICT , " not its lock" ) ;
5829
assert_eq! ( send ( " UNLOCK" , path.into ( ) , Some ( basic.clone ( ) ) , lock) .await.0 , StatusCode:: OK ) ;
5830
let ( st, got) = send ( " GET" , path.into ( ) , Some ( basic.clone ( ) ) , " " ) .await;
5831
assert_eq! ( ( st, got.as_str ( ) ) , ( StatusCode:: OK , state1) ) ;
5832
5833
let state2 = state1.replace ( " \" serial\" :1" , " \" serial\" :2" ) ;
5834
assert_eq! ( send ( " POST" , path.into ( ) , Some ( basic.clone ( ) ) , & state2) .await.0 , StatusCode:: OK ) ;
5835
let row = db:: repos:: get( & h.app.db, " andy" , " infra" ) .await.unwrap ( ) .unwrap ( ) ;
5836
assert_eq! ( db:: terraform:: versions( & h.app.db, row.id, " prod" ) .await.unwrap ( ) , 1 ) ;
5837
let kept = db:: terraform:: get( & h.app.db, row.id, " prod" ) .await.unwrap ( ) .unwrap ( ) ;
5838
assert_eq! ( kept.serial, 2 ) ;
5839
assert! ( ! String :: from_utf8_lossy( & kept.body) .contains ( " hunter2" ) , " sealed at rest" ) ;
5840
5841
let ( st, _ ) = send ( " GET" , " /api/v1/repos/andy/other/terraform/state/prod" .into ( ) , Some ( basic.clone ( ) ) , " " ) .await;
5842
assert_eq! ( st, StatusCode:: FORBIDDEN ) ;
5843
assert_eq! (
5844
send ( " GET" , " /api/v1/repos/andy/infra/terraform/state/..x" .into ( ) , Some ( basic.clone ( ) ) , " " ) .await.0 ,
5845
StatusCode:: BAD_REQUEST
5846
) ;
bb018142 Andy McCall 18 days ago ↶
5847
5848
5849
let mallory = db:: accounts:: create_account( & h.app.db, " mallory" , " [email protected] " , " x" , " salt" , false ) .await.unwrap ( ) ;
5850
let ( secret, hash) = oxygenhub:: auth:: new_token( ) ;
5851
db:: accounts:: create_token( & h.app.db, mallory.id, " cli" , & hash, " repo:write" , " " , None ) .await.unwrap ( ) ;
5852
let ( st, got) = send ( " GET" , path.into ( ) , Some ( format! ( " Bearer {secret} " ) ) , " " ) .await;
5853
assert_eq! ( st, StatusCode:: FORBIDDEN , " {got}" ) ;
5854
assert! ( ! got.contains ( " hunter2" ) ) ;
5855
let ( _ , page) = call ( & h.app, " GET" , " /andy/infra/terraform" , Some ( & secret) , None ) .await;
5856
assert! ( ! page.contains ( " <strong>prod</strong>" ) & & page.contains ( " shown to people who can write" ) , " {page}" ) ;
8dcaeeb8 Andy McCall 19 days ago ↶
5857
5858
let ( st, _ ) = send ( " GET" , path.into ( ) , Some ( " Bearer site" .into ( ) ) , " " ) .await;
5859
assert_eq! ( st, StatusCode:: UNAUTHORIZED , " the site token is not a person's token" ) ;
5860
5861
5862
let issuer = h.app.oidc ( ) .await.unwrap ( ) ;
5863
let now = db:: now_ms( ) ;
5864
let aud = oxygenhub:: terraform:: audience( h.app.config.base_url ( ) ) ;
5865
let own = issuer.mint_for ( h.app.config.base_url ( ) , " repo:andy/infra:bookmark:main" , & aud, now, 600 ) ;
5866
let ( st, got) = send ( " GET" , path.into ( ) , Some ( format! ( " Bearer {own} " ) ) , " " ) .await;
5867
assert_eq! ( ( st, got.contains ( " \" serial\" :2" ) ) , ( StatusCode:: OK , true ) ) ;
5868
let other = issuer.mint_for ( h.app.config.base_url ( ) , " repo:andy/other:bookmark:main" , & aud, now, 600 ) ;
5869
assert_eq! ( send ( " GET" , path.into ( ) , Some ( format! ( " Bearer {other} " ) ) , " " ) .await.0 , StatusCode:: FORBIDDEN ) ;
5870
let elsewhere = issuer.mint_for ( h.app.config.base_url ( ) , " repo:andy/infra:bookmark:main" , " sts.amazonaws.com" , now, 600 ) ;
5871
assert_eq! ( send ( " GET" , path.into ( ) , Some ( format! ( " Bearer {elsewhere} " ) ) , " " ) .await.0 , StatusCode:: UNAUTHORIZED ) ;
16ee5d19 Andy McCall 18 days ago ↶
5872
bb018142 Andy McCall 18 days ago ↶
5873
5874
let res = oxygenhub:: router( h.app.clone ( ) )
5875
.oneshot (
5876
Request:: post( " /register" )
5877
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
5878
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
5879
.unwrap ( ) ,
5880
)
5881
.await
5882
.unwrap ( ) ;
5883
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
5884
let res = oxygenhub:: router( h.app.clone ( ) )
67523e06 Andy McCall 18 days ago ↶
5885
.oneshot ( Request:: get( " /andy/infra/terraform" ) .header ( header:: COOKIE , cookie.clone ( ) ) .body ( Body:: empty( ) ) .unwrap ( ) )
bb018142 Andy McCall 18 days ago ↶
5886
.await
5887
.unwrap ( ) ;
5888
let st = res.status ( ) ;
5889
let page = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
16ee5d19 Andy McCall 18 days ago ↶
5890
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
5891
assert! ( page.contains ( " <strong>prod</strong>" ) & & page.contains ( " /api/v1/repos/andy/infra/terraform/state/NAME" ) , " {page}" ) ;
5892
assert! ( page.contains ( " allow "" ) | | page.contains ( " allow \" " ) , " the egress line names the hub" ) ;
67523e06 Andy McCall 18 days ago ↶
5893
assert! ( page.contains ( " 1 kept version" ) & & page.contains ( " >restore</button>" ) , " its versions, restorable by its owner: {page}" ) ;
5894
5895
5896
5897
5898
let andy = db:: accounts:: account_by_name( & h.app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
5899
let ( pat, hash) = oxygenhub:: auth:: new_token( ) ;
5900
db:: accounts:: create_token( & h.app.db, andy.id, " cli" , & hash, " repo:write" , " " , None ) .await.unwrap ( ) ;
5901
let bearer = format! ( " Bearer {pat} " ) ;
5902
let ( st, list) = send ( " GET" , format! ( " {path} /versions" ) , Some ( bearer.clone ( ) ) , " " ) .await;
5903
assert_eq! ( st, StatusCode:: OK , " {list}" ) ;
5904
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
5905
assert_eq! ( ( list.as_array ( ) .unwrap ( ) .len ( ) , list[ 0 ] [ " serial" ] .as_i64 ( ) ) , ( 1 , Some ( 1 ) ) , " {list}" ) ;
5906
let first = list[ 0 ] [ " id" ] .as_i64 ( ) .unwrap ( ) ;
5907
let ( st, got) = send ( " GET" , format! ( " {path} /versions/{first} " ) , Some ( bearer.clone ( ) ) , " " ) .await;
5908
assert_eq! ( ( st, got.as_str ( ) ) , ( StatusCode:: OK , state1) ) ;
5909
assert_eq! ( send ( " GET" , format! ( " {path} /versions" ) , Some ( format! ( " Bearer {secret} " ) ) , " " ) .await.0 , StatusCode:: FORBIDDEN , " a stranger" ) ;
5910
let restore = | id : i64 , auth : String| send ( " POST" , format! ( " {path} /versions/{id} /restore" ) , Some ( auth) , " " ) ;
5911
assert_eq! ( restore ( first, basic.clone ( ) ) .await.0 , StatusCode:: FORBIDDEN , " a job does not restore" ) ;
5912
assert_eq! ( restore ( first, format! ( " Bearer {secret} " ) ) .await.0 , StatusCode:: FORBIDDEN , " nor a stranger" ) ;
5913
assert_eq! ( restore ( 9999 , bearer.clone ( ) ) .await.0 , StatusCode:: NOT_FOUND ) ;
5914
assert_eq! ( send ( " LOCK" , path.into ( ) , Some ( basic.clone ( ) ) , lock) .await.0 , StatusCode:: OK ) ;
5915
assert_eq! ( restore ( first, bearer.clone ( ) ) .await.0 , StatusCode:: LOCKED , " not under someone's lock" ) ;
5916
assert_eq! ( send ( " UNLOCK" , path.into ( ) , Some ( basic.clone ( ) ) , lock) .await.0 , StatusCode:: OK ) ;
5917
let ( st, got) = restore ( first, bearer.clone ( ) ) .await;
5918
assert_eq! ( ( st, got.contains ( " \" serial\" :3" ) ) , ( StatusCode:: OK , true ) , " {got}" ) ;
5919
let ( _ , now) = send ( " GET" , path.into ( ) , Some ( bearer.clone ( ) ) , " " ) .await;
5920
let now: serde_json:: Value = serde_json:: from_str( & now) .unwrap ( ) ;
5921
assert_eq! (
5922
( now[ " serial" ] .as_i64 ( ) , now[ " lineage" ] .as_str ( ) , now[ " resources" ] [ 0 ] [ " secret" ] .as_str ( ) ) ,
5923
( Some ( 3 ) , Some ( " l" ) , Some ( " hunter2" ) )
5924
) ;
5925
5926
let ( _ , list) = send ( " GET" , format! ( " {path} /versions" ) , Some ( bearer.clone ( ) ) , " " ) .await;
5927
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
5928
let serials: Vec< i64 > = list.as_array ( ) .unwrap ( ) .iter ( ) .map ( | v | v[ " serial" ] .as_i64 ( ) .unwrap ( ) ) .collect ( ) ;
5929
assert_eq! ( serials, vec! [ 2 , 1 ] , " {list}" ) ;
5930
let ( st, got) = restore ( list[ 0 ] [ " id" ] .as_i64 ( ) .unwrap ( ) , bearer.clone ( ) ) .await;
5931
assert_eq! ( ( st, got.contains ( " \" serial\" :4" ) ) , ( StatusCode:: OK , true ) , " {got}" ) ;
5932
5933
5934
let page_with = | req : axum:: http:: request:: Builder, body : Body| {
5935
let ( app, cookie) = ( h.app.clone ( ) , cookie.clone ( ) ) ;
5936
async move {
5937
let res = oxygenhub:: router( app) .oneshot ( req.header ( header:: COOKIE , cookie) .body ( body) .unwrap ( ) ) .await.unwrap ( ) ;
5938
let st = res.status ( ) ;
5939
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
5940
}
5941
} ;
5942
let ( st, file) = page_with ( Request:: get( format! ( " /andy/infra/terraform/prod/download?version={first} " ) ) , Body:: empty( ) ) .await;
5943
assert_eq! ( ( st, file.as_str ( ) ) , ( StatusCode:: OK , state1) ) ;
5944
let ( _ , page) = page_with ( Request:: get( " /andy/infra/terraform" ) , Body:: empty( ) ) .await;
5945
let csrf = page.split ( " name=\" _csrf\" value=\" " ) .nth ( 1 ) .unwrap ( ) .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
5946
let ( st, page) = page_with (
5947
Request:: post( " /andy/infra/terraform/prod/restore" ) .header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ,
5948
Body:: from( format! ( " _csrf={csrf} &version={first} " ) ) ,
5949
)
5950
.await;
5951
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
5952
assert! ( page.contains ( " Restored prod: it is now serial 5." ) , " {page}" ) ;
5953
let audit = db:: accounts:: audit_rows( & h.app.db, 50 ) .await.unwrap ( ) ;
5954
assert_eq! ( audit.iter ( ) .filter ( | r | r.action = = " terraform.restored" ) .count ( ) , 3 , " every restore audited" ) ;
8dcaeeb8 Andy McCall 19 days ago ↶
5955
}
5956
5957
5958
5959
5960
5961
# [ tokio ::test ( flavor = " multi_thread" ) ]
5962
# [ ignore ]
5963
async fn tofu_keeps_its_state_on_the_hub ( ) {
5964
let h = hub ( ) .await;
5965
create ( & h.app, " andy" , " infra" ) .await;
5966
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5967
let c = client ( & format! ( " {} /andy/infra" , h.base) , work.path ( ) ) ;
5968
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5969
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5970
c.snapshot ( " test" ) .unwrap ( ) ;
5971
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5972
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5973
assert_eq! ( runs_of ( & h.app, " andy" , " infra" ) .await.len ( ) , 1 ) ;
5974
let job = claim ( & h.app) .await.expect ( " a job" ) ;
5975
let token = job[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5976
let tf = tempfile:: tempdir( ) .unwrap ( ) ;
5977
let address = format! ( " {} /api/v1/repos/andy/infra/terraform/state/prod" , h.base) ;
5978
std:: fs:: write(
5979
tf.path ( ) .join ( " main.tf" ) ,
5980
format! (
5981
" terraform {{ \n backend \" http\" {{ \n address = \" {address} \" \n lock_address = \" {address} \" \n unlock_address = \" {address} \" \n username = \" flow\" \n }} \n }} \n resource \" terraform_data\" \" x\" {{ \n input = \" hello\" \n }} \n "
5982
) ,
5983
)
5984
.unwrap ( ) ;
5985
let tofu = | args : & [& str ]| {
5986
let out = std:: process:: Command:: new( " docker" )
5987
.args ( [ " run" , " --rm" , " --network" , " host" , " -v" ] )
5988
.arg ( format! ( " {} :/w" , tf.path ( ) .display ( ) ) )
5989
.args ( [ " -w" , " /w" , " -e" ] )
5990
.arg ( format! ( " TF_HTTP_PASSWORD={token} " ) )
5991
.args ( [ " --entrypoint" , " tofu" , " oxygenflow-tofu:latest" ] )
5992
.args ( args)
5993
.output ( )
5994
.expect ( " docker" ) ;
5995
let text = format! ( " {} {} " , String :: from_utf8_lossy( & out.stdout) , String :: from_utf8_lossy( & out.stderr) ) ;
5996
assert! ( out.status.success ( ) , " tofu {args:?}: {text}" ) ;
5997
text
5998
} ;
5999
tofu ( & [ " init" , " -input=false" , " -no-color" ] ) ;
6000
let applied = tofu ( & [ " apply" , " -auto-approve" , " -input=false" , " -no-color" ] ) ;
6001
assert! ( applied.contains ( " 1 added" ) , " {applied}" ) ;
6002
let again = tofu ( & [ " apply" , " -auto-approve" , " -input=false" , " -no-color" ] ) ;
6003
assert! ( again.contains ( " No changes" ) , " the state was kept: {again}" ) ;
6004
let row = db:: repos:: get( & h.app.db, " andy" , " infra" ) .await.unwrap ( ) .unwrap ( ) ;
6005
let kept = db:: terraform:: get( & h.app.db, row.id, " prod" ) .await.unwrap ( ) .unwrap ( ) ;
6006
assert! ( kept.lock_id.is_empty ( ) , " unlocked after each run" ) ;
6007
assert! ( kept.serial > = 1 ) ;
6008
}
6009
8a48dd83 Andy McCall 19 days ago ↶
6010
6011
6012
# [ tokio ::test ( flavor = " multi_thread" ) ]
6013
async fn metrics_are_private_and_count_flow_jobs ( ) {
6014
let h = hub ( ) .await;
6015
let ( st, _ ) = call ( & h.app, " GET" , " /metrics" , None , None ) .await;
6016
assert_eq! ( st, StatusCode:: NOT_FOUND , " off by default" ) ;
6017
let text = oxygenhub:: metrics:: render( & h.app) .await;
6018
for want in [
6019
" # TYPE oxyhub_uptime_seconds gauge" ,
6020
" oxyhub_flow_jobs{state=\" queued\" } 0" ,
6021
" oxyhub_bucket_requests_total{kind=\" list\" }" ,
6022
" oxyhub_tlog_entries" ,
a6d824c5 Andy McCall 19 days ago ↶
6023
" oxyhub_db_connections{idle=\" true\" }" ,
6024
" oxyhub_pushes_last_day{result=\" refused\" } 0" ,
6025
" # TYPE oxyhub_backup_last_run_ok gauge" ,
2c47eecf Andy McCall 19 days ago ↶
6026
" # TYPE oxyhub_flow_runner_last_seen_seconds gauge" ,
6027
" oxyhub_flow_runs_last_day{result=\" failed\" } 0" ,
6028
" oxyhub_mail_total{result=\" sent\" }" ,
8a48dd83 Andy McCall 19 days ago ↶
6029
] {
6030
assert! ( text.contains ( want) , " missing {want}:\n {text}" ) ;
6031
}
6032
create ( & h.app, " andy" , " demo" ) .await;
6033
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6034
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
6035
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
6036
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
6037
c.snapshot ( " test" ) .unwrap ( ) ;
6038
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
6039
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6040
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , 1 ) ;
6041
let text = oxygenhub:: metrics:: render( & h.app) .await;
6042
assert! ( ! text.contains ( " oxyhub_flow_jobs{state=\" queued\" } 0" ) , " the run's jobs are queued:\n {text}" ) ;
a6d824c5 Andy McCall 19 days ago ↶
6043
assert! ( text.contains ( " oxyhub_pushes_last_day{result=\" accepted\" }" ) , " {text}" ) ;
85ec74fd Andy McCall 19 days ago ↶
6044
6045
6046
let pushed = text
6047
.lines ( )
6048
.find_map ( | l | l.strip_prefix ( " oxyhub_protocol_bytes_total{endpoint=\" push\" ,direction=\" received\" } " ) )
6049
.and_then ( | v | v.parse:: < f64 > ( ) .ok ( ) )
6050
.unwrap_or ( 0. 0 ) ;
6051
assert! ( pushed > 0. 0 , " {text}" ) ;
b9b74d99 Andy McCall 19 days ago ↶
6052
6053
call ( & h.app, " GET" , " /andy/demo" , None , None ) .await;
6054
let text = oxygenhub:: metrics:: render( & h.app) .await;
6055
assert! ( text.contains ( " oxyhub_http_requests_total{route=\" page:repository\" ,method=\" GET\" ,status=\" 2xx\" }" ) , " {text}" ) ;
6056
assert! ( text.contains ( " oxyhub_http_request_duration_seconds_bucket{route=\" page:repository\" ,method=\" GET\" ,le=\" +Inf\" }" ) ) ;
8a48dd83 Andy McCall 19 days ago ↶
6057
}
6058
e97500b7 Andy McCall 18 days ago ↶
6059
6060
6061
6062
6063
# [ tokio ::test ( flavor = " multi_thread" ) ]
6064
async fn insights_chart_pickup_from_when_a_job_could_run ( ) {
6065
let h = hub ( ) .await;
6066
create ( & h.app, " andy" , " demo" ) .await;
6067
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6068
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
6069
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
6070
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
6071
c.snapshot ( " test" ) .unwrap ( ) ;
6072
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
6073
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6074
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , 1 ) ;
6075
let mut finished = std:: collections:: HashMap:: new( ) ;
6076
for want in [ " build" , " lint" , " test" ] {
6077
let j = claim ( & h.app) .await.expect ( " a job" ) ;
6078
assert_eq! ( j[ " job" ] [ " name" ] .as_str ( ) , Some ( want) ) ;
6079
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
6080
let row = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
6081
let ( queued, ready, started) = ( row.queued_ms.unwrap ( ) , row.ready_ms.unwrap ( ) , row.started_ms.unwrap ( ) ) ;
6082
assert! ( queued < = ready & & ready < = started, " {want}: {queued} {ready} {started}" ) ;
6083
if want = = " test" {
6084
assert_eq! ( Some ( ready) , finished[ " build" ] , " ready when build passed, not when it was queued" ) ;
6085
}
6086
let ph = j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) ;
6087
let st = finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : receipt ( want, ph, 0 ) } ) ) .await;
6088
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
6089
finished.insert ( want, db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .finished_ms) ;
6090
}
6091
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/insights" , None , None ) .await;
6092
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
6093
assert! ( page.contains ( " <span class=\" kpi-v\" >3</span><span class=\" kpi-l\" >jobs finished" ) , " {page}" ) ;
6094
assert! ( page.contains ( " 100% passed" ) , " {page}" ) ;
6095
assert_eq! ( page.matches ( " <svg class=\" chart\" " ) .count ( ) , 5 , " five charts" ) ;
6096
assert! ( page.contains ( " class=\" bar s-ok\" " ) , " the passed jobs drawn" ) ;
6097
assert! ( page.contains ( " data-refresh=\" 30\" data-fragment=\" insights\" " ) , " it refreshes itself" ) ;
6098
assert! ( page.contains ( " <summary class=\" on\" >More</summary>" ) , " the bar's More menu holds it" ) ;
6099
let ( _ , wall) = call ( & h.app, " GET" , " /andy/demo/insights?range=7d&wall=1" , None , None ) .await;
6100
assert! ( wall.contains ( " <body class=\" wall\" " ) & & wall.contains ( " the last 7 days" ) & & wall.contains ( " leave the wall view" ) , " {wall}" ) ;
6101
6102
assert_eq! ( call ( & h.app, " GET" , " /insights/andy" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
6103
assert_eq! ( call ( & h.app, " GET" , " /admin/insights" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
6104
let res = oxygenhub:: router( h.app.clone ( ) )
6105
.oneshot (
6106
Request:: post( " /register" )
6107
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
6108
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
6109
.unwrap ( ) ,
6110
)
6111
.await
6112
.unwrap ( ) ;
6113
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
6114
let andy = db:: accounts:: account_by_name( & h.app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
6115
db:: accounts:: set_admin( & h.app.db, andy.id, true ) .await.unwrap ( ) ;
6116
for path in [ " /insights/andy" , " /admin/insights?runner=test-1&range=1h" ] {
6117
let res = oxygenhub:: router( h.app.clone ( ) )
6118
.oneshot ( Request:: get( path) .header ( header:: COOKIE , cookie.clone ( ) ) .body ( Body:: empty( ) ) .unwrap ( ) )
6119
.await
6120
.unwrap ( ) ;
6121
let st = res.status ( ) ;
6122
let body = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
6123
assert_eq! ( st, StatusCode:: OK , " {path}: {body}" ) ;
6124
assert! ( body.contains ( " <svg class=\" chart\" " ) , " {path}" ) ;
6125
}
6126
}
6127
7c06f5b2 Andy McCall 18 days ago ↶
6128
6129
6130
6131
# [ tokio ::test ( flavor = " multi_thread" ) ]
6132
async fn reads_do_not_wait_for_a_writer ( ) {
6133
use std:: time:: Duration;
6134
let h = hub ( ) .await;
6135
create ( & h.app, " andy" , " demo" ) .await;
6136
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6137
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
6138
write ( work.path ( ) , " a.txt" , " a\n " ) ;
6139
c.commit ( " A change" , & [ ] ) .unwrap ( ) ;
6140
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6141
let name = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
6142
let writer = h.app.repos.write_guard ( & name) .await;
6143
for path in [ " /andy/demo" , " /andy/demo/changes/main" , " /api/v1/repos/andy/demo/bookmarks" ] {
6144
let ( st, body) = tokio:: time:: timeout( Duration:: from_secs( 10 ) , call ( & h.app, " GET" , path, None , None ) )
6145
.await
6146
.unwrap_or_else ( | _| panic! ( " {path} waited for the writer" ) ) ;
6147
assert_eq! ( st, StatusCode:: OK , " {path}: {body}" ) ;
6148
}
6149
let refs = tokio:: time:: timeout( Duration:: from_secs( 10 ) , call ( & h.app, " POST" , " /andy/demo/oxy/v1/refs" , Some ( " site" ) , None ) ) .await;
6150
assert! ( refs.is_ok ( ) , " a fetch's refs waited for the writer" ) ;
6151
assert! ( tokio:: time:: timeout( Duration:: from_millis( 300 ) , h.app.repos.write_guard ( & name) ) .await.is_err ( ) , " writers take turns" ) ;
6152
assert! ( tokio:: time:: timeout( Duration:: from_millis( 300 ) , h.app.repos.exclusive_guard ( & name) ) .await.is_err ( ) , " a delete waits" ) ;
6153
drop ( writer) ;
6154
let all = tokio:: time:: timeout( Duration:: from_secs( 5 ) , h.app.repos.exclusive_guard ( & name) ) .await.expect ( " then it runs" ) ;
6155
assert! (
6156
tokio:: time:: timeout( Duration:: from_millis( 300 ) , h.app.repos.read_guard ( & name) ) .await.is_err ( ) ,
6157
" nothing reads a repository being deleted"
6158
) ;
6159
drop ( all) ;
6160
}
6161
a10446c6 Andy McCall 19 days ago ↶
6162
6163
6164
6165
6166
# [ tokio ::test ( flavor = " multi_thread" ) ]
6167
async fn the_issue_overview_and_diffusion_pages_listen_on_their_topics ( ) {
6168
let h = hub ( ) .await;
6169
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
6170
create ( & h.app, " andy" , " demo" ) .await;
6171
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
6172
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6173
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
6174
write ( work.path ( ) , " a.txt" , " a\n " ) ;
6175
c.snapshot ( " t" ) .unwrap ( ) ;
6176
c.commit ( " a" , & [ ] ) .unwrap ( ) ;
6177
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6178
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/issues" , Some ( " site" ) , Some ( r # "{"title":"Broken"}"# .into ( ) ) ) .await;
6179
assert! ( st.is_success ( ) , " {st} {body}" ) ;
6180
let repo = format! ( " repo:{} " , row.id) ;
6181
let ( _ , issue) = call ( & h.app, " GET" , " /andy/demo/issue/1" , None , None ) .await;
6182
assert! ( issue.contains ( & format! ( " data-live=\" {repo} /issue:1 {repo} \" data-fragment=\" timeline\" " ) ) , " the issue's timeline" ) ;
6183
let ( _ , overview) = call ( & h.app, " GET" , " /andy/demo" , None , None ) .await;
6184
assert! ( overview.contains ( & format! ( " data-live=\" {repo} \" data-fragment=\" latest\" " ) ) , " the latest change" ) ;
6185
assert! ( overview.contains ( & format! ( " data-live=\" {repo} \" data-fragment=\" bookmarks\" " ) ) , " the bookmarks" ) ;
6186
let ( _ , diffusion) = call ( & h.app, " GET" , " /andy/demo/diffusion" , None , None ) .await;
6187
assert! ( diffusion.contains ( & format! ( " data-live=\" diffusion:{} {repo} \" data-fragment=\" diffusion\" " , row.id) ) , " the Diffusion tab" ) ;
6188
}
6189
3eb491e9 Andy McCall 19 days ago ↶
6190
6191
6192
6193
6194
# [ tokio ::test ( flavor = " multi_thread" ) ]
6195
async fn the_hub_countersigns_an_update_by_a_key_it_certified ( ) {
6196
use oxy_core:: attest:: AttestBody;
6197
use oxy_core:: cert:: { Certificate, SignedCertificate} ;
6198
use oxy_core:: identity:: Scope;
6199
use oxy_core:: repo:: IdentityScope;
6200
let h = hub ( ) .await;
6201
create ( & h.app, " andy" , " demo" ) .await;
6202
let url = format! ( " {} /andy/demo" , h.base) ;
6203
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6204
let mut a = client ( & url, & work.path ( ) .join ( " a" ) ) .with_home ( work.path ( ) .join ( " home-a" ) ) ;
6205
a.create_identity ( " Andy" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6206
write ( & work.path ( ) .join ( " a" ) , " a.txt" , " a\n " ) ;
6207
a.commit ( " a" , & [ ] ) .unwrap ( ) ;
6208
a.policy_init ( ) .unwrap ( ) ;
6209
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6210
let hub_id = h.app.hub_key.identity.clone ( ) ;
6211
a.policy_grant_subject ( " agent:claude@andy" , hub_id.clone ( ) , Scope:: CHANGES | Scope:: REFS , None ) .unwrap ( ) ;
6212
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .any ( | r | r.name = = " policy" & & r.ok ) ) ;
6213
6214
6215
let mut b = Repo:: clone( & url, & work.path ( ) .join ( " b" ) ) .unwrap ( ) .with_home ( work.path ( ) .join ( " home-b" ) ) ;
6216
b.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
6217
b.save_config ( ) .unwrap ( ) ;
6218
let ( _ , key) = b.create_identity ( " claude" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6219
let ( _ , secret) = oxygenhub:: keys:: load_secret( & h.app.config.keys_dir ( ) ) .unwrap ( ) ;
6220
let now = db:: now_ms( ) as u64 ;
6221
let cert = SignedCertificate:: sign(
6222
Certificate {
6223
key,
6224
issuer: hub_id.hash ( ) ,
6225
subject: " agent:claude@andy" .into ( ) ,
6226
claims: vec! [ ] ,
6227
scope: Scope:: CHANGES | Scope:: REFS ,
6228
not_before: now - 60_000 ,
6229
expires: now + 600_000 ,
8aa5ef38 Andy McCall 19 days ago ↶
6230
grant: None ,
3eb491e9 Andy McCall 19 days ago ↶
6231
} ,
6232
& hub_id,
6233
& secret,
6234
) ;
6235
b.store ( ) .put_identity ( & hub_id) .unwrap ( ) ;
6236
b.store ( ) .put_certificate ( & cert) .unwrap ( ) ;
6237
write ( & work.path ( ) .join ( " b" ) , " b.txt" , " agent work\n " ) ;
6238
b.commit ( " work" , & [ ] ) .unwrap ( ) ;
6239
let r = b.push ( " origin" , false ) .unwrap ( ) ;
6240
assert! ( r.iter ( ) .all ( | x | x.ok ) , " {r:?}" ) ;
6241
6242
6243
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6244
let main = refs.refs.iter ( ) .find ( | r | r.name = = " main" ) .unwrap ( ) ;
6245
let ( attest, acc) = ( main.attestation.clone ( ) .unwrap ( ) , main.accepted.clone ( ) .expect ( " an acceptance" ) ) ;
6246
assert! ( matches! ( acc.attest.body, AttestBody:: Accepted { attest: r } if r = = attest.attest.hash ( ) ) ) ;
6247
assert_eq! ( acc.sigs[ 0 ] .key, hub_id.hash ( ) ) ;
6248
assert! ( acc.attest.hlc.wall_ms > = now) ;
6249
6250
a.pull ( " origin" ) .unwrap ( ) ;
6251
write ( & work.path ( ) .join ( " a" ) , " a.txt" , " again\n " ) ;
6252
a.commit ( " again" , & [ ] ) .unwrap ( ) ;
6253
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6254
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6255
assert! ( refs.refs.iter ( ) .find ( | r | r.name = = " main" ) .unwrap ( ) .accepted.is_none ( ) ) ;
6256
}
8aa5ef38 Andy McCall 19 days ago ↶
6257
b5c199bd Andy McCall 17 days ago ↶
6258
6259
6260
6261
6262
# [ tokio ::test ( flavor = " multi_thread" ) ]
6263
async fn a_tag_by_a_key_the_hub_certified_is_accepted_and_fetched ( ) {
6264
use oxy_core:: attest:: AttestBody;
6265
use oxy_core:: cert:: { Certificate, SignedCertificate} ;
6266
use oxy_core:: identity:: Scope;
6267
use oxy_core:: repo:: IdentityScope;
6268
use oxy_core:: tlog:: Entry;
6269
let h = hub ( ) .await;
6270
create ( & h.app, " andy" , " demo" ) .await;
6271
let url = format! ( " {} /andy/demo" , h.base) ;
6272
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6273
let mut a = client ( & url, & work.path ( ) .join ( " a" ) ) .with_home ( work.path ( ) .join ( " home-a" ) ) ;
6274
a.create_identity ( " Andy" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6275
write ( & work.path ( ) .join ( " a" ) , " a.txt" , " a\n " ) ;
6276
a.commit ( " a" , & [ ] ) .unwrap ( ) ;
6277
a.policy_init ( ) .unwrap ( ) ;
6278
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6279
let hub_id = h.app.hub_key.identity.clone ( ) ;
6280
a.policy_grant_subject ( " agent:claude@andy" , hub_id.clone ( ) , Scope:: CHANGES | Scope:: REFS , None ) .unwrap ( ) ;
6281
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .any ( | r | r.name = = " policy" & & r.ok ) ) ;
6282
6283
6284
let mut b = Repo:: clone( & url, & work.path ( ) .join ( " b" ) ) .unwrap ( ) .with_home ( work.path ( ) .join ( " home-b" ) ) ;
6285
b.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
6286
b.save_config ( ) .unwrap ( ) ;
6287
let ( _ , key) = b.create_identity ( " claude" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6288
let ( _ , secret) = oxygenhub:: keys:: load_secret( & h.app.config.keys_dir ( ) ) .unwrap ( ) ;
6289
let now = db:: now_ms( ) as u64 ;
6290
let cert = SignedCertificate:: sign(
6291
Certificate {
6292
key,
6293
issuer: hub_id.hash ( ) ,
6294
subject: " agent:claude@andy" .into ( ) ,
6295
claims: vec! [ ] ,
6296
scope: Scope:: CHANGES | Scope:: REFS ,
6297
not_before: now - 60_000 ,
6298
expires: now + 600_000 ,
6299
grant: None ,
6300
} ,
6301
& hub_id,
6302
& secret,
6303
) ;
6304
b.store ( ) .put_identity ( & hub_id) .unwrap ( ) ;
6305
b.store ( ) .put_certificate ( & cert) .unwrap ( ) ;
6306
let heads = b.bookmark_heads ( ) .unwrap ( ) ;
6307
b.tag_create ( " v0.2.0" , & heads, " OxygenFlow 0.2.0" ) .unwrap ( ) ;
6308
let r = b.push ( " origin" , false ) .unwrap ( ) ;
6309
assert! ( r.iter ( ) .any ( | x | x.name = = " tags/v0.2.0" & & x.ok ) , " {r:?}" ) ;
6310
6311
6312
6313
h.app.tlog.refresh_checkpoint ( & hub_id, & secret) .unwrap ( ) ;
6314
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6315
let tag = refs.tags.iter ( ) .find ( | t | t.name = = " v0.2.0" ) .expect ( " the tag" ) ;
6316
let ( attest, acc) = ( tag.attestation.clone ( ) .unwrap ( ) , tag.accepted.clone ( ) .expect ( " an acceptance" ) ) ;
6317
assert! ( matches! ( acc.attest.body, AttestBody:: Accepted { attest: r } if r = = attest.attest.hash ( ) ) ) ;
6318
assert_eq! ( acc.sigs[ 0 ] .key, hub_id.hash ( ) ) ;
6319
assert! ( acc.attest.hlc.wall_ms > = now) ;
6320
let logged = tag.logged.as_ref ( ) .expect ( " the tag's place in the log" ) ;
6321
let entry = Entry:: decode( & logged.entry) .unwrap ( ) ;
6322
assert_eq! ( ( entry.kind.as_str ( ) , entry.object) , ( " tag" , attest.attest.hash ( ) ) ) ;
6323
let logged_acceptance = db:: tlog:: entry_of( & h.app.db, & acc.attest.hash ( ) .to_hex ( ) , " accepted" ) .await.unwrap ( ) ;
6324
assert! ( logged_acceptance.is_some ( ) , " the acceptance is logged too" ) ;
6325
6326
6327
let c = Repo:: clone( & url, & work.path ( ) .join ( " c" ) ) .unwrap ( ) .with_home ( work.path ( ) .join ( " home-c" ) ) ;
6328
assert_eq! ( c.tag_heads ( " v0.2.0" ) .unwrap ( ) , Some ( heads) ) ;
6329
let report = c.fetch ( " origin" ) .unwrap ( ) ;
6330
assert! ( report.skipped.is_empty ( ) , " {:?}" , report.skipped) ;
6331
6332
a.pull ( " origin" ) .unwrap ( ) ;
6333
let t = a.tags ( ) .unwrap ( ) .into_iter ( ) .find ( | t | t.name = = " v0.2.0" ) .expect ( " pulled" ) ;
6334
assert_eq! ( t.signer.as_deref ( ) , Some ( " claude" ) ) ;
a0a1601e Andy McCall 16 days ago ↶
6335
6336
6337
6338
6339
a.tag_create ( " v0.1.0" , & a.bookmark_heads ( ) .unwrap ( ) , " by Andy" ) .unwrap ( ) ;
6340
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .any ( | r | r.name = = " tags/v0.1.0" & & r.ok ) ) ;
6341
let bare = oxy_core:: protocol:: http_get( & format! ( " {url} /oxy/v1/refs" ) , & RemoteOptions:: default( ) ) .unwrap ( ) ;
6342
let ( _ , old) = oxy_core:: protocol:: wire:: decode_refs( & bare) .unwrap ( ) ;
6343
let names: Vec< String> = old.tags.into_iter ( ) .map ( | t | t.name ) .collect ( ) ;
6344
assert_eq! ( names, [ " v0.1.0" ] , " no Oxy-Features header" ) ;
6345
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6346
let mut names: Vec< String> = refs.tags.into_iter ( ) .map ( | t | t.name ) .collect ( ) ;
6347
names.sort ( ) ;
6348
assert_eq! ( names, [ " v0.1.0" , " v0.2.0" ] , " a current client declares certified-tags" ) ;
b5c199bd Andy McCall 17 days ago ↶
6349
}
6350
8aa5ef38 Andy McCall 19 days ago ↶
6351
6352
6353
6354
6355
6356
# [ tokio ::test ( flavor = " multi_thread" ) ]
6357
async fn an_agent_with_a_grant_pushes_in_its_owner_s_name ( ) {
6358
use oxy_core:: cert:: { Grant, SignedGrant} ;
6359
use oxy_core:: identity:: { Identity, Scope, SecretKey} ;
6360
use oxy_core:: repo:: { IdentityScope, SignatureStatus} ;
6361
let h = hub ( ) .await;
6362
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
6363
let ( pat, pat_hash) = oxygenhub:: auth:: new_token( ) ;
6364
db:: accounts:: create_token( & h.app.db, andy.id, " cli" , & pat_hash, " repo:write" , " " , None ) .await.unwrap ( ) ;
6365
create ( & h.app, " andy" , " demo" ) .await;
6366
let url = format! ( " {} /andy/demo" , h.base) ;
6367
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6368
6369
let mut a = client ( & url, & work.path ( ) .join ( " a" ) ) .with_home ( work.path ( ) .join ( " home-a" ) ) ;
6370
let ( andy_key, andy_id) = a.create_identity ( " Andy" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6371
db:: accounts:: link_identity( & h.app.db, andy.id, & andy_key.to_hex ( ) , " Andy" , " [email protected] " , & andy_id.encode ( ) ) .await.unwrap ( ) ;
6372
write ( & work.path ( ) .join ( " a" ) , " a.txt" , " a\n " ) ;
6373
a.commit ( " a" , & [ ] ) .unwrap ( ) ;
6374
a.policy_init ( ) .unwrap ( ) ;
6375
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6376
let hub_id = h.app.hub_key.identity.clone ( ) ;
6377
a.policy_grant_subject ( " agent:claude@andy" , hub_id.clone ( ) , Scope:: CHANGES | Scope:: REFS , None ) .unwrap ( ) ;
6378
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6379
6380
6381
let ( _ , andy_sk) = a.secret_key ( ) .unwrap ( ) .unwrap ( ) ;
6382
let now = db:: now_ms( ) as u64 ;
6383
let grant = | by : & Identity, sk : & SecretKey, subject : & str | {
6384
SignedGrant:: sign(
6385
Grant {
6386
grantor: by.hash ( ) ,
6387
subject: subject.into ( ) ,
6388
issuer: hub_id.hash ( ) ,
6389
scope: Scope:: CHANGES | Scope:: SEAL | Scope:: REFS ,
6390
not_before: now - 60_000 ,
6391
expires: now + 86_400_000 ,
6392
} ,
6393
by,
6394
sk,
6395
)
6396
} ;
6397
let post = | g : SignedGrant| {
6398
let ( app, pat) = ( h.app.clone ( ) , pat.clone ( ) ) ;
6399
let body = serde_json:: json! ( { " grant" : oxy_core:: dsse:: base64( & g.encode ( ) ) } ) .to_string ( ) ;
6400
async move { call ( & app, " POST" , " /api/v1/agents/grants" , Some ( & pat) , Some ( body) ) .await }
6401
} ;
6402
let stray_sk = SecretKey:: generate( ) ;
6403
let stray = stray_sk.identity ( " stray" , " [email protected] " , 1 ) ;
6404
let ( st, body) = post ( grant ( & stray, & stray_sk, " agent:claude@andy" ) ) .await;
6405
assert_eq! ( st, StatusCode:: FORBIDDEN , " a key not linked to the account: {body}" ) ;
6406
let ( st, body) = post ( grant ( & andy_id, & andy_sk, " agent:claude@mallory" ) ) .await;
6407
assert_eq! ( st, StatusCode:: BAD_REQUEST , " another account's agent: {body}" ) ;
6408
let ( st, body) = post ( grant ( & andy_id, & andy_sk, " agent:claude@andy" ) ) .await;
6409
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
6410
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
6411
let token = v[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
6412
assert! ( token.starts_with ( " oxyagent_" ) ) ;
6413
let grant_id = v[ " id" ] .as_i64 ( ) .unwrap ( ) ;
6414
6415
6416
let agent_home = work.path ( ) .join ( " home-claude" ) ;
6417
oxy_core:: credentials:: Credentials:: at( & agent_home) .save_grant ( & h.base, & token) .unwrap ( ) ;
6418
let agent = | | {
6419
let n = std:: time:: SystemTime:: now( ) .duration_since ( std:: time:: UNIX_EPOCH ) .unwrap ( ) .as_nanos ( ) ;
6420
let dir = work.path ( ) .join ( format! ( " claude-{n} " ) ) ;
6421
let mut r = Repo:: clone( & url, & dir) .unwrap ( ) .with_home ( agent_home.clone ( ) ) ;
559c3920 Andy McCall 19 days ago ↶
6422
6423
r.config.set ( " remote.origin.token" , & token) .unwrap ( ) ;
8aa5ef38 Andy McCall 19 days ago ↶
6424
r.save_config ( ) .unwrap ( ) ;
6425
r.set_user_config ( " author.key" , & andy_key.to_hex ( ) ) .unwrap ( ) ;
6426
r.set_user_config ( " author.signer" , " keyless" ) .unwrap ( ) ;
6427
( r, dir)
6428
} ;
6429
let ( claude, dir) = agent ( ) ;
6430
assert! ( ! agent_home.join ( " keys" ) .exists ( ) , " no key on disk" ) ;
6431
write ( & dir, " work.txt" , " agent work\n " ) ;
6432
let change = claude.commit ( " Agent work" , & [ ] ) .unwrap ( ) ;
6433
let r = claude.push ( " origin" , false ) .unwrap ( ) ;
6434
assert! ( r.iter ( ) .all ( | x | x.ok ) , " {r:?}" ) ;
6435
let signer = claude.signing_key ( ) .expect ( " a certified key" ) ;
6436
assert_ne! ( signer, andy_key) ;
6437
assert! ( ! agent_home.join ( " keys" ) .exists ( ) , " still no key on disk" ) ;
6438
6439
6440
6441
let host = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
6442
assert_eq! ( host.store ( ) .get_change_meta ( & change) .unwrap ( ) .author, andy_key) ;
6443
assert! ( matches! (
6444
host.authenticate_change ( & change) .unwrap ( ) ,
6445
SignatureStatus:: Valid { key, sealed: true , .. } if key = = signer
6446
) ) ;
6447
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6448
assert! ( refs.refs.iter ( ) .find ( | r | r.name = = " main" ) .unwrap ( ) .accepted.is_some ( ) ) ;
6449
a.pull ( " origin" ) .unwrap ( ) ;
6450
assert! ( matches! ( a.authenticate_change ( & change) .unwrap ( ) , SignatureStatus:: Valid { .. } ) ) ;
6451
a66c05a6 Andy McCall 12 days ago ↶
6452
6453
6454
6455
create ( & h.app, " andy" , " fresh" ) .await;
6456
let fresh_url = format! ( " {} /andy/fresh" , h.base) ;
6457
let fresh_dir = work.path ( ) .join ( " claude-fresh" ) ;
6458
let mut fresh = client ( & fresh_url, & fresh_dir) .with_home ( agent_home.clone ( ) ) ;
6459
fresh.config.set ( " remote.origin.token" , & token) .unwrap ( ) ;
6460
fresh.save_config ( ) .unwrap ( ) ;
6461
fresh.set_user_config ( " author.key" , & andy_key.to_hex ( ) ) .unwrap ( ) ;
6462
fresh.set_user_config ( " author.signer" , " keyless" ) .unwrap ( ) ;
6463
assert! ( fresh.store ( ) .get_identity ( & andy_key) .is_err ( ) , " the fresh repository has never seen Andy" ) ;
6464
write ( & fresh_dir, " first.txt" , " first\n " ) ;
6465
let first = fresh.commit ( " First, by the agent" , & [ ] ) .unwrap ( ) ;
6466
let r = fresh.push ( " origin" , false ) .unwrap ( ) ;
6467
assert! ( r.iter ( ) .all ( | x | x.ok ) , " {r:?}" ) ;
6468
assert! ( matches! ( fresh.authenticate_change ( & first) .unwrap ( ) , SignatureStatus:: Valid { .. } ) , " locally" ) ;
6469
let fresh_host = h.app.repos.open ( & RepoName:: parse( " andy/fresh" ) .unwrap ( ) ) .unwrap ( ) ;
6470
assert! (
6471
matches! ( fresh_host.authenticate_change ( & first) .unwrap ( ) , SignatureStatus:: Valid { sealed: true , .. } ) ,
6472
" on the hub: {:?}" ,
6473
fresh_host.authenticate_change ( & first) .unwrap ( )
6474
) ;
6475
8aa5ef38 Andy McCall 19 days ago ↶
6476
6477
let ( st, _ ) = call ( & h.app, " DELETE" , & format! ( " /api/v1/agents/grants/{grant_id} " ) , Some ( & pat) , None ) .await;
6478
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
6479
let ( late, dir) = agent ( ) ;
6480
write ( & dir, " late.txt" , " after the revocation\n " ) ;
6481
late.commit ( " Late" , & [ ] ) .unwrap ( ) ;
6482
let err = late.push ( " origin" , false ) .unwrap_err ( ) .to_string ( ) ;
559c3920 Andy McCall 19 days ago ↶
6483
assert! ( err.contains ( " not valid" ) | | err.contains ( " would not certify" ) , " {err}" ) ;
6484
6485
let by_agent: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM pushes WHERE via = 'claude'" ) .fetch_one ( & h.app.db) .await.unwrap ( ) ;
6486
assert! ( by_agent > = 1 , " recorded as via the agent" ) ;
8aa5ef38 Andy McCall 19 days ago ↶
6487
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/agents/grants" , Some ( & pat) , None ) .await;
6488
let list: Vec< serde_json:: Value> = serde_json:: from_str( & body) .unwrap ( ) ;
6489
assert! ( list[ 0 ] [ " revoked_ms" ] .is_i64 ( ) & & list[ 0 ] .get ( " token" ) .is_none ( ) , " {body}" ) ;
6490
}
a9f1c0bf Andy McCall 17 days ago ↶
6491
6492
6493
6494
6495
6496
# [ tokio ::test ( flavor = " multi_thread" ) ]
6497
async fn newest_run_wins ( ) {
6498
use oxy_core:: hash:: ChangeHash;
6499
let h = hub ( ) .await;
6500
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
6501
create ( & h.app, " andy" , " newest" ) .await;
6502
let url = format! ( " {} /andy/newest" , h.base) ;
6503
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6504
let c = client ( & url, work.path ( ) ) ;
6505
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
6506
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
6507
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
6508
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6509
let pushed = runs_of ( & h.app, " andy" , " newest" ) .await[ 0 ] .clone ( ) ;
6510
assert_eq! ( ( pushed.number, pushed.trigger.as_str ( ) ) , ( 1 , " push" ) ) ;
6511
let heads: Vec< ChangeHash> = pushed.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
6512
let start = | row : db:: repos:: RepoRow, bookmark : & 'static str , trigger : & 'static str | {
6513
let ( app, state, heads) = ( h.app.clone ( ) , pushed.state.clone ( ) , heads.clone ( ) ) ;
6514
async move {
6515
oxygenhub:: flow:: start_run_scoped( & app, & row, bookmark, & state, & heads, " andy" , trigger, None ) .await.unwrap ( ) .expect ( " a run" )
6516
}
6517
} ;
6518
let row = db:: repos:: get( & h.app.db, " andy" , " newest" ) .await.unwrap ( ) .unwrap ( ) ;
6519
let run = | n : i64 | {
6520
let ( app, id) = ( h.app.clone ( ) , row.id) ;
6521
async move { db:: flow:: run( & app.db, id, n) .await.unwrap ( ) .unwrap ( ) }
6522
} ;
6523
let superseded_by = | n : i64 , by : i64 | {
6524
let app = h.app.clone ( ) ;
6525
let run = run ( n) ;
6526
async move {
6527
let r = run.await;
6528
assert_eq! ( r.status, " cancelled" , " run #{n} is superseded" ) ;
6529
let jobs = db:: flow:: jobs( & app.db, r.id) .await.unwrap ( ) ;
6530
let why = format! ( " superseded by run #{by} " ) ;
6531
assert! ( jobs.iter ( ) .all ( | j | j.status = = " cancelled" & & j.error = = why ) , " {jobs:?}" ) ;
6532
}
6533
} ;
6534
6535
6536
6537
assert_eq! ( start ( row.clone ( ) , " proposal/7" , " proposal" ) .await, 2 ) ;
6538
assert_eq! ( start ( row.clone ( ) , " proposal/7" , " proposal" ) .await, 3 ) ;
6539
superseded_by ( 2 , 3 ) .await;
6540
assert_eq! ( start ( row.clone ( ) , " proposal/8" , " proposal" ) .await, 4 ) ;
6541
assert! ( run ( 3 ) .await.live ( ) , " another proposal's run is not superseded" ) ;
6542
assert! ( run ( 1 ) .await.live ( ) , " a proposal's run leaves push runs alone" ) ;
6543
6544
6545
6546
assert_eq! ( start ( row.clone ( ) , " main" , " manual" ) .await, 5 ) ;
6547
assert_eq! ( start ( row.clone ( ) , " main" , " rerun:1:push" ) .await, 6 ) ;
6548
assert! ( run ( 1 ) .await.live ( ) , " a manual run or a rerun supersedes nothing" ) ;
6549
assert_eq! ( start ( row.clone ( ) , " main" , " push" ) .await, 7 ) ;
6550
superseded_by ( 1 , 7 ) .await;
6551
assert! ( run ( 5 ) .await.live ( ) & & run ( 6 ) .await.live ( ) , " a push leaves manual runs and reruns alone" ) ;
6552
6553
6554
6555
db:: repos:: set_flow_keep_running( & h.app.db, row.id, true ) .await.unwrap ( ) ;
6556
let kept = db:: repos:: get( & h.app.db, " andy" , " newest" ) .await.unwrap ( ) .unwrap ( ) ;
6557
assert! ( kept.flow_keep_running) ;
6558
let three = run ( 3 ) .await;
6559
sqlx:: query( " UPDATE flow_runs SET status = 'running' WHERE id = $1" ) .bind ( three.id) .execute ( & h.app.db) .await.unwrap ( ) ;
6560
sqlx:: query( " UPDATE flow_jobs SET status = 'running' WHERE run_id = $1 AND name = 'build'" )
6561
.bind ( three.id)
6562
.execute ( & h.app.db)
6563
.await
6564
.unwrap ( ) ;
6565
assert_eq! ( start ( kept.clone ( ) , " proposal/7" , " proposal" ) .await, 8 ) ;
6566
assert_eq! ( start ( kept.clone ( ) , " proposal/7" , " proposal" ) .await, 9 ) ;
6567
assert_eq! ( run ( 3 ) .await.status, " running" , " a running run is kept" ) ;
6568
superseded_by ( 8 , 9 ) .await;
6569
6570
db:: repos:: set_flow_keep_running( & h.app.db, row.id, false ) .await.unwrap ( ) ;
6571
let row = db:: repos:: get( & h.app.db, " andy" , " newest" ) .await.unwrap ( ) .unwrap ( ) ;
6572
assert_eq! ( start ( row.clone ( ) , " proposal/7" , " proposal" ) .await, 10 ) ;
6573
superseded_by ( 3 , 10 ) .await;
6574
superseded_by ( 9 , 10 ) .await;
6575
}
8501fd58 Andy McCall 11 days ago ↶
6576
6577
6578
6579
6580
# [ tokio ::test ( flavor = " multi_thread" ) ]
6581
async fn runners_are_offered_updates_by_their_nearest_setting ( ) {
6582
use oxy_core:: identity:: SecretKey;
6583
let h = hub ( ) .await;
6584
create ( & h.app, " andy" , " oxygenflow" ) .await;
6585
let res = oxygenhub:: router( h.app.clone ( ) )
6586
.oneshot (
6587
Request:: post( " /register" )
6588
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
6589
.body ( Body:: from( " username=carol&email=carol%40example.com&password=correct-horse-battery&setup=test-setup" ) )
6590
.unwrap ( ) ,
6591
)
6592
.await
6593
.unwrap ( ) ;
6594
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
6595
let admin = db:: accounts:: account_by_name( & h.app.db, " carol" ) .await.unwrap ( ) .unwrap ( ) ;
6596
db:: accounts:: set_admin( & h.app.db, admin.id, true ) .await.unwrap ( ) ;
6597
let web = | method : & 'static str , path : & str , body : Option< String> | {
6598
let ( app, cookie, path) = ( h.app.clone ( ) , cookie.clone ( ) , path.to_string ( ) ) ;
6599
async move {
6600
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie) ;
6601
if body.is_some ( ) {
6602
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
6603
}
6604
let res = oxygenhub:: router( app) .oneshot ( req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ) .await.unwrap ( ) ;
6605
let st = res.status ( ) ;
6606
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
6607
}
6608
} ;
6609
let ( st, page) = web ( " GET" , " /admin/runners/updates" , None ) .await;
6610
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
6611
let csrf = {
6612
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
6613
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
6614
} ;
6615
assert_eq! ( call ( & h.app, " GET" , " /admin/runners/updates" , None , None ) .await.0 , StatusCode:: NOT_FOUND , " administrators only" ) ;
6616
let mut creds = vec! [ ] ;
6617
for name in [ " plex" , " narvi" ] {
6618
let ( _ , page) = web ( " POST" , " /admin/runners/token" , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
6619
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
6620
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
6621
let identity = SecretKey:: generate( ) .identity ( & format! ( " {name} (OxygenFlow runner)" ) , " " , 1 ) ;
6622
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0 (build)" , " max_jobs" : 1 } ) ;
6623
let reg = serde_json:: json! ( { " token" : token, " name" : name, " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
6624
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
6625
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
6626
creds.push ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
6627
}
6628
let ask = | who : usize | {
6629
let ( app, cred) = ( h.app.clone ( ) , creds[ who] .clone ( ) ) ;
6630
async move {
6631
let ( st, body) = call ( & app, " GET" , " /api/v1/flow/runner/update" , Some ( & cred) , None ) .await;
6632
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
6633
serde_json:: from_str:: < flow_core:: api:: UpdateAnswer> ( & body) .unwrap ( )
6634
}
6635
} ;
6636
let a = ask ( 0 ) .await;
6637
assert! ( ! a.policy.updates & & a.offer.is_none ( ) , " off unless chosen: {a:?}" ) ;
6638
assert_eq! ( a.note, " updates are off (default)" ) ;
6639
assert_eq! ( call ( & h.app, " GET" , " /api/v1/flow/runner/update" , Some ( " flowrunner_nope" ) , None ) .await.0 , StatusCode:: UNAUTHORIZED ) ;
6640
6641
6642
let repo = db:: repos:: get( & h.app.db, " andy" , " oxygenflow" ) .await.unwrap ( ) .unwrap ( ) ;
6643
let rel = db:: releases:: create( & h.app.db, repo.id, " v0.2.0" , " 0.2.0" , " " , false , None ) .await.unwrap ( ) ;
6644
db:: releases:: add_asset( & h.app.db, rel.id, " flow-linux-x86_64" , 42 , & " ab" .repeat ( 32 ) , " application/octet-stream" ) .await.unwrap ( ) ;
6645
let ( _ , page) = web ( " POST" , " /admin/runners/updates/source" , Some ( format! ( " _csrf={csrf} &repo=andy%2Fnope" ) ) ) .await;
6646
assert! ( page.contains ( " There is no repository" ) , " {page}" ) ;
6647
let ( _ , page) = web ( " POST" , " /admin/runners/updates/source" , Some ( format! ( " _csrf={csrf} &repo=andy%2Foxygenflow" ) ) ) .await;
6648
assert! ( page.contains ( " runner builds come from andy/oxygenflow" ) , " {page}" ) ;
6649
6650
6651
let plex = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " plex" ) .unwrap ( ) ;
6652
let ( _ , page) =
6653
web ( " POST" , " /admin/runners/updates/groups" , Some ( format! ( " _csrf={csrf} &name=canary&mode=chosen&runner={} " , plex.id) ) ) .await;
6654
assert! ( page.contains ( " Test group canary made: 1 chosen runners" ) , " {page}" ) ;
6655
let group = db:: runner_updates:: groups( & h.app.db) .await.unwrap ( ) .remove ( 0 ) ;
6656
let ( _ , page) =
6657
web ( " POST" , " /admin/runners/updates/policy" , Some ( format! ( " _csrf={csrf} &scope=group&key={} &updates=on" , group.id) ) ) .await;
6658
assert! ( page.contains ( " Saved for group canary: updates on." ) , " {page}" ) ;
6659
let a = ask ( 0 ) .await;
6660
let offer = a.offer.clone ( ) .expect ( " plex is offered the build" ) ;
6661
assert_eq! ( ( offer.version.as_str ( ) , offer.asset.as_str ( ) , offer.blake3.len ( ) ) , ( " 0.2.0" , " flow-linux-x86_64" , 64 ) ) ;
6662
assert! ( offer.url.starts_with ( " /andy/oxygenflow/asset/" ) & & ! offer.older, " {offer:?}" ) ;
6663
assert_eq! ( a.policy.from[ 0 ] , " group canary" ) ;
6664
assert! ( ask ( 1 ) .await.offer.is_none ( ) , " narvi is not in the group" ) ;
6665
6666
6667
let ( _ , page) = web ( " POST" , " /admin/runners/updates/policy" , Some ( format! ( " _csrf={csrf} &scope=site&updates=off&dry_run=on" ) ) ) .await;
6668
assert! ( page.contains ( " Saved for the site" ) , " {page}" ) ;
6669
let a = ask ( 0 ) .await;
6670
assert! ( a.policy.dry_run & & a.policy.from[ 3 ] = = " site" & & a.note.contains ( " dry run" ) , " {a:?}" ) ;
6671
let narvi = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " narvi" ) .unwrap ( ) ;
6672
web ( " POST" , " /admin/runners/updates/policy" , Some ( format! ( " _csrf={csrf} &scope=runner&key={} &updates=on&channel=0.1.0" , narvi.id) ) )
6673
.await;
6674
let b = ask ( 1 ) .await;
6675
assert! ( b.policy.updates & & b.offer.is_none ( ) & & b.note.contains ( " has no release" ) , " {b:?}" ) ;
6676
6677
6678
let now = time:: OffsetDateTime:: now_utc( ) .hour ( ) ;
6679
let ( from, to) = ( ( now + 2 ) % 24 , ( now + 4 ) % 24 ) ;
6680
web (
6681
" POST" ,
6682
" /admin/runners/updates/policy" ,
6683
Some ( format! ( " _csrf={csrf} &scope=group&key={} &updates=on&window={from:02} -{to:02} " , group.id) ) ,
6684
)
6685
.await;
6686
let a = ask ( 0 ) .await;
6687
assert! ( a.offer.is_none ( ) & & a.note.contains ( " waits for its window" ) , " {a:?}" ) ;
6688
6689
6690
let ( _ , page) = web ( " POST" , " /admin/runners/updates/policy" , Some ( format! ( " _csrf={csrf} &scope=site&channel=latest" ) ) ) .await;
6691
assert! ( page.contains ( " A channel is stable, canary or a version" ) , " {page}" ) ;
6692
let ( _ , page) = web ( " POST" , " /admin/runners/updates/policy" , Some ( format! ( " _csrf={csrf} &scope=site&window=9" ) ) ) .await;
6693
assert! ( page.contains ( " a window is two hours" ) , " {page}" ) ;
6694
let ( _ , page) = web ( " POST" , " /admin/runners/updates/groups" , Some ( format! ( " _csrf={csrf} &name=farm&mode=percent&percent=0" ) ) ) .await;
6695
assert! ( page.contains ( " A percentage is a whole number from 1 to 100." ) , " {page}" ) ;
6696
6697
6698
let report = serde_json:: json! ( { " note" : " dry run: flow 0.2.0 checks out" } ) .to_string ( ) ;
6699
assert_eq! ( call ( & h.app, " POST" , " /api/v1/flow/runner/update" , Some ( & creds[ 0 ] ) , Some ( report) ) .await.0 , StatusCode:: NO_CONTENT ) ;
6700
let ( _ , page) = web ( " GET" , " /admin/runners/updates" , None ) .await;
6701
assert! ( page.contains ( " dry run: flow 0.2.0 checks out" ) & & page.contains ( " >canary</span>" ) , " {page}" ) ;
6702
assert! ( page.contains ( " updates on (group canary)" ) & & page.contains ( " updates on (runner) · channel 0.1.0 (runner)" ) , " {page}" ) ;
6703
9bbca80f Andy McCall 11 days ago ↶
6704
6705
let ( _ , tokens) = web ( " GET" , " /settings/tokens" , None ) .await;
6706
let tcsrf = {
6707
let i = tokens.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
6708
tokens[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
6709
} ;
6710
let ( _ , made) = web ( " POST" , " /settings/tokens" , Some ( format! ( " _csrf={tcsrf} &name=admin&scopes=user%3Aread" ) ) ) .await;
6711
let i = made.find ( " oxyhub_" ) .unwrap ( ) ;
6712
let admin_token: String = made[ i.. ] .chars ( ) .take_while ( | c | c.is_ascii_alphanumeric ( ) | | * c = = ' _' ) .collect ( ) ;
6713
let ( st, body) = call ( & h.app, " GET" , " /api/v1/admin/runner-updates" , Some ( & admin_token) , None ) .await;
6714
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
6715
let all: hub_api:: RunnerUpdates = serde_json:: from_str( & body) .unwrap ( ) ;
6716
assert_eq! ( all.repo, " andy/oxygenflow" ) ;
6717
assert! ( all.settings.iter ( ) .any ( | s | s.scope = = " group" & & s.updates = = Some ( true ) & & s.window.is_some ( ) ) , " {all:?}" ) ;
6718
let plex_row = all.runners.iter ( ) .find ( | r | r.name = = " plex" ) .unwrap ( ) ;
6719
assert! ( plex_row.updates & & plex_row.from[ 0 ] = = " group canary" & & plex_row.groups = = [ " canary" ] , " {plex_row:?}" ) ;
6720
let put = | body : serde_json:: Value| {
6721
let ( app, t) = ( h.app.clone ( ) , admin_token.clone ( ) ) ;
6722
async move { call ( & app, " PUT" , " /api/v1/admin/runner-updates/settings" , Some ( & t) , Some ( body.to_string ( ) ) ) .await }
6723
} ;
6724
let ( st, body) =
6725
put ( serde_json:: json! ( { " scope" : " runner" , " key" : narvi.id.to_string ( ) , " updates" : true , " channel" : " 0.1.0" , " dry_run" : true } ) )
6726
.await;
6727
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
6728
assert! ( body.contains ( " Saved for runner narvi: updates on, channel 0.1.0, dry run on." ) , " {body}" ) ;
6729
let ( st, body) = put ( serde_json:: json! ( { " scope" : " site" , " channel" : " latest" } ) ) .await;
6730
assert_eq! ( st, StatusCode:: UNPROCESSABLE_ENTITY , " {body}" ) ;
6731
assert_eq! (
6732
call ( & h.app, " GET" , " /api/v1/admin/runner-updates" , Some ( & creds[ 0 ] ) , None ) .await.0 ,
6733
StatusCode:: UNAUTHORIZED ,
6734
" not a runner's"
6735
) ;
6736
faf5246c Andy McCall 11 days ago ↶
6737
6738
let report = serde_json:: json! ( { " note" : " rolled back from flow 0.2.0 to 0.1.0: 3 starts without reaching the hub" } ) .to_string ( ) ;
6739
assert_eq! ( call ( & h.app, " POST" , " /api/v1/flow/runner/update" , Some ( & creds[ 0 ] ) , Some ( report) ) .await.0 , StatusCode:: NO_CONTENT ) ;
6740
let a = ask ( 0 ) .await;
6741
assert! ( a.offer.is_none ( ) & & a.note.contains ( " flow 0.2.0 is held: it rolled back on plex (3 starts" ) , " {a:?}" ) ;
6742
let ( _ , page) = web ( " GET" , " /admin/runners/updates" , None ) .await;
6743
assert! ( page.contains ( " Held builds" ) & & page.contains ( " <strong>flow 0.2.0</strong>" ) , " {page}" ) ;
9bbca80f Andy McCall 11 days ago ↶
6744
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/admin/runner-updates" , Some ( & admin_token) , None ) .await;
6745
assert! ( body.contains ( " \" version\" :\" 0.2.0\" " ) & & body.contains ( " \" runner\" :\" plex\" " ) , " the hold through the API: {body}" ) ;
faf5246c Andy McCall 11 days ago ↶
6746
let ( _ , page) = web ( " POST" , " /admin/runners/updates/holds/lift" , Some ( format! ( " _csrf={csrf} &version=0.2.0" ) ) ) .await;
6747
assert! ( page.contains ( " flow 0.2.0 is offered again" ) , " {page}" ) ;
9bbca80f Andy McCall 11 days ago ↶
6748
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/admin/runner-updates/holds/0.2.0/lift" , Some ( & admin_token) , Some ( " {}" .into ( ) ) ) .await;
6749
assert_eq! ( st, StatusCode:: NOT_FOUND , " no longer held" ) ;
faf5246c Andy McCall 11 days ago ↶
6750
assert! ( ask ( 0 ) .await.note.contains ( " waits for its window" ) , " lifted: back to what its settings say" ) ;
6751
let ( _ , page) = web ( " POST" , " /admin/runners/updates/holds/lift" , Some ( format! ( " _csrf={csrf} &version=0.2.0" ) ) ) .await;
6752
assert! ( page.contains ( " flow 0.2.0 is not held." ) , " {page}" ) ;
6753
8501fd58 Andy McCall 11 days ago ↶
6754
6755
let ( _ , page) =
6756
web ( " POST" , & format! ( " /admin/runners/updates/groups/{} " , group.id) , Some ( format! ( " _csrf={csrf} &mode=percent&percent=100" ) ) ) .await;
6757
assert! ( page.contains ( " Test group canary is now 100% of its runners." ) , " {page}" ) ;
6758
assert! ( ask ( 1 ) .await.policy.from[ 0 ] = = " runner" , " narvi's own setting is nearer than any group" ) ;
6759
let ( _ , page) = web ( " POST" , & format! ( " /admin/runners/updates/groups/{} /delete" , group.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
6760
assert! ( page.contains ( " Test group canary removed" ) , " {page}" ) ;
6761
assert! ( ! ask ( 0 ) .await.policy.updates, " back to the site's off" ) ;
6762
}
d7050703 Andy McCall 11 days ago ↶
6763
6764
6765
# [ tokio ::test ( flavor = " multi_thread" ) ]
6766
async fn a_wasm_jobs_placement_names_no_image ( ) {
6767
let h = hub ( ) .await;
6768
create ( & h.app, " andy" , " wasm" ) .await;
6769
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6770
let c = client ( & format! ( " {} /andy/wasm" , h.base) , work.path ( ) ) ;
6771
write (
6772
work.path ( ) ,
6773
" .flow/pipeline.kdl" ,
6774
" schema-version 1\n sandbox \" w\" {\n runtime \" wasm\" \n }\n job \" count\" {\n sandbox \" w\" \n exec \" count.wasm\" \n }\n " ,
6775
) ;
6776
write ( work.path ( ) , " count.wasm" , " \0 asm" ) ;
6777
c.commit ( " A WASM job" , & [ ] ) .unwrap ( ) ;
6778
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6779
runs_of ( & h.app, " andy" , " wasm" ) .await;
6780
let ( st, body) =
6781
call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( r # "{"runner":"test-1","runtimes":["wasm"]}"# .into ( ) ) ) .await;
6782
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
6783
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
6784
let placed = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .placed;
6785
assert! ( placed.starts_with ( " idle" ) & & ! placed.contains ( " image" ) , " {placed}" ) ;
6786
}
dc5c6429 Andy McCall 11 days ago ↶
6787
6788
6789
6790
# [ tokio ::test ( flavor = " multi_thread" ) ]
6791
async fn an_organisation_sets_its_own_runners_updates ( ) {
6792
use oxy_core:: identity:: SecretKey;
6793
let h = hub ( ) .await;
6794
let res = oxygenhub:: router( h.app.clone ( ) )
6795
.oneshot (
6796
Request:: post( " /register" )
6797
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
6798
.body ( Body:: from( " username=andy&email=andy%40example.com&password=correct-horse-battery&setup=test-setup" ) )
6799
.unwrap ( ) ,
6800
)
6801
.await
6802
.unwrap ( ) ;
6803
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
6804
let web = | method : & 'static str , path : String, body : Option< String> | {
6805
let ( app, cookie) = ( h.app.clone ( ) , cookie.clone ( ) ) ;
6806
async move {
6807
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie) ;
6808
if body.is_some ( ) {
6809
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
6810
}
6811
let res = oxygenhub:: router( app) .oneshot ( req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ) .await.unwrap ( ) ;
6812
let st = res.status ( ) ;
6813
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
6814
}
6815
} ;
6816
let csrf_of = | page : & str | {
6817
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
6818
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
6819
} ;
6820
let ( _ , page) = web ( " GET" , " /orgs/new" .into ( ) , None ) .await;
6821
let csrf = csrf_of ( & page) ;
6822
web ( " POST" , " /orgs/new" .into ( ) , Some ( format! ( " _csrf={csrf} &name=acme&display_name=Acme" ) ) ) .await;
6823
6824
let mut creds = vec! [ ] ;
6825
for name in [ " a1" , " a2" ] {
6826
let ( _ , page) = web ( " POST" , " /orgs/acme/settings/runners/token" .into ( ) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
6827
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
6828
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
6829
let identity = SecretKey:: generate( ) .identity ( name, " " , 1 ) ;
6830
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " max_jobs" : 1 } ) ;
6831
let reg = serde_json:: json! ( { " token" : token, " name" : name, " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
6832
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
6833
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
6834
creds.push ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
6835
}
6836
claim ( & h.app) .await;
6837
let all = db:: runners:: list( & h.app.db) .await.unwrap ( ) ;
6838
let id = | n : & str | all.iter ( ) .find ( | r | r.name = = n ) .unwrap ( ) .id ;
6839
let set = | form : String| web ( " POST" , " /orgs/acme/settings/runners/updates/policy" .into ( ) , Some ( format! ( " _csrf={csrf} &{form} " ) ) ) ;
6840
6841
let ( _ , page) = set ( " scope=owner&key=acme&updates=on" .into ( ) ) .await;
6842
assert! ( page.contains ( " Saved for owner acme: updates on." ) , " {page}" ) ;
6843
let ( _ , page) = set ( " scope=site&updates=on" .into ( ) ) .await;
6844
assert! ( page.contains ( " Choose the site, an owner, a test group or a runner." ) , " not the site's: {page}" ) ;
6845
let ( _ , page) = set ( " scope=owner&key=andy&updates=on" .into ( ) ) .await;
6846
assert! ( page.contains ( " An organisation sets its own runners" ) , " {page}" ) ;
6847
let ( _ , page) = set ( format! ( " scope=runner&key={} &updates=on" , id ( " test-1" ) ) ) .await;
6848
assert! ( page.contains ( " That runner is gone." ) , " the site's runner is not acme's: {page}" ) ;
6849
6850
6851
let ( _ , page) = web (
6852
" POST" ,
6853
" /orgs/acme/settings/runners/updates/groups" .into ( ) ,
6854
Some ( format! ( " _csrf={csrf} &name=canary&owner=&mode=chosen&runner={} &runner={} " , id ( " a1" ) , id ( " test-1" ) ) ) ,
6855
)
6856
.await;
6857
assert! ( page.contains ( " Test group canary made: 1 chosen runners" ) , " {page}" ) ;
6858
let canary = db:: runner_updates:: groups( & h.app.db) .await.unwrap ( ) .remove ( 0 ) ;
6859
assert_eq! ( canary.owner, " acme" , " an organisation's groups are its own" ) ;
6860
let ( _ , page) = set ( format! ( " scope=group&key={} &channel=canary" , canary.id) ) .await;
6861
assert! ( page.contains ( " Saved for group canary: channel canary." ) , " {page}" ) ;
6862
let ( _ , page) = set ( format! ( " scope=runner&key={} &updates=off" , id ( " a2" ) ) ) .await;
6863
assert! ( page.contains ( " Saved for runner a2: updates off." ) , " {page}" ) ;
6864
6865
6866
let site = db:: runner_updates:: create_group( & h.app.db, " " , " everyone" , Some ( 100 ) ) .await.unwrap ( ) ;
6867
let ( _ , page) =
6868
web ( " POST" , format! ( " /orgs/acme/settings/runners/updates/groups/{} /delete" , site.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
6869
assert! ( page.contains ( " That test group is gone." ) , " {page}" ) ;
6870
assert! ( db:: runner_updates:: group( & h.app.db, site.id) .await.unwrap ( ) .is_some ( ) ) ;
6871
6872
6873
let ( _ , page) = web ( " GET" , " /orgs/acme/settings" .into ( ) , None ) .await;
6874
assert! ( page.contains ( " id=\" runner-updates\" " ) & & page.contains ( " >canary <small" ) , " {page}" ) ;
6875
assert! ( page.contains ( " updates on (owner acme)" ) & & page.contains ( " updates off (runner)" ) , " {page}" ) ;
6876
let ask = | cred : String| {
6877
let app = h.app.clone ( ) ;
6878
async move {
6879
let ( _ , body) = call ( & app, " GET" , " /api/v1/flow/runner/update" , Some ( & cred) , None ) .await;
6880
serde_json:: from_str:: < flow_core:: api:: UpdateAnswer> ( & body) .unwrap ( ) .policy
6881
}
6882
} ;
6883
let a1 = ask ( creds[ 0 ] .clone ( ) ) .await;
6884
assert! ( a1.updates & & a1.channel = = " canary" & & a1.from[ .. 2 ] = = [ " owner acme" .to_string ( ) , " group canary" .to_string ( ) ] , " {a1:?}" ) ;
6885
assert! ( ! ask ( creds[ 1 ] .clone ( ) ) .await.updates, " a2's own setting wins" ) ;
6886
}