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 21 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 24 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 24 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 21 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 21 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 21 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 21 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 ( ) ) ) ;
3d086bc3 Andy McCall 9 days ago ↶
613
614
let ( st, body) = call ( & h.app, " GET" , " /api/v1/user/flow/runs" , Some ( " site" ) , None ) .await;
615
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
616
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
617
assert_eq! ( v.as_array ( ) .unwrap ( ) .len ( ) , 2 , " {v}" ) ;
618
assert_eq! (
619
( v[ 0 ] [ " repository" ] .as_str ( ) , v[ 0 ] [ " number" ] .as_i64 ( ) , v[ 1 ] [ " number" ] .as_i64 ( ) ) ,
620
( Some ( " andy/demo" ) , Some ( 2 ) , Some ( 1 ) ) ,
621
" {v}"
622
) ;
623
assert! ( ! v[ 0 ] [ " jobs" ] .as_array ( ) .unwrap ( ) .is_empty ( ) , " with its jobs: {v}" ) ;
624
let ( _ , body) = call ( & h.app, " GET" , & format! ( " /api/v1/user/flow/runs?before={} " , v[ 0 ] [ " created_ms" ] ) , Some ( " site" ) , None ) .await;
625
let older: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
626
assert! ( older.as_array ( ) .unwrap ( ) .iter ( ) .all ( | r | r[ " number" ] ! = 2 ) , " before pages back: {older}" ) ;
627
assert_eq! ( call ( & h.app, " GET" , " /api/v1/user/flow/runs" , None , None ) .await.0 , StatusCode:: UNAUTHORIZED ) ;
1edac4b6 Andy McCall 1 month ago ↶
628
assert_eq! ( db:: flow:: jobs( & h.app.db, second.id) .await.unwrap ( ) .len ( ) , 3 ) ;
629
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/2" , None , None ) .await;
630
assert! ( page.contains ( " started by andy" ) , " the run page says what started it" ) ;
631
4f692533 Andy McCall 1 month ago ↶
632
633
let head = first[ 0 ] .head_list ( ) [ 0 ] .to_string ( ) ;
634
let ( st, body) =
635
call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( format! ( r # "{{ "change":"{head} "}} "# ) ) ) .await;
636
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
637
let on_change = db:: flow:: run( & h.app.db, first[ 0 ] .repo_id, 3 ) .await.unwrap ( ) .unwrap ( ) ;
638
assert_eq! (
639
( on_change.bookmark.as_str ( ) , on_change.heads.as_str ( ) , on_change.state.as_str ( ) ) ,
640
( " " , head.as_str ( ) , first[ 0 ] .state.as_str ( ) )
641
) ;
642
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/3" , None , None ) .await;
643
assert! ( page.contains ( " title=\" Run on one change, not a bookmark\" >change</span>" ) , " run page" ) ;
644
let ( st, body) =
645
call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( format! ( r # "{{ "change":"{} "}} "# , " ab" .repeat ( 32 ) ) ) )
646
.await;
647
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " no change" ) , " {st} {body}" ) ;
648
649
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow" , None , None ) .await;
650
assert! ( page.contains ( " Sign in with write access to run the pipeline." ) & & ! page.contains ( " primary-btn" ) ) ;
651
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/bookmarks" , None , None ) .await;
652
assert! ( ! page.contains ( " /flow/start" ) , " no run buttons for anonymous viewers" ) ;
653
1edac4b6 Andy McCall 1 month ago ↶
654
655
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs/1/rerun" , Some ( " site" ) , None ) .await;
656
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " still queued" ) , " {st} {body}" ) ;
657
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( r # "{"bookmark":"nope"}"# .into ( ) ) ) .await;
658
assert_eq! ( st, StatusCode:: CONFLICT ) ;
659
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs/99/rerun" , Some ( " site" ) , None ) .await;
660
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
661
662
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , None , Some ( " {}" .into ( ) ) ) .await;
663
assert_ne! ( st, StatusCode:: CREATED ) ;
664
665
666
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
667
for r in [ & first[ 0 ] , & second] {
668
for j in db:: flow:: jobs( & h.app.db, r.id) .await.unwrap ( ) {
3b1bc27f Andy McCall 21 days ago
669
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 ↶
670
}
3b1bc27f Andy McCall 21 days ago
671
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 ↶
672
}
673
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs/1/rerun" , Some ( " site" ) , None ) .await;
674
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
4f692533 Andy McCall 1 month ago ↶
675
let third = db:: flow:: run( & h.app.db, row.id, 4 ) .await.unwrap ( ) .unwrap ( ) ;
348f8c4b Andy McCall 1 month ago ↶
676
assert_eq! (
677
( third.trigger_label ( ) .as_str ( ) , third.status.as_str ( ) , third.state.as_str ( ) ) ,
678
( " rerun:1" , " queued" , first[ 0 ] .state.as_str ( ) )
679
) ;
c889911b Andy McCall 1 month ago ↶
680
assert_eq! ( third.trigger, " rerun:1:push" , " a rerun keeps the event it repeats" ) ;
1edac4b6 Andy McCall 1 month ago ↶
681
let checks = db:: hooks:: checks_for_state( & h.app.db, row.id, & third.state) .await.unwrap ( ) ;
682
assert! (
4f692533 Andy McCall 1 month ago ↶
683
checks.iter ( ) .all ( | c | c.status = = " queued" & & c.url.ends_with ( " /flow/4" ) ) ,
1edac4b6 Andy McCall 1 month ago ↶
684
" the re-run's checks replace the old ones: {checks:?}"
685
) ;
4f692533 Andy McCall 1 month ago ↶
686
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/4" , None , None ) .await;
1edac4b6 Andy McCall 1 month ago ↶
687
assert! ( page.contains ( " re-run of #1 by andy" ) ) ;
688
assert! ( ! page.contains ( " RE-RUN" ) , " anonymous viewers get no button" ) ;
689
0650d94e Andy McCall 26 days ago ↶
690
691
692
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( r # "{"jobs":["test"]}"# .into ( ) ) ) .await;
693
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
694
let n = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " number" ] .as_i64 ( ) .unwrap ( ) ;
695
let only = db:: flow:: run( & h.app.db, first[ 0 ] .repo_id, n) .await.unwrap ( ) .unwrap ( ) ;
696
let mut names: Vec< String> = db:: flow:: jobs( & h.app.db, only.id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
697
names.sort ( ) ;
698
assert_eq! ( names, [ " build" , " test" ] , " test and the job it depends on, not lint" ) ;
699
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( r # "{"jobs":["deploy"]}"# .into ( ) ) ) .await;
700
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " not in the pipeline" ) & & body.contains ( " build, lint, test" ) , " {st} {body}" ) ;
701
1edac4b6 Andy McCall 1 month ago ↶
702
703
db:: repos:: set_flow( & h.app.db, row.id, " off" ) .await.unwrap ( ) ;
704
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
705
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " not enabled" ) , " {body}" ) ;
4f692533 Andy McCall 1 month ago ↶
706
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow" , None , None ) .await;
707
assert! ( page.contains ( " Flow is off for this repository" ) , " and the Flow tab says so" ) ;
90326c96 Andy McCall 1 month ago ↶
708
}
709
710
711
712
# [ tokio ::test ( flavor = " multi_thread" ) ]
713
async fn runs_jobs_and_logs_are_in_the_api ( ) {
714
let h = hub ( ) .await;
715
create ( & h.app, " andy" , " demo" ) .await;
716
let url = format! ( " {} /andy/demo" , h.base) ;
717
let work = tempfile:: tempdir( ) .unwrap ( ) ;
718
let c = client ( & url, work.path ( ) ) ;
719
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
720
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
721
let head = c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
722
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
723
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
724
725
726
let a = claim ( & h.app) .await.unwrap ( ) ;
727
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
728
let recs =
729
serde_json:: json! ( { " records" : [ { " t" : 1 , " stream" : " stdout" , " text" : " one" } , { " t" : 3 , " stream" : " stderr" , " text" : " three" } ] } ) ;
730
let ( st, _ ) = call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( recs.to_string ( ) ) ) .await;
731
assert! ( st.is_success ( ) , " {st}" ) ;
732
733
let ( st, body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs" , None , None ) .await;
734
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
735
let runs: Vec< hub_api:: FlowRun> = serde_json:: from_str( & body) .unwrap ( ) ;
736
assert_eq! ( runs.len ( ) , 1 ) ;
737
assert_eq! ( ( runs[ 0 ] .number, runs[ 0 ] .state.as_str ( ) , runs[ 0 ] .trigger.as_str ( ) ) , ( 1 , run.state.as_str ( ) , " push" ) ) ;
738
assert! ( runs[ 0 ] .url.ends_with ( " /andy/demo/flow/1" ) & & runs[ 0 ] .jobs.is_empty ( ) ) ;
739
740
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , None , None ) .await;
741
let one: hub_api:: FlowRun = serde_json:: from_str( & body) .unwrap ( ) ;
742
let names: Vec< ( & str , & str ) > = one.jobs.iter ( ) .map ( | j | ( j.name.as_str ( ) , j.status.as_str ( ) ) ) .collect ( ) ;
743
assert_eq! ( names.len ( ) , 3 ) ;
744
let running = one.jobs.iter ( ) .find ( | j | j.status = = " running" ) .unwrap ( ) ;
745
assert_eq! ( running.name, a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) ) ;
746
assert! ( one.jobs.iter ( ) .any ( | j | j.name = = " test" & & j.depends_on = = [ " build" ] ) ) ;
747
748
let job = running.name.clone ( ) ;
749
let ( st, body) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/demo/flow/runs/1/jobs/{job} /log" ) , None , None ) .await;
750
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
751
let lines: Vec< hub_api:: FlowLogLine> = serde_json:: from_str( & body) .unwrap ( ) ;
752
assert! ( lines.iter ( ) .any ( | l | l.text = = " three" ) , " {body}" ) ;
753
let last = lines.last ( ) .unwrap ( ) .seq;
754
let ( _ , body) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/demo/flow/runs/1/jobs/{job} /log?after={last} " ) , None , None ) .await;
755
assert_eq! ( body, " []" , " nothing after the last line" ) ;
756
assert_eq! ( call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/9" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
757
assert_eq! ( call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1/jobs/nope/log" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
758
759
760
let by = | key : String| {
761
let app = h.app.clone ( ) ;
762
async move {
763
let ( st, body) = call ( & app, " GET" , & format! ( " /api/v1/repos/andy/demo/checks/{key} " ) , None , None ) .await;
764
assert_eq! ( st, StatusCode:: OK , " {key}: {body}" ) ;
765
serde_json:: from_str:: < Vec< hub_api:: Check> > ( & body) .unwrap ( ) .len ( )
766
}
767
} ;
768
checks_when ( & h.app, run.repo_id, & run.state, 3 ) .await;
769
assert_eq! ( by ( run.state.clone ( ) ) .await, 3 ) ;
770
assert_eq! ( by ( head.to_hex ( ) ) .await, 3 , " a single head names its state" ) ;
771
assert_eq! ( by ( " main" .into ( ) ) .await, 3 ) ;
772
let ( st, _ ) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/checks/no-such-bookmark" , None , None ) .await;
773
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
774
775
776
let ( st, body) = call ( & h.app, " GET" , " /api/v1/users/andy/repos" , None , None ) .await;
777
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
778
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
779
assert_eq! ( v[ " status" ] , 404 , " {body}" ) ;
1edac4b6 Andy McCall 1 month ago ↶
780
}
cc19697f Andy McCall 1 month ago ↶
781
782
async fn get_bytes ( app : & Shared, path : & str ) -> ( StatusCode, Vec< u8 > ) {
783
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( Request:: get( path) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
784
let status = res.status ( ) ;
785
( status, res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) )
786
}
787
788
# [ tokio ::test ( flavor = " multi_thread" ) ]
789
async fn signed_receipts_are_checked_kept_and_shown ( ) {
790
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectRef, ObjectType} ;
791
use oxy_core:: identity:: SecretKey;
792
use oxy_core:: objects:: StateObject;
793
use oxy_core:: receipt:: SignedReceipt;
794
let h = hub ( ) .await;
795
create ( & h.app, " andy" , " demo" ) .await;
796
let url = format! ( " {} /andy/demo" , h.base) ;
797
let work = tempfile:: tempdir( ) .unwrap ( ) ;
798
let c = client ( & url, work.path ( ) ) ;
799
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
800
c.snapshot ( " test" ) .unwrap ( ) ;
801
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
802
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
803
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
804
let heads: Vec< ChangeHash> = run.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
805
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads.clone ( ) ) .encode ( ) ) ;
806
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
807
let sk = SecretKey:: generate( ) ;
808
let runner = sk.identity ( " test-1 (OxygenFlow runner)" , " " , 1 ) ;
809
let sign = | r : & serde_json:: Value, state : ObjectRef, sk : & SecretKey, id : & oxy_core:: identity:: Identity| {
810
let r: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
811
let run_url = format! ( " {url} /flow/{} " , run.number) ;
812
hex:: encode( SignedReceipt:: sign( r.to_oxy ( ) , repo_id, state, & run_url, id, sk) .encode ( ) )
813
} ;
814
815
let a = claim ( & h.app) .await.unwrap ( ) ;
816
let b = claim ( & h.app) .await.unwrap ( ) ;
817
let ph = a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
818
let ( a_id, b_id) = ( a[ " job_id" ] .as_i64 ( ) .unwrap ( ) , b[ " job_id" ] .as_i64 ( ) .unwrap ( ) ) ;
819
820
let rb = receipt ( " build" , & ph, 0 ) ;
821
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : rb, " signed_receipt" : sign ( & rb, state, & sk, & runner) } ) ;
822
assert_eq! ( finish ( & h.app, a_id, body) .await, StatusCode:: NO_CONTENT ) ;
823
824
let other = SecretKey:: generate( ) ;
825
let rl = receipt ( " lint" , & ph, 0 ) ;
826
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 300 , " receipt" : rl,
827
" signed_receipt" : sign ( & rl, state, & other, & other.identity ( " mallory" , " " , 1 ) ) } ) ;
828
assert_eq! ( finish ( & h.app, b_id, body) .await, StatusCode:: NO_CONTENT ) ;
829
830
let t = claim ( & h.app) .await.unwrap ( ) ;
831
let rt = receipt ( " test" , & ph, 0 ) ;
832
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 500 , " receipt" : rt,
833
" signed_receipt" : sign ( & rt, ObjectRef( [ 9 ; 32 ] ) , & sk, & runner) } ) ;
834
assert_eq! ( finish ( & h.app, t[ " job_id" ] .as_i64 ( ) .unwrap ( ) , body) .await, StatusCode:: NO_CONTENT ) ;
835
836
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
837
let job = | n : & str | jobs.iter ( ) .find ( | j | j.name = = n ) .unwrap ( ) .clone ( ) ;
838
assert_eq! ( job ( " build" ) .status, " passed" ) ;
839
assert_eq! ( ( job ( " build" ) .receipt_attest.len ( ) , job ( " build" ) .receipt_signer.as_str ( ) ) , ( 64 , " test-1 (OxygenFlow runner)" ) ) ;
840
assert_eq! ( job ( " lint" ) .status, " passed" , " the job's result stands" ) ;
841
assert! ( job ( " lint" ) .receipt_attest.is_empty ( ) ) ;
842
assert! ( job ( " lint" ) .receipt_note.contains ( " not the key this runner signs with" ) , " {}" , job ( " lint" ) .receipt_note) ;
843
assert! ( job ( " test" ) .receipt_note.contains ( " another state" ) , " {}" , job ( " test" ) .receipt_note) ;
844
845
846
847
let kept = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .receipts ( & heads) .unwrap ( ) ;
848
assert_eq! ( kept.len ( ) , 1 ) ;
849
let attest = job ( " build" ) .receipt_attest;
850
let ( st, bytes) = get_bytes ( & h.app, & format! ( " /andy/demo/receipts/{attest} " ) ) .await;
851
assert_eq! ( st, StatusCode:: OK ) ;
852
let sr = SignedReceipt:: decode( & bytes) .unwrap ( ) ;
853
sr.verify ( ) .unwrap ( ) ;
854
assert_eq! ( ( sr.state ( ) , sr.receipt.job_name.as_str ( ) ) , ( state, " build" ) ) ;
855
assert_eq! ( get_bytes ( & h.app, & format! ( " /andy/demo/receipts/{} " , " 0" .repeat ( 64 ) ) ) .await.0 , StatusCode:: NOT_FOUND ) ;
856
857
858
859
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
860
assert! ( page.contains ( " signed receipt</a> by test-1" ) , " run page" ) ;
861
assert! ( page.contains ( " receipt not kept" ) , " run page" ) ;
862
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/change/{} " , heads[ 0 ] .to_hex ( ) ) , None , None ) .await;
863
assert! ( page.contains ( " <dt>Receipts</dt>" ) & & page.contains ( & format! ( " /andy/demo/receipts/{attest} " ) ) , " change page" ) ;
864
}
a3c8288b Andy McCall 1 month ago ↶
865
866
async fn upload ( app : & Shared, job : i64 , name : & str , bytes : & [u8 ]) -> ( StatusCode, String) {
867
let req = Request:: post( format! ( " /api/v1/flow/jobs/{job} /artifacts/{name} " ) )
868
.header ( header:: AUTHORIZATION , " Bearer runner-secret" )
869
.header ( header:: CONTENT_TYPE , " application/octet-stream" )
870
.body ( Body:: from( bytes.to_vec ( ) ) )
871
.unwrap ( ) ;
872
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
873
let status = res.status ( ) ;
874
( status, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
875
}
876
877
# [ tokio ::test ( flavor = " multi_thread" ) ]
878
async fn artifacts_are_kept_named_by_the_receipt_and_served ( ) {
879
let h = hub ( ) .await;
880
create ( & h.app, " andy" , " demo" ) .await;
881
let url = format! ( " {} /andy/demo" , h.base) ;
882
let work = tempfile:: tempdir( ) .unwrap ( ) ;
883
let c = client ( & url, work.path ( ) ) ;
884
write (
885
work.path ( ) ,
886
" .flow/pipeline.kdl" ,
887
& format! (
888
" 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 "
889
) ,
890
) ;
891
c.snapshot ( " test" ) .unwrap ( ) ;
892
c.commit ( " Build a tool" , & [ ] ) .unwrap ( ) ;
893
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
894
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
895
let a = claim ( & h.app) .await.unwrap ( ) ;
896
assert_eq! ( a[ " job" ] [ " outputs" ] , serde_json:: json! ( [ { " name" : " tool" , " path" : " /src/out/tool" } ] ) , " the runner is told" ) ;
897
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
898
899
let body: Vec< u8 > = ( 0 .. 300_000 u32 ) .map ( | i | ( i % 253 ) as u8 ) .collect ( ) ;
900
let ( st, text) = upload ( & h.app, id, " other" , & body) .await;
901
assert_eq! ( st, StatusCode:: BAD_REQUEST , " an undeclared artifact: {text}" ) ;
902
let ( st, text) = upload ( & h.app, id, " tool" , & body) .await;
903
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
904
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
905
assert_eq! ( blob, oxy_core:: repo:: Repo:: blob_ref( & body) .to_hex ( ) , " the same blob the runner computes" ) ;
906
907
908
let mut r = receipt ( " build" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
909
assert_eq! (
910
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : r.clone ( ) } ) ) .await,
911
StatusCode:: BAD_REQUEST
912
) ;
913
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
914
assert_eq! ( finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : r } ) ) .await, StatusCode:: NO_CONTENT ) ;
915
916
917
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
918
let link = format! ( " /andy/demo/flow/{} /artifacts/build/tool" , run.number) ;
919
assert! ( page.contains ( & link) , " run page lists the artifact" ) ;
16ee5d19 Andy McCall 19 days ago ↶
920
921
922
let ( _ , list) = call ( & h.app, " GET" , " /andy/demo/artifacts" , None , None ) .await;
923
assert! ( list.contains ( & link) & & list.contains ( & format! ( " #{} </a>" , run.number) ) , " {list}" ) ;
924
assert! ( list.contains ( " <summary class=\" on\" >Artifacts</summary>" ) & & list.contains ( " href=\" /andy/demo/images\" " ) , " {list}" ) ;
a3c8288b Andy McCall 1 month ago ↶
925
let ( st, got) = get_bytes ( & h.app, & link) .await;
926
assert_eq! ( ( st, got = = body) , ( StatusCode:: OK , true ) ) ;
927
assert_eq! ( get_bytes ( & h.app, & format! ( " /andy/demo/flow/{} /artifacts/build/none" , run.number) ) .await.0 , StatusCode:: NOT_FOUND ) ;
076c7a7c Andy McCall 21 days ago ↶
928
929
930
let prov = format! ( " {link} /provenance" ) ;
931
assert_eq! ( get_bytes ( & h.app, & prov) .await.0 , StatusCode:: BAD_REQUEST , " no kept receipt, no provenance" ) ;
932
sqlx:: query( " UPDATE flow_jobs SET receipt_attest = 'a1b2', receipt_signer = 'test-1 runner' WHERE id = $1" )
933
.bind ( id)
934
.execute ( & h.app.db)
935
.await
936
.unwrap ( ) ;
937
let ( st, env) = get_bytes ( & h.app, & prov) .await;
938
assert_eq! ( st, StatusCode:: OK ) ;
939
let env: serde_json:: Value = serde_json:: from_slice( & env) .unwrap ( ) ;
940
assert_eq! ( env[ " payloadType" ] , " application/vnd.in-toto+json" ) ;
941
let unb64 = | t : & str | -> Vec< u8 > {
942
let val = | c : u8 | match c {
943
b ' A' .. = b ' Z' => c - b ' A' ,
944
b ' a' .. = b ' z' => c - b ' a' + 26 ,
945
b ' 0' .. = b ' 9' => c - b ' 0' + 52 ,
946
b ' +' => 62 ,
947
_ => 63 ,
948
} ;
949
let clean: Vec< u8 > = t.bytes ( ) .filter ( | c | * c ! = b ' =' ) .collect ( ) ;
950
let mut out = Vec :: new( ) ;
951
for chunk in clean.chunks ( 4 ) {
952
let n = chunk.iter ( ) .enumerate ( ) .fold ( 0 u32 , | acc , ( i , c ) | acc | ( val ( * c) as u32 ) < < ( 18 - 6 * i) ) ;
953
for i in 0 .. chunk.len ( ) - 1 {
954
out.push ( ( n > > ( 16 - 8 * i) ) as u8 ) ;
955
}
956
}
957
out
958
} ;
959
let payload = unb64 ( env[ " payload" ] .as_str ( ) .unwrap ( ) ) ;
960
let sig = unb64 ( env[ " signatures" ] [ 0 ] [ " sig" ] .as_str ( ) .unwrap ( ) ) ;
961
let signed = oxygenhub:: provenance:: pae( " application/vnd.in-toto+json" , & payload) ;
962
ring:: signature:: UnparsedPublicKey:: new( & ring:: signature:: ED25519 , h.app.hub_key.identity.ed25519_pk)
963
.verify ( & signed, & sig)
964
.expect ( " the hub's key signed the envelope" ) ;
965
let statement: serde_json:: Value = serde_json:: from_slice( & payload) .unwrap ( ) ;
966
use sha2:: Digest;
967
assert_eq! ( statement[ " subject" ] [ 0 ] [ " digest" ] [ " sha256" ] , hex:: encode( sha2:: Sha256:: digest( & body) ) ) ;
968
assert_eq! ( statement[ " predicateType" ] , " https://slsa.dev/provenance/v1" ) ;
969
assert_eq! ( statement[ " predicate" ] [ " buildDefinition" ] [ " externalParameters" ] [ " job" ] , " build" ) ;
970
assert_eq! ( statement[ " predicate" ] [ " runDetails" ] [ " byproducts" ] [ 0 ] [ " digest" ] [ " oxygen-attestation" ] , " a1b2" ) ;
971
let ( _ , pem) = get_bytes ( & h.app, " /.well-known/oxyhub-signing.pem" ) .await;
972
let pem = String :: from_utf8( pem) .unwrap ( ) ;
973
let der = unb64 ( pem.lines ( ) .filter ( | l | ! l.starts_with ( " -----" ) ) .collect:: < String> ( ) .as_str ( ) ) ;
974
assert_eq! ( & der[ der.len ( ) - 32 .. ] , & h.app.hub_key.identity.ed25519_pk[ .. ] , " the PEM holds the hub's key" ) ;
975
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 ↶
976
45977501 Andy McCall 27 days ago ↶
977
978
979
{
980
let name = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
981
let mut hosted = oxy_core:: repo:: Repo:: open( & h.app.repos.dir ( & name) ) .unwrap ( ) ;
982
hosted.config.set ( " core.gc-grace" , " 0" ) .unwrap ( ) ;
983
hosted.save_config ( ) .unwrap ( ) ;
984
}
985
let done = oxygenhub:: maintenance:: gc_all( & h.app) .await;
986
assert! ( done.iter ( ) .all ( | d | ! d.failed) , " {done:?}" ) ;
987
let ( st, got) = get_bytes ( & h.app, & link) .await;
988
assert_eq! ( ( st, got = = body) , ( StatusCode:: OK , true ) , " the artifact survives gc" ) ;
989
10708ab8 Andy McCall 1 month ago ↶
990
991
992
let form = | f : String| {
993
Request:: post( " /register" ) .header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) .body ( Body:: from( f) ) .unwrap ( )
994
} ;
995
let res = oxygenhub:: router( h.app.clone ( ) )
c8684d44 Andy McCall 21 days ago
996
.oneshot ( form ( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" .into ( ) ) )
10708ab8 Andy McCall 1 month ago ↶
997
.await
998
.unwrap ( ) ;
999
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1000
let with_cookie = | method : & str , path : & str , body : Option< String> | {
1001
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
1002
if body.is_some ( ) {
1003
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
1004
}
1005
req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( )
1006
} ;
1007
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
1008
db:: releases:: create( & h.app.db, row.id, " v1" , " One" , " " , false , None ) .await.unwrap ( ) ;
1009
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( with_cookie ( " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None ) ) .await.unwrap ( ) ;
1010
let page = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
1011
let attach = format! ( " /andy/demo/flow/{} /artifacts/build/tool/attach" , run.number) ;
1012
assert! ( page.contains ( & attach) , " a writer is offered the release" ) ;
1013
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
1014
let csrf = page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1015
let release_id = db:: releases:: list( & h.app.db, row.id) .await.unwrap ( ) [ 0 ] .id;
1016
let res = oxygenhub:: router( h.app.clone ( ) )
1017
.oneshot ( with_cookie ( " POST" , & attach, Some ( format! ( " _csrf={csrf} &release={release_id} " ) ) ) )
1018
.await
1019
.unwrap ( ) ;
1020
assert_eq! ( res.status ( ) , StatusCode:: SEE_OTHER ) ;
1021
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/releases/tag/v1" , None , None ) .await;
1022
assert! (
1023
page.contains ( " >tool</a>" ) & & page.contains ( & format! ( " built by <a href=\" /andy/demo/flow/{0} \" >run #{0} </a> build" , run.number) ) ,
1024
" {page}"
1025
) ;
1026
let ( _ , api) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/releases" , None , None ) .await;
1027
let api: serde_json:: Value = serde_json:: from_str( & api) .unwrap ( ) ;
1028
let asset = & api[ 0 ] [ " assets" ] [ 0 ] ;
1029
assert_eq! (
1030
( asset[ " name" ] .as_str ( ) , asset[ " built_by" ] [ " run" ] .as_i64 ( ) , asset[ " built_by" ] [ " job" ] .as_str ( ) ) ,
1031
( Some ( " tool" ) , Some ( run.number) , Some ( " build" ) )
1032
) ;
1033
assert! ( asset[ " built_by" ] [ " receipt" ] .is_null ( ) , " that receipt was not signed" ) ;
a3c8288b Andy McCall 1 month ago ↶
1034
}
df940d75 Andy McCall 1 month ago ↶
1035
d29070fa Andy McCall 1 month ago ↶
1036
1037
a36bab6b Andy McCall 27 days ago ↶
1038
async fn upload_report_file ( app : & Shared, job : i64 , name : & str , path : & str , bytes : & [u8 ]) -> ( StatusCode, String) {
1039
let req = Request:: post( format! ( " /api/v1/flow/jobs/{job} /reports/{name} /files" ) )
1040
.header ( header:: AUTHORIZATION , " Bearer runner-secret" )
1041
.header ( header:: CONTENT_TYPE , " application/octet-stream" )
1042
.header ( " X-Flow-Report-Path" , path)
1043
.body ( Body:: from( bytes.to_vec ( ) ) )
1044
.unwrap ( ) ;
1045
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
1046
let status = res.status ( ) ;
1047
( status, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
1048
}
1049
1050
1051
1052
1053
# [ tokio ::test ( flavor = " multi_thread" ) ]
1054
async fn reports_are_kept_file_by_file_and_indexed ( ) {
1055
let h = hub ( ) .await;
1056
create ( & h.app, " andy" , " demo" ) .await;
1057
let url = format! ( " {} /andy/demo" , h.base) ;
1058
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1059
let c = client ( & url, work.path ( ) ) ;
1060
write (
1061
work.path ( ) ,
1062
" .flow/pipeline.kdl" ,
1063
& format! (
1064
" 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 "
1065
) ,
1066
) ;
1067
c.snapshot ( " test" ) .unwrap ( ) ;
1068
c.commit ( " Measure frames" , & [ ] ) .unwrap ( ) ;
1069
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1070
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
1071
let a = claim ( & h.app) .await.unwrap ( ) ;
1072
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1073
1074
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 " ;
1075
let page = b " <p>frames</p>" .to_vec ( ) ;
1076
let data: Vec< u8 > = ( 0 .. 100_000 u32 ) .map ( | i | ( i % 241 ) as u8 ) .collect ( ) ;
c8df33a6 Andy McCall 26 days ago ↶
1077
let ( page_bytes, data_bytes) = ( page.clone ( ) , data.clone ( ) ) ;
a36bab6b Andy McCall 27 days ago ↶
1078
let files: [ ( & str , & [ u8 ] ) ; 3 ] = [ ( " data/frames.csv" , & data) , ( " index.html" , & page) , ( " report.toml" , toml) ] ;
1079
1080
assert_eq! ( upload_report_file ( & h.app, id, " tool" , " report.toml" , toml) .await.0 , StatusCode:: BAD_REQUEST , " not a report" ) ;
1081
assert_eq! ( upload_report_file ( & h.app, id, " frame-timing" , " ../x" , toml) .await.0 , StatusCode:: BAD_REQUEST , " not a clean path" ) ;
1082
let mut listed = vec! [ ] ;
1083
for ( path, bytes) in files {
1084
let ( st, text) = upload_report_file ( & h.app, id, " frame-timing" , path, bytes) .await;
1085
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
1086
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1087
assert_eq! ( blob, oxy_core:: repo:: Repo:: blob_ref( bytes) .to_hex ( ) ) ;
1088
listed.push ( serde_json:: json! ( { " path" : path, " blob" : blob, " size" : bytes.len ( ) } ) ) ;
1089
}
1090
let mut missing = listed.clone ( ) ;
1091
missing.push ( serde_json:: json! ( { " path" : " assets/x.js" , " blob" : " ab" .repeat ( 32 ) , " size" : 1 } ) ) ;
1092
let ( st, text) =
1093
upload ( & h.app, id, " frame-timing" , & serde_json:: to_vec( & serde_json:: json! ( { " version" : 1 , " files" : missing } ) ) .unwrap ( ) ) .await;
1094
assert_eq! ( st, StatusCode:: BAD_REQUEST , " a file never uploaded: {text}" ) ;
1095
assert! ( text.contains ( " assets/x.js" ) , " {text}" ) ;
1096
let manifest = serde_json:: to_vec( & serde_json:: json! ( { " version" : 1 , " files" : listed } ) ) .unwrap ( ) ;
1097
let ( st, text) = upload ( & h.app, id, " frame-timing" , & manifest) .await;
1098
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
1099
1100
let ( kind, title, summary, files_n, bytes) : ( String , String , String , i64 , i64 ) =
3b1bc27f Andy McCall 21 days ago
1101
sqlx:: query_as( " SELECT kind, title, summary, files, bytes FROM reports WHERE job_id = $1 AND name = 'frame-timing'" )
a36bab6b Andy McCall 27 days ago ↶
1102
.bind ( id)
1103
.fetch_one ( & h.app.db)
1104
.await
1105
.unwrap ( ) ;
1106
assert_eq! ( ( kind.as_str ( ) , title.as_str ( ) , summary.as_str ( ) ) , ( " performance" , " Frame timing" , " Median 16.4 ms" ) ) ;
1107
assert_eq! ( ( files_n, bytes) , ( 3 , ( data.len ( ) + page.len ( ) + toml.len ( ) ) as i64 ) ) ;
3b1bc27f Andy McCall 21 days ago
1108
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 27 days ago ↶
1109
let m = oxygenhub:: reports:: metrics( & h.app.db, rid) .await.unwrap ( ) ;
1110
assert_eq! ( ( m[ 0 ] .key.as_str ( ) , m[ 0 ] .value, m[ 0 ] .fail_above) , ( " median_ms" , 16. 4 , Some ( 20. 0 ) ) ) ;
1111
let log = db:: flow:: log( & h.app.db, id, 0 , 1000 ) .await.unwrap ( ) ;
1112
assert! ( log.iter ( ) .any ( | r | r.text.contains ( " report frame-timing: performance \" Frame timing\" , 1 metric" ) ) , " {log:?}" ) ;
c8df33a6 Andy McCall 26 days ago ↶
1113
1114
1115
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
1116
let c = checks.iter ( ) .find ( | c | c.name = = " report/frame-timing/median_ms" ) .expect ( " a check per threshold" ) ;
1117
assert_eq! ( c.status, " passed" , " {c:?}" ) ;
1118
assert! ( c.summary.contains ( " 16.4 ms" ) & & c.url.contains ( " /reports/frame-timing?id=" ) , " {c:?}" ) ;
1119
1120
1121
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo" , None , None ) .await;
1122
assert! ( page.contains ( " >Reports</a>" ) , " the tab" ) ;
1123
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/reports" , None , None ) .await;
1124
assert! ( st = = StatusCode:: OK & & page.contains ( " Frame timing" ) & & page.contains ( " median_ms" ) , " {st}" ) ;
1125
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/reports/frame-timing" , None , None ) .await;
1126
assert! ( st = = StatusCode:: OK & & page.contains ( " fails above 20" ) & & page.contains ( " sandbox=\" allow-scripts\" " ) , " {st}" ) ;
1127
let src = page.split ( " class=\" rep-frame\" src=\" " ) .nth ( 1 ) .unwrap ( ) .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1128
assert! ( src.ends_with ( " /index.html" ) , " {src}" ) ;
1129
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( Request:: get( & src) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
1130
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
1131
let csp = res.headers ( ) [ header:: CONTENT_SECURITY_POLICY ] .to_str ( ) .unwrap ( ) .to_string ( ) ;
1132
assert! ( csp.starts_with ( " sandbox allow-scripts;" ) & & csp.contains ( " default-src 'none'" ) , " {csp}" ) ;
d74ce22f Andy McCall 22 days ago ↶
1133
1134
assert! (
1135
res.headers ( ) .get ( header:: X_FRAME_OPTIONS ) .is_none ( ) & & res.headers ( ) .get ( header:: CONTENT_SECURITY_POLICY_REPORT_ONLY ) .is_none ( )
1136
) ;
1137
assert_eq! ( res.headers ( ) [ header:: X_CONTENT_TYPE_OPTIONS ] , " nosniff" ) ;
c8df33a6 Andy McCall 26 days ago ↶
1138
assert_eq! ( res.headers ( ) [ header:: ACCESS_CONTROL_ALLOW_ORIGIN ] , " *" ) ;
f3e5e2bc Andy McCall 26 days ago ↶
1139
1140
let served = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ;
1141
assert! ( served.starts_with ( & page_bytes) , " the report's page" ) ;
1142
assert! ( String :: from_utf8_lossy( & served) .contains ( " /static/report-frame.js" ) , " with the frame script" ) ;
1143
let ( st, _ ) = get_bytes ( & h.app, " /static/report.css" ) .await;
1144
assert_eq! ( st, StatusCode:: OK , " the stylesheet a report may link" ) ;
c8df33a6 Andy McCall 26 days ago ↶
1145
let data = src.replace ( " /index.html" , " /data/frames.csv" ) ;
1146
assert_eq! ( get_bytes ( & h.app, & data) .await, ( StatusCode:: OK , data_bytes.clone ( ) ) ) ;
1147
95b525b0 Andy McCall 26 days ago ↶
1148
1149
1150
let first: i64 = sqlx:: query_scalar( " SELECT id FROM reports WHERE name = 'frame-timing'" ) .fetch_one ( & h.app.db) .await.unwrap ( ) ;
1151
let second: i64 = sqlx:: query_scalar(
1152
" 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 21 days ago
1153
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 26 days ago ↶
1154
)
1155
.bind ( first)
1156
.fetch_one ( & h.app.db)
1157
.await
1158
.unwrap ( ) ;
1159
sqlx:: query(
3b1bc27f Andy McCall 21 days ago
1160
" INSERT INTO report_metrics (report_id, key, value, unit, better, fail_above) VALUES ($1, 'median_ms', 18.0, 'ms', 'lower', 20)" ,
95b525b0 Andy McCall 26 days ago ↶
1161
)
1162
.bind ( second)
1163
.execute ( & h.app.db)
1164
.await
1165
.unwrap ( ) ;
1166
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/reports/frame-timing" , None , None ) .await;
1167
assert_eq! ( page.matches ( " data-tl-stop" ) .count ( ) , 2 , " a stop per run" ) ;
1168
assert! ( page.contains ( " Median 18.0 ms" ) , " the newest run is shown" ) ;
1169
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/reports/frame-timing?id={first} &compare={second} &play=3" ) , None , None ) .await;
1170
assert! ( page.contains ( " This run against run #2" ) & & page.contains ( " -1.6" ) , " every metric against the other run" ) ;
1171
let stop = page.split ( " data-tl-stop" ) .next ( ) .unwrap_or ( " " ) .rsplit ( " href=" ) .next ( ) .unwrap_or ( " " ) .to_string ( ) ;
1172
assert! ( page.contains ( " data-play=\" 3\" " ) , " playing" ) ;
1173
assert! ( stop.contains ( " play" ) & & stop.contains ( ' 3' ) , " playing carries on from stop to stop: {stop}" ) ;
1174
assert! ( page.contains ( " data-report-runs=" ) , " the frame interface knows the runs" ) ;
c8df33a6 Andy McCall 26 days ago ↶
1175
let forged = src.replacen ( " /files/" , " /files/9" , 1 ) ;
1176
assert_eq! ( get_bytes ( & h.app, & forged) .await.0 , StatusCode:: NOT_FOUND ) ;
1177
assert_eq! ( get_bytes ( & h.app, & src.replace ( " /index.html" , " /secret.txt" ) ) .await.0 , StatusCode:: NOT_FOUND ) ;
a36bab6b Andy McCall 27 days ago ↶
1178
let _ = run;
1179
1180
1181
{
1182
let name = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
1183
let mut hosted = oxy_core:: repo:: Repo:: open( & h.app.repos.dir ( & name) ) .unwrap ( ) ;
1184
hosted.config.set ( " core.gc-grace" , " 0" ) .unwrap ( ) ;
1185
hosted.save_config ( ) .unwrap ( ) ;
1186
}
1187
let done = oxygenhub:: maintenance:: gc_all( & h.app) .await;
1188
assert! ( done.iter ( ) .all ( | d | ! d.failed) , " {done:?}" ) ;
1189
let hosted = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
1190
for ( path, bytes) in files {
1191
assert_eq! ( hosted.read_blob ( & oxy_core:: repo:: Repo:: blob_ref( bytes) ) .unwrap ( ) , bytes, " {path} survives gc" ) ;
1192
}
1193
}
1194
d29070fa Andy McCall 1 month ago ↶
1195
# [ tokio ::test ( flavor = " multi_thread" ) ]
1196
async fn a_failing_job_keeps_an_artifact_outside_its_receipt ( ) {
1197
let h = hub ( ) .await;
1198
create ( & h.app, " andy" , " demo" ) .await;
1199
let url = format! ( " {} /andy/demo" , h.base) ;
1200
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1201
let c = client ( & url, work.path ( ) ) ;
1202
write (
1203
work.path ( ) ,
1204
" .flow/pipeline.kdl" ,
1205
& format! (
336c953a Andy McCall 1 month ago ↶
1206
" 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 ↶
1207
) ,
1208
) ;
1209
c.snapshot ( " test" ) .unwrap ( ) ;
1210
c.commit ( " Test" , & [ ] ) .unwrap ( ) ;
1211
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1212
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
1213
let a = claim ( & h.app) .await.unwrap ( ) ;
336c953a Andy McCall 1 month ago ↶
1214
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 ↶
1215
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
0b203bbf Andy McCall 1 month ago ↶
1216
1217
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>"# ;
1218
let ( st, text) = upload ( & h.app, id, " junit" , xml) .await;
d29070fa Andy McCall 1 month ago ↶
1219
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
1220
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1221
let mut r = receipt ( " test" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 1 ) ;
1222
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
1223
assert_eq! (
1224
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 1 , " duration_ms" : 700 , " receipt" : r.clone ( ) } ) ) .await,
1225
StatusCode:: BAD_REQUEST ,
1226
" a failing receipt names no outputs"
1227
) ;
1228
r[ " outputs" ] = serde_json:: json! ( [ ] ) ;
04683947 Andy McCall 11 days ago ↶
1229
1230
let phases = serde_json:: json! ( { " image" : 9000 , " prepare" : 1200 , " run" : 3400 , " finish" : 500 } ) ;
1231
assert_eq! (
1232
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 1 , " duration_ms" : 700 , " receipt" : r, " phases" : phases } ) ) .await,
1233
StatusCode:: NO_CONTENT
1234
) ;
d29070fa Andy McCall 1 month ago ↶
1235
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
04683947 Andy McCall 11 days ago ↶
1236
let at = page.find ( " Where its time went: " ) .expect ( " the job's phases" ) ;
1237
let line = & page[ at.. at + 120 ] ;
1238
let order: Vec< usize > = [ " image" , " prepare" , " run" , " finish" ] .iter ( ) .map ( | p | line.find ( p) .expect ( p) ) .collect ( ) ;
1239
assert! ( order.windows ( 2 ) .all ( | w | w[ 0 ] < w[ 1 ] ) , " in the order they happened: {line}" ) ;
d29070fa Andy McCall 1 month ago ↶
1240
assert! ( page.contains ( & format! ( " /andy/demo/flow/{} /artifacts/test/junit" , run.number) ) , " listed" ) ;
1241
assert! ( page.contains ( " not in its receipt" ) , " marked" ) ;
0b203bbf Andy McCall 1 month ago ↶
1242
assert! ( page.contains ( " 2 tests: 1 failed" ) , " counts beside the job" ) ;
1243
assert! ( page.contains ( " tests.test_io::test_write" ) & & page.contains ( " disk full" ) & & page.contains ( " OSError" ) , " the failure" ) ;
1244
assert! ( page.contains ( " tests: 2 tests: 1 failed in 2.0 s" ) , " said in the job's log" ) ;
d29070fa Andy McCall 1 month ago ↶
1245
}
1246
df940d75 Andy McCall 1 month ago ↶
1247
1248
1249
1250
# [ tokio ::test ( flavor = " multi_thread" ) ]
1251
async fn a_release_is_audited_from_authors_to_receipts ( ) {
1252
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectType} ;
1253
use oxy_core:: identity:: SecretKey;
1254
use oxy_core:: objects:: StateObject;
1255
use oxy_core:: receipt:: SignedReceipt;
1256
let h = hub ( ) .await;
1257
create ( & h.app, " andy" , " demo" ) .await;
1258
let url = format! ( " {} /andy/demo" , h.base) ;
1259
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1260
let mut c = client ( & url, work.path ( ) ) ;
1261
c.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
1262
write (
1263
work.path ( ) ,
1264
" .flow/pipeline.kdl" ,
1265
& format! (
1266
" 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 "
1267
) ,
1268
) ;
1269
c.snapshot ( " test" ) .unwrap ( ) ;
1270
c.commit ( " Build a tool" , & [ ] ) .unwrap ( ) ;
1271
let heads = c.bookmark_heads ( ) .unwrap ( ) ;
1272
c.tag_create ( " v1" , & heads, " first" ) .unwrap ( ) ;
1273
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1274
1275
1276
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
1277
let a = claim ( & h.app) .await.unwrap ( ) ;
1278
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1279
let body: Vec< u8 > = b " #!/bin/sh\n echo tool\n " .to_vec ( ) ;
1280
let ( _ , text) = upload ( & h.app, id, " tool" , & body) .await;
1281
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1282
let mut r = receipt ( " build" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
1283
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
1284
let heads_run: Vec< ChangeHash> = run.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
1285
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads_run) .encode ( ) ) ;
1286
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
1287
let sk = SecretKey:: generate( ) ;
1288
let runner = sk.identity ( " test-1 (OxygenFlow runner)" , " " , 1 ) ;
1289
let fr: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
1290
let signed = hex:: encode( SignedReceipt:: sign( fr.to_oxy ( ) , repo_id, state, & format! ( " {url} /flow/1" ) , & runner, & sk) .encode ( ) ) ;
1291
let done = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 900 , " receipt" : r, " signed_receipt" : signed } ) ;
1292
assert_eq! ( finish ( & h.app, id, done) .await, StatusCode:: NO_CONTENT ) ;
1293
1294
1295
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
1296
let rel = db:: releases:: create( & h.app.db, row.id, " v1" , " One" , " " , false , None ) .await.unwrap ( ) ;
1297
oxygenhub:: web:: releases:: store_asset( & h.app, rel.id, " tool" , " " , & body) .await.unwrap ( ) ;
1298
oxygenhub:: web:: releases:: store_asset( & h.app, rel.id, " notes.txt" , " " , b " by hand\n " ) .await.unwrap ( ) ;
1299
1300
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/releases/trust/v1" , None , None ) .await;
1301
assert_eq! ( st, StatusCode:: OK ) ;
1302
for want in [
1303
" Tag v1 is signed" ,
1304
" The one change in the release is authenticated" ,
1305
" tool: signed receipt from run #1 build" ,
1306
" a runner key of this hub" ,
1307
" tool: built from the tagged state" ,
1308
" tool: pinned environment" ,
1309
" notes.txt: no build found" ,
1310
" <svg class=\" trust-graph\" " ,
bf5101d3 Andy McCall 1 month ago ↶
1311
1312
1313
" Tag signed by" ,
1314
" 1 of 1 changes signed or sealed" ,
1315
" 1 of 2 assets built and attested by OxygenFlow" ,
1316
" attested build" ,
1317
" uploaded by hand" ,
1318
" receipt verified" ,
1319
" class=\" shackle\" " ,
1320
" <title>notes.txt:" ,
df940d75 Andy McCall 1 month ago ↶
1321
] {
1322
assert! ( page.contains ( want) , " missing {want:?}" ) ;
1323
}
bf5101d3 Andy McCall 1 month ago ↶
1324
let svg = & page[ page.find ( " <svg class=\" trust-graph\" " ) .unwrap ( ) .. ] ;
1325
assert! ( svg[ .. 200 ] .contains ( " width=\" " ) & & svg[ .. 200 ] .contains ( " height=\" " ) , " the graph has its own size: {}" , & svg[ .. 200 ] ) ;
df940d75 Andy McCall 1 month ago ↶
1326
let ( _ , releases) = call ( & h.app, " GET" , " /andy/demo/releases" , None , None ) .await;
1327
assert! ( releases.contains ( " /andy/demo/releases/trust/v1" ) , " the release links its audit" ) ;
1328
1329
1330
let ( st, json) = call ( & h.app, " GET" , " /andy/demo/releases/trust/v1?format=json" , None , None ) .await;
1331
assert_eq! ( st, StatusCode:: OK ) ;
1332
let t: serde_json:: Value = serde_json:: from_str( & json) .unwrap ( ) ;
1333
assert_eq! ( ( t[ " tag" ] .as_str ( ) , t[ " tag_signature" ] .as_str ( ) , t[ " changes" ] .as_i64 ( ) ) , ( Some ( " v1" ) , Some ( " valid" ) , Some ( 1 ) ) ) ;
1334
assert_eq! ( t[ " authors" ] [ 0 ] [ " who" ] [ " name" ] , " Andy" ) ;
1335
let tool = t[ " assets" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | a | a[ " name" ] = = " tool" ) .unwrap ( ) ;
1336
let hex_receipt = tool[ " build" ] [ " signed_receipt" ] .as_str ( ) .unwrap ( ) ;
1337
let sr = SignedReceipt:: decode( & hex:: decode( hex_receipt) .unwrap ( ) ) .unwrap ( ) ;
1338
sr.verify ( ) .unwrap ( ) ;
1339
assert_eq! ( sr.receipt.outputs[ 0 ] .to_hex ( ) , blob) ;
1340
let audit = t[ " audit" ] .as_array ( ) .unwrap ( ) ;
1341
assert! ( audit.iter ( ) .any ( | c | c[ " verdict" ] = = " warn" & & c[ " title" ] .as_str ( ) .unwrap ( ) .starts_with ( " notes.txt" ) ) ) ;
1342
assert! ( ! audit.iter ( ) .any ( | c | c[ " verdict" ] = = " fail" ) , " {audit:?}" ) ;
1343
1344
let ( st, api) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/releases/trust/v1" , None , None ) .await;
1345
assert_eq! ( st, StatusCode:: OK ) ;
1346
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & api) .unwrap ( ) [ " assets" ] .as_array ( ) .unwrap ( ) .len ( ) , 2 ) ;
1347
assert_eq! ( call ( & h.app, " GET" , " /api/v1/repos/andy/demo/releases/trust/v9" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
1348
assert_eq! ( call ( & h.app, " GET" , " /andy/demo/releases/trust/v9" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
1349
}
b6b7e4cf Andy McCall 1 month ago ↶
1350
1351
1352
1353
1354
# [ tokio ::test ( flavor = " multi_thread" ) ]
1355
async fn runners_register_show_their_history_and_are_drained_and_removed ( ) {
1356
use oxy_core:: identity:: SecretKey;
1357
let h = hub ( ) .await;
1358
create ( & h.app, " andy" , " demo" ) .await;
1359
1360
let res = oxygenhub:: router( h.app.clone ( ) )
1361
.oneshot (
1362
Request:: post( " /register" )
1363
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 21 days ago
1364
.body ( Body:: from( " username=carol&email=carol%40example.com&password=correct-horse-battery&setup=test-setup" ) )
b6b7e4cf Andy McCall 1 month ago ↶
1365
.unwrap ( ) ,
1366
)
1367
.await
1368
.unwrap ( ) ;
1369
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1370
let admin = db:: accounts:: account_by_name( & h.app.db, " carol" ) .await.unwrap ( ) .unwrap ( ) ;
1371
db:: accounts:: set_admin( & h.app.db, admin.id, true ) .await.unwrap ( ) ;
1372
let web = | method : & 'static str , path : String, body : Option< String> | {
1373
let ( app, cookie) = ( h.app.clone ( ) , cookie.clone ( ) ) ;
1374
async move {
1375
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie) ;
1376
if body.is_some ( ) {
1377
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
1378
}
1379
let res = oxygenhub:: router( app) .oneshot ( req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ) .await.unwrap ( ) ;
1380
let st = res.status ( ) ;
1381
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
1382
}
1383
} ;
1384
let csrf_of = | page : & str | {
1385
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
1386
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
1387
} ;
1388
let ( st, page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1389
assert_eq! ( st, StatusCode:: OK ) ;
1390
assert! ( page.contains ( " No runners yet" ) ) ;
1391
assert_eq! ( call ( & h.app, " GET" , " /admin/runners" , None , None ) .await.0 , StatusCode:: NOT_FOUND , " administrators only" ) ;
1392
1393
1394
let ( _ , page) = web ( " POST" , " /admin/runners/token" .into ( ) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1395
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
1396
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
1397
assert! ( token.starts_with ( " flowreg_" ) , " {token}" ) ;
1398
1399
1400
let sk = SecretKey:: generate( ) ;
1401
let identity = sk.identity ( " plex (OxygenFlow runner)" , " " , 1 ) ;
1402
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 } ) ;
1403
let reg = serde_json:: json! ( { " token" : token, " name" : " plex" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1404
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1405
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1406
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1407
assert! ( credential.starts_with ( " flowrunner_" ) ) ;
1408
assert_eq! (
1409
call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await.0 ,
1410
StatusCode:: FORBIDDEN ,
1411
" used once"
1412
) ;
1413
1414
1415
let claim_as = | who : String| {
1416
let app = h.app.clone ( ) ;
1417
let info = info.clone ( ) ;
1418
async move {
1419
let body = serde_json:: json! ( { " runner" : " ignored" , " runtimes" : [ " microvm" ] , " info" : info } ) .to_string ( ) ;
1420
call ( & app, " POST" , " /api/v1/flow/claim" , Some ( & who) , Some ( body) ) .await
1421
}
1422
} ;
1423
assert_eq! ( claim_as ( credential.clone ( ) ) .await.0 , StatusCode:: NO_CONTENT , " nothing queued" ) ;
1424
assert_eq! ( claim ( & h.app) .await, None ) ;
1425
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1426
assert! (
1427
page.contains ( " >plex</strong>" ) & & page.contains ( " st-online" ) & & page.contains ( " x86_64 · linux" ) & & page.contains ( " big-disk" ) ,
1428
" {page}"
1429
) ;
1430
assert! ( page.contains ( " >test-1</strong>" ) & & page.contains ( " hub.toml" ) , " the configured runner, by its claimed name" ) ;
1431
let plex = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " plex" ) .unwrap ( ) ;
1432
assert_eq! ( plex.key, identity.hash ( ) .to_hex ( ) , " bound at registration" ) ;
1433
1434
1435
let url = format! ( " {} /andy/demo" , h.base) ;
1436
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1437
let c = client ( & url, work.path ( ) ) ;
1438
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1439
c.snapshot ( " test" ) .unwrap ( ) ;
1440
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1441
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1442
runs_of ( & h.app, " andy" , " demo" ) .await;
1443
let ( st, body) = claim_as ( credential.clone ( ) ) .await;
1444
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1445
let job: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1ec29691 Andy McCall 10 days ago ↶
1446
1447
let kept: String =
1448
sqlx:: query_scalar( " SELECT runner_version FROM flow_jobs WHERE runner_id = $1" ) .bind ( plex.id) .fetch_one ( & h.app.db) .await.unwrap ( ) ;
1449
assert_eq! ( kept, " 0.1.0" ) ;
b6b7e4cf Andy McCall 1 month ago ↶
1450
let ( _ , page) = web ( " GET" , format! ( " /admin/runners/{} " , plex.id) , None ) .await;
1451
assert! (
1452
page.contains ( " andy/demo" ) & & page.contains ( job[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) ) & & page.contains ( " Running now</dt><dd>1 job" ) ,
1453
" {page}"
1454
) ;
b66e70b7 Andy McCall 1 month ago ↶
1455
1456
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
e9b9619c Andy McCall 21 days ago ↶
1457
assert! ( page.contains ( " id=\" queue\" " ) & & page.contains ( " >plex</a></td>" ) , " {page}" ) ;
1458
1459
1460
assert! ( page.contains ( " running 1 of " ) & & page.contains ( " /admin/runners?runner=plex#queue" ) , " {page}" ) ;
1461
let ( _ , only) = web ( " GET" , " /admin/runners?runner=plex" .into ( ) , None ) .await;
1462
assert! ( only.contains ( " >plex</a></td>" ) & & only.contains ( " of " ) & & only.contains ( " >all</a>" ) , " {only}" ) ;
1463
let ( _ , none) = web ( " GET" , " /admin/runners?runner=nobody&status=running" .into ( ) , None ) .await;
1464
assert! ( none.contains ( " Nothing matches." ) , " {none}" ) ;
1465
1466
sqlx:: query( " UPDATE flow_runners SET last_seen_ms = 0 WHERE id = $1" ) .bind ( plex.id) .execute ( & h.app.db) .await.unwrap ( ) ;
1467
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1468
assert! ( page.contains ( " st-offline" ) , " quiet for long: offline" ) ;
1469
assert_eq! ( call ( & h.app, " POST" , " /api/v1/flow/runners/heartbeat" , Some ( & credential) , None ) .await.0 , StatusCode:: NO_CONTENT ) ;
1470
assert_eq! ( call ( & h.app, " POST" , " /api/v1/flow/runners/heartbeat" , Some ( " flowrunner_nope" ) , None ) .await.0 , StatusCode:: UNAUTHORIZED ) ;
1471
let plex_now = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " plex" ) .unwrap ( ) ;
1472
assert_eq! ( plex_now.connectivity ( ) , " online" ) ;
b66e70b7 Andy McCall 1 month ago ↶
1473
let ( _ , tokens) = web ( " GET" , " /settings/tokens" .into ( ) , None ) .await;
1474
let ( _ , made) = web ( " POST" , " /settings/tokens" .into ( ) , Some ( format! ( " _csrf={} &name=admin&scopes=user%3Aread" , csrf_of ( & tokens) ) ) ) .await;
1475
let i = made.find ( " oxyhub_" ) .unwrap ( ) ;
1476
let admin_token: String = made[ i.. ] .chars ( ) .take_while ( | c | c.is_ascii_alphanumeric ( ) | | * c = = ' _' ) .collect ( ) ;
1477
let ( st, body) = call ( & h.app, " GET" , " /api/v1/runners" , Some ( & admin_token) , None ) .await;
1478
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1479
let listed: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1480
let plex_row = listed.as_array ( ) .unwrap ( ) .iter ( ) .find ( | r | r[ " name" ] = = " plex" ) .unwrap ( ) ;
1481
assert_eq! ( ( plex_row[ " running" ] .as_i64 ( ) , plex_row[ " arch" ] .as_str ( ) ) , ( Some ( 1 ) , Some ( " x86_64" ) ) ) ;
7f5f6c05 Andy McCall 1 month ago ↶
1482
1483
let ( _ , page) = web ( " GET" , format! ( " /admin/runners/{} " , plex.id) , None ) .await;
1484
let ( st, _ ) = web (
1485
" POST" ,
1486
format! ( " /admin/runners/{} /limits" , plex.id) ,
1487
Some ( format! ( " _csrf={} &labels=gpu%2C+plex&timeout_minutes=5" , csrf_of ( & page) ) ) ,
1488
)
1489
.await;
1490
assert_eq! ( st, StatusCode:: SEE_OTHER ) ;
1491
let ( _ , page) = web ( " GET" , format! ( " /admin/runners/{} " , plex.id) , None ) .await;
1492
assert! ( page.contains ( " >gpu<" ) & & page.contains ( " 5 min for its jobs" ) , " {page}" ) ;
1493
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/runners" , Some ( & admin_token) , None ) .await;
1494
let listed: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1495
let plex_row = listed.as_array ( ) .unwrap ( ) .iter ( ) .find ( | r | r[ " name" ] = = " plex" ) .unwrap ( ) ;
1496
assert! ( plex_row[ " labels" ] .as_array ( ) .unwrap ( ) .iter ( ) .any ( | l | l = = " gpu" ) , " {plex_row}" ) ;
1497
let ( st, _ ) = web (
1498
" POST" ,
1499
format! ( " /admin/runners/{} /limits" , plex.id) ,
1500
Some ( format! ( " _csrf={} &labels=bad%20label%21&timeout_minutes=" , csrf_of ( & page) ) ) ,
1501
)
1502
.await;
1503
assert_eq! ( st, StatusCode:: BAD_REQUEST , " a label with a space or punctuation is refused" ) ;
b66e70b7 Andy McCall 1 month ago ↶
1504
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/runners/queue" , Some ( & admin_token) , None ) .await;
1505
let queue: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1506
assert! ( queue.as_array ( ) .unwrap ( ) .iter ( ) .any ( | q | q[ " status" ] = = " running" & & q[ " runner" ] = = " plex" ) , " {queue}" ) ;
1507
let ( _ , body) = call ( & h.app, " GET" , & format! ( " /api/v1/runners/{} /jobs" , plex.id) , Some ( & admin_token) , None ) .await;
1508
let jobs: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1509
assert_eq! ( jobs[ 0 ] [ " repo" ] , " andy/demo" , " {jobs}" ) ;
1510
assert_eq! ( call ( & h.app, " GET" , " /api/v1/runners" , None , None ) .await.0 , StatusCode:: NOT_FOUND , " administrators only" ) ;
b6b7e4cf Andy McCall 1 month ago ↶
1511
1512
1513
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1514
let csrf = csrf_of ( & page) ;
1515
web ( " POST" , format! ( " /admin/runners/{} /state" , plex.id) , Some ( format! ( " _csrf={csrf} &state=draining" ) ) ) .await;
1516
assert_eq! ( claim_as ( credential.clone ( ) ) .await.0 , StatusCode:: NO_CONTENT , " a draining runner takes nothing" ) ;
1517
let ( _ , page) = web ( " POST" , format! ( " /admin/runners/{} /remove" , plex.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1518
assert! ( page.contains ( " is running jobs: drain it first" ) , " {page}" ) ;
1519
1520
let id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1521
let done = call (
1522
& h.app,
1523
" POST" ,
1524
& format! ( " /api/v1/flow/jobs/{id} /finish" ) ,
1525
Some ( & credential) ,
1526
Some ( r # "{"exit_code":0,"duration_ms":5}"# .into ( ) ) ,
1527
)
1528
.await;
1529
assert_eq! ( done.0 , StatusCode:: NO_CONTENT ) ;
1530
web ( " POST" , format! ( " /admin/runners/{} /state" , plex.id) , Some ( format! ( " _csrf={csrf} &state=disabled" ) ) ) .await;
1531
assert_eq! ( claim_as ( credential.clone ( ) ) .await.0 , StatusCode:: FORBIDDEN ) ;
1532
let ( _ , page) = web ( " POST" , format! ( " /admin/runners/{} /remove" , plex.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1533
assert! ( page.contains ( " Removed plex." ) , " {page}" ) ;
1534
assert_eq! ( claim_as ( credential) .await.0 , StatusCode:: UNAUTHORIZED ) ;
1535
}
74be9796 Andy McCall 1 month ago ↶
1536
1537
1538
1539
# [ tokio ::test ( flavor = " multi_thread" ) ]
1540
async fn scoped_runners_take_only_their_scope ( ) {
1541
use oxy_core:: identity:: SecretKey;
1542
let h = hub ( ) .await;
1543
create ( & h.app, " andy" , " demo" ) .await;
1544
create ( & h.app, " andy" , " other" ) .await;
1545
let register = | scope : db:: runners:: Scope, name : & 'static str | {
1546
let app = h.app.clone ( ) ;
1547
async move {
1548
let token = format! ( " flowreg_{name} " ) ;
1549
db:: runners:: add_registration( & app.db, & blake3:: hash( token.as_bytes ( ) ) .to_hex ( ) , " admin" , & scope) .await.unwrap ( ) ;
1550
let identity = SecretKey:: generate( ) .identity ( name, " " , 1 ) ;
1551
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
1552
let reg = serde_json:: json! ( { " token" : token, " name" : name, " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1553
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1554
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1555
serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( )
1556
}
1557
} ;
1558
let demo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
1559
let only_demo = register ( db:: runners:: Scope { owner: " andy" .into ( ) , repo_id: Some ( demo.id) } , " demo-runner" ) .await;
1560
let carols = register ( db:: runners:: Scope { owner: " carol" .into ( ) , repo_id: None } , " carol-runner" ) .await;
1561
let andys = register ( db:: runners:: Scope { owner: " andy" .into ( ) , repo_id: None } , " andy-runner" ) .await;
1562
let claim_as = | who : String| {
1563
let app = h.app.clone ( ) ;
1564
async move {
1565
let body = serde_json:: json! ( { " runner" : " x" , " runtimes" : [ " microvm" ] } ) .to_string ( ) ;
1566
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( & who) , Some ( body) ) .await;
1567
( st = = StatusCode:: OK ) .then ( | | serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " repo" ] .as_str ( ) .unwrap ( ) .to_string ( ) )
1568
}
1569
} ;
1570
1571
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1572
let c = client ( & format! ( " {} /andy/other" , h.base) , work.path ( ) ) ;
1573
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1574
c.snapshot ( " t" ) .unwrap ( ) ;
1575
c.commit ( " pipeline" , & [ ] ) .unwrap ( ) ;
1576
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1577
assert! ( ! runs_of ( & h.app, " andy" , " other" ) .await.is_empty ( ) ) ;
1578
assert_eq! ( claim_as ( only_demo.clone ( ) ) .await, None , " nothing of andy/demo is queued" ) ;
1579
assert_eq! ( claim_as ( carols) .await, None , " nothing of carol's" ) ;
1580
assert_eq! ( claim_as ( andys) .await.as_deref ( ) , Some ( " andy/other" ) , " the owner's runner takes its repositories' jobs" ) ;
1581
1582
assert_eq! ( claim ( & h.app) .await.unwrap ( ) [ " repo" ] , " andy/other" ) ;
1583
assert_eq! ( claim_as ( only_demo) .await, None , " still nothing of its own" ) ;
1584
}
7974adb0 Andy McCall 1 month ago ↶
1585
5714347a Andy McCall 12 days ago ↶
1586
1587
1588
1589
# [ tokio ::test ( flavor = " multi_thread" ) ]
1590
async fn a_user_registers_a_runner_for_their_own_repositories ( ) {
1591
use oxy_core:: identity:: SecretKey;
1592
let h = hub ( ) .await;
1593
create ( & h.app, " andy" , " demo" ) .await;
1594
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " " , " salt" , false ) .await.unwrap ( ) ;
1595
let pat = " oxyhub_phone_test_token" ;
1596
db:: accounts:: create_token( & h.app.db, andy.id, " phone" , & oxygenhub:: auth:: token_hash( pat) , " repo:read,repo:write" , " " , None )
1597
.await
1598
.unwrap ( ) ;
1599
let read_only = " oxyhub_read_only_token" ;
1600
db:: accounts:: create_token( & h.app.db, andy.id, " ro" , & oxygenhub:: auth:: token_hash( read_only) , " repo:read" , " " , None ) .await.unwrap ( ) ;
1601
assert_eq! ( call ( & h.app, " POST" , " /api/v1/user/runner-token" , Some ( read_only) , None ) .await.0 , StatusCode:: FORBIDDEN ) ;
1602
assert_eq! ( call ( & h.app, " POST" , " /api/v1/user/runner-token" , None , None ) .await.0 , StatusCode:: UNAUTHORIZED ) ;
1603
let ( st, body) = call ( & h.app, " POST" , " /api/v1/user/runner-token" , Some ( pat) , None ) .await;
1604
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
1605
let got: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1606
let token = got[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1607
assert! ( token.starts_with ( " flowreg_" ) & & got[ " owner" ] = = " andy" , " {body}" ) ;
1608
let identity = SecretKey:: generate( ) .identity ( " andy's phone" , " " , 1 ) ;
1609
let info = serde_json:: json! ( { " arch" : " aarch64" , " os" : " android" , " version" : " 0.3.1" , " labels" : [ " android" ] , " max_jobs" : 1 } ) ;
1610
let reg = serde_json:: json! ( { " token" : token, " name" : " pixel" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1611
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1612
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1613
let phone = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " pixel" ) .unwrap ( ) ;
1614
assert_eq! ( ( phone.scope_owner.as_str ( ) , phone.scope_repo_id) , ( " andy" , None ) , " andy's repositories, all of them, and no one else's" ) ;
1615
}
1616
7974adb0 Andy McCall 1 month ago ↶
1617
1618
1619
1620
# [ tokio ::test ( flavor = " multi_thread" ) ]
1621
async fn repository_admins_add_and_remove_their_own_runners ( ) {
1622
use oxy_core:: identity:: SecretKey;
1623
let h = hub ( ) .await;
1624
let res = oxygenhub:: router( h.app.clone ( ) )
1625
.oneshot (
1626
Request:: post( " /register" )
1627
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 21 days ago
1628
.body ( Body:: from( " username=andy&email=andy%40example.com&password=correct-horse-battery&setup=test-setup" ) )
7974adb0 Andy McCall 1 month ago ↶
1629
.unwrap ( ) ,
1630
)
1631
.await
1632
.unwrap ( ) ;
1633
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1634
create ( & h.app, " andy" , " demo" ) .await;
1635
let demo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
1636
let web = | method : & 'static str , path : String, body : Option< String> | {
1637
let ( app, cookie) = ( h.app.clone ( ) , cookie.clone ( ) ) ;
1638
async move {
1639
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie) ;
1640
if body.is_some ( ) {
1641
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
1642
}
1643
let res = oxygenhub:: router( app) .oneshot ( req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ) .await.unwrap ( ) ;
1644
let st = res.status ( ) ;
1645
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
1646
}
1647
} ;
1648
let csrf_of = | page : & str | {
1649
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
1650
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
1651
} ;
c7067c7b Andy McCall 17 days ago
1652
let ( st, page) = web ( " GET" , " /andy/demo/settings/runners" .into ( ) , None ) .await;
7974adb0 Andy McCall 1 month ago ↶
1653
assert_eq! ( st, StatusCode:: OK ) ;
c7067c7b Andy McCall 17 days ago
1654
assert! ( page.contains ( " <h2>Runners</h2>" ) , " the owner sees the page" ) ;
7974adb0 Andy McCall 1 month ago ↶
1655
let ( _ , page) = web ( " POST" , " /andy/demo/settings/runners/token" .into ( ) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1656
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
1657
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
1658
let identity = SecretKey:: generate( ) .identity ( " mine" , " " , 1 ) ;
1659
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
1660
let reg = serde_json:: json! ( { " token" : token, " name" : " mine" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1661
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1662
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1663
let mine = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " mine" ) .unwrap ( ) ;
1664
assert_eq! ( mine.scope ( ) , db:: runners:: Scope { owner: " andy" .into ( ) , repo_id: Some ( demo.id) } ) ;
1665
1666
claim ( & h.app) .await;
1667
let site = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " test-1" ) .unwrap ( ) ;
c7067c7b Andy McCall 17 days ago
1668
let ( _ , page) = web ( " GET" , " /andy/demo/settings/runners" .into ( ) , None ) .await;
7974adb0 Andy McCall 1 month ago ↶
1669
assert! ( page.contains ( " <strong>mine</strong>" ) & & page.contains ( " this repository" ) , " {page}" ) ;
1670
assert! ( page.contains ( " <strong>test-1</strong>" ) & & page.contains ( " the whole site" ) , " {page}" ) ;
1671
let csrf = csrf_of ( & page) ;
1672
let ( st, _ ) = web ( " POST" , format! ( " /andy/demo/settings/runners/{} /remove" , site.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1673
assert_eq! ( st, StatusCode:: NOT_FOUND , " not the repository's own" ) ;
c7067c7b Andy McCall 17 days ago
1674
let ( st, _ ) = web ( " POST" , format! ( " /andy/demo/settings/runners/{} /remove" , mine.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1675
assert_eq! ( st, StatusCode:: SEE_OTHER , " back to the Runners page" ) ;
1676
let ( _ , page) = web ( " GET" , " /andy/demo/settings/runners?notice=x" .into ( ) , None ) .await;
1677
assert! ( ! page.contains ( " <strong>mine</strong>" ) , " {page}" ) ;
7974adb0 Andy McCall 1 month ago ↶
1678
assert! ( db:: runners:: get( & h.app.db, mine.id) .await.unwrap ( ) .is_none ( ) ) ;
1679
1680
1681
let ( _ , page) = web ( " GET" , " /orgs/new" .into ( ) , None ) .await;
1682
web ( " POST" , " /orgs/new" .into ( ) , Some ( format! ( " _csrf={} &name=acme&display_name=Acme" , csrf_of ( & page) ) ) ) .await;
1683
let ( st, page) = web ( " GET" , " /orgs/acme/settings" .into ( ) , None ) .await;
1684
assert_eq! ( st, StatusCode:: OK ) ;
1685
let ( _ , page) = web ( " POST" , " /orgs/acme/settings/runners/token" .into ( ) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1686
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
1687
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
1688
let identity = SecretKey:: generate( ) .identity ( " acme-runner" , " " , 1 ) ;
1689
let reg = serde_json:: json! ( { " token" : token, " name" : " acme-runner" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1690
assert_eq! ( call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await.0 , StatusCode:: OK ) ;
1691
let acme = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " acme-runner" ) .unwrap ( ) ;
1692
assert_eq! ( acme.scope ( ) , db:: runners:: Scope { owner: " acme" .into ( ) , repo_id: None } ) ;
1693
let ( _ , page) = web ( " GET" , " /orgs/acme/settings" .into ( ) , None ) .await;
1694
assert! ( page.contains ( " <strong>acme-runner</strong>" ) & & page.contains ( " the organisation's repositories" ) , " {page}" ) ;
1695
let ( _ , page) = web ( " POST" , format! ( " /orgs/acme/settings/runners/{} /remove" , acme.id) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1696
assert! ( page.contains ( " acme-runner removed" ) , " {page}" ) ;
1697
}
c4c87f48 Andy McCall 1 month ago ↶
1698
1699
1700
1701
# [ tokio ::test ( flavor = " multi_thread" ) ]
1702
async fn jobs_that_ask_for_labels_wait_for_a_runner_with_them ( ) {
1703
use oxy_core:: identity:: SecretKey;
1704
let h = hub ( ) .await;
1705
create ( & h.app, " andy" , " demo" ) .await;
1706
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1707
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
1708
let cross = " job \" cross\" {\n sandbox \" alpine\" \n runs-on \" riscv64\" \n exec \" true\" \n }\n " ;
1709
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( cross) ) ;
1710
c.snapshot ( " t" ) .unwrap ( ) ;
1711
c.commit ( " pipeline" , & [ ] ) .unwrap ( ) ;
1712
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1713
assert! ( ! runs_of ( & h.app, " andy" , " demo" ) .await.is_empty ( ) ) ;
1714
1715
let mut taken = Vec :: new( ) ;
1716
while let Some ( j) = claim ( & h.app) .await {
1717
taken.push ( j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
1718
}
1719
assert! ( ! taken.contains ( & " cross" .to_string ( ) ) & & taken.contains ( & " lint" .to_string ( ) ) , " {taken:?}" ) ;
1720
let queue = db:: flow:: queue( & h.app.db) .await.unwrap ( ) ;
1721
let waiting = queue.iter ( ) .find ( | q | q.job = = " cross" ) .unwrap ( ) ;
1722
assert_eq! ( ( waiting.status.as_str ( ) , waiting.needs ( ) ) , ( " queued" , vec! [ " riscv64" .to_string ( ) ] ) ) ;
1723
1724
let token = " flowreg_riscv" ;
1725
db:: runners:: add_registration( & h.app.db, & blake3:: hash( token.as_bytes ( ) ) .to_hex ( ) , " admin" , & Default :: default( ) ) .await.unwrap ( ) ;
1726
let identity = SecretKey:: generate( ) .identity ( " visionfive" , " " , 1 ) ;
1727
let info = serde_json:: json! ( { " arch" : " riscv64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ " riscv64" ] , " max_jobs" : 1 } ) ;
1728
let reg = serde_json:: json! ( { " token" : token, " name" : " visionfive" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1729
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1730
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1731
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1732
let claim_body = serde_json:: json! ( { " runner" : " x" , " runtimes" : [ " microvm" ] , " info" : info } ) .to_string ( ) ;
1733
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( & credential) , Some ( claim_body) ) .await;
1734
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1735
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job" ] [ " name" ] , " cross" ) ;
1736
}
070a7b64 Andy McCall 1 month ago ↶
1737
1738
1739
1740
1741
1742
# [ tokio ::test ( flavor = " multi_thread" ) ]
1743
async fn a_job_no_runner_reports_is_ended_after_the_time_limit ( ) {
1744
let h = hub ( ) .await;
1745
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1746
create ( & h.app, " andy" , " lost" ) .await;
1747
let url = format! ( " {} /andy/lost" , h.base) ;
1748
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1749
let c = client ( & url, work.path ( ) ) ;
1750
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1751
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1752
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1753
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1754
assert_eq! ( runs_of ( & h.app, " andy" , " lost" ) .await.len ( ) , 1 ) ;
1755
let job = claim ( & h.app) .await.expect ( " a job to claim" ) ;
1756
let id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1757
1758
1759
assert_eq! ( oxygenhub:: flow:: reap_lost_jobs( & h.app) .await.unwrap ( ) , 0 ) ;
1760
1761
let old = db:: now_ms( ) - 24 * 3600 * 1000 ;
3b1bc27f Andy McCall 21 days ago
1762
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 ↶
1763
assert_eq! ( oxygenhub:: flow:: reap_lost_jobs( & h.app) .await.unwrap ( ) , 1 ) ;
1764
let j = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
1765
assert_eq! ( j.status, " error" ) ;
1766
assert! ( j.error.starts_with ( " lost:" ) , " {}" , j.error) ;
1767
let run = & runs_of ( & h.app, " andy" , " lost" ) .await[ 0 ] ;
1768
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
1769
assert! ( jobs.iter ( ) .all ( | j | j.status ! = " running" ) , " nothing left running" ) ;
1770
1771
assert_ne! ( finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 } ) ) .await, StatusCode:: NO_CONTENT ) ;
1772
assert_eq! ( db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .status, " error" ) ;
1773
}
b0612e7d Andy McCall 1 month ago ↶
1774
1775
1776
1777
# [ tokio ::test ( flavor = " multi_thread" ) ]
1778
async fn a_runner_is_offered_only_jobs_whose_image_it_holds ( ) {
1779
let h = hub ( ) .await;
1780
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1781
create ( & h.app, " andy" , " images" ) .await;
1782
let url = format! ( " {} /andy/images" , h.base) ;
1783
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1784
let c = client ( & url, work.path ( ) ) ;
1785
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1786
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1787
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1788
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1789
assert_eq! ( runs_of ( & h.app, " andy" , " images" ) .await.len ( ) , 1 ) ;
1790
let claim_with = | images : Vec< String> | {
1791
let body = serde_json:: json! ( { " runner" : " test-1" , " runtimes" : [ " microvm" ] , " images" : images } ) .to_string ( ) ;
1792
let app = h.app.clone ( ) ;
1793
async move {
1794
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body) ) .await;
1795
( st, body)
1796
}
1797
} ;
1798
let ( st, _ ) = claim_with ( vec! [ " ab" .repeat ( 32 ) ] ) .await;
1799
assert_eq! ( st, StatusCode:: NO_CONTENT , " no job for a runner without the image" ) ;
1800
let jobs = db:: flow:: jobs( & h.app.db, runs_of ( & h.app, " andy" , " images" ) .await[ 0 ] .id) .await.unwrap ( ) ;
1801
let rootfs: serde_json:: Value = serde_json:: from_str( & jobs[ 0 ] .sandbox) .unwrap ( ) ;
1802
let rootfs = rootfs[ " rootfs" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1803
let ( st, body) = claim_with ( vec! [ " ab" .repeat ( 32 ) , rootfs] ) .await;
1804
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1805
}
eef677c4 Andy McCall 1 month ago ↶
1806
1807
1808
1809
1810
# [ tokio ::test ( flavor = " multi_thread" ) ]
1811
async fn a_job_its_runner_no_longer_holds_is_ended_at_its_next_claim ( ) {
1812
let h = hub ( ) .await;
1813
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1814
create ( & h.app, " andy" , " held" ) .await;
1815
let url = format! ( " {} /andy/held" , h.base) ;
1816
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1817
let c = client ( & url, work.path ( ) ) ;
1818
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1819
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1820
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1821
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1822
assert_eq! ( runs_of ( & h.app, " andy" , " held" ) .await.len ( ) , 1 ) ;
1823
let id = claim ( & h.app) .await.expect ( " a job to claim" ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1824
let claim_holding = | holding : Vec< i64 > | {
1825
let body = serde_json:: json! ( { " runner" : " test-1" , " runtimes" : [ " microvm" ] , " holding" : holding } ) .to_string ( ) ;
1826
let app = h.app.clone ( ) ;
1827
async move { call ( & app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body) ) .await.0 }
1828
} ;
1829
claim_holding ( vec! [ id] ) .await;
1830
assert_eq! ( db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .status, " running" , " a held job stays" ) ;
1831
claim_holding ( vec! [ ] ) .await;
1832
let j = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
1833
assert_eq! ( j.status, " error" ) ;
1834
assert! ( j.error.starts_with ( " lost:" ) & & j.error.contains ( " no longer holds" ) , " {}" , j.error) ;
1835
let run = & runs_of ( & h.app, " andy" , " held" ) .await[ 0 ] ;
1836
assert! ( db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) .iter ( ) .all ( | j | j.status ! = " running" ) , " the run settles" ) ;
1837
}
d7ce41ac Andy McCall 1 month ago ↶
1838
1839
1840
1841
1842
# [ tokio ::test ( flavor = " multi_thread" ) ]
1843
async fn a_scoped_job_runs_only_when_a_push_touches_its_paths ( ) {
1844
let h = hub ( ) .await;
1845
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1846
create ( & h.app, " andy" , " scoped" ) .await;
1847
let url = format! ( " {} /andy/scoped" , h.base) ;
1848
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1849
let c = client ( & url, work.path ( ) ) ;
1850
let extra = " job \" dist\" {\n sandbox \" alpine\" \n scope \" Cargo.toml\" \" packaging/**\" \n exec \" true\" \n }\n " ;
1851
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
1852
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1853
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.0\" \n " ) ;
1854
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
1855
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1856
let names = | app : Shared, n : usize | async move {
1857
let mut runs = Vec :: new( ) ;
1858
for _ in 0 .. 100 {
1859
runs = runs_of ( & app, " andy" , " scoped" ) .await;
1860
if runs.len ( ) > = n {
1861
break ;
1862
}
1863
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
1864
}
1865
assert_eq! ( runs.len ( ) , n, " runs" ) ;
1866
let mut j: Vec< String> = db:: flow:: jobs( & app.db, runs[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
1867
j.sort ( ) ;
745d225d Andy McCall 1 month ago ↶
1868
3b1bc27f Andy McCall 21 days ago
1869
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 ↶
1870
j
1871
} ;
1872
assert_eq! ( names ( h.app.clone ( ) , 1 ) .await, [ " build" , " dist" , " lint" , " test" ] , " the first run has every job" ) ;
1873
write ( work.path ( ) , " test.sh" , " echo still ok\n " ) ;
1874
c.commit ( " Edit the test" , & [ ] ) .unwrap ( ) ;
1875
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1876
assert_eq! ( names ( h.app.clone ( ) , 2 ) .await, [ " build" , " lint" , " test" ] , " dist is out of scope" ) ;
1877
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.1\" \n " ) ;
1878
c.commit ( " Release 0.1.1" , & [ ] ) .unwrap ( ) ;
1879
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1880
assert_eq! ( names ( h.app.clone ( ) , 3 ) .await, [ " build" , " dist" , " lint" , " test" ] , " a version bump runs dist" ) ;
1881
}
5dead1f4 Andy McCall 1 month ago ↶
1882
367f34b8 Andy McCall 17 days ago ↶
1883
1884
1885
1886
# [ tokio ::test ( flavor = " multi_thread" ) ]
1887
async fn a_push_can_skip_its_run ( ) {
1888
let h = hub ( ) .await;
1889
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1890
create ( & h.app, " andy" , " skip" ) .await;
1891
let url = format! ( " {} /andy/skip" , h.base) ;
1892
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1893
let c = client ( & url, work.path ( ) ) ;
1894
let extra = " job \" dist\" {\n sandbox \" alpine\" \n scope \" Cargo.toml\" \n exec \" true\" \n }\n " ;
1895
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
1896
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1897
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.0\" \n " ) ;
1898
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
1899
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1900
let newest = | app : Shared, n : usize | async move {
1901
for _ in 0 .. 100 {
1902
let runs = runs_of ( & app, " andy" , " skip" ) .await;
1903
if runs.len ( ) > = n {
1904
assert_eq! ( runs.len ( ) , n, " runs" ) ;
1905
let r = runs[ 0 ] .clone ( ) ;
1906
let mut jobs: Vec< String> = db:: flow:: jobs( & app.db, r.id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
1907
jobs.sort ( ) ;
1908
if r.status ! = " skipped" {
1909
sqlx:: query( " UPDATE flow_runs SET status = 'passed' WHERE id = $1" ) .bind ( r.id) .execute ( & app.db) .await.unwrap ( ) ;
1910
}
1911
return ( r, jobs) ;
1912
}
1913
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
1914
}
1915
panic! ( " no run {n}" ) ;
1916
} ;
1917
assert_eq! ( newest ( h.app.clone ( ) , 1 ) .await.1 , [ " build" , " dist" , " lint" , " test" ] ) ;
1918
1919
1920
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.1\" \n " ) ;
1921
c.commit ( " Bump the version [Skip CI]" , & [ ] ) .unwrap ( ) ;
1922
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1923
let ( run, jobs) = newest ( h.app.clone ( ) , 2 ) .await;
1924
assert_eq! ( run.status, " skipped" ) ;
1925
assert! ( jobs.is_empty ( ) , " {jobs:?}" ) ;
1926
assert! ( run.error.starts_with ( " [skip ci] in change " ) & & run.error.contains ( " , pushed by " ) , " {}" , run.error) ;
1927
assert! ( run.finished_ms.is_some ( ) ) ;
1928
let ( _ , page) = call ( & h.app, " GET" , " /andy/skip/flow/2" , None , None ) .await;
1929
assert! ( page.contains ( " the push asked to skip this run" ) , " {page}" ) ;
1930
1931
1932
1933
write ( work.path ( ) , " test.sh" , " echo still ok\n " ) ;
1934
c.commit ( " Edit the test" , & [ ] ) .unwrap ( ) ;
1935
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1936
assert_eq! ( newest ( h.app.clone ( ) , 3 ) .await.1 , [ " build" , " dist" , " lint" , " test" ] , " the skipped bump still runs dist" ) ;
1937
1938
1939
let row = db:: repos:: get( & h.app.db, " andy" , " skip" ) .await.unwrap ( ) .unwrap ( ) ;
1940
db:: repos:: set_flow_skip( & h.app.db, row.id, " others" ) .await.unwrap ( ) ;
1941
write ( work.path ( ) , " test.sh" , " echo ok again\n " ) ;
1942
c.commit ( " Docs only [skip flow]" , & [ ] ) .unwrap ( ) ;
1943
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1944
let ( run, jobs) = newest ( h.app.clone ( ) , 4 ) .await;
1945
assert_ne! ( run.status, " skipped" ) ;
1946
assert_eq! ( jobs, [ " build" , " lint" , " test" ] ) ;
1947
let row = db:: repos:: get( & h.app.db, " andy" , " skip" ) .await.unwrap ( ) .unwrap ( ) ;
1948
assert! ( oxygenhub:: flow:: skip_allowed( & row, " feature" ) & & ! oxygenhub:: flow:: skip_allowed( & row, " main" ) ) ;
1949
db:: repos:: set_flow_skip( & h.app.db, row.id, " never" ) .await.unwrap ( ) ;
1950
let row = db:: repos:: get( & h.app.db, " andy" , " skip" ) .await.unwrap ( ) .unwrap ( ) ;
1951
assert! ( ! oxygenhub:: flow:: skip_allowed( & row, " feature" ) ) ;
1952
}
1953
5588bb36 Andy McCall 17 days ago ↶
1954
1955
1956
1957
# [ tokio ::test ( flavor = " multi_thread" ) ]
1958
async fn a_push_reuses_a_pass_on_the_same_state ( ) {
1959
let h = hub ( ) .await;
1960
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1961
create ( & h.app, " andy" , " reuse" ) .await;
1962
let url = format! ( " {} /andy/reuse" , h.base) ;
1963
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1964
let c = client ( & url, work.path ( ) ) ;
1965
let gated = " job \" deploy\" {\n sandbox \" alpine\" \n depends-on \" test\" \n approval {\n by \" maintain\" \n }\n exec \" true\" \n }\n " ;
1966
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( gated) ) ;
1967
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1968
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
1969
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1970
let first = runs_of ( & h.app, " andy" , " reuse" ) .await.remove ( 0 ) ;
1971
1972
sqlx:: query( " UPDATE flow_jobs SET status = 'passed', exit_code = 0, receipt_hash = 'r-' || name WHERE run_id = $1" )
1973
.bind ( first.id)
1974
.execute ( & h.app.db)
1975
.await
1976
.unwrap ( ) ;
1977
sqlx:: query( " UPDATE flow_runs SET status = 'passed' WHERE id = $1" ) .bind ( first.id) .execute ( & h.app.db) .await.unwrap ( ) ;
1978
let heads: Vec< oxy_core:: hash:: ChangeHash> = first.head_list ( ) .into_iter ( ) .filter_map ( oxy_core:: hash:: ChangeHash:: from_hex) .collect ( ) ;
1979
let row = db:: repos:: get( & h.app.db, " andy" , " reuse" ) .await.unwrap ( ) .unwrap ( ) ;
1980
assert! ( row.flow_reuse, " on by default" ) ;
1981
let again = | row : db:: repos:: RepoRow| {
1982
let ( app, state, heads) = ( h.app.clone ( ) , first.state.clone ( ) , heads.clone ( ) ) ;
1983
async move {
1984
let n =
1985
oxygenhub:: flow:: start_run_scoped( & app, & row, " main" , & state, & heads, " andy" , " push" , None ) .await.unwrap ( ) .expect ( " a run" ) ;
1986
let run = db:: flow:: run( & app.db, row.id, n) .await.unwrap ( ) .unwrap ( ) ;
1987
let jobs = db:: flow:: jobs( & app.db, run.id) .await.unwrap ( ) ;
1988
( run, jobs)
1989
}
1990
} ;
1991
let ( run, jobs) = again ( row.clone ( ) ) .await;
1992
let by = | name : & str | jobs.iter ( ) .find ( | j | j.name = = name ) .unwrap ( ) .clone ( ) ;
1993
for name in [ " build" , " test" , " lint" ] {
1994
let j = by ( name) ;
1995
assert_eq! ( ( j.status.as_str ( ) , j.reused_from ( ) ) , ( " passed" , Some ( first.number) ) , " {name}: {j:?}" ) ;
1996
assert_eq! ( j.receipt_hash, format! ( " r-{name} " ) , " the pass's receipt stands for it" ) ;
1997
}
1998
let deploy = by ( " deploy" ) ;
1999
assert! ( deploy.status = = " queued" & & deploy.reused_from ( ) .is_none ( ) , " a gated job runs again: {deploy:?}" ) ;
2000
assert_eq! ( run.status, " queued" , " the run waits for the gated job" ) ;
2001
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/reuse/flow/{} " , run.number) , None , None ) .await;
2002
assert! ( page.contains ( " data-reused" ) & & page.contains ( & format! ( " /andy/reuse/flow/{} " , first.number) ) , " {page}" ) ;
2003
2004
let checks = db:: hooks:: checks_for_state( & h.app.db, row.id, & first.state) .await.unwrap ( ) ;
2005
assert! (
2006
checks.iter ( ) .any ( | c | c.name = = " flow/build" & & c.status = = " passed" & & c.summary.contains ( " reused" ) ) ,
2007
" a reused pass reports its check: {checks:?}"
2008
) ;
2009
2010
2011
db:: repos:: set_flow_reuse( & h.app.db, row.id, false ) .await.unwrap ( ) ;
2012
let row = db:: repos:: get( & h.app.db, " andy" , " reuse" ) .await.unwrap ( ) .unwrap ( ) ;
2013
let ( _ , jobs) = again ( row) .await;
2014
assert! ( jobs.iter ( ) .all ( | j | j.status = = " queued" & & j.reused_from ( ) .is_none ( ) ) , " {jobs:?}" ) ;
2015
}
2016
42cd46c9 Andy McCall 10 days ago ↶
2017
2018
2019
2020
# [ tokio ::test ( flavor = " multi_thread" ) ]
2021
async fn a_fork_reuses_what_upstream_proved ( ) {
2022
let h = hub ( ) .await;
2023
for who in [ " andy" , " bob" ] {
2024
db:: accounts:: create_account( & h.app.db, who, & format! ( " {who} @example.com" ) , " x" , " salt" , true ) .await.unwrap ( ) ;
2025
}
2026
create ( & h.app, " andy" , " game" ) .await;
2027
create ( & h.app, " bob" , " game" ) .await;
2028
let up = db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
2029
let fork = db:: repos:: get( & h.app.db, " bob" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
2030
db:: repos:: set_forked_from( & h.app.db, fork.id, up.id) .await.unwrap ( ) ;
2031
2032
sqlx:: query( " UPDATE repositories SET flow = 'on' WHERE id = $1" ) .bind ( fork.id) .execute ( & h.app.db) .await.unwrap ( ) ;
2033
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2034
let mut c = client ( & format! ( " {} /andy/game" , h.base) , work.path ( ) ) ;
2035
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
2036
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2037
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
2038
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2039
let first = runs_of ( & h.app, " andy" , " game" ) .await.remove ( 0 ) ;
06b75635 Andy McCall 10 days ago ↶
2040
2041
2042
{
2043
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectType} ;
2044
use oxy_core:: identity:: SecretKey;
2045
use oxy_core:: objects:: StateObject;
2046
use oxy_core:: receipt:: SignedReceipt;
2047
let heads: Vec< ChangeHash> = first.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
2048
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads) .encode ( ) ) ;
2049
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/game" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
2050
let sk = SecretKey:: generate( ) ;
2051
let runner = sk.identity ( " test-1 (OxygenFlow runner)" , " " , 1 ) ;
2052
let run_url = format! ( " {} /andy/game/flow/{} " , h.base, first.number) ;
2053
while let Some ( j) = claim ( & h.app) .await {
2054
let r = receipt ( j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
2055
let parsed: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
2056
let signed = hex:: encode( SignedReceipt:: sign( parsed.to_oxy ( ) , repo_id, state, & run_url, & runner, & sk) .encode ( ) ) ;
2057
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 5 , " receipt" : r, " signed_receipt" : signed } ) ;
2058
assert_eq! ( finish ( & h.app, j[ " job_id" ] .as_i64 ( ) .unwrap ( ) , body) .await, StatusCode:: NO_CONTENT ) ;
2059
}
2060
}
2061
let up_jobs = db:: flow:: jobs( & h.app.db, first.id) .await.unwrap ( ) ;
2062
assert! ( up_jobs.iter ( ) .all ( | j | j.status = = " passed" & & j.receipt_attest.len ( ) = = 64 ) , " {up_jobs:?}" ) ;
42cd46c9 Andy McCall 10 days ago ↶
2063
2064
c.config.set ( " remote.origin.url" , & format! ( " {} /bob/game" , h.base) ) .unwrap ( ) ;
2065
c.save_config ( ) .unwrap ( ) ;
2066
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2067
let run = runs_of ( & h.app, " bob" , " game" ) .await.remove ( 0 ) ;
2068
assert_eq! ( run.state, first.state) ;
2069
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
2070
for j in & jobs {
2071
assert_eq! ( ( j.status.as_str ( ) , j.reused_from ( ) , j.reused_repo ( ) ) , ( " passed" , Some ( first.number) , Some ( " andy/game" ) ) , " {j:?}" ) ;
06b75635 Andy McCall 10 days ago ↶
2072
let from = up_jobs.iter ( ) .find ( | u | u.name = = j.name ) .unwrap ( ) ;
2073
assert_eq! ( j.receipt_attest, from.receipt_attest, " upstream's signed receipt stands for it" ) ;
42cd46c9 Andy McCall 10 days ago ↶
2074
}
2075
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /bob/game/flow/{} " , run.number) , None , None ) .await;
2076
assert! ( page.contains ( & format! ( " href=\" /andy/game/flow/{} \" >andy/game run #{} </a>" , first.number, first.number) ) , " {page}" ) ;
2077
let checks = db:: hooks:: checks_for_state( & h.app.db, fork.id, & run.state) .await.unwrap ( ) ;
2078
assert! ( checks.iter ( ) .any ( | c | c.summary.contains ( " passed in andy/game run #" ) ) , " {checks:?}" ) ;
06b75635 Andy McCall 10 days ago ↶
2079
2080
2081
2082
let lint = up_jobs.iter ( ) .find ( | j | j.name = = " lint" ) .unwrap ( ) ;
2083
sqlx:: query( " UPDATE flow_jobs SET receipt_attest = $1 WHERE id = $2" )
2084
.bind ( " ab" .repeat ( 32 ) )
2085
.bind ( lint.id)
2086
.execute ( & h.app.db)
2087
.await
2088
.unwrap ( ) ;
2089
create ( & h.app, " bob" , " game2" ) .await;
2090
let fork2 = db:: repos:: get( & h.app.db, " bob" , " game2" ) .await.unwrap ( ) .unwrap ( ) ;
2091
db:: repos:: set_forked_from( & h.app.db, fork2.id, up.id) .await.unwrap ( ) ;
2092
sqlx:: query( " UPDATE repositories SET flow = 'on' WHERE id = $1" ) .bind ( fork2.id) .execute ( & h.app.db) .await.unwrap ( ) ;
2093
c.config.set ( " remote.origin.url" , & format! ( " {} /bob/game2" , h.base) ) .unwrap ( ) ;
2094
c.save_config ( ) .unwrap ( ) ;
2095
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2096
let jobs2 = db:: flow:: jobs( & h.app.db, runs_of ( & h.app, " bob" , " game2" ) .await.remove ( 0 ) .id) .await.unwrap ( ) ;
2097
let status = | name : & str | jobs2.iter ( ) .find ( | j | j.name = = name ) .map ( | j | ( j.status.clone ( ) , j.reused_repo ( ) .map ( String :: from) ) ) .unwrap ( ) ;
2098
assert_eq! ( status ( " lint" ) , ( " queued" .to_string ( ) , None ) , " {jobs2:?}" ) ;
2099
assert_eq! ( status ( " build" ) , ( " passed" .to_string ( ) , Some ( " andy/game" .to_string ( ) ) ) , " {jobs2:?}" ) ;
2100
sqlx:: query( " UPDATE flow_jobs SET receipt_attest = $1 WHERE id = $2" )
2101
.bind ( & lint.receipt_attest)
2102
.bind ( lint.id)
2103
.execute ( & h.app.db)
2104
.await
2105
.unwrap ( ) ;
42cd46c9 Andy McCall 10 days ago ↶
2106
91c306aa Andy McCall 10 days ago ↶
2107
2108
2109
for who in [ " carol" ] {
2110
db:: accounts:: create_account( & h.app.db, who, & format! ( " {who} @example.com" ) , " x" , " salt" , false ) .await.unwrap ( ) ;
2111
}
2112
let push_to = | owner : & 'static str , name : & 'static str | {
2113
let ( app, base) = ( h.app.clone ( ) , h.base.clone ( ) ) ;
2114
let path = work.path ( ) .to_path_buf ( ) ;
2115
async move {
2116
create ( & app, owner, name) .await;
2117
let r = db:: repos:: get( & app.db, owner, name) .await.unwrap ( ) .unwrap ( ) ;
2118
sqlx:: query( " UPDATE repositories SET flow = 'on' WHERE id = $1" ) .bind ( r.id) .execute ( & app.db) .await.unwrap ( ) ;
2119
let mut c = Repo:: open( & path) .unwrap ( ) ;
2120
c.config.set ( " remote.origin.url" , & format! ( " {base} /{owner} /{name} " ) ) .unwrap ( ) ;
2121
c.save_config ( ) .unwrap ( ) ;
2122
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2123
let run = runs_of ( & app, owner, name) .await.remove ( 0 ) ;
2124
db:: flow:: jobs( & app.db, run.id) .await.unwrap ( )
2125
}
2126
} ;
2127
let jobs = push_to ( " carol" , " tetris-clone" ) .await;
2128
assert! ( jobs.iter ( ) .all ( | j | j.status = = " passed" & & j.reused_repo ( ) .is_some ( ) ) , " {jobs:?}" ) ;
2129
2130
2131
2132
sqlx:: query( " UPDATE repositories SET visibility = 'private' WHERE owner IN ('andy', 'bob')" ) .execute ( & h.app.db) .await.unwrap ( ) ;
2133
sqlx:: query( " UPDATE repositories SET visibility = 'private' WHERE owner = 'carol'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
2134
sqlx:: query( " DELETE FROM flow_jobs WHERE run_id IN (SELECT id FROM flow_runs WHERE repo_id IN (SELECT id FROM repositories WHERE owner = 'carol'))" )
2135
.execute ( & h.app.db)
2136
.await
2137
.unwrap ( ) ;
2138
let jobs = push_to ( " carol" , " another" ) .await;
42cd46c9 Andy McCall 10 days ago ↶
2139
assert! ( jobs.iter ( ) .all ( | j | j.status = = " queued" & & j.reused_from ( ) .is_none ( ) ) , " {jobs:?}" ) ;
2140
}
2141
367f34b8 Andy McCall 17 days ago ↶
2142
# [ test ]
2143
fn skip_markers_are_the_forges_spellings_in_any_case ( ) {
2144
use oxygenhub:: flow:: skip_marker;
2145
assert_eq! ( skip_marker ( " Fix typo [skip flow]" ) , Some ( " [skip flow]" ) ) ;
2146
assert_eq! ( skip_marker ( " [CI SKIP] docs" ) , Some ( " [ci skip]" ) ) ;
2147
assert_eq! ( skip_marker ( " Docs\n \n [Skip CI]" ) , Some ( " [skip ci]" ) ) ;
2148
assert_eq! ( skip_marker ( " skip ci without brackets" ) , None ) ;
3b3a51af Andy McCall 17 days ago ↶
2149
2150
assert_eq! ( skip_marker ( " Document the `[skip flow]` marker" ) , None ) ;
2151
assert_eq! ( skip_marker ( " Example:\n \n ```\n git commit -m 'x [skip ci]'\n ```\n " ) , None ) ;
2152
assert_eq! ( skip_marker ( " `code` then [skip ci]" ) , Some ( " [skip ci]" ) ) ;
2153
assert_eq! ( skip_marker ( " an odd ` backtick [skip ci]" ) , Some ( " [skip ci]" ) ) ;
367f34b8 Andy McCall 17 days ago ↶
2154
}
2155
745d225d Andy McCall 1 month ago ↶
2156
2157
2158
2159
# [ tokio ::test ( flavor = " multi_thread" ) ]
2160
async fn runs_are_cancelled_by_hand_or_by_a_newer_push ( ) {
2161
let h = hub ( ) .await;
2162
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2163
create ( & h.app, " andy" , " stop" ) .await;
2164
let url = format! ( " {} /andy/stop" , h.base) ;
2165
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2166
let c = client ( & url, work.path ( ) ) ;
2167
let extra = " job \" dist\" {\n sandbox \" alpine\" \n scope \" Cargo.toml\" \n exec \" true\" \n }\n " ;
2168
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
2169
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2170
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.0\" \n " ) ;
2171
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
2172
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2173
let wait_runs = | app : Shared, n : usize | async move {
2174
for _ in 0 .. 100 {
2175
let runs = runs_of ( & app, " andy" , " stop" ) .await;
2176
if runs.len ( ) > = n {
2177
return runs;
2178
}
2179
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2180
}
2181
panic! ( " no run {n}" ) ;
2182
} ;
2183
let first = wait_runs ( h.app.clone ( ) , 1 ) .await;
3b1bc27f Andy McCall 21 days ago
2184
sqlx:: query( " UPDATE flow_runs SET status = 'passed' WHERE id = $1" ) .bind ( first[ 0 ] .id) .execute ( & h.app.db) .await.unwrap ( ) ;
2185
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 ↶
2186
2187
2188
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.1\" \n " ) ;
2189
c.commit ( " Release 0.1.1" , & [ ] ) .unwrap ( ) ;
2190
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2191
wait_runs ( h.app.clone ( ) , 2 ) .await;
2192
let job = claim ( & h.app) .await.expect ( " a job of run 2" ) ;
2193
let id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2194
write ( work.path ( ) , " test.sh" , " echo still ok\n " ) ;
2195
c.commit ( " Edit the test" , & [ ] ) .unwrap ( ) ;
2196
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2197
let runs = wait_runs ( h.app.clone ( ) , 3 ) .await;
2198
let third = & runs[ 0 ] ;
2199
let mut second = runs[ 1 ] .clone ( ) ;
2200
for _ in 0 .. 100 {
2201
second = db:: flow:: run_by_id( & h.app.db, runs[ 1 ] .id) .await.unwrap ( ) .unwrap ( ) ;
2202
if ! second.live ( ) {
2203
break ;
2204
}
2205
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2206
}
2207
assert_eq! ( second.status, " cancelled" , " a newer push supersedes a run still going" ) ;
2208
let jobs = db:: flow:: jobs( & h.app.db, second.id) .await.unwrap ( ) ;
2209
assert! ( jobs.iter ( ) .all ( | j | j.status = = " cancelled" & & j.error = = " superseded by run #3" ) , " {jobs:?}" ) ;
2210
let mut names: Vec< String> = db:: flow:: jobs( & h.app.db, third.id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
2211
names.sort ( ) ;
2212
assert_eq! ( names, [ " build" , " dist" , " lint" , " test" ] , " run 3 also checks what run 2 would have: the version bump" ) ;
2213
let mut checks = vec! [ ] ;
2214
for _ in 0 .. 100 {
2215
checks = db:: hooks:: checks_for_state( & h.app.db, second.repo_id, & second.state) .await.unwrap ( ) ;
2216
if checks.iter ( ) .all ( | c | c.status = = " cancelled" ) {
2217
break ;
2218
}
2219
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2220
}
2221
assert! ( checks.iter ( ) .all ( | c | c.status = = " cancelled" ) , " {checks:?}" ) ;
2222
2223
let ( st, _ ) =
2224
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( r # "{"records":[]}"# .into ( ) ) ) .await;
2225
assert_eq! ( st, StatusCode:: CONFLICT ) ;
2226
2227
2228
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/stop/flow/runs/3/cancel" , Some ( " site" ) , None ) .await;
2229
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
2230
let third = db:: flow:: run_by_id( & h.app.db, third.id) .await.unwrap ( ) .unwrap ( ) ;
2231
assert_eq! ( third.status, " cancelled" ) ;
2232
assert! ( db:: flow:: jobs( & h.app.db, third.id) .await.unwrap ( ) .iter ( ) .all ( | j | j.error = = " cancelled by andy" ) ) ;
2233
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/stop/flow/runs/3/cancel" , Some ( " site" ) , None ) .await;
2234
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " already cancelled" ) , " {st} {body}" ) ;
2235
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/stop/flow/runs/3/cancel" , None , None ) .await;
2236
assert_ne! ( st, StatusCode:: OK , " anonymous callers cannot cancel" ) ;
2237
let ( _ , page) = call ( & h.app, " GET" , " /andy/stop/flow/3" , None , None ) .await;
2238
assert! ( page.contains ( " ⊘" ) & & ! page.contains ( " CANCEL</button>" ) , " a cancelled run shows so, with no button" ) ;
2239
}
2240
5dead1f4 Andy McCall 1 month ago ↶
2241
2242
2243
2244
# [ tokio ::test ( flavor = " multi_thread" ) ]
2245
async fn a_job_runs_under_its_own_time_limit_within_the_sites ( ) {
2246
let h = hub ( ) .await;
2247
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2248
db:: accounts:: set_setting( & h.app.db, oxygenhub:: flow:: SETTING_DEFAULT , " 30" ) .await.unwrap ( ) ;
2249
db:: accounts:: set_setting( & h.app.db, oxygenhub:: flow:: SETTING_MAX , " 120" ) .await.unwrap ( ) ;
2250
create ( & h.app, " andy" , " limits" ) .await;
2251
let url = format! ( " {} /andy/limits" , h.base) ;
2252
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2253
let c = client ( & url, work.path ( ) ) ;
2254
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 " ;
2255
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
2256
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2257
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2258
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2259
assert_eq! ( runs_of ( & h.app, " andy" , " limits" ) .await.len ( ) , 1 ) ;
2260
let mut limits = std:: collections:: HashMap:: new( ) ;
2261
let mut ids = std:: collections:: HashMap:: new( ) ;
2262
while let Some ( j) = claim ( & h.app) .await {
2263
let name = j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
2264
limits.insert ( name.clone ( ) , j[ " timeout_s" ] .as_u64 ( ) .unwrap ( ) ) ;
2265
ids.insert ( name, j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ) ;
2266
}
2267
assert_eq! ( limits[ " slow" ] , 5400 , " its own" ) ;
2268
assert_eq! ( limits[ " huge" ] , 7200 , " cut to the site's maximum" ) ;
2269
assert_eq! ( limits[ " build" ] , 1800 , " the site's default" ) ;
2270
2271
let started = db:: now_ms( ) - 40 * 60 * 1000 ;
3b1bc27f Andy McCall 21 days ago
2272
sqlx:: query( " UPDATE flow_jobs SET started_ms = $1" ) .bind ( started) .execute ( & h.app.db) .await.unwrap ( ) ;
5dead1f4 Andy McCall 1 month ago ↶
2273
oxygenhub:: flow:: reap_lost_jobs( & h.app) .await.unwrap ( ) ;
2274
let status = | id : i64 | {
2275
let app = h.app.clone ( ) ;
2276
async move { db:: flow:: job( & app.db, id) .await.unwrap ( ) .unwrap ( ) .status }
2277
} ;
2278
assert_eq! ( status ( ids[ " build" ] ) .await, " error" , " past its 30 minutes" ) ;
2279
assert_eq! ( status ( ids[ " slow" ] ) .await, " running" , " within its 90" ) ;
2280
assert_eq! ( status ( ids[ " huge" ] ) .await, " running" , " within its 120" ) ;
2281
}
c889911b Andy McCall 1 month ago ↶
2282
2283
2284
2285
2286
# [ tokio ::test ( flavor = " multi_thread" ) ]
2287
async fn on_decides_which_events_start_a_job ( ) {
2288
let h = hub ( ) .await;
2289
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2290
create ( & h.app, " andy" , " events" ) .await;
2291
let url = format! ( " {} /andy/events" , h.base) ;
2292
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2293
let c = client ( & url, work.path ( ) ) ;
2294
let text = format! (
2295
" on {{ \n push \" main\" \n manual\n }} \n {} job \" dist\" {{ \n sandbox \" alpine\" \n on {{ \n tag \" v*\" \n }} \n exec \" true\" \n }} \n " ,
2296
pipeline ( " " )
2297
) ;
2298
2299
let text = text.replacen (
2300
" on {\n push \" main\" \n manual\n }\n schema-version 1\n " ,
2301
" schema-version 1\n on {\n push \" main\" \n manual\n }\n " ,
2302
1 ,
2303
) ;
2304
write ( work.path ( ) , " .flow/pipeline.kdl" , & text) ;
2305
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2306
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
2307
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2308
let jobs_of_latest = | app : Shared, n : usize | async move {
2309
let mut runs = Vec :: new( ) ;
2310
for _ in 0 .. 100 {
2311
runs = runs_of ( & app, " andy" , " events" ) .await;
2312
if runs.len ( ) > = n {
2313
break ;
2314
}
2315
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2316
}
2317
assert_eq! ( runs.len ( ) , n, " runs" ) ;
2318
let mut j: Vec< String> = db:: flow:: jobs( & app.db, runs[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
2319
j.sort ( ) ;
2320
( runs[ 0 ] .clone ( ) , j)
2321
} ;
2322
let ( _ , j) = jobs_of_latest ( h.app.clone ( ) , 1 ) .await;
2323
assert_eq! ( j, [ " build" , " lint" , " test" ] , " a push to main: no dist" ) ;
2324
2325
let heads = c.bookmark_heads ( ) .unwrap ( ) ;
2326
c.tag_create ( " v1.0.0" , & heads, " first" ) .unwrap ( ) ;
2327
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2328
let ( run, j) = jobs_of_latest ( h.app.clone ( ) , 2 ) .await;
2329
assert_eq! ( j, [ " dist" ] , " a tag runs only what names it" ) ;
2330
assert_eq! ( run.trigger, " tag:v1.0.0" ) ;
2331
2332
let row = db:: repos:: get( & h.app.db, " andy" , " events" ) .await.unwrap ( ) .unwrap ( ) ;
2333
db:: flow:: finish(
2334
& h.app.db,
2335
db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) [ 0 ] .id,
2336
db:: flow:: Outcome { status: " passed" , exit_code: Some ( 0 ) , error: " " , receipt: " " , receipt_hash: " " } ,
2337
)
2338
.await
2339
.unwrap ( ) ;
2340
let run = db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) ;
2341
oxygenhub:: flow:: rerun( & h.app, & row, & run, " andy" ) .await.unwrap ( ) ;
2342
let ( _ , j) = jobs_of_latest ( h.app.clone ( ) , 3 ) .await;
2343
assert_eq! ( j, [ " dist" ] , " the rerun is the tag's run again" ) ;
2344
2345
c.bookmark_create ( " develop" , None ) .unwrap ( ) ;
2346
assert! ( c.push_bookmark ( " origin" , " develop" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2347
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
2348
assert_eq! ( runs_of ( & h.app, " andy" , " events" ) .await.len ( ) , 3 , " no run for develop" ) ;
348f8c4b Andy McCall 1 month ago ↶
2349
}
2350
2351
2352
2353
# [ tokio ::test ( flavor = " multi_thread" ) ]
2354
async fn scheduled_jobs_run_when_due ( ) {
2355
let h = hub ( ) .await;
2356
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2357
create ( & h.app, " andy" , " nightly" ) .await;
2358
let url = format! ( " {} /andy/nightly" , h.base) ;
2359
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2360
let c = client ( & url, work.path ( ) ) ;
2361
2362
let extra = " job \" nightly\" {\n sandbox \" alpine\" \n on {\n schedule \" 17 4 29 2 *\" \n }\n exec \" true\" \n }\n " ;
2363
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
2364
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2365
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
2366
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2367
assert_eq! ( runs_of ( & h.app, " andy" , " nightly" ) .await.len ( ) , 1 ) ;
2368
let first = runs_of ( & h.app, " andy" , " nightly" ) .await;
2369
let names: Vec< String> = db:: flow:: jobs( & h.app.db, first[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
2370
assert! ( ! names.contains ( & " nightly" .to_string ( ) ) , " a push does not run a scheduled job" ) ;
2371
use flow_core:: pipeline:: When;
2372
let not_yet = When { minute: 16 , hour: 4 , day: 29 , month: 2 , weekday: 4 } ;
2373
assert_eq! ( oxygenhub:: flow:: run_due( & h.app, not_yet) .await, 0 ) ;
2374
let due = When { minute: 17 , .. not_yet } ;
2375
assert_eq! ( oxygenhub:: flow:: run_due( & h.app, due) .await, 1 ) ;
2376
let runs = runs_of ( & h.app, " andy" , " nightly" ) .await;
2377
assert_eq! ( runs.len ( ) , 2 ) ;
2378
assert_eq! ( runs[ 0 ] .trigger, " schedule" ) ;
2379
let names: Vec< String> = db:: flow:: jobs( & h.app.db, runs[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
2380
assert_eq! ( names, [ " nightly" ] ) ;
2381
assert_eq! ( runs[ 0 ] .bookmark, " main" , " on the default bookmark" ) ;
5f3fa151 Andy McCall 12 days ago ↶
2382
2383
2384
write ( work.path ( ) , " .flow/pipeline.kdl" , " schema-version 1\n " ) ;
2385
c.commit ( " No jobs" , & [ ] ) .unwrap ( ) ;
2386
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2387
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
2388
let runs = runs_of ( & h.app, " andy" , " nightly" ) .await;
2389
assert_eq! ( runs.len ( ) , 3 ) ;
2390
assert! ( runs[ 0 ] .error.contains ( " at least one job" ) , " {}" , runs[ 0 ] .error) ;
2391
assert_eq! ( oxygenhub:: flow:: run_due( & h.app, due) .await, 0 ) ;
2392
assert_eq! ( oxygenhub:: flow:: run_due( & h.app, not_yet) .await, 0 ) ;
2393
assert_eq! ( runs_of ( & h.app, " andy" , " nightly" ) .await.len ( ) , 3 , " no scheduled runs of a broken pipeline" ) ;
c889911b Andy McCall 1 month ago ↶
2394
}
2a83f530 Andy McCall 1 month ago ↶
2395
2396
# [ tokio ::test ( flavor = " multi_thread" ) ]
2397
async fn variables_from_three_levels_reach_a_job_inner_overriding_outer ( ) {
2398
use oxygenhub:: levels:: Scope;
2399
use oxygenhub:: variables;
2400
let h = hub ( ) .await;
2401
let app = & h.app;
2402
2403
let res = oxygenhub:: router( app.clone ( ) )
2404
.oneshot (
2405
Request:: post( " /register" )
2406
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 21 days ago
2407
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
2a83f530 Andy McCall 1 month ago ↶
2408
.unwrap ( ) ,
2409
)
2410
.await
2411
.unwrap ( ) ;
2412
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
2413
let andy = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
2414
db:: accounts:: create_org( & app.db, " acme" , " Acme" , andy.id) .await.unwrap ( ) ;
2415
create ( app, " acme" , " app" ) .await;
2416
let page = | path : & str , form : Option< String> | {
2417
let mut req =
2418
Request:: builder( ) .method ( if form.is_some ( ) { " POST" } else { " GET" } ) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
2419
if form.is_some ( ) {
2420
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
2421
}
2422
let req = req.body ( Body:: from( form.unwrap_or_default ( ) ) ) .unwrap ( ) ;
c7067c7b Andy McCall 17 days ago
2423
let ( app, cookie) = ( app.clone ( ) , cookie.clone ( ) ) ;
2a83f530 Andy McCall 1 month ago ↶
2424
async move {
c7067c7b Andy McCall 17 days ago
2425
let mut res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
2426
2427
let back =
2428
res.headers ( ) .get ( header:: LOCATION ) .and_then ( | v | v.to_str ( ) .ok ( ) ) .filter ( | l | l.contains ( " /settings/" ) ) .map ( String :: from) ;
2429
if let Some ( to) = back {
2430
let req = Request:: get( to) .header ( header:: COOKIE , cookie) .body ( Body:: empty( ) ) .unwrap ( ) ;
2431
res = oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) ;
2432
}
2a83f530 Andy McCall 1 month ago ↶
2433
let status = res.status ( ) ;
2434
let body = String :: from_utf8( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ) .unwrap ( ) ;
2435
( status, body)
2436
}
2437
} ;
2438
let csrf_of = | page : & str | {
2439
let i = page.find ( " name=\" _csrf\" value=\" " ) .expect ( " csrf field" ) + " name=\" _csrf\" value=\" " .len ( ) ;
2440
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
2441
} ;
2442
2443
2444
2445
variables:: set( & app.db, Scope:: Site, " CC" , " gcc" ) .await.unwrap ( ) ;
2446
variables:: set( & app.db, Scope:: Site, " LANG" , " C" ) .await.unwrap ( ) ;
2447
let org = db:: accounts:: org_by_name( & app.db, " acme" ) .await.unwrap ( ) .unwrap ( ) ;
2448
variables:: set( & app.db, Scope:: Org( org.id) , " CC" , " clang" ) .await.unwrap ( ) ;
c7067c7b Andy McCall 17 days ago
2449
let ( st, settings) = page ( " /acme/app/settings/variables" , None ) .await;
2a83f530 Andy McCall 1 month ago ↶
2450
assert_eq! ( st, StatusCode:: OK , " {settings}" ) ;
2451
assert! ( settings.contains ( " Inherited" ) & & settings.contains ( " the acme organisation" ) & & settings.contains ( " overridden" ) , " {settings}" ) ;
2452
let csrf = csrf_of ( & settings) ;
2453
let ( _ , body) = page ( " /acme/app/settings/variables" , Some ( format! ( " _csrf={csrf} &action=set&name=RUST_LOG&value=debug" ) ) ) .await;
2454
assert! ( body.contains ( " RUST_LOG set." ) & & body.contains ( " <code>debug</code>" ) , " {body}" ) ;
2455
let ( _ , body) = page ( " /acme/app/settings/variables" , Some ( format! ( " _csrf={csrf} &action=set&name=FLOW_REPO&value=x" ) ) ) .await;
2456
assert! ( body.contains ( " not FLOW_" ) , " {body}" ) ;
2457
let repo = db:: repos:: get( & app.db, " acme" , " app" ) .await.unwrap ( ) .unwrap ( ) ;
2458
db:: repos:: set_flow( & app.db, repo.id, " on" ) .await.unwrap ( ) ;
2459
let env = variables:: env_for_repo( & app.db, & repo) .await.unwrap ( ) ;
2460
assert_eq! (
2461
env.iter ( ) .map ( | ( k , v ) | ( k.as_str ( ) , v.as_str ( ) ) ) .collect:: < Vec< _ > > ( ) ,
2462
[ ( " CC" , " clang" ) , ( " LANG" , " C" ) , ( " RUST_LOG" , " debug" ) ] ,
2463
" the organisation's CC wins over the site's"
2464
) ;
2465
2466
2467
let url = format! ( " {} /acme/app" , h.base) ;
2468
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2469
let c = client ( & url, work.path ( ) ) ;
2470
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
2471
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2472
c.snapshot ( " test" ) .unwrap ( ) ;
2473
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2474
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2475
let runs = runs_of ( app, " acme" , " app" ) .await;
2476
assert_eq! ( runs.len ( ) , 1 , " {runs:?}" ) ;
2477
let got = claim ( app) .await.unwrap_or_else ( | | panic! ( " a job: {:?}" , runs[ 0 ] ) ) ;
2478
assert_eq! ( got[ " env" ] , serde_json:: json! ( { " CC" : " clang" , " LANG" : " C" , " RUST_LOG" : " debug" } ) , " {got}" ) ;
2479
2480
737de0fa Andy McCall 17 days ago ↶
2481
let ( _ , admin) = page ( " /admin/variables" , None ) .await;
2a83f530 Andy McCall 1 month ago ↶
2482
assert! ( admin.contains ( " Variables" ) & & admin.contains ( " <code>gcc</code>" ) , " {admin}" ) ;
2483
let ( _ , body) = page ( " /admin/variables" , Some ( format! ( " _csrf={} &action=delete&name=LANG" , csrf_of ( & admin) ) ) ) .await;
2484
assert! ( body.contains ( " LANG removed." ) , " {body}" ) ;
2485
let got = claim ( app) .await.expect ( " another job" ) ;
2486
assert_eq! ( got[ " env" ] , serde_json:: json! ( { " CC" : " clang" , " RUST_LOG" : " debug" } ) , " {got}" ) ;
2487
}
4ac01a73 Andy McCall 1 month ago ↶
2488
2489
# [ tokio ::test ( flavor = " multi_thread" ) ]
2490
async fn secrets_a_job_declares_come_with_the_claim_and_never_reach_the_log ( ) {
2491
let h = hub ( ) .await;
2492
let app = & h.app;
2493
let res = oxygenhub:: router( app.clone ( ) )
2494
.oneshot (
2495
Request:: post( " /register" )
2496
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 21 days ago
2497
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
4ac01a73 Andy McCall 1 month ago ↶
2498
.unwrap ( ) ,
2499
)
2500
.await
2501
.unwrap ( ) ;
2502
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
2503
let page = | path : & str , form : Option< String> | {
2504
let mut req =
2505
Request:: builder( ) .method ( if form.is_some ( ) { " POST" } else { " GET" } ) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
2506
if form.is_some ( ) {
2507
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
2508
}
2509
let req = req.body ( Body:: from( form.unwrap_or_default ( ) ) ) .unwrap ( ) ;
c7067c7b Andy McCall 17 days ago
2510
let ( app, cookie) = ( app.clone ( ) , cookie.clone ( ) ) ;
4ac01a73 Andy McCall 1 month ago ↶
2511
async move {
c7067c7b Andy McCall 17 days ago
2512
let mut res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
2513
2514
let back =
2515
res.headers ( ) .get ( header:: LOCATION ) .and_then ( | v | v.to_str ( ) .ok ( ) ) .filter ( | l | l.contains ( " /settings/" ) ) .map ( String :: from) ;
2516
if let Some ( to) = back {
2517
let req = Request:: get( to) .header ( header:: COOKIE , cookie) .body ( Body:: empty( ) ) .unwrap ( ) ;
2518
res = oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) ;
2519
}
4ac01a73 Andy McCall 1 month ago ↶
2520
let status = res.status ( ) ;
2521
let body = String :: from_utf8( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ) .unwrap ( ) ;
2522
( status, body)
2523
}
2524
} ;
2525
let csrf_of = | page : & str | {
2526
let i = page.find ( " name=\" _csrf\" value=\" " ) .expect ( " csrf field" ) + " name=\" _csrf\" value=\" " .len ( ) ;
2527
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
2528
} ;
2529
create ( app, " andy" , " demo" ) .await;
2530
2531
2532
let ( _ , admin) = page ( " /admin" , None ) .await;
2533
let csrf = csrf_of ( & admin) ;
2534
let ( _ , body) = page ( " /admin/secrets" , Some ( format! ( " _csrf={csrf} &action=set&name=deploy-token&value=site-level-value" ) ) ) .await;
2535
assert! ( body.contains ( " deploy-token set." ) & & ! body.contains ( " site-level-value" ) , " {body}" ) ;
2536
let ( _ , body) = page ( " /admin/secrets" , Some ( format! ( " _csrf={csrf} &action=set&name=bad+name&value=x" ) ) ) .await;
2537
assert! ( body.contains ( " name is 1 to 64 of" ) , " {body}" ) ;
c7067c7b Andy McCall 17 days ago
2538
let ( _ , settings) = page ( " /andy/demo/settings/secrets" , None ) .await;
4ac01a73 Andy McCall 1 month ago ↶
2539
assert! (
2540
settings.contains ( " Secrets" ) & & settings.contains ( " <code>deploy-token</code>" ) & & settings.contains ( " the site</td>" ) ,
2541
" {settings}"
2542
) ;
2543
let csrf_r = csrf_of ( & settings) ;
2544
let ( _ , body) =
2545
page ( " /andy/demo/settings/secrets" , Some ( format! ( " _csrf={csrf_r} &action=set&name=deploy-token&value=hunter22-repo-value" ) ) ) .await;
2546
assert! ( body.contains ( " deploy-token set." ) & & body.contains ( " overridden" ) & & ! body.contains ( " hunter22" ) , " {body}" ) ;
2547
let repo = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
2548
assert! (
2549
! String :: from_utf8_lossy( & oxygenhub:: secrets:: list( & app.db, oxygenhub:: levels:: Scope:: Repo( repo.id) ) .await.unwrap ( ) [ 0 ] .value)
2550
.contains ( " hunter22" ) ,
2551
" sealed at rest"
2552
) ;
2553
2554
2555
let url = format! ( " {} /andy/demo" , h.base) ;
2556
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2557
let c = client ( & url, work.path ( ) ) ;
2558
write (
2559
work.path ( ) ,
2560
" .flow/pipeline.kdl" ,
2561
& 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 " ) ,
2562
) ;
2563
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2564
c.snapshot ( " test" ) .unwrap ( ) ;
2565
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2566
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2567
let runs = runs_of ( app, " andy" , " demo" ) .await;
2568
let jobs = match runs.first ( ) {
2569
Some ( r) => db:: flow:: jobs( & app.db, r.id) .await.unwrap ( ) .iter ( ) .map ( | j | format! ( " {} :{} " , j.name, j.status) ) .collect:: < Vec< _ > > ( ) ,
2570
None => vec! [ ] ,
2571
} ;
2572
let mut deploy = None ;
2573
for _ in 0 .. 50 {
2574
let Some ( got) = claim ( app) .await else {
2575
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
2576
continue ;
2577
} ;
2578
if got[ " job" ] [ " name" ] = = " deploy" {
2579
deploy = Some ( got) ;
2580
break ;
2581
}
2582
assert_eq! ( got[ " secrets" ] , serde_json:: Value:: Null, " {got}" ) ;
2583
}
2584
let got = deploy.unwrap_or_else ( | | panic! ( " the deploy job: runs {runs:?} jobs {jobs:?}" ) ) ;
2585
assert_eq! (
2586
got[ " secrets" ] ,
2587
serde_json:: json! ( [ { " name" : " deploy-token" , " path" : " /run/secrets/token" , " value" : " hunter22-repo-value" } ] ) ,
2588
" the repository's value, the missing one left out: {got}"
2589
) ;
2590
let id = got[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2591
let log = db:: flow:: log( & app.db, id, 0 , 100 ) .await.unwrap ( ) ;
2592
assert! ( log.iter ( ) .any ( | l | l.stream = = " flow" & & l.text.contains ( " secret \" missing\" is not set" ) ) , " {log:?}" ) ;
2593
2594
2595
let records = serde_json:: json! ( { " records" : [
2596
{ " t" : 1 , " stream" : " out" , " text" : " token=hunter22-repo-value" } ,
2597
{ " t" : 2 , " stream" : " err" , " text" : " b64 aHVudGVyMjItcmVwby12YWx1ZQ== end" } ,
2598
] } ) ;
2599
let ( st, _ ) = call ( app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( records.to_string ( ) ) ) .await;
2600
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
2601
let log = db:: flow:: log( & app.db, id, 0 , 100 ) .await.unwrap ( ) ;
2602
let texts: Vec< & str > = log.iter ( ) .map ( | l | l.text.as_str ( ) ) .collect ( ) ;
2603
assert! ( texts.contains ( & " token=***" ) & & texts.contains ( & " b64 *** end" ) , " {texts:?}" ) ;
2604
assert! ( ! texts.iter ( ) .any ( | t | t.contains ( " hunter22" ) ) , " {texts:?}" ) ;
2605
}
d1727c57 Andy McCall 1 month ago ↶
2606
2607
# [ tokio ::test ( flavor = " multi_thread" ) ]
2608
async fn a_job_s_identity_is_a_signed_token_the_discovery_document_lets_anyone_verify ( ) {
2609
let h = hub ( ) .await;
2610
let app = & h.app;
2611
let ( st, disc) = call ( app, " GET" , " /.well-known/openid-configuration" , None , None ) .await;
2612
assert_eq! ( st, StatusCode:: OK , " {disc}" ) ;
2613
let disc: serde_json:: Value = serde_json:: from_str( & disc) .unwrap ( ) ;
2614
assert_eq! ( disc[ " issuer" ] , h.base.as_str ( ) ) ;
2615
assert_eq! ( disc[ " jwks_uri" ] , format! ( " {} /.well-known/jwks.json" , h.base) ) ;
2616
let ( st, jwks) = call ( app, " GET" , " /.well-known/jwks.json" , None , None ) .await;
2617
assert_eq! ( st, StatusCode:: OK , " {jwks}" ) ;
2618
let jwks: serde_json:: Value = serde_json:: from_str( & jwks) .unwrap ( ) ;
2619
assert_eq! ( jwks[ " keys" ] [ 0 ] [ " alg" ] , " RS256" ) ;
2620
let kid = jwks[ " keys" ] [ 0 ] [ " kid" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
2621
2622
create ( app, " andy" , " demo" ) .await;
2623
let url = format! ( " {} /andy/demo" , h.base) ;
2624
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2625
let c = client ( & url, work.path ( ) ) ;
2626
write (
2627
work.path ( ) ,
2628
" .flow/pipeline.kdl" ,
2629
& pipeline ( " job \" deploy\" {\n sandbox \" alpine\" \n identity \" sts.amazonaws.com\" {\n mount-path \" /run/secrets/aws-token\" \n }\n exec \" deploy\" \n }\n " ) ,
2630
) ;
2631
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2632
c.snapshot ( " test" ) .unwrap ( ) ;
2633
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2634
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2635
let mut deploy = None ;
2636
for _ in 0 .. 50 {
2637
let Some ( got) = claim ( app) .await else {
2638
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
2639
continue ;
2640
} ;
2641
if got[ " job" ] [ " name" ] = = " deploy" {
2642
deploy = Some ( got) ;
2643
break ;
2644
}
2645
}
2646
let got = deploy.expect ( " the deploy job" ) ;
2647
let secrets = got[ " secrets" ] .as_array ( ) .expect ( " secrets" ) ;
2648
assert_eq! ( secrets.len ( ) , 1 ) ;
2649
assert_eq! (
2650
( secrets[ 0 ] [ " name" ] .as_str ( ) , secrets[ 0 ] [ " path" ] .as_str ( ) ) ,
2651
( Some ( " identity:sts.amazonaws.com" ) , Some ( " /run/secrets/aws-token" ) )
2652
) ;
2653
let token = secrets[ 0 ] [ " value" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
2654
let issuer = app.oidc ( ) .await.unwrap ( ) ;
2655
assert_eq! ( issuer.kid, kid, " the key the JWKS publishes signed it" ) ;
2656
let claims = issuer.verify ( & token, " sts.amazonaws.com" , db:: now_ms( ) ) .expect ( " verifies" ) ;
2657
assert_eq! ( claims[ " sub" ] , " repo:andy/demo:bookmark:main" ) ;
2658
assert_eq! ( claims[ " repository" ] , " andy/demo" ) ;
2659
assert_eq! ( claims[ " job" ] , " deploy" ) ;
2660
assert_eq! ( claims[ " run_number" ] , 1 ) ;
2661
assert_eq! ( claims[ " runner" ] , " test-1" ) ;
2662
assert_eq! ( claims[ " iss" ] , h.base.as_str ( ) ) ;
2663
assert! ( claims[ " state" ] .as_str ( ) .unwrap ( ) .len ( ) = = 64 ) ;
2664
2665
let id = got[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2666
let records = serde_json:: json! ( { " records" : [ { " t" : 1 , " stream" : " out" , " text" : format! ( " TOKEN={token} end" ) } ] } ) ;
2667
let ( st, _ ) = call ( app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( records.to_string ( ) ) ) .await;
2668
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
2669
let log = db:: flow:: log( & app.db, id, 0 , 100 ) .await.unwrap ( ) ;
2670
assert! ( log.iter ( ) .any ( | l | l.text = = " TOKEN=*** end" ) , " {log:?}" ) ;
2671
}
efd29a00 Andy McCall 1 month ago ↶
2672
2673
# [ tokio ::test ( flavor = " multi_thread" ) ]
2674
async fn jobs_are_offered_only_to_runners_of_their_architecture ( ) {
2675
let h = hub ( ) .await;
2676
let app = & h.app;
2677
create ( app, " andy" , " demo" ) .await;
2678
let url = format! ( " {} /andy/demo" , h.base) ;
2679
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2680
let c = client ( & url, work.path ( ) ) ;
2681
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 ) ) ) ;
2682
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2683
c.snapshot ( " test" ) .unwrap ( ) ;
2684
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2685
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2686
let claim_as = | arch : & 'static str , archs : serde_json:: Value| {
2687
let app = app.clone ( ) ;
2688
async move {
2689
let body = serde_json:: json! ( {
2690
" runner" : format! ( " {arch} -1" ) , " runtimes" : [ " microvm" ] ,
2691
" info" : { " arch" : arch, " os" : " linux" , " version" : " t" , " labels" : [ ] , " max_jobs" : 1 } ,
2692
" image_archs" : archs,
2693
} ) ;
2694
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body.to_string ( ) ) ) .await;
2695
match st {
2696
StatusCode:: NO_CONTENT => None ,
2697
StatusCode:: OK => {
2698
Some ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) )
2699
}
2700
other => panic! ( " claim: {other} {body}" ) ,
2701
}
2702
}
2703
} ;
2704
2705
2706
let rootfs = pipeline ( " " ) .split ( " cas://blob/" ) .nth ( 1 ) .unwrap ( ) [ .. 64 ] .to_string ( ) ;
2707
for _ in 0 .. 50 {
2708
if ! runs_of ( app, " andy" , " demo" ) .await.is_empty ( ) {
2709
break ;
2710
}
2711
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
2712
}
2713
let runs = runs_of ( app, " andy" , " demo" ) .await;
2714
assert_eq! ( runs.len ( ) , 1 , " {runs:?}" ) ;
2715
let mut taken = vec! [ ] ;
2716
while let Some ( j) = claim_as ( " x86_64" , serde_json:: json! ( { rootfs.clone ( ) : " x86_64" } ) ) .await {
2717
taken.push ( j) ;
2718
}
2719
assert_eq! ( taken, [ " build" , " lint" ] , " test waits for build; flash is for riscv64" ) ;
2720
2721
assert_eq! ( claim_as ( " riscv64" , serde_json:: json! ( { } ) ) .await.as_deref ( ) , Some ( " flash" ) ) ;
2722
assert_eq! ( claim_as ( " riscv64" , serde_json:: json! ( { } ) ) .await, None ) ;
2723
let ( _ , queue) = call ( app, " GET" , " /admin/runners" , None , None ) .await;
2724
let _ = queue;
2725
}
4d2e44d7 Andy McCall 1 month ago ↶
2726
2727
2728
2729
2730
2731
# [ tokio ::test ( flavor = " multi_thread" ) ]
2732
async fn a_declined_job_goes_to_another_runner_or_ends_unplaceable ( ) {
2733
let h = hub ( ) .await;
2734
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2735
create ( & h.app, " andy" , " declines" ) .await;
2736
let url = format! ( " {} /andy/declines" , h.base) ;
2737
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2738
let c = client ( & url, work.path ( ) ) ;
2739
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
2740
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2741
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2742
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2743
let run = runs_of ( & h.app, " andy" , " declines" ) .await[ 0 ] .clone ( ) ;
2744
let claim_as = | token : & 'static str , name : & 'static str | {
2745
let app = h.app.clone ( ) ;
2746
async move {
2747
let body = serde_json:: json! ( { " runner" : name, " runtimes" : [ " microvm" ] } ) .to_string ( ) ;
2748
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( token) , Some ( body) ) .await;
2749
( st = = StatusCode:: OK ) .then ( | | serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) )
2750
}
2751
} ;
2752
let decline = | token : & 'static str , id : i64 , reason : & 'static str | {
2753
let app = h.app.clone ( ) ;
2754
async move {
2755
let body = serde_json:: json! ( { " reason" : reason } ) .to_string ( ) ;
2756
call ( & app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /decline" ) , Some ( token) , Some ( body) ) .await.0
2757
}
2758
} ;
2759
let job = claim_as ( " runner-secret" , " earth-1" ) .await.expect ( " a job" ) ;
2760
2761
assert_eq! ( decline ( " runner-two" , job, " not mine" ) .await, StatusCode:: CONFLICT , " only the runner it runs on may decline it" ) ;
2762
assert_eq! ( decline ( " runner-secret" , job, " the MicroVM tier needs /dev/kvm" ) .await, StatusCode:: NO_CONTENT ) ;
2763
let j = db:: flow:: job( & h.app.db, job) .await.unwrap ( ) .unwrap ( ) ;
2764
assert_eq! ( ( j.status.as_str ( ) , j.runner.as_str ( ) ) , ( " queued" , " " ) ) ;
2765
assert_eq! ( j.waiting, " declined by earth-1: the MicroVM tier needs /dev/kvm; waiting for another runner" ) ;
2766
2767
let next = claim_as ( " runner-secret" , " earth-1" ) .await.expect ( " the other ready job" ) ;
2768
assert_ne! ( next, job) ;
2769
let page = call ( & h.app, " GET" , & format! ( " /andy/declines/flow/{} " , run.number) , None , None ) .await.1 ;
2770
assert! ( page.contains ( " Waiting: declined by earth-1: the MicroVM tier needs /dev/kvm" ) , " {page}" ) ;
2771
assert_eq! ( claim_as ( " runner-two" , " plex-1" ) .await, Some ( job) , " another runner takes it" ) ;
2772
assert_eq! ( decline ( " runner-two" , job, " rootfs 36fc4e323804 is not on this runner" ) .await, StatusCode:: NO_CONTENT ) ;
2773
assert_eq! ( claim_as ( " runner-three" , " narvi" ) .await, Some ( job) ) ;
2774
assert_eq! ( decline ( " runner-three" , job, " rootfs 36fc4e323804 is for x86_64; this runner is riscv64" ) .await, StatusCode:: NO_CONTENT ) ;
2775
let j = db:: flow:: job( & h.app.db, job) .await.unwrap ( ) .unwrap ( ) ;
2776
assert_eq! ( j.status, " error" ) ;
2777
assert_eq! (
2778
j.error,
2779
" 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"
2780
) ;
2781
let r = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " plex-1" ) .unwrap ( ) ;
2782
let declined = db:: flow:: declines_of_runner( & h.app.db, r.id, 10 ) .await.unwrap ( ) ;
2783
assert_eq! ( declined.len ( ) , 1 ) ;
2784
assert_eq! ( ( declined[ 0 ] .job_id, declined[ 0 ] .reason.as_str ( ) ) , ( job, " rootfs 36fc4e323804 is not on this runner" ) ) ;
2785
assert_eq! ( r.tier_list ( ) , vec! [ " microvm" ] ) ;
2786
}
554ccacf Andy McCall 1 month ago ↶
2787
ab4df6aa Andy McCall 12 days ago ↶
2788
2789
2790
2791
2792
# [ tokio ::test ( flavor = " multi_thread" ) ]
2793
async fn a_drained_job_goes_back_ahead_of_new_work ( ) {
2794
let h = hub ( ) .await;
2795
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2796
create ( & h.app, " andy" , " drains" ) .await;
2797
let url = format! ( " {} /andy/drains" , h.base) ;
2798
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2799
let c = client ( & url, work.path ( ) ) ;
e4203e14 Andy McCall 12 days ago ↶
2800
let bulk = " job \" bulk\" {\n sandbox \" alpine\" \n priority \" low\" \n checkpoint \" /src/state\" \n exec \" true\" \n }\n " ;
ab4df6aa Andy McCall 12 days ago ↶
2801
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( bulk) ) ;
2802
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2803
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2804
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2805
runs_of ( & h.app, " andy" , " drains" ) .await;
2806
let claim_as = | token : & 'static str , name : & 'static str | {
2807
let app = h.app.clone ( ) ;
2808
async move {
2809
let body = serde_json:: json! ( { " runner" : name, " runtimes" : [ " microvm" ] } ) .to_string ( ) ;
2810
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( token) , Some ( body) ) .await;
2811
( st = = StatusCode:: OK ) .then ( | | serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) )
2812
}
2813
} ;
2814
let release = | token : & 'static str , id : i64 , reason : & 'static str | {
2815
let app = h.app.clone ( ) ;
2816
async move {
2817
let body = serde_json:: json! ( { " reason" : reason } ) .to_string ( ) ;
2818
call ( & app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /release" ) , Some ( token) , Some ( body) ) .await.0
2819
}
2820
} ;
2821
let name_of = | id : i64 | {
2822
let app = h.app.clone ( ) ;
2823
async move { db:: flow:: job( & app.db, id) .await.unwrap ( ) .unwrap ( ) .name }
2824
} ;
2825
let build = claim_as ( " runner-secret" , " earth-1" ) .await.unwrap ( ) ;
2826
let lint = claim_as ( " runner-two" , " plex-1" ) .await.unwrap ( ) ;
2827
let low = claim_as ( " runner-three" , " phone" ) .await.unwrap ( ) ;
2828
assert_eq! ( ( name_of ( build) .await, name_of ( lint) .await, name_of ( low) .await) , ( " build" .into ( ) , " lint" .into ( ) , " bulk" .into ( ) ) ) ;
2829
2830
let line = serde_json:: json! ( { " records" : [ { " t" : 1200 , " stream" : " stdout" , " text" : " step 1 of 30" } ] } ) .to_string ( ) ;
2831
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{low} /log" ) , Some ( " runner-three" ) , Some ( line) ) .await;
2832
assert_eq! ( finish ( & h.app, build, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 } ) ) .await, StatusCode:: NO_CONTENT ) ;
e4203e14 Andy McCall 12 days ago ↶
2833
2834
let ( st, body) = call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{lint} /checkpoint" ) , Some ( " runner-two" ) , Some ( " x" .into ( ) ) ) .await;
2835
assert_eq! ( st, StatusCode:: BAD_REQUEST , " lint declares no checkpoint: {body}" ) ;
2836
let ( st, body) =
2837
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{low} /checkpoint" ) , Some ( " runner-three" ) , Some ( " step 32" .into ( ) ) ) .await;
2838
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
2839
let saved = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
ab4df6aa Andy McCall 12 days ago ↶
2840
assert_eq! ( release ( " runner-two" , low, " off Wi-Fi" ) .await, StatusCode:: CONFLICT , " only the runner that holds it" ) ;
2841
assert_eq! ( release ( " runner-three" , low, " off Wi-Fi" ) .await, StatusCode:: NO_CONTENT ) ;
2842
let j = db:: flow:: job( & h.app.db, low) .await.unwrap ( ) .unwrap ( ) ;
2843
assert_eq! ( ( j.status.as_str ( ) , j.runner.as_str ( ) , j.handbacks) , ( " queued" , " " , 1 ) ) ;
2844
let log: Vec< String> = db:: flow:: log( & h.app.db, low, 0 , 50 ) .await.unwrap ( ) .into_iter ( ) .map ( | l | l.text ) .collect ( ) ;
2845
assert_eq! ( log, [ " step 1 of 30" , " ── handed back by phone: off Wi-Fi; queued again ahead of new work" ] ) ;
2846
assert! ( db:: flow:: declines_of_job( & h.app.db, low) .await.unwrap ( ) .is_empty ( ) , " not a decline" ) ;
2847
2848
e4203e14 Andy McCall 12 days ago ↶
2849
let body = serde_json:: json! ( { " runner" : " phone" , " runtimes" : [ " microvm" ] } ) .to_string ( ) ;
2850
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-three" ) , Some ( body) ) .await;
2851
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
2852
let again: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
2853
assert_eq! ( ( again[ " job_id" ] .as_i64 ( ) , again[ " checkpoint" ] .as_str ( ) ) , ( Some ( low) , Some ( saved.as_str ( ) ) ) , " it resumes: {again}" ) ;
2854
let ( st, bytes) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/jobs/{low} /checkpoint" ) , Some ( " runner-three" ) , None ) .await;
2855
assert_eq! ( ( st, bytes.as_str ( ) ) , ( StatusCode:: OK , " step 32" ) ) ;
2856
assert! ( db:: flow:: artifacts( & h.app.db, low) .await.unwrap ( ) .is_empty ( ) , " a checkpoint is no artifact of the job" ) ;
ab4df6aa Andy McCall 12 days ago ↶
2857
assert_eq! ( claim_as ( " runner-secret" , " earth-1" ) .await.map ( | id | id ! = low ) , Some ( true ) , " then new work" ) ;
2858
2859
2860
let phone = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " phone" ) .unwrap ( ) ;
2861
sqlx:: query( " UPDATE flow_runners SET last_seen_ms = $1 WHERE id = $2" )
2862
.bind ( db:: now_ms( ) - oxygenhub:: flow:: LEASE_MS - 1000 )
2863
.bind ( phone.id)
2864
.execute ( & h.app.db)
2865
.await
2866
.unwrap ( ) ;
2867
assert_eq! ( oxygenhub:: flow:: requeue_lapsed( & h.app) .await.unwrap ( ) , 1 ) ;
2868
let j = db:: flow:: job( & h.app.db, low) .await.unwrap ( ) .unwrap ( ) ;
2869
assert_eq! ( ( j.status.as_str ( ) , j.handbacks) , ( " queued" , 2 ) ) ;
2870
let log: Vec< String> = db:: flow:: log( & h.app.db, low, 0 , 50 ) .await.unwrap ( ) .into_iter ( ) .map ( | l | l.text ) .collect ( ) ;
2871
assert_eq! ( log.last ( ) .unwrap ( ) , " ── the runner phone has not been heard from for 3 minutes; queued again ahead of new work" ) ;
2872
assert_eq! ( claim_as ( " runner-two" , " plex-1" ) .await, Some ( low) , " another runner takes it" ) ;
2873
}
2874
3b650652 Andy McCall 12 days ago ↶
2875
2876
2877
2878
# [ tokio ::test ( flavor = " multi_thread" ) ]
2879
async fn a_claim_carries_the_job_s_expected_time ( ) {
2880
let h = hub ( ) .await;
2881
create ( & h.app, " andy" , " eta" ) .await;
2882
let url = format! ( " {} /andy/eta" , h.base) ;
2883
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2884
let c = client ( & url, work.path ( ) ) ;
2885
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 ) ;
2886
write ( work.path ( ) , " .flow/pipeline.kdl" , & one) ;
2887
c.commit ( " One job" , & [ ] ) .unwrap ( ) ;
2888
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2889
runs_of ( & h.app, " andy" , " eta" ) .await;
2890
let first = claim ( & h.app) .await.expect ( " a job" ) ;
2891
assert! ( first.get ( " expected_ms" ) .is_none ( ) , " never passed: no estimate, {first}" ) ;
2892
let id = first[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2893
assert_eq! ( finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 } ) ) .await, StatusCode:: NO_CONTENT ) ;
2894
2895
sqlx:: query( " UPDATE flow_jobs SET started_ms = finished_ms - 42000 WHERE id = $1" ) .bind ( id) .execute ( & h.app.db) .await.unwrap ( ) ;
2896
write ( work.path ( ) , " README" , " again\n " ) ;
2897
c.commit ( " Again" , & [ ] ) .unwrap ( ) ;
2898
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2899
let mut next = None ;
2900
for _ in 0 .. 100 {
2901
if let Some ( j) = claim ( & h.app) .await {
2902
next = Some ( j) ;
2903
break ;
2904
}
2905
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2906
}
2907
let next = next.expect ( " the second run's job" ) ;
2908
assert_eq! ( next[ " expected_ms" ] , 42_000 , " {next}" ) ;
2909
}
2910
554ccacf Andy McCall 1 month ago ↶
2911
2912
2913
2914
# [ tokio ::test ( flavor = " multi_thread" ) ]
2915
async fn a_coverage_artifact_becomes_a_coverage_report ( ) {
2916
let h = hub ( ) .await;
2917
create ( & h.app, " andy" , " cov" ) .await;
2918
let url = format! ( " {} /andy/cov" , h.base) ;
2919
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2920
let c = client ( & url, work.path ( ) ) ;
2921
write (
2922
work.path ( ) ,
2923
" .flow/pipeline.kdl" ,
2924
& format! (
2925
" 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 "
2926
) ,
2927
) ;
2928
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 " ) ;
2929
c.snapshot ( " test" ) .unwrap ( ) ;
2930
c.commit ( " Code and coverage" , & [ ] ) .unwrap ( ) ;
2931
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2932
let run = runs_of ( & h.app, " andy" , " cov" ) .await.remove ( 0 ) ;
c8df33a6 Andy McCall 26 days ago ↶
2933
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 ↶
2934
let id = claim ( & h.app) .await.unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2935
2936
let ( st, text) = upload ( & h.app, id, " coverage" , b " this is not a report" ) .await;
2937
assert_eq! ( st, StatusCode:: OK , " a bad report never fails the upload: {text}" ) ;
2938
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 \
2939
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 " ;
2940
let ( st, text) = upload ( & h.app, id, " coverage" , lcov.as_bytes ( ) ) .await;
2941
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
2942
let log: Vec< String> = db:: flow:: log( & h.app.db, id, 0 , 50 ) .await.unwrap ( ) .into_iter ( ) .map ( | l | l.text ) .collect ( ) ;
2943
assert! ( log.iter ( ) .any ( | l | l.contains ( " coverage: this is not an LCOV report" ) ) , " {log:?}" ) ;
2944
assert! (
2945
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)" ) ,
2946
" {log:?}"
2947
) ;
2948
2949
let ( st, page) = call ( & h.app, " GET" , " /andy/cov/coverage" , None , None ) .await;
2950
assert_eq! ( st, StatusCode:: OK ) ;
c987c865 Andy McCall 1 month ago ↶
2951
assert! (
c8df33a6 Andy McCall 26 days ago ↶
2952
page.contains ( " >Reports</a>" )
c987c865 Andy McCall 1 month ago ↶
2953
& & page.contains ( " 50.0%" )
2954
& & page.contains ( " <strong>2</strong> of <strong>4</strong> relevant lines covered" ) ,
2955
" {page}"
2956
) ;
554ccacf Andy McCall 1 month ago ↶
2957
assert! ( page.contains ( " 1 of 2" ) & & page.contains ( " /andy/cov/coverage/" ) & & page.contains ( " /file/src/lib.rs" ) , " {page}" ) ;
2958
let report = crate_report ( & h.app, run.id) .await;
2959
let ( st, file) = call ( & h.app, " GET" , & format! ( " /andy/cov/coverage/{report} /file/src/lib.rs" ) , None , None ) .await;
2960
assert_eq! ( st, StatusCode:: OK , " {file}" ) ;
2961
assert_eq! ( file.matches ( " class=\" cov-hit\" " ) .count ( ) , 2 , " {file}" ) ;
2962
assert_eq! ( file.matches ( " class=\" cov-miss\" " ) .count ( ) , 2 , " {file}" ) ;
2963
assert! ( file.contains ( " 3×" ) , " {file}" ) ;
2964
assert_eq! ( call ( & h.app, " GET" , & format! ( " /andy/cov/coverage/{report} /file/src/gone.rs" ) , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
2965
let ( _ , badge) = call ( & h.app, " GET" , " /andy/cov/coverage/badge.svg" , None , None ) .await;
2966
assert! ( badge.contains ( " coverage: 50.0%" ) , " {badge}" ) ;
2967
let ( _ , run_page) = call ( & h.app, " GET" , & format! ( " /andy/cov/flow/{} " , run.number) , None , None ) .await;
2968
assert! ( run_page.contains ( " coverage 50.0%" ) , " {run_page}" ) ;
617c2840 Andy McCall 1 month ago ↶
2969
2970
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/cov/coverage" , None , None ) .await;
2971
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
2972
assert_eq! ( ( list[ 0 ] [ " percent" ] .as_f64 ( ) , list[ 0 ] [ " format" ] .as_str ( ) ) , ( Some ( 50. 0 ) , Some ( " lcov" ) ) , " {list}" ) ;
2973
let ( _ , one) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/cov/coverage/{report} " ) , None , None ) .await;
2974
let one: serde_json:: Value = serde_json:: from_str( & one) .unwrap ( ) ;
2975
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 ↶
2976
2977
2978
2979
let row = db:: repos:: get( & h.app.db, " andy" , " cov" ) .await.unwrap ( ) .unwrap ( ) ;
2980
let check = | checks : Vec< db:: hooks:: Check> | checks.into_iter ( ) .find ( | c | c.name = = " coverage" ) .expect ( " a coverage check" ) ;
2981
let c = check ( db:: hooks:: checks_for_state( & h.app.db, row.id, & run.state) .await.unwrap ( ) ) ;
2982
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " passed" , " 50.0%" ) ) ;
2983
assert! ( c.url.ends_with ( & format! ( " /andy/cov/coverage?report={report} " ) ) , " {}" , c.url) ;
2984
oxygenhub:: coverage:: set_rules( & h.app.db, row.id, oxygenhub:: coverage:: Rules { min: Some ( 60. 0 ) , max_drop: None } ) .await.unwrap ( ) ;
2985
assert_eq! ( upload ( & h.app, id, " coverage" , lcov.as_bytes ( ) ) .await.0 , StatusCode:: OK ) ;
2986
let c = check ( db:: hooks:: checks_for_state( & h.app.db, row.id, & run.state) .await.unwrap ( ) ) ;
2987
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " failed" , " 50.0%: below the minimum of 60%" ) ) ;
554ccacf Andy McCall 1 month ago ↶
2988
}
2989
2990
async fn crate_report ( app : & Shared, run_id : i64 ) -> i64 {
2991
oxygenhub:: coverage:: for_run( & app.db, run_id) .await.unwrap ( ) [ 0 ] .id
2992
}
36e5a9c1 Andy McCall 1 month ago ↶
2993
2994
2995
2996
# [ tokio ::test ( flavor = " multi_thread" ) ]
2997
async fn junit_reports_make_a_tests_tab ( ) {
2998
let h = hub ( ) .await;
2999
create ( & h.app, " andy" , " t" ) .await;
3000
let url = format! ( " {} /andy/t" , h.base) ;
3001
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3002
let c = client ( & url, work.path ( ) ) ;
3003
write (
3004
work.path ( ) ,
3005
" .flow/pipeline.kdl" ,
3006
& format! (
3007
" 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 "
3008
) ,
3009
) ;
c8df33a6 Andy McCall 26 days ago ↶
3010
assert! ( ! call ( & h.app, " GET" , " /andy/t" , None , None ) .await.1. contains ( " >Reports</a>" ) ) ;
36e5a9c1 Andy McCall 1 month ago ↶
3011
let case = | name : & str , ok : bool , secs : & str | {
3012
if ok {
3013
format! ( r # "<testcase classname="t.suite" name="{name} " time="{secs} "/>"# )
3014
} else {
3015
format! (
3016
r # "<testcase classname="t.suite" name="{name} " time="{secs} "><failure message="{name} broke">AssertionError</failure></testcase>"#
3017
)
3018
}
3019
} ;
3020
let runs = [
3021
vec! [ case ( " a" , true , " 0.1" ) , case ( " b" , true , " 2.5" ) , case ( " c" , true , " 0.2" ) ] ,
3022
vec! [ case ( " a" , true , " 0.1" ) , case ( " b" , false , " 2.6" ) , case ( " c" , true , " 0.2" ) , case ( " d" , true , " 0.3" ) ] ,
3023
vec! [ case ( " a" , true , " 0.1" ) , case ( " b" , true , " 2.4" ) , case ( " c" , true , " 0.2" ) , case ( " d" , true , " 0.3" ) ] ,
3024
] ;
3025
for ( i, cases) in runs.iter ( ) .enumerate ( ) {
3026
write ( work.path ( ) , " n.txt" , & i.to_string ( ) ) ;
3027
c.snapshot ( " test" ) .unwrap ( ) ;
3028
c.commit ( & format! ( " Run {i} " ) , & [ ] ) .unwrap ( ) ;
3029
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3030
3031
let mut a = None ;
3032
for _ in 0 .. 100 {
3033
a = claim ( & h.app) .await;
3034
if a.is_some ( ) {
3035
break ;
3036
}
3037
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3038
}
3039
let a = a.expect ( " a job to claim" ) ;
3040
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3041
let xml = format! ( " <testsuites><testsuite name=\" t\" >{} </testsuite></testsuites>" , cases.concat ( ) ) ;
3042
assert_eq! ( upload ( & h.app, id, " junit" , xml.as_bytes ( ) ) .await.0 , StatusCode:: OK ) ;
3043
let failed = cases.iter ( ) .any ( | c | c.contains ( " <failure" ) ) ;
3044
let mut r = receipt ( " test" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , failed as u32 ) ;
3045
if ! failed {
3046
r[ " outputs" ] = serde_json:: json! ( [ oxy_core:: repo:: Repo:: blob_ref( xml.as_bytes ( ) ) .to_hex ( ) ] ) ;
3047
}
3048
let code = failed as i32 ;
3049
assert_eq! (
3050
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : code, " duration_ms" : 700 , " receipt" : r } ) ) .await,
3051
StatusCode:: NO_CONTENT
3052
) ;
3053
}
3054
let ( st, page) = call ( & h.app, " GET" , " /andy/t/tests" , None , None ) .await;
3055
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
3056
if let Some ( out) = std:: env:: var_os( " OXYHUB_DUMP_TESTS_PAGE" ) {
3057
std:: fs:: write( out, & page) .unwrap ( ) ;
3058
}
c8df33a6 Andy McCall 26 days ago ↶
3059
assert! ( page.contains ( " >Reports</a>" ) , " the tab" ) ;
36e5a9c1 Andy McCall 1 month ago ↶
3060
assert! ( page.contains ( " 100%" ) & & page.contains ( " of 4 tests passed" ) , " the latest run passes" ) ;
3061
assert! ( page.contains ( " 1 fixed" ) & & page.contains ( " t.suite::b" ) , " b fixed against the run before" ) ;
3062
assert! ( page.contains ( " 1 ✕" ) & & page.contains ( " 2 ✓" ) , " b is flaky: {page}" ) ;
3063
assert! ( page.contains ( " <svg class=\" cov-chart\" " ) , " charts" ) ;
3064
3065
let slow = page.find ( " Slowest tests" ) .unwrap ( ) ;
3066
assert! ( page[ slow.. ] .find ( " t.suite::b" ) .unwrap ( ) < page[ slow.. ] .find ( " t.suite::d" ) .unwrap ( ) ) ;
3067
3068
let row = db:: repos:: get( & h.app.db, " andy" , " t" ) .await.unwrap ( ) .unwrap ( ) ;
3069
let second = oxygenhub:: junit:: on_bookmark( & h.app.db, row.id, " main" , 10 ) .await.unwrap ( ) [ 1 ] .id;
3070
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/t/tests?report={second} " ) , None , None ) .await;
3071
assert! ( page.contains ( " 1 failing" ) & & page.contains ( " Newly failing" ) & & page.contains ( " b broke" ) , " {page}" ) ;
3072
assert! ( page.contains ( " 1 new tests" ) , " d is new" ) ;
617c2840 Andy McCall 1 month ago ↶
3073
3074
3075
3076
let ( st, list) = call ( & h.app, " GET" , " /api/v1/repos/andy/t/tests" , None , None ) .await;
3077
assert_eq! ( st, StatusCode:: OK , " {list}" ) ;
3078
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
3079
assert_eq! ( list.as_array ( ) .unwrap ( ) .len ( ) , 3 ) ;
3080
assert_eq! ( ( list[ 0 ] [ " tests" ] .as_i64 ( ) , list[ 1 ] [ " failed" ] .as_i64 ( ) ) , ( Some ( 4 ) , Some ( 1 ) ) ) ;
3081
let ( _ , one) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/t/tests/{second} ?status=failed" ) , None , None ) .await;
3082
let one: serde_json:: Value = serde_json:: from_str( & one) .unwrap ( ) ;
3083
assert_eq! ( one[ " cases" ] .as_array ( ) .unwrap ( ) .len ( ) , 1 ) ;
3084
assert_eq! ( ( one[ " cases" ] [ 0 ] [ " name" ] .as_str ( ) , one[ " cases" ] [ 0 ] [ " message" ] .as_str ( ) ) , ( Some ( " b" ) , Some ( " b broke" ) ) ) ;
3085
let ( _ , all) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/t/tests/{second} " ) , None , None ) .await;
3086
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & all) .unwrap ( ) [ " cases" ] .as_array ( ) .unwrap ( ) .len ( ) , 4 ) ;
3087
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 ↶
3088
3089
3090
let reports = oxygenhub:: junit:: on_bookmark( & h.app.db, row.id, " main" , 10 ) .await.unwrap ( ) ;
3091
let check = | state : String| {
3092
let app = h.app.clone ( ) ;
3093
async move { db:: hooks:: checks_for_state( & app.db, row.id, & state) .await.unwrap ( ) .into_iter ( ) .find ( | c | c.name = = " tests" ) .unwrap ( ) }
3094
} ;
3095
let c = check ( reports[ 1 ] .state.clone ( ) ) .await;
3096
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " failed" , " 1 failing; 1 new against main: t.suite::b" ) ) ;
3097
let c = check ( reports[ 0 ] .state.clone ( ) ) .await;
3098
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " passed" , " 4 passed" ) ) ;
3099
3100
3101
let ( _ , tab) = call ( & h.app, " GET" , " /andy/t/tests" , None , None ) .await;
3102
assert! (
3103
tab.contains ( " /andy/t/tests/case?classname=t.suite&name=b" ) | | tab.contains ( " /andy/t/tests/case?classname=t.suite&name=b" ) ,
3104
" linked"
3105
) ;
3106
let ( st, hist) = call ( & h.app, " GET" , " /andy/t/tests/case?classname=t.suite&name=b" , None , None ) .await;
3107
assert_eq! ( st, StatusCode:: OK , " {hist}" ) ;
3108
assert! ( hist.contains ( " 2 passed" ) & & hist.contains ( " 1 failed" ) & & hist.contains ( " b broke" ) , " {hist}" ) ;
3109
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 ↶
3110
}
67040b15 Andy McCall 26 days ago ↶
3111
3112
3113
3114
# [ tokio ::test ( flavor = " multi_thread" ) ]
3115
async fn a_job_no_runner_can_take_says_why ( ) {
3116
let h = hub ( ) .await;
3117
create ( & h.app, " andy" , " demo" ) .await;
3118
let url = format! ( " {} /andy/demo" , h.base) ;
3119
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3120
let c = client ( & url, work.path ( ) ) ;
3121
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3122
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3123
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
3124
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3125
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
3126
3127
3128
let r = db:: runners:: configured( & h.app.db, & blake3:: hash( b " elsewhere" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
3129
db:: runners:: seen( & h.app.db, r.id, None , None ) .await.unwrap ( ) ;
3130
let other = " ab" .repeat ( 32 ) ;
92a797b2 Andy McCall 26 days ago ↶
3131
db:: runners:: set_offer( & h.app.db, r.id, & [ " microvm" .to_string ( ) ] , Some ( & [ other] ) , false , " p" ) .await.unwrap ( ) ;
67040b15 Andy McCall 26 days ago ↶
3132
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3133
3134
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
3135
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
3136
let build = v[ " jobs" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | j | j[ " name" ] = = " build" ) .unwrap ( ) .clone ( ) ;
3137
let why = build[ " waiting" ] .as_str ( ) .unwrap_or_default ( ) .to_string ( ) ;
3138
assert! ( why.contains ( " earth-9 does not hold image" ) & & why.contains ( & ROOTFS [ .. 12 ] ) , " {body}" ) ;
3139
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/1" , None , None ) .await;
3140
assert! ( page.contains ( " data-waiting" ) & & page.contains ( " does not hold image" ) , " the run page says it" ) ;
13b27cb2 Andy McCall 19 days ago ↶
3141
3142
3143
3144
let mut summary = String :: new( ) ;
3145
for _ in 0 .. 40 {
3146
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
3147
summary = checks.iter ( ) .find ( | c | c.name = = " flow/build" ) .unwrap ( ) .summary.clone ( ) ;
3148
if summary.contains ( " does not hold image" ) {
3149
break ;
3150
}
3151
let build = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) .into_iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) ;
3152
db:: flow:: set_waiting( & h.app.db, build.id, " " ) .await.unwrap ( ) ;
3153
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3154
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3155
}
3156
assert! ( summary.contains ( " does not hold image" ) , " {summary}" ) ;
67040b15 Andy McCall 26 days ago ↶
3157
}
92a797b2 Andy McCall 26 days ago ↶
3158
58cbf424 Andy McCall 19 days ago ↶
3159
3160
3161
3162
# [ tokio ::test ( flavor = " multi_thread" ) ]
3163
async fn a_job_waiting_for_a_busy_runner_says_so ( ) {
3164
let h = hub ( ) .await;
3165
create ( & h.app, " andy" , " demo" ) .await;
3166
let url = format! ( " {} /andy/demo" , h.base) ;
3167
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3168
let c = client ( & url, work.path ( ) ) ;
3169
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3170
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3171
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
3172
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3173
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
3174
3175
let r = db:: runners:: configured( & h.app.db, & blake3:: hash( b " busy" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
3176
db:: runners:: seen( & h.app.db, r.id, None , None ) .await.unwrap ( ) ;
3177
db:: runners:: set_offer( & h.app.db, r.id, & [ " microvm" .to_string ( ) ] , Some ( & [ ROOTFS .to_string ( ) ] ) , false , " p" ) .await.unwrap ( ) ;
3178
sqlx:: query(
3179
" UPDATE flow_jobs SET status = 'running', runner_id = $1, runner = 'earth-9', started_ms = $2 WHERE run_id = $3 AND name = 'lint'" ,
3180
)
3181
.bind ( r.id)
3182
.bind ( db:: now_ms( ) )
3183
.bind ( run.id)
3184
.execute ( & h.app.db)
3185
.await
3186
.unwrap ( ) ;
3187
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3188
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
3189
let build = jobs.iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) ;
3190
assert! ( build.waiting.contains ( " the runners that can take it are busy: earth-9 busy (1 of 1)" ) , " {}" , build.waiting) ;
3191
3192
sqlx:: query( " UPDATE flow_runners SET max_jobs = 2 WHERE id = $1" ) .bind ( r.id) .execute ( & h.app.db) .await.unwrap ( ) ;
3193
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3194
let build = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) .into_iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) ;
5d66f27c Andy McCall 10 days ago ↶
3195
assert! ( build.waiting.contains ( " earth-9 has a free slot (1 of 2) but not the memory, CPUs or disk for it now" ) , " {}" , build.waiting) ;
58cbf424 Andy McCall 19 days ago ↶
3196
}
3197
794ff94f Andy McCall 13 days ago ↶
3198
3199
3200
# [ tokio ::test ( flavor = " multi_thread" ) ]
3201
async fn an_expedited_run_moves_the_queue_at_once ( ) {
3202
let h = hub ( ) .await;
3203
for name in [ " first" , " urgent" ] {
3204
create ( & h.app, " andy" , name) .await;
3205
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3206
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
3207
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3208
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3209
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
3210
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3211
3212
assert_eq! ( runs_of ( & h.app, " andy" , name) .await.len ( ) , 1 ) ;
3213
}
3214
3215
let r = db:: runners:: configured( & h.app.db, & blake3:: hash( b " busy" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
3216
db:: runners:: seen( & h.app.db, r.id, None , None ) .await.unwrap ( ) ;
3217
db:: runners:: set_offer( & h.app.db, r.id, & [ " microvm" .to_string ( ) ] , Some ( & [ ROOTFS .to_string ( ) ] ) , false , " p" ) .await.unwrap ( ) ;
3218
let first = runs_of ( & h.app, " andy" , " first" ) .await.remove ( 0 ) ;
3219
sqlx:: query(
3220
" UPDATE flow_jobs SET status = 'running', runner_id = $1, runner = 'earth-9', started_ms = $2 WHERE run_id = $3 AND name = 'lint'" ,
3221
)
3222
.bind ( r.id)
3223
.bind ( db:: now_ms( ) )
3224
.bind ( first.id)
3225
.execute ( & h.app.db)
3226
.await
3227
.unwrap ( ) ;
3228
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3229
let db = h.app.db.clone ( ) ;
3230
let build = | run : i64 | {
3231
let db = db.clone ( ) ;
3232
async move { db:: flow:: jobs( & db, run) .await.unwrap ( ) .into_iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) }
3233
} ;
3234
let urgent = runs_of ( & h.app, " andy" , " urgent" ) .await.remove ( 0 ) ;
3235
assert! ( build ( urgent.id) .await.waiting.contains ( " 1 ready job comes first" ) , " {}" , build ( urgent.id) .await.waiting) ;
3236
3237
3238
let res = oxygenhub:: router( h.app.clone ( ) )
3239
.oneshot (
3240
Request:: post( " /register" )
3241
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
3242
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
3243
.unwrap ( ) ,
3244
)
3245
.await
3246
.unwrap ( ) ;
3247
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
3248
let page = oxygenhub:: router( h.app.clone ( ) )
3249
.oneshot ( Request:: get( " /andy/urgent/flow/1" ) .header ( header:: COOKIE , cookie.clone ( ) ) .body ( Body:: empty( ) ) .unwrap ( ) )
3250
.await
3251
.unwrap ( ) ;
3252
let page = String :: from_utf8_lossy( & page.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
3253
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
3254
let csrf = page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
3255
let res = oxygenhub:: router( h.app.clone ( ) )
3256
.oneshot (
3257
Request:: post( " /andy/urgent/flow/1/expedite" )
3258
.header ( header:: COOKIE , cookie.clone ( ) )
3259
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
3260
.body ( Body:: from( format! ( " _csrf={csrf} &reason=incident" ) ) )
3261
.unwrap ( ) ,
3262
)
3263
.await
3264
.unwrap ( ) ;
3265
assert! ( res.status ( ) .is_redirection ( ) , " {}" , res.status ( ) ) ;
3266
assert! ( ! build ( urgent.id) .await.waiting.contains ( " come" ) , " first now: {}" , build ( urgent.id) .await.waiting) ;
3267
let w = build ( first.id) .await.waiting;
3268
assert! ( w.contains ( " 2 ready jobs come first (after expedited run andy/urgent #1)" ) , " {w}" ) ;
b1860d60 Andy McCall 10 days ago ↶
3269
3270
3271
3272
let andy = db:: accounts:: account_by_name( & h.app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
3273
let ( token, hash) = oxygenhub:: auth:: new_token( ) ;
3274
db:: accounts:: create_token( & h.app.db, andy.id, " phone" , & hash, " repo:read,repo:write" , " " , None ) .await.unwrap ( ) ;
3275
let url = format! ( " /api/v1/repos/andy/first/flow/runs/{} /expedite" , first.number) ;
3276
let ( st, body) = call ( & h.app, " POST" , & url, Some ( & token) , Some ( r # "{"reason":" "}"# .into ( ) ) ) .await;
3277
assert_eq! ( st, StatusCode:: CONFLICT , " {body}" ) ;
3278
assert! ( body.contains ( " Say why the run is urgent." ) , " {body}" ) ;
3279
let ( st, body) = call ( & h.app, " POST" , & url, Some ( & token) , Some ( r # "{"reason":"the fix for the outage"}"# .into ( ) ) ) .await;
3280
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3281
let run = db:: flow:: run( & h.app.db, first.repo_id, first.number) .await.unwrap ( ) .unwrap ( ) ;
3282
assert! (
3283
run.priority = = " urgent" & & run.priority_note.contains ( " expedited by andy: the fix for the outage" ) ,
3284
" {} {}" ,
3285
run.priority,
3286
run.priority_note
3287
) ;
794ff94f Andy McCall 13 days ago ↶
3288
}
3289
92a797b2 Andy McCall 26 days ago ↶
3290
async fn put_bytes ( app : & Shared, path : & str , bearer : & str , bytes : Vec< u8 > ) -> ( StatusCode, String) {
3291
let req = Request:: put( path)
3292
.header ( header:: AUTHORIZATION , format! ( " Bearer {bearer} " ) )
3293
.header ( header:: CONTENT_TYPE , " application/octet-stream" )
3294
.body ( Body:: from( bytes) )
3295
.unwrap ( ) ;
3296
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
3297
let st = res.status ( ) ;
3298
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
3299
}
3300
3301
3302
3303
3304
951aed50 Andy McCall 11 days ago ↶
3305
3306
3307
92a797b2 Andy McCall 26 days ago ↶
3308
# [ tokio ::test ( flavor = " multi_thread" ) ]
951aed50 Andy McCall 11 days ago ↶
3309
async fn a_runner_offers_the_manifest_of_an_image_the_hub_lacks ( ) {
3310
let h = hub ( ) .await;
3311
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3312
let image: Vec< u8 > = ( 0 .. 300_000 u32 ) .map ( | i | ( i % 241 ) as u8 ) .collect ( ) ;
3313
let mut chunks = std:: collections:: HashMap:: new( ) ;
3314
let m = jet:: images:: scan( & image[ .. ] , Some ( " x86_64" .into ( ) ) , | c , b | {
3315
chunks.insert ( c.hash ( ) .to_string ( ) , b.to_vec ( ) ) ;
3316
Ok ( ( ) )
3317
} )
3318
.unwrap ( ) ;
3319
let hash = m.hash.clone ( ) ;
3320
3321
let held = serde_json:: json! ( { " images" : [ hash.clone ( ) , " not-a-hash" ] } ) .to_string ( ) ;
3322
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/images/held" , Some ( " runner-secret" ) , Some ( held.clone ( ) ) ) .await;
3323
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3324
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " wanted" ] , serde_json:: json! ( [ hash.clone ( ) ] ) ) ;
3325
3326
assert_ne! ( call ( & h.app, " POST" , " /api/v1/flow/images/held" , Some ( " site" ) , Some ( held.clone ( ) ) ) .await.0 , StatusCode:: OK ) ;
3327
3328
let manifest = serde_json:: to_string( & m) .unwrap ( ) ;
3329
let wrong = format! ( " /api/v1/flow/images/{} /held" , " ab" .repeat ( 32 ) ) ;
3330
assert_eq! ( call ( & h.app, " PUT" , & wrong, Some ( " runner-secret" ) , Some ( manifest.clone ( ) ) ) .await.0 , StatusCode:: BAD_REQUEST ) ;
3331
let ( st, body) = call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{hash} /held" ) , Some ( " runner-secret" ) , Some ( manifest.clone ( ) ) ) .await;
3332
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
3333
let kept = oxygenhub:: images:: kept( & h.app.db) .await.unwrap ( ) ;
3334
let row = kept.iter ( ) .find ( | k | k.hash = = hash ) .expect ( " recorded" ) ;
3335
assert_eq! ( row.source, " peer" ) ;
3336
let ( _ , body) = call ( & h.app, " POST" , " /api/v1/flow/images/held" , Some ( " runner-secret" ) , Some ( held) ) .await;
3337
assert! ( body.contains ( " \" wanted\" :[]" ) , " {body}" ) ;
3338
3339
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " runner-secret" ) , None ) .await;
3340
assert_eq! ( st, StatusCode:: OK ) ;
3341
let first = m.needed ( ) [ 0 ] .to_string ( ) ;
3342
assert_ne! ( call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/chunks/{first} " ) , Some ( " runner-secret" ) , None ) .await.0 , StatusCode:: OK ) ;
3343
let reach = oxygenhub:: images:: reach( & h.app) .await.unwrap ( ) ;
3344
assert! ( reach.peer_only ( & hash) ) ;
3345
3346
for c in m.needed ( ) {
3347
let ( st, body) = put_bytes ( & h.app, & format! ( " /api/v1/flow/images/chunks/{c} " ) , " site" , chunks[ c] .clone ( ) ) .await;
3348
assert_eq! ( st, StatusCode:: NO_CONTENT , " {body}" ) ;
3349
}
3350
let ( st, body) = call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " site" ) , Some ( manifest) ) .await;
3351
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
3352
let kept = oxygenhub:: images:: kept( & h.app.db) .await.unwrap ( ) ;
3353
assert_eq! ( kept.iter ( ) .find ( | k | k.hash = = hash ) .unwrap ( ) .source, " hub" ) ;
3354
assert! ( ! oxygenhub:: images:: reach( & h.app) .await.unwrap ( ) .peer_only ( & hash) ) ;
3355
}
3356
3357
# [ tokio ::test ( flavor = " multi_thread" ) ]
92a797b2 Andy McCall 26 days ago ↶
3358
async fn images_are_sent_by_chunk_and_offered_to_runners_that_fetch ( ) {
3359
let h = hub ( ) .await;
3360
3361
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3362
3363
let mut image: Vec< u8 > = ( 0 .. 400_000 u32 ) .map ( | i | ( i % 251 ) as u8 ) .collect ( ) ;
3364
image.extend ( std:: iter:: repeat_n( 0 u8 , 2 < < 20 ) ) ;
3365
image.extend ( ( 0 .. 100_000 u32 ) .map ( | i | ( i % 239 ) as u8 ) ) ;
3366
let mut chunks = std:: collections:: HashMap:: new( ) ;
3367
let m = jet:: images:: scan( & image[ .. ] , Some ( " x86_64" .into ( ) ) , | c , b | {
3368
chunks.insert ( c.hash ( ) .to_string ( ) , b.to_vec ( ) ) ;
3369
Ok ( ( ) )
3370
} )
3371
.unwrap ( ) ;
3372
let hash = m.hash.clone ( ) ;
3373
3374
3375
let ask = serde_json:: json! ( { " chunks" : m.needed ( ) } ) .to_string ( ) ;
3376
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/images/missing" , Some ( " site" ) , Some ( ask.clone ( ) ) ) .await;
3377
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3378
let missing: Vec< String> = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " missing" ]
3379
.as_array ( )
3380
.unwrap ( )
3381
.iter ( )
3382
.map ( | v | v.as_str ( ) .unwrap ( ) .to_string ( ) )
3383
.collect ( ) ;
3384
assert_eq! ( missing.len ( ) , m.needed ( ) .len ( ) ) ;
3385
let manifest = serde_json:: to_string( & m) .unwrap ( ) ;
3386
let ( st, body) = call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " site" ) , Some ( manifest.clone ( ) ) ) .await;
3387
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " was not sent" ) , " {st} {body}" ) ;
3388
3389
let first = & missing[ 0 ] ;
3390
assert_eq! ( put_bytes ( & h.app, & format! ( " /api/v1/flow/images/chunks/{first} " ) , " site" , vec! [ 1 , 2 , 3 ] ) .await.0 , StatusCode:: BAD_REQUEST ) ;
3391
assert_ne! (
3392
put_bytes ( & h.app, & format! ( " /api/v1/flow/images/chunks/{first} " ) , " runner-secret" , chunks[ first] .clone ( ) ) .await.0 ,
3393
StatusCode:: NO_CONTENT
3394
) ;
3395
for c in & missing {
3396
let ( st, body) = put_bytes ( & h.app, & format! ( " /api/v1/flow/images/chunks/{c} " ) , " site" , chunks[ c] .clone ( ) ) .await;
3397
assert_eq! ( st, StatusCode:: NO_CONTENT , " {body}" ) ;
3398
}
3399
let ( _ , body) = call ( & h.app, " POST" , " /api/v1/flow/images/missing" , Some ( " site" ) , Some ( ask) ) .await;
3400
assert! ( body.contains ( " \" missing\" :[]" ) , " {body}" ) ;
750579ec Andy McCall 25 days ago ↶
3401
3402
let many: Vec< String> = ( 0 .. 60_000 u32 ) .map ( | i | blake3:: hash( & i.to_le_bytes ( ) ) .to_hex ( ) .to_string ( ) ) .collect ( ) ;
3403
let ( st, body) =
3404
call ( & h.app, " POST" , " /api/v1/flow/images/missing" , Some ( " site" ) , Some ( serde_json:: json! ( { " chunks" : many } ) .to_string ( ) ) ) .await;
3405
assert_eq! ( st, StatusCode:: OK , " {}" , & body[ .. body.len ( ) .min ( 200 ) ] ) ;
3406
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " missing" ] .as_array ( ) .unwrap ( ) .len ( ) , 60_000 ) ;
92a797b2 Andy McCall 26 days ago ↶
3407
3408
let mut wrong = m.clone ( ) ;
3409
wrong.hash = " ab" .repeat ( 32 ) ;
3410
let ( st, _ ) =
3411
call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{} " , wrong.hash) , Some ( " site" ) , Some ( serde_json:: to_string( & wrong) .unwrap ( ) ) )
3412
.await;
3413
assert_eq! ( st, StatusCode:: CONFLICT , " chunks that make another image" ) ;
d9d0d314 Andy McCall 11 days ago ↶
3414
let ( st, body) = call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " site" ) , Some ( manifest.clone ( ) ) ) .await;
3415
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
3416
3417
3418
3419
sqlx:: query( " DELETE FROM jet_images" ) .execute ( & h.app.db) .await.unwrap ( ) ;
3420
let gone = h.app.config.data.join ( " images/chunks" ) .join ( & missing[ 0 ] [ .. 2 ] ) .join ( & missing[ 0 ] ) ;
3421
let kept = std:: fs:: read( & gone) .unwrap ( ) ;
3422
std:: fs:: remove_file( & gone) .unwrap ( ) ;
92a797b2 Andy McCall 26 days ago ↶
3423
let ( st, body) = call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " site" ) , Some ( manifest) ) .await;
3424
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
d9d0d314 Andy McCall 11 days ago ↶
3425
assert! ( oxygenhub:: images:: held( & h.app.db) .await.unwrap ( ) .contains ( & hash) ) ;
3426
std:: fs:: write( & gone, kept) .unwrap ( ) ;
92a797b2 Andy McCall 26 days ago ↶
3427
3428
3429
let ( st, body) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " runner-secret" ) , None ) .await;
3430
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3431
let got: jet:: images:: Manifest = serde_json:: from_str( & body) .unwrap ( ) ;
3432
assert_eq! ( got, m) ;
3433
let dir = tempfile:: tempdir( ) .unwrap ( ) ;
3434
let out = dir.path ( ) .join ( " img.ext4" ) ;
3435
let mut fetched = Vec :: new( ) ;
3436
for c in got.needed ( ) {
3437
let req = Request:: get( format! ( " /api/v1/flow/images/chunks/{c} " ) )
3438
.header ( header:: AUTHORIZATION , " Bearer runner-secret" )
3439
.body ( Body:: empty( ) )
3440
.unwrap ( ) ;
3441
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
3442
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
3443
let bytes = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ;
3444
fetched.push ( ( c.to_string ( ) , bytes) ) ;
3445
}
3446
jet:: images:: assemble( & got, & out, | c | Ok ( fetched.iter ( ) .find ( | ( h , _) | h = = c.hash ( ) ) .unwrap ( ) .1. clone ( ) ) ) .unwrap ( ) ;
3447
assert_eq! ( std:: fs:: read( & out) .unwrap ( ) , image) ;
3448
assert_eq! (
3449
call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{} " , " cd" .repeat ( 32 ) ) , Some ( " runner-secret" ) , None ) .await.0 ,
3450
StatusCode:: NOT_FOUND
3451
) ;
3452
assert_ne! ( call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{hash} " ) , None , None ) .await.0 , StatusCode:: OK , " not anonymous" ) ;
3453
3454
3455
3456
create ( & h.app, " andy" , " demo" ) .await;
3457
let url = format! ( " {} /andy/demo" , h.base) ;
3458
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3459
let c = client ( & url, work.path ( ) ) ;
3460
write (
3461
work.path ( ) ,
3462
" .flow/pipeline.kdl" ,
3463
& format! ( " schema-version 1\n sandbox \" img\" {{ \n rootfs \" cas://blob/{hash} \" \n }} \n job \" build\" {{ \n sandbox \" img\" \n exec \" make\" \n }} \n " ) ,
3464
) ;
3465
c.commit ( " Build on the image" , & [ ] ) .unwrap ( ) ;
3466
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3467
runs_of ( & h.app, " andy" , " demo" ) .await;
1c8276b9 Andy McCall 26 days ago ↶
3468
3469
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
3470
let run1 = db:: flow:: run( & h.app.db, row.id, 1 ) .await.unwrap ( ) .unwrap ( ) ;
3471
let checks = checks_when ( & h.app, row.id, & run1.state, 1 ) .await;
3472
assert! ( checks.iter ( ) .all ( | c | c.name ! = " pipeline/images" ) , " {checks:?}" ) ;
92a797b2 Andy McCall 26 days ago ↶
3473
let ask = | fetches : bool | format! ( r # "{{ "runner":"test-1","runtimes":["microvm"],"images":[],"fetches_images":{fetches} }} "# ) ;
3474
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( false ) ) ) .await;
3475
assert_eq! ( st, StatusCode:: NO_CONTENT , " it lacks the image and does not fetch" ) ;
3476
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3477
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
3478
assert! ( body.contains ( " does not hold image" ) , " {body}" ) ;
3479
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( true ) ) ) .await;
3480
assert_eq! ( st, StatusCode:: OK , " it fetches, and the hub holds the image: {body}" ) ;
7829b7c7 Andy McCall 26 days ago ↶
3481
3482
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
3483
assert! ( ! body.contains ( " waiting" ) , " a running job keeps no waiting reason: {body}" ) ;
92a797b2 Andy McCall 26 days ago ↶
3484
}
40dd3f86 Andy McCall 26 days ago ↶
3485
3486
3487
3488
3489
# [ tokio ::test ( flavor = " multi_thread" ) ]
3490
async fn coverage_uploaded_from_a_workstation_says_so ( ) {
3491
let h = hub ( ) .await;
3492
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3493
create ( & h.app, " andy" , " vk" ) .await;
3494
let url = format! ( " {} /andy/vk" , h.base) ;
3495
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3496
let c = client ( & url, work.path ( ) ) ;
3497
write ( work.path ( ) , " src/game.c" , " int a;\n int b;\n int c;\n int d;\n " ) ;
3498
c.commit ( " Game" , & [ ] ) .unwrap ( ) ;
3499
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3500
3501
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 " ;
3502
let body = serde_json:: json! ( { " bookmark" : " main" , " content" : lcov } ) .to_string ( ) ;
3503
let ( st, out) = call ( & h.app, " POST" , " /api/v1/repos/andy/vk/coverage" , Some ( " site" ) , Some ( body) ) .await;
3504
assert_eq! ( st, StatusCode:: CREATED , " {out}" ) ;
3505
let v: serde_json:: Value = serde_json:: from_str( & out) .unwrap ( ) ;
3506
assert_eq! ( ( v[ " uploaded_by" ] .as_str ( ) , v[ " lines_hit" ] .as_i64 ( ) , v[ " lines_found" ] .as_i64 ( ) ) , ( Some ( " andy" ) , Some ( 3 ) , Some ( 4 ) ) ) ;
3507
assert_eq! ( v[ " dropped" ] , 1 , " a file outside the state is left out" ) ;
3508
assert_eq! ( v[ " bookmark" ] , " main" ) ;
3509
3510
let ( _ , page) = call ( & h.app, " GET" , " /andy/vk/coverage" , None , None ) .await;
3511
assert! ( page.contains ( " uploaded by andy" ) & & page.contains ( " 75.0%" ) , " the Coverage page says where it came from" ) ;
3512
let ( _ , page) = call ( & h.app, " GET" , " /andy/vk/reports" , None , None ) .await;
3513
assert! ( page.contains ( " uploaded by andy" ) , " and the Reports card" ) ;
3514
let row = db:: repos:: get( & h.app.db, " andy" , " vk" ) .await.unwrap ( ) .unwrap ( ) ;
3515
let state = v[ " state" ] .as_str ( ) .unwrap ( ) ;
3516
let checks = db:: hooks:: checks_for_state( & h.app.db, row.id, state) .await.unwrap ( ) ;
3517
assert! ( checks.iter ( ) .all ( | c | c.name ! = " coverage" ) , " an upload sets no check: {checks:?}" ) ;
3518
3519
3520
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/vk/coverage" , None , Some ( r # "{"content":"x"}"# .into ( ) ) ) .await;
3521
assert_ne! ( st, StatusCode:: CREATED ) ;
3522
let ( st, _ ) = call (
3523
& h.app,
3524
" POST" ,
3525
" /api/v1/repos/andy/vk/coverage" ,
3526
Some ( " site" ) ,
3527
Some ( r # "{"bookmark":"nope","content":"SF:a\nend_of_record\n"}"# .into ( ) ) ,
3528
)
3529
.await;
3530
assert_eq! ( st, StatusCode:: BAD_REQUEST ) ;
3531
}
2fb70d01 Andy McCall 26 days ago ↶
3532
3533
3534
# [ tokio ::test ( flavor = " multi_thread" ) ]
3535
async fn a_runners_facts_are_kept_from_its_claim ( ) {
3536
let h = hub ( ) .await;
3537
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3538
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"}}"# ;
3539
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body.into ( ) ) ) .await;
3540
assert_eq! ( st, StatusCode:: NO_CONTENT , " nothing to run, but the facts are kept" ) ;
3541
let ( st, out) = call ( & h.app, " GET" , " /api/v1/runners" , Some ( " site" ) , None ) .await;
3542
assert_eq! ( st, StatusCode:: OK , " {out}" ) ;
3543
let v: serde_json:: Value = serde_json:: from_str( & out) .unwrap ( ) ;
3544
let r = & v[ 0 ] ;
3545
assert_eq! (
3546
( r[ " speed" ] .as_i64 ( ) , r[ " speed_class" ] .as_i64 ( ) , r[ " free_disk_mib" ] .as_i64 ( ) , r[ " load_x100" ] .as_i64 ( ) ) ,
3547
( Some ( 100 ) , Some ( 3 ) , Some ( 42000 ) , Some ( 112 ) )
3548
) ;
3549
let row = db:: runners:: list( & h.app.db) .await.unwrap ( ) .remove ( 0 ) ;
3550
assert_eq! ( row.facts_line ( ) , " speed 100 (class 3) · load 1.12 · 41 GiB free" ) ;
3551
}
44a3d2eb Andy McCall 26 days ago ↶
3552
3553
3554
3555
# [ tokio ::test ( flavor = " multi_thread" ) ]
3556
async fn a_jobs_next_run_needs_the_disk_its_last_took ( ) {
3557
let h = hub ( ) .await;
3558
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3559
create ( & h.app, " andy" , " demo" ) .await;
3560
let url = format! ( " {} /andy/demo" , h.base) ;
3561
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3562
let c = client ( & url, work.path ( ) ) ;
3563
write (
3564
work.path ( ) ,
3565
" .flow/pipeline.kdl" ,
3566
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
3567
) ;
3568
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
3569
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3570
runs_of ( & h.app, " andy" , " demo" ) .await;
3571
let claim_with = | free : u64 | format! ( r # "{{ "runner":"test-1","runtimes":["microvm"],"facts":{{ "free_disk_mib":{free} }} }} "# ) ;
3572
3573
3574
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( claim_with ( 500 ) ) ) .await;
3575
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3576
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3577
let done = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " peak_disk_mib" : 8000 , " peak_memory_mib" : 900 } ) ;
3578
assert_eq! ( finish ( & h.app, id, done) .await, StatusCode:: NO_CONTENT ) ;
3579
let job = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
3580
assert_eq! ( ( job.peak_disk_mib, job.peak_memory_mib) , ( Some ( 8000 ) , Some ( 900 ) ) ) ;
3581
3582
3583
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
3584
assert_eq! ( st, StatusCode:: CREATED ) ;
3585
let repo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
3586
let run2 = db:: flow:: run( & h.app.db, repo.id, 2 ) .await.unwrap ( ) .unwrap ( ) ;
3587
let next = db:: flow:: jobs( & h.app.db, run2.id) .await.unwrap ( ) .remove ( 0 ) ;
3588
assert_eq! ( next.need_disk_mib, Some ( 8000 + 2000 + 64 ) ) ;
3589
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( claim_with ( 9000 ) ) ) .await;
3590
assert_eq! ( st, StatusCode:: NO_CONTENT , " 9000 MiB free is not enough" ) ;
3591
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3592
let waiting = db:: flow:: job( & h.app.db, next.id) .await.unwrap ( ) .unwrap ( ) .waiting;
3593
assert! ( waiting.contains ( " 8.8 GiB free; the job needs ~9.8 GiB" ) , " {waiting}" ) ;
3594
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( claim_with ( 20_000 ) ) ) .await;
3595
assert_eq! ( st, StatusCode:: OK , " a roomier runner takes it: {body}" ) ;
3596
}
501d8b26 Andy McCall 26 days ago ↶
3597
3598
3599
3600
3601
# [ tokio ::test ( flavor = " multi_thread" ) ]
3602
async fn a_job_waits_briefly_for_a_runner_that_holds_its_image ( ) {
3603
let h = hub ( ) .await;
3604
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3605
3606
db:: runners:: configured( & h.app.db, & blake3:: hash( b " other-secret" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
3607
let ask = | images : & str | {
3608
format! (
3609
r # "{{ "runner":"r","runtimes":["microvm"],"images":[{images} ],"fetches_images":true,"facts":{{ "speed":100,"speed_class":3,"free_disk_mib":50000}} }} "#
3610
)
3611
} ;
3612
let holder = ask ( & format! ( " \" {ROOTFS} \" " ) ) ;
3613
3614
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder.clone ( ) ) ) .await;
3615
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
3616
3b1bc27f Andy McCall 21 days ago
3617
sqlx:: query( " INSERT INTO jet_images (hash, size, arch, chunks, uploader, created_ms) VALUES ($1, 1, 'x86_64', 1, 'andy', 0)" )
501d8b26 Andy McCall 26 days ago ↶
3618
.bind ( ROOTFS )
3619
.execute ( & h.app.db)
3620
.await
3621
.unwrap ( ) ;
3622
3623
create ( & h.app, " andy" , " demo" ) .await;
3624
let url = format! ( " {} /andy/demo" , h.base) ;
3625
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3626
let c = client ( & url, work.path ( ) ) ;
3627
write (
3628
work.path ( ) ,
3629
" .flow/pipeline.kdl" ,
3630
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
3631
) ;
3632
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
3633
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3634
runs_of ( & h.app, " andy" , " demo" ) .await;
3635
3636
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( " " ) ) ) .await;
3637
assert_eq! ( st, StatusCode:: NO_CONTENT , " held for the runner that holds the image" ) ;
3638
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder) ) .await;
3639
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3640
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3641
let placed = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .placed;
3642
assert! ( placed.starts_with ( " holds the image, idle, speed 100 (class 3)" ) , " {placed}" ) ;
3643
assert! ( placed.contains ( " held for a better runner" ) , " {placed}" ) ;
3644
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
3645
assert! ( body.contains ( " \" placed\" :\" holds the image" ) , " {body}" ) ;
3646
}
20b8fe85 Andy McCall 26 days ago ↶
3647
4fa0da12 Andy McCall 24 days ago ↶
3648
3649
3650
# [ tokio ::test ( flavor = " multi_thread" ) ]
3651
async fn a_job_waits_briefly_for_the_runner_holding_its_build_cache ( ) {
3652
let h = hub ( ) .await;
3653
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3654
db:: runners:: configured( & h.app.db, & blake3:: hash( b " other-secret" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
3655
let slot = flow_core:: api:: cache_slot( " andy/demo" , " build" ) ;
3656
let ask = | caches : & str | {
3657
format! (
3658
r # "{{ "runner":"r","runtimes":["microvm"],"images":["{ROOTFS} "],"fetches_images":true,"caches":[{caches} ],"facts":{{ "speed":100,"speed_class":3,"free_disk_mib":50000}} }} "#
3659
)
3660
} ;
3661
let holder = ask ( & format! ( " \" {slot} \" " ) ) ;
3662
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder.clone ( ) ) ) .await;
3663
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
3664
3665
create ( & h.app, " andy" , " demo" ) .await;
3666
let url = format! ( " {} /andy/demo" , h.base) ;
3667
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3668
let c = client ( & url, work.path ( ) ) ;
3669
write (
3670
work.path ( ) ,
3671
" .flow/pipeline.kdl" ,
3672
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
3673
) ;
3674
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
3675
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3676
runs_of ( & h.app, " andy" , " demo" ) .await;
3677
3678
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( " " ) ) ) .await;
3679
assert_eq! ( st, StatusCode:: NO_CONTENT , " held for the runner holding the build cache" ) ;
3680
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder) ) .await;
3681
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3682
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3683
let placed = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .placed;
3684
assert! ( placed.starts_with ( " holds its build cache, holds the image" ) , " {placed}" ) ;
3685
}
3686
c2be148c Andy McCall 24 days ago ↶
3687
3688
3689
# [ tokio ::test ( flavor = " multi_thread" ) ]
3690
async fn a_job_waits_briefly_for_the_fresher_build_cache ( ) {
3691
let h = hub ( ) .await;
3692
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3693
db:: runners:: configured( & h.app.db, & blake3:: hash( b " other-secret" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
3694
let slot = flow_core:: api:: cache_slot( " andy/demo" , " build" ) ;
3695
let now = db:: now_ms( ) ;
3696
let ask = | kept : i64 | {
3697
format! (
3698
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}} }} "#
3699
)
3700
} ;
3701
3702
let fresh = ask ( now - 60_000 ) ;
3703
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( fresh.clone ( ) ) ) .await;
3704
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
3705
3706
create ( & h.app, " andy" , " demo" ) .await;
3707
let url = format! ( " {} /andy/demo" , h.base) ;
3708
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3709
let c = client ( & url, work.path ( ) ) ;
3710
write (
3711
work.path ( ) ,
3712
" .flow/pipeline.kdl" ,
3713
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
3714
) ;
3715
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
3716
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3717
runs_of ( & h.app, " andy" , " demo" ) .await;
3718
3719
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( now - 86_400_000 ) ) ) .await;
3720
assert_eq! ( st, StatusCode:: NO_CONTENT , " held for the fresher copy" ) ;
3721
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( fresh) ) .await;
3722
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3723
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3724
let placed = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .placed;
3725
assert! ( placed.starts_with ( " holds its build cache (kept 1 min ago), holds the image" ) , " {placed}" ) ;
3726
}
3727
20b8fe85 Andy McCall 26 days ago ↶
3728
3729
3730
3731
# [ tokio ::test ( flavor = " multi_thread" ) ]
3732
async fn report_files_expire_under_retention ( ) {
3733
let h = hub ( ) .await;
3734
create ( & h.app, " andy" , " demo" ) .await;
3735
let repo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
3736
let add = | bookmark : & 'static str , at : i64 | {
3737
let db = h.app.db.clone ( ) ;
3738
async move {
3739
let id: i64 = sqlx:: query_scalar(
3740
" INSERT INTO reports (repo_id, name, kind, title, summary, context, job, bookmark, state, files, bytes, created_ms) \
3b1bc27f Andy McCall 21 days ago
3741
VALUES ($1, 'perf', 'performance', 'Perf', '', '{}', 'perf', $2, 's', 1, 1, $3) RETURNING id" ,
20b8fe85 Andy McCall 26 days ago ↶
3742
)
3743
.bind ( repo.id)
3744
.bind ( bookmark)
3745
.bind ( at)
3746
.fetch_one ( & db)
3747
.await
3748
.unwrap ( ) ;
3b1bc27f Andy McCall 21 days ago
3749
sqlx:: query( " INSERT INTO report_files (report_id, path, blob, size) VALUES ($1, 'report.toml', $2, 1)" )
20b8fe85 Andy McCall 26 days ago ↶
3750
.bind ( id)
3751
.bind ( format! ( " {:064x}" , id) )
3752
.execute ( & db)
3753
.await
3754
.unwrap ( ) ;
3b1bc27f Andy McCall 21 days ago
3755
sqlx:: query( " INSERT INTO report_metrics (report_id, key, value, unit, better) VALUES ($1, 'ms', 1, '', '')" )
20b8fe85 Andy McCall 26 days ago ↶
3756
.bind ( id)
3757
.execute ( & db)
3758
.await
3759
.unwrap ( ) ;
3760
id
3761
}
3762
} ;
3763
let main_old = add ( " main" , 1 ) .await;
3764
add ( " main" , 2 ) .await;
3765
let mut feature = Vec :: new( ) ;
3766
for at in 0 .. 52 {
3767
feature.push ( add ( " feature" , 10 + at) .await) ;
3768
}
3769
assert_eq! ( oxygenhub:: reports:: expire( & h.app.db) .await.unwrap ( ) , 2 , " the two oldest feature runs" ) ;
3770
let files = | id : i64 | {
3771
let db = h.app.db.clone ( ) ;
3772
async move {
3b1bc27f Andy McCall 21 days ago
3773
sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM report_files WHERE report_id = $1" ) .bind ( id) .fetch_one ( & db) .await.unwrap ( )
20b8fe85 Andy McCall 26 days ago ↶
3774
}
3775
} ;
3776
assert_eq! ( ( files ( feature[ 0 ] ) .await, files ( feature[ 1 ] ) .await, files ( feature[ 2 ] ) .await) , ( 0 , 0 , 1 ) ) ;
3777
assert_eq! ( files ( main_old) .await, 1 , " the default bookmark keeps every run" ) ;
3778
assert_eq! ( oxygenhub:: reports:: metrics( & h.app.db, feature[ 0 ] ) .await.unwrap ( ) .len ( ) , 1 , " metrics are kept" ) ;
3779
assert_eq! ( oxygenhub:: reports:: expire( & h.app.db) .await.unwrap ( ) , 0 , " once" ) ;
3780
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/reports/perf?id={} " , feature[ 0 ] ) , None , None ) .await;
3781
assert! ( page.contains ( " files have expired" ) , " the page says so" ) ;
1c8276b9 Andy McCall 26 days ago ↶
3782
}
3783
3784
3785
3786
3787
# [ tokio ::test ( flavor = " multi_thread" ) ]
3788
async fn a_pipeline_pinning_an_image_nobody_holds_fails_a_check ( ) {
3789
let h = hub ( ) .await;
3790
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3791
3792
let none = r # "{"runner":"test-1","runtimes":["microvm"],"images":[]}"# ;
3793
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( none.into ( ) ) ) .await;
3794
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
3795
create ( & h.app, " andy" , " demo" ) .await;
3796
let url = format! ( " {} /andy/demo" , h.base) ;
3797
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3798
let c = client ( & url, work.path ( ) ) ;
3799
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3800
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3801
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
3802
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3803
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
3804
let checks = checks_when ( & h.app, run.repo_id, & run.state, 4 ) .await;
3805
let check = checks.iter ( ) .find ( | c | c.name = = " pipeline/images" ) .expect ( " an images check" ) ;
3806
assert! ( check.status = = " failed" & & check.summary.contains ( & ROOTFS [ .. 12 ] ) & & check.summary.contains ( " flow rootfs push" ) , " {check:?}" ) ;
3807
3808
let holds = format! ( r # "{{ "runner":"test-1","runtimes":["microvm"],"images":["{ROOTFS} "]}} "# ) ;
3809
call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( holds) ) .await;
3810
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
3811
assert_eq! ( st, StatusCode:: CREATED ) ;
3812
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
3813
let check = checks.iter ( ) .find ( | c | c.name = = " pipeline/images" ) .unwrap ( ) ;
3814
assert_eq! ( check.status, " passed" , " {check:?}" ) ;
20b8fe85 Andy McCall 26 days ago ↶
3815
}
a46d15e7 Andy McCall 25 days ago ↶
3816
3817
3818
3819
3820
# [ tokio ::test ( flavor = " multi_thread" ) ]
3821
async fn the_tracker_names_peers_in_the_same_site_and_signs_a_ticket ( ) {
3822
let h = hub ( ) .await;
3823
let app = & h.app;
3824
let image = " ab" .repeat ( 32 ) ;
3825
let claim_as = | token : & 'static str , name : & 'static str , body : serde_json:: Value| async move {
3826
let mut b = serde_json:: json! ( { " runner" : name, " runtimes" : [ " microvm" ] } ) ;
3827
b.as_object_mut ( ) .unwrap ( ) .extend ( body.as_object ( ) .unwrap ( ) .clone ( ) ) ;
3828
let ( st, out) = call ( app, " POST" , " /api/v1/flow/claim" , Some ( token) , Some ( b.to_string ( ) ) ) .await;
3829
assert_eq! ( st, StatusCode:: NO_CONTENT , " {out}" ) ;
3830
} ;
3831
let announce = | token : & 'static str , state : & 'static str | {
3832
let body = serde_json:: json! ( { " image" : image, " state" : state } ) .to_string ( ) ;
3833
async move { call ( app, " POST" , " /api/v1/flow/swarm/announce" , Some ( token) , Some ( body) ) .await }
3834
} ;
3835
6adcd68b Andy McCall 25 days ago ↶
3836
claim_as (
3837
" runner-secret" ,
3838
" a" ,
3839
serde_json:: json! ( { " images" : [ image] , " swarm" : { " addr" : " 10.0.0.1:7401" , " site" : " lab" , " cert" : " 11" .repeat ( 32 ) } } ) ,
3840
)
3841
.await;
3842
claim_as (
3843
" runner-two" ,
3844
" b" ,
3845
serde_json:: json! ( { " images" : [ ] , " swarm" : { " addr" : " 10.0.0.2:7401" , " site" : " lab" , " cert" : " 22" .repeat ( 32 ) } } ) ,
3846
)
3847
.await;
3848
claim_as (
3849
" runner-three" ,
3850
" c" ,
3851
serde_json:: json! ( { " images" : [ ] , " swarm" : { " addr" : " 10.0.0.3:7401" , " site" : " lab" , " cert" : " 33" .repeat ( 32 ) } } ) ,
3852
)
3853
.await;
a46d15e7 Andy McCall 25 days ago ↶
3854
3855
let ( st, body) = announce ( " runner-two" , " fetching" ) .await;
3856
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3857
let a: flow_core:: api:: Announced = serde_json:: from_str( & body) .unwrap ( ) ;
3858
let names: Vec< ( & str , bool ) > = a.peers.iter ( ) .map ( | p | ( p.addr.as_str ( ) , p.complete) ) .collect ( ) ;
3859
assert_eq! ( names, vec! [ ( " 10.0.0.1:7401" , true ) ] , " only A holds it so far" ) ;
6adcd68b Andy McCall 25 days ago ↶
3860
assert_eq! ( a.peers[ 0 ] .cert, " 11" .repeat ( 32 ) , " with the fingerprint peers check it by" ) ;
a46d15e7 Andy McCall 25 days ago ↶
3861
let t = jet:: swarm:: Ticket:: open( & a.ticket, & app.hub_key.identity, oxygenhub:: db:: now_ms( ) as u64 ) .unwrap ( ) ;
3862
assert_eq! ( ( t.name.as_str ( ) , t.site.as_str ( ) ) , ( " b" , " lab" ) ) ;
3863
3864
3865
let ( _ , body) = announce ( " runner-three" , " fetching" ) .await;
3866
let a: flow_core:: api:: Announced = serde_json:: from_str( & body) .unwrap ( ) ;
3867
let mut peers: Vec< ( & str , bool ) > = a.peers.iter ( ) .map ( | p | ( p.addr.as_str ( ) , p.complete) ) .collect ( ) ;
3868
peers.sort ( ) ;
3869
assert_eq! ( peers, vec! [ ( " 10.0.0.1:7401" , true ) , ( " 10.0.0.2:7401" , false ) ] ) ;
3870
3871
3872
announce ( " runner-two" , " done" ) .await;
6adcd68b Andy McCall 25 days ago ↶
3873
claim_as (
3874
" runner-three" ,
3875
" c" ,
3876
serde_json:: json! ( { " images" : [ ] , " swarm" : { " addr" : " 10.0.0.3:7401" , " site" : " elsewhere" , " cert" : " 33" .repeat ( 32 ) } } ) ,
3877
)
3878
.await;
a46d15e7 Andy McCall 25 days ago ↶
3879
let ( _ , body) = announce ( " runner-three" , " fetching" ) .await;
3880
let a: flow_core:: api:: Announced = serde_json:: from_str( & body) .unwrap ( ) ;
3881
assert! ( a.peers.is_empty ( ) , " {:?}" , a.peers) ;
6adcd68b Andy McCall 25 days ago ↶
3882
3883
3884
claim_as ( " runner-secret" , " a" , serde_json:: json! ( { " images" : [ image] , " swarm" : { " addr" : " 10.0.0.1:7401" , " site" : " elsewhere" } } ) )
3885
.await;
3886
let ( st, _ ) = announce ( " runner-secret" , " fetching" ) .await;
3887
assert_eq! ( st, StatusCode:: CONFLICT ) ;
a46d15e7 Andy McCall 25 days ago ↶
3888
3889
3890
claim_as ( " runner-two" , " b" , serde_json:: json! ( { " images" : [ ] } ) ) .await;
3891
let ( st, body) = announce ( " runner-two" , " fetching" ) .await;
3892
assert_eq! ( st, StatusCode:: CONFLICT ) ;
3893
assert! ( body.contains ( " [runner.swarm]" ) , " {body}" ) ;
3894
let ( st, _ ) =
3895
call ( app, " POST" , " /api/v1/flow/swarm/announce" , Some ( " runner-secret" ) , Some ( r # "{"image":"nope","state":"fetching"}"# .into ( ) ) )
3896
.await;
3897
assert_eq! ( st, StatusCode:: BAD_REQUEST ) ;
3898
let ( st, _ ) = call (
3899
app,
3900
" POST" ,
3901
" /api/v1/flow/swarm/announce" ,
3902
None ,
3903
Some ( serde_json:: json! ( { " image" : image, " state" : " fetching" } ) .to_string ( ) ) ,
3904
)
3905
.await;
3906
assert_eq! ( st, StatusCode:: UNAUTHORIZED ) ;
3907
}
6ca0b971 Andy McCall 25 days ago ↶
3908
3909
3910
# [ tokio ::test ( flavor = " multi_thread" ) ]
3911
async fn the_images_page_says_where_each_image_is_and_what_uses_it ( ) {
3912
let h = hub ( ) .await;
3913
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3914
create ( & h.app, " andy" , " demo" ) .await;
3915
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3916
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
3917
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3918
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3919
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
3920
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3921
3922
let held = " cd" .repeat ( 32 ) ;
3923
let kept = " ef" .repeat ( 32 ) ;
3924
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 ) } } ) ;
3925
let ( st, out) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body.to_string ( ) ) ) .await;
3926
assert! ( st = = StatusCode:: NO_CONTENT | | st = = StatusCode:: OK , " {st} {out}" ) ;
3b1bc27f Andy McCall 21 days ago
3927
sqlx:: query( " INSERT INTO jet_images (hash, size, arch, chunks, uploader, created_ms) VALUES ($1, 1048576, 'x86_64', 9, 'andy', 1)" )
6ca0b971 Andy McCall 25 days ago ↶
3928
.bind ( & kept)
3929
.execute ( & h.app.db)
3930
.await
3931
.unwrap ( ) ;
3932
3933
let res = oxygenhub:: router( h.app.clone ( ) )
3934
.oneshot (
3935
Request:: post( " /register" )
3936
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 21 days ago
3937
.body ( Body:: from( " username=carol&email=carol%40example.com&password=correct-horse-battery&setup=test-setup" ) )
6ca0b971 Andy McCall 25 days ago ↶
3938
.unwrap ( ) ,
3939
)
3940
.await
3941
.unwrap ( ) ;
3942
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
3943
let carol = db:: accounts:: account_by_name( & h.app.db, " carol" ) .await.unwrap ( ) .unwrap ( ) ;
3944
db:: accounts:: set_admin( & h.app.db, carol.id, true ) .await.unwrap ( ) ;
5d66f27c Andy McCall 10 days ago ↶
3945
3946
let lost = jet:: images:: scan( & [ 7 u8 ; 4096 ] [ .. ] , Some ( " x86_64" .into ( ) ) , | _, _| Ok ( ( ) ) ) .unwrap ( ) ;
3947
oxygenhub:: images:: record_peer( & h.app.db, & lost, " mars-1" ) .await.unwrap ( ) ;
6ca0b971 Andy McCall 25 days ago ↶
3948
let res = oxygenhub:: router( h.app.clone ( ) )
3949
.oneshot ( Request:: get( " /admin/runners/images" ) .header ( header:: COOKIE , cookie) .body ( Body:: empty( ) ) .unwrap ( ) )
3950
.await
3951
.unwrap ( ) ;
3952
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
3953
let page = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
3954
let row = | hash : & str | {
3955
let at = page.find ( & format! ( " title=\" {hash} \" " ) ) .unwrap_or_else ( | | panic! ( " {hash} is not on the page" ) ) ;
3956
let end = page[ at.. ] .find ( " </tr>" ) .unwrap ( ) ;
3957
page[ at.. at + end] .to_string ( )
3958
} ;
3959
let pinned = row ( ROOTFS ) ;
3960
assert! ( pinned.contains ( " nowhere" ) & & pinned.contains ( " andy/demo" ) & & pinned.contains ( " 3 jobs" ) , " {pinned}" ) ;
3961
let on_runner = row ( & held) ;
3962
assert! ( on_runner.contains ( " img-holds" ) & & on_runner.contains ( " earth-1" ) & & ! on_runner.contains ( " nowhere" ) , " {on_runner}" ) ;
3963
let on_hub = row ( & kept) ;
3964
assert! ( on_hub.contains ( " img-hub" ) & & on_hub.contains ( " 1.0 MiB" ) & & on_hub.contains ( " x86_64" ) , " {on_hub}" ) ;
5d66f27c Andy McCall 10 days ago ↶
3965
let peer_only = row ( & lost.hash) ;
3966
assert! ( peer_only.contains ( " img-peer" ) & & peer_only.contains ( " sent by mars-1" ) & & peer_only.contains ( " nowhere" ) , " {peer_only}" ) ;
6ca0b971 Andy McCall 25 days ago ↶
3967
assert! ( page.contains ( " earth-1 (home)" ) , " the swarm is named" ) ;
6246d1e5 Andy McCall 24 days ago ↶
3968
3969
assert! ( page.contains ( r # "{"images":"# ) & & page.contains ( r # ""scene":"swarm""# ) & & page.contains ( r # ""earth-1""# ) , " the swarm's data" ) ;
6ca0b971 Andy McCall 25 days ago ↶
3970
assert_eq! ( call ( & h.app, " GET" , " /admin/runners/images" , None , None ) .await.0 , StatusCode:: NOT_FOUND , " administrators only" ) ;
3971
}
f677d627 Andy McCall 25 days ago ↶
3972
3973
3974
3975
3976
# [ tokio ::test ( flavor = " multi_thread" ) ]
3977
async fn consumers_of_a_subrepo_are_indexed_with_how_far_behind_they_are ( ) {
3978
let h = hub ( ) .await;
e7158350 Andy McCall 25 days ago ↶
3979
3980
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
f677d627 Andy McCall 25 days ago ↶
3981
create ( & h.app, " andy" , " api" ) .await;
3982
create ( & h.app, " andy" , " game" ) .await;
3983
let api_work = tempfile:: tempdir( ) .unwrap ( ) ;
3984
let api = client ( & format! ( " {} /andy/api" , h.base) , api_work.path ( ) ) ;
3985
write ( api_work.path ( ) , " api.h" , " v1\n " ) ;
3986
api.snapshot ( " t" ) .unwrap ( ) ;
3987
api.commit ( " api v1" , & [ ] ) .unwrap ( ) ;
3988
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3989
3990
let game_work = tempfile:: tempdir( ) .unwrap ( ) ;
3991
let game = client ( & format! ( " {} /andy/game" , h.base) , game_work.path ( ) ) ;
3992
game.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
3993
write ( game_work.path ( ) , " game.c" , " int main;\n " ) ;
3994
game.snapshot ( " t" ) .unwrap ( ) ;
3995
game.commit ( " game on api v1" , & [ ] ) .unwrap ( ) ;
3996
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3997
3998
let consumers = | | async {
3999
let ( st, body) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , Some ( " site" ) , None ) .await;
4000
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4001
serde_json:: from_str:: < Vec< serde_json:: Value> > ( & body) .unwrap ( )
4002
} ;
4003
4004
4005
let settle = | want : & 'static ( dyn Fn( & [serde_json:: Value]) -> bool + Sync) | {
4006
let consumers = & consumers;
4007
async move {
4008
for _ in 0 .. 100 {
4009
let list = consumers ( ) .await;
4010
if want ( & list) {
4011
return list;
4012
}
4013
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4014
}
4015
consumers ( ) .await
4016
}
4017
} ;
4018
let list = settle ( & | l| ! l.is_empty ( ) ) .await;
4019
assert_eq! ( list.len ( ) , 1 , " {list:?}" ) ;
4020
assert_eq! (
4021
( list[ 0 ] [ " name" ] .as_str ( ) , list[ 0 ] [ " path" ] .as_str ( ) , list[ 0 ] [ " bookmark" ] .as_str ( ) ) ,
4022
( Some ( " game" ) , Some ( " deps/api" ) , Some ( " main" ) )
4023
) ;
4024
assert_eq! ( list[ 0 ] [ " behind" ] , 0 ) ;
4025
4026
4027
write ( api_work.path ( ) , " api.c" , " v2\n " ) ;
4028
api.snapshot ( " t" ) .unwrap ( ) ;
4029
api.commit ( " api v2" , & [ ] ) .unwrap ( ) ;
4030
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4031
assert_eq! ( settle ( & | l| l.first ( ) .is_some_and ( | c | c[ " behind" ] = = 1 ) ) .await[ 0 ] [ " behind" ] , 1 ) ;
4032
let ( _ , page) = call ( & h.app, " GET" , " /andy/api" , None , None ) .await;
c9aacc0e Andy McCall 24 days ago ↶
4033
assert! ( page.contains ( " Diffuses to" ) & & page.contains ( " andy/game" ) & & page.contains ( " 1 behind" ) , " the page shows it" ) ;
f677d627 Andy McCall 25 days ago ↶
4034
4035
4036
sqlx:: query( " UPDATE repositories SET visibility = 'private' WHERE name = 'game'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
4037
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , None , None ) .await;
4038
assert_eq! ( body.trim ( ) , " []" ) ;
4039
let ( _ , page) = call ( & h.app, " GET" , " /andy/api" , None , None ) .await;
4040
assert! ( ! page.contains ( " andy/game" ) ) ;
4041
sqlx:: query( " UPDATE repositories SET visibility = 'public' WHERE name = 'game'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
e7158350 Andy McCall 25 days ago ↶
4042
1d634f69 Andy McCall 25 days ago ↶
4043
4044
4045
let game_row = db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
4046
let api_row = db:: repos:: get( & h.app.db, " andy" , " api" ) .await.unwrap ( ) .unwrap ( ) ;
4047
let api_heads = api.bookmark_heads ( ) .unwrap ( ) ;
4048
let bumped = oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, api_heads.clone ( ) ) .await.unwrap ( ) ;
4049
let oxygenhub:: diffusion:: Bumped:: Moved { bookmark, heads, .. } = bumped else { panic! ( " {bumped:?}" ) } ;
4050
assert_eq! ( bookmark, " diffusion/deps/api" ) ;
4051
game.fetch ( " origin" ) .unwrap ( ) ;
4052
let bump_state = game.load_state ( & heads) .unwrap ( ) .materialize ( ) ;
4053
let pin = match & bump_state.files[ " deps/api" ] .content {
4054
oxy_core:: state:: FileContent:: Opaque { blobs } => oxy_core:: subrepo:: Pin:: decode( & game.read_blob ( & blobs[ 0 ] ) .unwrap ( ) ) .unwrap ( ) ,
4055
other => panic! ( " {other:?}" ) ,
4056
} ;
4057
assert! (
4058
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 } ) ,
4059
" {pin:?}"
4060
) ;
4061
assert! ( bump_state.files.contains_key ( " game.c" ) , " the rest of the game is as it was" ) ;
9aaa88b2 Andy McCall 24 days ago ↶
4062
assert! ( matches! (
1d634f69 Andy McCall 25 days ago ↶
4063
oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, api_heads.clone ( ) ) .await.unwrap ( ) ,
9aaa88b2 Andy McCall 24 days ago ↶
4064
oxygenhub:: diffusion:: Bumped:: Already { .. }
4065
) ) ;
7a14bdae Andy McCall 25 days ago ↶
4066
4067
4068
1d634f69 Andy McCall 25 days ago ↶
4069
write ( api_work.path ( ) , " other.c" , " v3\n " ) ;
4070
api.snapshot ( " t" ) .unwrap ( ) ;
4071
let v3 = api.commit ( " api v3, on its own" , & [ ] ) .unwrap ( ) ;
4072
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4073
let only_v3 = api.load_state ( & [ v3] ) .unwrap ( ) .heads ( ) .to_vec ( ) ;
7a14bdae Andy McCall 25 days ago ↶
4074
let combined = oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, only_v3) .await.unwrap ( ) ;
4075
let oxygenhub:: diffusion:: Bumped:: Moved { heads: ch, .. } = combined else { panic! ( " {combined:?}" ) } ;
4076
game.fetch ( " origin" ) .unwrap ( ) ;
4077
let st = game.load_state ( & ch) .unwrap ( ) .materialize ( ) ;
4078
let pin = match & st.files[ " deps/api" ] .content {
4079
oxy_core:: state:: FileContent:: Opaque { blobs } => oxy_core:: subrepo:: Pin:: decode( & game.read_blob ( & blobs[ 0 ] ) .unwrap ( ) ) .unwrap ( ) ,
4080
other => panic! ( " {other:?}" ) ,
4081
} ;
4082
let oxy_core:: subrepo:: Pin:: Oxy { heads: pinned, .. } = pin else { panic! ( ) } ;
4083
let all = api.load_state ( & pinned) .unwrap ( ) ;
4084
4085
assert! ( all.contains ( & v3) & & all.len ( ) = = 2 , " v1 and v3, {} changes" , all.len ( ) ) ;
1d634f69 Andy McCall 25 days ago ↶
4086
4087
let newer = api.bookmark_heads ( ) .unwrap ( ) ;
4088
let again = oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, newer) .await.unwrap ( ) ;
4089
assert! ( matches! ( again, oxygenhub:: diffusion:: Bumped:: Moved { ref bookmark, .. } if bookmark = = " diffusion/deps/api" ) , " {again:?}" ) ;
4090
e7158350 Andy McCall 25 days ago ↶
4091
4092
let put =
4093
| body : & 'static str , who : Option< & 'static str > | call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , who, Some ( body.into ( ) ) ) ;
4094
let ( st, body) = put ( r # "{"follow":"green"}"# , Some ( " site" ) ) .await;
4095
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4096
assert! ( body.contains ( " \" follow\" :\" green\" " ) & & body.contains ( " \" from\" :\" setting\" " ) , " {body}" ) ;
4097
assert_eq! ( put ( r # "{"follow":"sometimes"}"# , Some ( " site" ) ) .await.0 , StatusCode:: BAD_REQUEST ) ;
4098
assert_ne! ( put ( r # "{"follow":"any"}"# , None ) .await.0 , StatusCode:: OK , " not without a token" ) ;
4099
let ( _ , body) = put ( r # "{"follow":"default"}"# , Some ( " site" ) ) .await;
4100
assert! ( body.contains ( " \" follow\" :\" off\" " ) & & body.contains ( " \" from\" :\" site\" " ) , " {body}" ) ;
f677d627 Andy McCall 25 days ago ↶
4101
4102
4103
game.submodule_remove ( " deps/api" ) .unwrap ( ) ;
4104
std:: fs:: remove_dir_all( game_work.path ( ) .join ( " deps/api" ) ) .unwrap ( ) ;
4105
game.snapshot ( " t" ) .unwrap ( ) ;
4106
game.commit ( " game without api" , & [ ] ) .unwrap ( ) ;
4107
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4108
let left = settle ( & | l| l.is_empty ( ) ) .await;
4109
assert! ( left.is_empty ( ) , " {left:?}" ) ;
4110
}
a7e6e8e6 Andy McCall 25 days ago ↶
4111
4112
4113
4114
4115
# [ tokio ::test ( flavor = " multi_thread" ) ]
4116
async fn diffusion_proposes_a_bump_when_the_upstream_moves ( ) {
4117
let h = hub ( ) .await;
4118
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4119
create ( & h.app, " andy" , " api" ) .await;
4120
create ( & h.app, " andy" , " game" ) .await;
180dc43a Andy McCall 24 days ago ↶
4121
4122
let seen: std:: sync:: Mutex< Vec< serde_json:: Value> > = std:: sync:: Mutex:: new( Vec :: new( ) ) ;
4123
let seen = & seen;
4124
let hub_app = & h.app;
4125
let pass_all = move | | async move {
4126
let h_app = hub_app;
a7e6e8e6 Andy McCall 25 days ago ↶
4127
for _ in 0 .. 20 {
180dc43a Andy McCall 24 days ago ↶
4128
let Some ( a) = claim ( h_app) .await else {
a7e6e8e6 Andy McCall 25 days ago ↶
4129
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
180dc43a Andy McCall 24 days ago ↶
4130
if ! jobs_left ( h_app) .await {
a7e6e8e6 Andy McCall 25 days ago ↶
4131
break ;
4132
}
4133
continue ;
4134
} ;
180dc43a Andy McCall 24 days ago ↶
4135
seen.lock ( ) .unwrap ( ) .push ( a.clone ( ) ) ;
a7e6e8e6 Andy McCall 25 days ago ↶
4136
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4137
let ( name, ph) = ( a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
f7bcee49 Andy McCall 23 days ago ↶
4138
let st = finish ( h_app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, 0 ) } ) ) .await;
4139
4140
4141
4142
if st = = StatusCode:: CONFLICT {
4143
let job = db:: flow:: job( & h_app.db, id) .await.unwrap ( ) .unwrap ( ) ;
4144
assert_eq! ( job.status, " cancelled" , " refused only because a newer run cancelled it" ) ;
4145
continue ;
4146
}
4147
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
a7e6e8e6 Andy McCall 25 days ago ↶
4148
}
4149
} ;
4150
let api_work = tempfile:: tempdir( ) .unwrap ( ) ;
4151
let api = client ( & format! ( " {} /andy/api" , h.base) , api_work.path ( ) ) ;
4152
write ( api_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4153
write ( api_work.path ( ) , " test.sh" , " echo ok\n " ) ;
4154
write ( api_work.path ( ) , " api.h" , " v1\n " ) ;
4155
api.snapshot ( " t" ) .unwrap ( ) ;
4156
api.commit ( " api v1" , & [ ] ) .unwrap ( ) ;
4157
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4158
pass_all ( ) .await;
4159
4160
let game_work = tempfile:: tempdir( ) .unwrap ( ) ;
4161
let game = client ( & format! ( " {} /andy/game" , h.base) , game_work.path ( ) ) ;
4162
game.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
4163
write ( game_work.path ( ) , " game.c" , " int main;\n " ) ;
f01cb865 Andy McCall 25 days ago ↶
4164
write ( game_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4165
write ( game_work.path ( ) , " test.sh" , " echo ok\n " ) ;
a7e6e8e6 Andy McCall 25 days ago ↶
4166
game.snapshot ( " t" ) .unwrap ( ) ;
4167
game.commit ( " game on api v1" , & [ ] ) .unwrap ( ) ;
4168
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4169
let game_row = db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
4170
let bump = | | async {
4171
for _ in 0 .. 100 {
4172
let open = db:: proposals:: list( & h.app.db, game_row.id, & [ " open" ] ) .await.unwrap ( ) ;
4173
if let Some ( p) = open.into_iter ( ) .find ( | p | p.source_bookmark = = " diffusion/deps/api" ) {
4174
return Some ( p) ;
4175
}
4176
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4177
}
4178
None
4179
} ;
4180
4181
4182
write ( api_work.path ( ) , " a2.c" , " v2\n " ) ;
4183
api.snapshot ( " t" ) .unwrap ( ) ;
4184
api.commit ( " api v2: faster sprites" , & [ ] ) .unwrap ( ) ;
4185
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4186
pass_all ( ) .await;
4187
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
4188
assert! ( db:: proposals:: list( & h.app.db, game_row.id, & [ ] ) .await.unwrap ( ) .is_empty ( ) , " off proposes nothing" ) ;
4189
3776b70d Andy McCall 25 days ago ↶
4190
4191
a7e6e8e6 Andy McCall 25 days ago ↶
4192
let put = | body : & 'static str | call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , Some ( body.into ( ) ) ) ;
4193
assert_eq! ( put ( r # "{"follow":"any"}"# ) .await.0 , StatusCode:: OK ) ;
3776b70d Andy McCall 25 days ago ↶
4194
let first = bump ( ) .await.expect ( " caught up when turned on" ) ;
4195
assert! ( first.title.contains ( " 1 change" ) & & first.body.contains ( " api v2: faster sprites" ) , " {:?}" , first.title) ;
f01cb865 Andy McCall 25 days ago ↶
4196
4197
let mut ran = false ;
4198
for _ in 0 .. 100 {
4199
if runs_of ( & h.app, " andy" , " game" ) .await.iter ( ) .any ( | r | r.bookmark = = " diffusion/deps/api" ) {
4200
ran = true ;
4201
break ;
4202
}
4203
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4204
}
4205
assert! ( ran, " a run of the bump" ) ;
4206
pass_all ( ) .await;
a7e6e8e6 Andy McCall 25 days ago ↶
4207
write ( api_work.path ( ) , " a3.c" , " v3\n " ) ;
4208
api.snapshot ( " t" ) .unwrap ( ) ;
4209
api.commit ( " api v3: save slots past ten" , & [ ] ) .unwrap ( ) ;
4210
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3776b70d Andy McCall 25 days ago ↶
4211
let mut p = first.clone ( ) ;
4212
for _ in 0 .. 100 {
4213
p = bump ( ) .await.unwrap ( ) ;
4214
if p.body.contains ( " api v3" ) {
4215
break ;
4216
}
4217
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4218
}
4219
assert_eq! ( p.number, first.number, " the same proposal" ) ;
a7e6e8e6 Andy McCall 25 days ago ↶
4220
assert_eq! ( ( p.author.as_str ( ) , p.target.as_str ( ) ) , ( " diffusion" , " main" ) ) ;
4221
assert! ( p.title.contains ( " deps/api" ) & & p.title.contains ( " andy/api" ) & & p.title.contains ( " 2 changes" ) , " {}" , p.title) ;
4222
assert! ( p.body.contains ( " api v2: faster sprites" ) & & p.body.contains ( " api v3: save slots past ten" ) , " {}" , p.body) ;
4223
let labels = db:: issues:: proposal_labels( & h.app.db, p.id) .await.unwrap ( ) ;
4224
assert! ( labels.iter ( ) .any ( | l | l.name = = " dependencies" ) ) ;
4225
pass_all ( ) .await;
4226
4227
4228
4229
assert_eq! ( put ( r # "{"follow":"green"}"# ) .await.0 , StatusCode:: OK ) ;
4230
write ( api_work.path ( ) , " a4.c" , " v4\n " ) ;
4231
api.snapshot ( " t" ) .unwrap ( ) ;
4232
api.commit ( " api v4: rsm_set cheaper" , & [ ] ) .unwrap ( ) ;
4233
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4234
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
4235
assert! ( ! bump ( ) .await.unwrap ( ) .body.contains ( " api v4" ) , " not before its run passes" ) ;
4236
pass_all ( ) .await;
4237
let mut updated = None ;
04683947 Andy McCall 11 days ago ↶
4238
4239
for _ in 0 .. 600 {
a7e6e8e6 Andy McCall 25 days ago ↶
4240
let p2 = bump ( ) .await.unwrap ( ) ;
4241
if p2.body.contains ( " api v4" ) {
4242
updated = Some ( p2) ;
4243
break ;
4244
}
4245
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4246
}
4247
let p2 = updated.expect ( " brought up to date once the run passed" ) ;
4248
assert_eq! ( p2.number, p.number, " the same proposal" ) ;
4249
assert! ( p2.title.contains ( " 3 changes" ) , " {}" , p2.title) ;
4250
assert_eq! ( db:: proposals:: list( & h.app.db, game_row.id, & [ ] ) .await.unwrap ( ) .len ( ) , 1 , " never a pile of bumps" ) ;
3776b70d Andy McCall 25 days ago ↶
4251
757195ad Andy McCall 24 days ago ↶
4252
4253
4254
pass_all ( ) .await;
4255
let ( st, page) = call ( & h.app, " GET" , " /andy/api/diffusion" , None , None ) .await;
4256
assert_eq! ( st, StatusCode:: OK ) ;
c9aacc0e Andy McCall 24 days ago ↶
4257
assert! ( page.contains ( " Diffuses to" ) & & page.contains ( " andy/game" ) & & page.contains ( & format! ( " #{} " , p2.number) ) , " the bump is listed" ) ;
757195ad Andy McCall 24 days ago ↶
4258
assert! ( page.contains ( " df-green" ) , " a passing run of a checked upstream is green" ) ;
6246d1e5 Andy McCall 24 days ago ↶
4259
4260
assert! (
4261
page.contains ( r # ""scene":"ripples""# ) & & page.contains ( r # "["andy/game","# ) & & page.contains ( r # ""repo":"andy/api""# ) ,
4262
" the ripples' data"
4263
) ;
757195ad Andy McCall 24 days ago ↶
4264
let ( _ , page) = call ( & h.app, " GET" , " /andy/game/diffusion" , None , None ) .await;
c9aacc0e Andy McCall 24 days ago ↶
4265
assert! ( page.contains ( " Takes from" ) & & page.contains ( " andy/api" ) ) ;
757195ad Andy McCall 24 days ago ↶
4266
let ( _ , overview) = call ( & h.app, " GET" , " /andy/game" , None , None ) .await;
c9aacc0e Andy McCall 24 days ago ↶
4267
assert! ( overview.contains ( " Takes from" ) & & overview.contains ( " >Diffusion</a>" ) , " the tab and the panel" ) ;
eb2a3502 Andy McCall 24 days ago ↶
4268
4269
let ( st, body) = call ( & h.app, " GET" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , None ) .await;
4270
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4271
let d: hub_api:: Diffusion = serde_json:: from_str( & body) .unwrap ( ) ;
4272
assert! ( d.diffuses_to.is_empty ( ) , " nothing pins the game" ) ;
4273
let pin = d.takes_from.first ( ) .expect ( " the game takes from the api" ) ;
4274
assert_eq! ( ( pin.upstream.as_str ( ) , pin.consumer.as_str ( ) ) , ( " andy/api" , " andy/game" ) ) ;
4275
assert_eq! ( pin.bump, Some ( p2.number) ) ;
4276
assert_eq! ( pin.upstream_state.tone, " green" , " {body}" ) ;
4277
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/diffusion" , Some ( " site" ) , None ) .await;
4278
let d: hub_api:: Diffusion = serde_json:: from_str( & body) .unwrap ( ) ;
4279
assert! ( d.takes_from.is_empty ( ) & & d.diffuses_to.iter ( ) .any ( | p | p.consumer = = " andy/game" ) , " {body}" ) ;
757195ad Andy McCall 24 days ago ↶
4280
let ( st, _ ) = call ( & h.app, " GET" , " /admin/diffusion" , None , None ) .await;
4281
assert_eq! ( st, StatusCode:: NOT_FOUND , " administrators only" ) ;
4282
4283
5d54cf74 Andy McCall 22 days ago ↶
4284
4285
4286
let game = oxygenhub:: db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
4287
oxygenhub:: diffusion:: fail_next_records( & h.app, game.id, 1 ) ;
757195ad Andy McCall 24 days ago ↶
4288
let ( st, body) =
4289
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/game/proposals/{} /land" , p2.number) , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
4290
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4291
let mut behind = serde_json:: Value:: Null;
4292
for _ in 0 .. 100 {
4293
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , Some ( " site" ) , None ) .await;
4294
let list: Vec< serde_json:: Value> = serde_json:: from_str( & list) .unwrap ( ) ;
4295
behind = list.iter ( ) .find ( | c | c[ " name" ] = = " game" ) .map ( | c | c[ " behind" ] .clone ( ) ) .unwrap_or_default ( ) ;
4296
if behind = = 0 {
4297
break ;
4298
}
4299
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4300
}
4301
assert_eq! ( behind, 0 , " the index followed the landing" ) ;
5d54cf74 Andy McCall 22 days ago ↶
4302
assert_eq! ( oxygenhub:: diffusion:: forced_left( & h.app, game.id) , 0 , " the first write did fail" ) ;
0e04dfc2 Andy McCall 22 days ago ↶
4303
4304
4305
4306
let pins_of = | | async {
3b1bc27f Andy McCall 21 days ago
4307
sqlx:: query_scalar:: < _ , String> ( " SELECT heads FROM diffusion_pins WHERE consumer_id = $1" )
0e04dfc2 Andy McCall 22 days ago ↶
4308
.bind ( game.id)
4309
.fetch_one ( & h.app.db)
4310
.await
4311
.unwrap ( )
4312
} ;
4313
let right = pins_of ( ) .await;
3b1bc27f Andy McCall 21 days ago
4314
sqlx:: query( " UPDATE diffusion_pins SET heads = 'stale' WHERE consumer_id = $1" ) .bind ( game.id) .execute ( & h.app.db) .await.unwrap ( ) ;
4315
sqlx:: query( " DELETE FROM diffusion_indexed WHERE consumer_id = $1" ) .bind ( game.id) .execute ( & h.app.db) .await.unwrap ( ) ;
0e04dfc2 Andy McCall 22 days ago ↶
4316
let r = oxygenhub:: diffusion:: reconcile( & h.app) .await;
4317
assert! ( r.reindexed > = 1 , " {r:?}" ) ;
4318
assert_eq! ( pins_of ( ) .await, right, " the pin read again from the game's bookmark" ) ;
4319
assert_eq! ( oxygenhub:: diffusion:: reconcile( & h.app) .await.reindexed, 0 , " nothing left to mend" ) ;
757195ad Andy McCall 24 days ago ↶
4320
180dc43a Andy McCall 24 days ago ↶
4321
4322
4323
sqlx:: query( " UPDATE repositories SET visibility = 'private' WHERE name = 'api'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
4324
seen.lock ( ) .unwrap ( ) .clear ( ) ;
4325
write ( api_work.path ( ) , " a6.c" , " v6\n " ) ;
4326
api.snapshot ( " t" ) .unwrap ( ) ;
4327
api.commit ( " api v6" , & [ ] ) .unwrap ( ) ;
4328
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4fa0da12 Andy McCall 24 days ago ↶
4329
4330
4331
let mut bump_job = None ;
4332
for _ in 0 .. 100 {
4333
pass_all ( ) .await;
4334
bump_job = seen.lock ( ) .unwrap ( ) .iter ( ) .find ( | c | c[ " repo" ] = = " andy/game" & & c[ " bookmark" ] = = " diffusion/deps/api" ) .cloned ( ) ;
4335
if bump_job.is_some ( ) {
4336
break ;
4337
}
4338
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
4339
}
4340
let bump_job = bump_job.expect ( " the bump's job ran" ) ;
180dc43a Andy McCall 24 days ago ↶
4341
let subs: Vec< & str > = bump_job[ " subrepos" ] .as_array ( ) .unwrap ( ) .iter ( ) .filter_map ( | s | s[ " path" ] .as_str ( ) ) .collect ( ) ;
4342
assert_eq! ( subs, vec! [ " deps/api" ] , " {bump_job}" ) ;
4343
3776b70d Andy McCall 25 days ago ↶
4344
4345
4346
create ( & h.app, " andy" , " game2" ) .await;
4347
let g2_work = tempfile:: tempdir( ) .unwrap ( ) ;
4348
let g2 = client ( & format! ( " {} /andy/game2" , h.base) , g2_work.path ( ) ) ;
4349
g2.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
4350
g2.snapshot ( " t" ) .unwrap ( ) ;
4351
g2.commit ( " game2 on api" , & [ ] ) .unwrap ( ) ;
4352
assert! ( g2.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4353
4354
write ( api_work.path ( ) , " a5.c" , " v5\n " ) ;
4355
api.snapshot ( " t" ) .unwrap ( ) ;
4356
api.commit ( " api v5" , & [ ] ) .unwrap ( ) ;
7e01a766 Andy McCall 22 days ago ↶
4357
let api_row = db:: repos:: get( & h.app.db, " andy" , " api" ) .await.unwrap ( ) .unwrap ( ) ;
4358
let runs_before = db:: flow:: runs( & h.app.db, api_row.id, 100 , None ) .await.unwrap ( ) .len ( ) ;
3776b70d Andy McCall 25 days ago ↶
4359
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
7e01a766 Andy McCall 22 days ago ↶
4360
4361
4362
for _ in 0 .. 300 {
4363
if db:: flow:: runs( & h.app.db, api_row.id, 100 , None ) .await.unwrap ( ) .len ( ) > runs_before {
4364
break ;
4365
}
4366
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 20 ) ) .await;
4367
}
3776b70d Andy McCall 25 days ago ↶
4368
pass_all ( ) .await;
4369
let g2_row = db:: repos:: get( & h.app.db, " andy" , " game2" ) .await.unwrap ( ) .unwrap ( ) ;
4370
for _ in 0 .. 100 {
3b1bc27f Andy McCall 21 days ago
4371
if sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = $1" )
3776b70d Andy McCall 25 days ago ↶
4372
.bind ( g2_row.id)
4373
.fetch_one ( & h.app.db)
4374
.await
4375
.unwrap ( )
4376
> 0
4377
{
4378
break ;
4379
}
4380
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4381
}
4382
assert! ( db:: proposals:: list( & h.app.db, g2_row.id, & [ ] ) .await.unwrap ( ) .is_empty ( ) , " off until turned on" ) ;
4383
let ( st, _ ) = call ( & h.app, " PUT" , " /api/v1/repos/andy/game2/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green"}"# .into ( ) ) ) .await;
4384
assert_eq! ( st, StatusCode:: OK ) ;
4385
let mut caught = None ;
5d54cf74 Andy McCall 22 days ago ↶
4386
4387
for _ in 0 .. 300 {
3776b70d Andy McCall 25 days ago ↶
4388
if let Some ( p) = db:: proposals:: list( & h.app.db, g2_row.id, & [ " open" ] ) .await.unwrap ( ) .into_iter ( ) .next ( ) {
4389
caught = Some ( p) ;
4390
break ;
4391
}
4392
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4393
}
4394
let caught = caught.expect ( " bumped as soon as it was turned on" ) ;
4395
assert! ( caught.body.contains ( " api v5" ) & & caught.title.contains ( " 1 change" ) , " {:?} {:?}" , caught.title, caught.body) ;
0e04dfc2 Andy McCall 22 days ago ↶
4396
3b1bc27f Andy McCall 21 days ago
4397
sqlx:: query( " UPDATE proposals SET state = 'closed' WHERE id = $1" ) .bind ( caught.id) .execute ( & h.app.db) .await.unwrap ( ) ;
62cdf34d Andy McCall 22 days ago ↶
4398
4399
0e04dfc2 Andy McCall 22 days ago ↶
4400
let r = oxygenhub:: diffusion:: reconcile( & h.app) .await;
62cdf34d Andy McCall 22 days ago ↶
4401
assert_eq! ( r.caught_up, 0 , " the reconciler respects the closed bump: {r:?}" ) ;
a7e6e8e6 Andy McCall 25 days ago ↶
4402
}
4403
4404
4405
async fn jobs_left ( app : & Shared) -> bool {
4406
sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM flow_jobs WHERE status IN ('queued', 'running')" )
4407
.fetch_one ( & app.db)
4408
.await
4409
.unwrap_or ( 0 )
4410
> 0
9aaa88b2 Andy McCall 24 days ago ↶
4411
}
4412
4413
4414
4415
# [ tokio ::test ( flavor = " multi_thread" ) ]
4416
async fn bumps_from_an_upstream_without_checks_are_marked_unverified ( ) {
4417
let h = hub ( ) .await;
4418
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4419
create ( & h.app, " andy" , " lib" ) .await;
4420
create ( & h.app, " andy" , " app" ) .await;
4421
let lib_work = tempfile:: tempdir( ) .unwrap ( ) ;
4422
let lib = client ( & format! ( " {} /andy/lib" , h.base) , lib_work.path ( ) ) ;
4423
write ( lib_work.path ( ) , " lib.h" , " v1\n " ) ;
4424
lib.snapshot ( " t" ) .unwrap ( ) ;
4425
lib.commit ( " lib v1" , & [ ] ) .unwrap ( ) ;
4426
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4427
let app_work = tempfile:: tempdir( ) .unwrap ( ) ;
4428
let app = client ( & format! ( " {} /andy/app" , h.base) , app_work.path ( ) ) ;
4429
app.submodule_add ( & format! ( " {} /andy/lib" , h.base) , " deps/lib" , Some ( " main" ) ) .unwrap ( ) ;
c4e432da Andy McCall 24 days ago ↶
4430
4431
let modules = std:: fs:: read_to_string( app_work.path ( ) .join ( " .oxymodules" ) ) .unwrap ( ) ;
4432
write ( app_work.path ( ) , " .oxymodules" , & format! ( " {modules} watch = lib.c\n " ) ) ;
9aaa88b2 Andy McCall 24 days ago ↶
4433
app.snapshot ( " t" ) .unwrap ( ) ;
4434
app.commit ( " app on lib" , & [ ] ) .unwrap ( ) ;
4435
assert! ( app.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4436
let app_row = db:: repos:: get( & h.app.db, " andy" , " app" ) .await.unwrap ( ) .unwrap ( ) ;
4437
for _ in 0 .. 100 {
3b1bc27f Andy McCall 21 days ago
4438
if sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = $1" )
9aaa88b2 Andy McCall 24 days ago ↶
4439
.bind ( app_row.id)
4440
.fetch_one ( & h.app.db)
4441
.await
4442
.unwrap ( )
4443
> 0
4444
{
4445
break ;
4446
}
4447
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4448
}
4449
let ( st, _ ) = call ( & h.app, " PUT" , " /api/v1/repos/andy/app/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green"}"# .into ( ) ) ) .await;
4450
assert_eq! ( st, StatusCode:: OK ) ;
4451
write ( lib_work.path ( ) , " lib.c" , " v2\n " ) ;
4452
lib.snapshot ( " t" ) .unwrap ( ) ;
4453
lib.commit ( " lib v2" , & [ ] ) .unwrap ( ) ;
4454
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4455
let mut found = None ;
4456
for _ in 0 .. 100 {
4457
if let Some ( p) = db:: proposals:: list( & h.app.db, app_row.id, & [ " open" ] ) .await.unwrap ( ) .into_iter ( ) .next ( ) {
4458
found = Some ( p) ;
4459
break ;
4460
}
4461
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4462
}
4463
let p = found.expect ( " a bump although the upstream never goes green" ) ;
4464
assert! ( p.body.contains ( " Upstream unverified" ) , " {}" , p.body) ;
c4e432da Andy McCall 24 days ago ↶
4465
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 ↶
4466
let mut labels = Vec :: new( ) ;
4467
for _ in 0 .. 100 {
4468
labels = db:: issues:: proposal_labels( & h.app.db, p.id) .await.unwrap ( ) .into_iter ( ) .map ( | l | l.name ) .collect:: < Vec< String> > ( ) ;
4469
if labels.len ( ) = = 2 {
4470
break ;
4471
}
4472
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4473
}
4474
assert! ( labels.contains ( & " upstream unverified" .to_string ( ) ) & & labels.contains ( & " dependencies" .to_string ( ) ) , " {labels:?}" ) ;
a7e6e8e6 Andy McCall 25 days ago ↶
4475
}
d943160d Andy McCall 24 days ago ↶
4476
4477
4478
4479
# [ tokio ::test ( flavor = " multi_thread" ) ]
4480
async fn a_moved_target_runs_its_open_proposals_again ( ) {
4481
let h = hub ( ) .await;
4482
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4483
create ( & h.app, " andy" , " demo" ) .await;
4484
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4485
let mut c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
4486
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4487
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
4488
c.snapshot ( " t" ) .unwrap ( ) ;
4489
let base = c.commit ( " base" , & [ ] ) .unwrap ( ) ;
4490
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4491
4492
for ( b, file) in [ ( " fix-a" , " a.txt" ) , ( " fix-b" , " b.txt" ) ] {
4493
c.bookmark_create ( b, Some ( & [ base] ) ) .unwrap ( ) ;
4494
c.bookmark_switch ( b, true ) .unwrap ( ) ;
4495
write ( work.path ( ) , file, " x\n " ) ;
4496
c.snapshot ( " t" ) .unwrap ( ) ;
4497
c.commit ( & format! ( " add {file} " ) , & [ ] ) .unwrap ( ) ;
4498
assert! ( c.push_bookmark ( " origin" , b, false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4499
let body = serde_json:: json! ( { " title" : b, " target" : " main" , " source" : b } ) .to_string ( ) ;
4500
let ( st, out) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/proposals" , Some ( " site" ) , Some ( body) ) .await;
4501
assert_eq! ( st, StatusCode:: CREATED , " {out}" ) ;
4502
}
4503
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
4504
let props = db:: proposals:: list( & h.app.db, row.id, & [ " open" ] ) .await.unwrap ( ) ;
4505
let a = props.iter ( ) .find ( | p | p.source_bookmark = = " fix-a" ) .unwrap ( ) .number;
4506
let b = props.iter ( ) .find ( | p | p.source_bookmark = = " fix-b" ) .unwrap ( ) .number;
4507
4508
let ( st, out) = call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/demo/proposals/{a} /land" ) , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
4509
assert_eq! ( st, StatusCode:: OK , " {out}" ) ;
4510
let label = format! ( " proposal/{b} " ) ;
4511
let mut found = None ;
4512
for _ in 0 .. 100 {
4513
if let Some ( r) = runs_of ( & h.app, " andy" , " demo" ) .await.into_iter ( ) .find ( | r | r.bookmark = = label ) {
4514
found = Some ( r) ;
4515
break ;
4516
}
4517
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4518
}
4519
let run = found.expect ( " a run of fix-b's new projected state" ) ;
4520
let projected = db:: proposals:: get( & h.app.db, row.id, b) .await.unwrap ( ) .unwrap ( ) ;
4521
assert_eq! ( ( run.trigger.as_str ( ) , run.state.as_str ( ) ) , ( " proposal" , projected.projected_state.as_str ( ) ) , " on its projected state" ) ;
4522
4523
let before = runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) ;
4524
oxygenhub:: web:: proposals:: settle_after_push( & h.app, & row, " main" , " andy" ) .await;
4525
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , before, " not again for the same state" ) ;
4526
}
93501da2 Andy McCall 24 days ago ↶
4527
4528
4529
4530
async fn send_image ( app : & Shared, image : & [u8 ]) -> ( String, serde_json:: Value) {
4531
let mut chunks = std:: collections:: HashMap:: new( ) ;
4532
let m = jet:: images:: scan( image, None , | c , b | {
4533
chunks.insert ( c.hash ( ) .to_string ( ) , b.to_vec ( ) ) ;
4534
Ok ( ( ) )
4535
} )
4536
.unwrap ( ) ;
4537
for c in m.needed ( ) {
4538
let ( st, body) = put_bytes ( app, & format! ( " /api/v1/flow/images/chunks/{c} " ) , " site" , chunks[ c] .clone ( ) ) .await;
4539
assert_eq! ( st, StatusCode:: NO_CONTENT , " {body}" ) ;
4540
}
4541
let ( st, body) =
4542
call ( app, " PUT" , & format! ( " /api/v1/flow/images/{} " , m.hash) , Some ( " site" ) , Some ( serde_json:: to_string( & m) .unwrap ( ) ) ) .await;
4543
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
4544
( m.hash.clone ( ) , serde_json:: from_str( & body) .unwrap ( ) )
4545
}
4546
4547
4548
4549
4550
4551
# [ tokio ::test ( flavor = " multi_thread" ) ]
4552
async fn an_image_names_its_own_architecture_and_a_pass_teaches_the_rest ( ) {
4553
use oxy_core:: identity:: SecretKey;
4554
let h = hub ( ) .await;
4555
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4556
4557
let dir = tempfile:: tempdir( ) .unwrap ( ) ;
4558
let tree = dir.path ( ) .join ( " tree" ) ;
4559
std:: fs:: create_dir_all( tree.join ( " bin" ) ) .unwrap ( ) ;
4560
let mut elf = vec! [ 0 u8 ; 64 ] ;
4561
elf[ .. 7 ] .copy_from_slice ( b " \x7f ELF\x02 \x01 \x01 " ) ;
4562
elf[ 18 .. 20 ] .copy_from_slice ( & 0x3E u16 .to_le_bytes ( ) ) ;
4563
std:: fs:: write( tree.join ( " bin/sh" ) , & elf) .unwrap ( ) ;
4564
let img = dir.path ( ) .join ( " rootfs.ext4" ) ;
4565
let made = std:: process:: Command:: new( " mkfs.ext4" ) .args ( [ " -q" , " -F" , " -d" ] ) .arg ( & tree) .arg ( & img) .arg ( " 8M" ) .status ( ) ;
4566
if ! made.is_ok_and ( | s | s.success ( ) ) {
4567
eprintln! ( " mkfs.ext4 not found: skipped" ) ;
4568
return ;
4569
}
4570
let ( x86, answer) = send_image ( & h.app, & std:: fs:: read( & img) .unwrap ( ) ) .await;
4571
assert_eq! ( answer[ " arch" ] , " x86_64" , " read from its shell: {answer}" ) ;
4572
let ( _ , body) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{x86} " ) , Some ( " runner-secret" ) , None ) .await;
4573
assert_eq! ( serde_json:: from_str:: < jet:: images:: Manifest> ( & body) .unwrap ( ) .arch.as_deref ( ) , Some ( " x86_64" ) , " runners fetch it named" ) ;
4574
4575
let blob: Vec< u8 > = ( 0 .. 300_000 u32 ) .map ( | i | ( i % 241 ) as u8 ) .collect ( ) ;
4576
let ( unread, answer) = send_image ( & h.app, & blob) .await;
4577
assert! ( answer[ " arch" ] .is_null ( ) , " {answer}" ) ;
4578
let archs = db:: flow:: image_archs( & h.app.db) .await.unwrap ( ) ;
4579
assert_eq! ( ( archs.get ( & x86) .map ( String :: as_str) , archs.get ( & unread) ) , ( Some ( " x86_64" ) , None ) ) ;
4580
4581
sqlx:: query( " UPDATE jet_images SET arch = ''" ) .execute ( & h.app.db) .await.unwrap ( ) ;
4582
sqlx:: query( " DELETE FROM flow_images" ) .execute ( & h.app.db) .await.unwrap ( ) ;
4583
assert_eq! ( oxygenhub:: images:: learn_archs( & h.app) .await.unwrap ( ) , 1 ) ;
4584
assert_eq! ( db:: flow:: image_archs( & h.app.db) .await.unwrap ( ) .get ( & x86) .map ( String :: as_str) , Some ( " x86_64" ) ) ;
4585
4586
4587
create ( & h.app, " andy" , " arch" ) .await;
4588
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4589
let c = client ( & format! ( " {} /andy/arch" , h.base) , work.path ( ) ) ;
4590
let kdl = format! (
4591
" schema-version 1\n sandbox \" unread\" {{ \n rootfs \" cas://blob/{unread} \" \n }} \n sandbox \" x86\" {{ \n rootfs \" cas://blob/{x86} \" \n }} \n \
4592
job \" first\" {{ \n sandbox \" unread\" \n exec \" make\" \n }} \n job \" second\" {{ \n sandbox \" x86\" \n exec \" make\" \n depends-on \" first\" \n }} \n \
4593
job \" third\" {{ \n sandbox \" unread\" \n exec \" make\" \n depends-on \" first\" \n }} \n "
4594
) ;
4595
write ( work.path ( ) , " .flow/pipeline.kdl" , & kdl) ;
4596
c.commit ( " Jobs on two images" , & [ ] ) .unwrap ( ) ;
4597
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4598
runs_of ( & h.app, " andy" , " arch" ) .await;
4599
4600
4601
let x86_info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
4602
let x86_claim =
4603
serde_json:: json! ( { " runner" : " earth" , " runtimes" : [ " microvm" ] , " images" : [ ] , " fetches_images" : true , " info" : x86_info } ) ;
4604
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( x86_claim.to_string ( ) ) ) .await;
4605
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4606
let first: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
4607
assert_eq! ( first[ " job" ] [ " name" ] , " first" ) ;
4608
let ph = first[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) ;
4609
let id = first[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4610
assert_eq! (
4611
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : receipt ( " first" , ph, 0 ) } ) ) .await,
4612
StatusCode:: NO_CONTENT
4613
) ;
4614
assert_eq! ( db:: flow:: image_archs( & h.app.db) .await.unwrap ( ) .get ( & unread) .map ( String :: as_str) , Some ( " x86_64" ) , " learned" ) ;
4615
4616
4617
let token = " flowreg_narvi" ;
4618
db:: runners:: add_registration( & h.app.db, & blake3:: hash( token.as_bytes ( ) ) .to_hex ( ) , " admin" , & Default :: default( ) ) .await.unwrap ( ) ;
4619
let identity = SecretKey:: generate( ) .identity ( " narvi" , " " , 1 ) ;
4620
let info = serde_json:: json! ( { " arch" : " riscv64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
4621
let reg = serde_json:: json! ( { " token" : token, " name" : " narvi" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
4622
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
4623
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4624
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
4625
let narvi =
4626
serde_json:: json! ( { " runner" : " narvi" , " runtimes" : [ " microvm" ] , " images" : [ ] , " fetches_images" : true , " info" : info } ) .to_string ( ) ;
4627
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( & credential) , Some ( narvi) ) .await;
4628
assert_eq! ( st, StatusCode:: NO_CONTENT , " the second job's image is x86_64 and the third's learned so: {body}" ) ;
4629
for _ in 0 .. 2 {
4630
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( x86_claim.to_string ( ) ) ) .await;
4631
assert_eq! ( st, StatusCode:: OK , " both go to x86_64: {body}" ) ;
4632
}
4633
}
f4d6ef41 Andy McCall 24 days ago ↶
4634
4635
4636
4637
4638
4639
# [ tokio ::test ( flavor = " multi_thread" ) ]
4640
async fn a_consumer_that_chose_auto_lands_green_bumps_itself ( ) {
4641
let h = hub ( ) .await;
4642
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4643
create ( & h.app, " andy" , " api" ) .await;
4644
create ( & h.app, " andy" , " game" ) .await;
4645
let hub_app = & h.app;
4646
4647
let pass_all = move | | async move {
4648
for _ in 0 .. 20 {
4649
let Some ( a) = claim ( hub_app) .await else {
4650
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4651
if ! jobs_left ( hub_app) .await {
4652
break ;
4653
}
4654
continue ;
4655
} ;
4656
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4657
let ( name, ph) = ( a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4658
assert_eq! (
4659
finish ( hub_app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, 0 ) } ) ) .await,
4660
StatusCode:: NO_CONTENT
4661
) ;
4662
}
4663
} ;
4664
let api_work = tempfile:: tempdir( ) .unwrap ( ) ;
4665
let api = client ( & format! ( " {} /andy/api" , h.base) , api_work.path ( ) ) ;
4666
write ( api_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4667
write ( api_work.path ( ) , " test.sh" , " echo ok\n " ) ;
4668
api.snapshot ( " t" ) .unwrap ( ) ;
4669
api.commit ( " api v1" , & [ ] ) .unwrap ( ) ;
4670
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4671
pass_all ( ) .await;
4672
let game_work = tempfile:: tempdir( ) .unwrap ( ) ;
4673
let game = client ( & format! ( " {} /andy/game" , h.base) , game_work.path ( ) ) ;
4674
game.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
4675
write ( game_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4676
write ( game_work.path ( ) , " test.sh" , " echo ok\n " ) ;
4677
game.snapshot ( " t" ) .unwrap ( ) ;
4678
game.commit ( " game on api v1" , & [ ] ) .unwrap ( ) ;
4679
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4680
pass_all ( ) .await;
4681
let game_row = db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
4682
for _ in 0 .. 100 {
3b1bc27f Andy McCall 21 days ago
4683
let n: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = $1" )
f4d6ef41 Andy McCall 24 days ago ↶
4684
.bind ( game_row.id)
4685
.fetch_one ( & h.app.db)
4686
.await
4687
.unwrap ( ) ;
4688
if n > 0 {
4689
break ;
4690
}
4691
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4692
}
4693
let ( st, body) =
4694
call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green","land":"auto"}"# .into ( ) ) ) .await;
4695
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4696
assert! ( body.contains ( r # ""land":"auto""# ) , " {body}" ) ;
4697
let ( st, _ ) =
4698
call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green","land":"soon"}"# .into ( ) ) ) .await;
4699
assert_eq! ( st, StatusCode:: BAD_REQUEST ) ;
4700
4701
4702
4703
write ( api_work.path ( ) , " a2.c" , " v2\n " ) ;
4704
api.snapshot ( " t" ) .unwrap ( ) ;
4705
api.commit ( " api v2: faster sprites" , & [ ] ) .unwrap ( ) ;
4706
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4707
let mut landed = None ;
4708
for _ in 0 .. 100 {
4709
pass_all ( ) .await;
4710
let all = db:: proposals:: list( & h.app.db, game_row.id, & [ ] ) .await.unwrap ( ) ;
4711
if let Some ( p) = all.into_iter ( ) .find ( | p | p.source_bookmark = = " diffusion/deps/api" & & p.state = = " landed" ) {
4712
landed = Some ( p) ;
4713
break ;
4714
}
4715
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4716
}
4717
let p = landed.expect ( " landed by itself once both halves were green" ) ;
4718
assert! ( p.body.contains ( " lands its bumps itself" ) , " {}" , p.body) ;
4719
let events = db:: proposals:: events( & h.app.db, p.id) .await.unwrap ( ) ;
4720
let landing = events.iter ( ) .find ( | e | e.event = = " landed" ) .expect ( " a landing on the timeline" ) ;
4721
assert_eq! ( landing.actor, " diffusion" ) ;
4722
assert! ( landing.detail.contains ( " Diffusion, both halves green" ) , " {}" , landing.detail) ;
4723
4724
let mut behind = serde_json:: Value:: Null;
4725
for _ in 0 .. 100 {
4726
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , Some ( " site" ) , None ) .await;
4727
let list: Vec< serde_json:: Value> = serde_json:: from_str( & list) .unwrap ( ) ;
4728
behind = list.iter ( ) .find ( | c | c[ " name" ] = = " game" ) .map ( | c | c[ " behind" ] .clone ( ) ) .unwrap_or_default ( ) ;
4729
if behind = = 0 {
4730
break ;
4731
}
4732
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4733
}
4734
assert_eq! ( behind, 0 , " main moved to the bump" ) ;
4735
4736
4737
4738
create ( & h.app, " andy" , " lib" ) .await;
4739
create ( & h.app, " andy" , " app" ) .await;
4740
let lib_work = tempfile:: tempdir( ) .unwrap ( ) ;
4741
let lib = client ( & format! ( " {} /andy/lib" , h.base) , lib_work.path ( ) ) ;
4742
write ( lib_work.path ( ) , " lib.h" , " v1\n " ) ;
4743
lib.snapshot ( " t" ) .unwrap ( ) ;
4744
lib.commit ( " lib v1" , & [ ] ) .unwrap ( ) ;
4745
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4746
let app_work = tempfile:: tempdir( ) .unwrap ( ) ;
4747
let app = client ( & format! ( " {} /andy/app" , h.base) , app_work.path ( ) ) ;
4748
app.submodule_add ( & format! ( " {} /andy/lib" , h.base) , " deps/lib" , Some ( " main" ) ) .unwrap ( ) ;
4749
let modules = std:: fs:: read_to_string( app_work.path ( ) .join ( " .oxymodules" ) ) .unwrap ( ) ;
4750
write ( app_work.path ( ) , " .oxymodules" , & format! ( " {modules} follow = green\n land = auto\n " ) ) ;
4751
write ( app_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4752
write ( app_work.path ( ) , " test.sh" , " echo ok\n " ) ;
4753
app.snapshot ( " t" ) .unwrap ( ) ;
4754
app.commit ( " app on lib" , & [ ] ) .unwrap ( ) ;
4755
assert! ( app.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4756
pass_all ( ) .await;
4757
let app_row = db:: repos:: get( & h.app.db, " andy" , " app" ) .await.unwrap ( ) .unwrap ( ) ;
4758
for _ in 0 .. 100 {
3b1bc27f Andy McCall 21 days ago
4759
let n: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = $1" )
f4d6ef41 Andy McCall 24 days ago ↶
4760
.bind ( app_row.id)
4761
.fetch_one ( & h.app.db)
4762
.await
4763
.unwrap ( ) ;
4764
if n > 0 {
4765
break ;
4766
}
4767
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4768
}
4769
write ( lib_work.path ( ) , " lib.c" , " v2\n " ) ;
4770
lib.snapshot ( " t" ) .unwrap ( ) ;
4771
lib.commit ( " lib v2" , & [ ] ) .unwrap ( ) ;
4772
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4773
let mut passed = false ;
4774
for _ in 0 .. 100 {
4775
pass_all ( ) .await;
4776
let s: Option< String> = sqlx:: query_scalar(
3b1bc27f Andy McCall 21 days ago
4777
" SELECT status FROM flow_runs WHERE repo_id = $1 AND bookmark = 'diffusion/deps/lib' ORDER BY id DESC LIMIT 1" ,
f4d6ef41 Andy McCall 24 days ago ↶
4778
)
4779
.bind ( app_row.id)
4780
.fetch_optional ( & h.app.db)
4781
.await
4782
.unwrap ( ) ;
4783
if s.as_deref ( ) = = Some ( " passed" ) {
4784
passed = true ;
4785
break ;
4786
}
4787
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4788
}
4789
assert! ( passed, " the app's run on the bump passed" ) ;
4790
assert_eq! ( oxygenhub:: diffusion:: auto_land( & h.app, & app_row, " deps/lib" ) .await, Err ( oxygenhub:: diffusion:: Held:: Unverified) ) ;
4791
let open = db:: proposals:: list( & h.app.db, app_row.id, & [ " open" ] ) .await.unwrap ( ) ;
4792
assert! ( open.iter ( ) .any ( | p | p.source_bookmark = = " diffusion/deps/lib" ) , " amber waits for a person" ) ;
4793
}
e8fa5369 Andy McCall 24 days ago ↶
4794
4795
4796
4797
4798
4799
# [ tokio ::test ( flavor = " multi_thread" ) ]
4800
async fn one_job_reruns_with_what_depends_on_it ( ) {
4801
let h = hub ( ) .await;
4802
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4803
create ( & h.app, " andy" , " flaky" ) .await;
4804
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4805
let c = client ( & format! ( " {} /andy/flaky" , h.base) , work.path ( ) ) ;
4806
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4807
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
4808
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
4809
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4810
let run = runs_of ( & h.app, " andy" , " flaky" ) .await[ 0 ] .clone ( ) ;
4811
4812
let ( st, _ ) =
4813
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/test/rerun" , run.number) , Some ( " site" ) , None ) .await;
4814
assert_eq! ( st, StatusCode:: CONFLICT , " not while the run is live" ) ;
4815
while let Some ( j) = claim ( & h.app) .await {
4816
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4817
let ( name, ph) = ( j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4818
let exit = if name = = " test" { 1 } else { 0 } ;
4819
let log = serde_json:: json! ( { " records" : [ { " t" : 1 , " stream" : " out" , " text" : format! ( " {name} ran" ) } ] } ) ;
4820
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( log.to_string ( ) ) ) .await;
4821
assert_eq! (
4822
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : exit, " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, exit as u32 ) } ) )
4823
.await,
4824
StatusCode:: NO_CONTENT
4825
) ;
4826
}
4827
let status = | n : & 'static str | {
4828
let ( app, run_id) = ( h.app.clone ( ) , run.id) ;
4829
async move { db:: flow:: jobs( & app.db, run_id) .await.unwrap ( ) .into_iter ( ) .find ( | j | j.name = = n ) .unwrap ( ) }
4830
} ;
4831
assert_eq! ( db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) .status, " failed" ) ;
4832
let failed_test = status ( " test" ) .await;
4833
4834
4835
let ( st, body) =
4836
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/test/rerun" , run.number) , Some ( " site" ) , None ) .await;
4837
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4838
assert! ( body.contains ( r # ""queued":1"# ) , " {body}" ) ;
4839
assert_eq! (
4840
( status ( " build" ) .await.status, status ( " lint" ) .await.status, status ( " test" ) .await.status) ,
4841
( " passed" .into ( ) , " passed" .into ( ) , " queued" .into ( ) )
4842
) ;
4843
assert! ( db:: flow:: log( & h.app.db, failed_test.id, 0 , 100 ) .await.unwrap ( ) .is_empty ( ) , " its old log is gone" ) ;
4844
assert! ( db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) .live ( ) ) ;
4845
let checks = checks_when ( & h.app, run.repo_id, & run.state, 3 ) .await;
4846
assert_eq! ( checks.iter ( ) .find ( | c | c.name = = " flow/test" ) .map ( | c | c.status.as_str ( ) ) , Some ( " queued" ) ) ;
4847
let j = claim ( & h.app) .await.expect ( " the test, queued again" ) ;
4848
assert_eq! ( j[ " job" ] [ " name" ] , " test" ) ;
4849
let ph = j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
4850
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4851
assert_eq! (
4852
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( " test" , & ph, 0 ) } ) ) .await,
4853
StatusCode:: NO_CONTENT
4854
) ;
4855
assert_eq! ( db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) .status, " passed" , " the run is whole again" ) ;
bff9106e Andy McCall 23 days ago ↶
4856
4857
let andy_id = db:: accounts:: account_by_name( & h.app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
4858
let kinds: Vec< String> = db:: notify:: inbox( & h.app.db, andy_id, 20 ) .await.unwrap ( ) .into_iter ( ) .map ( | n | n.kind ) .collect ( ) ;
4859
assert! ( kinds.contains ( & " flow.failed" .to_string ( ) ) & & kinds.contains ( & " flow.fixed" .to_string ( ) ) , " {kinds:?}" ) ;
e8fa5369 Andy McCall 24 days ago ↶
4860
4861
4862
let ( _ , body) =
4863
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/build/rerun" , run.number) , Some ( " site" ) , None ) .await;
4864
assert! ( body.contains ( r # ""queued":2"# ) , " {body}" ) ;
4865
assert_eq! (
4866
( status ( " build" ) .await.status, status ( " lint" ) .await.status, status ( " test" ) .await.status) ,
4867
( " queued" .into ( ) , " passed" .into ( ) , " queued" .into ( ) )
4868
) ;
4869
let ( st, _ ) =
4870
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/deploy/rerun" , run.number) , Some ( " site" ) , None ) .await;
4871
assert_eq! ( st, StatusCode:: CONFLICT , " live again, and no such job either way" ) ;
4872
}
4a4400c5 Andy McCall 24 days ago ↶
4873
4874
4875
4876
4877
4878
# [ tokio ::test ( flavor = " multi_thread" ) ]
4879
async fn failed_runs_are_told_and_an_erroring_runner_is_drained ( ) {
4880
let h = hub ( ) .await;
4881
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4882
let bob = db:: accounts:: create_account( & h.app.db, " bob" , " [email protected] " , " x" , " salt" , false ) .await.unwrap ( ) ;
4883
create ( & h.app, " andy" , " alerts" ) .await;
4884
let row = db:: repos:: get( & h.app.db, " andy" , " alerts" ) .await.unwrap ( ) .unwrap ( ) ;
4885
db:: notify:: set_watch( & h.app.db, bob.id, row.id, true ) .await.unwrap ( ) ;
4886
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4887
let c = client ( & format! ( " {} /andy/alerts" , h.base) , work.path ( ) ) ;
4888
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4889
write ( work.path ( ) , " test.sh" , " exit 1\n " ) ;
4890
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
4891
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4892
let run = runs_of ( & h.app, " andy" , " alerts" ) .await[ 0 ] .clone ( ) ;
3b1bc27f Andy McCall 21 days ago
4893
sqlx:: query( " UPDATE flow_runs SET actor = 'andy' WHERE id = $1" ) .bind ( run.id) .execute ( & h.app.db) .await.unwrap ( ) ;
4a4400c5 Andy McCall 24 days ago ↶
4894
while let Some ( j) = claim ( & h.app) .await {
4895
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4896
let ( name, ph) = ( j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4897
let exit = if name = = " test" { 1 } else { 0 } ;
4898
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : exit, " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, exit as u32 ) } ) ) .await;
4899
}
4900
let told = | id : i64 | {
4901
let db = h.app.db.clone ( ) ;
4902
async move { db:: notify:: inbox( & db, id, 20 ) .await.unwrap ( ) .into_iter ( ) .filter ( | n | n.kind = = " flow.failed" ) .collect:: < Vec< _ > > ( ) }
4903
} ;
4904
let ( for_andy, for_bob) = ( told ( andy.id) .await, told ( bob.id) .await) ;
4905
assert_eq! ( ( for_andy.len ( ) , for_bob.len ( ) ) , ( 1 , 1 ) , " the pusher and a watcher" ) ;
4906
assert! ( for_andy[ 0 ] .title.contains ( & format! ( " run #{} on main failed (test)" , run.number) ) , " {}" , for_andy[ 0 ] .title) ;
4907
assert_eq! ( for_andy[ 0 ] .url, format! ( " /andy/alerts/flow/{} " , run.number) ) ;
4908
4909
4910
let runner_id = | | {
4911
let db = h.app.db.clone ( ) ;
4912
async move { sqlx:: query_scalar:: < _ , i64 > ( " SELECT id FROM flow_runners WHERE name = 'test-1'" ) .fetch_one ( & db) .await.unwrap ( ) }
4913
} ;
4914
let mut errors = 0 ;
4915
while errors < 5 {
4916
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/alerts/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
4917
assert_eq! ( st, StatusCode:: CREATED ) ;
4918
while let Some ( j) = claim ( & h.app) .await {
4919
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4920
let st = finish (
4921
& h.app,
4922
id,
4923
serde_json:: json! ( { " exit_code" : null, " duration_ms" : 10 , " error" : " the microVM did not start: /dev/kvm busy" } ) ,
4924
)
4925
.await;
4926
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
4927
errors + = 1 ;
4928
if errors = = 5 {
4929
break ;
4930
}
4931
}
4932
}
4933
let state = | id : i64 | {
4934
let db = h.app.db.clone ( ) ;
4935
async move { db:: runners:: get( & db, id) .await.unwrap ( ) .unwrap ( ) .state }
4936
} ;
4937
let id = runner_id ( ) .await;
4938
assert_eq! ( state ( id) .await, " draining" ) ;
4939
let drained: Vec< _ > =
4940
db:: notify:: inbox( & h.app.db, andy.id, 50 ) .await.unwrap ( ) .into_iter ( ) .filter ( | n | n.kind = = " flow.runner_drained" ) .collect ( ) ;
4941
assert_eq! ( drained.len ( ) , 1 , " the administrator is told" ) ;
4942
assert! ( drained[ 0 ] .title.contains ( " /dev/kvm busy" ) , " {}" , drained[ 0 ] .title) ;
4943
assert! (
4944
! told ( bob.id) .await.is_empty ( )
4945
& & db:: notify:: inbox( & h.app.db, bob.id, 50 ) .await.unwrap ( ) .iter ( ) .all ( | n | n.kind ! = " flow.runner_drained" )
4946
) ;
4947
4948
db:: runners:: set_state( & h.app.db, id, " active" ) .await.unwrap ( ) ;
4949
assert_eq! ( db:: runners:: erroring( & h.app.db, id, 5 ) .await.unwrap ( ) , None ) ;
f7bcee49 Andy McCall 23 days ago ↶
4950
}
4951
4952
4953
4954
# [ tokio ::test ( flavor = " multi_thread" ) ]
4955
async fn a_landed_proposal_runs_its_targets_pipeline ( ) {
4956
let h = hub ( ) .await;
4957
create ( & h.app, " andy" , " demo" ) .await;
4958
let url = format! ( " {} /andy/demo" , h.base) ;
4959
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4960
let mut c = client ( & url, work.path ( ) ) ;
4961
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4962
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
4963
c.snapshot ( " test" ) .unwrap ( ) ;
4964
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
4965
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4966
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , 1 ) ;
4967
c.bookmark_create ( " feature" , None ) .unwrap ( ) ;
4968
c.bookmark_switch ( " feature" , false ) .unwrap ( ) ;
4969
write ( work.path ( ) , " b.txt" , " more\n " ) ;
4970
c.snapshot ( " test" ) .unwrap ( ) ;
4971
c.commit ( " More" , & [ ] ) .unwrap ( ) ;
4972
assert! ( c.push_bookmark ( " origin" , " feature" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4973
let runs_before =
4974
db:: flow:: runs( & h.app.db, db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) .id, 10 , None ) .await.unwrap ( ) .len ( ) ;
4975
4976
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4977
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
4978
db:: proposals:: create(
4979
& h.app.db,
4980
db:: proposals:: NewProposal {
4981
repo_id: row.id,
4982
title: " More" ,
4983
body: " " ,
4984
author_id: Some ( andy.id) ,
4985
author: " andy" ,
4986
draft: false ,
4987
target: " main" ,
4988
source_kind: " bookmark" ,
4989
source_repo_id: Some ( row.id) ,
4990
source_bookmark: " feature" ,
4991
source_changes: " " ,
4992
} ,
4993
)
4994
.await
4995
.unwrap ( ) ;
4996
let repo = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
2a8b6216 Andy McCall 19 days ago ↶
4997
let state = oxygenhub:: web:: proposals:: land_as( & h.app, & repo, & row, & andy, 1 , " web" , false , None ) .await.ok ( ) .expect ( " landed" ) ;
f7bcee49 Andy McCall 23 days ago ↶
4998
let mut runs = vec! [ ] ;
4999
for _ in 0 .. 100 {
5000
runs = db:: flow:: runs( & h.app.db, row.id, 10 , None ) .await.unwrap ( ) ;
5001
if runs.len ( ) > runs_before {
5002
break ;
5003
}
5004
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
5005
}
5006
let landed = runs.iter ( ) .find ( | r | r.bookmark = = " main" & & r.state = = state ) .expect ( " a run of the landed state on main" ) ;
5007
assert_eq! ( ( landed.trigger.as_str ( ) , landed.actor.as_str ( ) ) , ( " push" , " andy" ) ) ;
4a4400c5 Andy McCall 24 days ago ↶
5008
}
1870e4fa Andy McCall 22 days ago ↶
5009
5010
5011
5012
# [ tokio ::test ( flavor = " multi_thread" ) ]
5013
async fn owners_run_flow_on_their_own_runners_only ( ) {
5014
use oxy_core:: identity:: SecretKey;
5015
let h = hub ( ) .await;
5016
let register = | name : & 'static str | {
5017
let app = h.app.clone ( ) ;
5018
async move {
c8684d44 Andy McCall 21 days ago
5019
let body = format! ( " username={name} &email={name} %40example.com&password=correct-horse-battery&setup=test-setup" ) ;
1870e4fa Andy McCall 22 days ago ↶
5020
let res = oxygenhub:: router( app)
5021
.oneshot (
5022
Request:: post( " /register" )
5023
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
5024
.body ( Body:: from( body) )
5025
.unwrap ( ) ,
5026
)
5027
.await
5028
.unwrap ( ) ;
5029
res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( )
5030
}
5031
} ;
5032
register ( " andy" ) .await;
5033
let bob = register ( " bob" ) .await;
5034
create ( & h.app, " bob" , " game" ) .await;
5035
let web = | method : & 'static str , path : & str , body : Option< String> | {
5036
let ( app, cookie, path) = ( h.app.clone ( ) , bob.clone ( ) , path.to_string ( ) ) ;
5037
async move {
c7067c7b Andy McCall 17 days ago
5038
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
1870e4fa Andy McCall 22 days ago ↶
5039
if body.is_some ( ) {
5040
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
5041
}
c7067c7b Andy McCall 17 days ago
5042
let req = req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ;
5043
let mut res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
5044
5045
let back =
5046
res.headers ( ) .get ( header:: LOCATION ) .and_then ( | v | v.to_str ( ) .ok ( ) ) .filter ( | l | l.contains ( " /settings/" ) ) .map ( String :: from) ;
5047
if let Some ( to) = back {
5048
let req = Request:: get( to) .header ( header:: COOKIE , cookie) .body ( Body:: empty( ) ) .unwrap ( ) ;
5049
res = oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) ;
5050
}
1870e4fa Andy McCall 22 days ago ↶
5051
String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( )
5052
}
5053
} ;
5054
let csrf_of = | page : & str | {
5055
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
5056
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
5057
} ;
5058
c7067c7b Andy McCall 17 days ago
5059
let page = web ( " GET" , " /bob/game/settings/flow" , None ) .await;
1870e4fa Andy McCall 22 days ago ↶
5060
assert! ( page.contains ( " register a runner for this repository" ) & & ! page.contains ( " value=\" own\" " ) , " {page}" ) ;
5061
let page = web ( " POST" , " /bob/game/settings/flow" , Some ( format! ( " _csrf={} &flow=own" , csrf_of ( & page) ) ) ) .await;
5062
assert! ( page.contains ( " Register a runner for this repository" ) , " {page}" ) ;
5063
5064
let page = web ( " GET" , " /bob/game/settings" , None ) .await;
5065
let page = web ( " POST" , " /bob/game/settings/runners/token" , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
5066
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
5067
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
5068
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
5069
let identity = SecretKey:: generate( ) .identity ( " bobs" , " " , 1 ) ;
5070
let reg = serde_json:: json! ( { " token" : token, " name" : " bobs" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
5071
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
5072
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5073
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5074
c7067c7b Andy McCall 17 days ago
5075
let page = web ( " GET" , " /bob/game/settings/flow" , None ) .await;
1870e4fa Andy McCall 22 days ago ↶
5076
assert! ( page.contains ( " value=\" own\" " ) & & ! page.contains ( " value=\" on\" " ) , " {page}" ) ;
5077
let page = web ( " POST" , " /bob/game/settings/flow" , Some ( format! ( " _csrf={} &flow=on" , csrf_of ( & page) ) ) ) .await;
5078
assert! ( page.contains ( " Only site administrators let the site's runners" ) , " {page}" ) ;
5079
let page = web ( " POST" , " /bob/game/settings/flow" , Some ( format! ( " _csrf={} &flow=own" , csrf_of ( & page) ) ) ) .await;
5080
assert! ( page.contains ( " value=\" own\" checked" ) & & page.contains ( " start runs" ) , " {page}" ) ;
5081
assert_eq! ( db:: repos:: get( & h.app.db, " bob" , " game" ) .await.unwrap ( ) .unwrap ( ) .flow, " own" ) ;
5082
5083
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5084
let c = client ( & format! ( " {} /bob/game" , h.base) , work.path ( ) ) ;
5085
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5086
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5087
c.snapshot ( " t" ) .unwrap ( ) ;
5088
c.commit ( " A pipeline" , & [ ] ) .unwrap ( ) ;
5089
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5090
assert_eq! ( runs_of ( & h.app, " bob" , " game" ) .await.len ( ) , 1 , " a run started" ) ;
5091
assert! ( claim ( & h.app) .await.is_none ( ) , " the site's runner is not offered it" ) ;
5092
let body = serde_json:: json! ( { " runner" : " ignored" , " runtimes" : [ " microvm" ] , " info" : info } ) .to_string ( ) ;
5093
let ( st, job) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( & credential) , Some ( body) ) .await;
5094
assert_eq! ( st, StatusCode:: OK , " {job}" ) ;
5095
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & job) .unwrap ( ) [ " repo" ] , " bob/game" ) ;
5096
}
cd437c4f Andy McCall 22 days ago ↶
5097
5098
# [ tokio ::test ( flavor = " multi_thread" ) ]
5099
async fn a_job_takes_the_artifact_its_producer_kept ( ) {
5100
let h = hub ( ) .await;
5101
create ( & h.app, " andy" , " demo" ) .await;
5102
let url = format! ( " {} /andy/demo" , h.base) ;
5103
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5104
let c = client ( & url, work.path ( ) ) ;
5105
write (
5106
work.path ( ) ,
5107
" .flow/pipeline.kdl" ,
5108
& format! (
5109
" 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 "
5110
) ,
5111
) ;
5112
c.snapshot ( " test" ) .unwrap ( ) ;
5113
c.commit ( " Plan, then apply that plan" , & [ ] ) .unwrap ( ) ;
5114
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5115
5116
5117
let runs = runs_of ( & h.app, " andy" , " demo" ) .await;
5118
assert_eq! ( runs.len ( ) , 1 , " a run" ) ;
5119
let jobs = db:: flow:: jobs( & h.app.db, runs[ 0 ] .id) .await.unwrap ( ) ;
5120
assert! ( ! jobs.is_empty ( ) , " jobs: {:?}" , runs[ 0 ] ) ;
5121
let p = claim ( & h.app)
5122
.await
5123
.unwrap_or_else ( | | panic! ( " nothing to claim: {:?}" , jobs.iter ( ) .map ( | j | ( & j.name, & j.status, & j.error) ) .collect:: < Vec< _ > > ( ) ) ) ;
5124
assert_eq! ( p[ " job" ] [ " name" ] , " plan" ) ;
5125
let plan_id = p[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
5126
let plan = b " the reviewed plan" .to_vec ( ) ;
5127
let ( st, text) = upload ( & h.app, plan_id, " tfplan" , & plan) .await;
5128
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
5129
let blob = oxy_core:: repo:: Repo:: blob_ref( & plan) .to_hex ( ) ;
5130
let mut r = receipt ( " plan" , p[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
5131
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
5132
assert_eq! (
5133
finish ( & h.app, plan_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : r } ) ) .await,
5134
StatusCode:: NO_CONTENT
5135
) ;
5136
5137
5138
5139
let a = claim ( & h.app) .await.unwrap ( ) ;
5140
assert_eq! ( a[ " job" ] [ " name" ] , " apply" ) ;
5141
let apply_id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
5142
assert_eq! (
5143
a[ " inputs" ] ,
5144
serde_json:: json! ( [ { " artifact" : " tfplan" , " from" : " plan" , " path" : " /src/tfplan" , " blob" : blob } ] ) ,
5145
" the claim names the producer's blob"
5146
) ;
5147
let req = Request:: get( format! ( " /api/v1/flow/jobs/{apply_id} /inputs/0" ) ) .header ( header:: AUTHORIZATION , " Bearer runner-secret" ) ;
5148
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( req.body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
5149
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
5150
assert_eq! ( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) , plan) ;
5151
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/jobs/{apply_id} /inputs/1" ) , Some ( " runner-secret" ) , None ) .await;
5152
assert_eq! ( st, StatusCode:: NOT_FOUND , " no second input" ) ;
5153
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/jobs/{apply_id} /inputs/0" ) , None , None ) .await;
5154
assert_eq! ( st, StatusCode:: UNAUTHORIZED , " runners only" ) ;
5155
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/jobs/{plan_id} /inputs/0" ) , Some ( " runner-secret" ) , None ) .await;
5156
assert_eq! ( st, StatusCode:: CONFLICT , " only a running job's" ) ;
5157
}
417184c7 Andy McCall 22 days ago ↶
5158
5159
# [ tokio ::test ( flavor = " multi_thread" ) ]
5160
async fn a_job_waits_for_approval_and_is_approved_rejected_or_expires ( ) {
5161
let h = hub ( ) .await;
5162
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
5163
let bob = db:: accounts:: create_account( & h.app.db, " bob" , " [email protected] " , " x" , " salt" , false ) .await.unwrap ( ) ;
5164
let carol = db:: accounts:: create_account( & h.app.db, " carol" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
5165
create ( & h.app, " andy" , " gated" ) .await;
5166
let row = db:: repos:: get( & h.app.db, " andy" , " gated" ) .await.unwrap ( ) .unwrap ( ) ;
5167
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5168
let c = client ( & format! ( " {} /andy/gated" , h.base) , work.path ( ) ) ;
5169
write (
5170
work.path ( ) ,
5171
" .flow/pipeline.kdl" ,
5172
& format! (
47e37402 Andy McCall 21 days ago ↶
5173
" 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 22 days ago ↶
5174
) ,
5175
) ;
5176
let mut n = 0 ;
5177
let mut push = | | {
5178
n + = 1 ;
5179
write ( work.path ( ) , " n.txt" , & format! ( " {n} \n " ) ) ;
5180
c.snapshot ( " test" ) .unwrap ( ) ;
5181
c.commit ( & format! ( " Push {n} " ) , & [ ] ) .unwrap ( ) ;
5182
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5183
} ;
5184
5185
let plan_passes = | number : i64 | {
5186
let app = h.app.clone ( ) ;
5187
let row = row.clone ( ) ;
5188
async move {
5189
let mut run = None ;
5190
for _ in 0 .. 100 {
5191
if let Some ( r) = db:: flow:: run( & app.db, row.id, number) .await.unwrap ( ) {
5192
run = Some ( r) ;
5193
break ;
5194
}
5195
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
5196
}
5197
let run = run.expect ( " the run" ) ;
3b1bc27f Andy McCall 21 days ago
5198
sqlx:: query( " UPDATE flow_runs SET actor = 'andy' WHERE id = $1" ) .bind ( run.id) .execute ( & app.db) .await.unwrap ( ) ;
417184c7 Andy McCall 22 days ago ↶
5199
let j = claim ( & app) .await.expect ( " plan is claimed" ) ;
5200
assert_eq! ( j[ " job" ] [ " name" ] , " plan" ) ;
5201
let ( id, ph) = ( j[ " job_id" ] .as_i64 ( ) .unwrap ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
5202
finish ( & app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( " plan" , & ph, 0 ) } ) ) .await;
5203
db:: flow:: run( & app.db, row.id, number) .await.unwrap ( ) .unwrap ( )
5204
}
5205
} ;
5206
5207
5208
push ( ) ;
5209
let run = runs_of ( & h.app, " andy" , " gated" ) .await[ 0 ] .clone ( ) ;
7e01a766 Andy McCall 22 days ago ↶
5210
33d98147 Andy McCall 18 days ago
5211
let heard = | topic : String| match h.app.bus.live.since ( 0 , & [ topic] ) {
7e01a766 Andy McCall 22 days ago ↶
5212
oxygenhub:: events:: Replay:: Events( e) => e.into_iter ( ) .map ( | e | e.kind ) .collect:: < Vec< _ > > ( ) ,
5213
oxygenhub:: events:: Replay:: Reload => vec! [ " reload" .into ( ) ] ,
5214
} ;
5215
assert! ( heard ( oxygenhub:: events:: topic:: repo( row.id) ) .contains ( & " push" .to_string ( ) ) , " the push is on the repository's topic" ) ;
3b1bc27f Andy McCall 21 days ago
5216
sqlx:: query( " UPDATE flow_runs SET actor = 'andy' WHERE id = $1" ) .bind ( run.id) .execute ( & h.app.db) .await.unwrap ( ) ;
05f1a85a Andy McCall 20 days ago ↶
5217
let early = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & carol, true , None ) .await.unwrap_err ( ) ;
417184c7 Andy McCall 22 days ago ↶
5218
assert! ( early.contains ( " waits for plan first" ) , " {early}" ) ;
5219
let run = plan_passes ( run.number) .await;
5220
assert! ( claim ( & h.app) .await.is_none ( ) , " apply waits for a person, so no runner gets it" ) ;
7e01a766 Andy McCall 22 days ago ↶
5221
assert! ( heard ( oxygenhub:: events:: topic:: run( run.id) ) .contains ( & " jobs" .to_string ( ) ) , " the run's topic hears its jobs" ) ;
dc59d1b5 Andy McCall 22 days ago ↶
5222
5223
5224
let told = | id : i64 | {
5225
let db = h.app.db.clone ( ) ;
5226
async move { db:: notify:: inbox( & db, id, 20 ) .await.unwrap ( ) .into_iter ( ) .filter ( | n | n.kind = = " flow.approval" ) .count ( ) }
5227
} ;
5228
assert_eq! ( ( told ( carol.id) .await, told ( andy.id) .await, told ( bob.id) .await) , ( 1 , 0 , 0 ) ) ;
417184c7 Andy McCall 22 days ago ↶
5229
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/gated/flow/{} " , run.number) , None , None ) .await;
5230
assert! ( page.contains ( " waits for approval by someone with the <em>maintain</em> role, other than andy" ) , " the run page says so" ) ;
b1860d60 Andy McCall 10 days ago ↶
5231
5232
let ( _ , v) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/gated/flow/runs/{} " , run.number) , None , None ) .await;
5233
let v: serde_json:: Value = serde_json:: from_str( & v) .unwrap ( ) ;
5234
let apply = v[ " jobs" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | j | j[ " name" ] = = " apply" ) .unwrap ( ) .clone ( ) ;
5235
assert_eq! ( apply[ " approval" ] , " waiting" , " {v}" ) ;
5236
assert! ( v[ " jobs" ] .as_array ( ) .unwrap ( ) .iter ( ) .filter ( | j | j[ " name" ] ! = " apply" ) .all ( | j | j.get ( " approval" ) .is_none ( ) ) , " {v}" ) ;
05f1a85a Andy McCall 20 days ago ↶
5237
let own = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & andy, true , None ) .await.unwrap_err ( ) ;
417184c7 Andy McCall 22 days ago ↶
5238
assert! ( own.contains ( " someone other than andy" ) , " {own}" ) ;
05f1a85a Andy McCall 20 days ago ↶
5239
let no_role = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & bob, true , None ) .await.unwrap_err ( ) ;
417184c7 Andy McCall 22 days ago ↶
5240
assert! ( no_role.contains ( " takes the maintain role" ) , " {no_role}" ) ;
05f1a85a Andy McCall 20 days ago ↶
5241
assert_eq! ( oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & carol, true , None ) .await.unwrap ( ) , " apply: approved by carol" ) ;
dc59d1b5 Andy McCall 22 days ago ↶
5242
assert_eq! ( told ( carol.id) .await, 1 , " told once" ) ;
417184c7 Andy McCall 22 days ago ↶
5243
let j = claim ( & h.app) .await.expect ( " approved, it runs" ) ;
5244
assert_eq! ( j[ " job" ] [ " name" ] , " apply" ) ;
b644e175 Andy McCall 22 days ago ↶
5245
5246
let token =
5247
j[ " secrets" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | s | s[ " path" ] = = " /run/secrets/cloud" ) .unwrap ( ) [ " value" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5248
let claims = h.app.oidc ( ) .await.unwrap ( ) .verify ( & token, " https://sts.example" , db:: now_ms( ) ) .expect ( " a valid token" ) ;
5249
assert_eq! ( claims[ " environment" ] , " prod" ) ;
417184c7 Andy McCall 22 days ago ↶
5250
let ( id, ph) = ( j[ " job_id" ] .as_i64 ( ) .unwrap ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
5251
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( " apply" , & ph, 0 ) } ) ) .await;
5252
let audit = db:: accounts:: audit_rows( & h.app.db, 50 ) .await.unwrap ( ) ;
5253
assert! ( audit.iter ( ) .any ( | r | r.action = = " flow.approved" & & r.actor = = " carol" & & r.target.ends_with ( " #1 apply" ) ) , " audited" ) ;
5254
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/gated/flow/{} " , run.number) , None , None ) .await;
5255
assert! ( page.contains ( " approved by carol" ) , " the run says who" ) ;
b644e175 Andy McCall 22 days ago ↶
5256
let ( _ , page) = call ( & h.app, " GET" , " /andy/gated/deployments" , None , None ) .await;
5257
assert! ( page.contains ( " <strong>prod</strong>" ) & & page.contains ( " >#1</a>" ) & & page.contains ( " <td>carol</td>" ) , " the Deployments page" ) ;
5258
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/gated/deployments" , Some ( " site" ) , None ) .await;
5259
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
5260
assert_eq! ( ( list[ 0 ] [ " environment" ] .as_str ( ) , list[ 0 ] [ " approved_by" ] .as_str ( ) ) , ( Some ( " prod" ) , Some ( " carol" ) ) ) ;
417184c7 Andy McCall 22 days ago ↶
5261
05f1a85a Andy McCall 20 days ago ↶
5262
5263
5264
417184c7 Andy McCall 22 days ago ↶
5265
push ( ) ;
5266
let run = plan_passes ( 2 ) .await;
05f1a85a Andy McCall 20 days ago ↶
5267
let ( secret, hash) = oxygenhub:: auth:: new_token( ) ;
5268
let tok = db:: accounts:: create_token( & h.app.db, carol.id, " claude-code" , & hash, " repo:write" , " " , None ) .await.unwrap ( ) ;
5269
db:: accounts:: set_token_agent( & h.app.db, carol.id, tok.id, true ) .await.unwrap ( ) ;
4a68f106 Andy McCall 19 days ago ↶
5270
5271
05f1a85a Andy McCall 20 days ago ↶
5272
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 19 days ago ↶
5273
assert_eq! ( st, StatusCode:: CONFLICT , " {body}" ) ;
5274
assert! ( body.contains ( " decided by a person here" ) & & body.contains ( " Settings › OxygenFlow" ) , " {body}" ) ;
5275
let gated = db:: repos:: get( & h.app.db, " andy" , " gated" ) .await.unwrap ( ) .unwrap ( ) ;
5276
db:: repos:: set_agents_decide_gates( & h.app.db, gated.id, true ) .await.unwrap ( ) ;
5277
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 20 days ago ↶
5278
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5279
assert! ( body.contains ( " rejected by carol via claude-code" ) , " {body}" ) ;
417184c7 Andy McCall 22 days ago ↶
5280
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
5281
let apply = jobs.iter ( ) .find ( | j | j.name = = " apply" ) .unwrap ( ) ;
05f1a85a Andy McCall 20 days ago ↶
5282
assert_eq! ( ( apply.status.as_str ( ) , apply.error.as_str ( ) ) , ( " cancelled" , " rejected by carol via claude-code" ) ) ;
5283
assert_eq! ( ( apply.approved_by.as_str ( ) , apply.approved_via.as_str ( ) ) , ( " carol" , " claude-code" ) ) ;
5284
let audit = db:: accounts:: audit_rows( & h.app.db, 50 ) .await.unwrap ( ) ;
5285
assert! (
5286
audit.iter ( ) .any ( | r | r.action = = " flow.rejected" & & r.actor = = " carol" & & r.detail.contains ( " via claude-code" ) ) ,
5287
" audited with the agent"
5288
) ;
417184c7 Andy McCall 22 days ago ↶
5289
assert! ( claim ( & h.app) .await.is_none ( ) ) ;
5290
5291
5292
push ( ) ;
5293
let run = plan_passes ( 3 ) .await;
5294
assert_eq! ( oxygenhub:: flow:: expire_approvals( & h.app, db:: now_ms( ) + 3_600_000 ) .await.unwrap ( ) , 0 , " not yet" ) ;
5295
assert_eq! ( oxygenhub:: flow:: expire_approvals( & h.app, db:: now_ms( ) + 25 * 3_600_000 ) .await.unwrap ( ) , 1 ) ;
5296
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
5297
let apply = jobs.iter ( ) .find ( | j | j.name = = " apply" ) .unwrap ( ) ;
5298
assert_eq! ( ( apply.status.as_str ( ) , apply.error.as_str ( ) ) , ( " cancelled" , " no approval within 24h" ) ) ;
5299
}
67be8ac0 Andy McCall 22 days ago ↶
5300
5301
# [ tokio ::test ( flavor = " multi_thread" ) ]
5302
async fn live_streams_only_topics_the_viewer_may_hear_and_replays ( ) {
5303
let h = hub ( ) .await;
5304
create ( & h.app, " andy" , " open" ) .await;
5305
h.app.repos.create ( & RepoName:: parse( " andy/shut" ) .unwrap ( ) , None ) .unwrap ( ) ;
5306
db:: repos:: create( & h.app.db, " andy" , " shut" , " " , hub_api:: Visibility:: Private) .await.unwrap ( ) ;
5307
let open = db:: repos:: get( & h.app.db, " andy" , " open" ) .await.unwrap ( ) .unwrap ( ) ;
5308
let shut = db:: repos:: get( & h.app.db, " andy" , " shut" ) .await.unwrap ( ) .unwrap ( ) ;
33d98147 Andy McCall 18 days ago
5309
let live = & h.app.bus.live;
67be8ac0 Andy McCall 22 days ago ↶
5310
let first = live.publish ( & oxygenhub:: events:: topic:: repo( open.id) , " push" ) ;
5311
live.publish ( & oxygenhub:: events:: topic:: repo( shut.id) , " push" ) ;
5312
live.publish ( & oxygenhub:: events:: topic:: proposal( open.id, 3 ) , " review" ) ;
5313
5314
5315
let read = | path : String, last : Option< u64 > | {
5316
let app = h.app.clone ( ) ;
5317
async move {
5318
let mut req = Request:: get( & path) ;
5319
if let Some ( l) = last {
5320
req = req.header ( " last-event-id" , l.to_string ( ) ) ;
5321
}
5322
let res = oxygenhub:: router( app) .oneshot ( req.body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
5323
let status = res.status ( ) ;
5324
let mut body = res.into_body ( ) ;
5325
let mut text = String :: new( ) ;
5326
while let Ok ( Some ( Ok ( frame) ) ) = tokio:: time:: timeout( std:: time:: Duration:: from_millis( 300 ) , body.frame ( ) ) .await {
5327
if let Some ( d) = frame.data_ref ( ) {
5328
text.push_str ( & String :: from_utf8_lossy( d) ) ;
5329
}
5330
}
5331
( status, text)
5332
}
5333
} ;
5334
let topics = format! ( " /live?topics=repo:{} ,repo:{} /proposal:3" , open.id, open.id) ;
5335
5336
let ( st, text) = read ( topics.clone ( ) , Some ( first.id) ) .await;
5337
assert_eq! ( st, StatusCode:: OK ) ;
5338
assert! ( text.contains ( " event: change" ) & & text.contains ( & format! ( " \" topic\" :\" repo:{} /proposal:3\" " , open.id) ) , " {text}" ) ;
5339
assert! ( ! text.contains ( & format! ( " \" topic\" :\" repo:{} \" " , shut.id) ) , " never another topic: {text}" ) ;
5340
assert! ( ! text.contains ( & format! ( " id: {} \n " , first.id) ) , " not what was already seen: {text}" ) ;
5341
5342
let ( st, _ ) = read ( format! ( " /live?topics=repo:{} ,repo:{} " , open.id, shut.id) , None ) .await;
5343
assert_eq! ( st, StatusCode:: FORBIDDEN ) ;
5344
assert_eq! ( read ( " /live?topics=user:1" .into ( ) , None ) .await.0 , StatusCode:: FORBIDDEN ) ;
5345
assert_eq! ( read ( " /live?topics=nonsense" .into ( ) , None ) .await.0 , StatusCode:: BAD_REQUEST ) ;
5346
assert_eq! ( read ( " /live?topics=" .into ( ) , None ) .await.0 , StatusCode:: BAD_REQUEST ) ;
5347
5348
for _ in 0 .. oxygenhub:: events:: REPLAY {
5349
live.publish ( " repo:999999" , " push" ) ;
5350
}
5351
let ( _ , text) = read ( topics, Some ( first.id) ) .await;
5352
assert! ( text.contains ( " event: reload" ) , " {text}" ) ;
5353
}
47e37402 Andy McCall 21 days ago ↶
5354
5355
# [ tokio ::test ( flavor = " multi_thread" ) ]
5356
async fn a_sealed_approval_releases_exactly_the_reviewed_plan ( ) {
5357
use oxy_core:: approval:: { SignedApproval, Statement} ;
5358
use oxy_core:: hash:: ObjectRef;
5359
use oxy_core:: identity:: SecretKey;
5360
let h = hub ( ) .await;
5361
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
5362
let carol = db:: accounts:: create_account( & h.app.db, " carol" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
5363
let sk = SecretKey:: generate( ) ;
5364
let carol_id = sk.identity ( " Carol" , " [email protected] " , 1 ) ;
5365
db:: accounts:: link_identity( & h.app.db, carol.id, & carol_id.hash ( ) .to_hex ( ) , " Carol" , " [email protected] " , & carol_id.encode ( ) )
5366
.await
5367
.unwrap ( ) ;
5368
create ( & h.app, " andy" , " infra" ) .await;
5369
let row = db:: repos:: get( & h.app.db, " andy" , " infra" ) .await.unwrap ( ) .unwrap ( ) ;
5370
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5371
let c = client ( & format! ( " {} /andy/infra" , h.base) , work.path ( ) ) ;
5372
write (
5373
work.path ( ) ,
5374
" .flow/pipeline.kdl" ,
5375
& format! (
5376
" 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 "
5377
) ,
5378
) ;
5379
c.snapshot ( " test" ) .unwrap ( ) ;
5380
c.commit ( " Plan, then apply" , & [ ] ) .unwrap ( ) ;
5381
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5382
let run = runs_of ( & h.app, " andy" , " infra" ) .await[ 0 ] .clone ( ) ;
5383
let p = claim ( & h.app) .await.unwrap ( ) ;
5384
let plan_id = p[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
5385
let plan = b " the reviewed plan" .to_vec ( ) ;
5386
upload ( & h.app, plan_id, " tfplan" , & plan) .await;
5387
let mut r = receipt ( " plan" , p[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
5388
r[ " outputs" ] = serde_json:: json! ( [ oxy_core:: repo:: Repo:: blob_ref( & plan) .to_hex ( ) ] ) ;
5389
finish ( & h.app, plan_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : r } ) ) .await;
5390
let run = db:: flow:: run( & h.app.db, row.id, run.number) .await.unwrap ( ) .unwrap ( ) ;
5391
5392
05f1a85a Andy McCall 20 days ago ↶
5393
let plain = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & carol, true , None ) .await.unwrap_err ( ) ;
47e37402 Andy McCall 21 days ago ↶
5394
assert! ( plain.contains ( & format! ( " oxy seal --flow {} /andy/infra/flow/{} --job apply" , h.base, run.number) ) , " {plain}" ) ;
5395
5396
let ( st, body) =
5397
call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/infra/flow/runs/{} /jobs/apply/approval" , run.number) , Some ( " site" ) , None ) .await;
5398
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5399
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
5400
assert_eq! ( v[ " environment" ] , " prod" ) ;
5401
assert_eq! ( v[ " inputs" ] , serde_json:: json! ( [ oxy_core:: repo:: Repo:: blob_ref( & plan) .to_hex ( ) ] ) , " the reviewed plan" ) ;
5402
let mut repo = [ 0 u8 ; 16 ] ;
5403
repo.copy_from_slice ( & hex:: decode( v[ " repo" ] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ) ;
5404
let oref = | k : & str | ObjectRef:: from_hex( v[ k] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ;
5405
let statement = Statement {
5406
repo,
5407
run: v[ " run" ] .as_str ( ) .unwrap ( ) .into ( ) ,
5408
job: " apply" .into ( ) ,
5409
job_hash: oref ( " job_hash" ) ,
5410
state: oref ( " state" ) ,
5411
inputs: v[ " inputs" ] .as_array ( ) .unwrap ( ) .iter ( ) .map ( | i | ObjectRef:: from_hex( i.as_str ( ) .unwrap ( ) ) .unwrap ( ) ) .collect ( ) ,
5412
environment: " prod" .into ( ) ,
5413
approve: true ,
5414
} ;
5415
let seal = | s : & Statement, key : & SecretKey, id : & oxy_core:: identity:: Identity| {
5416
let body = serde_json:: json! ( { " signed" : hex:: encode( SignedApproval:: sign( s, id, key) .encode ( ) ) } ) .to_string ( ) ;
5417
let app = h.app.clone ( ) ;
5418
let path = format! ( " /api/v1/repos/andy/infra/flow/runs/{} /jobs/apply/seal" , run.number) ;
5419
async move { call ( & app, " POST" , & path, Some ( " site" ) , Some ( body) ) .await }
5420
} ;
5421
5422
let other_plan = Statement { inputs: vec! [ ObjectRef( [ 9 ; 32 ] ) ] , .. statement.clone ( ) } ;
5423
let ( st, body) = seal ( & other_plan, & sk, & carol_id) .await;
5424
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " approves something else" ) , " {st} {body}" ) ;
5425
let agent = SecretKey:: generate( ) ;
5426
let agent_id = agent.identity ( " Claude Code" , " [email protected] " , 1 ) ;
5427
let ( st, body) = seal ( & statement, & agent, & agent_id) .await;
5428
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " not linked to an account" ) , " {st} {body}" ) ;
5429
assert! ( claim ( & h.app) .await.is_none ( ) , " still waiting" ) ;
5430
5431
5432
let ( st, body) = seal ( & statement, & sk, & carol_id) .await;
5433
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5434
assert! ( body.contains ( " approved by carol, sealed" ) , " {body}" ) ;
5435
let name = oxygenhub:: repos:: RepoName:: parse( " andy/infra" ) .unwrap ( ) ;
5436
let state = statement.state;
5437
let kept = h.app.repos.read ( & name, move | r, _ | Ok ( r.store ( ) .approvals_for ( & state) ? ) ) .await.unwrap ( ) ;
5438
assert_eq! ( kept.len ( ) , 1 , " the approval is in the repository" ) ;
5439
let j = claim ( & h.app) .await.expect ( " released" ) ;
5440
assert_eq! ( j[ " job" ] [ " name" ] , " apply" ) ;
5441
let token =
5442
j[ " secrets" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | s | s[ " path" ] = = " /run/secrets/cloud" ) .unwrap ( ) [ " value" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5443
let claims = h.app.oidc ( ) .await.unwrap ( ) .verify ( & token, " https://sts.example" , db:: now_ms( ) ) .unwrap ( ) ;
5444
assert_eq! ( ( claims[ " environment" ] .as_str ( ) , claims[ " approved_by" ] .as_str ( ) ) , ( Some ( " prod" ) , Some ( " carol" ) ) ) ;
5445
assert_eq! ( claims[ " approval" ] .as_str ( ) , Some ( kept[ 0 ] .to_hex ( ) .as_str ( ) ) ) ;
5446
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/infra/flow/{} " , run.number) , None , None ) .await;
5447
assert! ( page.contains ( " sealed with their key" ) , " the run page says so" ) ;
5448
}
55673c65 Andy McCall 21 days ago ↶
5449
5450
5451
5452
# [ tokio ::test ( flavor = " multi_thread" ) ]
5453
async fn repositories_take_turns_on_the_runners ( ) {
5454
let h = hub ( ) .await;
5455
for name in [ " busy" , " quiet" ] {
5456
create ( & h.app, " andy" , name) .await;
5457
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5458
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
5459
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5460
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5461
c.snapshot ( " test" ) .unwrap ( ) ;
5462
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5463
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5464
assert_eq! ( runs_of ( & h.app, " andy" , name) .await.len ( ) , 1 ) ;
5465
}
5466
5467
5468
let order: Vec< String> = {
5469
let mut out = Vec :: new( ) ;
5470
for _ in 0 .. 3 {
5471
let c = claim ( & h.app) .await.expect ( " a job to claim" ) ;
5472
out.push ( c[ " repo" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
5473
}
5474
out
5475
} ;
5476
assert_eq! ( order, [ " andy/busy" , " andy/quiet" , " andy/busy" ] ) ;
5477
}
107eaba5 Andy McCall 21 days ago ↶
5478
53a0fb2e Andy McCall 20 days ago ↶
5479
5480
5481
5482
5483
# [ tokio ::test ( flavor = " multi_thread" ) ]
5484
async fn a_pipeline_lowers_its_jobs_and_expediting_still_wins ( ) {
5485
let h = hub ( ) .await;
5486
for ( name, extra) in [ ( " nightly" , " priority \" low\" \n " ) , ( " everyday" , " " ) ] {
5487
create ( & h.app, " andy" , name) .await;
5488
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5489
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
5490
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
5491
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5492
c.snapshot ( " test" ) .unwrap ( ) ;
5493
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5494
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5495
assert_eq! ( runs_of ( & h.app, " andy" , name) .await.len ( ) , 1 ) ;
5496
}
5497
let nightly = db:: repos:: get( & h.app.db, " andy" , " nightly" ) .await.unwrap ( ) .unwrap ( ) ;
5498
db:: repos:: set_flow_priority( & h.app.db, nightly.id, " high" ) .await.unwrap ( ) ;
5499
let q = db:: flow:: queue( & h.app.db) .await.unwrap ( ) ;
5500
assert! (
5501
q.iter ( ) .filter ( | e | e.name = = " nightly" ) .all ( | e | e.priority ( ) = = " low" & & e.priority_note = = " pipeline: low" ) ,
5502
" {:?}" ,
5503
q.iter ( ) .map ( | e | ( & e.name, e.priority ( ) , & e.priority_note) ) .collect:: < Vec< _ > > ( )
5504
) ;
5505
let repo_of = | c : serde_json:: Value| c[ " repo" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5506
5507
5508
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/everyday" ) ;
5509
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/everyday" ) ;
5510
let run = & runs_of ( & h.app, " andy" , " nightly" ) .await[ 0 ] ;
5511
assert! ( db:: flow:: expedite( & h.app.db, run.id, " expedited by andy: needed now" ) .await.unwrap ( ) ) ;
5512
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/nightly" , " an expedited run is urgent whatever its jobs say" ) ;
5513
}
5514
107eaba5 Andy McCall 21 days ago ↶
5515
5516
5517
# [ tokio ::test ( flavor = " multi_thread" ) ]
5518
async fn priority_then_turns_and_an_expedited_run_first ( ) {
5519
let h = hub ( ) .await;
5520
for name in [ " early" , " later" ] {
5521
create ( & h.app, " andy" , name) .await;
5522
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5523
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
5524
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5525
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5526
c.snapshot ( " test" ) .unwrap ( ) ;
5527
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5528
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5529
assert_eq! ( runs_of ( & h.app, " andy" , name) .await.len ( ) , 1 ) ;
5530
}
5531
let early = db:: repos:: get( & h.app.db, " andy" , " early" ) .await.unwrap ( ) .unwrap ( ) ;
5532
db:: repos:: set_flow_priority( & h.app.db, early.id, " low" ) .await.unwrap ( ) ;
5533
5534
let repo_of = | c : serde_json:: Value| c[ " repo" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5535
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/later" ) ;
5536
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/later" ) ;
5537
5538
let run = & runs_of ( & h.app, " andy" , " early" ) .await[ 0 ] ;
5539
assert! ( db:: flow:: expedite( & h.app.db, run.id, " expedited by andy: an incident" ) .await.unwrap ( ) ) ;
76f93eae Andy McCall 21 days ago ↶
5540
5541
assert_eq! ( db:: flow:: expedited_live( & h.app.db, " andy" ) .await.unwrap ( ) , 1 ) ;
5542
assert_eq! ( db:: flow:: expedited_live( & h.app.db, " someone-else" ) .await.unwrap ( ) , 0 ) ;
107eaba5 Andy McCall 21 days ago ↶
5543
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/early" ) ;
5544
let q = db:: flow:: queue( & h.app.db) .await.unwrap ( ) ;
5545
assert! (
5546
q.iter ( ) .any ( | e | e.name = = " early" & & e.priority ( ) = = " urgent" & & e.priority_note.contains ( " an incident" ) ) ,
5547
" {:?}" ,
5548
q.iter ( ) .map ( | e | ( & e.name, e.priority ( ) ) ) .collect:: < Vec< _ > > ( )
5549
) ;
5550
5551
db:: accounts:: set_setting( & h.app.db, " flow_priority" , " high" ) .await.unwrap ( ) ;
5552
let q = db:: flow:: queue( & h.app.db) .await.unwrap ( ) ;
5553
assert! ( q.iter ( ) .filter ( | e | e.name = = " later" ) .all ( | e | e.priority ( ) = = " high" ) ) ;
5554
}
026a76d2 Andy McCall 20 days ago ↶
5555
5556
5557
5558
5559
5560
5561
# [ tokio ::test ( flavor = " multi_thread" ) ]
5562
async fn a_running_job_gets_a_short_certificate_for_a_fresh_key ( ) {
5563
use oxy_core:: cert:: { certify_challenge, SignedCertificate} ;
5564
use oxy_core:: identity:: { Scope, SecretKey} ;
5565
let h = hub ( ) .await;
5566
create ( & h.app, " andy" , " demo" ) .await;
5567
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5568
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
5569
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5570
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5571
c.snapshot ( " test" ) .unwrap ( ) ;
5572
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5573
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5574
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , 1 ) ;
5575
let job = claim ( & h.app) .await.expect ( " a job" ) ;
5576
let token = job[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5577
let job_id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
5578
5579
let sk = SecretKey:: generate( ) ;
5580
let id = sk.identity ( " job" , " [email protected] " , 1 ) ;
5581
let ask = | credential : & str , signed_for : & str | {
5582
let proof = sk.sign_challenge ( & id.hash ( ) , & certify_challenge ( signed_for) ) .unwrap ( ) ;
5583
let body = serde_json:: json! ( {
5584
" identity" : oxy_core:: dsse:: base64( & id.encode ( ) ) ,
5585
" proof" : oxy_core:: dsse:: base64( & oxy_core:: cbor:: encode( & proof.to_value ( ) ) ) ,
5586
" scope" : " changes,refs" ,
5587
} ) ;
5588
let ( credential, app) = ( credential.to_string ( ) , h.app.clone ( ) ) ;
5589
async move { call ( & app, " POST" , " /api/v1/keyless/certify" , Some ( & credential) , Some ( body.to_string ( ) ) ) .await }
5590
} ;
5591
let ( st, body) = ask ( & token, & token) .await;
5592
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5593
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
5594
let sc = SignedCertificate:: decode( & oxy_core:: dsse:: base64_decode( v[ " certificate" ] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ) .unwrap ( ) ;
5595
assert! ( sc.verify ( & h.app.hub_key.identity) .is_ok ( ) , " signed with the hub's key" ) ;
5596
assert_eq! ( sc.cert.key_id ( ) , id.hash ( ) ) ;
5597
assert_eq! ( sc.cert.subject, " repo:andy/demo:bookmark:main" ) ;
5598
assert_eq! ( sc.cert.claim ( " repository" ) , Some ( " andy/demo" ) ) ;
5599
assert! ( sc.cert.claim ( " job" ) .is_some ( ) & & sc.cert.claim ( " run" ) = = Some ( " 1" ) ) ;
5600
assert_eq! ( sc.cert.scope, Scope:: CHANGES | Scope:: REFS ) ;
5601
assert! ( sc.cert.expires - sc.cert.not_before < = 10 * 60_000 + 30_000 , " ten minutes" ) ;
5602
5603
5604
let ( st, body) = ask ( & token, " flowjob_someone_else" ) .await;
5605
assert_eq! ( st, StatusCode:: UNAUTHORIZED , " {body}" ) ;
5606
assert! ( body.contains ( " the proof does not show" ) , " {body}" ) ;
5607
5608
5609
5610
let issuer = h.app.oidc ( ) .await.unwrap ( ) ;
5611
let now = oxygenhub:: db:: now_ms( ) ;
5612
let aud = oxygenhub:: keyless:: audience( h.app.config.base_url ( ) ) ;
5613
let good = issuer.mint_for ( h.app.config.base_url ( ) , " agent:claude@andy" , & aud, now, 600 ) ;
5614
let ( st, body) = ask ( & good, & good) .await;
5615
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5616
assert! ( body.contains ( " \" subject\" :\" agent:claude@andy\" " ) , " {body}" ) ;
5617
let other = issuer.mint_for ( h.app.config.base_url ( ) , " agent:claude@andy" , " sts.amazonaws.com" , now, 600 ) ;
5618
let ( st, _ ) = ask ( & other, & other) .await;
5619
assert_eq! ( st, StatusCode:: UNAUTHORIZED ) ;
5620
5621
5622
sqlx:: query( " UPDATE flow_jobs SET status = 'passed' WHERE id = $1" ) .bind ( job_id) .execute ( & h.app.db) .await.unwrap ( ) ;
5623
let ( st, body) = ask ( & token, & token) .await;
5624
assert_eq! ( st, StatusCode:: UNAUTHORIZED , " {body}" ) ;
5625
}
2c31915f Andy McCall 20 days ago ↶
5626
5627
5628
5629
5630
5631
# [ tokio ::test ( flavor = " multi_thread" ) ]
5632
async fn a_receipt_signed_by_a_certified_key_is_kept ( ) {
5633
use oxy_core:: cert:: { certify_challenge, SignedCertificate} ;
5634
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectType} ;
5635
use oxy_core:: identity:: SecretKey;
5636
use oxy_core:: objects:: StateObject;
5637
use oxy_core:: receipt:: SignedReceipt;
5638
let h = hub ( ) .await;
5639
create ( & h.app, " andy" , " demo" ) .await;
5640
let url = format! ( " {} /andy/demo" , h.base) ;
5641
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5642
let c = client ( & url, work.path ( ) ) ;
5643
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5644
c.snapshot ( " test" ) .unwrap ( ) ;
5645
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5646
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5647
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
5648
let heads: Vec< ChangeHash> = run.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
5649
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads.clone ( ) ) .encode ( ) ) ;
5650
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
5651
5652
5653
5654
let certify = | token : String| {
5655
let app = h.app.clone ( ) ;
5656
async move {
5657
let sk = SecretKey:: generate( ) ;
5658
let id = sk.identity ( " test-1 job" , " " , 1 ) ;
5659
let proof = sk.sign_challenge ( & id.hash ( ) , & certify_challenge ( & token) ) .unwrap ( ) ;
5660
let body = serde_json:: json! ( {
5661
" identity" : oxy_core:: dsse:: base64( & id.encode ( ) ) ,
5662
" proof" : oxy_core:: dsse:: base64( & oxy_core:: cbor:: encode( & proof.to_value ( ) ) ) ,
5663
" scope" : " seal" ,
5664
} ) ;
5665
let ( st, body) = call ( & app, " POST" , " /api/v1/keyless/certify" , Some ( & token) , Some ( body.to_string ( ) ) ) .await;
5666
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5667
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
5668
let cert = SignedCertificate:: decode( & oxy_core:: dsse:: base64_decode( v[ " certificate" ] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ) .unwrap ( ) ;
5669
( sk, id, cert)
5670
}
5671
} ;
5672
let sign = | r : & serde_json:: Value,
5673
sk: & SecretKey,
5674
id: & oxy_core:: identity:: Identity,
5675
cert: SignedCertificate,
5676
issuer: oxy_core:: identity:: Identity| {
5677
let r: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
5678
let run_url = format! ( " {url} /flow/{} " , run.number) ;
5679
hex:: encode( SignedReceipt:: sign( r.to_oxy ( ) , repo_id, state, & run_url, id, sk) .with_certificate ( cert, issuer) .encode ( ) )
5680
} ;
5681
let hub_id = h.app.hub_key.identity.clone ( ) ;
5682
5683
let a = claim ( & h.app) .await.unwrap ( ) ;
5684
let b = claim ( & h.app) .await.unwrap ( ) ;
5685
let ph = a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5686
let ( a_name, b_name) = ( a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , b[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
5687
let ( a_id, b_id) = ( a[ " job_id" ] .as_i64 ( ) .unwrap ( ) , b[ " job_id" ] .as_i64 ( ) .unwrap ( ) ) ;
5688
let ( sk_a, id_a, cert_a) = certify ( a[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) .await;
5689
5690
5691
let rb = receipt ( & b_name, & ph, 0 ) ;
5692
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 ( ) ) } ) ;
5693
assert_eq! ( finish ( & h.app, b_id, body) .await, StatusCode:: NO_CONTENT ) ;
5694
5695
let ra = receipt ( & a_name, & ph, 0 ) ;
5696
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 ( ) ) } ) ;
5697
assert_eq! ( finish ( & h.app, a_id, body) .await, StatusCode:: NO_CONTENT ) ;
5698
5699
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
5700
let job = | n : & str | jobs.iter ( ) .find ( | j | j.name = = n ) .unwrap ( ) .clone ( ) ;
5701
assert_eq! ( job ( & a_name) .receipt_attest.len ( ) , 64 , " {}" , job ( & a_name) .receipt_note) ;
5702
assert! ( job ( & a_name) .receipt_signer.ends_with ( " (short-lived key)" ) , " {}" , job ( & a_name) .receipt_signer) ;
5703
assert! ( job ( & b_name) .receipt_attest.is_empty ( ) ) ;
5704
assert! ( job ( & b_name) .receipt_note.contains ( " another run or job" ) , " {}" , job ( & b_name) .receipt_note) ;
5705
5706
let kept = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .receipts ( & heads) .unwrap ( ) ;
5707
assert_eq! ( kept.len ( ) , 1 ) ;
5708
assert! ( kept[ 0 ] .problem.is_none ( ) & & kept[ 0 ] .signed.certificate.is_some ( ) ) ;
5709
5710
5711
let t = claim ( & h.app) .await.unwrap ( ) ;
5712
let t_name = t[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5713
let fake_sk = SecretKey:: generate( ) ;
5714
let fake = fake_sk.identity ( " not the hub" , " " , 1 ) ;
5715
let ( sk_t , id_t , mut cert_t ) = certify ( t[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) .await;
5716
cert_t .cert.issuer = fake.hash ( ) ;
5717
let cert_t = SignedCertificate:: sign( cert_t .cert, & fake, & fake_sk) ;
5718
let rt = receipt ( & t_name, & ph, 0 ) ;
5719
let body =
5720
serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 500 , " receipt" : rt, " signed_receipt" : sign ( & rt, & sk_t , & id_t , cert_t , fake) } ) ;
5721
assert_eq! ( finish ( & h.app, t[ " job_id" ] .as_i64 ( ) .unwrap ( ) , body) .await, StatusCode:: NO_CONTENT ) ;
5722
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
5723
let t_job = jobs.iter ( ) .find ( | j | j.name = = t_name ) .unwrap ( ) ;
5724
assert! ( t_job.receipt_note.contains ( " not issued by this hub" ) , " {}" , t_job.receipt_note) ;
5725
}
3eb491e9 Andy McCall 20 days ago ↶
5726
24472413 Andy McCall 20 days ago ↶
5727
5728
5729
5730
5731
# [ tokio ::test ( flavor = " multi_thread" ) ]
5732
async fn a_release_built_by_a_certified_key_is_attested ( ) {
5733
use oxy_core:: cert:: { certify_challenge, SignedCertificate} ;
5734
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectType} ;
5735
use oxy_core:: identity:: SecretKey;
5736
use oxy_core:: objects:: StateObject;
5737
use oxy_core:: receipt:: SignedReceipt;
5738
let h = hub ( ) .await;
5739
create ( & h.app, " andy" , " demo" ) .await;
5740
let url = format! ( " {} /andy/demo" , h.base) ;
5741
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5742
let mut c = client ( & url, work.path ( ) ) ;
5743
c.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
5744
write (
5745
work.path ( ) ,
5746
" .flow/pipeline.kdl" ,
5747
& format! (
5748
" 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 "
5749
) ,
5750
) ;
5751
c.snapshot ( " test" ) .unwrap ( ) ;
5752
c.commit ( " Build a tool" , & [ ] ) .unwrap ( ) ;
5753
let heads = c.bookmark_heads ( ) .unwrap ( ) ;
5754
c.tag_create ( " v1" , & heads, " first" ) .unwrap ( ) ;
5755
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5756
5757
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
5758
let a = claim ( & h.app) .await.unwrap ( ) ;
5759
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
5760
let token = a[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5761
let body: Vec< u8 > = b " #!/bin/sh\n echo tool\n " .to_vec ( ) ;
5762
let ( _ , text) = upload ( & h.app, id, " tool" , & body) .await;
5763
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5764
let mut r = receipt ( " build" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
5765
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
5766
let heads_run: Vec< ChangeHash> = run.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
5767
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads_run) .encode ( ) ) ;
5768
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
5769
5770
let sk = SecretKey:: generate( ) ;
5771
let key = sk.identity ( " test-1 job" , " " , 1 ) ;
5772
let proof = sk.sign_challenge ( & key.hash ( ) , & certify_challenge ( & token) ) .unwrap ( ) ;
5773
let ask = serde_json:: json! ( {
5774
" identity" : oxy_core:: dsse:: base64( & key.encode ( ) ) ,
5775
" proof" : oxy_core:: dsse:: base64( & oxy_core:: cbor:: encode( & proof.to_value ( ) ) ) ,
5776
" scope" : " seal" ,
5777
} ) ;
5778
let ( st, answer) = call ( & h.app, " POST" , " /api/v1/keyless/certify" , Some ( & token) , Some ( ask.to_string ( ) ) ) .await;
5779
assert_eq! ( st, StatusCode:: OK , " {answer}" ) ;
5780
let v: serde_json:: Value = serde_json:: from_str( & answer) .unwrap ( ) ;
5781
let cert = SignedCertificate:: decode( & oxy_core:: dsse:: base64_decode( v[ " certificate" ] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ) .unwrap ( ) ;
5782
let fr: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
5783
let signed = hex:: encode(
5784
SignedReceipt:: sign( fr.to_oxy ( ) , repo_id, state, & format! ( " {url} /flow/{} " , run.number) , & key, & sk)
5785
.with_certificate ( cert, h.app.hub_key.identity.clone ( ) )
5786
.encode ( ) ,
5787
) ;
5788
let done = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 900 , " receipt" : r, " signed_receipt" : signed } ) ;
5789
assert_eq! ( finish ( & h.app, id, done) .await, StatusCode:: NO_CONTENT ) ;
5790
5791
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
5792
let rel = db:: releases:: create( & h.app.db, row.id, " v1" , " One" , " " , false , None ) .await.unwrap ( ) ;
5793
oxygenhub:: web:: releases:: store_asset( & h.app, rel.id, " tool" , " " , & body) .await.unwrap ( ) ;
5794
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/releases/trust/v1" , None , None ) .await;
5795
assert_eq! ( st, StatusCode:: OK ) ;
5796
for want in [
5797
" tool: signed receipt from run #1 build" ,
5798
" a short-lived key this hub certified for repo:andy/demo:bookmark:main" ,
5799
" 1 of 1 assets built and attested by OxygenFlow" ,
5800
] {
5801
assert! ( page.contains ( want) , " missing {want:?}" ) ;
5802
}
5803
assert! ( ! page.contains ( " not one this hub" ) , " not an unknown key" ) ;
5804
}
5805
965b950f Andy McCall 20 days ago ↶
5806
5807
5808
5809
5810
# [ tokio ::test ( flavor = " multi_thread" ) ]
5811
async fn a_push_is_logged_and_the_log_checks_out ( ) {
5812
use oxy_core:: tlog:: { self , Checkpoint, Entry, Hash, Note, Tile} ;
5813
let h = hub ( ) .await;
5814
create ( & h.app, " andy" , " demo" ) .await;
5815
let url = format! ( " {} /andy/demo" , h.base) ;
5816
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5817
let mut c = client ( & url, work.path ( ) ) ;
5818
c.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
5819
write ( work.path ( ) , " a.txt" , " a\n " ) ;
5820
c.commit ( " a" , & [ ] ) .unwrap ( ) ;
5821
c.policy_init ( ) .unwrap ( ) ;
5822
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5823
let host = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
5824
let ( attest, _ ) = host.latest_ref_attest ( " main" ) .unwrap ( ) .unwrap ( ) ;
5825
5826
5827
let ( _ , sk) = oxygenhub:: keys:: load_secret( & h.app.config.keys_dir ( ) ) .unwrap ( ) ;
5828
h.app.tlog.refresh_checkpoint ( & h.app.hub_key.identity, & sk) .unwrap ( ) ;
5829
let ( st, note) = call ( & h.app, " GET" , " /log/checkpoint" , None , None ) .await;
5830
assert_eq! ( st, StatusCode:: OK , " {note}" ) ;
5831
let origin = oxygenhub:: tlog:: origin_of( h.app.config.base_url ( ) ) ;
5832
let cp = Checkpoint:: verify( & Note:: parse( & note) .unwrap ( ) , & origin, & h.app.hub_key.identity) .unwrap ( ) ;
5833
assert! ( cp.size > = 3 , " ref, seal and policy: {}" , cp.size) ;
5834
5835
5836
let bundle_path = Tile { level: 0 , n: 0 , width: cp.size as u32 } .entries_path ( ) ;
5837
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /log/{bundle_path} " ) , None , None ) .await;
5838
assert_eq! ( st, StatusCode:: OK ) ;
5839
let res =
5840
oxygenhub:: router( h.app.clone ( ) ) .oneshot ( Request:: get( format! ( " /log/{bundle_path} " ) ) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
5841
assert_eq! ( res.headers ( ) [ header:: CACHE_CONTROL ] , " public, max-age=5" , " a partial tile changes" ) ;
5842
let bytes = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
5843
let entries: Vec< Entry> = tlog:: decode_bundle( & bytes) .unwrap ( ) .iter ( ) .map ( | b | Entry:: decode( b) .unwrap ( ) ) .collect ( ) ;
5844
let kinds: Vec< & str > = entries.iter ( ) .map ( | e | e.kind.as_str ( ) ) .collect ( ) ;
5845
assert! ( kinds.contains ( & " ref" ) & & kinds.contains ( & " seal" ) & & kinds.contains ( & " policy" ) , " {kinds:?}" ) ;
5846
let r = entries.iter ( ) .find ( | e | e.kind = = " ref" ) .unwrap ( ) ;
5847
assert_eq! ( r.object, attest) ;
5848
assert_eq! ( r.repo, c.id ( ) .unwrap ( ) ) ;
5849
5850
5851
5852
let mut stored: Vec< Hash> = Vec :: new( ) ;
5853
for ( n, e) in entries.iter ( ) .enumerate ( ) {
5854
stored.extend ( tlog:: stored_hashes_for_leaf( n as u64 , e.leaf_hash ( ) , & stored) .unwrap ( ) ) ;
5855
}
5856
assert_eq! ( tlog:: tree_hash( cp.size, & stored) .unwrap ( ) , cp.root) ;
5857
let t = Tile { level: 0 , n: 0 , width: cp.size as u32 } ;
5858
let res =
5859
oxygenhub:: router( h.app.clone ( ) ) .oneshot ( Request:: get( format! ( " /log/{} " , t.path ( ) ) ) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
5860
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
5861
let data = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
5862
assert_eq! ( data.to_vec ( ) , t.read_data ( & stored) .unwrap ( ) ) ;
5863
let ( st, _ ) = call ( & h.app, " GET" , " /log/tile/0/000.p/200" , None , None ) .await;
5864
assert_eq! ( st, StatusCode:: NOT_FOUND , " not held yet" ) ;
5865
5866
5867
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5868
assert_eq! ( h.app.tlog.size ( ) , cp.size) ;
5869
let ( st, page) = call ( & h.app, " GET" , " /log" , None , None ) .await;
5870
assert_eq! ( st, StatusCode:: OK ) ;
5871
assert! ( page.contains ( " Transparency log" ) & & page.contains ( " andy/demo" ) & & page.contains ( & origin) , " {page}" ) ;
5872
}
5873
e5b97ac3 Andy McCall 20 days ago ↶
5874
5875
5876
5877
5878
5879
# [ tokio ::test ( flavor = " multi_thread" ) ]
5880
async fn a_fetch_checks_the_hub_s_log_and_refuses_a_fork ( ) {
5881
use oxy_core:: tlog:: { Checkpoint, Note} ;
5882
let h = hub ( ) .await;
5883
create ( & h.app, " andy" , " demo" ) .await;
5884
let url = format! ( " {} /andy/demo" , h.base) ;
5885
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5886
let mut a = client ( & url, & work.path ( ) .join ( " a" ) ) ;
5887
a.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
5888
let ( _ , sk) = oxygenhub:: keys:: load_secret( & h.app.config.keys_dir ( ) ) .unwrap ( ) ;
5889
let hub_id = h.app.hub_key.identity.clone ( ) ;
5890
let checkpoint = | | h.app.tlog.refresh_checkpoint ( & hub_id, & sk) .unwrap ( ) ;
5891
let mut n = 0 ;
5892
let mut push = | a : & Repo| {
5893
n + = 1 ;
5894
write ( & work.path ( ) .join ( " a" ) , " a.txt" , & format! ( " {n} \n " ) ) ;
5895
a.commit ( & format! ( " change {n} " ) , & [ ] ) .unwrap ( ) ;
5896
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5897
} ;
5898
push ( & a) ;
5899
checkpoint ( ) ;
5900
5901
let home = work.path ( ) .join ( " home-b" ) ;
5902
let b = Repo:: clone( & url, & work.path ( ) .join ( " b" ) ) .unwrap ( ) .with_home ( home.clone ( ) ) ;
5903
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5904
assert! ( report.log_notes.is_empty ( ) , " {:?}" , report.log_notes) ;
5905
let pin = std:: fs:: read_dir( home.join ( " tlog" ) ) .unwrap ( ) .next ( ) .unwrap ( ) .unwrap ( ) .path ( ) ;
5906
let seen = | p : & std:: path:: Path| {
5907
let t = std:: fs:: read_to_string( p) .unwrap ( ) ;
5908
Checkpoint:: parse( & Note:: parse( t.split_once ( ' \n ' ) .unwrap ( ) .1 ) .unwrap ( ) .text) .unwrap ( )
5909
} ;
5910
let first = seen ( & pin) ;
5911
5912
5913
push ( & a) ;
5914
checkpoint ( ) ;
5915
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5916
assert! ( report.log_notes.is_empty ( ) , " {:?}" , report.log_notes) ;
5917
assert! ( seen ( & pin) .size > first.size) ;
5918
5919
5920
push ( & a) ;
5921
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5922
assert! ( report.log_notes.iter ( ) .any ( | n | n.contains ( " not in its transparency log yet" ) ) , " {:?}" , report.log_notes) ;
5923
7dbb41b0 Andy McCall 20 days ago ↶
5924
5925
5926
let text = std:: fs:: read_to_string( & pin) .unwrap ( ) ;
5927
let other_sk = oxy_core:: identity:: SecretKey:: generate( ) ;
5928
let other = other_sk.identity ( " another hub" , " [email protected] " , 1 ) ;
5929
let note = text.split_once ( ' \n ' ) .unwrap ( ) .1 ;
5930
std:: fs:: write( & pin, format! ( " {} \n {note} " , oxy_core:: dsse:: base64( & other.encode ( ) ) ) ) .unwrap ( ) ;
5931
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5932
assert! ( report.log_notes.iter ( ) .any ( | n | n.contains ( " signed by another key" ) ) , " {:?}" , report.log_notes) ;
5933
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5934
assert! ( ! report.log_notes.iter ( ) .any ( | n | n.contains ( " another key" ) ) , " pinned now: {:?}" , report.log_notes) ;
5935
e5b97ac3 Andy McCall 20 days ago ↶
5936
5937
5938
let size = seen ( & pin) .size;
5939
let forged = Checkpoint { origin: oxygenhub:: tlog:: origin_of( h.app.config.base_url ( ) ) , size, root: [ 7 ; 32 ] , extensions: vec! [ ] } ;
5940
let text = std:: fs:: read_to_string( & pin) .unwrap ( ) ;
5941
let key = text.split_once ( ' \n ' ) .unwrap ( ) .0 ;
5942
std:: fs:: write( & pin, format! ( " {key} \n {} " , forged.sign ( & hub_id, & sk) .unwrap ( ) .encode ( ) ) ) .unwrap ( ) ;
5943
checkpoint ( ) ;
5944
let err = b.fetch ( " origin" ) .unwrap_err ( ) .to_string ( ) ;
5945
assert! ( err.contains ( " showed two histories" ) & & err.contains ( " does not extend" ) , " {err}" ) ;
5946
}
5947
4f40eed0 Andy McCall 20 days ago ↶
5948
5949
5950
5951
5952
5953
# [ tokio ::test ( flavor = " multi_thread" ) ]
5954
async fn a_hub_witnesses_another_hub_s_log ( ) {
5955
use oxy_core:: tlog:: { AddCheckpoint, Checkpoint, Note} ;
5956
let log = hub ( ) .await;
5957
let witness = hub ( ) .await;
5958
let origin = oxygenhub:: tlog:: origin_of( log.app.config.base_url ( ) ) ;
5959
witness.app.tlog.add_witness_for ( & origin, log.app.hub_key.identity.clone ( ) ) ;
5960
let name = witness.app.tlog.witness_name ( ) .to_string ( ) ;
5961
log.app.tlog.set_witnesses ( vec! [ oxygenhub:: tlog:: Peer {
5962
name: name.clone ( ) ,
5963
url: format! ( " {} /log/witness/add-checkpoint" , witness.base) ,
5964
identity: witness.app.hub_key.identity.clone ( ) ,
5965
} ] ) ;
5966
create ( & log.app, " andy" , " demo" ) .await;
5967
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5968
let mut c = client ( & format! ( " {} /andy/demo" , log.base) , work.path ( ) ) ;
5969
c.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
5970
let ( _ , sk) = oxygenhub:: keys:: load_secret( & log.app.config.keys_dir ( ) ) .unwrap ( ) ;
5971
let log_id = log.app.hub_key.identity.clone ( ) ;
5972
let wid = witness.app.hub_key.identity.clone ( ) ;
5973
for n in 0 .. 2 {
5974
write ( work.path ( ) , " a.txt" , & format! ( " {n} \n " ) ) ;
5975
c.commit ( & format! ( " change {n} " ) , & [ ] ) .unwrap ( ) ;
5976
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5977
log.app.tlog.refresh_checkpoint ( & log_id, & sk) .unwrap ( ) ;
5978
oxygenhub:: tlog:: collect( & log.app) .await;
5979
let note = Note:: parse( & log.app.tlog.checkpoint ( ) .unwrap ( ) ) .unwrap ( ) ;
5980
assert! ( note.cosigned_by ( & name, & wid) .is_ok ( ) , " round {n}: {}" , note.encode ( ) ) ;
5981
5982
let ( _ , served) = call ( & log.app, " GET" , " /log/checkpoint" , None , None ) .await;
5983
assert! ( Note:: parse( & served) .unwrap ( ) .cosigned_by ( & name, & wid) .is_ok ( ) ) ;
5984
}
5985
let cp = Checkpoint:: parse( & Note:: parse( & log.app.tlog.checkpoint ( ) .unwrap ( ) ) .unwrap ( ) .text) .unwrap ( ) ;
13b27cb2 Andy McCall 19 days ago ↶
5986
5987
5988
5989
5990
let ( _ , listed) = call ( & log.app, " GET" , " /log/witnesses" , None , None ) .await;
5991
assert! ( listed.starts_with ( & format! ( " {name} " ) ) , " {listed}" ) ;
5992
let url = format! ( " {} /andy/demo" , log.base) ;
5993
let refs = tokio:: task:: spawn_blocking( move | | {
5994
let opts = RemoteOptions { token: Some ( " site" .into ( ) ) , .. Default :: default( ) } ;
5995
open_remote ( & url, & opts) .unwrap ( ) .refs ( ) .unwrap ( )
5996
} )
5997
.await
5998
.unwrap ( ) ;
5999
assert_eq! ( refs.witnesses.len ( ) , 1 ) ;
6000
assert_eq! ( refs.witnesses[ 0 ] .0 , name) ;
6001
let main = refs.refs.iter ( ) .find ( | r | r.name = = " main" ) .expect ( " main" ) ;
6002
let logged = main.logged.as_ref ( ) .expect ( " logged" ) ;
6003
assert! ( ! main.integration.is_empty ( ) , " a witnessed checkpoint holds it" ) ;
6004
let cosigned: Vec< ( & str , & [[u8 ; 32]]) > = main.integration.iter ( ) .map ( | i | ( i.note.as_str ( ) , i.proof.as_slice ( ) ) ) .collect ( ) ;
6005
let at = oxy_core:: tlog:: integrated_time( & logged.entry, logged.index, & cosigned, & origin, & log_id, & [ ( name.clone ( ) , wid.clone ( ) ) ] )
6006
.expect ( " an integrated time" ) ;
6007
assert_eq! ( at.witness, name) ;
6008
assert! ( at.size > logged.index) ;
4f40eed0 Andy McCall 20 days ago ↶
6009
6010
let ask = | req : AddCheckpoint| {
6011
let app = witness.app.clone ( ) ;
6012
async move { call ( & app, " POST" , " /log/witness/add-checkpoint" , None , Some ( req.encode ( ) ) ) .await }
6013
} ;
6014
6015
let forked = Checkpoint { root: [ 7 ; 32 ] , .. cp.clone ( ) } .sign ( & log_id, & sk) .unwrap ( ) .encode ( ) ;
6016
let ( st, body) = ask ( AddCheckpoint { old_size: cp.size, proof: vec! [ ] , note: forked.clone ( ) } ) .await;
6017
assert_eq! ( st, StatusCode:: UNPROCESSABLE_ENTITY , " {body}" ) ;
6018
6019
let ( st, body) = ask ( AddCheckpoint { old_size: 1 , proof: vec! [ ] , note: forked } ) .await;
6020
assert_eq! ( ( st, body.trim ( ) ) , ( StatusCode:: CONFLICT , cp.size.to_string ( ) .as_str ( ) ) ) ;
6021
6022
let stranger = oxy_core:: identity:: SecretKey:: generate( ) ;
6023
let sid = stranger.identity ( " elsewhere" , " [email protected] " , 1 ) ;
6024
let other = Checkpoint { origin: " elsewhere.example/log" .into ( ) , .. cp.clone ( ) } .sign ( & sid, & stranger) .unwrap ( ) .encode ( ) ;
6025
let ( st, _ ) = ask ( AddCheckpoint { old_size: 0 , proof: vec! [ ] , note: other } ) .await;
6026
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
6027
}
6028
8dcaeeb8 Andy McCall 20 days ago ↶
6029
6030
6031
6032
6033
# [ tokio ::test ( flavor = " multi_thread" ) ]
6034
async fn a_job_keeps_its_terraform_state_on_the_hub ( ) {
6035
let h = hub ( ) .await;
6036
create ( & h.app, " andy" , " infra" ) .await;
6037
create ( & h.app, " andy" , " other" ) .await;
6038
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6039
let c = client ( & format! ( " {} /andy/infra" , h.base) , work.path ( ) ) ;
6040
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
6041
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
6042
c.snapshot ( " test" ) .unwrap ( ) ;
6043
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
6044
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6045
assert_eq! ( runs_of ( & h.app, " andy" , " infra" ) .await.len ( ) , 1 ) ;
6046
let job = claim ( & h.app) .await.expect ( " a job" ) ;
6047
let token = job[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
6048
let basic = format! ( " Basic {} " , oxy_core:: dsse:: base64( format! ( " flow:{token} " ) .as_bytes ( ) ) ) ;
6049
let path = " /api/v1/repos/andy/infra/terraform/state/prod" ;
6050
let send = | method : & str , uri : String, auth : Option< String> , body : & str | {
6051
let mut req = Request:: builder( ) .method ( method) .uri ( uri) ;
6052
if let Some ( a) = auth {
6053
req = req.header ( header:: AUTHORIZATION , a) ;
6054
}
6055
let req = req.body ( Body:: from( body.to_string ( ) ) ) .unwrap ( ) ;
6056
let app = h.app.clone ( ) ;
6057
async move {
6058
let res = oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) ;
6059
let st = res.status ( ) ;
6060
let b = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
6061
( st, String :: from_utf8_lossy( & b) .into_owned ( ) )
6062
}
6063
} ;
6064
let lock = r # "{"ID":"lock-1","Operation":"OperationTypeApply","Who":"flow@runner","Version":"1.8.0"}"# ;
6065
6066
assert_eq! ( send ( " GET" , path.into ( ) , None , " " ) .await.0 , StatusCode:: UNAUTHORIZED ) ;
6067
assert_eq! ( send ( " GET" , path.into ( ) , Some ( basic.clone ( ) ) , " " ) .await.0 , StatusCode:: NO_CONTENT ) ;
6068
6069
let ( st, body) = send ( " LOCK" , path.into ( ) , Some ( basic.clone ( ) ) , lock) .await;
6070
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
6071
let other_lock = r # "{"ID":"lock-2","Operation":"OperationTypePlan"}"# ;
6072
let ( st, held) = send ( " LOCK" , path.into ( ) , Some ( basic.clone ( ) ) , other_lock) .await;
6073
assert_eq! ( st, StatusCode:: LOCKED ) ;
6074
assert! ( held.contains ( " lock-1" ) , " the holder's lock info: {held}" ) ;
6075
let state1 = r # "{"version":4,"serial":1,"lineage":"l","resources":[{"secret":"hunter2"}]}"# ;
6076
assert_eq! ( send ( " POST" , path.into ( ) , Some ( basic.clone ( ) ) , state1) .await.0 , StatusCode:: LOCKED , " not under its lock" ) ;
6077
assert_eq! ( send ( " POST" , format! ( " {path} ?ID=lock-1" ) , Some ( basic.clone ( ) ) , state1) .await.0 , StatusCode:: OK ) ;
6078
assert_eq! ( send ( " UNLOCK" , path.into ( ) , Some ( basic.clone ( ) ) , other_lock) .await.0 , StatusCode:: CONFLICT , " not its lock" ) ;
6079
assert_eq! ( send ( " UNLOCK" , path.into ( ) , Some ( basic.clone ( ) ) , lock) .await.0 , StatusCode:: OK ) ;
6080
let ( st, got) = send ( " GET" , path.into ( ) , Some ( basic.clone ( ) ) , " " ) .await;
6081
assert_eq! ( ( st, got.as_str ( ) ) , ( StatusCode:: OK , state1) ) ;
6082
6083
let state2 = state1.replace ( " \" serial\" :1" , " \" serial\" :2" ) ;
6084
assert_eq! ( send ( " POST" , path.into ( ) , Some ( basic.clone ( ) ) , & state2) .await.0 , StatusCode:: OK ) ;
6085
let row = db:: repos:: get( & h.app.db, " andy" , " infra" ) .await.unwrap ( ) .unwrap ( ) ;
6086
assert_eq! ( db:: terraform:: versions( & h.app.db, row.id, " prod" ) .await.unwrap ( ) , 1 ) ;
6087
let kept = db:: terraform:: get( & h.app.db, row.id, " prod" ) .await.unwrap ( ) .unwrap ( ) ;
6088
assert_eq! ( kept.serial, 2 ) ;
6089
assert! ( ! String :: from_utf8_lossy( & kept.body) .contains ( " hunter2" ) , " sealed at rest" ) ;
6090
6091
let ( st, _ ) = send ( " GET" , " /api/v1/repos/andy/other/terraform/state/prod" .into ( ) , Some ( basic.clone ( ) ) , " " ) .await;
6092
assert_eq! ( st, StatusCode:: FORBIDDEN ) ;
6093
assert_eq! (
6094
send ( " GET" , " /api/v1/repos/andy/infra/terraform/state/..x" .into ( ) , Some ( basic.clone ( ) ) , " " ) .await.0 ,
6095
StatusCode:: BAD_REQUEST
6096
) ;
bb018142 Andy McCall 19 days ago ↶
6097
6098
6099
let mallory = db:: accounts:: create_account( & h.app.db, " mallory" , " [email protected] " , " x" , " salt" , false ) .await.unwrap ( ) ;
6100
let ( secret, hash) = oxygenhub:: auth:: new_token( ) ;
6101
db:: accounts:: create_token( & h.app.db, mallory.id, " cli" , & hash, " repo:write" , " " , None ) .await.unwrap ( ) ;
6102
let ( st, got) = send ( " GET" , path.into ( ) , Some ( format! ( " Bearer {secret} " ) ) , " " ) .await;
6103
assert_eq! ( st, StatusCode:: FORBIDDEN , " {got}" ) ;
6104
assert! ( ! got.contains ( " hunter2" ) ) ;
6105
let ( _ , page) = call ( & h.app, " GET" , " /andy/infra/terraform" , Some ( & secret) , None ) .await;
6106
assert! ( ! page.contains ( " <strong>prod</strong>" ) & & page.contains ( " shown to people who can write" ) , " {page}" ) ;
8dcaeeb8 Andy McCall 20 days ago ↶
6107
6108
let ( st, _ ) = send ( " GET" , path.into ( ) , Some ( " Bearer site" .into ( ) ) , " " ) .await;
6109
assert_eq! ( st, StatusCode:: UNAUTHORIZED , " the site token is not a person's token" ) ;
6110
6111
6112
let issuer = h.app.oidc ( ) .await.unwrap ( ) ;
6113
let now = db:: now_ms( ) ;
6114
let aud = oxygenhub:: terraform:: audience( h.app.config.base_url ( ) ) ;
6115
let own = issuer.mint_for ( h.app.config.base_url ( ) , " repo:andy/infra:bookmark:main" , & aud, now, 600 ) ;
6116
let ( st, got) = send ( " GET" , path.into ( ) , Some ( format! ( " Bearer {own} " ) ) , " " ) .await;
6117
assert_eq! ( ( st, got.contains ( " \" serial\" :2" ) ) , ( StatusCode:: OK , true ) ) ;
6118
let other = issuer.mint_for ( h.app.config.base_url ( ) , " repo:andy/other:bookmark:main" , & aud, now, 600 ) ;
6119
assert_eq! ( send ( " GET" , path.into ( ) , Some ( format! ( " Bearer {other} " ) ) , " " ) .await.0 , StatusCode:: FORBIDDEN ) ;
6120
let elsewhere = issuer.mint_for ( h.app.config.base_url ( ) , " repo:andy/infra:bookmark:main" , " sts.amazonaws.com" , now, 600 ) ;
6121
assert_eq! ( send ( " GET" , path.into ( ) , Some ( format! ( " Bearer {elsewhere} " ) ) , " " ) .await.0 , StatusCode:: UNAUTHORIZED ) ;
16ee5d19 Andy McCall 19 days ago ↶
6122
bb018142 Andy McCall 19 days ago ↶
6123
6124
let res = oxygenhub:: router( h.app.clone ( ) )
6125
.oneshot (
6126
Request:: post( " /register" )
6127
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
6128
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
6129
.unwrap ( ) ,
6130
)
6131
.await
6132
.unwrap ( ) ;
6133
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
6134
let res = oxygenhub:: router( h.app.clone ( ) )
67523e06 Andy McCall 19 days ago ↶
6135
.oneshot ( Request:: get( " /andy/infra/terraform" ) .header ( header:: COOKIE , cookie.clone ( ) ) .body ( Body:: empty( ) ) .unwrap ( ) )
bb018142 Andy McCall 19 days ago ↶
6136
.await
6137
.unwrap ( ) ;
6138
let st = res.status ( ) ;
6139
let page = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
16ee5d19 Andy McCall 19 days ago ↶
6140
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
6141
assert! ( page.contains ( " <strong>prod</strong>" ) & & page.contains ( " /api/v1/repos/andy/infra/terraform/state/NAME" ) , " {page}" ) ;
6142
assert! ( page.contains ( " allow "" ) | | page.contains ( " allow \" " ) , " the egress line names the hub" ) ;
67523e06 Andy McCall 19 days ago ↶
6143
assert! ( page.contains ( " 1 kept version" ) & & page.contains ( " >restore</button>" ) , " its versions, restorable by its owner: {page}" ) ;
6144
6145
6146
6147
6148
let andy = db:: accounts:: account_by_name( & h.app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
6149
let ( pat, hash) = oxygenhub:: auth:: new_token( ) ;
6150
db:: accounts:: create_token( & h.app.db, andy.id, " cli" , & hash, " repo:write" , " " , None ) .await.unwrap ( ) ;
6151
let bearer = format! ( " Bearer {pat} " ) ;
6152
let ( st, list) = send ( " GET" , format! ( " {path} /versions" ) , Some ( bearer.clone ( ) ) , " " ) .await;
6153
assert_eq! ( st, StatusCode:: OK , " {list}" ) ;
6154
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
6155
assert_eq! ( ( list.as_array ( ) .unwrap ( ) .len ( ) , list[ 0 ] [ " serial" ] .as_i64 ( ) ) , ( 1 , Some ( 1 ) ) , " {list}" ) ;
6156
let first = list[ 0 ] [ " id" ] .as_i64 ( ) .unwrap ( ) ;
6157
let ( st, got) = send ( " GET" , format! ( " {path} /versions/{first} " ) , Some ( bearer.clone ( ) ) , " " ) .await;
6158
assert_eq! ( ( st, got.as_str ( ) ) , ( StatusCode:: OK , state1) ) ;
6159
assert_eq! ( send ( " GET" , format! ( " {path} /versions" ) , Some ( format! ( " Bearer {secret} " ) ) , " " ) .await.0 , StatusCode:: FORBIDDEN , " a stranger" ) ;
6160
let restore = | id : i64 , auth : String| send ( " POST" , format! ( " {path} /versions/{id} /restore" ) , Some ( auth) , " " ) ;
6161
assert_eq! ( restore ( first, basic.clone ( ) ) .await.0 , StatusCode:: FORBIDDEN , " a job does not restore" ) ;
6162
assert_eq! ( restore ( first, format! ( " Bearer {secret} " ) ) .await.0 , StatusCode:: FORBIDDEN , " nor a stranger" ) ;
6163
assert_eq! ( restore ( 9999 , bearer.clone ( ) ) .await.0 , StatusCode:: NOT_FOUND ) ;
6164
assert_eq! ( send ( " LOCK" , path.into ( ) , Some ( basic.clone ( ) ) , lock) .await.0 , StatusCode:: OK ) ;
6165
assert_eq! ( restore ( first, bearer.clone ( ) ) .await.0 , StatusCode:: LOCKED , " not under someone's lock" ) ;
6166
assert_eq! ( send ( " UNLOCK" , path.into ( ) , Some ( basic.clone ( ) ) , lock) .await.0 , StatusCode:: OK ) ;
6167
let ( st, got) = restore ( first, bearer.clone ( ) ) .await;
6168
assert_eq! ( ( st, got.contains ( " \" serial\" :3" ) ) , ( StatusCode:: OK , true ) , " {got}" ) ;
6169
let ( _ , now) = send ( " GET" , path.into ( ) , Some ( bearer.clone ( ) ) , " " ) .await;
6170
let now: serde_json:: Value = serde_json:: from_str( & now) .unwrap ( ) ;
6171
assert_eq! (
6172
( now[ " serial" ] .as_i64 ( ) , now[ " lineage" ] .as_str ( ) , now[ " resources" ] [ 0 ] [ " secret" ] .as_str ( ) ) ,
6173
( Some ( 3 ) , Some ( " l" ) , Some ( " hunter2" ) )
6174
) ;
6175
6176
let ( _ , list) = send ( " GET" , format! ( " {path} /versions" ) , Some ( bearer.clone ( ) ) , " " ) .await;
6177
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
6178
let serials: Vec< i64 > = list.as_array ( ) .unwrap ( ) .iter ( ) .map ( | v | v[ " serial" ] .as_i64 ( ) .unwrap ( ) ) .collect ( ) ;
6179
assert_eq! ( serials, vec! [ 2 , 1 ] , " {list}" ) ;
6180
let ( st, got) = restore ( list[ 0 ] [ " id" ] .as_i64 ( ) .unwrap ( ) , bearer.clone ( ) ) .await;
6181
assert_eq! ( ( st, got.contains ( " \" serial\" :4" ) ) , ( StatusCode:: OK , true ) , " {got}" ) ;
6182
6183
6184
let page_with = | req : axum:: http:: request:: Builder, body : Body| {
6185
let ( app, cookie) = ( h.app.clone ( ) , cookie.clone ( ) ) ;
6186
async move {
6187
let res = oxygenhub:: router( app) .oneshot ( req.header ( header:: COOKIE , cookie) .body ( body) .unwrap ( ) ) .await.unwrap ( ) ;
6188
let st = res.status ( ) ;
6189
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
6190
}
6191
} ;
6192
let ( st, file) = page_with ( Request:: get( format! ( " /andy/infra/terraform/prod/download?version={first} " ) ) , Body:: empty( ) ) .await;
6193
assert_eq! ( ( st, file.as_str ( ) ) , ( StatusCode:: OK , state1) ) ;
6194
let ( _ , page) = page_with ( Request:: get( " /andy/infra/terraform" ) , Body:: empty( ) ) .await;
6195
let csrf = page.split ( " name=\" _csrf\" value=\" " ) .nth ( 1 ) .unwrap ( ) .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
6196
let ( st, page) = page_with (
6197
Request:: post( " /andy/infra/terraform/prod/restore" ) .header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ,
6198
Body:: from( format! ( " _csrf={csrf} &version={first} " ) ) ,
6199
)
6200
.await;
6201
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
6202
assert! ( page.contains ( " Restored prod: it is now serial 5." ) , " {page}" ) ;
6203
let audit = db:: accounts:: audit_rows( & h.app.db, 50 ) .await.unwrap ( ) ;
6204
assert_eq! ( audit.iter ( ) .filter ( | r | r.action = = " terraform.restored" ) .count ( ) , 3 , " every restore audited" ) ;
8dcaeeb8 Andy McCall 20 days ago ↶
6205
}
6206
6207
6208
6209
6210
6211
# [ tokio ::test ( flavor = " multi_thread" ) ]
6212
# [ ignore ]
6213
async fn tofu_keeps_its_state_on_the_hub ( ) {
6214
let h = hub ( ) .await;
6215
create ( & h.app, " andy" , " infra" ) .await;
6216
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6217
let c = client ( & format! ( " {} /andy/infra" , h.base) , work.path ( ) ) ;
6218
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
6219
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
6220
c.snapshot ( " test" ) .unwrap ( ) ;
6221
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
6222
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6223
assert_eq! ( runs_of ( & h.app, " andy" , " infra" ) .await.len ( ) , 1 ) ;
6224
let job = claim ( & h.app) .await.expect ( " a job" ) ;
6225
let token = job[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
6226
let tf = tempfile:: tempdir( ) .unwrap ( ) ;
6227
let address = format! ( " {} /api/v1/repos/andy/infra/terraform/state/prod" , h.base) ;
6228
std:: fs:: write(
6229
tf.path ( ) .join ( " main.tf" ) ,
6230
format! (
6231
" 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 "
6232
) ,
6233
)
6234
.unwrap ( ) ;
6235
let tofu = | args : & [& str ]| {
6236
let out = std:: process:: Command:: new( " docker" )
6237
.args ( [ " run" , " --rm" , " --network" , " host" , " -v" ] )
6238
.arg ( format! ( " {} :/w" , tf.path ( ) .display ( ) ) )
6239
.args ( [ " -w" , " /w" , " -e" ] )
6240
.arg ( format! ( " TF_HTTP_PASSWORD={token} " ) )
6241
.args ( [ " --entrypoint" , " tofu" , " oxygenflow-tofu:latest" ] )
6242
.args ( args)
6243
.output ( )
6244
.expect ( " docker" ) ;
6245
let text = format! ( " {} {} " , String :: from_utf8_lossy( & out.stdout) , String :: from_utf8_lossy( & out.stderr) ) ;
6246
assert! ( out.status.success ( ) , " tofu {args:?}: {text}" ) ;
6247
text
6248
} ;
6249
tofu ( & [ " init" , " -input=false" , " -no-color" ] ) ;
6250
let applied = tofu ( & [ " apply" , " -auto-approve" , " -input=false" , " -no-color" ] ) ;
6251
assert! ( applied.contains ( " 1 added" ) , " {applied}" ) ;
6252
let again = tofu ( & [ " apply" , " -auto-approve" , " -input=false" , " -no-color" ] ) ;
6253
assert! ( again.contains ( " No changes" ) , " the state was kept: {again}" ) ;
6254
let row = db:: repos:: get( & h.app.db, " andy" , " infra" ) .await.unwrap ( ) .unwrap ( ) ;
6255
let kept = db:: terraform:: get( & h.app.db, row.id, " prod" ) .await.unwrap ( ) .unwrap ( ) ;
6256
assert! ( kept.lock_id.is_empty ( ) , " unlocked after each run" ) ;
6257
assert! ( kept.serial > = 1 ) ;
6258
}
6259
8a48dd83 Andy McCall 20 days ago ↶
6260
6261
6262
# [ tokio ::test ( flavor = " multi_thread" ) ]
6263
async fn metrics_are_private_and_count_flow_jobs ( ) {
6264
let h = hub ( ) .await;
6265
let ( st, _ ) = call ( & h.app, " GET" , " /metrics" , None , None ) .await;
6266
assert_eq! ( st, StatusCode:: NOT_FOUND , " off by default" ) ;
6267
let text = oxygenhub:: metrics:: render( & h.app) .await;
6268
for want in [
6269
" # TYPE oxyhub_uptime_seconds gauge" ,
6270
" oxyhub_flow_jobs{state=\" queued\" } 0" ,
6271
" oxyhub_bucket_requests_total{kind=\" list\" }" ,
6272
" oxyhub_tlog_entries" ,
a6d824c5 Andy McCall 20 days ago ↶
6273
" oxyhub_db_connections{idle=\" true\" }" ,
6274
" oxyhub_pushes_last_day{result=\" refused\" } 0" ,
6275
" # TYPE oxyhub_backup_last_run_ok gauge" ,
2c47eecf Andy McCall 20 days ago ↶
6276
" # TYPE oxyhub_flow_runner_last_seen_seconds gauge" ,
6277
" oxyhub_flow_runs_last_day{result=\" failed\" } 0" ,
6278
" oxyhub_mail_total{result=\" sent\" }" ,
8a48dd83 Andy McCall 20 days ago ↶
6279
] {
6280
assert! ( text.contains ( want) , " missing {want}:\n {text}" ) ;
6281
}
6282
create ( & h.app, " andy" , " demo" ) .await;
6283
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6284
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
6285
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
6286
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
6287
c.snapshot ( " test" ) .unwrap ( ) ;
6288
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
6289
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6290
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , 1 ) ;
6291
let text = oxygenhub:: metrics:: render( & h.app) .await;
6292
assert! ( ! text.contains ( " oxyhub_flow_jobs{state=\" queued\" } 0" ) , " the run's jobs are queued:\n {text}" ) ;
a6d824c5 Andy McCall 20 days ago ↶
6293
assert! ( text.contains ( " oxyhub_pushes_last_day{result=\" accepted\" }" ) , " {text}" ) ;
85ec74fd Andy McCall 19 days ago ↶
6294
6295
6296
let pushed = text
6297
.lines ( )
6298
.find_map ( | l | l.strip_prefix ( " oxyhub_protocol_bytes_total{endpoint=\" push\" ,direction=\" received\" } " ) )
6299
.and_then ( | v | v.parse:: < f64 > ( ) .ok ( ) )
6300
.unwrap_or ( 0. 0 ) ;
6301
assert! ( pushed > 0. 0 , " {text}" ) ;
b9b74d99 Andy McCall 20 days ago ↶
6302
6303
call ( & h.app, " GET" , " /andy/demo" , None , None ) .await;
6304
let text = oxygenhub:: metrics:: render( & h.app) .await;
6305
assert! ( text.contains ( " oxyhub_http_requests_total{route=\" page:repository\" ,method=\" GET\" ,status=\" 2xx\" }" ) , " {text}" ) ;
6306
assert! ( text.contains ( " oxyhub_http_request_duration_seconds_bucket{route=\" page:repository\" ,method=\" GET\" ,le=\" +Inf\" }" ) ) ;
8a48dd83 Andy McCall 20 days ago ↶
6307
}
6308
e97500b7 Andy McCall 19 days ago ↶
6309
6310
6311
6312
6313
# [ tokio ::test ( flavor = " multi_thread" ) ]
6314
async fn insights_chart_pickup_from_when_a_job_could_run ( ) {
6315
let h = hub ( ) .await;
6316
create ( & h.app, " andy" , " demo" ) .await;
6317
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6318
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
6319
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
6320
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
6321
c.snapshot ( " test" ) .unwrap ( ) ;
6322
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
6323
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6324
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , 1 ) ;
6325
let mut finished = std:: collections:: HashMap:: new( ) ;
6326
for want in [ " build" , " lint" , " test" ] {
6327
let j = claim ( & h.app) .await.expect ( " a job" ) ;
6328
assert_eq! ( j[ " job" ] [ " name" ] .as_str ( ) , Some ( want) ) ;
6329
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
6330
let row = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
6331
let ( queued, ready, started) = ( row.queued_ms.unwrap ( ) , row.ready_ms.unwrap ( ) , row.started_ms.unwrap ( ) ) ;
6332
assert! ( queued < = ready & & ready < = started, " {want}: {queued} {ready} {started}" ) ;
6333
if want = = " test" {
6334
assert_eq! ( Some ( ready) , finished[ " build" ] , " ready when build passed, not when it was queued" ) ;
6335
}
6336
let ph = j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) ;
6337
let st = finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : receipt ( want, ph, 0 ) } ) ) .await;
6338
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
6339
finished.insert ( want, db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .finished_ms) ;
6340
}
6341
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/insights" , None , None ) .await;
6342
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
6343
assert! ( page.contains ( " <span class=\" kpi-v\" >3</span><span class=\" kpi-l\" >jobs finished" ) , " {page}" ) ;
6344
assert! ( page.contains ( " 100% passed" ) , " {page}" ) ;
6345
assert_eq! ( page.matches ( " <svg class=\" chart\" " ) .count ( ) , 5 , " five charts" ) ;
6346
assert! ( page.contains ( " class=\" bar s-ok\" " ) , " the passed jobs drawn" ) ;
6347
assert! ( page.contains ( " data-refresh=\" 30\" data-fragment=\" insights\" " ) , " it refreshes itself" ) ;
6348
assert! ( page.contains ( " <summary class=\" on\" >More</summary>" ) , " the bar's More menu holds it" ) ;
6349
let ( _ , wall) = call ( & h.app, " GET" , " /andy/demo/insights?range=7d&wall=1" , None , None ) .await;
6350
assert! ( wall.contains ( " <body class=\" wall\" " ) & & wall.contains ( " the last 7 days" ) & & wall.contains ( " leave the wall view" ) , " {wall}" ) ;
6351
6352
assert_eq! ( call ( & h.app, " GET" , " /insights/andy" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
6353
assert_eq! ( call ( & h.app, " GET" , " /admin/insights" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
6354
let res = oxygenhub:: router( h.app.clone ( ) )
6355
.oneshot (
6356
Request:: post( " /register" )
6357
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
6358
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
6359
.unwrap ( ) ,
6360
)
6361
.await
6362
.unwrap ( ) ;
6363
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
6364
let andy = db:: accounts:: account_by_name( & h.app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
6365
db:: accounts:: set_admin( & h.app.db, andy.id, true ) .await.unwrap ( ) ;
6366
for path in [ " /insights/andy" , " /admin/insights?runner=test-1&range=1h" ] {
6367
let res = oxygenhub:: router( h.app.clone ( ) )
6368
.oneshot ( Request:: get( path) .header ( header:: COOKIE , cookie.clone ( ) ) .body ( Body:: empty( ) ) .unwrap ( ) )
6369
.await
6370
.unwrap ( ) ;
6371
let st = res.status ( ) ;
6372
let body = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
6373
assert_eq! ( st, StatusCode:: OK , " {path}: {body}" ) ;
6374
assert! ( body.contains ( " <svg class=\" chart\" " ) , " {path}" ) ;
6375
}
6376
}
6377
7c06f5b2 Andy McCall 19 days ago ↶
6378
6379
6380
6381
# [ tokio ::test ( flavor = " multi_thread" ) ]
6382
async fn reads_do_not_wait_for_a_writer ( ) {
6383
use std:: time:: Duration;
6384
let h = hub ( ) .await;
6385
create ( & h.app, " andy" , " demo" ) .await;
6386
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6387
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
6388
write ( work.path ( ) , " a.txt" , " a\n " ) ;
6389
c.commit ( " A change" , & [ ] ) .unwrap ( ) ;
6390
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6391
let name = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
6392
let writer = h.app.repos.write_guard ( & name) .await;
6393
for path in [ " /andy/demo" , " /andy/demo/changes/main" , " /api/v1/repos/andy/demo/bookmarks" ] {
6394
let ( st, body) = tokio:: time:: timeout( Duration:: from_secs( 10 ) , call ( & h.app, " GET" , path, None , None ) )
6395
.await
6396
.unwrap_or_else ( | _| panic! ( " {path} waited for the writer" ) ) ;
6397
assert_eq! ( st, StatusCode:: OK , " {path}: {body}" ) ;
6398
}
6399
let refs = tokio:: time:: timeout( Duration:: from_secs( 10 ) , call ( & h.app, " POST" , " /andy/demo/oxy/v1/refs" , Some ( " site" ) , None ) ) .await;
6400
assert! ( refs.is_ok ( ) , " a fetch's refs waited for the writer" ) ;
6401
assert! ( tokio:: time:: timeout( Duration:: from_millis( 300 ) , h.app.repos.write_guard ( & name) ) .await.is_err ( ) , " writers take turns" ) ;
6402
assert! ( tokio:: time:: timeout( Duration:: from_millis( 300 ) , h.app.repos.exclusive_guard ( & name) ) .await.is_err ( ) , " a delete waits" ) ;
6403
drop ( writer) ;
6404
let all = tokio:: time:: timeout( Duration:: from_secs( 5 ) , h.app.repos.exclusive_guard ( & name) ) .await.expect ( " then it runs" ) ;
6405
assert! (
6406
tokio:: time:: timeout( Duration:: from_millis( 300 ) , h.app.repos.read_guard ( & name) ) .await.is_err ( ) ,
6407
" nothing reads a repository being deleted"
6408
) ;
6409
drop ( all) ;
6410
}
6411
a10446c6 Andy McCall 20 days ago ↶
6412
6413
6414
6415
6416
# [ tokio ::test ( flavor = " multi_thread" ) ]
6417
async fn the_issue_overview_and_diffusion_pages_listen_on_their_topics ( ) {
6418
let h = hub ( ) .await;
6419
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
6420
create ( & h.app, " andy" , " demo" ) .await;
6421
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
6422
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6423
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
6424
write ( work.path ( ) , " a.txt" , " a\n " ) ;
6425
c.snapshot ( " t" ) .unwrap ( ) ;
6426
c.commit ( " a" , & [ ] ) .unwrap ( ) ;
6427
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6428
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/issues" , Some ( " site" ) , Some ( r # "{"title":"Broken"}"# .into ( ) ) ) .await;
6429
assert! ( st.is_success ( ) , " {st} {body}" ) ;
6430
let repo = format! ( " repo:{} " , row.id) ;
6431
let ( _ , issue) = call ( & h.app, " GET" , " /andy/demo/issue/1" , None , None ) .await;
6432
assert! ( issue.contains ( & format! ( " data-live=\" {repo} /issue:1 {repo} \" data-fragment=\" timeline\" " ) ) , " the issue's timeline" ) ;
6433
let ( _ , overview) = call ( & h.app, " GET" , " /andy/demo" , None , None ) .await;
6434
assert! ( overview.contains ( & format! ( " data-live=\" {repo} \" data-fragment=\" latest\" " ) ) , " the latest change" ) ;
6435
assert! ( overview.contains ( & format! ( " data-live=\" {repo} \" data-fragment=\" bookmarks\" " ) ) , " the bookmarks" ) ;
6436
let ( _ , diffusion) = call ( & h.app, " GET" , " /andy/demo/diffusion" , None , None ) .await;
6437
assert! ( diffusion.contains ( & format! ( " data-live=\" diffusion:{} {repo} \" data-fragment=\" diffusion\" " , row.id) ) , " the Diffusion tab" ) ;
6438
}
6439
3eb491e9 Andy McCall 20 days ago ↶
6440
6441
6442
6443
6444
# [ tokio ::test ( flavor = " multi_thread" ) ]
6445
async fn the_hub_countersigns_an_update_by_a_key_it_certified ( ) {
6446
use oxy_core:: attest:: AttestBody;
6447
use oxy_core:: cert:: { Certificate, SignedCertificate} ;
6448
use oxy_core:: identity:: Scope;
6449
use oxy_core:: repo:: IdentityScope;
6450
let h = hub ( ) .await;
6451
create ( & h.app, " andy" , " demo" ) .await;
6452
let url = format! ( " {} /andy/demo" , h.base) ;
6453
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6454
let mut a = client ( & url, & work.path ( ) .join ( " a" ) ) .with_home ( work.path ( ) .join ( " home-a" ) ) ;
6455
a.create_identity ( " Andy" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6456
write ( & work.path ( ) .join ( " a" ) , " a.txt" , " a\n " ) ;
6457
a.commit ( " a" , & [ ] ) .unwrap ( ) ;
6458
a.policy_init ( ) .unwrap ( ) ;
6459
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6460
let hub_id = h.app.hub_key.identity.clone ( ) ;
6461
a.policy_grant_subject ( " agent:claude@andy" , hub_id.clone ( ) , Scope:: CHANGES | Scope:: REFS , None ) .unwrap ( ) ;
6462
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .any ( | r | r.name = = " policy" & & r.ok ) ) ;
6463
6464
6465
let mut b = Repo:: clone( & url, & work.path ( ) .join ( " b" ) ) .unwrap ( ) .with_home ( work.path ( ) .join ( " home-b" ) ) ;
6466
b.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
6467
b.save_config ( ) .unwrap ( ) ;
6468
let ( _ , key) = b.create_identity ( " claude" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6469
let ( _ , secret) = oxygenhub:: keys:: load_secret( & h.app.config.keys_dir ( ) ) .unwrap ( ) ;
6470
let now = db:: now_ms( ) as u64 ;
6471
let cert = SignedCertificate:: sign(
6472
Certificate {
6473
key,
6474
issuer: hub_id.hash ( ) ,
6475
subject: " agent:claude@andy" .into ( ) ,
6476
claims: vec! [ ] ,
6477
scope: Scope:: CHANGES | Scope:: REFS ,
6478
not_before: now - 60_000 ,
6479
expires: now + 600_000 ,
8aa5ef38 Andy McCall 20 days ago ↶
6480
grant: None ,
3eb491e9 Andy McCall 20 days ago ↶
6481
} ,
6482
& hub_id,
6483
& secret,
6484
) ;
6485
b.store ( ) .put_identity ( & hub_id) .unwrap ( ) ;
6486
b.store ( ) .put_certificate ( & cert) .unwrap ( ) ;
6487
write ( & work.path ( ) .join ( " b" ) , " b.txt" , " agent work\n " ) ;
6488
b.commit ( " work" , & [ ] ) .unwrap ( ) ;
6489
let r = b.push ( " origin" , false ) .unwrap ( ) ;
6490
assert! ( r.iter ( ) .all ( | x | x.ok ) , " {r:?}" ) ;
6491
6492
6493
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6494
let main = refs.refs.iter ( ) .find ( | r | r.name = = " main" ) .unwrap ( ) ;
6495
let ( attest, acc) = ( main.attestation.clone ( ) .unwrap ( ) , main.accepted.clone ( ) .expect ( " an acceptance" ) ) ;
6496
assert! ( matches! ( acc.attest.body, AttestBody:: Accepted { attest: r } if r = = attest.attest.hash ( ) ) ) ;
6497
assert_eq! ( acc.sigs[ 0 ] .key, hub_id.hash ( ) ) ;
6498
assert! ( acc.attest.hlc.wall_ms > = now) ;
6499
6500
a.pull ( " origin" ) .unwrap ( ) ;
6501
write ( & work.path ( ) .join ( " a" ) , " a.txt" , " again\n " ) ;
6502
a.commit ( " again" , & [ ] ) .unwrap ( ) ;
6503
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6504
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6505
assert! ( refs.refs.iter ( ) .find ( | r | r.name = = " main" ) .unwrap ( ) .accepted.is_none ( ) ) ;
6506
}
8aa5ef38 Andy McCall 20 days ago ↶
6507
b5c199bd Andy McCall 18 days ago ↶
6508
6509
6510
6511
6512
# [ tokio ::test ( flavor = " multi_thread" ) ]
6513
async fn a_tag_by_a_key_the_hub_certified_is_accepted_and_fetched ( ) {
6514
use oxy_core:: attest:: AttestBody;
6515
use oxy_core:: cert:: { Certificate, SignedCertificate} ;
6516
use oxy_core:: identity:: Scope;
6517
use oxy_core:: repo:: IdentityScope;
6518
use oxy_core:: tlog:: Entry;
6519
let h = hub ( ) .await;
6520
create ( & h.app, " andy" , " demo" ) .await;
6521
let url = format! ( " {} /andy/demo" , h.base) ;
6522
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6523
let mut a = client ( & url, & work.path ( ) .join ( " a" ) ) .with_home ( work.path ( ) .join ( " home-a" ) ) ;
6524
a.create_identity ( " Andy" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6525
write ( & work.path ( ) .join ( " a" ) , " a.txt" , " a\n " ) ;
6526
a.commit ( " a" , & [ ] ) .unwrap ( ) ;
6527
a.policy_init ( ) .unwrap ( ) ;
6528
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6529
let hub_id = h.app.hub_key.identity.clone ( ) ;
6530
a.policy_grant_subject ( " agent:claude@andy" , hub_id.clone ( ) , Scope:: CHANGES | Scope:: REFS , None ) .unwrap ( ) ;
6531
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .any ( | r | r.name = = " policy" & & r.ok ) ) ;
6532
6533
6534
let mut b = Repo:: clone( & url, & work.path ( ) .join ( " b" ) ) .unwrap ( ) .with_home ( work.path ( ) .join ( " home-b" ) ) ;
6535
b.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
6536
b.save_config ( ) .unwrap ( ) ;
6537
let ( _ , key) = b.create_identity ( " claude" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6538
let ( _ , secret) = oxygenhub:: keys:: load_secret( & h.app.config.keys_dir ( ) ) .unwrap ( ) ;
6539
let now = db:: now_ms( ) as u64 ;
6540
let cert = SignedCertificate:: sign(
6541
Certificate {
6542
key,
6543
issuer: hub_id.hash ( ) ,
6544
subject: " agent:claude@andy" .into ( ) ,
6545
claims: vec! [ ] ,
6546
scope: Scope:: CHANGES | Scope:: REFS ,
6547
not_before: now - 60_000 ,
6548
expires: now + 600_000 ,
6549
grant: None ,
6550
} ,
6551
& hub_id,
6552
& secret,
6553
) ;
6554
b.store ( ) .put_identity ( & hub_id) .unwrap ( ) ;
6555
b.store ( ) .put_certificate ( & cert) .unwrap ( ) ;
6556
let heads = b.bookmark_heads ( ) .unwrap ( ) ;
6557
b.tag_create ( " v0.2.0" , & heads, " OxygenFlow 0.2.0" ) .unwrap ( ) ;
6558
let r = b.push ( " origin" , false ) .unwrap ( ) ;
6559
assert! ( r.iter ( ) .any ( | x | x.name = = " tags/v0.2.0" & & x.ok ) , " {r:?}" ) ;
6560
6561
6562
6563
h.app.tlog.refresh_checkpoint ( & hub_id, & secret) .unwrap ( ) ;
6564
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6565
let tag = refs.tags.iter ( ) .find ( | t | t.name = = " v0.2.0" ) .expect ( " the tag" ) ;
6566
let ( attest, acc) = ( tag.attestation.clone ( ) .unwrap ( ) , tag.accepted.clone ( ) .expect ( " an acceptance" ) ) ;
6567
assert! ( matches! ( acc.attest.body, AttestBody:: Accepted { attest: r } if r = = attest.attest.hash ( ) ) ) ;
6568
assert_eq! ( acc.sigs[ 0 ] .key, hub_id.hash ( ) ) ;
6569
assert! ( acc.attest.hlc.wall_ms > = now) ;
6570
let logged = tag.logged.as_ref ( ) .expect ( " the tag's place in the log" ) ;
6571
let entry = Entry:: decode( & logged.entry) .unwrap ( ) ;
6572
assert_eq! ( ( entry.kind.as_str ( ) , entry.object) , ( " tag" , attest.attest.hash ( ) ) ) ;
6573
let logged_acceptance = db:: tlog:: entry_of( & h.app.db, & acc.attest.hash ( ) .to_hex ( ) , " accepted" ) .await.unwrap ( ) ;
6574
assert! ( logged_acceptance.is_some ( ) , " the acceptance is logged too" ) ;
6575
6576
6577
let c = Repo:: clone( & url, & work.path ( ) .join ( " c" ) ) .unwrap ( ) .with_home ( work.path ( ) .join ( " home-c" ) ) ;
6578
assert_eq! ( c.tag_heads ( " v0.2.0" ) .unwrap ( ) , Some ( heads) ) ;
6579
let report = c.fetch ( " origin" ) .unwrap ( ) ;
6580
assert! ( report.skipped.is_empty ( ) , " {:?}" , report.skipped) ;
6581
6582
a.pull ( " origin" ) .unwrap ( ) ;
6583
let t = a.tags ( ) .unwrap ( ) .into_iter ( ) .find ( | t | t.name = = " v0.2.0" ) .expect ( " pulled" ) ;
6584
assert_eq! ( t.signer.as_deref ( ) , Some ( " claude" ) ) ;
a0a1601e Andy McCall 17 days ago ↶
6585
6586
6587
6588
6589
a.tag_create ( " v0.1.0" , & a.bookmark_heads ( ) .unwrap ( ) , " by Andy" ) .unwrap ( ) ;
6590
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .any ( | r | r.name = = " tags/v0.1.0" & & r.ok ) ) ;
6591
let bare = oxy_core:: protocol:: http_get( & format! ( " {url} /oxy/v1/refs" ) , & RemoteOptions:: default( ) ) .unwrap ( ) ;
6592
let ( _ , old) = oxy_core:: protocol:: wire:: decode_refs( & bare) .unwrap ( ) ;
6593
let names: Vec< String> = old.tags.into_iter ( ) .map ( | t | t.name ) .collect ( ) ;
6594
assert_eq! ( names, [ " v0.1.0" ] , " no Oxy-Features header" ) ;
6595
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6596
let mut names: Vec< String> = refs.tags.into_iter ( ) .map ( | t | t.name ) .collect ( ) ;
6597
names.sort ( ) ;
6598
assert_eq! ( names, [ " v0.1.0" , " v0.2.0" ] , " a current client declares certified-tags" ) ;
b5c199bd Andy McCall 18 days ago ↶
6599
}
6600
8aa5ef38 Andy McCall 20 days ago ↶
6601
6602
6603
6604
6605
6606
# [ tokio ::test ( flavor = " multi_thread" ) ]
6607
async fn an_agent_with_a_grant_pushes_in_its_owner_s_name ( ) {
6608
use oxy_core:: cert:: { Grant, SignedGrant} ;
6609
use oxy_core:: identity:: { Identity, Scope, SecretKey} ;
6610
use oxy_core:: repo:: { IdentityScope, SignatureStatus} ;
6611
let h = hub ( ) .await;
6612
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
6613
let ( pat, pat_hash) = oxygenhub:: auth:: new_token( ) ;
6614
db:: accounts:: create_token( & h.app.db, andy.id, " cli" , & pat_hash, " repo:write" , " " , None ) .await.unwrap ( ) ;
6615
create ( & h.app, " andy" , " demo" ) .await;
6616
let url = format! ( " {} /andy/demo" , h.base) ;
6617
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6618
6619
let mut a = client ( & url, & work.path ( ) .join ( " a" ) ) .with_home ( work.path ( ) .join ( " home-a" ) ) ;
6620
let ( andy_key, andy_id) = a.create_identity ( " Andy" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6621
db:: accounts:: link_identity( & h.app.db, andy.id, & andy_key.to_hex ( ) , " Andy" , " [email protected] " , & andy_id.encode ( ) ) .await.unwrap ( ) ;
6622
write ( & work.path ( ) .join ( " a" ) , " a.txt" , " a\n " ) ;
6623
a.commit ( " a" , & [ ] ) .unwrap ( ) ;
6624
a.policy_init ( ) .unwrap ( ) ;
6625
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6626
let hub_id = h.app.hub_key.identity.clone ( ) ;
6627
a.policy_grant_subject ( " agent:claude@andy" , hub_id.clone ( ) , Scope:: CHANGES | Scope:: REFS , None ) .unwrap ( ) ;
6628
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6629
6630
6631
let ( _ , andy_sk) = a.secret_key ( ) .unwrap ( ) .unwrap ( ) ;
6632
let now = db:: now_ms( ) as u64 ;
6633
let grant = | by : & Identity, sk : & SecretKey, subject : & str | {
6634
SignedGrant:: sign(
6635
Grant {
6636
grantor: by.hash ( ) ,
6637
subject: subject.into ( ) ,
6638
issuer: hub_id.hash ( ) ,
6639
scope: Scope:: CHANGES | Scope:: SEAL | Scope:: REFS ,
6640
not_before: now - 60_000 ,
6641
expires: now + 86_400_000 ,
6642
} ,
6643
by,
6644
sk,
6645
)
6646
} ;
6647
let post = | g : SignedGrant| {
6648
let ( app, pat) = ( h.app.clone ( ) , pat.clone ( ) ) ;
6649
let body = serde_json:: json! ( { " grant" : oxy_core:: dsse:: base64( & g.encode ( ) ) } ) .to_string ( ) ;
6650
async move { call ( & app, " POST" , " /api/v1/agents/grants" , Some ( & pat) , Some ( body) ) .await }
6651
} ;
6652
let stray_sk = SecretKey:: generate( ) ;
6653
let stray = stray_sk.identity ( " stray" , " [email protected] " , 1 ) ;
6654
let ( st, body) = post ( grant ( & stray, & stray_sk, " agent:claude@andy" ) ) .await;
6655
assert_eq! ( st, StatusCode:: FORBIDDEN , " a key not linked to the account: {body}" ) ;
6656
let ( st, body) = post ( grant ( & andy_id, & andy_sk, " agent:claude@mallory" ) ) .await;
6657
assert_eq! ( st, StatusCode:: BAD_REQUEST , " another account's agent: {body}" ) ;
6658
let ( st, body) = post ( grant ( & andy_id, & andy_sk, " agent:claude@andy" ) ) .await;
6659
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
6660
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
6661
let token = v[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
6662
assert! ( token.starts_with ( " oxyagent_" ) ) ;
6663
let grant_id = v[ " id" ] .as_i64 ( ) .unwrap ( ) ;
6664
6665
6666
let agent_home = work.path ( ) .join ( " home-claude" ) ;
6667
oxy_core:: credentials:: Credentials:: at( & agent_home) .save_grant ( & h.base, & token) .unwrap ( ) ;
6668
let agent = | | {
6669
let n = std:: time:: SystemTime:: now( ) .duration_since ( std:: time:: UNIX_EPOCH ) .unwrap ( ) .as_nanos ( ) ;
6670
let dir = work.path ( ) .join ( format! ( " claude-{n} " ) ) ;
6671
let mut r = Repo:: clone( & url, & dir) .unwrap ( ) .with_home ( agent_home.clone ( ) ) ;
559c3920 Andy McCall 20 days ago ↶
6672
6673
r.config.set ( " remote.origin.token" , & token) .unwrap ( ) ;
8aa5ef38 Andy McCall 20 days ago ↶
6674
r.save_config ( ) .unwrap ( ) ;
6675
r.set_user_config ( " author.key" , & andy_key.to_hex ( ) ) .unwrap ( ) ;
6676
r.set_user_config ( " author.signer" , " keyless" ) .unwrap ( ) ;
6677
( r, dir)
6678
} ;
6679
let ( claude, dir) = agent ( ) ;
6680
assert! ( ! agent_home.join ( " keys" ) .exists ( ) , " no key on disk" ) ;
6681
write ( & dir, " work.txt" , " agent work\n " ) ;
6682
let change = claude.commit ( " Agent work" , & [ ] ) .unwrap ( ) ;
6683
let r = claude.push ( " origin" , false ) .unwrap ( ) ;
6684
assert! ( r.iter ( ) .all ( | x | x.ok ) , " {r:?}" ) ;
6685
let signer = claude.signing_key ( ) .expect ( " a certified key" ) ;
6686
assert_ne! ( signer, andy_key) ;
6687
assert! ( ! agent_home.join ( " keys" ) .exists ( ) , " still no key on disk" ) ;
6688
6689
6690
6691
let host = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
6692
assert_eq! ( host.store ( ) .get_change_meta ( & change) .unwrap ( ) .author, andy_key) ;
6693
assert! ( matches! (
6694
host.authenticate_change ( & change) .unwrap ( ) ,
6695
SignatureStatus:: Valid { key, sealed: true , .. } if key = = signer
6696
) ) ;
6697
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6698
assert! ( refs.refs.iter ( ) .find ( | r | r.name = = " main" ) .unwrap ( ) .accepted.is_some ( ) ) ;
6699
a.pull ( " origin" ) .unwrap ( ) ;
6700
assert! ( matches! ( a.authenticate_change ( & change) .unwrap ( ) , SignatureStatus:: Valid { .. } ) ) ;
6701
a66c05a6 Andy McCall 13 days ago ↶
6702
6703
6704
6705
create ( & h.app, " andy" , " fresh" ) .await;
6706
let fresh_url = format! ( " {} /andy/fresh" , h.base) ;
6707
let fresh_dir = work.path ( ) .join ( " claude-fresh" ) ;
6708
let mut fresh = client ( & fresh_url, & fresh_dir) .with_home ( agent_home.clone ( ) ) ;
6709
fresh.config.set ( " remote.origin.token" , & token) .unwrap ( ) ;
6710
fresh.save_config ( ) .unwrap ( ) ;
6711
fresh.set_user_config ( " author.key" , & andy_key.to_hex ( ) ) .unwrap ( ) ;
6712
fresh.set_user_config ( " author.signer" , " keyless" ) .unwrap ( ) ;
6713
assert! ( fresh.store ( ) .get_identity ( & andy_key) .is_err ( ) , " the fresh repository has never seen Andy" ) ;
6714
write ( & fresh_dir, " first.txt" , " first\n " ) ;
6715
let first = fresh.commit ( " First, by the agent" , & [ ] ) .unwrap ( ) ;
6716
let r = fresh.push ( " origin" , false ) .unwrap ( ) ;
6717
assert! ( r.iter ( ) .all ( | x | x.ok ) , " {r:?}" ) ;
6718
assert! ( matches! ( fresh.authenticate_change ( & first) .unwrap ( ) , SignatureStatus:: Valid { .. } ) , " locally" ) ;
6719
let fresh_host = h.app.repos.open ( & RepoName:: parse( " andy/fresh" ) .unwrap ( ) ) .unwrap ( ) ;
6720
assert! (
6721
matches! ( fresh_host.authenticate_change ( & first) .unwrap ( ) , SignatureStatus:: Valid { sealed: true , .. } ) ,
6722
" on the hub: {:?}" ,
6723
fresh_host.authenticate_change ( & first) .unwrap ( )
6724
) ;
6725
27babfa1 Andy McCall 11 days ago ↶
6726
6727
6728
6729
let rot = a.rotate_identity ( ) .unwrap ( ) ;
6730
db:: accounts:: link_identity( & h.app.db, andy.id, & rot.new.to_hex ( ) , " Andy" , " [email protected] " , & rot.identity.encode ( ) ) .await.unwrap ( ) ;
6731
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6732
let ( mut warm, dir) = agent ( ) ;
6733
warm.set_user_config ( " author.for" , " andy" ) .unwrap ( ) ;
6734
write ( & dir, " warm.txt" , " before the hub has said\n " ) ;
6735
warm.commit ( " Warm" , & [ ] ) .unwrap ( ) ;
6736
assert! ( warm.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6737
let followed = std:: fs:: read_to_string( agent_home.join ( " keys" ) .join ( " author.for" ) ) .unwrap ( ) ;
6738
assert_eq! ( followed.trim ( ) , rot.new.to_hex ( ) , " the hub named Andy's newest identity" ) ;
6739
let ( follower, dir) = agent ( ) ;
6740
assert_eq! ( follower.config.author_key, Some ( rot.new) ) ;
6741
write ( & dir, " follow.txt" , " as Andy's newest identity\n " ) ;
6742
let followed = follower.commit ( " Followed" , & [ ] ) .unwrap ( ) ;
6743
assert! ( follower.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6744
assert_eq! ( host.store ( ) .get_change_meta ( & followed) .unwrap ( ) .author, rot.new) ;
6745
assert! (
6746
matches! ( host.authenticate_change ( & followed) .unwrap ( ) , SignatureStatus:: Valid { sealed: true , .. } ) ,
6747
" {:?}" ,
6748
host.authenticate_change ( & followed) .unwrap ( )
6749
) ;
6750
8aa5ef38 Andy McCall 20 days ago ↶
6751
6752
let ( st, _ ) = call ( & h.app, " DELETE" , & format! ( " /api/v1/agents/grants/{grant_id} " ) , Some ( & pat) , None ) .await;
6753
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
6754
let ( late, dir) = agent ( ) ;
6755
write ( & dir, " late.txt" , " after the revocation\n " ) ;
6756
late.commit ( " Late" , & [ ] ) .unwrap ( ) ;
6757
let err = late.push ( " origin" , false ) .unwrap_err ( ) .to_string ( ) ;
559c3920 Andy McCall 20 days ago ↶
6758
assert! ( err.contains ( " not valid" ) | | err.contains ( " would not certify" ) , " {err}" ) ;
6759
6760
let by_agent: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM pushes WHERE via = 'claude'" ) .fetch_one ( & h.app.db) .await.unwrap ( ) ;
6761
assert! ( by_agent > = 1 , " recorded as via the agent" ) ;
8aa5ef38 Andy McCall 20 days ago ↶
6762
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/agents/grants" , Some ( & pat) , None ) .await;
6763
let list: Vec< serde_json:: Value> = serde_json:: from_str( & body) .unwrap ( ) ;
6764
assert! ( list[ 0 ] [ " revoked_ms" ] .is_i64 ( ) & & list[ 0 ] .get ( " token" ) .is_none ( ) , " {body}" ) ;
6765
}
a9f1c0bf Andy McCall 18 days ago ↶
6766
6767
6768
6769
6770
6771
# [ tokio ::test ( flavor = " multi_thread" ) ]
6772
async fn newest_run_wins ( ) {
6773
use oxy_core:: hash:: ChangeHash;
6774
let h = hub ( ) .await;
6775
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
6776
create ( & h.app, " andy" , " newest" ) .await;
6777
let url = format! ( " {} /andy/newest" , h.base) ;
6778
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6779
let c = client ( & url, work.path ( ) ) ;
6780
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
6781
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
6782
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
6783
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6784
let pushed = runs_of ( & h.app, " andy" , " newest" ) .await[ 0 ] .clone ( ) ;
6785
assert_eq! ( ( pushed.number, pushed.trigger.as_str ( ) ) , ( 1 , " push" ) ) ;
6786
let heads: Vec< ChangeHash> = pushed.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
6787
let start = | row : db:: repos:: RepoRow, bookmark : & 'static str , trigger : & 'static str | {
6788
let ( app, state, heads) = ( h.app.clone ( ) , pushed.state.clone ( ) , heads.clone ( ) ) ;
6789
async move {
6790
oxygenhub:: flow:: start_run_scoped( & app, & row, bookmark, & state, & heads, " andy" , trigger, None ) .await.unwrap ( ) .expect ( " a run" )
6791
}
6792
} ;
6793
let row = db:: repos:: get( & h.app.db, " andy" , " newest" ) .await.unwrap ( ) .unwrap ( ) ;
6794
let run = | n : i64 | {
6795
let ( app, id) = ( h.app.clone ( ) , row.id) ;
6796
async move { db:: flow:: run( & app.db, id, n) .await.unwrap ( ) .unwrap ( ) }
6797
} ;
6798
let superseded_by = | n : i64 , by : i64 | {
6799
let app = h.app.clone ( ) ;
6800
let run = run ( n) ;
6801
async move {
6802
let r = run.await;
6803
assert_eq! ( r.status, " cancelled" , " run #{n} is superseded" ) ;
6804
let jobs = db:: flow:: jobs( & app.db, r.id) .await.unwrap ( ) ;
6805
let why = format! ( " superseded by run #{by} " ) ;
6806
assert! ( jobs.iter ( ) .all ( | j | j.status = = " cancelled" & & j.error = = why ) , " {jobs:?}" ) ;
6807
}
6808
} ;
6809
6810
6811
6812
assert_eq! ( start ( row.clone ( ) , " proposal/7" , " proposal" ) .await, 2 ) ;
6813
assert_eq! ( start ( row.clone ( ) , " proposal/7" , " proposal" ) .await, 3 ) ;
6814
superseded_by ( 2 , 3 ) .await;
6815
assert_eq! ( start ( row.clone ( ) , " proposal/8" , " proposal" ) .await, 4 ) ;
6816
assert! ( run ( 3 ) .await.live ( ) , " another proposal's run is not superseded" ) ;
6817
assert! ( run ( 1 ) .await.live ( ) , " a proposal's run leaves push runs alone" ) ;
6818
6819
6820
6821
assert_eq! ( start ( row.clone ( ) , " main" , " manual" ) .await, 5 ) ;
6822
assert_eq! ( start ( row.clone ( ) , " main" , " rerun:1:push" ) .await, 6 ) ;
6823
assert! ( run ( 1 ) .await.live ( ) , " a manual run or a rerun supersedes nothing" ) ;
6824
assert_eq! ( start ( row.clone ( ) , " main" , " push" ) .await, 7 ) ;
6825
superseded_by ( 1 , 7 ) .await;
6826
assert! ( run ( 5 ) .await.live ( ) & & run ( 6 ) .await.live ( ) , " a push leaves manual runs and reruns alone" ) ;
6827
6828
6829
6830
db:: repos:: set_flow_keep_running( & h.app.db, row.id, true ) .await.unwrap ( ) ;
6831
let kept = db:: repos:: get( & h.app.db, " andy" , " newest" ) .await.unwrap ( ) .unwrap ( ) ;
6832
assert! ( kept.flow_keep_running) ;
6833
let three = run ( 3 ) .await;
6834
sqlx:: query( " UPDATE flow_runs SET status = 'running' WHERE id = $1" ) .bind ( three.id) .execute ( & h.app.db) .await.unwrap ( ) ;
6835
sqlx:: query( " UPDATE flow_jobs SET status = 'running' WHERE run_id = $1 AND name = 'build'" )
6836
.bind ( three.id)
6837
.execute ( & h.app.db)
6838
.await
6839
.unwrap ( ) ;
6840
assert_eq! ( start ( kept.clone ( ) , " proposal/7" , " proposal" ) .await, 8 ) ;
6841
assert_eq! ( start ( kept.clone ( ) , " proposal/7" , " proposal" ) .await, 9 ) ;
6842
assert_eq! ( run ( 3 ) .await.status, " running" , " a running run is kept" ) ;
6843
superseded_by ( 8 , 9 ) .await;
6844
6845
db:: repos:: set_flow_keep_running( & h.app.db, row.id, false ) .await.unwrap ( ) ;
6846
let row = db:: repos:: get( & h.app.db, " andy" , " newest" ) .await.unwrap ( ) .unwrap ( ) ;
6847
assert_eq! ( start ( row.clone ( ) , " proposal/7" , " proposal" ) .await, 10 ) ;
6848
superseded_by ( 3 , 10 ) .await;
6849
superseded_by ( 9 , 10 ) .await;
6850
}
8501fd58 Andy McCall 12 days ago ↶
6851
6852
6853
6854
6855
# [ tokio ::test ( flavor = " multi_thread" ) ]
6856
async fn runners_are_offered_updates_by_their_nearest_setting ( ) {
6857
use oxy_core:: identity:: SecretKey;
6858
let h = hub ( ) .await;
6859
create ( & h.app, " andy" , " oxygenflow" ) .await;
6860
let res = oxygenhub:: router( h.app.clone ( ) )
6861
.oneshot (
6862
Request:: post( " /register" )
6863
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
6864
.body ( Body:: from( " username=carol&email=carol%40example.com&password=correct-horse-battery&setup=test-setup" ) )
6865
.unwrap ( ) ,
6866
)
6867
.await
6868
.unwrap ( ) ;
6869
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
6870
let admin = db:: accounts:: account_by_name( & h.app.db, " carol" ) .await.unwrap ( ) .unwrap ( ) ;
6871
db:: accounts:: set_admin( & h.app.db, admin.id, true ) .await.unwrap ( ) ;
6872
let web = | method : & 'static str , path : & str , body : Option< String> | {
6873
let ( app, cookie, path) = ( h.app.clone ( ) , cookie.clone ( ) , path.to_string ( ) ) ;
6874
async move {
6875
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie) ;
6876
if body.is_some ( ) {
6877
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
6878
}
6879
let res = oxygenhub:: router( app) .oneshot ( req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ) .await.unwrap ( ) ;
6880
let st = res.status ( ) ;
6881
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
6882
}
6883
} ;
6884
let ( st, page) = web ( " GET" , " /admin/runners/updates" , None ) .await;
6885
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
6886
let csrf = {
6887
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
6888
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
6889
} ;
6890
assert_eq! ( call ( & h.app, " GET" , " /admin/runners/updates" , None , None ) .await.0 , StatusCode:: NOT_FOUND , " administrators only" ) ;
6891
let mut creds = vec! [ ] ;
6892
for name in [ " plex" , " narvi" ] {
6893
let ( _ , page) = web ( " POST" , " /admin/runners/token" , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
6894
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
6895
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
6896
let identity = SecretKey:: generate( ) .identity ( & format! ( " {name} (OxygenFlow runner)" ) , " " , 1 ) ;
6897
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0 (build)" , " max_jobs" : 1 } ) ;
6898
let reg = serde_json:: json! ( { " token" : token, " name" : name, " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
6899
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
6900
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
6901
creds.push ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
6902
}
6903
let ask = | who : usize | {
6904
let ( app, cred) = ( h.app.clone ( ) , creds[ who] .clone ( ) ) ;
6905
async move {
6906
let ( st, body) = call ( & app, " GET" , " /api/v1/flow/runner/update" , Some ( & cred) , None ) .await;
6907
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
6908
serde_json:: from_str:: < flow_core:: api:: UpdateAnswer> ( & body) .unwrap ( )
6909
}
6910
} ;
6911
let a = ask ( 0 ) .await;
6912
assert! ( ! a.policy.updates & & a.offer.is_none ( ) , " off unless chosen: {a:?}" ) ;
6913
assert_eq! ( a.note, " updates are off (default)" ) ;
6914
assert_eq! ( call ( & h.app, " GET" , " /api/v1/flow/runner/update" , Some ( " flowrunner_nope" ) , None ) .await.0 , StatusCode:: UNAUTHORIZED ) ;
6915
6916
6917
let repo = db:: repos:: get( & h.app.db, " andy" , " oxygenflow" ) .await.unwrap ( ) .unwrap ( ) ;
6918
let rel = db:: releases:: create( & h.app.db, repo.id, " v0.2.0" , " 0.2.0" , " " , false , None ) .await.unwrap ( ) ;
6919
db:: releases:: add_asset( & h.app.db, rel.id, " flow-linux-x86_64" , 42 , & " ab" .repeat ( 32 ) , " application/octet-stream" ) .await.unwrap ( ) ;
6920
let ( _ , page) = web ( " POST" , " /admin/runners/updates/source" , Some ( format! ( " _csrf={csrf} &repo=andy%2Fnope" ) ) ) .await;
6921
assert! ( page.contains ( " There is no repository" ) , " {page}" ) ;
6922
let ( _ , page) = web ( " POST" , " /admin/runners/updates/source" , Some ( format! ( " _csrf={csrf} &repo=andy%2Foxygenflow" ) ) ) .await;
6923
assert! ( page.contains ( " runner builds come from andy/oxygenflow" ) , " {page}" ) ;
6924
6925
6926
let plex = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " plex" ) .unwrap ( ) ;
6927
let ( _ , page) =
6928
web ( " POST" , " /admin/runners/updates/groups" , Some ( format! ( " _csrf={csrf} &name=canary&mode=chosen&runner={} " , plex.id) ) ) .await;
6929
assert! ( page.contains ( " Test group canary made: 1 chosen runners" ) , " {page}" ) ;
6930
let group = db:: runner_updates:: groups( & h.app.db) .await.unwrap ( ) .remove ( 0 ) ;
6931
let ( _ , page) =
6932
web ( " POST" , " /admin/runners/updates/policy" , Some ( format! ( " _csrf={csrf} &scope=group&key={} &updates=on" , group.id) ) ) .await;
6933
assert! ( page.contains ( " Saved for group canary: updates on." ) , " {page}" ) ;
6934
let a = ask ( 0 ) .await;
6935
let offer = a.offer.clone ( ) .expect ( " plex is offered the build" ) ;
6936
assert_eq! ( ( offer.version.as_str ( ) , offer.asset.as_str ( ) , offer.blake3.len ( ) ) , ( " 0.2.0" , " flow-linux-x86_64" , 64 ) ) ;
6937
assert! ( offer.url.starts_with ( " /andy/oxygenflow/asset/" ) & & ! offer.older, " {offer:?}" ) ;
6938
assert_eq! ( a.policy.from[ 0 ] , " group canary" ) ;
6939
assert! ( ask ( 1 ) .await.offer.is_none ( ) , " narvi is not in the group" ) ;
6940
6941
6942
let ( _ , page) = web ( " POST" , " /admin/runners/updates/policy" , Some ( format! ( " _csrf={csrf} &scope=site&updates=off&dry_run=on" ) ) ) .await;
6943
assert! ( page.contains ( " Saved for the site" ) , " {page}" ) ;
6944
let a = ask ( 0 ) .await;
6945
assert! ( a.policy.dry_run & & a.policy.from[ 3 ] = = " site" & & a.note.contains ( " dry run" ) , " {a:?}" ) ;
6946
let narvi = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " narvi" ) .unwrap ( ) ;
6947
web ( " POST" , " /admin/runners/updates/policy" , Some ( format! ( " _csrf={csrf} &scope=runner&key={} &updates=on&channel=0.1.0" , narvi.id) ) )
6948
.await;
6949
let b = ask ( 1 ) .await;
6950
assert! ( b.policy.updates & & b.offer.is_none ( ) & & b.note.contains ( " has no release" ) , " {b:?}" ) ;
6951
6952
6953
let now = time:: OffsetDateTime:: now_utc( ) .hour ( ) ;
6954
let ( from, to) = ( ( now + 2 ) % 24 , ( now + 4 ) % 24 ) ;
6955
web (
6956
" POST" ,
6957
" /admin/runners/updates/policy" ,
6958
Some ( format! ( " _csrf={csrf} &scope=group&key={} &updates=on&window={from:02} -{to:02} " , group.id) ) ,
6959
)
6960
.await;
6961
let a = ask ( 0 ) .await;
6962
assert! ( a.offer.is_none ( ) & & a.note.contains ( " waits for its window" ) , " {a:?}" ) ;
6963
6964
6965
let ( _ , page) = web ( " POST" , " /admin/runners/updates/policy" , Some ( format! ( " _csrf={csrf} &scope=site&channel=latest" ) ) ) .await;
6966
assert! ( page.contains ( " A channel is stable, canary or a version" ) , " {page}" ) ;
6967
let ( _ , page) = web ( " POST" , " /admin/runners/updates/policy" , Some ( format! ( " _csrf={csrf} &scope=site&window=9" ) ) ) .await;
6968
assert! ( page.contains ( " a window is two hours" ) , " {page}" ) ;
6969
let ( _ , page) = web ( " POST" , " /admin/runners/updates/groups" , Some ( format! ( " _csrf={csrf} &name=farm&mode=percent&percent=0" ) ) ) .await;
6970
assert! ( page.contains ( " A percentage is a whole number from 1 to 100." ) , " {page}" ) ;
6971
6972
6973
let report = serde_json:: json! ( { " note" : " dry run: flow 0.2.0 checks out" } ) .to_string ( ) ;
6974
assert_eq! ( call ( & h.app, " POST" , " /api/v1/flow/runner/update" , Some ( & creds[ 0 ] ) , Some ( report) ) .await.0 , StatusCode:: NO_CONTENT ) ;
6975
let ( _ , page) = web ( " GET" , " /admin/runners/updates" , None ) .await;
6976
assert! ( page.contains ( " dry run: flow 0.2.0 checks out" ) & & page.contains ( " >canary</span>" ) , " {page}" ) ;
6977
assert! ( page.contains ( " updates on (group canary)" ) & & page.contains ( " updates on (runner) · channel 0.1.0 (runner)" ) , " {page}" ) ;
6978
9bbca80f Andy McCall 12 days ago ↶
6979
6980
let ( _ , tokens) = web ( " GET" , " /settings/tokens" , None ) .await;
6981
let tcsrf = {
6982
let i = tokens.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
6983
tokens[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
6984
} ;
6985
let ( _ , made) = web ( " POST" , " /settings/tokens" , Some ( format! ( " _csrf={tcsrf} &name=admin&scopes=user%3Aread" ) ) ) .await;
6986
let i = made.find ( " oxyhub_" ) .unwrap ( ) ;
6987
let admin_token: String = made[ i.. ] .chars ( ) .take_while ( | c | c.is_ascii_alphanumeric ( ) | | * c = = ' _' ) .collect ( ) ;
6988
let ( st, body) = call ( & h.app, " GET" , " /api/v1/admin/runner-updates" , Some ( & admin_token) , None ) .await;
6989
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
6990
let all: hub_api:: RunnerUpdates = serde_json:: from_str( & body) .unwrap ( ) ;
6991
assert_eq! ( all.repo, " andy/oxygenflow" ) ;
6992
assert! ( all.settings.iter ( ) .any ( | s | s.scope = = " group" & & s.updates = = Some ( true ) & & s.window.is_some ( ) ) , " {all:?}" ) ;
6993
let plex_row = all.runners.iter ( ) .find ( | r | r.name = = " plex" ) .unwrap ( ) ;
6994
assert! ( plex_row.updates & & plex_row.from[ 0 ] = = " group canary" & & plex_row.groups = = [ " canary" ] , " {plex_row:?}" ) ;
6995
let put = | body : serde_json:: Value| {
6996
let ( app, t) = ( h.app.clone ( ) , admin_token.clone ( ) ) ;
6997
async move { call ( & app, " PUT" , " /api/v1/admin/runner-updates/settings" , Some ( & t) , Some ( body.to_string ( ) ) ) .await }
6998
} ;
6999
let ( st, body) =
7000
put ( serde_json:: json! ( { " scope" : " runner" , " key" : narvi.id.to_string ( ) , " updates" : true , " channel" : " 0.1.0" , " dry_run" : true } ) )
7001
.await;
7002
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
7003
assert! ( body.contains ( " Saved for runner narvi: updates on, channel 0.1.0, dry run on." ) , " {body}" ) ;
7004
let ( st, body) = put ( serde_json:: json! ( { " scope" : " site" , " channel" : " latest" } ) ) .await;
7005
assert_eq! ( st, StatusCode:: UNPROCESSABLE_ENTITY , " {body}" ) ;
7006
assert_eq! (
7007
call ( & h.app, " GET" , " /api/v1/admin/runner-updates" , Some ( & creds[ 0 ] ) , None ) .await.0 ,
7008
StatusCode:: UNAUTHORIZED ,
7009
" not a runner's"
7010
) ;
7011
faf5246c Andy McCall 12 days ago ↶
7012
7013
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 ( ) ;
7014
assert_eq! ( call ( & h.app, " POST" , " /api/v1/flow/runner/update" , Some ( & creds[ 0 ] ) , Some ( report) ) .await.0 , StatusCode:: NO_CONTENT ) ;
7015
let a = ask ( 0 ) .await;
7016
assert! ( a.offer.is_none ( ) & & a.note.contains ( " flow 0.2.0 is held: it rolled back on plex (3 starts" ) , " {a:?}" ) ;
7017
let ( _ , page) = web ( " GET" , " /admin/runners/updates" , None ) .await;
7018
assert! ( page.contains ( " Held builds" ) & & page.contains ( " <strong>flow 0.2.0</strong>" ) , " {page}" ) ;
9bbca80f Andy McCall 12 days ago ↶
7019
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/admin/runner-updates" , Some ( & admin_token) , None ) .await;
7020
assert! ( body.contains ( " \" version\" :\" 0.2.0\" " ) & & body.contains ( " \" runner\" :\" plex\" " ) , " the hold through the API: {body}" ) ;
faf5246c Andy McCall 12 days ago ↶
7021
let ( _ , page) = web ( " POST" , " /admin/runners/updates/holds/lift" , Some ( format! ( " _csrf={csrf} &version=0.2.0" ) ) ) .await;
7022
assert! ( page.contains ( " flow 0.2.0 is offered again" ) , " {page}" ) ;
9bbca80f Andy McCall 12 days ago ↶
7023
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/admin/runner-updates/holds/0.2.0/lift" , Some ( & admin_token) , Some ( " {}" .into ( ) ) ) .await;
7024
assert_eq! ( st, StatusCode:: NOT_FOUND , " no longer held" ) ;
faf5246c Andy McCall 12 days ago ↶
7025
assert! ( ask ( 0 ) .await.note.contains ( " waits for its window" ) , " lifted: back to what its settings say" ) ;
7026
let ( _ , page) = web ( " POST" , " /admin/runners/updates/holds/lift" , Some ( format! ( " _csrf={csrf} &version=0.2.0" ) ) ) .await;
7027
assert! ( page.contains ( " flow 0.2.0 is not held." ) , " {page}" ) ;
7028
8501fd58 Andy McCall 12 days ago ↶
7029
7030
let ( _ , page) =
7031
web ( " POST" , & format! ( " /admin/runners/updates/groups/{} " , group.id) , Some ( format! ( " _csrf={csrf} &mode=percent&percent=100" ) ) ) .await;
7032
assert! ( page.contains ( " Test group canary is now 100% of its runners." ) , " {page}" ) ;
7033
assert! ( ask ( 1 ) .await.policy.from[ 0 ] = = " runner" , " narvi's own setting is nearer than any group" ) ;
7034
let ( _ , page) = web ( " POST" , & format! ( " /admin/runners/updates/groups/{} /delete" , group.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
7035
assert! ( page.contains ( " Test group canary removed" ) , " {page}" ) ;
7036
assert! ( ! ask ( 0 ) .await.policy.updates, " back to the site's off" ) ;
7037
}
d7050703 Andy McCall 12 days ago ↶
7038
1ec29691 Andy McCall 10 days ago ↶
7039
7040
7041
7042
# [ tokio ::test ( flavor = " multi_thread" ) ]
7043
async fn a_version_reaches_wave_two_once_wave_one_proves_it ( ) {
7044
use oxy_core:: identity:: SecretKey;
7045
let h = hub ( ) .await;
7046
create ( & h.app, " andy" , " oxygenflow" ) .await;
7047
let res = oxygenhub:: router( h.app.clone ( ) )
7048
.oneshot (
7049
Request:: post( " /register" )
7050
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
7051
.body ( Body:: from( " username=carol&email=carol%40example.com&password=correct-horse-battery&setup=test-setup" ) )
7052
.unwrap ( ) ,
7053
)
7054
.await
7055
.unwrap ( ) ;
7056
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
7057
let admin = db:: accounts:: account_by_name( & h.app.db, " carol" ) .await.unwrap ( ) .unwrap ( ) ;
7058
db:: accounts:: set_admin( & h.app.db, admin.id, true ) .await.unwrap ( ) ;
7059
let web = | method : & 'static str , path : & str , body : Option< String> | {
7060
let ( app, cookie, path) = ( h.app.clone ( ) , cookie.clone ( ) , path.to_string ( ) ) ;
7061
async move {
7062
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie) ;
7063
if body.is_some ( ) {
7064
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
7065
}
7066
let res = oxygenhub:: router( app) .oneshot ( req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ) .await.unwrap ( ) ;
7067
( res.status ( ) , String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
7068
}
7069
} ;
7070
let ( _ , page) = web ( " GET" , " /admin/runners/updates" , None ) .await;
7071
let csrf = {
7072
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
7073
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
7074
} ;
7075
let mut creds = vec! [ ] ;
7076
for name in [ " plex" , " narvi" ] {
7077
let ( _ , page) = web ( " POST" , " /admin/runners/token" , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
7078
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
7079
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
7080
let identity = SecretKey:: generate( ) .identity ( & format! ( " {name} (OxygenFlow runner)" ) , " " , 1 ) ;
7081
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0 (build)" , " max_jobs" : 1 } ) ;
7082
let reg = serde_json:: json! ( { " token" : token, " name" : name, " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
7083
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
7084
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
7085
creds.push ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
7086
}
7087
let ask = | who : usize | {
7088
let ( app, cred) = ( h.app.clone ( ) , creds[ who] .clone ( ) ) ;
7089
async move {
7090
let ( st, body) = call ( & app, " GET" , " /api/v1/flow/runner/update" , Some ( & cred) , None ) .await;
7091
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
7092
serde_json:: from_str:: < flow_core:: api:: UpdateAnswer> ( & body) .unwrap ( )
7093
}
7094
} ;
7095
let repo = db:: repos:: get( & h.app.db, " andy" , " oxygenflow" ) .await.unwrap ( ) .unwrap ( ) ;
7096
let rel = db:: releases:: create( & h.app.db, repo.id, " v0.2.0" , " 0.2.0" , " " , false , None ) .await.unwrap ( ) ;
7097
db:: releases:: add_asset( & h.app.db, rel.id, " flow-linux-x86_64" , 42 , & " ab" .repeat ( 32 ) , " application/octet-stream" ) .await.unwrap ( ) ;
7098
let ( _ , page) =
7099
web ( " POST" , " /admin/runners/updates/source" , Some ( format! ( " _csrf={csrf} &repo=andy%2Foxygenflow&soak_jobs=2&soak_hours=48" ) ) ) .await;
7100
assert! ( page.contains ( " runner builds come from andy/oxygenflow" ) & & page.contains ( " value=\" 2\" " ) , " {page}" ) ;
7101
let ( _ , page) = web ( " POST" , " /admin/runners/updates/source" , Some ( format! ( " _csrf={csrf} &repo=andy%2Foxygenflow&soak_jobs=0" ) ) ) .await;
7102
assert! ( page.contains ( " is a whole number from 1 to 1000" ) , " {page}" ) ;
7103
web ( " POST" , " /admin/runners/updates/policy" , Some ( format! ( " _csrf={csrf} &scope=site&updates=on" ) ) ) .await;
7104
7105
7106
let runners = db:: runners:: list( & h.app.db) .await.unwrap ( ) ;
7107
let id = | name : & str | runners.iter ( ) .find ( | r | r.name = = name ) .unwrap ( ) .id ;
7108
let ( _ , page) =
7109
web ( " POST" , " /admin/runners/updates/groups" , Some ( format! ( " _csrf={csrf} &name=first&mode=chosen&runner={} &wave=1" , id ( " plex" ) ) ) )
7110
.await;
7111
assert! ( page.contains ( " Test group first made: 1 chosen runners, wave 1" ) & & page.contains ( " · wave 1</small>" ) , " {page}" ) ;
7112
let ( _ , page) =
7113
web ( " POST" , " /admin/runners/updates/groups" , Some ( format! ( " _csrf={csrf} &name=rest&mode=chosen&runner={} &wave=0" , id ( " narvi" ) ) ) )
7114
.await;
7115
assert! ( page.contains ( " A wave is a whole number from 1 to 9" ) , " {page}" ) ;
7116
web ( " POST" , " /admin/runners/updates/groups" , Some ( format! ( " _csrf={csrf} &name=rest&mode=chosen&runner={} &wave=2" , id ( " narvi" ) ) ) ) .await;
7117
assert_eq! ( ask ( 0 ) .await.offer.map ( | o | o.version ) .as_deref ( ) , Some ( " 0.2.0" ) , " wave 1 goes first" ) ;
7118
let b = ask ( 1 ) .await;
7119
assert! ( b.offer.is_none ( ) & & b.note.contains ( " flow 0.2.0 waits for wave 1 to prove it: 0 of 1 runner on it, 0 jobs" ) , " {b:?}" ) ;
7120
7121
7122
let run: i64 = sqlx:: query_scalar(
7123
" INSERT INTO flow_runs (repo_id, bookmark, state, heads, status, created_ms, number) VALUES ($1, 'main', 's', 'h', 'passed', 1, 1) RETURNING id" ,
7124
)
7125
.bind ( repo.id)
7126
.fetch_one ( & h.app.db)
7127
.await
7128
.unwrap ( ) ;
7129
let job = | name : & 'static str , status : & 'static str | {
7130
let ( app, plex) = ( h.app.clone ( ) , id ( " plex" ) ) ;
7131
async move {
7132
sqlx:: query(
7133
" INSERT INTO flow_jobs (run_id, name, depth, depends, spec, sandbox, runtime, status, runner, runner_id, started_ms, finished_ms, error, runner_version)
7134
VALUES ($1, $2, 0, '', '{}', 'microvm', 'microvm', $3, 'plex', $4, $5, $5, $6, '0.2.0')" ,
7135
)
7136
.bind ( run)
7137
.bind ( name)
7138
.bind ( status)
7139
.bind ( plex)
7140
.bind ( db:: now_ms( ) )
7141
.bind ( if status = = " error" { " the runner lost its disk" } else { " " } )
7142
.execute ( & app.db)
7143
.await
7144
.unwrap ( ) ;
7145
}
7146
} ;
7147
job ( " build" , " passed" ) .await;
7148
assert! ( ask ( 1 ) .await.note.contains ( " 1 of 1 runner on it, 1 job (each needs 2" ) , " still soaking" ) ;
7149
job ( " test" , " failed" ) .await;
7150
assert_eq! ( ask ( 1 ) .await.offer.map ( | o | o.version ) .as_deref ( ) , Some ( " 0.2.0" ) , " a failed test is not the runner's fault: proved" ) ;
7151
let ( _ , page) = web ( " GET" , " /admin/runners/updates" , None ) .await;
7152
assert! ( ! page.contains ( " waits for wave" ) , " {page}" ) ;
7153
7154
7155
job ( " lint" , " error" ) .await;
7156
let ( _ , page) = web ( " GET" , " /admin/runners/updates" , None ) .await;
7157
assert! ( page.contains ( " wave 1 stopped it on plex: job lint of andy/oxygenflow run #1: the runner lost its disk" ) , " {page}" ) ;
7158
assert! ( db:: runner_updates:: held( & h.app.db, " 0.2.0" ) .await.unwrap ( ) .is_none ( ) , " a page view holds nothing" ) ;
7159
let b = ask ( 1 ) .await;
7160
assert! ( b.offer.is_none ( ) & & b.note.contains ( " flow 0.2.0 is held: wave 1 stopped it on plex" ) , " {b:?}" ) ;
7161
assert! ( ask ( 0 ) .await.note.contains ( " is held" ) , " wave 1 too" ) ;
7162
let ( _ , page) = web ( " GET" , " /admin/runners/updates" , None ) .await;
7163
assert! ( page.contains ( " Held builds" ) & & page.contains ( " wave 1 stopped it on plex" ) , " {page}" ) ;
7164
}
7165
d7050703 Andy McCall 12 days ago ↶
7166
7167
# [ tokio ::test ( flavor = " multi_thread" ) ]
7168
async fn a_wasm_jobs_placement_names_no_image ( ) {
7169
let h = hub ( ) .await;
7170
create ( & h.app, " andy" , " wasm" ) .await;
7171
let work = tempfile:: tempdir( ) .unwrap ( ) ;
7172
let c = client ( & format! ( " {} /andy/wasm" , h.base) , work.path ( ) ) ;
7173
write (
7174
work.path ( ) ,
7175
" .flow/pipeline.kdl" ,
7176
" schema-version 1\n sandbox \" w\" {\n runtime \" wasm\" \n }\n job \" count\" {\n sandbox \" w\" \n exec \" count.wasm\" \n }\n " ,
7177
) ;
7178
write ( work.path ( ) , " count.wasm" , " \0 asm" ) ;
7179
c.commit ( " A WASM job" , & [ ] ) .unwrap ( ) ;
7180
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
7181
runs_of ( & h.app, " andy" , " wasm" ) .await;
7182
let ( st, body) =
7183
call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( r # "{"runner":"test-1","runtimes":["wasm"]}"# .into ( ) ) ) .await;
7184
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
7185
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
7186
let placed = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .placed;
7187
assert! ( placed.starts_with ( " idle" ) & & ! placed.contains ( " image" ) , " {placed}" ) ;
7188
}
dc5c6429 Andy McCall 12 days ago ↶
7189
7190
7191
7192
# [ tokio ::test ( flavor = " multi_thread" ) ]
7193
async fn an_organisation_sets_its_own_runners_updates ( ) {
7194
use oxy_core:: identity:: SecretKey;
7195
let h = hub ( ) .await;
7196
let res = oxygenhub:: router( h.app.clone ( ) )
7197
.oneshot (
7198
Request:: post( " /register" )
7199
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
7200
.body ( Body:: from( " username=andy&email=andy%40example.com&password=correct-horse-battery&setup=test-setup" ) )
7201
.unwrap ( ) ,
7202
)
7203
.await
7204
.unwrap ( ) ;
7205
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
7206
let web = | method : & 'static str , path : String, body : Option< String> | {
7207
let ( app, cookie) = ( h.app.clone ( ) , cookie.clone ( ) ) ;
7208
async move {
7209
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie) ;
7210
if body.is_some ( ) {
7211
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
7212
}
7213
let res = oxygenhub:: router( app) .oneshot ( req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ) .await.unwrap ( ) ;
7214
let st = res.status ( ) ;
7215
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
7216
}
7217
} ;
7218
let csrf_of = | page : & str | {
7219
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
7220
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
7221
} ;
7222
let ( _ , page) = web ( " GET" , " /orgs/new" .into ( ) , None ) .await;
7223
let csrf = csrf_of ( & page) ;
7224
web ( " POST" , " /orgs/new" .into ( ) , Some ( format! ( " _csrf={csrf} &name=acme&display_name=Acme" ) ) ) .await;
7225
7226
let mut creds = vec! [ ] ;
7227
for name in [ " a1" , " a2" ] {
7228
let ( _ , page) = web ( " POST" , " /orgs/acme/settings/runners/token" .into ( ) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
7229
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
7230
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
7231
let identity = SecretKey:: generate( ) .identity ( name, " " , 1 ) ;
7232
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " max_jobs" : 1 } ) ;
7233
let reg = serde_json:: json! ( { " token" : token, " name" : name, " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
7234
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
7235
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
7236
creds.push ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
7237
}
7238
claim ( & h.app) .await;
7239
let all = db:: runners:: list( & h.app.db) .await.unwrap ( ) ;
7240
let id = | n : & str | all.iter ( ) .find ( | r | r.name = = n ) .unwrap ( ) .id ;
7241
let set = | form : String| web ( " POST" , " /orgs/acme/settings/runners/updates/policy" .into ( ) , Some ( format! ( " _csrf={csrf} &{form} " ) ) ) ;
7242
7243
let ( _ , page) = set ( " scope=owner&key=acme&updates=on" .into ( ) ) .await;
7244
assert! ( page.contains ( " Saved for owner acme: updates on." ) , " {page}" ) ;
7245
let ( _ , page) = set ( " scope=site&updates=on" .into ( ) ) .await;
7246
assert! ( page.contains ( " Choose the site, an owner, a test group or a runner." ) , " not the site's: {page}" ) ;
7247
let ( _ , page) = set ( " scope=owner&key=andy&updates=on" .into ( ) ) .await;
7248
assert! ( page.contains ( " An organisation sets its own runners" ) , " {page}" ) ;
7249
let ( _ , page) = set ( format! ( " scope=runner&key={} &updates=on" , id ( " test-1" ) ) ) .await;
7250
assert! ( page.contains ( " That runner is gone." ) , " the site's runner is not acme's: {page}" ) ;
7251
7252
7253
let ( _ , page) = web (
7254
" POST" ,
7255
" /orgs/acme/settings/runners/updates/groups" .into ( ) ,
7256
Some ( format! ( " _csrf={csrf} &name=canary&owner=&mode=chosen&runner={} &runner={} " , id ( " a1" ) , id ( " test-1" ) ) ) ,
7257
)
7258
.await;
7259
assert! ( page.contains ( " Test group canary made: 1 chosen runners" ) , " {page}" ) ;
7260
let canary = db:: runner_updates:: groups( & h.app.db) .await.unwrap ( ) .remove ( 0 ) ;
7261
assert_eq! ( canary.owner, " acme" , " an organisation's groups are its own" ) ;
7262
let ( _ , page) = set ( format! ( " scope=group&key={} &channel=canary" , canary.id) ) .await;
7263
assert! ( page.contains ( " Saved for group canary: channel canary." ) , " {page}" ) ;
7264
let ( _ , page) = set ( format! ( " scope=runner&key={} &updates=off" , id ( " a2" ) ) ) .await;
7265
assert! ( page.contains ( " Saved for runner a2: updates off." ) , " {page}" ) ;
7266
7267
7268
let site = db:: runner_updates:: create_group( & h.app.db, " " , " everyone" , Some ( 100 ) ) .await.unwrap ( ) ;
7269
let ( _ , page) =
7270
web ( " POST" , format! ( " /orgs/acme/settings/runners/updates/groups/{} /delete" , site.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
7271
assert! ( page.contains ( " That test group is gone." ) , " {page}" ) ;
7272
assert! ( db:: runner_updates:: group( & h.app.db, site.id) .await.unwrap ( ) .is_some ( ) ) ;
7273
7274
7275
let ( _ , page) = web ( " GET" , " /orgs/acme/settings" .into ( ) , None ) .await;
7276
assert! ( page.contains ( " id=\" runner-updates\" " ) & & page.contains ( " >canary <small" ) , " {page}" ) ;
7277
assert! ( page.contains ( " updates on (owner acme)" ) & & page.contains ( " updates off (runner)" ) , " {page}" ) ;
7278
let ask = | cred : String| {
7279
let app = h.app.clone ( ) ;
7280
async move {
7281
let ( _ , body) = call ( & app, " GET" , " /api/v1/flow/runner/update" , Some ( & cred) , None ) .await;
7282
serde_json:: from_str:: < flow_core:: api:: UpdateAnswer> ( & body) .unwrap ( ) .policy
7283
}
7284
} ;
7285
let a1 = ask ( creds[ 0 ] .clone ( ) ) .await;
7286
assert! ( a1.updates & & a1.channel = = " canary" & & a1.from[ .. 2 ] = = [ " owner acme" .to_string ( ) , " group canary" .to_string ( ) ] , " {a1:?}" ) ;
7287
assert! ( ! ask ( creds[ 1 ] .clone ( ) ) .await.updates, " a2's own setting wins" ) ;
7288
}