42fa9554 Andy McCall 1 month ago ↶
1
2
3
4
5
use axum:: body:: Body;
6
use axum:: http:: { header, Request, StatusCode} ;
7
use http_body_util:: BodyExt;
8
use oxy_core:: protocol:: { open_remote, RemoteOptions} ;
9
use oxy_core:: repo:: Repo;
10
use oxygenhub:: config:: Config;
11
use oxygenhub:: repos:: RepoName;
12
use oxygenhub:: { db, Shared} ;
13
use std:: path:: Path;
14
use tower:: ServiceExt;
15
16
const ROOTFS : & str = " 36fc4e3238044e424219a2b9002bf61bbe1805fec14d52cf37e17fae77c9ff16" ;
17
18
fn hermetic ( ) {
19
static ONCE : std:: sync:: Once = std:: sync:: Once:: new( ) ;
20
static DIR : std:: sync:: OnceLock< tempfile:: TempDir> = std:: sync:: OnceLock:: new( ) ;
21
ONCE .call_once ( | | {
22
let d = tempfile:: tempdir( ) .unwrap ( ) ;
23
std:: env:: set_var( " OXY_HOME" , d.path ( ) ) ;
24
DIR .set ( d) .ok ( ) ;
25
} ) ;
26
}
27
28
fn write ( root : & Path, rel : & str , content : & str ) {
29
let p = root.join ( rel) ;
30
std:: fs:: create_dir_all( p.parent ( ) .unwrap ( ) ) .unwrap ( ) ;
31
std:: fs:: write( p, content) .unwrap ( ) ;
32
}
33
34
struct Hub {
35
app : Shared,
36
base : String,
37
_dir : tempfile:: TempDir,
38
}
39
40
async fn hub ( ) -> Hub {
41
hermetic ( ) ;
42
let dir = tempfile:: tempdir( ) .unwrap ( ) ;
43
let mut config = Config:: load( dir.path ( ) ) .unwrap ( ) ;
c8684d44 Andy McCall 20 days ago
44
45
config.registration.setup_code = " test-setup" .into ( ) ;
42fa9554 Andy McCall 1 month ago ↶
46
config.auth.push_tokens = vec! [ " site" .into ( ) ] ;
4d2e44d7 Andy McCall 1 month ago ↶
47
config.flow.runner_tokens = vec! [ " runner-secret" .into ( ) , " runner-two" .into ( ) , " runner-three" .into ( ) ] ;
42fa9554 Andy McCall 1 month ago ↶
48
config.flow.repos = vec! [ " andy/*" .into ( ) ] ;
10708ab8 Andy McCall 1 month ago ↶
49
config.registration.mode = " open" .into ( ) ;
42fa9554 Andy McCall 1 month ago ↶
50
let listener = tokio:: net:: TcpListener:: bind( " 127.0.0.1:0" ) .await.unwrap ( ) ;
51
let port = listener.local_addr ( ) .unwrap ( ) .port ( ) ;
52
config.server.base_url = format! ( " http://127.0.0.1:{port} " ) ;
53
let app = oxygenhub:: build( config) .await.unwrap ( ) ;
54
let router = oxygenhub:: router( app.clone ( ) ) ;
55
tokio:: spawn( async move { axum:: serve( listener, router) .await.unwrap ( ) } ) ;
56
Hub { app, base: format! ( " http://127.0.0.1:{port} " ) , _dir: dir }
57
}
58
59
async fn call ( app : & Shared, method : & str , path : & str , bearer : Option< & str > , json : Option< String> ) -> ( StatusCode, String) {
60
let mut req = Request:: builder( ) .method ( method) .uri ( path) ;
61
if let Some ( b) = bearer {
62
req = req.header ( header:: AUTHORIZATION , format! ( " Bearer {b} " ) ) ;
63
}
64
let body = match json {
65
Some ( j) => {
66
req = req.header ( header:: CONTENT_TYPE , " application/json" ) ;
67
Body:: from( j)
68
}
69
None => Body:: empty( ) ,
70
} ;
71
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req.body ( body) .unwrap ( ) ) .await.unwrap ( ) ;
72
let status = res.status ( ) ;
73
let bytes = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
74
( status, String :: from_utf8_lossy( & bytes) .into_owned ( ) )
75
}
76
736f6813 Andy McCall 1 month ago ↶
77
78
async fn head ( app : & Shared, path : & str ) -> ( StatusCode, String, String) {
79
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( Request:: get( path) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
80
let h = | k : header:: HeaderName| res.headers ( ) .get ( k) .and_then ( | v | v.to_str ( ) .ok ( ) ) .unwrap_or ( " " ) .to_string ( ) ;
81
( res.status ( ) , h ( header:: LOCATION ) , h ( header:: CONTENT_TYPE ) )
82
}
83
42fa9554 Andy McCall 1 month ago ↶
84
async fn claim ( app : & Shared) -> Option< serde_json:: Value> {
85
let ( st, body) =
86
call ( app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( r # "{"runner":"test-1","runtimes":["microvm"]}"# .into ( ) ) ) .await;
87
match st {
88
StatusCode:: NO_CONTENT => None ,
89
StatusCode:: OK => Some ( serde_json:: from_str( & body) .unwrap ( ) ) ,
90
other => panic! ( " claim: {other} {body}" ) ,
91
}
92
}
93
94
fn receipt ( job : & str , pipeline_hash : & str , exit : u32 ) -> serde_json:: Value {
95
serde_json:: json! ( {
96
" job_name" : job, " pipeline_hash" : pipeline_hash, " scope_tree_hash" : " 11" .repeat ( 32 ) ,
97
" inputs" : [ ] , " environment" : " 22" .repeat ( 32 ) , " outputs" : [ ] , " exit_code" : exit, " duration_ms" : 640
98
} )
99
}
100
101
async fn finish ( app : & Shared, id : i64 , body : serde_json:: Value) -> StatusCode {
102
call ( app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /finish" ) , Some ( " runner-secret" ) , Some ( body.to_string ( ) ) ) .await.0
103
}
104
105
async fn create ( app : & Shared, owner : & str , name : & str ) {
106
app.repos.create ( & RepoName:: parse( & format! ( " {owner} /{name} " ) ) .unwrap ( ) , None ) .unwrap ( ) ;
107
db:: repos:: create( & app.db, owner, name, " " , hub_api:: Visibility:: Public) .await.unwrap ( ) ;
108
}
109
110
fn client ( url : & str , work : & Path) -> Repo {
111
let mut c = Repo:: init( work) .unwrap ( ) ;
112
c.config.set ( " remote.origin.url" , url) .unwrap ( ) ;
113
c.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
114
c.save_config ( ) .unwrap ( ) ;
115
c
116
}
117
118
fn pipeline ( extra : & str ) -> String {
119
format! (
120
" schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n job \" test\" {{ \n sandbox \" alpine\" \n depends-on \" build\" \n exec \" sh\" \" -eu\" \" test.sh\" \n }} \n job \" lint\" {{ \n sandbox \" alpine\" \n exec \" true\" \n }} \n {extra} "
121
)
122
}
123
124
async fn runs_of ( app : & Shared, owner : & str , name : & str ) -> Vec< db:: flow:: Run> {
125
let row = db:: repos:: get( & app.db, owner, name) .await.unwrap ( ) .unwrap ( ) ;
126
for _ in 0 .. 100 {
127
let r = db:: flow:: runs( & app.db, row.id, 10 , None ) .await.unwrap ( ) ;
128
if ! r.is_empty ( ) {
129
return r;
130
}
131
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
132
}
133
vec! [ ]
134
}
135
90326c96 Andy McCall 1 month ago ↶
136
137
async fn checks_when ( app : & Shared, repo_id : i64 , state : & str , n : usize ) -> Vec< db:: hooks:: Check> {
138
for _ in 0 .. 100 {
139
let c = db:: hooks:: checks_for_state( & app.db, repo_id, state) .await.unwrap ( ) ;
140
if c.len ( ) > = n {
141
return c;
142
}
143
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
144
}
145
db:: hooks:: checks_for_state( & app.db, repo_id, state) .await.unwrap ( )
146
}
147
42fa9554 Andy McCall 1 month ago ↶
148
# [ tokio ::test ( flavor = " multi_thread" ) ]
149
async fn a_push_runs_the_pipeline_through_a_runner ( ) {
150
let h = hub ( ) .await;
151
create ( & h.app, " andy" , " demo" ) .await;
152
create ( & h.app, " andy" , " secret" ) .await;
153
create ( & h.app, " other" , " plain" ) .await;
154
let url = format! ( " {} /andy/demo" , h.base) ;
155
let work = tempfile:: tempdir( ) .unwrap ( ) ;
156
let mut c = client ( & url, work.path ( ) ) ;
157
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
158
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
159
c.snapshot ( " test" ) .unwrap ( ) ;
160
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
161
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
162
163
164
let runs = runs_of ( & h.app, " andy" , " demo" ) .await;
165
assert_eq! ( runs.len ( ) , 1 ) ;
166
let run = & runs[ 0 ] ;
167
assert_eq! ( ( run.bookmark.as_str ( ) , run.status.as_str ( ) , run.actor.as_str ( ) ) , ( " main" , " queued" , " site token" ) ) ;
168
assert_eq! ( run.pipeline_hash.len ( ) , 64 ) ;
169
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
170
assert_eq! ( jobs.iter ( ) .map ( | j | ( j.name.as_str ( ) , j.depth) ) .collect:: < Vec< _ > > ( ) , [ ( " build" , 0 ) , ( " lint" , 0 ) , ( " test" , 1 ) ] ) ;
90326c96 Andy McCall 1 month ago ↶
171
172
let checks = checks_when ( & h.app, run.repo_id, & run.state, 3 ) .await;
42fa9554 Andy McCall 1 month ago ↶
173
assert_eq! (
174
checks.iter ( ) .map ( | c | ( c.name.as_str ( ) , c.status.as_str ( ) ) ) .collect:: < Vec< _ > > ( ) ,
175
[ ( " flow/build" , " queued" ) , ( " flow/lint" , " queued" ) , ( " flow/test" , " queued" ) ]
176
) ;
177
178
179
assert_eq! (
180
call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " site" ) , Some ( r # "{"runner":"x","runtimes":["microvm"]}"# .into ( ) ) ) .await.0 ,
181
StatusCode:: UNAUTHORIZED
182
) ;
183
assert_eq! (
184
call ( & h.app, " POST" , " /api/v1/flow/claim" , None , Some ( r # "{"runner":"x","runtimes":["microvm"]}"# .into ( ) ) ) .await.0 ,
185
StatusCode:: UNAUTHORIZED
186
) ;
187
188
let ( st, _ ) =
189
call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( r # "{"runner":"x","runtimes":["wasm"]}"# .into ( ) ) ) .await;
190
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
191
192
193
let a = claim ( & h.app) .await.unwrap ( ) ;
194
let b = claim ( & h.app) .await.unwrap ( ) ;
195
assert_eq! ( ( a[ " job" ] [ " name" ] .as_str ( ) , b[ " job" ] [ " name" ] .as_str ( ) ) , ( Some ( " build" ) , Some ( " lint" ) ) ) ;
196
assert! ( claim ( & h.app) .await.is_none ( ) , " test waits for build" ) ;
197
assert_eq! ( a[ " repo" ] , " andy/demo" ) ;
198
assert_eq! ( a[ " clone_url" ] , url.as_str ( ) ) ;
199
assert_eq! ( a[ " sandbox" ] [ " rootfs" ] , ROOTFS ) ;
200
assert_eq! ( a[ " job" ] [ " exec" ] , serde_json:: json! ( [ " make" ] ) ) ;
201
assert_eq! ( a[ " timeout_s" ] , 3600 ) ;
9a065bc0 Andy McCall 1 month ago ↶
202
203
assert_eq! (
204
( a[ " state" ] .as_str ( ) , a[ " bookmark" ] .as_str ( ) , a[ " run_number" ] .as_i64 ( ) ) ,
205
( Some ( run.state.as_str ( ) ) , Some ( " main" ) , Some ( run.number) )
206
) ;
42fa9554 Andy McCall 1 month ago ↶
207
let heads: Vec< String> = a[ " heads" ] .as_array ( ) .unwrap ( ) .iter ( ) .map ( | v | v.as_str ( ) .unwrap ( ) .to_string ( ) ) .collect ( ) ;
208
assert_eq! ( heads.join ( " +" ) , run.heads) ;
209
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
210
assert_eq! ( checks.iter ( ) .find ( | c | c.name = = " flow/build" ) .unwrap ( ) .status, " running" ) ;
211
212
213
let token = a[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
214
assert! ( token.starts_with ( " flowjob_" ) ) ;
e8fa5369 Andy McCall 22 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 22 days ago ↶
218
let bad = RemoteOptions { token: Some ( " flowjob_00" .into ( ) ) , .. Default :: default( ) } ;
42fa9554 Andy McCall 1 month ago ↶
219
assert! ( open_remote ( & url, & bad) .unwrap ( ) .refs ( ) .is_err ( ) , " an unknown job token" ) ;
220
221
c.config.set ( " remote.origin.token" , & token) .unwrap ( ) ;
222
c.save_config ( ) .unwrap ( ) ;
223
write ( work.path ( ) , " more.txt" , " x\n " ) ;
224
c.snapshot ( " test" ) .unwrap ( ) ;
225
c.commit ( " More" , & [ ] ) .unwrap ( ) ;
226
let pushed = c.push ( " origin" , false ) ;
227
assert! ( pushed.is_err ( ) | | pushed.unwrap ( ) .iter ( ) .all ( | r | ! r.ok) , " a job token cannot push" ) ;
228
c.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
229
c.save_config ( ) .unwrap ( ) ;
230
231
232
let a_id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
233
let b_id = b[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
234
let log = serde_json:: json! ( { " records" : [
235
{ " t" : 5 , " stream" : " flow" , " text" : " booting" } ,
236
{ " t" : 120 , " stream" : " out" , " text" : " cc -o demo demo.c" } ,
237
{ " t" : 130 , " stream" : " weird" , " text" : " x" .repeat ( 20_000 ) } ,
238
] } ) ;
239
assert_eq! (
240
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{a_id} /log" ) , Some ( " runner-secret" ) , Some ( log.to_string ( ) ) ) .await.0 ,
241
StatusCode:: NO_CONTENT
242
) ;
243
let lines = db:: flow:: log( & h.app.db, a_id, 0 , 100 ) .await.unwrap ( ) ;
244
assert_eq! ( lines.len ( ) , 3 ) ;
245
assert_eq! ( ( lines[ 2 ] .stream.as_str ( ) , lines[ 2 ] .text.len ( ) ) , ( " console" , 16 * 1024 ) , " streams are known ones, text is capped" ) ;
246
let ph = a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) ;
247
assert_eq! (
248
finish ( & h.app, a_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : receipt ( " lint" , ph, 0 ) } ) ) .await,
249
StatusCode:: BAD_REQUEST ,
250
" a receipt for another job"
251
) ;
252
assert_eq! (
253
finish ( & h.app, a_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : receipt ( " build" , ph, 0 ) } ) ) .await,
254
StatusCode:: NO_CONTENT
255
) ;
256
assert_eq! ( finish ( & h.app, a_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 } ) ) .await, StatusCode:: CONFLICT , " only once" ) ;
257
assert! ( open_remote ( & url, & opts) .unwrap ( ) .refs ( ) .is_err ( ) , " the token ends with its job" ) ;
258
assert_eq! (
259
finish ( & h.app, b_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 300 , " receipt" : receipt ( " lint" , ph, 0 ) } ) ) .await,
260
StatusCode:: NO_CONTENT
261
) ;
262
let t = claim ( & h.app) .await.unwrap ( ) ;
263
assert_eq! ( t[ " job" ] [ " name" ] , " test" ) ;
264
let t_id = t[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
265
assert_eq! (
266
finish ( & h.app, t_id, serde_json:: json! ( { " exit_code" : 3 , " duration_ms" : 500 , " receipt" : receipt ( " test" , ph, 3 ) } ) ) .await,
267
StatusCode:: NO_CONTENT
268
) ;
269
270
let run = db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) ;
271
assert_eq! ( run.status, " failed" ) ;
272
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
273
let build = jobs.iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) ;
274
assert_eq! ( ( build.status.as_str ( ) , build.exit_code, build.receipt_hash.len ( ) ) , ( " passed" , Some ( 0 ) , 64 ) ) ;
275
assert_eq! ( jobs.iter ( ) .find ( | j | j.name = = " test" ) .unwrap ( ) .status, " failed" ) ;
276
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
277
assert_eq! (
278
checks.iter ( ) .map ( | c | ( c.name.as_str ( ) , c.status.as_str ( ) ) ) .collect:: < Vec< _ > > ( ) ,
279
[ ( " flow/build" , " passed" ) , ( " flow/lint" , " passed" ) , ( " flow/test" , " failed" ) ]
280
) ;
a0d4bbfb Andy McCall 1 month ago ↶
281
assert! ( checks[ 2 ] .summary.contains ( " exit 3" ) & & checks[ 2 ] .url.ends_with ( & format! ( " /andy/demo/flow/{} " , run.number) ) ) ;
42fa9554 Andy McCall 1 month ago ↶
282
283
284
let ( st, list) = call ( & h.app, " GET" , " /andy/demo/flow" , None , None ) .await;
285
assert_eq! ( st, StatusCode:: OK ) ;
a0d4bbfb Andy McCall 1 month ago ↶
286
assert! ( list.contains ( & format! ( " /andy/demo/flow/{} " , run.number) ) & & list.contains ( " st-failed" ) , " {list}" ) ;
287
let ( st, page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
42fa9554 Andy McCall 1 month ago ↶
288
assert_eq! ( st, StatusCode:: OK ) ;
289
assert! ( page.contains ( " <svg class=\" flow-graph\" " ) , " graph" ) ;
290
assert_eq! ( page.matches ( " data-job-node=" ) .count ( ) , 3 ) ;
291
assert_eq! ( page.matches ( " <path class=\" edge" ) .count ( ) , 1 , " one dependency edge" ) ;
292
assert! ( page.contains ( " cc -o demo demo.c" ) , " log shown" ) ;
293
assert! ( ! page.contains ( " data-flow-live" ) , " a finished run does not poll" ) ;
a0d4bbfb Andy McCall 1 month ago ↶
294
let ( st, live) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} /live?after={a_id} :1" , run.number) , None , None ) .await;
42fa9554 Andy McCall 1 month ago ↶
295
assert_eq! ( st, StatusCode:: OK ) ;
296
let live: serde_json:: Value = serde_json:: from_str( & live) .unwrap ( ) ;
297
assert_eq! ( live[ " status" ] , " failed" ) ;
298
let build_live = live[ " jobs" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | j | j[ " name" ] = = " build" ) .unwrap ( ) .clone ( ) ;
299
assert_eq! ( build_live[ " lines" ] .as_array ( ) .unwrap ( ) .len ( ) , 2 , " lines after seq 1" ) ;
300
assert_eq! ( call ( & h.app, " GET" , " /andy/demo/flow/999" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
301
302
303
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
304
write ( work.path ( ) , " test.sh" , " echo changed\n " ) ;
305
c.snapshot ( " test" ) .unwrap ( ) ;
306
c.commit ( " Change the test" , & [ ] ) .unwrap ( ) ;
307
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
308
let mut second = None ;
309
for _ in 0 .. 100 {
310
let r = runs_of ( & h.app, " andy" , " demo" ) .await;
311
if r.len ( ) = = 2 {
312
second = Some ( r[ 0 ] .clone ( ) ) ;
313
break ;
314
}
315
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
316
}
317
let second = second.expect ( " a second run" ) ;
318
let x = claim ( & h.app) .await.unwrap ( ) ;
319
let y = claim ( & h.app) .await.unwrap ( ) ;
320
let ( build_id, lint_id) = if x[ " job" ] [ " name" ] = = " build" {
321
( x[ " job_id" ] .as_i64 ( ) .unwrap ( ) , y[ " job_id" ] .as_i64 ( ) .unwrap ( ) )
322
} else {
323
( y[ " job_id" ] .as_i64 ( ) .unwrap ( ) , x[ " job_id" ] .as_i64 ( ) .unwrap ( ) )
324
} ;
325
assert_eq! (
326
finish ( & h.app, build_id, serde_json:: json! ( { " exit_code" : null, " duration_ms" : 10 , " error" : " rootfs is not on this runner" } ) )
327
.await,
328
StatusCode:: NO_CONTENT
329
) ;
330
assert_eq! ( finish ( & h.app, lint_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 } ) ) .await, StatusCode:: NO_CONTENT ) ;
331
assert! ( claim ( & h.app) .await.is_none ( ) , " test was skipped" ) ;
332
let jobs = db:: flow:: jobs( & h.app.db, second.id) .await.unwrap ( ) ;
333
assert_eq! (
334
jobs.iter ( ) .map ( | j | ( j.name.as_str ( ) , j.status.as_str ( ) ) ) .collect:: < Vec< _ > > ( ) ,
335
[ ( " build" , " error" ) , ( " lint" , " passed" ) , ( " test" , " skipped" ) ]
336
) ;
337
assert_eq! ( db:: flow:: run_by_id( & h.app.db, second.id) .await.unwrap ( ) .unwrap ( ) .status, " failed" ) ;
338
339
340
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " job \" broken\" {\n sandbox \" missing\" \n exec \" x\" \n }\n " ) ) ;
341
c.snapshot ( " test" ) .unwrap ( ) ;
342
c.commit ( " Break the pipeline" , & [ ] ) .unwrap ( ) ;
343
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
344
let mut third = None ;
345
for _ in 0 .. 100 {
346
let r = runs_of ( & h.app, " andy" , " demo" ) .await;
347
if r.len ( ) = = 3 {
348
third = Some ( r[ 0 ] .clone ( ) ) ;
349
break ;
350
}
351
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
352
}
353
let third = third.expect ( " a third run" ) ;
354
assert_eq! ( third.status, " error" ) ;
355
assert! ( third.error.contains ( " line 18" ) & & third.error.contains ( " \" missing\" " ) , " {}" , third.error) ;
a0d4bbfb Andy McCall 1 month ago ↶
356
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , third.number) , None , None ) .await;
42fa9554 Andy McCall 1 month ago ↶
357
assert! ( page.contains ( " The pipeline is not valid" ) ) ;
358
736f6813 Andy McCall 1 month ago ↶
359
360
write ( work.path ( ) , " README.md" , " # Demo\n \n [](.flow/run)\n \n \n " ) ;
361
std:: fs:: create_dir_all( work.path ( ) .join ( " img" ) ) .unwrap ( ) ;
362
std:: fs:: write( work.path ( ) .join ( " img/logo.png" ) , b " \x89 PNG\r \n \x1a \n \0 \0 \0 \r IHDR\0 \0 \0 \x01 \0 \0 \0 \x01 \x08 \x06 \0 \0 \0 \x1f \x15 \xc4 \x89 " )
363
.unwrap ( ) ;
364
c.snapshot ( " test" ) .unwrap ( ) ;
365
c.commit ( " A README with a stamp" , & [ ] ) .unwrap ( ) ;
366
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
367
let mut fourth = None ;
368
for _ in 0 .. 100 {
369
let r = runs_of ( & h.app, " andy" , " demo" ) .await;
370
if r.len ( ) = = 4 {
371
fourth = Some ( r[ 0 ] .clone ( ) ) ;
372
break ;
373
}
374
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
375
}
376
let fourth = fourth.expect ( " a fourth run" ) ;
377
let ( _ , overview) = call ( & h.app, " GET" , " /andy/demo" , None , None ) .await;
378
assert! ( overview.contains ( " src=\" /andy/demo/raw/main/.flow/stamp\" " ) , " {overview}" ) ;
379
assert! ( overview.contains ( " href=\" /andy/demo/file/main/.flow/run\" " ) ) ;
380
assert! ( overview.contains ( " src=\" /andy/demo/raw/main/img/logo.png\" " ) , " images use the raw route" ) ;
381
let ( st, _ , ct) = head ( & h.app, " /andy/demo/raw/main/img/logo.png" ) .await;
382
assert_eq! ( ( st, ct.as_str ( ) ) , ( StatusCode:: OK , " image/png" ) ) ;
383
let ( st, loc, _ ) = head ( & h.app, " /andy/demo/raw/main/.flow/stamp" ) .await;
384
assert! ( st.is_redirection ( ) & & loc = = " /andy/demo/flow/stamp/main" , " {st} {loc}" ) ;
385
let ( st, loc, _ ) = head ( & h.app, " /andy/demo/flow/stamp/main" ) .await;
386
assert! ( st.is_redirection ( ) & & loc = = format! ( " /andy/demo/flow/stamp/{} " , fourth.heads) , " a bookmark's stamp names its state: {loc}" ) ;
387
let ( st, _ , ct) = head ( & h.app, & loc) .await;
388
assert_eq! ( ( st, ct.as_str ( ) ) , ( StatusCode:: OK , " image/svg+xml; charset=utf-8" ) ) ;
389
let ( _ , svg) = call ( & h.app, " GET" , & loc, None , None ) .await;
390
assert! ( svg.starts_with ( " <svg" ) & & svg.contains ( " error ·" ) & & svg.contains ( & fourth.heads[ .. 12 ] ) , " {svg}" ) ;
391
let ( _ , svg1) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/stamp/{} " , run.heads) , None , None ) .await;
392
assert! ( svg1.contains ( " failed ·" ) & & svg1.contains ( & format! ( " run #{} failed" , run.id) ) , " an older state keeps its own result: {svg1}" ) ;
393
assert! ( svg1.contains ( & build.receipt_hash) , " receipt hashes in the metadata" ) ;
394
assert_eq! ( svg1.matches ( " text-anchor=\" middle\" " ) .count ( ) , 3 , " one glyph per job" ) ;
395
let ( st, loc, _ ) = head ( & h.app, " /andy/demo/file/main/.flow/run" ) .await;
396
assert! ( st.is_redirection ( ) & & loc = = " /andy/demo/flow/at/main" , " {loc}" ) ;
397
let ( st, loc, _ ) = head ( & h.app, " /andy/demo/flow/at/main" ) .await;
398
assert! ( st.is_redirection ( ) & & loc = = format! ( " /andy/demo/flow/{} " , fourth.id) , " {loc}" ) ;
399
42fa9554 Andy McCall 1 month ago ↶
400
401
let work2 = tempfile:: tempdir( ) .unwrap ( ) ;
402
let c2 = client ( & format! ( " {} /other/plain" , h.base) , work2.path ( ) ) ;
403
write ( work2.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
404
c2.snapshot ( " test" ) .unwrap ( ) ;
405
c2.commit ( " Pipeline" , & [ ] ) .unwrap ( ) ;
406
assert! ( c2.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
407
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 500 ) ) .await;
408
let row = db:: repos:: get( & h.app.db, " other" , " plain" ) .await.unwrap ( ) .unwrap ( ) ;
409
assert! ( db:: flow:: runs( & h.app.db, row.id, 10 , None ) .await.unwrap ( ) .is_empty ( ) ) ;
410
let ( _ , page) = call ( & h.app, " GET" , " /other/plain/flow" , None , None ) .await;
411
assert! ( page.contains ( " not enabled for this repository" ) ) ;
736f6813 Andy McCall 1 month ago ↶
412
let ( _ , loc, _ ) = head ( & h.app, " /other/plain/flow/stamp/main" ) .await;
413
let ( _ , svg) = call ( & h.app, " GET" , & loc, None , None ) .await;
414
assert! ( svg.contains ( " no run ·" ) , " {svg}" ) ;
415
let ( _ , loc, _ ) = head ( & h.app, " /other/plain/flow/at/main" ) .await;
416
assert_eq! ( loc, " /other/plain/flow" ) ;
42fa9554 Andy McCall 1 month ago ↶
417
}
fd3db644 Andy McCall 1 month ago ↶
418
419
# [ tokio ::test ( flavor = " multi_thread" ) ]
420
async fn claims_carry_pinned_subrepos_and_the_job_token_reads_only_those ( ) {
421
let h = hub ( ) .await;
25091e5c Andy McCall 1 month ago ↶
422
for n in [ " base" , " lib" , " vault" , " other" , " app" ] {
fd3db644 Andy McCall 1 month ago ↶
423
create ( & h.app, " andy" , n) .await;
424
}
425
let push = | name : & str , file : & str | {
426
let work = tempfile:: tempdir( ) .unwrap ( ) ;
427
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
428
write ( work.path ( ) , file, " x\n " ) ;
429
c.snapshot ( " t" ) .unwrap ( ) ;
430
c.commit ( " first" , & [ ] ) .unwrap ( ) ;
431
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
432
let heads = c.bookmark_heads ( ) .unwrap ( ) ;
433
( work, heads)
434
} ;
25091e5c Andy McCall 1 month ago ↶
435
let ( _b, base_heads) = push ( " base" , " base.rs" ) ;
436
437
let lib_work = tempfile:: tempdir( ) .unwrap ( ) ;
438
let lib = client ( & format! ( " {} /andy/lib" , h.base) , lib_work.path ( ) ) ;
439
lib.submodule_add ( & format! ( " {} /andy/base" , h.base) , " deps/base" , Some ( " main" ) ) .unwrap ( ) ;
440
write ( lib_work.path ( ) , " lib.rs" , " x\n " ) ;
441
lib.snapshot ( " t" ) .unwrap ( ) ;
442
lib.commit ( " lib on base" , & [ ] ) .unwrap ( ) ;
443
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
444
let lib_heads = lib.bookmark_heads ( ) .unwrap ( ) ;
fd3db644 Andy McCall 1 month ago ↶
445
let ( _v, _ ) = push ( " vault" , " secret.rs" ) ;
446
let ( _o, _ ) = push ( " other" , " o.rs" ) ;
447
448
449
let work = tempfile:: tempdir( ) .unwrap ( ) ;
450
let app = client ( & format! ( " {} /andy/app" , h.base) , work.path ( ) ) ;
451
app.submodule_add ( & format! ( " {} /andy/lib" , h.base) , " deps/lib" , Some ( " main" ) ) .unwrap ( ) ;
a4f01d51 Andy McCall 1 month ago ↶
452
2bbe88a8 Andy McCall 1 month ago ↶
453
454
fd3db644 Andy McCall 1 month ago ↶
455
sqlx:: query( " UPDATE repositories SET visibility = 'private' WHERE name = 'vault'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
a4f01d51 Andy McCall 1 month ago ↶
456
let bare_work = tempfile:: tempdir( ) .unwrap ( ) ;
457
let mut bare = Repo:: init( bare_work.path ( ) ) .unwrap ( ) ;
458
bare.config.set ( " remote.origin.url" , & format! ( " {} /andy/bare" , h.base) ) .unwrap ( ) ;
459
bare.save_config ( ) .unwrap ( ) ;
460
assert! ( bare.submodule_add ( & format! ( " {} /andy/vault" , h.base) , " deps/vault" , Some ( " main" ) ) .is_err ( ) , " not without a token" ) ;
370035a4 Andy McCall 1 month ago ↶
461
app.submodule_add ( & format! ( " {} /andy/vault" , h.base) , " deps/vault" , Some ( " main" ) ) .unwrap ( ) ;
a4f01d51 Andy McCall 1 month ago ↶
462
let nested = Repo:: open( & work.path ( ) .join ( " deps/vault" ) ) .unwrap ( ) ;
2bbe88a8 Andy McCall 1 month ago ↶
463
assert! ( nested.config.remote_tokens.is_empty ( ) , " no token is copied into a subrepo" ) ;
464
nested.fetch ( " origin" ) .unwrap ( ) ;
370035a4 Andy McCall 1 month ago ↶
465
fd3db644 Andy McCall 1 month ago ↶
466
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
467
write ( work.path ( ) , " Makefile" , " all:\n " ) ;
468
app.snapshot ( " t" ) .unwrap ( ) ;
469
app.commit ( " app with deps" , & [ ] ) .unwrap ( ) ;
470
assert! ( app.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
471
assert! ( ! runs_of ( & h.app, " andy" , " app" ) .await.is_empty ( ) ) ;
472
473
let job = claim ( & h.app) .await.unwrap ( ) ;
474
let subs = job[ " subrepos" ] .as_array ( ) .cloned ( ) .unwrap_or_default ( ) ;
25091e5c Andy McCall 1 month ago ↶
475
assert_eq! ( subs.len ( ) , 2 , " lib and what lib pins; vault is private and the pusher has no account: {subs:?}" ) ;
476
assert_eq! ( subs[ 1 ] [ " path" ] , " deps/lib/deps/base" , " nested under its parent, after it" ) ;
477
assert_eq! ( subs[ 1 ] [ " heads" ] , serde_json:: json! ( base_heads.iter ( ) .map ( | h | h.to_hex ( ) ) .collect:: < Vec< _ > > ( ) ) ) ;
370035a4 Andy McCall 1 month ago ↶
478
let _ = std:: fs:: remove_dir_all( work.path ( ) .join ( " deps/vault2" ) ) ;
fd3db644 Andy McCall 1 month ago ↶
479
assert_eq! ( subs[ 0 ] [ " path" ] , " deps/lib" ) ;
480
assert_eq! ( subs[ 0 ] [ " repo" ] , " andy/lib" ) ;
481
assert_eq! ( subs[ 0 ] [ " heads" ] , serde_json:: json! ( lib_heads.iter ( ) .map ( | h | h.to_hex ( ) ) .collect:: < Vec< _ > > ( ) ) ) ;
482
assert! ( subs[ 0 ] [ " clone_url" ] .as_str ( ) .unwrap ( ) .ends_with ( " /andy/lib" ) ) ;
afd0be38 Andy McCall 1 month ago ↶
483
484
let job_id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
485
let lines = db:: flow:: log( & h.app.db, job_id, 0 , 100 ) .await.unwrap ( ) ;
486
assert! (
487
lines.iter ( ) .any ( | l | l.stream = = " flow" & & l.text.contains ( " subrepo deps/vault (andy/vault) is left out" ) ) ,
488
" {:?}" ,
489
lines.iter ( ) .map ( | l | & l.text) .collect:: < Vec< _ > > ( )
490
) ;
fd3db644 Andy McCall 1 month ago ↶
491
492
493
let token = job[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
494
let read = | name : & str | {
495
let opts = RemoteOptions { token: Some ( token.clone ( ) ) , .. Default :: default( ) } ;
496
open_remote ( & format! ( " {} /andy/{name} " , h.base) , & opts) .and_then ( | r | r.refs ( ) ) .is_ok ( )
497
} ;
25091e5c Andy McCall 1 month ago ↶
498
assert! ( read ( " app" ) & & read ( " lib" ) & & read ( " base" ) ) ;
fd3db644 Andy McCall 1 month ago ↶
499
assert! ( ! read ( " vault" ) , " not pinned for this pusher" ) ;
500
assert! ( ! read ( " other" ) , " public, but not the job's to read" ) ;
501
}
941abdd6 Andy McCall 1 month ago ↶
502
503
# [ tokio ::test ( flavor = " multi_thread" , worker_threads = 8 ) ]
504
async fn concurrent_log_appends_never_fail_on_a_locked_database ( ) {
505
let h = hub ( ) .await;
506
create ( & h.app, " andy" , " demo" ) .await;
507
let work = tempfile:: tempdir( ) .unwrap ( ) ;
508
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
509
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
510
write ( work.path ( ) , " Makefile" , " all:\n " ) ;
511
c.snapshot ( " t" ) .unwrap ( ) ;
512
c.commit ( " pipeline" , & [ ] ) .unwrap ( ) ;
513
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
514
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
515
let job = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) .remove ( 0 ) ;
516
517
518
let mut tasks = Vec :: new( ) ;
519
for w in 0 .. 16 {
520
let pool = h.app.db.clone ( ) ;
521
tasks.push ( tokio:: spawn( async move {
522
let records: Vec< flow_core:: api:: Record> =
523
( 0 .. 20 ) .map ( | i | flow_core:: api:: Record { t: i, stream: " out" .into ( ) , text: format! ( " writer {w} line {i} " ) } ) .collect ( ) ;
524
db:: flow:: append_log( & pool, job.id, & records, 100_000 ) .await
525
} ) ) ;
526
}
527
for t in tasks {
528
assert_eq! ( t.await.unwrap ( ) .unwrap ( ) , 20 , " every append succeeds" ) ;
529
}
530
let lines = db:: flow:: log( & h.app.db, job.id, 0 , 1_000 ) .await.unwrap ( ) ;
531
assert_eq! ( lines.len ( ) , 320 ) ;
532
let seqs: std:: collections:: BTreeSet< i64 > = lines.iter ( ) .map ( | l | l.seq ) .collect ( ) ;
533
assert_eq! ( seqs.len ( ) , 320 , " sequence numbers are unique" ) ;
534
}
9008bf6c Andy McCall 1 month ago ↶
535
536
537
# [ tokio ::test ( flavor = " multi_thread" ) ]
538
async fn a_repository_setting_turns_flow_on_or_off ( ) {
539
let h = hub ( ) .await;
540
create ( & h.app, " other" , " plain" ) .await;
541
create ( & h.app, " andy" , " quiet" ) .await;
a0d4bbfb Andy McCall 1 month ago ↶
542
543
create ( & h.app, " andy" , " first" ) .await;
544
{
545
let work = tempfile:: tempdir( ) .unwrap ( ) ;
546
let c = client ( & format! ( " {} /andy/first" , h.base) , work.path ( ) ) ;
547
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
548
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
549
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
550
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
551
assert_eq! ( runs_of ( & h.app, " andy" , " first" ) .await.len ( ) , 1 ) ;
552
}
9008bf6c Andy McCall 1 month ago ↶
553
let row = db:: repos:: get( & h.app.db, " other" , " plain" ) .await.unwrap ( ) .unwrap ( ) ;
554
db:: repos:: set_flow( & h.app.db, row.id, " on" ) .await.unwrap ( ) ;
555
let quiet = db:: repos:: get( & h.app.db, " andy" , " quiet" ) .await.unwrap ( ) .unwrap ( ) ;
556
db:: repos:: set_flow( & h.app.db, quiet.id, " off" ) .await.unwrap ( ) ;
557
for ( owner, name) in [ ( " other" , " plain" ) , ( " andy" , " quiet" ) ] {
558
let work = tempfile:: tempdir( ) .unwrap ( ) ;
559
let c = client ( & format! ( " {} /{owner} /{name} " , h.base) , work.path ( ) ) ;
560
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
561
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
562
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
563
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
564
}
a0d4bbfb Andy McCall 1 month ago ↶
565
let plain = runs_of ( & h.app, " other" , " plain" ) .await;
566
assert_eq! ( plain.len ( ) , 1 , " turned on" ) ;
55673c65 Andy McCall 20 days ago ↶
567
568
bf3d0754 Andy McCall 1 month ago ↶
569
let a = claim ( & h.app) .await.unwrap ( ) ;
570
assert_eq! ( a[ " repo" ] , " andy/first" ) ;
571
let ( _ , page) = call ( & h.app, " GET" , " /other/plain/flow/1" , None , None ) .await;
55673c65 Andy McCall 20 days ago ↶
572
assert! ( page.contains ( " Waiting for a runner: 1st in line" ) & & page.contains ( " Running now: andy/first #1" ) , " {page}" ) ;
bf3d0754 Andy McCall 1 month ago ↶
573
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/other/plain/flow/runs/1" , None , None ) .await;
55673c65 Andy McCall 20 days ago ↶
574
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " queue_position" ] , 1 ) ;
bf3d0754 Andy McCall 1 month ago ↶
575
let ( _ , list) = call ( & h.app, " GET" , " /other/plain/flow" , None , None ) .await;
55673c65 Andy McCall 20 days ago ↶
576
assert! ( list.contains ( " queued, 1st in line" ) , " {list}" ) ;
a0d4bbfb Andy McCall 1 month ago ↶
577
578
579
assert_eq! ( plain[ 0 ] .number, 1 ) ;
90326c96 Andy McCall 1 month ago ↶
580
let checks = checks_when ( & h.app, row.id, & plain[ 0 ] .state, 3 ) .await;
a0d4bbfb Andy McCall 1 month ago ↶
581
assert! ( checks.iter ( ) .all ( | c | c.url.ends_with ( " /other/plain/flow/1" ) ) , " {checks:?}" ) ;
582
let ( st, page) = call ( & h.app, " GET" , " /other/plain/flow/1" , None , None ) .await;
583
assert! ( st = = StatusCode:: OK & & page.contains ( " Run #1" ) , " {st}" ) ;
9008bf6c Andy McCall 1 month ago ↶
584
assert! ( runs_of ( & h.app, " andy" , " quiet" ) .await.is_empty ( ) , " turned off despite andy/*" ) ;
585
}
1edac4b6 Andy McCall 1 month ago ↶
586
587
588
589
# [ tokio ::test ( flavor = " multi_thread" ) ]
590
async fn runs_start_without_a_push_and_finished_runs_re_run ( ) {
591
let h = hub ( ) .await;
592
593
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
594
create ( & h.app, " andy" , " demo" ) .await;
595
let url = format! ( " {} /andy/demo" , h.base) ;
596
let work = tempfile:: tempdir( ) .unwrap ( ) ;
597
let c = client ( & url, work.path ( ) ) ;
598
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
599
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
600
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
601
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
602
let first = runs_of ( & h.app, " andy" , " demo" ) .await;
603
assert_eq! ( ( first.len ( ) , first[ 0 ] .trigger.as_str ( ) ) , ( 1 , " push" ) ) ;
604
605
606
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
607
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
608
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
609
assert_eq! ( v[ " number" ] , 2 ) ;
610
assert! ( v[ " url" ] .as_str ( ) .unwrap ( ) .ends_with ( " /andy/demo/flow/2" ) ) ;
611
let second = db:: flow:: run( & h.app.db, first[ 0 ] .repo_id, 2 ) .await.unwrap ( ) .unwrap ( ) ;
612
assert_eq! ( ( second.trigger.as_str ( ) , second.actor.as_str ( ) , second.state.as_str ( ) ) , ( " manual" , " andy" , first[ 0 ] .state.as_str ( ) ) ) ;
613
assert_eq! ( db:: flow:: jobs( & h.app.db, second.id) .await.unwrap ( ) .len ( ) , 3 ) ;
614
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/2" , None , None ) .await;
615
assert! ( page.contains ( " started by andy" ) , " the run page says what started it" ) ;
616
4f692533 Andy McCall 1 month ago ↶
617
618
let head = first[ 0 ] .head_list ( ) [ 0 ] .to_string ( ) ;
619
let ( st, body) =
620
call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( format! ( r # "{{ "change":"{head} "}} "# ) ) ) .await;
621
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
622
let on_change = db:: flow:: run( & h.app.db, first[ 0 ] .repo_id, 3 ) .await.unwrap ( ) .unwrap ( ) ;
623
assert_eq! (
624
( on_change.bookmark.as_str ( ) , on_change.heads.as_str ( ) , on_change.state.as_str ( ) ) ,
625
( " " , head.as_str ( ) , first[ 0 ] .state.as_str ( ) )
626
) ;
627
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/3" , None , None ) .await;
628
assert! ( page.contains ( " title=\" Run on one change, not a bookmark\" >change</span>" ) , " run page" ) ;
629
let ( st, body) =
630
call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( format! ( r # "{{ "change":"{} "}} "# , " ab" .repeat ( 32 ) ) ) )
631
.await;
632
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " no change" ) , " {st} {body}" ) ;
633
634
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow" , None , None ) .await;
635
assert! ( page.contains ( " Sign in with write access to run the pipeline." ) & & ! page.contains ( " primary-btn" ) ) ;
636
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/bookmarks" , None , None ) .await;
637
assert! ( ! page.contains ( " /flow/start" ) , " no run buttons for anonymous viewers" ) ;
638
1edac4b6 Andy McCall 1 month ago ↶
639
640
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs/1/rerun" , Some ( " site" ) , None ) .await;
641
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " still queued" ) , " {st} {body}" ) ;
642
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( r # "{"bookmark":"nope"}"# .into ( ) ) ) .await;
643
assert_eq! ( st, StatusCode:: CONFLICT ) ;
644
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs/99/rerun" , Some ( " site" ) , None ) .await;
645
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
646
647
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , None , Some ( " {}" .into ( ) ) ) .await;
648
assert_ne! ( st, StatusCode:: CREATED ) ;
649
650
651
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
652
for r in [ & first[ 0 ] , & second] {
653
for j in db:: flow:: jobs( & h.app.db, r.id) .await.unwrap ( ) {
3b1bc27f Andy McCall 20 days ago
654
sqlx:: query( " UPDATE flow_jobs SET status = 'passed' WHERE id = $1" ) .bind ( j.id) .execute ( & h.app.db) .await.unwrap ( ) ;
1edac4b6 Andy McCall 1 month ago ↶
655
}
3b1bc27f Andy McCall 20 days ago
656
sqlx:: query( " UPDATE flow_runs SET status = 'passed', finished_ms = 1 WHERE id = $1" ) .bind ( r.id) .execute ( & h.app.db) .await.unwrap ( ) ;
1edac4b6 Andy McCall 1 month ago ↶
657
}
658
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs/1/rerun" , Some ( " site" ) , None ) .await;
659
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
4f692533 Andy McCall 1 month ago ↶
660
let third = db:: flow:: run( & h.app.db, row.id, 4 ) .await.unwrap ( ) .unwrap ( ) ;
348f8c4b Andy McCall 1 month ago ↶
661
assert_eq! (
662
( third.trigger_label ( ) .as_str ( ) , third.status.as_str ( ) , third.state.as_str ( ) ) ,
663
( " rerun:1" , " queued" , first[ 0 ] .state.as_str ( ) )
664
) ;
c889911b Andy McCall 1 month ago ↶
665
assert_eq! ( third.trigger, " rerun:1:push" , " a rerun keeps the event it repeats" ) ;
1edac4b6 Andy McCall 1 month ago ↶
666
let checks = db:: hooks:: checks_for_state( & h.app.db, row.id, & third.state) .await.unwrap ( ) ;
667
assert! (
4f692533 Andy McCall 1 month ago ↶
668
checks.iter ( ) .all ( | c | c.status = = " queued" & & c.url.ends_with ( " /flow/4" ) ) ,
1edac4b6 Andy McCall 1 month ago ↶
669
" the re-run's checks replace the old ones: {checks:?}"
670
) ;
4f692533 Andy McCall 1 month ago ↶
671
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/4" , None , None ) .await;
1edac4b6 Andy McCall 1 month ago ↶
672
assert! ( page.contains ( " re-run of #1 by andy" ) ) ;
673
assert! ( ! page.contains ( " RE-RUN" ) , " anonymous viewers get no button" ) ;
674
0650d94e Andy McCall 25 days ago ↶
675
676
677
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( r # "{"jobs":["test"]}"# .into ( ) ) ) .await;
678
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
679
let n = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " number" ] .as_i64 ( ) .unwrap ( ) ;
680
let only = db:: flow:: run( & h.app.db, first[ 0 ] .repo_id, n) .await.unwrap ( ) .unwrap ( ) ;
681
let mut names: Vec< String> = db:: flow:: jobs( & h.app.db, only.id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
682
names.sort ( ) ;
683
assert_eq! ( names, [ " build" , " test" ] , " test and the job it depends on, not lint" ) ;
684
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( r # "{"jobs":["deploy"]}"# .into ( ) ) ) .await;
685
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " not in the pipeline" ) & & body.contains ( " build, lint, test" ) , " {st} {body}" ) ;
686
1edac4b6 Andy McCall 1 month ago ↶
687
688
db:: repos:: set_flow( & h.app.db, row.id, " off" ) .await.unwrap ( ) ;
689
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
690
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " not enabled" ) , " {body}" ) ;
4f692533 Andy McCall 1 month ago ↶
691
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow" , None , None ) .await;
692
assert! ( page.contains ( " Flow is off for this repository" ) , " and the Flow tab says so" ) ;
90326c96 Andy McCall 1 month ago ↶
693
}
694
695
696
697
# [ tokio ::test ( flavor = " multi_thread" ) ]
698
async fn runs_jobs_and_logs_are_in_the_api ( ) {
699
let h = hub ( ) .await;
700
create ( & h.app, " andy" , " demo" ) .await;
701
let url = format! ( " {} /andy/demo" , h.base) ;
702
let work = tempfile:: tempdir( ) .unwrap ( ) ;
703
let c = client ( & url, work.path ( ) ) ;
704
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
705
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
706
let head = c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
707
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
708
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
709
710
711
let a = claim ( & h.app) .await.unwrap ( ) ;
712
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
713
let recs =
714
serde_json:: json! ( { " records" : [ { " t" : 1 , " stream" : " stdout" , " text" : " one" } , { " t" : 3 , " stream" : " stderr" , " text" : " three" } ] } ) ;
715
let ( st, _ ) = call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( recs.to_string ( ) ) ) .await;
716
assert! ( st.is_success ( ) , " {st}" ) ;
717
718
let ( st, body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs" , None , None ) .await;
719
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
720
let runs: Vec< hub_api:: FlowRun> = serde_json:: from_str( & body) .unwrap ( ) ;
721
assert_eq! ( runs.len ( ) , 1 ) ;
722
assert_eq! ( ( runs[ 0 ] .number, runs[ 0 ] .state.as_str ( ) , runs[ 0 ] .trigger.as_str ( ) ) , ( 1 , run.state.as_str ( ) , " push" ) ) ;
723
assert! ( runs[ 0 ] .url.ends_with ( " /andy/demo/flow/1" ) & & runs[ 0 ] .jobs.is_empty ( ) ) ;
724
725
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , None , None ) .await;
726
let one: hub_api:: FlowRun = serde_json:: from_str( & body) .unwrap ( ) ;
727
let names: Vec< ( & str , & str ) > = one.jobs.iter ( ) .map ( | j | ( j.name.as_str ( ) , j.status.as_str ( ) ) ) .collect ( ) ;
728
assert_eq! ( names.len ( ) , 3 ) ;
729
let running = one.jobs.iter ( ) .find ( | j | j.status = = " running" ) .unwrap ( ) ;
730
assert_eq! ( running.name, a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) ) ;
731
assert! ( one.jobs.iter ( ) .any ( | j | j.name = = " test" & & j.depends_on = = [ " build" ] ) ) ;
732
733
let job = running.name.clone ( ) ;
734
let ( st, body) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/demo/flow/runs/1/jobs/{job} /log" ) , None , None ) .await;
735
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
736
let lines: Vec< hub_api:: FlowLogLine> = serde_json:: from_str( & body) .unwrap ( ) ;
737
assert! ( lines.iter ( ) .any ( | l | l.text = = " three" ) , " {body}" ) ;
738
let last = lines.last ( ) .unwrap ( ) .seq;
739
let ( _ , body) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/demo/flow/runs/1/jobs/{job} /log?after={last} " ) , None , None ) .await;
740
assert_eq! ( body, " []" , " nothing after the last line" ) ;
741
assert_eq! ( call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/9" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
742
assert_eq! ( call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1/jobs/nope/log" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
743
744
745
let by = | key : String| {
746
let app = h.app.clone ( ) ;
747
async move {
748
let ( st, body) = call ( & app, " GET" , & format! ( " /api/v1/repos/andy/demo/checks/{key} " ) , None , None ) .await;
749
assert_eq! ( st, StatusCode:: OK , " {key}: {body}" ) ;
750
serde_json:: from_str:: < Vec< hub_api:: Check> > ( & body) .unwrap ( ) .len ( )
751
}
752
} ;
753
checks_when ( & h.app, run.repo_id, & run.state, 3 ) .await;
754
assert_eq! ( by ( run.state.clone ( ) ) .await, 3 ) ;
755
assert_eq! ( by ( head.to_hex ( ) ) .await, 3 , " a single head names its state" ) ;
756
assert_eq! ( by ( " main" .into ( ) ) .await, 3 ) ;
757
let ( st, _ ) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/checks/no-such-bookmark" , None , None ) .await;
758
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
759
760
761
let ( st, body) = call ( & h.app, " GET" , " /api/v1/users/andy/repos" , None , None ) .await;
762
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
763
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
764
assert_eq! ( v[ " status" ] , 404 , " {body}" ) ;
1edac4b6 Andy McCall 1 month ago ↶
765
}
cc19697f Andy McCall 1 month ago ↶
766
767
async fn get_bytes ( app : & Shared, path : & str ) -> ( StatusCode, Vec< u8 > ) {
768
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( Request:: get( path) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
769
let status = res.status ( ) ;
770
( status, res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) )
771
}
772
773
# [ tokio ::test ( flavor = " multi_thread" ) ]
774
async fn signed_receipts_are_checked_kept_and_shown ( ) {
775
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectRef, ObjectType} ;
776
use oxy_core:: identity:: SecretKey;
777
use oxy_core:: objects:: StateObject;
778
use oxy_core:: receipt:: SignedReceipt;
779
let h = hub ( ) .await;
780
create ( & h.app, " andy" , " demo" ) .await;
781
let url = format! ( " {} /andy/demo" , h.base) ;
782
let work = tempfile:: tempdir( ) .unwrap ( ) ;
783
let c = client ( & url, work.path ( ) ) ;
784
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
785
c.snapshot ( " test" ) .unwrap ( ) ;
786
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
787
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
788
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
789
let heads: Vec< ChangeHash> = run.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
790
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads.clone ( ) ) .encode ( ) ) ;
791
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
792
let sk = SecretKey:: generate( ) ;
793
let runner = sk.identity ( " test-1 (OxygenFlow runner)" , " " , 1 ) ;
794
let sign = | r : & serde_json:: Value, state : ObjectRef, sk : & SecretKey, id : & oxy_core:: identity:: Identity| {
795
let r: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
796
let run_url = format! ( " {url} /flow/{} " , run.number) ;
797
hex:: encode( SignedReceipt:: sign( r.to_oxy ( ) , repo_id, state, & run_url, id, sk) .encode ( ) )
798
} ;
799
800
let a = claim ( & h.app) .await.unwrap ( ) ;
801
let b = claim ( & h.app) .await.unwrap ( ) ;
802
let ph = a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
803
let ( a_id, b_id) = ( a[ " job_id" ] .as_i64 ( ) .unwrap ( ) , b[ " job_id" ] .as_i64 ( ) .unwrap ( ) ) ;
804
805
let rb = receipt ( " build" , & ph, 0 ) ;
806
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : rb, " signed_receipt" : sign ( & rb, state, & sk, & runner) } ) ;
807
assert_eq! ( finish ( & h.app, a_id, body) .await, StatusCode:: NO_CONTENT ) ;
808
809
let other = SecretKey:: generate( ) ;
810
let rl = receipt ( " lint" , & ph, 0 ) ;
811
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 300 , " receipt" : rl,
812
" signed_receipt" : sign ( & rl, state, & other, & other.identity ( " mallory" , " " , 1 ) ) } ) ;
813
assert_eq! ( finish ( & h.app, b_id, body) .await, StatusCode:: NO_CONTENT ) ;
814
815
let t = claim ( & h.app) .await.unwrap ( ) ;
816
let rt = receipt ( " test" , & ph, 0 ) ;
817
let body = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 500 , " receipt" : rt,
818
" signed_receipt" : sign ( & rt, ObjectRef( [ 9 ; 32 ] ) , & sk, & runner) } ) ;
819
assert_eq! ( finish ( & h.app, t[ " job_id" ] .as_i64 ( ) .unwrap ( ) , body) .await, StatusCode:: NO_CONTENT ) ;
820
821
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
822
let job = | n : & str | jobs.iter ( ) .find ( | j | j.name = = n ) .unwrap ( ) .clone ( ) ;
823
assert_eq! ( job ( " build" ) .status, " passed" ) ;
824
assert_eq! ( ( job ( " build" ) .receipt_attest.len ( ) , job ( " build" ) .receipt_signer.as_str ( ) ) , ( 64 , " test-1 (OxygenFlow runner)" ) ) ;
825
assert_eq! ( job ( " lint" ) .status, " passed" , " the job's result stands" ) ;
826
assert! ( job ( " lint" ) .receipt_attest.is_empty ( ) ) ;
827
assert! ( job ( " lint" ) .receipt_note.contains ( " not the key this runner signs with" ) , " {}" , job ( " lint" ) .receipt_note) ;
828
assert! ( job ( " test" ) .receipt_note.contains ( " another state" ) , " {}" , job ( " test" ) .receipt_note) ;
829
830
831
832
let kept = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .receipts ( & heads) .unwrap ( ) ;
833
assert_eq! ( kept.len ( ) , 1 ) ;
834
let attest = job ( " build" ) .receipt_attest;
835
let ( st, bytes) = get_bytes ( & h.app, & format! ( " /andy/demo/receipts/{attest} " ) ) .await;
836
assert_eq! ( st, StatusCode:: OK ) ;
837
let sr = SignedReceipt:: decode( & bytes) .unwrap ( ) ;
838
sr.verify ( ) .unwrap ( ) ;
839
assert_eq! ( ( sr.state ( ) , sr.receipt.job_name.as_str ( ) ) , ( state, " build" ) ) ;
840
assert_eq! ( get_bytes ( & h.app, & format! ( " /andy/demo/receipts/{} " , " 0" .repeat ( 64 ) ) ) .await.0 , StatusCode:: NOT_FOUND ) ;
841
842
843
844
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
845
assert! ( page.contains ( " signed receipt</a> by test-1" ) , " run page" ) ;
846
assert! ( page.contains ( " receipt not kept" ) , " run page" ) ;
847
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/change/{} " , heads[ 0 ] .to_hex ( ) ) , None , None ) .await;
848
assert! ( page.contains ( " <dt>Receipts</dt>" ) & & page.contains ( & format! ( " /andy/demo/receipts/{attest} " ) ) , " change page" ) ;
849
}
a3c8288b Andy McCall 1 month ago ↶
850
851
async fn upload ( app : & Shared, job : i64 , name : & str , bytes : & [u8 ]) -> ( StatusCode, String) {
852
let req = Request:: post( format! ( " /api/v1/flow/jobs/{job} /artifacts/{name} " ) )
853
.header ( header:: AUTHORIZATION , " Bearer runner-secret" )
854
.header ( header:: CONTENT_TYPE , " application/octet-stream" )
855
.body ( Body:: from( bytes.to_vec ( ) ) )
856
.unwrap ( ) ;
857
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
858
let status = res.status ( ) ;
859
( status, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
860
}
861
862
# [ tokio ::test ( flavor = " multi_thread" ) ]
863
async fn artifacts_are_kept_named_by_the_receipt_and_served ( ) {
864
let h = hub ( ) .await;
865
create ( & h.app, " andy" , " demo" ) .await;
866
let url = format! ( " {} /andy/demo" , h.base) ;
867
let work = tempfile:: tempdir( ) .unwrap ( ) ;
868
let c = client ( & url, work.path ( ) ) ;
869
write (
870
work.path ( ) ,
871
" .flow/pipeline.kdl" ,
872
& format! (
873
" schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n output {{ \n artifact \" tool\" {{ \n path \" /src/out/tool\" \n }} \n }} \n }} \n "
874
) ,
875
) ;
876
c.snapshot ( " test" ) .unwrap ( ) ;
877
c.commit ( " Build a tool" , & [ ] ) .unwrap ( ) ;
878
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
879
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
880
let a = claim ( & h.app) .await.unwrap ( ) ;
881
assert_eq! ( a[ " job" ] [ " outputs" ] , serde_json:: json! ( [ { " name" : " tool" , " path" : " /src/out/tool" } ] ) , " the runner is told" ) ;
882
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
883
884
let body: Vec< u8 > = ( 0 .. 300_000 u32 ) .map ( | i | ( i % 253 ) as u8 ) .collect ( ) ;
885
let ( st, text) = upload ( & h.app, id, " other" , & body) .await;
886
assert_eq! ( st, StatusCode:: BAD_REQUEST , " an undeclared artifact: {text}" ) ;
887
let ( st, text) = upload ( & h.app, id, " tool" , & body) .await;
888
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
889
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
890
assert_eq! ( blob, oxy_core:: repo:: Repo:: blob_ref( & body) .to_hex ( ) , " the same blob the runner computes" ) ;
891
892
893
let mut r = receipt ( " build" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
894
assert_eq! (
895
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : r.clone ( ) } ) ) .await,
896
StatusCode:: BAD_REQUEST
897
) ;
898
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
899
assert_eq! ( finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : r } ) ) .await, StatusCode:: NO_CONTENT ) ;
900
901
902
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
903
let link = format! ( " /andy/demo/flow/{} /artifacts/build/tool" , run.number) ;
904
assert! ( page.contains ( & link) , " run page lists the artifact" ) ;
16ee5d19 Andy McCall 18 days ago ↶
905
906
907
let ( _ , list) = call ( & h.app, " GET" , " /andy/demo/artifacts" , None , None ) .await;
908
assert! ( list.contains ( & link) & & list.contains ( & format! ( " #{} </a>" , run.number) ) , " {list}" ) ;
909
assert! ( list.contains ( " <summary class=\" on\" >Artifacts</summary>" ) & & list.contains ( " href=\" /andy/demo/images\" " ) , " {list}" ) ;
a3c8288b Andy McCall 1 month ago ↶
910
let ( st, got) = get_bytes ( & h.app, & link) .await;
911
assert_eq! ( ( st, got = = body) , ( StatusCode:: OK , true ) ) ;
912
assert_eq! ( get_bytes ( & h.app, & format! ( " /andy/demo/flow/{} /artifacts/build/none" , run.number) ) .await.0 , StatusCode:: NOT_FOUND ) ;
076c7a7c Andy McCall 19 days ago ↶
913
914
915
let prov = format! ( " {link} /provenance" ) ;
916
assert_eq! ( get_bytes ( & h.app, & prov) .await.0 , StatusCode:: BAD_REQUEST , " no kept receipt, no provenance" ) ;
917
sqlx:: query( " UPDATE flow_jobs SET receipt_attest = 'a1b2', receipt_signer = 'test-1 runner' WHERE id = $1" )
918
.bind ( id)
919
.execute ( & h.app.db)
920
.await
921
.unwrap ( ) ;
922
let ( st, env) = get_bytes ( & h.app, & prov) .await;
923
assert_eq! ( st, StatusCode:: OK ) ;
924
let env: serde_json:: Value = serde_json:: from_slice( & env) .unwrap ( ) ;
925
assert_eq! ( env[ " payloadType" ] , " application/vnd.in-toto+json" ) ;
681db69e Andy McCall 19 days ago ↶
926
let unb64 = | t : & str | oxy_core:: dsse:: base64_decode( t) .expect ( " base64" ) ;
076c7a7c Andy McCall 19 days ago ↶
927
let payload = unb64 ( env[ " payload" ] .as_str ( ) .unwrap ( ) ) ;
928
let sig = unb64 ( env[ " signatures" ] [ 0 ] [ " sig" ] .as_str ( ) .unwrap ( ) ) ;
929
let signed = oxygenhub:: provenance:: pae( " application/vnd.in-toto+json" , & payload) ;
930
ring:: signature:: UnparsedPublicKey:: new( & ring:: signature:: ED25519 , h.app.hub_key.identity.ed25519_pk)
931
.verify ( & signed, & sig)
932
.expect ( " the hub's key signed the envelope" ) ;
933
let statement: serde_json:: Value = serde_json:: from_slice( & payload) .unwrap ( ) ;
934
use sha2:: Digest;
935
assert_eq! ( statement[ " subject" ] [ 0 ] [ " digest" ] [ " sha256" ] , hex:: encode( sha2:: Sha256:: digest( & body) ) ) ;
936
assert_eq! ( statement[ " predicateType" ] , " https://slsa.dev/provenance/v1" ) ;
937
assert_eq! ( statement[ " predicate" ] [ " buildDefinition" ] [ " externalParameters" ] [ " job" ] , " build" ) ;
938
assert_eq! ( statement[ " predicate" ] [ " runDetails" ] [ " byproducts" ] [ 0 ] [ " digest" ] [ " oxygen-attestation" ] , " a1b2" ) ;
939
let ( _ , pem) = get_bytes ( & h.app, " /.well-known/oxyhub-signing.pem" ) .await;
940
let pem = String :: from_utf8( pem) .unwrap ( ) ;
941
let der = unb64 ( pem.lines ( ) .filter ( | l | ! l.starts_with ( " -----" ) ) .collect:: < String> ( ) .as_str ( ) ) ;
942
assert_eq! ( & der[ der.len ( ) - 32 .. ] , & h.app.hub_key.identity.ed25519_pk[ .. ] , " the PEM holds the hub's key" ) ;
943
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 ↶
944
45977501 Andy McCall 26 days ago ↶
945
946
947
{
948
let name = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
949
let mut hosted = oxy_core:: repo:: Repo:: open( & h.app.repos.dir ( & name) ) .unwrap ( ) ;
950
hosted.config.set ( " core.gc-grace" , " 0" ) .unwrap ( ) ;
951
hosted.save_config ( ) .unwrap ( ) ;
952
}
953
let done = oxygenhub:: maintenance:: gc_all( & h.app) .await;
954
assert! ( done.iter ( ) .all ( | d | ! d.failed) , " {done:?}" ) ;
955
let ( st, got) = get_bytes ( & h.app, & link) .await;
956
assert_eq! ( ( st, got = = body) , ( StatusCode:: OK , true ) , " the artifact survives gc" ) ;
957
10708ab8 Andy McCall 1 month ago ↶
958
959
960
let form = | f : String| {
961
Request:: post( " /register" ) .header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) .body ( Body:: from( f) ) .unwrap ( )
962
} ;
963
let res = oxygenhub:: router( h.app.clone ( ) )
c8684d44 Andy McCall 20 days ago
964
.oneshot ( form ( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" .into ( ) ) )
10708ab8 Andy McCall 1 month ago ↶
965
.await
966
.unwrap ( ) ;
967
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
968
let with_cookie = | method : & str , path : & str , body : Option< String> | {
969
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
970
if body.is_some ( ) {
971
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
972
}
973
req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( )
974
} ;
975
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
976
db:: releases:: create( & h.app.db, row.id, " v1" , " One" , " " , false , None ) .await.unwrap ( ) ;
977
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( with_cookie ( " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None ) ) .await.unwrap ( ) ;
978
let page = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
979
let attach = format! ( " /andy/demo/flow/{} /artifacts/build/tool/attach" , run.number) ;
980
assert! ( page.contains ( & attach) , " a writer is offered the release" ) ;
981
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
982
let csrf = page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
983
let release_id = db:: releases:: list( & h.app.db, row.id) .await.unwrap ( ) [ 0 ] .id;
984
let res = oxygenhub:: router( h.app.clone ( ) )
985
.oneshot ( with_cookie ( " POST" , & attach, Some ( format! ( " _csrf={csrf} &release={release_id} " ) ) ) )
986
.await
987
.unwrap ( ) ;
988
assert_eq! ( res.status ( ) , StatusCode:: SEE_OTHER ) ;
989
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/releases/tag/v1" , None , None ) .await;
990
assert! (
991
page.contains ( " >tool</a>" ) & & page.contains ( & format! ( " built by <a href=\" /andy/demo/flow/{0} \" >run #{0} </a> build" , run.number) ) ,
992
" {page}"
993
) ;
994
let ( _ , api) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/releases" , None , None ) .await;
995
let api: serde_json:: Value = serde_json:: from_str( & api) .unwrap ( ) ;
996
let asset = & api[ 0 ] [ " assets" ] [ 0 ] ;
997
assert_eq! (
998
( asset[ " name" ] .as_str ( ) , asset[ " built_by" ] [ " run" ] .as_i64 ( ) , asset[ " built_by" ] [ " job" ] .as_str ( ) ) ,
999
( Some ( " tool" ) , Some ( run.number) , Some ( " build" ) )
1000
) ;
1001
assert! ( asset[ " built_by" ] [ " receipt" ] .is_null ( ) , " that receipt was not signed" ) ;
97fc47fe Andy McCall 19 days ago ↶
1002
assert! ( asset[ " built_by" ] [ " provenance_url" ] .is_null ( ) , " no signed receipt, no provenance" ) ;
1003
1004
1005
sqlx:: query( " UPDATE flow_jobs SET receipt_attest = 'a1b2', receipt_signer = 'earth-1' WHERE run_id = $1 AND name = 'build'" )
1006
.bind ( run.id)
1007
.execute ( & h.app.db)
1008
.await
1009
.unwrap ( ) ;
1010
let want = format! ( " /andy/demo/flow/{} /artifacts/build/tool/provenance" , run.number) ;
1011
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/releases/tag/v1" , None , None ) .await;
1012
assert! ( page.contains ( & format! ( " href=\" {want} \" " ) ) , " {page}" ) ;
1013
let ( _ , api) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/releases" , None , None ) .await;
1014
let api: serde_json:: Value = serde_json:: from_str( & api) .unwrap ( ) ;
1015
assert! ( api[ 0 ] [ " assets" ] [ 0 ] [ " built_by" ] [ " provenance_url" ] .as_str ( ) .unwrap ( ) .ends_with ( & want) ) ;
a3c8288b Andy McCall 1 month ago ↶
1016
}
df940d75 Andy McCall 1 month ago ↶
1017
d29070fa Andy McCall 1 month ago ↶
1018
1019
a36bab6b Andy McCall 25 days ago ↶
1020
async fn upload_report_file ( app : & Shared, job : i64 , name : & str , path : & str , bytes : & [u8 ]) -> ( StatusCode, String) {
1021
let req = Request:: post( format! ( " /api/v1/flow/jobs/{job} /reports/{name} /files" ) )
1022
.header ( header:: AUTHORIZATION , " Bearer runner-secret" )
1023
.header ( header:: CONTENT_TYPE , " application/octet-stream" )
1024
.header ( " X-Flow-Report-Path" , path)
1025
.body ( Body:: from( bytes.to_vec ( ) ) )
1026
.unwrap ( ) ;
1027
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
1028
let status = res.status ( ) ;
1029
( status, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
1030
}
1031
1032
1033
1034
1035
# [ tokio ::test ( flavor = " multi_thread" ) ]
1036
async fn reports_are_kept_file_by_file_and_indexed ( ) {
1037
let h = hub ( ) .await;
1038
create ( & h.app, " andy" , " demo" ) .await;
1039
let url = format! ( " {} /andy/demo" , h.base) ;
1040
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1041
let c = client ( & url, work.path ( ) ) ;
1042
write (
1043
work.path ( ) ,
1044
" .flow/pipeline.kdl" ,
1045
& format! (
1046
" 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 "
1047
) ,
1048
) ;
1049
c.snapshot ( " test" ) .unwrap ( ) ;
1050
c.commit ( " Measure frames" , & [ ] ) .unwrap ( ) ;
1051
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1052
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
1053
let a = claim ( & h.app) .await.unwrap ( ) ;
1054
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1055
1056
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 " ;
1057
let page = b " <p>frames</p>" .to_vec ( ) ;
1058
let data: Vec< u8 > = ( 0 .. 100_000 u32 ) .map ( | i | ( i % 241 ) as u8 ) .collect ( ) ;
c8df33a6 Andy McCall 25 days ago ↶
1059
let ( page_bytes, data_bytes) = ( page.clone ( ) , data.clone ( ) ) ;
a36bab6b Andy McCall 25 days ago ↶
1060
let files: [ ( & str , & [ u8 ] ) ; 3 ] = [ ( " data/frames.csv" , & data) , ( " index.html" , & page) , ( " report.toml" , toml) ] ;
1061
1062
assert_eq! ( upload_report_file ( & h.app, id, " tool" , " report.toml" , toml) .await.0 , StatusCode:: BAD_REQUEST , " not a report" ) ;
1063
assert_eq! ( upload_report_file ( & h.app, id, " frame-timing" , " ../x" , toml) .await.0 , StatusCode:: BAD_REQUEST , " not a clean path" ) ;
1064
let mut listed = vec! [ ] ;
1065
for ( path, bytes) in files {
1066
let ( st, text) = upload_report_file ( & h.app, id, " frame-timing" , path, bytes) .await;
1067
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
1068
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1069
assert_eq! ( blob, oxy_core:: repo:: Repo:: blob_ref( bytes) .to_hex ( ) ) ;
1070
listed.push ( serde_json:: json! ( { " path" : path, " blob" : blob, " size" : bytes.len ( ) } ) ) ;
1071
}
1072
let mut missing = listed.clone ( ) ;
1073
missing.push ( serde_json:: json! ( { " path" : " assets/x.js" , " blob" : " ab" .repeat ( 32 ) , " size" : 1 } ) ) ;
1074
let ( st, text) =
1075
upload ( & h.app, id, " frame-timing" , & serde_json:: to_vec( & serde_json:: json! ( { " version" : 1 , " files" : missing } ) ) .unwrap ( ) ) .await;
1076
assert_eq! ( st, StatusCode:: BAD_REQUEST , " a file never uploaded: {text}" ) ;
1077
assert! ( text.contains ( " assets/x.js" ) , " {text}" ) ;
1078
let manifest = serde_json:: to_vec( & serde_json:: json! ( { " version" : 1 , " files" : listed } ) ) .unwrap ( ) ;
1079
let ( st, text) = upload ( & h.app, id, " frame-timing" , & manifest) .await;
1080
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
1081
1082
let ( kind, title, summary, files_n, bytes) : ( String , String , String , i64 , i64 ) =
3b1bc27f Andy McCall 20 days ago
1083
sqlx:: query_as( " SELECT kind, title, summary, files, bytes FROM reports WHERE job_id = $1 AND name = 'frame-timing'" )
a36bab6b Andy McCall 25 days ago ↶
1084
.bind ( id)
1085
.fetch_one ( & h.app.db)
1086
.await
1087
.unwrap ( ) ;
1088
assert_eq! ( ( kind.as_str ( ) , title.as_str ( ) , summary.as_str ( ) ) , ( " performance" , " Frame timing" , " Median 16.4 ms" ) ) ;
1089
assert_eq! ( ( files_n, bytes) , ( 3 , ( data.len ( ) + page.len ( ) + toml.len ( ) ) as i64 ) ) ;
3b1bc27f Andy McCall 20 days ago
1090
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 25 days ago ↶
1091
let m = oxygenhub:: reports:: metrics( & h.app.db, rid) .await.unwrap ( ) ;
1092
assert_eq! ( ( m[ 0 ] .key.as_str ( ) , m[ 0 ] .value, m[ 0 ] .fail_above) , ( " median_ms" , 16. 4 , Some ( 20. 0 ) ) ) ;
1093
let log = db:: flow:: log( & h.app.db, id, 0 , 1000 ) .await.unwrap ( ) ;
1094
assert! ( log.iter ( ) .any ( | r | r.text.contains ( " report frame-timing: performance \" Frame timing\" , 1 metric" ) ) , " {log:?}" ) ;
c8df33a6 Andy McCall 25 days ago ↶
1095
1096
1097
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
1098
let c = checks.iter ( ) .find ( | c | c.name = = " report/frame-timing/median_ms" ) .expect ( " a check per threshold" ) ;
1099
assert_eq! ( c.status, " passed" , " {c:?}" ) ;
1100
assert! ( c.summary.contains ( " 16.4 ms" ) & & c.url.contains ( " /reports/frame-timing?id=" ) , " {c:?}" ) ;
1101
1102
1103
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo" , None , None ) .await;
1104
assert! ( page.contains ( " >Reports</a>" ) , " the tab" ) ;
1105
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/reports" , None , None ) .await;
1106
assert! ( st = = StatusCode:: OK & & page.contains ( " Frame timing" ) & & page.contains ( " median_ms" ) , " {st}" ) ;
1107
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/reports/frame-timing" , None , None ) .await;
1108
assert! ( st = = StatusCode:: OK & & page.contains ( " fails above 20" ) & & page.contains ( " sandbox=\" allow-scripts\" " ) , " {st}" ) ;
1109
let src = page.split ( " class=\" rep-frame\" src=\" " ) .nth ( 1 ) .unwrap ( ) .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1110
assert! ( src.ends_with ( " /index.html" ) , " {src}" ) ;
1111
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( Request:: get( & src) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
1112
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
1113
let csp = res.headers ( ) [ header:: CONTENT_SECURITY_POLICY ] .to_str ( ) .unwrap ( ) .to_string ( ) ;
1114
assert! ( csp.starts_with ( " sandbox allow-scripts;" ) & & csp.contains ( " default-src 'none'" ) , " {csp}" ) ;
d74ce22f Andy McCall 20 days ago ↶
1115
1116
assert! (
1117
res.headers ( ) .get ( header:: X_FRAME_OPTIONS ) .is_none ( ) & & res.headers ( ) .get ( header:: CONTENT_SECURITY_POLICY_REPORT_ONLY ) .is_none ( )
1118
) ;
1119
assert_eq! ( res.headers ( ) [ header:: X_CONTENT_TYPE_OPTIONS ] , " nosniff" ) ;
c8df33a6 Andy McCall 25 days ago ↶
1120
assert_eq! ( res.headers ( ) [ header:: ACCESS_CONTROL_ALLOW_ORIGIN ] , " *" ) ;
f3e5e2bc Andy McCall 25 days ago ↶
1121
1122
let served = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ;
1123
assert! ( served.starts_with ( & page_bytes) , " the report's page" ) ;
1124
assert! ( String :: from_utf8_lossy( & served) .contains ( " /static/report-frame.js" ) , " with the frame script" ) ;
1125
let ( st, _ ) = get_bytes ( & h.app, " /static/report.css" ) .await;
1126
assert_eq! ( st, StatusCode:: OK , " the stylesheet a report may link" ) ;
c8df33a6 Andy McCall 25 days ago ↶
1127
let data = src.replace ( " /index.html" , " /data/frames.csv" ) ;
1128
assert_eq! ( get_bytes ( & h.app, & data) .await, ( StatusCode:: OK , data_bytes.clone ( ) ) ) ;
1129
95b525b0 Andy McCall 25 days ago ↶
1130
1131
1132
let first: i64 = sqlx:: query_scalar( " SELECT id FROM reports WHERE name = 'frame-timing'" ) .fetch_one ( & h.app.db) .await.unwrap ( ) ;
1133
let second: i64 = sqlx:: query_scalar(
1134
" INSERT INTO reports (repo_id, name, kind, title, summary, context, run_id, job_id, run_number, job, bookmark, state, manifest, files, bytes, created_ms) \
3b1bc27f Andy McCall 20 days ago
1135
SELECT repo_id, name, kind, title, 'Median 18.0 ms', context, run_id, NULL, 2, job, bookmark, state, manifest, files, bytes, created_ms + 1000 FROM reports WHERE id = $1 RETURNING id" ,
95b525b0 Andy McCall 25 days ago ↶
1136
)
1137
.bind ( first)
1138
.fetch_one ( & h.app.db)
1139
.await
1140
.unwrap ( ) ;
1141
sqlx:: query(
3b1bc27f Andy McCall 20 days ago
1142
" INSERT INTO report_metrics (report_id, key, value, unit, better, fail_above) VALUES ($1, 'median_ms', 18.0, 'ms', 'lower', 20)" ,
95b525b0 Andy McCall 25 days ago ↶
1143
)
1144
.bind ( second)
1145
.execute ( & h.app.db)
1146
.await
1147
.unwrap ( ) ;
1148
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/reports/frame-timing" , None , None ) .await;
1149
assert_eq! ( page.matches ( " data-tl-stop" ) .count ( ) , 2 , " a stop per run" ) ;
1150
assert! ( page.contains ( " Median 18.0 ms" ) , " the newest run is shown" ) ;
1151
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/reports/frame-timing?id={first} &compare={second} &play=3" ) , None , None ) .await;
1152
assert! ( page.contains ( " This run against run #2" ) & & page.contains ( " -1.6" ) , " every metric against the other run" ) ;
1153
let stop = page.split ( " data-tl-stop" ) .next ( ) .unwrap_or ( " " ) .rsplit ( " href=" ) .next ( ) .unwrap_or ( " " ) .to_string ( ) ;
1154
assert! ( page.contains ( " data-play=\" 3\" " ) , " playing" ) ;
1155
assert! ( stop.contains ( " play" ) & & stop.contains ( ' 3' ) , " playing carries on from stop to stop: {stop}" ) ;
1156
assert! ( page.contains ( " data-report-runs=" ) , " the frame interface knows the runs" ) ;
c8df33a6 Andy McCall 25 days ago ↶
1157
let forged = src.replacen ( " /files/" , " /files/9" , 1 ) ;
1158
assert_eq! ( get_bytes ( & h.app, & forged) .await.0 , StatusCode:: NOT_FOUND ) ;
1159
assert_eq! ( get_bytes ( & h.app, & src.replace ( " /index.html" , " /secret.txt" ) ) .await.0 , StatusCode:: NOT_FOUND ) ;
a36bab6b Andy McCall 25 days ago ↶
1160
let _ = run;
1161
1162
1163
{
1164
let name = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
1165
let mut hosted = oxy_core:: repo:: Repo:: open( & h.app.repos.dir ( & name) ) .unwrap ( ) ;
1166
hosted.config.set ( " core.gc-grace" , " 0" ) .unwrap ( ) ;
1167
hosted.save_config ( ) .unwrap ( ) ;
1168
}
1169
let done = oxygenhub:: maintenance:: gc_all( & h.app) .await;
1170
assert! ( done.iter ( ) .all ( | d | ! d.failed) , " {done:?}" ) ;
1171
let hosted = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
1172
for ( path, bytes) in files {
1173
assert_eq! ( hosted.read_blob ( & oxy_core:: repo:: Repo:: blob_ref( bytes) ) .unwrap ( ) , bytes, " {path} survives gc" ) ;
1174
}
1175
}
1176
d29070fa Andy McCall 1 month ago ↶
1177
# [ tokio ::test ( flavor = " multi_thread" ) ]
1178
async fn a_failing_job_keeps_an_artifact_outside_its_receipt ( ) {
1179
let h = hub ( ) .await;
1180
create ( & h.app, " andy" , " demo" ) .await;
1181
let url = format! ( " {} /andy/demo" , h.base) ;
1182
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1183
let c = client ( & url, work.path ( ) ) ;
1184
write (
1185
work.path ( ) ,
1186
" .flow/pipeline.kdl" ,
1187
& format! (
336c953a Andy McCall 1 month ago ↶
1188
" 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 ↶
1189
) ,
1190
) ;
1191
c.snapshot ( " test" ) .unwrap ( ) ;
1192
c.commit ( " Test" , & [ ] ) .unwrap ( ) ;
1193
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1194
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
1195
let a = claim ( & h.app) .await.unwrap ( ) ;
336c953a Andy McCall 1 month ago ↶
1196
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 ↶
1197
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
0b203bbf Andy McCall 1 month ago ↶
1198
1199
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>"# ;
1200
let ( st, text) = upload ( & h.app, id, " junit" , xml) .await;
d29070fa Andy McCall 1 month ago ↶
1201
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
1202
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1203
let mut r = receipt ( " test" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 1 ) ;
1204
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
1205
assert_eq! (
1206
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 1 , " duration_ms" : 700 , " receipt" : r.clone ( ) } ) ) .await,
1207
StatusCode:: BAD_REQUEST ,
1208
" a failing receipt names no outputs"
1209
) ;
1210
r[ " outputs" ] = serde_json:: json! ( [ ] ) ;
1211
assert_eq! ( finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 1 , " duration_ms" : 700 , " receipt" : r } ) ) .await, StatusCode:: NO_CONTENT ) ;
1212
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/flow/{} " , run.number) , None , None ) .await;
1213
assert! ( page.contains ( & format! ( " /andy/demo/flow/{} /artifacts/test/junit" , run.number) ) , " listed" ) ;
1214
assert! ( page.contains ( " not in its receipt" ) , " marked" ) ;
0b203bbf Andy McCall 1 month ago ↶
1215
assert! ( page.contains ( " 2 tests: 1 failed" ) , " counts beside the job" ) ;
1216
assert! ( page.contains ( " tests.test_io::test_write" ) & & page.contains ( " disk full" ) & & page.contains ( " OSError" ) , " the failure" ) ;
1217
assert! ( page.contains ( " tests: 2 tests: 1 failed in 2.0 s" ) , " said in the job's log" ) ;
d29070fa Andy McCall 1 month ago ↶
1218
}
1219
df940d75 Andy McCall 1 month ago ↶
1220
1221
1222
1223
# [ tokio ::test ( flavor = " multi_thread" ) ]
1224
async fn a_release_is_audited_from_authors_to_receipts ( ) {
1225
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectType} ;
1226
use oxy_core:: identity:: SecretKey;
1227
use oxy_core:: objects:: StateObject;
1228
use oxy_core:: receipt:: SignedReceipt;
1229
let h = hub ( ) .await;
1230
create ( & h.app, " andy" , " demo" ) .await;
1231
let url = format! ( " {} /andy/demo" , h.base) ;
1232
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1233
let mut c = client ( & url, work.path ( ) ) ;
1234
c.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
1235
write (
1236
work.path ( ) ,
1237
" .flow/pipeline.kdl" ,
1238
& format! (
1239
" 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 "
1240
) ,
1241
) ;
1242
c.snapshot ( " test" ) .unwrap ( ) ;
1243
c.commit ( " Build a tool" , & [ ] ) .unwrap ( ) ;
1244
let heads = c.bookmark_heads ( ) .unwrap ( ) ;
1245
c.tag_create ( " v1" , & heads, " first" ) .unwrap ( ) ;
1246
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1247
1248
1249
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
1250
let a = claim ( & h.app) .await.unwrap ( ) ;
1251
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1252
let body: Vec< u8 > = b " #!/bin/sh\n echo tool\n " .to_vec ( ) ;
1253
let ( _ , text) = upload ( & h.app, id, " tool" , & body) .await;
1254
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1255
let mut r = receipt ( " build" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
1256
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
1257
let heads_run: Vec< ChangeHash> = run.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
1258
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads_run) .encode ( ) ) ;
1259
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
1260
let sk = SecretKey:: generate( ) ;
1261
let runner = sk.identity ( " test-1 (OxygenFlow runner)" , " " , 1 ) ;
1262
let fr: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
1263
let signed = hex:: encode( SignedReceipt:: sign( fr.to_oxy ( ) , repo_id, state, & format! ( " {url} /flow/1" ) , & runner, & sk) .encode ( ) ) ;
1264
let done = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 900 , " receipt" : r, " signed_receipt" : signed } ) ;
1265
assert_eq! ( finish ( & h.app, id, done) .await, StatusCode:: NO_CONTENT ) ;
1266
1267
1268
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
1269
let rel = db:: releases:: create( & h.app.db, row.id, " v1" , " One" , " " , false , None ) .await.unwrap ( ) ;
1270
oxygenhub:: web:: releases:: store_asset( & h.app, rel.id, " tool" , " " , & body) .await.unwrap ( ) ;
1271
oxygenhub:: web:: releases:: store_asset( & h.app, rel.id, " notes.txt" , " " , b " by hand\n " ) .await.unwrap ( ) ;
1272
1273
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/releases/trust/v1" , None , None ) .await;
1274
assert_eq! ( st, StatusCode:: OK ) ;
1275
for want in [
1276
" Tag v1 is signed" ,
1277
" The one change in the release is authenticated" ,
1278
" tool: signed receipt from run #1 build" ,
1279
" a runner key of this hub" ,
edcef275 Andy McCall 1 month ago ↶
1280
" The tagged state is one change" ,
df940d75 Andy McCall 1 month ago ↶
1281
" tool: built from the tagged state" ,
1282
" tool: pinned environment" ,
1283
" notes.txt: no build found" ,
1284
" <svg class=\" trust-graph\" " ,
bf5101d3 Andy McCall 1 month ago ↶
1285
1286
1287
" Tag signed by" ,
1288
" 1 of 1 changes signed or sealed" ,
1289
" 1 of 2 assets built and attested by OxygenFlow" ,
1290
" attested build" ,
1291
" uploaded by hand" ,
1292
" receipt verified" ,
1293
" class=\" shackle\" " ,
1294
" <title>notes.txt:" ,
df940d75 Andy McCall 1 month ago ↶
1295
] {
1296
assert! ( page.contains ( want) , " missing {want:?}" ) ;
1297
}
bf5101d3 Andy McCall 1 month ago ↶
1298
let svg = & page[ page.find ( " <svg class=\" trust-graph\" " ) .unwrap ( ) .. ] ;
1299
assert! ( svg[ .. 200 ] .contains ( " width=\" " ) & & svg[ .. 200 ] .contains ( " height=\" " ) , " the graph has its own size: {}" , & svg[ .. 200 ] ) ;
df940d75 Andy McCall 1 month ago ↶
1300
let ( _ , releases) = call ( & h.app, " GET" , " /andy/demo/releases" , None , None ) .await;
1301
assert! ( releases.contains ( " /andy/demo/releases/trust/v1" ) , " the release links its audit" ) ;
1302
1303
1304
let ( st, json) = call ( & h.app, " GET" , " /andy/demo/releases/trust/v1?format=json" , None , None ) .await;
1305
assert_eq! ( st, StatusCode:: OK ) ;
1306
let t: serde_json:: Value = serde_json:: from_str( & json) .unwrap ( ) ;
1307
assert_eq! ( ( t[ " tag" ] .as_str ( ) , t[ " tag_signature" ] .as_str ( ) , t[ " changes" ] .as_i64 ( ) ) , ( Some ( " v1" ) , Some ( " valid" ) , Some ( 1 ) ) ) ;
1308
assert_eq! ( t[ " authors" ] [ 0 ] [ " who" ] [ " name" ] , " Andy" ) ;
1309
let tool = t[ " assets" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | a | a[ " name" ] = = " tool" ) .unwrap ( ) ;
1310
let hex_receipt = tool[ " build" ] [ " signed_receipt" ] .as_str ( ) .unwrap ( ) ;
1311
let sr = SignedReceipt:: decode( & hex:: decode( hex_receipt) .unwrap ( ) ) .unwrap ( ) ;
1312
sr.verify ( ) .unwrap ( ) ;
1313
assert_eq! ( sr.receipt.outputs[ 0 ] .to_hex ( ) , blob) ;
1314
let audit = t[ " audit" ] .as_array ( ) .unwrap ( ) ;
1315
assert! ( audit.iter ( ) .any ( | c | c[ " verdict" ] = = " warn" & & c[ " title" ] .as_str ( ) .unwrap ( ) .starts_with ( " notes.txt" ) ) ) ;
1316
assert! ( ! audit.iter ( ) .any ( | c | c[ " verdict" ] = = " fail" ) , " {audit:?}" ) ;
1317
1318
let ( st, api) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/releases/trust/v1" , None , None ) .await;
1319
assert_eq! ( st, StatusCode:: OK ) ;
1320
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & api) .unwrap ( ) [ " assets" ] .as_array ( ) .unwrap ( ) .len ( ) , 2 ) ;
1321
assert_eq! ( call ( & h.app, " GET" , " /api/v1/repos/andy/demo/releases/trust/v9" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
1322
assert_eq! ( call ( & h.app, " GET" , " /andy/demo/releases/trust/v9" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
1323
}
b6b7e4cf Andy McCall 1 month ago ↶
1324
1325
1326
1327
1328
# [ tokio ::test ( flavor = " multi_thread" ) ]
1329
async fn runners_register_show_their_history_and_are_drained_and_removed ( ) {
1330
use oxy_core:: identity:: SecretKey;
1331
let h = hub ( ) .await;
1332
create ( & h.app, " andy" , " demo" ) .await;
1333
1334
let res = oxygenhub:: router( h.app.clone ( ) )
1335
.oneshot (
1336
Request:: post( " /register" )
1337
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 20 days ago
1338
.body ( Body:: from( " username=carol&email=carol%40example.com&password=correct-horse-battery&setup=test-setup" ) )
b6b7e4cf Andy McCall 1 month ago ↶
1339
.unwrap ( ) ,
1340
)
1341
.await
1342
.unwrap ( ) ;
1343
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1344
let admin = db:: accounts:: account_by_name( & h.app.db, " carol" ) .await.unwrap ( ) .unwrap ( ) ;
1345
db:: accounts:: set_admin( & h.app.db, admin.id, true ) .await.unwrap ( ) ;
1346
let web = | method : & 'static str , path : String, body : Option< String> | {
1347
let ( app, cookie) = ( h.app.clone ( ) , cookie.clone ( ) ) ;
1348
async move {
1349
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie) ;
1350
if body.is_some ( ) {
1351
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
1352
}
1353
let res = oxygenhub:: router( app) .oneshot ( req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ) .await.unwrap ( ) ;
1354
let st = res.status ( ) ;
1355
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
1356
}
1357
} ;
1358
let csrf_of = | page : & str | {
1359
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
1360
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
1361
} ;
1362
let ( st, page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1363
assert_eq! ( st, StatusCode:: OK ) ;
1364
assert! ( page.contains ( " No runners yet" ) ) ;
1365
assert_eq! ( call ( & h.app, " GET" , " /admin/runners" , None , None ) .await.0 , StatusCode:: NOT_FOUND , " administrators only" ) ;
1366
1367
1368
let ( _ , page) = web ( " POST" , " /admin/runners/token" .into ( ) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1369
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
1370
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
1371
assert! ( token.starts_with ( " flowreg_" ) , " {token}" ) ;
1372
1373
1374
let sk = SecretKey:: generate( ) ;
1375
let identity = sk.identity ( " plex (OxygenFlow runner)" , " " , 1 ) ;
1376
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 } ) ;
1377
let reg = serde_json:: json! ( { " token" : token, " name" : " plex" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1378
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1379
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1380
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1381
assert! ( credential.starts_with ( " flowrunner_" ) ) ;
1382
assert_eq! (
1383
call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await.0 ,
1384
StatusCode:: FORBIDDEN ,
1385
" used once"
1386
) ;
1387
1388
1389
let claim_as = | who : String| {
1390
let app = h.app.clone ( ) ;
1391
let info = info.clone ( ) ;
1392
async move {
1393
let body = serde_json:: json! ( { " runner" : " ignored" , " runtimes" : [ " microvm" ] , " info" : info } ) .to_string ( ) ;
1394
call ( & app, " POST" , " /api/v1/flow/claim" , Some ( & who) , Some ( body) ) .await
1395
}
1396
} ;
1397
assert_eq! ( claim_as ( credential.clone ( ) ) .await.0 , StatusCode:: NO_CONTENT , " nothing queued" ) ;
1398
assert_eq! ( claim ( & h.app) .await, None ) ;
1399
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1400
assert! (
1401
page.contains ( " >plex</strong>" ) & & page.contains ( " st-online" ) & & page.contains ( " x86_64 · linux" ) & & page.contains ( " big-disk" ) ,
1402
" {page}"
1403
) ;
1404
assert! ( page.contains ( " >test-1</strong>" ) & & page.contains ( " hub.toml" ) , " the configured runner, by its claimed name" ) ;
1405
let plex = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " plex" ) .unwrap ( ) ;
1406
assert_eq! ( plex.key, identity.hash ( ) .to_hex ( ) , " bound at registration" ) ;
1407
1408
1409
let url = format! ( " {} /andy/demo" , h.base) ;
1410
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1411
let c = client ( & url, work.path ( ) ) ;
1412
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1413
c.snapshot ( " test" ) .unwrap ( ) ;
1414
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1415
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1416
runs_of ( & h.app, " andy" , " demo" ) .await;
1417
let ( st, body) = claim_as ( credential.clone ( ) ) .await;
1418
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1419
let job: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1420
let ( _ , page) = web ( " GET" , format! ( " /admin/runners/{} " , plex.id) , None ) .await;
1421
assert! (
1422
page.contains ( " andy/demo" ) & & page.contains ( job[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) ) & & page.contains ( " Running now</dt><dd>1 job" ) ,
1423
" {page}"
1424
) ;
b66e70b7 Andy McCall 1 month ago ↶
1425
1426
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
e9b9619c Andy McCall 20 days ago ↶
1427
assert! ( page.contains ( " id=\" queue\" " ) & & page.contains ( " >plex</a></td>" ) , " {page}" ) ;
1428
1429
1430
assert! ( page.contains ( " running 1 of " ) & & page.contains ( " /admin/runners?runner=plex#queue" ) , " {page}" ) ;
1431
let ( _ , only) = web ( " GET" , " /admin/runners?runner=plex" .into ( ) , None ) .await;
1432
assert! ( only.contains ( " >plex</a></td>" ) & & only.contains ( " of " ) & & only.contains ( " >all</a>" ) , " {only}" ) ;
1433
let ( _ , none) = web ( " GET" , " /admin/runners?runner=nobody&status=running" .into ( ) , None ) .await;
1434
assert! ( none.contains ( " Nothing matches." ) , " {none}" ) ;
1435
1436
sqlx:: query( " UPDATE flow_runners SET last_seen_ms = 0 WHERE id = $1" ) .bind ( plex.id) .execute ( & h.app.db) .await.unwrap ( ) ;
1437
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1438
assert! ( page.contains ( " st-offline" ) , " quiet for long: offline" ) ;
1439
assert_eq! ( call ( & h.app, " POST" , " /api/v1/flow/runners/heartbeat" , Some ( & credential) , None ) .await.0 , StatusCode:: NO_CONTENT ) ;
1440
assert_eq! ( call ( & h.app, " POST" , " /api/v1/flow/runners/heartbeat" , Some ( " flowrunner_nope" ) , None ) .await.0 , StatusCode:: UNAUTHORIZED ) ;
1441
let plex_now = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " plex" ) .unwrap ( ) ;
1442
assert_eq! ( plex_now.connectivity ( ) , " online" ) ;
b66e70b7 Andy McCall 1 month ago ↶
1443
let ( _ , tokens) = web ( " GET" , " /settings/tokens" .into ( ) , None ) .await;
1444
let ( _ , made) = web ( " POST" , " /settings/tokens" .into ( ) , Some ( format! ( " _csrf={} &name=admin&scopes=user%3Aread" , csrf_of ( & tokens) ) ) ) .await;
1445
let i = made.find ( " oxyhub_" ) .unwrap ( ) ;
1446
let admin_token: String = made[ i.. ] .chars ( ) .take_while ( | c | c.is_ascii_alphanumeric ( ) | | * c = = ' _' ) .collect ( ) ;
1447
let ( st, body) = call ( & h.app, " GET" , " /api/v1/runners" , Some ( & admin_token) , None ) .await;
1448
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1449
let listed: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1450
let plex_row = listed.as_array ( ) .unwrap ( ) .iter ( ) .find ( | r | r[ " name" ] = = " plex" ) .unwrap ( ) ;
1451
assert_eq! ( ( plex_row[ " running" ] .as_i64 ( ) , plex_row[ " arch" ] .as_str ( ) ) , ( Some ( 1 ) , Some ( " x86_64" ) ) ) ;
7f5f6c05 Andy McCall 1 month ago ↶
1452
1453
let ( _ , page) = web ( " GET" , format! ( " /admin/runners/{} " , plex.id) , None ) .await;
1454
let ( st, _ ) = web (
1455
" POST" ,
1456
format! ( " /admin/runners/{} /limits" , plex.id) ,
1457
Some ( format! ( " _csrf={} &labels=gpu%2C+plex&timeout_minutes=5" , csrf_of ( & page) ) ) ,
1458
)
1459
.await;
1460
assert_eq! ( st, StatusCode:: SEE_OTHER ) ;
1461
let ( _ , page) = web ( " GET" , format! ( " /admin/runners/{} " , plex.id) , None ) .await;
1462
assert! ( page.contains ( " >gpu<" ) & & page.contains ( " 5 min for its jobs" ) , " {page}" ) ;
1463
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/runners" , Some ( & admin_token) , None ) .await;
1464
let listed: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1465
let plex_row = listed.as_array ( ) .unwrap ( ) .iter ( ) .find ( | r | r[ " name" ] = = " plex" ) .unwrap ( ) ;
1466
assert! ( plex_row[ " labels" ] .as_array ( ) .unwrap ( ) .iter ( ) .any ( | l | l = = " gpu" ) , " {plex_row}" ) ;
1467
let ( st, _ ) = web (
1468
" POST" ,
1469
format! ( " /admin/runners/{} /limits" , plex.id) ,
1470
Some ( format! ( " _csrf={} &labels=bad%20label%21&timeout_minutes=" , csrf_of ( & page) ) ) ,
1471
)
1472
.await;
1473
assert_eq! ( st, StatusCode:: BAD_REQUEST , " a label with a space or punctuation is refused" ) ;
b66e70b7 Andy McCall 1 month ago ↶
1474
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/runners/queue" , Some ( & admin_token) , None ) .await;
1475
let queue: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1476
assert! ( queue.as_array ( ) .unwrap ( ) .iter ( ) .any ( | q | q[ " status" ] = = " running" & & q[ " runner" ] = = " plex" ) , " {queue}" ) ;
1477
let ( _ , body) = call ( & h.app, " GET" , & format! ( " /api/v1/runners/{} /jobs" , plex.id) , Some ( & admin_token) , None ) .await;
1478
let jobs: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
1479
assert_eq! ( jobs[ 0 ] [ " repo" ] , " andy/demo" , " {jobs}" ) ;
1480
assert_eq! ( call ( & h.app, " GET" , " /api/v1/runners" , None , None ) .await.0 , StatusCode:: NOT_FOUND , " administrators only" ) ;
b6b7e4cf Andy McCall 1 month ago ↶
1481
1482
1483
let ( _ , page) = web ( " GET" , " /admin/runners" .into ( ) , None ) .await;
1484
let csrf = csrf_of ( & page) ;
1485
web ( " POST" , format! ( " /admin/runners/{} /state" , plex.id) , Some ( format! ( " _csrf={csrf} &state=draining" ) ) ) .await;
1486
assert_eq! ( claim_as ( credential.clone ( ) ) .await.0 , StatusCode:: NO_CONTENT , " a draining runner takes nothing" ) ;
1487
let ( _ , page) = web ( " POST" , format! ( " /admin/runners/{} /remove" , plex.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1488
assert! ( page.contains ( " is running jobs: drain it first" ) , " {page}" ) ;
1489
1490
let id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1491
let done = call (
1492
& h.app,
1493
" POST" ,
1494
& format! ( " /api/v1/flow/jobs/{id} /finish" ) ,
1495
Some ( & credential) ,
1496
Some ( r # "{"exit_code":0,"duration_ms":5}"# .into ( ) ) ,
1497
)
1498
.await;
1499
assert_eq! ( done.0 , StatusCode:: NO_CONTENT ) ;
1500
web ( " POST" , format! ( " /admin/runners/{} /state" , plex.id) , Some ( format! ( " _csrf={csrf} &state=disabled" ) ) ) .await;
1501
assert_eq! ( claim_as ( credential.clone ( ) ) .await.0 , StatusCode:: FORBIDDEN ) ;
1502
let ( _ , page) = web ( " POST" , format! ( " /admin/runners/{} /remove" , plex.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1503
assert! ( page.contains ( " Removed plex." ) , " {page}" ) ;
1504
assert_eq! ( claim_as ( credential) .await.0 , StatusCode:: UNAUTHORIZED ) ;
1505
}
74be9796 Andy McCall 1 month ago ↶
1506
1507
1508
1509
# [ tokio ::test ( flavor = " multi_thread" ) ]
1510
async fn scoped_runners_take_only_their_scope ( ) {
1511
use oxy_core:: identity:: SecretKey;
1512
let h = hub ( ) .await;
1513
create ( & h.app, " andy" , " demo" ) .await;
1514
create ( & h.app, " andy" , " other" ) .await;
1515
let register = | scope : db:: runners:: Scope, name : & 'static str | {
1516
let app = h.app.clone ( ) ;
1517
async move {
1518
let token = format! ( " flowreg_{name} " ) ;
1519
db:: runners:: add_registration( & app.db, & blake3:: hash( token.as_bytes ( ) ) .to_hex ( ) , " admin" , & scope) .await.unwrap ( ) ;
1520
let identity = SecretKey:: generate( ) .identity ( name, " " , 1 ) ;
1521
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
1522
let reg = serde_json:: json! ( { " token" : token, " name" : name, " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1523
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1524
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1525
serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( )
1526
}
1527
} ;
1528
let demo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
1529
let only_demo = register ( db:: runners:: Scope { owner: " andy" .into ( ) , repo_id: Some ( demo.id) } , " demo-runner" ) .await;
1530
let carols = register ( db:: runners:: Scope { owner: " carol" .into ( ) , repo_id: None } , " carol-runner" ) .await;
1531
let andys = register ( db:: runners:: Scope { owner: " andy" .into ( ) , repo_id: None } , " andy-runner" ) .await;
1532
let claim_as = | who : String| {
1533
let app = h.app.clone ( ) ;
1534
async move {
1535
let body = serde_json:: json! ( { " runner" : " x" , " runtimes" : [ " microvm" ] } ) .to_string ( ) ;
1536
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( & who) , Some ( body) ) .await;
1537
( st = = StatusCode:: OK ) .then ( | | serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " repo" ] .as_str ( ) .unwrap ( ) .to_string ( ) )
1538
}
1539
} ;
1540
1541
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1542
let c = client ( & format! ( " {} /andy/other" , h.base) , work.path ( ) ) ;
1543
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1544
c.snapshot ( " t" ) .unwrap ( ) ;
1545
c.commit ( " pipeline" , & [ ] ) .unwrap ( ) ;
1546
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1547
assert! ( ! runs_of ( & h.app, " andy" , " other" ) .await.is_empty ( ) ) ;
1548
assert_eq! ( claim_as ( only_demo.clone ( ) ) .await, None , " nothing of andy/demo is queued" ) ;
1549
assert_eq! ( claim_as ( carols) .await, None , " nothing of carol's" ) ;
1550
assert_eq! ( claim_as ( andys) .await.as_deref ( ) , Some ( " andy/other" ) , " the owner's runner takes its repositories' jobs" ) ;
1551
1552
assert_eq! ( claim ( & h.app) .await.unwrap ( ) [ " repo" ] , " andy/other" ) ;
1553
assert_eq! ( claim_as ( only_demo) .await, None , " still nothing of its own" ) ;
1554
}
7974adb0 Andy McCall 1 month ago ↶
1555
1556
1557
1558
1559
# [ tokio ::test ( flavor = " multi_thread" ) ]
1560
async fn repository_admins_add_and_remove_their_own_runners ( ) {
1561
use oxy_core:: identity:: SecretKey;
1562
let h = hub ( ) .await;
1563
let res = oxygenhub:: router( h.app.clone ( ) )
1564
.oneshot (
1565
Request:: post( " /register" )
1566
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 20 days ago
1567
.body ( Body:: from( " username=andy&email=andy%40example.com&password=correct-horse-battery&setup=test-setup" ) )
7974adb0 Andy McCall 1 month ago ↶
1568
.unwrap ( ) ,
1569
)
1570
.await
1571
.unwrap ( ) ;
1572
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
1573
create ( & h.app, " andy" , " demo" ) .await;
1574
let demo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
1575
let web = | method : & 'static str , path : String, body : Option< String> | {
1576
let ( app, cookie) = ( h.app.clone ( ) , cookie.clone ( ) ) ;
1577
async move {
1578
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie) ;
1579
if body.is_some ( ) {
1580
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
1581
}
1582
let res = oxygenhub:: router( app) .oneshot ( req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ) .await.unwrap ( ) ;
1583
let st = res.status ( ) ;
1584
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
1585
}
1586
} ;
1587
let csrf_of = | page : & str | {
1588
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
1589
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
1590
} ;
c7067c7b Andy McCall 16 days ago
1591
let ( st, page) = web ( " GET" , " /andy/demo/settings/runners" .into ( ) , None ) .await;
7974adb0 Andy McCall 1 month ago ↶
1592
assert_eq! ( st, StatusCode:: OK ) ;
c7067c7b Andy McCall 16 days ago
1593
assert! ( page.contains ( " <h2>Runners</h2>" ) , " the owner sees the page" ) ;
7974adb0 Andy McCall 1 month ago ↶
1594
let ( _ , page) = web ( " POST" , " /andy/demo/settings/runners/token" .into ( ) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1595
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
1596
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
1597
let identity = SecretKey:: generate( ) .identity ( " mine" , " " , 1 ) ;
1598
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
1599
let reg = serde_json:: json! ( { " token" : token, " name" : " mine" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1600
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1601
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1602
let mine = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " mine" ) .unwrap ( ) ;
1603
assert_eq! ( mine.scope ( ) , db:: runners:: Scope { owner: " andy" .into ( ) , repo_id: Some ( demo.id) } ) ;
1604
1605
claim ( & h.app) .await;
1606
let site = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " test-1" ) .unwrap ( ) ;
c7067c7b Andy McCall 16 days ago
1607
let ( _ , page) = web ( " GET" , " /andy/demo/settings/runners" .into ( ) , None ) .await;
7974adb0 Andy McCall 1 month ago ↶
1608
assert! ( page.contains ( " <strong>mine</strong>" ) & & page.contains ( " this repository" ) , " {page}" ) ;
1609
assert! ( page.contains ( " <strong>test-1</strong>" ) & & page.contains ( " the whole site" ) , " {page}" ) ;
1610
let csrf = csrf_of ( & page) ;
1611
let ( st, _ ) = web ( " POST" , format! ( " /andy/demo/settings/runners/{} /remove" , site.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1612
assert_eq! ( st, StatusCode:: NOT_FOUND , " not the repository's own" ) ;
c7067c7b Andy McCall 16 days ago
1613
let ( st, _ ) = web ( " POST" , format! ( " /andy/demo/settings/runners/{} /remove" , mine.id) , Some ( format! ( " _csrf={csrf} " ) ) ) .await;
1614
assert_eq! ( st, StatusCode:: SEE_OTHER , " back to the Runners page" ) ;
1615
let ( _ , page) = web ( " GET" , " /andy/demo/settings/runners?notice=x" .into ( ) , None ) .await;
1616
assert! ( ! page.contains ( " <strong>mine</strong>" ) , " {page}" ) ;
7974adb0 Andy McCall 1 month ago ↶
1617
assert! ( db:: runners:: get( & h.app.db, mine.id) .await.unwrap ( ) .is_none ( ) ) ;
1618
1619
1620
let ( _ , page) = web ( " GET" , " /orgs/new" .into ( ) , None ) .await;
1621
web ( " POST" , " /orgs/new" .into ( ) , Some ( format! ( " _csrf={} &name=acme&display_name=Acme" , csrf_of ( & page) ) ) ) .await;
1622
let ( st, page) = web ( " GET" , " /orgs/acme/settings" .into ( ) , None ) .await;
1623
assert_eq! ( st, StatusCode:: OK ) ;
1624
let ( _ , page) = web ( " POST" , " /orgs/acme/settings/runners/token" .into ( ) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1625
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
1626
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
1627
let identity = SecretKey:: generate( ) .identity ( " acme-runner" , " " , 1 ) ;
1628
let reg = serde_json:: json! ( { " token" : token, " name" : " acme-runner" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1629
assert_eq! ( call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await.0 , StatusCode:: OK ) ;
1630
let acme = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " acme-runner" ) .unwrap ( ) ;
1631
assert_eq! ( acme.scope ( ) , db:: runners:: Scope { owner: " acme" .into ( ) , repo_id: None } ) ;
1632
let ( _ , page) = web ( " GET" , " /orgs/acme/settings" .into ( ) , None ) .await;
1633
assert! ( page.contains ( " <strong>acme-runner</strong>" ) & & page.contains ( " the organisation's repositories" ) , " {page}" ) ;
1634
let ( _ , page) = web ( " POST" , format! ( " /orgs/acme/settings/runners/{} /remove" , acme.id) , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
1635
assert! ( page.contains ( " acme-runner removed" ) , " {page}" ) ;
1636
}
c4c87f48 Andy McCall 1 month ago ↶
1637
1638
1639
1640
# [ tokio ::test ( flavor = " multi_thread" ) ]
1641
async fn jobs_that_ask_for_labels_wait_for_a_runner_with_them ( ) {
1642
use oxy_core:: identity:: SecretKey;
1643
let h = hub ( ) .await;
1644
create ( & h.app, " andy" , " demo" ) .await;
1645
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1646
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
1647
let cross = " job \" cross\" {\n sandbox \" alpine\" \n runs-on \" riscv64\" \n exec \" true\" \n }\n " ;
1648
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( cross) ) ;
1649
c.snapshot ( " t" ) .unwrap ( ) ;
1650
c.commit ( " pipeline" , & [ ] ) .unwrap ( ) ;
1651
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1652
assert! ( ! runs_of ( & h.app, " andy" , " demo" ) .await.is_empty ( ) ) ;
1653
1654
let mut taken = Vec :: new( ) ;
1655
while let Some ( j) = claim ( & h.app) .await {
1656
taken.push ( j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
1657
}
1658
assert! ( ! taken.contains ( & " cross" .to_string ( ) ) & & taken.contains ( & " lint" .to_string ( ) ) , " {taken:?}" ) ;
1659
let queue = db:: flow:: queue( & h.app.db) .await.unwrap ( ) ;
1660
let waiting = queue.iter ( ) .find ( | q | q.job = = " cross" ) .unwrap ( ) ;
1661
assert_eq! ( ( waiting.status.as_str ( ) , waiting.needs ( ) ) , ( " queued" , vec! [ " riscv64" .to_string ( ) ] ) ) ;
1662
1663
let token = " flowreg_riscv" ;
1664
db:: runners:: add_registration( & h.app.db, & blake3:: hash( token.as_bytes ( ) ) .to_hex ( ) , " admin" , & Default :: default( ) ) .await.unwrap ( ) ;
1665
let identity = SecretKey:: generate( ) .identity ( " visionfive" , " " , 1 ) ;
1666
let info = serde_json:: json! ( { " arch" : " riscv64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ " riscv64" ] , " max_jobs" : 1 } ) ;
1667
let reg = serde_json:: json! ( { " token" : token, " name" : " visionfive" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
1668
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
1669
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1670
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1671
let claim_body = serde_json:: json! ( { " runner" : " x" , " runtimes" : [ " microvm" ] , " info" : info } ) .to_string ( ) ;
1672
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( & credential) , Some ( claim_body) ) .await;
1673
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1674
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job" ] [ " name" ] , " cross" ) ;
1675
}
070a7b64 Andy McCall 1 month ago ↶
1676
1677
1678
1679
1680
1681
# [ tokio ::test ( flavor = " multi_thread" ) ]
1682
async fn a_job_no_runner_reports_is_ended_after_the_time_limit ( ) {
1683
let h = hub ( ) .await;
1684
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1685
create ( & h.app, " andy" , " lost" ) .await;
1686
let url = format! ( " {} /andy/lost" , h.base) ;
1687
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1688
let c = client ( & url, work.path ( ) ) ;
1689
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1690
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1691
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1692
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1693
assert_eq! ( runs_of ( & h.app, " andy" , " lost" ) .await.len ( ) , 1 ) ;
1694
let job = claim ( & h.app) .await.expect ( " a job to claim" ) ;
1695
let id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1696
1697
1698
assert_eq! ( oxygenhub:: flow:: reap_lost_jobs( & h.app) .await.unwrap ( ) , 0 ) ;
1699
1700
let old = db:: now_ms( ) - 24 * 3600 * 1000 ;
3b1bc27f Andy McCall 20 days ago
1701
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 ↶
1702
assert_eq! ( oxygenhub:: flow:: reap_lost_jobs( & h.app) .await.unwrap ( ) , 1 ) ;
1703
let j = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
1704
assert_eq! ( j.status, " error" ) ;
1705
assert! ( j.error.starts_with ( " lost:" ) , " {}" , j.error) ;
1706
let run = & runs_of ( & h.app, " andy" , " lost" ) .await[ 0 ] ;
1707
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
1708
assert! ( jobs.iter ( ) .all ( | j | j.status ! = " running" ) , " nothing left running" ) ;
1709
1710
assert_ne! ( finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 } ) ) .await, StatusCode:: NO_CONTENT ) ;
1711
assert_eq! ( db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .status, " error" ) ;
1712
}
b0612e7d Andy McCall 1 month ago ↶
1713
1714
1715
1716
# [ tokio ::test ( flavor = " multi_thread" ) ]
1717
async fn a_runner_is_offered_only_jobs_whose_image_it_holds ( ) {
1718
let h = hub ( ) .await;
1719
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1720
create ( & h.app, " andy" , " images" ) .await;
1721
let url = format! ( " {} /andy/images" , h.base) ;
1722
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1723
let c = client ( & url, work.path ( ) ) ;
1724
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1725
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1726
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1727
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1728
assert_eq! ( runs_of ( & h.app, " andy" , " images" ) .await.len ( ) , 1 ) ;
1729
let claim_with = | images : Vec< String> | {
1730
let body = serde_json:: json! ( { " runner" : " test-1" , " runtimes" : [ " microvm" ] , " images" : images } ) .to_string ( ) ;
1731
let app = h.app.clone ( ) ;
1732
async move {
1733
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body) ) .await;
1734
( st, body)
1735
}
1736
} ;
1737
let ( st, _ ) = claim_with ( vec! [ " ab" .repeat ( 32 ) ] ) .await;
1738
assert_eq! ( st, StatusCode:: NO_CONTENT , " no job for a runner without the image" ) ;
1739
let jobs = db:: flow:: jobs( & h.app.db, runs_of ( & h.app, " andy" , " images" ) .await[ 0 ] .id) .await.unwrap ( ) ;
1740
let rootfs: serde_json:: Value = serde_json:: from_str( & jobs[ 0 ] .sandbox) .unwrap ( ) ;
1741
let rootfs = rootfs[ " rootfs" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
1742
let ( st, body) = claim_with ( vec! [ " ab" .repeat ( 32 ) , rootfs] ) .await;
1743
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
1744
}
eef677c4 Andy McCall 1 month ago ↶
1745
1746
1747
1748
1749
# [ tokio ::test ( flavor = " multi_thread" ) ]
1750
async fn a_job_its_runner_no_longer_holds_is_ended_at_its_next_claim ( ) {
1751
let h = hub ( ) .await;
1752
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1753
create ( & h.app, " andy" , " held" ) .await;
1754
let url = format! ( " {} /andy/held" , h.base) ;
1755
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1756
let c = client ( & url, work.path ( ) ) ;
1757
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
1758
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1759
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
1760
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1761
assert_eq! ( runs_of ( & h.app, " andy" , " held" ) .await.len ( ) , 1 ) ;
1762
let id = claim ( & h.app) .await.expect ( " a job to claim" ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
1763
let claim_holding = | holding : Vec< i64 > | {
1764
let body = serde_json:: json! ( { " runner" : " test-1" , " runtimes" : [ " microvm" ] , " holding" : holding } ) .to_string ( ) ;
1765
let app = h.app.clone ( ) ;
1766
async move { call ( & app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body) ) .await.0 }
1767
} ;
1768
claim_holding ( vec! [ id] ) .await;
1769
assert_eq! ( db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .status, " running" , " a held job stays" ) ;
1770
claim_holding ( vec! [ ] ) .await;
1771
let j = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
1772
assert_eq! ( j.status, " error" ) ;
1773
assert! ( j.error.starts_with ( " lost:" ) & & j.error.contains ( " no longer holds" ) , " {}" , j.error) ;
1774
let run = & runs_of ( & h.app, " andy" , " held" ) .await[ 0 ] ;
1775
assert! ( db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) .iter ( ) .all ( | j | j.status ! = " running" ) , " the run settles" ) ;
1776
}
d7ce41ac Andy McCall 1 month ago ↶
1777
1778
1779
1780
1781
# [ tokio ::test ( flavor = " multi_thread" ) ]
1782
async fn a_scoped_job_runs_only_when_a_push_touches_its_paths ( ) {
1783
let h = hub ( ) .await;
1784
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1785
create ( & h.app, " andy" , " scoped" ) .await;
1786
let url = format! ( " {} /andy/scoped" , h.base) ;
1787
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1788
let c = client ( & url, work.path ( ) ) ;
1789
let extra = " job \" dist\" {\n sandbox \" alpine\" \n scope \" Cargo.toml\" \" packaging/**\" \n exec \" true\" \n }\n " ;
1790
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
1791
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1792
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.0\" \n " ) ;
1793
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
1794
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1795
let names = | app : Shared, n : usize | async move {
1796
let mut runs = Vec :: new( ) ;
1797
for _ in 0 .. 100 {
1798
runs = runs_of ( & app, " andy" , " scoped" ) .await;
1799
if runs.len ( ) > = n {
1800
break ;
1801
}
1802
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
1803
}
1804
assert_eq! ( runs.len ( ) , n, " runs" ) ;
1805
let mut j: Vec< String> = db:: flow:: jobs( & app.db, runs[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
1806
j.sort ( ) ;
745d225d Andy McCall 1 month ago ↶
1807
3b1bc27f Andy McCall 20 days ago
1808
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 ↶
1809
j
1810
} ;
1811
assert_eq! ( names ( h.app.clone ( ) , 1 ) .await, [ " build" , " dist" , " lint" , " test" ] , " the first run has every job" ) ;
1812
write ( work.path ( ) , " test.sh" , " echo still ok\n " ) ;
1813
c.commit ( " Edit the test" , & [ ] ) .unwrap ( ) ;
1814
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1815
assert_eq! ( names ( h.app.clone ( ) , 2 ) .await, [ " build" , " lint" , " test" ] , " dist is out of scope" ) ;
1816
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.1\" \n " ) ;
1817
c.commit ( " Release 0.1.1" , & [ ] ) .unwrap ( ) ;
1818
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1819
assert_eq! ( names ( h.app.clone ( ) , 3 ) .await, [ " build" , " dist" , " lint" , " test" ] , " a version bump runs dist" ) ;
1820
}
5dead1f4 Andy McCall 1 month ago ↶
1821
367f34b8 Andy McCall 16 days ago ↶
1822
1823
1824
1825
# [ tokio ::test ( flavor = " multi_thread" ) ]
1826
async fn a_push_can_skip_its_run ( ) {
1827
let h = hub ( ) .await;
1828
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1829
create ( & h.app, " andy" , " skip" ) .await;
1830
let url = format! ( " {} /andy/skip" , h.base) ;
1831
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1832
let c = client ( & url, work.path ( ) ) ;
1833
let extra = " job \" dist\" {\n sandbox \" alpine\" \n scope \" Cargo.toml\" \n exec \" true\" \n }\n " ;
1834
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
1835
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1836
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.0\" \n " ) ;
1837
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
1838
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1839
let newest = | app : Shared, n : usize | async move {
1840
for _ in 0 .. 100 {
1841
let runs = runs_of ( & app, " andy" , " skip" ) .await;
1842
if runs.len ( ) > = n {
1843
assert_eq! ( runs.len ( ) , n, " runs" ) ;
1844
let r = runs[ 0 ] .clone ( ) ;
1845
let mut jobs: Vec< String> = db:: flow:: jobs( & app.db, r.id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
1846
jobs.sort ( ) ;
1847
if r.status ! = " skipped" {
1848
sqlx:: query( " UPDATE flow_runs SET status = 'passed' WHERE id = $1" ) .bind ( r.id) .execute ( & app.db) .await.unwrap ( ) ;
1849
}
1850
return ( r, jobs) ;
1851
}
1852
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
1853
}
1854
panic! ( " no run {n}" ) ;
1855
} ;
1856
assert_eq! ( newest ( h.app.clone ( ) , 1 ) .await.1 , [ " build" , " dist" , " lint" , " test" ] ) ;
1857
1858
1859
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.1\" \n " ) ;
1860
c.commit ( " Bump the version [Skip CI]" , & [ ] ) .unwrap ( ) ;
1861
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1862
let ( run, jobs) = newest ( h.app.clone ( ) , 2 ) .await;
1863
assert_eq! ( run.status, " skipped" ) ;
1864
assert! ( jobs.is_empty ( ) , " {jobs:?}" ) ;
1865
assert! ( run.error.starts_with ( " [skip ci] in change " ) & & run.error.contains ( " , pushed by " ) , " {}" , run.error) ;
1866
assert! ( run.finished_ms.is_some ( ) ) ;
1867
let ( _ , page) = call ( & h.app, " GET" , " /andy/skip/flow/2" , None , None ) .await;
1868
assert! ( page.contains ( " the push asked to skip this run" ) , " {page}" ) ;
1869
1870
1871
1872
write ( work.path ( ) , " test.sh" , " echo still ok\n " ) ;
1873
c.commit ( " Edit the test" , & [ ] ) .unwrap ( ) ;
1874
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1875
assert_eq! ( newest ( h.app.clone ( ) , 3 ) .await.1 , [ " build" , " dist" , " lint" , " test" ] , " the skipped bump still runs dist" ) ;
1876
1877
1878
let row = db:: repos:: get( & h.app.db, " andy" , " skip" ) .await.unwrap ( ) .unwrap ( ) ;
1879
db:: repos:: set_flow_skip( & h.app.db, row.id, " others" ) .await.unwrap ( ) ;
1880
write ( work.path ( ) , " test.sh" , " echo ok again\n " ) ;
1881
c.commit ( " Docs only [skip flow]" , & [ ] ) .unwrap ( ) ;
1882
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1883
let ( run, jobs) = newest ( h.app.clone ( ) , 4 ) .await;
1884
assert_ne! ( run.status, " skipped" ) ;
1885
assert_eq! ( jobs, [ " build" , " lint" , " test" ] ) ;
1886
let row = db:: repos:: get( & h.app.db, " andy" , " skip" ) .await.unwrap ( ) .unwrap ( ) ;
1887
assert! ( oxygenhub:: flow:: skip_allowed( & row, " feature" ) & & ! oxygenhub:: flow:: skip_allowed( & row, " main" ) ) ;
1888
db:: repos:: set_flow_skip( & h.app.db, row.id, " never" ) .await.unwrap ( ) ;
1889
let row = db:: repos:: get( & h.app.db, " andy" , " skip" ) .await.unwrap ( ) .unwrap ( ) ;
1890
assert! ( ! oxygenhub:: flow:: skip_allowed( & row, " feature" ) ) ;
1891
}
1892
5588bb36 Andy McCall 15 days ago ↶
1893
1894
1895
1896
# [ tokio ::test ( flavor = " multi_thread" ) ]
1897
async fn a_push_reuses_a_pass_on_the_same_state ( ) {
1898
let h = hub ( ) .await;
1899
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1900
create ( & h.app, " andy" , " reuse" ) .await;
1901
let url = format! ( " {} /andy/reuse" , h.base) ;
1902
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1903
let c = client ( & url, work.path ( ) ) ;
1904
let gated = " job \" deploy\" {\n sandbox \" alpine\" \n depends-on \" test\" \n approval {\n by \" maintain\" \n }\n exec \" true\" \n }\n " ;
1905
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( gated) ) ;
1906
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1907
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
1908
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1909
let first = runs_of ( & h.app, " andy" , " reuse" ) .await.remove ( 0 ) ;
1910
1911
sqlx:: query( " UPDATE flow_jobs SET status = 'passed', exit_code = 0, receipt_hash = 'r-' || name WHERE run_id = $1" )
1912
.bind ( first.id)
1913
.execute ( & h.app.db)
1914
.await
1915
.unwrap ( ) ;
1916
sqlx:: query( " UPDATE flow_runs SET status = 'passed' WHERE id = $1" ) .bind ( first.id) .execute ( & h.app.db) .await.unwrap ( ) ;
1917
let heads: Vec< oxy_core:: hash:: ChangeHash> = first.head_list ( ) .into_iter ( ) .filter_map ( oxy_core:: hash:: ChangeHash:: from_hex) .collect ( ) ;
1918
let row = db:: repos:: get( & h.app.db, " andy" , " reuse" ) .await.unwrap ( ) .unwrap ( ) ;
1919
assert! ( row.flow_reuse, " on by default" ) ;
1920
let again = | row : db:: repos:: RepoRow| {
1921
let ( app, state, heads) = ( h.app.clone ( ) , first.state.clone ( ) , heads.clone ( ) ) ;
1922
async move {
1923
let n =
1924
oxygenhub:: flow:: start_run_scoped( & app, & row, " main" , & state, & heads, " andy" , " push" , None ) .await.unwrap ( ) .expect ( " a run" ) ;
1925
let run = db:: flow:: run( & app.db, row.id, n) .await.unwrap ( ) .unwrap ( ) ;
1926
let jobs = db:: flow:: jobs( & app.db, run.id) .await.unwrap ( ) ;
1927
( run, jobs)
1928
}
1929
} ;
1930
let ( run, jobs) = again ( row.clone ( ) ) .await;
1931
let by = | name : & str | jobs.iter ( ) .find ( | j | j.name = = name ) .unwrap ( ) .clone ( ) ;
1932
for name in [ " build" , " test" , " lint" ] {
1933
let j = by ( name) ;
1934
assert_eq! ( ( j.status.as_str ( ) , j.reused_from ( ) ) , ( " passed" , Some ( first.number) ) , " {name}: {j:?}" ) ;
1935
assert_eq! ( j.receipt_hash, format! ( " r-{name} " ) , " the pass's receipt stands for it" ) ;
1936
}
1937
let deploy = by ( " deploy" ) ;
1938
assert! ( deploy.status = = " queued" & & deploy.reused_from ( ) .is_none ( ) , " a gated job runs again: {deploy:?}" ) ;
1939
assert_eq! ( run.status, " queued" , " the run waits for the gated job" ) ;
1940
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/reuse/flow/{} " , run.number) , None , None ) .await;
1941
assert! ( page.contains ( " data-reused" ) & & page.contains ( & format! ( " /andy/reuse/flow/{} " , first.number) ) , " {page}" ) ;
1942
1943
let checks = db:: hooks:: checks_for_state( & h.app.db, row.id, & first.state) .await.unwrap ( ) ;
1944
assert! (
1945
checks.iter ( ) .any ( | c | c.name = = " flow/build" & & c.status = = " passed" & & c.summary.contains ( " reused" ) ) ,
1946
" a reused pass reports its check: {checks:?}"
1947
) ;
1948
1949
1950
db:: repos:: set_flow_reuse( & h.app.db, row.id, false ) .await.unwrap ( ) ;
1951
let row = db:: repos:: get( & h.app.db, " andy" , " reuse" ) .await.unwrap ( ) .unwrap ( ) ;
1952
let ( _ , jobs) = again ( row) .await;
1953
assert! ( jobs.iter ( ) .all ( | j | j.status = = " queued" & & j.reused_from ( ) .is_none ( ) ) , " {jobs:?}" ) ;
1954
}
1955
367f34b8 Andy McCall 16 days ago ↶
1956
# [ test ]
1957
fn skip_markers_are_the_forges_spellings_in_any_case ( ) {
1958
use oxygenhub:: flow:: skip_marker;
1959
assert_eq! ( skip_marker ( " Fix typo [skip flow]" ) , Some ( " [skip flow]" ) ) ;
1960
assert_eq! ( skip_marker ( " [CI SKIP] docs" ) , Some ( " [ci skip]" ) ) ;
1961
assert_eq! ( skip_marker ( " Docs\n \n [Skip CI]" ) , Some ( " [skip ci]" ) ) ;
1962
assert_eq! ( skip_marker ( " skip ci without brackets" ) , None ) ;
3b3a51af Andy McCall 15 days ago ↶
1963
1964
assert_eq! ( skip_marker ( " Document the `[skip flow]` marker" ) , None ) ;
1965
assert_eq! ( skip_marker ( " Example:\n \n ```\n git commit -m 'x [skip ci]'\n ```\n " ) , None ) ;
1966
assert_eq! ( skip_marker ( " `code` then [skip ci]" ) , Some ( " [skip ci]" ) ) ;
1967
assert_eq! ( skip_marker ( " an odd ` backtick [skip ci]" ) , Some ( " [skip ci]" ) ) ;
367f34b8 Andy McCall 16 days ago ↶
1968
}
1969
745d225d Andy McCall 1 month ago ↶
1970
1971
1972
1973
# [ tokio ::test ( flavor = " multi_thread" ) ]
1974
async fn runs_are_cancelled_by_hand_or_by_a_newer_push ( ) {
1975
let h = hub ( ) .await;
1976
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
1977
create ( & h.app, " andy" , " stop" ) .await;
1978
let url = format! ( " {} /andy/stop" , h.base) ;
1979
let work = tempfile:: tempdir( ) .unwrap ( ) ;
1980
let c = client ( & url, work.path ( ) ) ;
1981
let extra = " job \" dist\" {\n sandbox \" alpine\" \n scope \" Cargo.toml\" \n exec \" true\" \n }\n " ;
1982
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
1983
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
1984
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.0\" \n " ) ;
1985
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
1986
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
1987
let wait_runs = | app : Shared, n : usize | async move {
1988
for _ in 0 .. 100 {
1989
let runs = runs_of ( & app, " andy" , " stop" ) .await;
1990
if runs.len ( ) > = n {
1991
return runs;
1992
}
1993
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
1994
}
1995
panic! ( " no run {n}" ) ;
1996
} ;
1997
let first = wait_runs ( h.app.clone ( ) , 1 ) .await;
3b1bc27f Andy McCall 20 days ago
1998
sqlx:: query( " UPDATE flow_runs SET status = 'passed' WHERE id = $1" ) .bind ( first[ 0 ] .id) .execute ( & h.app.db) .await.unwrap ( ) ;
1999
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 ↶
2000
2001
2002
write ( work.path ( ) , " Cargo.toml" , " version = \" 0.1.1\" \n " ) ;
2003
c.commit ( " Release 0.1.1" , & [ ] ) .unwrap ( ) ;
2004
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2005
wait_runs ( h.app.clone ( ) , 2 ) .await;
2006
let job = claim ( & h.app) .await.expect ( " a job of run 2" ) ;
2007
let id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2008
write ( work.path ( ) , " test.sh" , " echo still ok\n " ) ;
2009
c.commit ( " Edit the test" , & [ ] ) .unwrap ( ) ;
2010
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2011
let runs = wait_runs ( h.app.clone ( ) , 3 ) .await;
2012
let third = & runs[ 0 ] ;
2013
let mut second = runs[ 1 ] .clone ( ) ;
2014
for _ in 0 .. 100 {
2015
second = db:: flow:: run_by_id( & h.app.db, runs[ 1 ] .id) .await.unwrap ( ) .unwrap ( ) ;
2016
if ! second.live ( ) {
2017
break ;
2018
}
2019
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2020
}
2021
assert_eq! ( second.status, " cancelled" , " a newer push supersedes a run still going" ) ;
2022
let jobs = db:: flow:: jobs( & h.app.db, second.id) .await.unwrap ( ) ;
2023
assert! ( jobs.iter ( ) .all ( | j | j.status = = " cancelled" & & j.error = = " superseded by run #3" ) , " {jobs:?}" ) ;
2024
let mut names: Vec< String> = db:: flow:: jobs( & h.app.db, third.id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
2025
names.sort ( ) ;
2026
assert_eq! ( names, [ " build" , " dist" , " lint" , " test" ] , " run 3 also checks what run 2 would have: the version bump" ) ;
2027
let mut checks = vec! [ ] ;
2028
for _ in 0 .. 100 {
2029
checks = db:: hooks:: checks_for_state( & h.app.db, second.repo_id, & second.state) .await.unwrap ( ) ;
2030
if checks.iter ( ) .all ( | c | c.status = = " cancelled" ) {
2031
break ;
2032
}
2033
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2034
}
2035
assert! ( checks.iter ( ) .all ( | c | c.status = = " cancelled" ) , " {checks:?}" ) ;
2036
2037
let ( st, _ ) =
2038
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( r # "{"records":[]}"# .into ( ) ) ) .await;
2039
assert_eq! ( st, StatusCode:: CONFLICT ) ;
2040
2041
2042
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/stop/flow/runs/3/cancel" , Some ( " site" ) , None ) .await;
2043
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
2044
let third = db:: flow:: run_by_id( & h.app.db, third.id) .await.unwrap ( ) .unwrap ( ) ;
2045
assert_eq! ( third.status, " cancelled" ) ;
2046
assert! ( db:: flow:: jobs( & h.app.db, third.id) .await.unwrap ( ) .iter ( ) .all ( | j | j.error = = " cancelled by andy" ) ) ;
2047
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/stop/flow/runs/3/cancel" , Some ( " site" ) , None ) .await;
2048
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " already cancelled" ) , " {st} {body}" ) ;
2049
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/stop/flow/runs/3/cancel" , None , None ) .await;
2050
assert_ne! ( st, StatusCode:: OK , " anonymous callers cannot cancel" ) ;
2051
let ( _ , page) = call ( & h.app, " GET" , " /andy/stop/flow/3" , None , None ) .await;
2052
assert! ( page.contains ( " ⊘" ) & & ! page.contains ( " CANCEL</button>" ) , " a cancelled run shows so, with no button" ) ;
2053
}
2054
5dead1f4 Andy McCall 1 month ago ↶
2055
2056
2057
2058
# [ tokio ::test ( flavor = " multi_thread" ) ]
2059
async fn a_job_runs_under_its_own_time_limit_within_the_sites ( ) {
2060
let h = hub ( ) .await;
2061
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2062
db:: accounts:: set_setting( & h.app.db, oxygenhub:: flow:: SETTING_DEFAULT , " 30" ) .await.unwrap ( ) ;
2063
db:: accounts:: set_setting( & h.app.db, oxygenhub:: flow:: SETTING_MAX , " 120" ) .await.unwrap ( ) ;
2064
create ( & h.app, " andy" , " limits" ) .await;
2065
let url = format! ( " {} /andy/limits" , h.base) ;
2066
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2067
let c = client ( & url, work.path ( ) ) ;
2068
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 " ;
2069
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
2070
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2071
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2072
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2073
assert_eq! ( runs_of ( & h.app, " andy" , " limits" ) .await.len ( ) , 1 ) ;
2074
let mut limits = std:: collections:: HashMap:: new( ) ;
2075
let mut ids = std:: collections:: HashMap:: new( ) ;
2076
while let Some ( j) = claim ( & h.app) .await {
2077
let name = j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
2078
limits.insert ( name.clone ( ) , j[ " timeout_s" ] .as_u64 ( ) .unwrap ( ) ) ;
2079
ids.insert ( name, j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ) ;
2080
}
2081
assert_eq! ( limits[ " slow" ] , 5400 , " its own" ) ;
2082
assert_eq! ( limits[ " huge" ] , 7200 , " cut to the site's maximum" ) ;
2083
assert_eq! ( limits[ " build" ] , 1800 , " the site's default" ) ;
2084
2085
let started = db:: now_ms( ) - 40 * 60 * 1000 ;
3b1bc27f Andy McCall 20 days ago
2086
sqlx:: query( " UPDATE flow_jobs SET started_ms = $1" ) .bind ( started) .execute ( & h.app.db) .await.unwrap ( ) ;
5dead1f4 Andy McCall 1 month ago ↶
2087
oxygenhub:: flow:: reap_lost_jobs( & h.app) .await.unwrap ( ) ;
2088
let status = | id : i64 | {
2089
let app = h.app.clone ( ) ;
2090
async move { db:: flow:: job( & app.db, id) .await.unwrap ( ) .unwrap ( ) .status }
2091
} ;
2092
assert_eq! ( status ( ids[ " build" ] ) .await, " error" , " past its 30 minutes" ) ;
2093
assert_eq! ( status ( ids[ " slow" ] ) .await, " running" , " within its 90" ) ;
2094
assert_eq! ( status ( ids[ " huge" ] ) .await, " running" , " within its 120" ) ;
2095
}
c889911b Andy McCall 1 month ago ↶
2096
2097
2098
2099
2100
# [ tokio ::test ( flavor = " multi_thread" ) ]
2101
async fn on_decides_which_events_start_a_job ( ) {
2102
let h = hub ( ) .await;
2103
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2104
create ( & h.app, " andy" , " events" ) .await;
2105
let url = format! ( " {} /andy/events" , h.base) ;
2106
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2107
let c = client ( & url, work.path ( ) ) ;
2108
let text = format! (
2109
" on {{ \n push \" main\" \n manual\n }} \n {} job \" dist\" {{ \n sandbox \" alpine\" \n on {{ \n tag \" v*\" \n }} \n exec \" true\" \n }} \n " ,
2110
pipeline ( " " )
2111
) ;
2112
2113
let text = text.replacen (
2114
" on {\n push \" main\" \n manual\n }\n schema-version 1\n " ,
2115
" schema-version 1\n on {\n push \" main\" \n manual\n }\n " ,
2116
1 ,
2117
) ;
2118
write ( work.path ( ) , " .flow/pipeline.kdl" , & text) ;
2119
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2120
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
2121
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2122
let jobs_of_latest = | app : Shared, n : usize | async move {
2123
let mut runs = Vec :: new( ) ;
2124
for _ in 0 .. 100 {
2125
runs = runs_of ( & app, " andy" , " events" ) .await;
2126
if runs.len ( ) > = n {
2127
break ;
2128
}
2129
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2130
}
2131
assert_eq! ( runs.len ( ) , n, " runs" ) ;
2132
let mut j: Vec< String> = db:: flow:: jobs( & app.db, runs[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
2133
j.sort ( ) ;
2134
( runs[ 0 ] .clone ( ) , j)
2135
} ;
2136
let ( _ , j) = jobs_of_latest ( h.app.clone ( ) , 1 ) .await;
2137
assert_eq! ( j, [ " build" , " lint" , " test" ] , " a push to main: no dist" ) ;
2138
2139
let heads = c.bookmark_heads ( ) .unwrap ( ) ;
2140
c.tag_create ( " v1.0.0" , & heads, " first" ) .unwrap ( ) ;
2141
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2142
let ( run, j) = jobs_of_latest ( h.app.clone ( ) , 2 ) .await;
2143
assert_eq! ( j, [ " dist" ] , " a tag runs only what names it" ) ;
2144
assert_eq! ( run.trigger, " tag:v1.0.0" ) ;
2145
2146
let row = db:: repos:: get( & h.app.db, " andy" , " events" ) .await.unwrap ( ) .unwrap ( ) ;
2147
db:: flow:: finish(
2148
& h.app.db,
2149
db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) [ 0 ] .id,
2150
db:: flow:: Outcome { status: " passed" , exit_code: Some ( 0 ) , error: " " , receipt: " " , receipt_hash: " " } ,
2151
)
2152
.await
2153
.unwrap ( ) ;
2154
let run = db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) ;
2155
oxygenhub:: flow:: rerun( & h.app, & row, & run, " andy" ) .await.unwrap ( ) ;
2156
let ( _ , j) = jobs_of_latest ( h.app.clone ( ) , 3 ) .await;
2157
assert_eq! ( j, [ " dist" ] , " the rerun is the tag's run again" ) ;
2158
2159
c.bookmark_create ( " develop" , None ) .unwrap ( ) ;
2160
assert! ( c.push_bookmark ( " origin" , " develop" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2161
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
2162
assert_eq! ( runs_of ( & h.app, " andy" , " events" ) .await.len ( ) , 3 , " no run for develop" ) ;
348f8c4b Andy McCall 1 month ago ↶
2163
}
2164
2165
2166
2167
# [ tokio ::test ( flavor = " multi_thread" ) ]
2168
async fn scheduled_jobs_run_when_due ( ) {
2169
let h = hub ( ) .await;
2170
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2171
create ( & h.app, " andy" , " nightly" ) .await;
2172
let url = format! ( " {} /andy/nightly" , h.base) ;
2173
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2174
let c = client ( & url, work.path ( ) ) ;
2175
2176
let extra = " job \" nightly\" {\n sandbox \" alpine\" \n on {\n schedule \" 17 4 29 2 *\" \n }\n exec \" true\" \n }\n " ;
2177
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
2178
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2179
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
2180
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2181
assert_eq! ( runs_of ( & h.app, " andy" , " nightly" ) .await.len ( ) , 1 ) ;
2182
let first = runs_of ( & h.app, " andy" , " nightly" ) .await;
2183
let names: Vec< String> = db:: flow:: jobs( & h.app.db, first[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
2184
assert! ( ! names.contains ( & " nightly" .to_string ( ) ) , " a push does not run a scheduled job" ) ;
2185
use flow_core:: pipeline:: When;
2186
let not_yet = When { minute: 16 , hour: 4 , day: 29 , month: 2 , weekday: 4 } ;
2187
assert_eq! ( oxygenhub:: flow:: run_due( & h.app, not_yet) .await, 0 ) ;
2188
let due = When { minute: 17 , .. not_yet } ;
2189
assert_eq! ( oxygenhub:: flow:: run_due( & h.app, due) .await, 1 ) ;
2190
let runs = runs_of ( & h.app, " andy" , " nightly" ) .await;
2191
assert_eq! ( runs.len ( ) , 2 ) ;
2192
assert_eq! ( runs[ 0 ] .trigger, " schedule" ) ;
2193
let names: Vec< String> = db:: flow:: jobs( & h.app.db, runs[ 0 ] .id) .await.unwrap ( ) .into_iter ( ) .map ( | j | j.name ) .collect ( ) ;
2194
assert_eq! ( names, [ " nightly" ] ) ;
2195
assert_eq! ( runs[ 0 ] .bookmark, " main" , " on the default bookmark" ) ;
c889911b Andy McCall 1 month ago ↶
2196
}
2a83f530 Andy McCall 1 month ago ↶
2197
2198
# [ tokio ::test ( flavor = " multi_thread" ) ]
2199
async fn variables_from_three_levels_reach_a_job_inner_overriding_outer ( ) {
2200
use oxygenhub:: levels:: Scope;
2201
use oxygenhub:: variables;
2202
let h = hub ( ) .await;
2203
let app = & h.app;
2204
2205
let res = oxygenhub:: router( app.clone ( ) )
2206
.oneshot (
2207
Request:: post( " /register" )
2208
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 20 days ago
2209
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
2a83f530 Andy McCall 1 month ago ↶
2210
.unwrap ( ) ,
2211
)
2212
.await
2213
.unwrap ( ) ;
2214
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
2215
let andy = db:: accounts:: account_by_name( & app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
2216
db:: accounts:: create_org( & app.db, " acme" , " Acme" , andy.id) .await.unwrap ( ) ;
2217
create ( app, " acme" , " app" ) .await;
2218
let page = | path : & str , form : Option< String> | {
2219
let mut req =
2220
Request:: builder( ) .method ( if form.is_some ( ) { " POST" } else { " GET" } ) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
2221
if form.is_some ( ) {
2222
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
2223
}
2224
let req = req.body ( Body:: from( form.unwrap_or_default ( ) ) ) .unwrap ( ) ;
c7067c7b Andy McCall 16 days ago
2225
let ( app, cookie) = ( app.clone ( ) , cookie.clone ( ) ) ;
2a83f530 Andy McCall 1 month ago ↶
2226
async move {
c7067c7b Andy McCall 16 days ago
2227
let mut res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
2228
2229
let back =
2230
res.headers ( ) .get ( header:: LOCATION ) .and_then ( | v | v.to_str ( ) .ok ( ) ) .filter ( | l | l.contains ( " /settings/" ) ) .map ( String :: from) ;
2231
if let Some ( to) = back {
2232
let req = Request:: get( to) .header ( header:: COOKIE , cookie) .body ( Body:: empty( ) ) .unwrap ( ) ;
2233
res = oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) ;
2234
}
2a83f530 Andy McCall 1 month ago ↶
2235
let status = res.status ( ) ;
2236
let body = String :: from_utf8( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ) .unwrap ( ) ;
2237
( status, body)
2238
}
2239
} ;
2240
let csrf_of = | page : & str | {
2241
let i = page.find ( " name=\" _csrf\" value=\" " ) .expect ( " csrf field" ) + " name=\" _csrf\" value=\" " .len ( ) ;
2242
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
2243
} ;
2244
2245
2246
2247
variables:: set( & app.db, Scope:: Site, " CC" , " gcc" ) .await.unwrap ( ) ;
2248
variables:: set( & app.db, Scope:: Site, " LANG" , " C" ) .await.unwrap ( ) ;
2249
let org = db:: accounts:: org_by_name( & app.db, " acme" ) .await.unwrap ( ) .unwrap ( ) ;
2250
variables:: set( & app.db, Scope:: Org( org.id) , " CC" , " clang" ) .await.unwrap ( ) ;
c7067c7b Andy McCall 16 days ago
2251
let ( st, settings) = page ( " /acme/app/settings/variables" , None ) .await;
2a83f530 Andy McCall 1 month ago ↶
2252
assert_eq! ( st, StatusCode:: OK , " {settings}" ) ;
2253
assert! ( settings.contains ( " Inherited" ) & & settings.contains ( " the acme organisation" ) & & settings.contains ( " overridden" ) , " {settings}" ) ;
2254
let csrf = csrf_of ( & settings) ;
2255
let ( _ , body) = page ( " /acme/app/settings/variables" , Some ( format! ( " _csrf={csrf} &action=set&name=RUST_LOG&value=debug" ) ) ) .await;
2256
assert! ( body.contains ( " RUST_LOG set." ) & & body.contains ( " <code>debug</code>" ) , " {body}" ) ;
2257
let ( _ , body) = page ( " /acme/app/settings/variables" , Some ( format! ( " _csrf={csrf} &action=set&name=FLOW_REPO&value=x" ) ) ) .await;
2258
assert! ( body.contains ( " not FLOW_" ) , " {body}" ) ;
2259
let repo = db:: repos:: get( & app.db, " acme" , " app" ) .await.unwrap ( ) .unwrap ( ) ;
2260
db:: repos:: set_flow( & app.db, repo.id, " on" ) .await.unwrap ( ) ;
2261
let env = variables:: env_for_repo( & app.db, & repo) .await.unwrap ( ) ;
2262
assert_eq! (
2263
env.iter ( ) .map ( | ( k , v ) | ( k.as_str ( ) , v.as_str ( ) ) ) .collect:: < Vec< _ > > ( ) ,
2264
[ ( " CC" , " clang" ) , ( " LANG" , " C" ) , ( " RUST_LOG" , " debug" ) ] ,
2265
" the organisation's CC wins over the site's"
2266
) ;
2267
2268
2269
let url = format! ( " {} /acme/app" , h.base) ;
2270
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2271
let c = client ( & url, work.path ( ) ) ;
2272
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
2273
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2274
c.snapshot ( " test" ) .unwrap ( ) ;
2275
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2276
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2277
let runs = runs_of ( app, " acme" , " app" ) .await;
2278
assert_eq! ( runs.len ( ) , 1 , " {runs:?}" ) ;
2279
let got = claim ( app) .await.unwrap_or_else ( | | panic! ( " a job: {:?}" , runs[ 0 ] ) ) ;
2280
assert_eq! ( got[ " env" ] , serde_json:: json! ( { " CC" : " clang" , " LANG" : " C" , " RUST_LOG" : " debug" } ) , " {got}" ) ;
2281
2282
737de0fa Andy McCall 15 days ago ↶
2283
let ( _ , admin) = page ( " /admin/variables" , None ) .await;
2a83f530 Andy McCall 1 month ago ↶
2284
assert! ( admin.contains ( " Variables" ) & & admin.contains ( " <code>gcc</code>" ) , " {admin}" ) ;
2285
let ( _ , body) = page ( " /admin/variables" , Some ( format! ( " _csrf={} &action=delete&name=LANG" , csrf_of ( & admin) ) ) ) .await;
2286
assert! ( body.contains ( " LANG removed." ) , " {body}" ) ;
2287
let got = claim ( app) .await.expect ( " another job" ) ;
2288
assert_eq! ( got[ " env" ] , serde_json:: json! ( { " CC" : " clang" , " RUST_LOG" : " debug" } ) , " {got}" ) ;
2289
}
4ac01a73 Andy McCall 1 month ago ↶
2290
2291
# [ tokio ::test ( flavor = " multi_thread" ) ]
2292
async fn secrets_a_job_declares_come_with_the_claim_and_never_reach_the_log ( ) {
2293
let h = hub ( ) .await;
2294
let app = & h.app;
2295
let res = oxygenhub:: router( app.clone ( ) )
2296
.oneshot (
2297
Request:: post( " /register" )
2298
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 20 days ago
2299
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
4ac01a73 Andy McCall 1 month ago ↶
2300
.unwrap ( ) ,
2301
)
2302
.await
2303
.unwrap ( ) ;
2304
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
2305
let page = | path : & str , form : Option< String> | {
2306
let mut req =
2307
Request:: builder( ) .method ( if form.is_some ( ) { " POST" } else { " GET" } ) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
2308
if form.is_some ( ) {
2309
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
2310
}
2311
let req = req.body ( Body:: from( form.unwrap_or_default ( ) ) ) .unwrap ( ) ;
c7067c7b Andy McCall 16 days ago
2312
let ( app, cookie) = ( app.clone ( ) , cookie.clone ( ) ) ;
4ac01a73 Andy McCall 1 month ago ↶
2313
async move {
c7067c7b Andy McCall 16 days ago
2314
let mut res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
2315
2316
let back =
2317
res.headers ( ) .get ( header:: LOCATION ) .and_then ( | v | v.to_str ( ) .ok ( ) ) .filter ( | l | l.contains ( " /settings/" ) ) .map ( String :: from) ;
2318
if let Some ( to) = back {
2319
let req = Request:: get( to) .header ( header:: COOKIE , cookie) .body ( Body:: empty( ) ) .unwrap ( ) ;
2320
res = oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) ;
2321
}
4ac01a73 Andy McCall 1 month ago ↶
2322
let status = res.status ( ) ;
2323
let body = String :: from_utf8( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ) .unwrap ( ) ;
2324
( status, body)
2325
}
2326
} ;
2327
let csrf_of = | page : & str | {
2328
let i = page.find ( " name=\" _csrf\" value=\" " ) .expect ( " csrf field" ) + " name=\" _csrf\" value=\" " .len ( ) ;
2329
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
2330
} ;
2331
create ( app, " andy" , " demo" ) .await;
2332
2333
2334
let ( _ , admin) = page ( " /admin" , None ) .await;
2335
let csrf = csrf_of ( & admin) ;
2336
let ( _ , body) = page ( " /admin/secrets" , Some ( format! ( " _csrf={csrf} &action=set&name=deploy-token&value=site-level-value" ) ) ) .await;
2337
assert! ( body.contains ( " deploy-token set." ) & & ! body.contains ( " site-level-value" ) , " {body}" ) ;
2338
let ( _ , body) = page ( " /admin/secrets" , Some ( format! ( " _csrf={csrf} &action=set&name=bad+name&value=x" ) ) ) .await;
2339
assert! ( body.contains ( " name is 1 to 64 of" ) , " {body}" ) ;
c7067c7b Andy McCall 16 days ago
2340
let ( _ , settings) = page ( " /andy/demo/settings/secrets" , None ) .await;
4ac01a73 Andy McCall 1 month ago ↶
2341
assert! (
2342
settings.contains ( " Secrets" ) & & settings.contains ( " <code>deploy-token</code>" ) & & settings.contains ( " the site</td>" ) ,
2343
" {settings}"
2344
) ;
2345
let csrf_r = csrf_of ( & settings) ;
2346
let ( _ , body) =
2347
page ( " /andy/demo/settings/secrets" , Some ( format! ( " _csrf={csrf_r} &action=set&name=deploy-token&value=hunter22-repo-value" ) ) ) .await;
2348
assert! ( body.contains ( " deploy-token set." ) & & body.contains ( " overridden" ) & & ! body.contains ( " hunter22" ) , " {body}" ) ;
2349
let repo = db:: repos:: get( & app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
2350
assert! (
2351
! String :: from_utf8_lossy( & oxygenhub:: secrets:: list( & app.db, oxygenhub:: levels:: Scope:: Repo( repo.id) ) .await.unwrap ( ) [ 0 ] .value)
2352
.contains ( " hunter22" ) ,
2353
" sealed at rest"
2354
) ;
2355
2356
2357
let url = format! ( " {} /andy/demo" , h.base) ;
2358
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2359
let c = client ( & url, work.path ( ) ) ;
2360
write (
2361
work.path ( ) ,
2362
" .flow/pipeline.kdl" ,
2363
& 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 " ) ,
2364
) ;
2365
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2366
c.snapshot ( " test" ) .unwrap ( ) ;
2367
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2368
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2369
let runs = runs_of ( app, " andy" , " demo" ) .await;
2370
let jobs = match runs.first ( ) {
2371
Some ( r) => db:: flow:: jobs( & app.db, r.id) .await.unwrap ( ) .iter ( ) .map ( | j | format! ( " {} :{} " , j.name, j.status) ) .collect:: < Vec< _ > > ( ) ,
2372
None => vec! [ ] ,
2373
} ;
2374
let mut deploy = None ;
2375
for _ in 0 .. 50 {
2376
let Some ( got) = claim ( app) .await else {
2377
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
2378
continue ;
2379
} ;
2380
if got[ " job" ] [ " name" ] = = " deploy" {
2381
deploy = Some ( got) ;
2382
break ;
2383
}
2384
assert_eq! ( got[ " secrets" ] , serde_json:: Value:: Null, " {got}" ) ;
2385
}
2386
let got = deploy.unwrap_or_else ( | | panic! ( " the deploy job: runs {runs:?} jobs {jobs:?}" ) ) ;
2387
assert_eq! (
2388
got[ " secrets" ] ,
2389
serde_json:: json! ( [ { " name" : " deploy-token" , " path" : " /run/secrets/token" , " value" : " hunter22-repo-value" } ] ) ,
2390
" the repository's value, the missing one left out: {got}"
2391
) ;
2392
let id = got[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2393
let log = db:: flow:: log( & app.db, id, 0 , 100 ) .await.unwrap ( ) ;
2394
assert! ( log.iter ( ) .any ( | l | l.stream = = " flow" & & l.text.contains ( " secret \" missing\" is not set" ) ) , " {log:?}" ) ;
2395
2396
2397
let records = serde_json:: json! ( { " records" : [
2398
{ " t" : 1 , " stream" : " out" , " text" : " token=hunter22-repo-value" } ,
2399
{ " t" : 2 , " stream" : " err" , " text" : " b64 aHVudGVyMjItcmVwby12YWx1ZQ== end" } ,
2400
] } ) ;
2401
let ( st, _ ) = call ( app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( records.to_string ( ) ) ) .await;
2402
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
2403
let log = db:: flow:: log( & app.db, id, 0 , 100 ) .await.unwrap ( ) ;
2404
let texts: Vec< & str > = log.iter ( ) .map ( | l | l.text.as_str ( ) ) .collect ( ) ;
2405
assert! ( texts.contains ( & " token=***" ) & & texts.contains ( & " b64 *** end" ) , " {texts:?}" ) ;
2406
assert! ( ! texts.iter ( ) .any ( | t | t.contains ( " hunter22" ) ) , " {texts:?}" ) ;
2407
}
d1727c57 Andy McCall 1 month ago ↶
2408
2409
# [ tokio ::test ( flavor = " multi_thread" ) ]
2410
async fn a_job_s_identity_is_a_signed_token_the_discovery_document_lets_anyone_verify ( ) {
2411
let h = hub ( ) .await;
2412
let app = & h.app;
2413
let ( st, disc) = call ( app, " GET" , " /.well-known/openid-configuration" , None , None ) .await;
2414
assert_eq! ( st, StatusCode:: OK , " {disc}" ) ;
2415
let disc: serde_json:: Value = serde_json:: from_str( & disc) .unwrap ( ) ;
2416
assert_eq! ( disc[ " issuer" ] , h.base.as_str ( ) ) ;
2417
assert_eq! ( disc[ " jwks_uri" ] , format! ( " {} /.well-known/jwks.json" , h.base) ) ;
2418
let ( st, jwks) = call ( app, " GET" , " /.well-known/jwks.json" , None , None ) .await;
2419
assert_eq! ( st, StatusCode:: OK , " {jwks}" ) ;
2420
let jwks: serde_json:: Value = serde_json:: from_str( & jwks) .unwrap ( ) ;
2421
assert_eq! ( jwks[ " keys" ] [ 0 ] [ " alg" ] , " RS256" ) ;
2422
let kid = jwks[ " keys" ] [ 0 ] [ " kid" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
2423
2424
create ( app, " andy" , " demo" ) .await;
2425
let url = format! ( " {} /andy/demo" , h.base) ;
2426
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2427
let c = client ( & url, work.path ( ) ) ;
2428
write (
2429
work.path ( ) ,
2430
" .flow/pipeline.kdl" ,
2431
& pipeline ( " job \" deploy\" {\n sandbox \" alpine\" \n identity \" sts.amazonaws.com\" {\n mount-path \" /run/secrets/aws-token\" \n }\n exec \" deploy\" \n }\n " ) ,
2432
) ;
2433
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2434
c.snapshot ( " test" ) .unwrap ( ) ;
2435
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2436
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2437
let mut deploy = None ;
2438
for _ in 0 .. 50 {
2439
let Some ( got) = claim ( app) .await else {
2440
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
2441
continue ;
2442
} ;
2443
if got[ " job" ] [ " name" ] = = " deploy" {
2444
deploy = Some ( got) ;
2445
break ;
2446
}
2447
}
2448
let got = deploy.expect ( " the deploy job" ) ;
2449
let secrets = got[ " secrets" ] .as_array ( ) .expect ( " secrets" ) ;
2450
assert_eq! ( secrets.len ( ) , 1 ) ;
2451
assert_eq! (
2452
( secrets[ 0 ] [ " name" ] .as_str ( ) , secrets[ 0 ] [ " path" ] .as_str ( ) ) ,
2453
( Some ( " identity:sts.amazonaws.com" ) , Some ( " /run/secrets/aws-token" ) )
2454
) ;
2455
let token = secrets[ 0 ] [ " value" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
2456
let issuer = app.oidc ( ) .await.unwrap ( ) ;
2457
assert_eq! ( issuer.kid, kid, " the key the JWKS publishes signed it" ) ;
2458
let claims = issuer.verify ( & token, " sts.amazonaws.com" , db:: now_ms( ) ) .expect ( " verifies" ) ;
2459
assert_eq! ( claims[ " sub" ] , " repo:andy/demo:bookmark:main" ) ;
2460
assert_eq! ( claims[ " repository" ] , " andy/demo" ) ;
2461
assert_eq! ( claims[ " job" ] , " deploy" ) ;
2462
assert_eq! ( claims[ " run_number" ] , 1 ) ;
2463
assert_eq! ( claims[ " runner" ] , " test-1" ) ;
2464
assert_eq! ( claims[ " iss" ] , h.base.as_str ( ) ) ;
2465
assert! ( claims[ " state" ] .as_str ( ) .unwrap ( ) .len ( ) = = 64 ) ;
2466
2467
let id = got[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2468
let records = serde_json:: json! ( { " records" : [ { " t" : 1 , " stream" : " out" , " text" : format! ( " TOKEN={token} end" ) } ] } ) ;
2469
let ( st, _ ) = call ( app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( records.to_string ( ) ) ) .await;
2470
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
2471
let log = db:: flow:: log( & app.db, id, 0 , 100 ) .await.unwrap ( ) ;
2472
assert! ( log.iter ( ) .any ( | l | l.text = = " TOKEN=*** end" ) , " {log:?}" ) ;
2473
}
efd29a00 Andy McCall 1 month ago ↶
2474
2475
# [ tokio ::test ( flavor = " multi_thread" ) ]
2476
async fn jobs_are_offered_only_to_runners_of_their_architecture ( ) {
2477
let h = hub ( ) .await;
2478
let app = & h.app;
2479
create ( app, " andy" , " demo" ) .await;
2480
let url = format! ( " {} /andy/demo" , h.base) ;
2481
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2482
let c = client ( & url, work.path ( ) ) ;
2483
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 ) ) ) ;
2484
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2485
c.snapshot ( " test" ) .unwrap ( ) ;
2486
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2487
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2488
let claim_as = | arch : & 'static str , archs : serde_json:: Value| {
2489
let app = app.clone ( ) ;
2490
async move {
2491
let body = serde_json:: json! ( {
2492
" runner" : format! ( " {arch} -1" ) , " runtimes" : [ " microvm" ] ,
2493
" info" : { " arch" : arch, " os" : " linux" , " version" : " t" , " labels" : [ ] , " max_jobs" : 1 } ,
2494
" image_archs" : archs,
2495
} ) ;
2496
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body.to_string ( ) ) ) .await;
2497
match st {
2498
StatusCode:: NO_CONTENT => None ,
2499
StatusCode:: OK => {
2500
Some ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) )
2501
}
2502
other => panic! ( " claim: {other} {body}" ) ,
2503
}
2504
}
2505
} ;
2506
2507
2508
let rootfs = pipeline ( " " ) .split ( " cas://blob/" ) .nth ( 1 ) .unwrap ( ) [ .. 64 ] .to_string ( ) ;
2509
for _ in 0 .. 50 {
2510
if ! runs_of ( app, " andy" , " demo" ) .await.is_empty ( ) {
2511
break ;
2512
}
2513
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
2514
}
2515
let runs = runs_of ( app, " andy" , " demo" ) .await;
2516
assert_eq! ( runs.len ( ) , 1 , " {runs:?}" ) ;
2517
let mut taken = vec! [ ] ;
2518
while let Some ( j) = claim_as ( " x86_64" , serde_json:: json! ( { rootfs.clone ( ) : " x86_64" } ) ) .await {
2519
taken.push ( j) ;
2520
}
2521
assert_eq! ( taken, [ " build" , " lint" ] , " test waits for build; flash is for riscv64" ) ;
2522
2523
assert_eq! ( claim_as ( " riscv64" , serde_json:: json! ( { } ) ) .await.as_deref ( ) , Some ( " flash" ) ) ;
2524
assert_eq! ( claim_as ( " riscv64" , serde_json:: json! ( { } ) ) .await, None ) ;
2525
let ( _ , queue) = call ( app, " GET" , " /admin/runners" , None , None ) .await;
2526
let _ = queue;
2527
}
4d2e44d7 Andy McCall 1 month ago ↶
2528
2529
2530
2531
2532
2533
# [ tokio ::test ( flavor = " multi_thread" ) ]
2534
async fn a_declined_job_goes_to_another_runner_or_ends_unplaceable ( ) {
2535
let h = hub ( ) .await;
2536
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2537
create ( & h.app, " andy" , " declines" ) .await;
2538
let url = format! ( " {} /andy/declines" , h.base) ;
2539
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2540
let c = client ( & url, work.path ( ) ) ;
2541
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
2542
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2543
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2544
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2545
let run = runs_of ( & h.app, " andy" , " declines" ) .await[ 0 ] .clone ( ) ;
2546
let claim_as = | token : & 'static str , name : & 'static str | {
2547
let app = h.app.clone ( ) ;
2548
async move {
2549
let body = serde_json:: json! ( { " runner" : name, " runtimes" : [ " microvm" ] } ) .to_string ( ) ;
2550
let ( st, body) = call ( & app, " POST" , " /api/v1/flow/claim" , Some ( token) , Some ( body) ) .await;
2551
( st = = StatusCode:: OK ) .then ( | | serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) )
2552
}
2553
} ;
2554
let decline = | token : & 'static str , id : i64 , reason : & 'static str | {
2555
let app = h.app.clone ( ) ;
2556
async move {
2557
let body = serde_json:: json! ( { " reason" : reason } ) .to_string ( ) ;
2558
call ( & app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /decline" ) , Some ( token) , Some ( body) ) .await.0
2559
}
2560
} ;
2561
let job = claim_as ( " runner-secret" , " earth-1" ) .await.expect ( " a job" ) ;
2562
2563
assert_eq! ( decline ( " runner-two" , job, " not mine" ) .await, StatusCode:: CONFLICT , " only the runner it runs on may decline it" ) ;
2564
assert_eq! ( decline ( " runner-secret" , job, " the MicroVM tier needs /dev/kvm" ) .await, StatusCode:: NO_CONTENT ) ;
2565
let j = db:: flow:: job( & h.app.db, job) .await.unwrap ( ) .unwrap ( ) ;
2566
assert_eq! ( ( j.status.as_str ( ) , j.runner.as_str ( ) ) , ( " queued" , " " ) ) ;
2567
assert_eq! ( j.waiting, " declined by earth-1: the MicroVM tier needs /dev/kvm; waiting for another runner" ) ;
2568
2569
let next = claim_as ( " runner-secret" , " earth-1" ) .await.expect ( " the other ready job" ) ;
2570
assert_ne! ( next, job) ;
2571
let page = call ( & h.app, " GET" , & format! ( " /andy/declines/flow/{} " , run.number) , None , None ) .await.1 ;
2572
assert! ( page.contains ( " Waiting: declined by earth-1: the MicroVM tier needs /dev/kvm" ) , " {page}" ) ;
2573
assert_eq! ( claim_as ( " runner-two" , " plex-1" ) .await, Some ( job) , " another runner takes it" ) ;
2574
assert_eq! ( decline ( " runner-two" , job, " rootfs 36fc4e323804 is not on this runner" ) .await, StatusCode:: NO_CONTENT ) ;
2575
assert_eq! ( claim_as ( " runner-three" , " narvi" ) .await, Some ( job) ) ;
2576
assert_eq! ( decline ( " runner-three" , job, " rootfs 36fc4e323804 is for x86_64; this runner is riscv64" ) .await, StatusCode:: NO_CONTENT ) ;
2577
let j = db:: flow:: job( & h.app.db, job) .await.unwrap ( ) .unwrap ( ) ;
2578
assert_eq! ( j.status, " error" ) ;
2579
assert_eq! (
2580
j.error,
2581
" 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"
2582
) ;
2583
let r = db:: runners:: list( & h.app.db) .await.unwrap ( ) .into_iter ( ) .find ( | r | r.name = = " plex-1" ) .unwrap ( ) ;
2584
let declined = db:: flow:: declines_of_runner( & h.app.db, r.id, 10 ) .await.unwrap ( ) ;
2585
assert_eq! ( declined.len ( ) , 1 ) ;
2586
assert_eq! ( ( declined[ 0 ] .job_id, declined[ 0 ] .reason.as_str ( ) ) , ( job, " rootfs 36fc4e323804 is not on this runner" ) ) ;
2587
assert_eq! ( r.tier_list ( ) , vec! [ " microvm" ] ) ;
2588
}
554ccacf Andy McCall 1 month ago ↶
2589
2590
2591
2592
2593
# [ tokio ::test ( flavor = " multi_thread" ) ]
2594
async fn a_coverage_artifact_becomes_a_coverage_report ( ) {
2595
let h = hub ( ) .await;
2596
create ( & h.app, " andy" , " cov" ) .await;
2597
let url = format! ( " {} /andy/cov" , h.base) ;
2598
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2599
let c = client ( & url, work.path ( ) ) ;
2600
write (
2601
work.path ( ) ,
2602
" .flow/pipeline.kdl" ,
2603
& format! (
2604
" 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 "
2605
) ,
2606
) ;
2607
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 " ) ;
2608
c.snapshot ( " test" ) .unwrap ( ) ;
2609
c.commit ( " Code and coverage" , & [ ] ) .unwrap ( ) ;
2610
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2611
let run = runs_of ( & h.app, " andy" , " cov" ) .await.remove ( 0 ) ;
c8df33a6 Andy McCall 25 days ago ↶
2612
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 ↶
2613
let id = claim ( & h.app) .await.unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2614
2615
let ( st, text) = upload ( & h.app, id, " coverage" , b " this is not a report" ) .await;
2616
assert_eq! ( st, StatusCode:: OK , " a bad report never fails the upload: {text}" ) ;
2617
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 \
2618
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 " ;
2619
let ( st, text) = upload ( & h.app, id, " coverage" , lcov.as_bytes ( ) ) .await;
2620
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
2621
let log: Vec< String> = db:: flow:: log( & h.app.db, id, 0 , 50 ) .await.unwrap ( ) .into_iter ( ) .map ( | l | l.text ) .collect ( ) ;
2622
assert! ( log.iter ( ) .any ( | l | l.contains ( " coverage: this is not an LCOV report" ) ) , " {log:?}" ) ;
2623
assert! (
2624
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)" ) ,
2625
" {log:?}"
2626
) ;
2627
2628
let ( st, page) = call ( & h.app, " GET" , " /andy/cov/coverage" , None , None ) .await;
2629
assert_eq! ( st, StatusCode:: OK ) ;
c987c865 Andy McCall 1 month ago ↶
2630
assert! (
c8df33a6 Andy McCall 25 days ago ↶
2631
page.contains ( " >Reports</a>" )
c987c865 Andy McCall 1 month ago ↶
2632
& & page.contains ( " 50.0%" )
2633
& & page.contains ( " <strong>2</strong> of <strong>4</strong> relevant lines covered" ) ,
2634
" {page}"
2635
) ;
554ccacf Andy McCall 1 month ago ↶
2636
assert! ( page.contains ( " 1 of 2" ) & & page.contains ( " /andy/cov/coverage/" ) & & page.contains ( " /file/src/lib.rs" ) , " {page}" ) ;
2637
let report = crate_report ( & h.app, run.id) .await;
2638
let ( st, file) = call ( & h.app, " GET" , & format! ( " /andy/cov/coverage/{report} /file/src/lib.rs" ) , None , None ) .await;
2639
assert_eq! ( st, StatusCode:: OK , " {file}" ) ;
2640
assert_eq! ( file.matches ( " class=\" cov-hit\" " ) .count ( ) , 2 , " {file}" ) ;
2641
assert_eq! ( file.matches ( " class=\" cov-miss\" " ) .count ( ) , 2 , " {file}" ) ;
2642
assert! ( file.contains ( " 3×" ) , " {file}" ) ;
2643
assert_eq! ( call ( & h.app, " GET" , & format! ( " /andy/cov/coverage/{report} /file/src/gone.rs" ) , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
2644
let ( _ , badge) = call ( & h.app, " GET" , " /andy/cov/coverage/badge.svg" , None , None ) .await;
2645
assert! ( badge.contains ( " coverage: 50.0%" ) , " {badge}" ) ;
2646
let ( _ , run_page) = call ( & h.app, " GET" , & format! ( " /andy/cov/flow/{} " , run.number) , None , None ) .await;
2647
assert! ( run_page.contains ( " coverage 50.0%" ) , " {run_page}" ) ;
617c2840 Andy McCall 1 month ago ↶
2648
2649
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/cov/coverage" , None , None ) .await;
2650
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
2651
assert_eq! ( ( list[ 0 ] [ " percent" ] .as_f64 ( ) , list[ 0 ] [ " format" ] .as_str ( ) ) , ( Some ( 50. 0 ) , Some ( " lcov" ) ) , " {list}" ) ;
2652
let ( _ , one) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/cov/coverage/{report} " ) , None , None ) .await;
2653
let one: serde_json:: Value = serde_json:: from_str( & one) .unwrap ( ) ;
2654
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 ↶
2655
2656
2657
2658
let row = db:: repos:: get( & h.app.db, " andy" , " cov" ) .await.unwrap ( ) .unwrap ( ) ;
2659
let check = | checks : Vec< db:: hooks:: Check> | checks.into_iter ( ) .find ( | c | c.name = = " coverage" ) .expect ( " a coverage check" ) ;
2660
let c = check ( db:: hooks:: checks_for_state( & h.app.db, row.id, & run.state) .await.unwrap ( ) ) ;
2661
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " passed" , " 50.0%" ) ) ;
2662
assert! ( c.url.ends_with ( & format! ( " /andy/cov/coverage?report={report} " ) ) , " {}" , c.url) ;
2663
oxygenhub:: coverage:: set_rules( & h.app.db, row.id, oxygenhub:: coverage:: Rules { min: Some ( 60. 0 ) , max_drop: None } ) .await.unwrap ( ) ;
2664
assert_eq! ( upload ( & h.app, id, " coverage" , lcov.as_bytes ( ) ) .await.0 , StatusCode:: OK ) ;
2665
let c = check ( db:: hooks:: checks_for_state( & h.app.db, row.id, & run.state) .await.unwrap ( ) ) ;
2666
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " failed" , " 50.0%: below the minimum of 60%" ) ) ;
554ccacf Andy McCall 1 month ago ↶
2667
}
2668
2669
async fn crate_report ( app : & Shared, run_id : i64 ) -> i64 {
2670
oxygenhub:: coverage:: for_run( & app.db, run_id) .await.unwrap ( ) [ 0 ] .id
2671
}
36e5a9c1 Andy McCall 1 month ago ↶
2672
2673
2674
2675
# [ tokio ::test ( flavor = " multi_thread" ) ]
2676
async fn junit_reports_make_a_tests_tab ( ) {
2677
let h = hub ( ) .await;
2678
create ( & h.app, " andy" , " t" ) .await;
2679
let url = format! ( " {} /andy/t" , h.base) ;
2680
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2681
let c = client ( & url, work.path ( ) ) ;
2682
write (
2683
work.path ( ) ,
2684
" .flow/pipeline.kdl" ,
2685
& format! (
2686
" 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 "
2687
) ,
2688
) ;
c8df33a6 Andy McCall 25 days ago ↶
2689
assert! ( ! call ( & h.app, " GET" , " /andy/t" , None , None ) .await.1. contains ( " >Reports</a>" ) ) ;
36e5a9c1 Andy McCall 1 month ago ↶
2690
let case = | name : & str , ok : bool , secs : & str | {
2691
if ok {
2692
format! ( r # "<testcase classname="t.suite" name="{name} " time="{secs} "/>"# )
2693
} else {
2694
format! (
2695
r # "<testcase classname="t.suite" name="{name} " time="{secs} "><failure message="{name} broke">AssertionError</failure></testcase>"#
2696
)
2697
}
2698
} ;
2699
let runs = [
2700
vec! [ case ( " a" , true , " 0.1" ) , case ( " b" , true , " 2.5" ) , case ( " c" , true , " 0.2" ) ] ,
2701
vec! [ case ( " a" , true , " 0.1" ) , case ( " b" , false , " 2.6" ) , case ( " c" , true , " 0.2" ) , case ( " d" , true , " 0.3" ) ] ,
2702
vec! [ case ( " a" , true , " 0.1" ) , case ( " b" , true , " 2.4" ) , case ( " c" , true , " 0.2" ) , case ( " d" , true , " 0.3" ) ] ,
2703
] ;
2704
for ( i, cases) in runs.iter ( ) .enumerate ( ) {
2705
write ( work.path ( ) , " n.txt" , & i.to_string ( ) ) ;
2706
c.snapshot ( " test" ) .unwrap ( ) ;
2707
c.commit ( & format! ( " Run {i} " ) , & [ ] ) .unwrap ( ) ;
2708
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2709
2710
let mut a = None ;
2711
for _ in 0 .. 100 {
2712
a = claim ( & h.app) .await;
2713
if a.is_some ( ) {
2714
break ;
2715
}
2716
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2717
}
2718
let a = a.expect ( " a job to claim" ) ;
2719
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
2720
let xml = format! ( " <testsuites><testsuite name=\" t\" >{} </testsuite></testsuites>" , cases.concat ( ) ) ;
2721
assert_eq! ( upload ( & h.app, id, " junit" , xml.as_bytes ( ) ) .await.0 , StatusCode:: OK ) ;
2722
let failed = cases.iter ( ) .any ( | c | c.contains ( " <failure" ) ) ;
2723
let mut r = receipt ( " test" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , failed as u32 ) ;
2724
if ! failed {
2725
r[ " outputs" ] = serde_json:: json! ( [ oxy_core:: repo:: Repo:: blob_ref( xml.as_bytes ( ) ) .to_hex ( ) ] ) ;
2726
}
2727
let code = failed as i32 ;
2728
assert_eq! (
2729
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : code, " duration_ms" : 700 , " receipt" : r } ) ) .await,
2730
StatusCode:: NO_CONTENT
2731
) ;
2732
}
2733
let ( st, page) = call ( & h.app, " GET" , " /andy/t/tests" , None , None ) .await;
2734
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
2735
if let Some ( out) = std:: env:: var_os( " OXYHUB_DUMP_TESTS_PAGE" ) {
2736
std:: fs:: write( out, & page) .unwrap ( ) ;
2737
}
c8df33a6 Andy McCall 25 days ago ↶
2738
assert! ( page.contains ( " >Reports</a>" ) , " the tab" ) ;
36e5a9c1 Andy McCall 1 month ago ↶
2739
assert! ( page.contains ( " 100%" ) & & page.contains ( " of 4 tests passed" ) , " the latest run passes" ) ;
2740
assert! ( page.contains ( " 1 fixed" ) & & page.contains ( " t.suite::b" ) , " b fixed against the run before" ) ;
2741
assert! ( page.contains ( " 1 ✕" ) & & page.contains ( " 2 ✓" ) , " b is flaky: {page}" ) ;
2742
assert! ( page.contains ( " <svg class=\" cov-chart\" " ) , " charts" ) ;
2743
2744
let slow = page.find ( " Slowest tests" ) .unwrap ( ) ;
2745
assert! ( page[ slow.. ] .find ( " t.suite::b" ) .unwrap ( ) < page[ slow.. ] .find ( " t.suite::d" ) .unwrap ( ) ) ;
2746
2747
let row = db:: repos:: get( & h.app.db, " andy" , " t" ) .await.unwrap ( ) .unwrap ( ) ;
2748
let second = oxygenhub:: junit:: on_bookmark( & h.app.db, row.id, " main" , 10 ) .await.unwrap ( ) [ 1 ] .id;
2749
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/t/tests?report={second} " ) , None , None ) .await;
2750
assert! ( page.contains ( " 1 failing" ) & & page.contains ( " Newly failing" ) & & page.contains ( " b broke" ) , " {page}" ) ;
2751
assert! ( page.contains ( " 1 new tests" ) , " d is new" ) ;
617c2840 Andy McCall 1 month ago ↶
2752
2753
2754
2755
let ( st, list) = call ( & h.app, " GET" , " /api/v1/repos/andy/t/tests" , None , None ) .await;
2756
assert_eq! ( st, StatusCode:: OK , " {list}" ) ;
2757
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
2758
assert_eq! ( list.as_array ( ) .unwrap ( ) .len ( ) , 3 ) ;
2759
assert_eq! ( ( list[ 0 ] [ " tests" ] .as_i64 ( ) , list[ 1 ] [ " failed" ] .as_i64 ( ) ) , ( Some ( 4 ) , Some ( 1 ) ) ) ;
2760
let ( _ , one) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/t/tests/{second} ?status=failed" ) , None , None ) .await;
2761
let one: serde_json:: Value = serde_json:: from_str( & one) .unwrap ( ) ;
2762
assert_eq! ( one[ " cases" ] .as_array ( ) .unwrap ( ) .len ( ) , 1 ) ;
2763
assert_eq! ( ( one[ " cases" ] [ 0 ] [ " name" ] .as_str ( ) , one[ " cases" ] [ 0 ] [ " message" ] .as_str ( ) ) , ( Some ( " b" ) , Some ( " b broke" ) ) ) ;
2764
let ( _ , all) = call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/t/tests/{second} " ) , None , None ) .await;
2765
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & all) .unwrap ( ) [ " cases" ] .as_array ( ) .unwrap ( ) .len ( ) , 4 ) ;
2766
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 ↶
2767
2768
2769
let reports = oxygenhub:: junit:: on_bookmark( & h.app.db, row.id, " main" , 10 ) .await.unwrap ( ) ;
2770
let check = | state : String| {
2771
let app = h.app.clone ( ) ;
2772
async move { db:: hooks:: checks_for_state( & app.db, row.id, & state) .await.unwrap ( ) .into_iter ( ) .find ( | c | c.name = = " tests" ) .unwrap ( ) }
2773
} ;
2774
let c = check ( reports[ 1 ] .state.clone ( ) ) .await;
2775
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " failed" , " 1 failing; 1 new against main: t.suite::b" ) ) ;
2776
let c = check ( reports[ 0 ] .state.clone ( ) ) .await;
2777
assert_eq! ( ( c.status.as_str ( ) , c.summary.as_str ( ) ) , ( " passed" , " 4 passed" ) ) ;
2778
2779
2780
let ( _ , tab) = call ( & h.app, " GET" , " /andy/t/tests" , None , None ) .await;
2781
assert! (
2782
tab.contains ( " /andy/t/tests/case?classname=t.suite&name=b" ) | | tab.contains ( " /andy/t/tests/case?classname=t.suite&name=b" ) ,
2783
" linked"
2784
) ;
2785
let ( st, hist) = call ( & h.app, " GET" , " /andy/t/tests/case?classname=t.suite&name=b" , None , None ) .await;
2786
assert_eq! ( st, StatusCode:: OK , " {hist}" ) ;
2787
assert! ( hist.contains ( " 2 passed" ) & & hist.contains ( " 1 failed" ) & & hist.contains ( " b broke" ) , " {hist}" ) ;
2788
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 ↶
2789
}
67040b15 Andy McCall 25 days ago ↶
2790
2791
2792
2793
# [ tokio ::test ( flavor = " multi_thread" ) ]
2794
async fn a_job_no_runner_can_take_says_why ( ) {
2795
let h = hub ( ) .await;
2796
create ( & h.app, " andy" , " demo" ) .await;
2797
let url = format! ( " {} /andy/demo" , h.base) ;
2798
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2799
let c = client ( & url, work.path ( ) ) ;
2800
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
2801
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2802
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2803
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2804
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
2805
2806
2807
let r = db:: runners:: configured( & h.app.db, & blake3:: hash( b " elsewhere" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
2808
db:: runners:: seen( & h.app.db, r.id, None , None ) .await.unwrap ( ) ;
2809
let other = " ab" .repeat ( 32 ) ;
92a797b2 Andy McCall 25 days ago ↶
2810
db:: runners:: set_offer( & h.app.db, r.id, & [ " microvm" .to_string ( ) ] , Some ( & [ other] ) , false , " p" ) .await.unwrap ( ) ;
67040b15 Andy McCall 25 days ago ↶
2811
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
2812
2813
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
2814
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
2815
let build = v[ " jobs" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | j | j[ " name" ] = = " build" ) .unwrap ( ) .clone ( ) ;
2816
let why = build[ " waiting" ] .as_str ( ) .unwrap_or_default ( ) .to_string ( ) ;
2817
assert! ( why.contains ( " earth-9 does not hold image" ) & & why.contains ( & ROOTFS [ .. 12 ] ) , " {body}" ) ;
2818
let ( _ , page) = call ( & h.app, " GET" , " /andy/demo/flow/1" , None , None ) .await;
2819
assert! ( page.contains ( " data-waiting" ) & & page.contains ( " does not hold image" ) , " the run page says it" ) ;
13b27cb2 Andy McCall 18 days ago ↶
2820
2821
2822
2823
let mut summary = String :: new( ) ;
2824
for _ in 0 .. 40 {
2825
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
2826
summary = checks.iter ( ) .find ( | c | c.name = = " flow/build" ) .unwrap ( ) .summary.clone ( ) ;
2827
if summary.contains ( " does not hold image" ) {
2828
break ;
2829
}
2830
let build = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) .into_iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) ;
2831
db:: flow:: set_waiting( & h.app.db, build.id, " " ) .await.unwrap ( ) ;
2832
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
2833
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
2834
}
2835
assert! ( summary.contains ( " does not hold image" ) , " {summary}" ) ;
67040b15 Andy McCall 25 days ago ↶
2836
}
92a797b2 Andy McCall 25 days ago ↶
2837
58cbf424 Andy McCall 18 days ago ↶
2838
2839
2840
2841
# [ tokio ::test ( flavor = " multi_thread" ) ]
2842
async fn a_job_waiting_for_a_busy_runner_says_so ( ) {
2843
let h = hub ( ) .await;
2844
create ( & h.app, " andy" , " demo" ) .await;
2845
let url = format! ( " {} /andy/demo" , h.base) ;
2846
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2847
let c = client ( & url, work.path ( ) ) ;
2848
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
2849
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2850
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2851
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2852
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
2853
2854
let r = db:: runners:: configured( & h.app.db, & blake3:: hash( b " busy" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
2855
db:: runners:: seen( & h.app.db, r.id, None , None ) .await.unwrap ( ) ;
2856
db:: runners:: set_offer( & h.app.db, r.id, & [ " microvm" .to_string ( ) ] , Some ( & [ ROOTFS .to_string ( ) ] ) , false , " p" ) .await.unwrap ( ) ;
2857
sqlx:: query(
2858
" UPDATE flow_jobs SET status = 'running', runner_id = $1, runner = 'earth-9', started_ms = $2 WHERE run_id = $3 AND name = 'lint'" ,
2859
)
2860
.bind ( r.id)
2861
.bind ( db:: now_ms( ) )
2862
.bind ( run.id)
2863
.execute ( & h.app.db)
2864
.await
2865
.unwrap ( ) ;
2866
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
2867
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
2868
let build = jobs.iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) ;
2869
assert! ( build.waiting.contains ( " the runners that can take it are busy: earth-9 busy (1 of 1)" ) , " {}" , build.waiting) ;
2870
2871
sqlx:: query( " UPDATE flow_runners SET max_jobs = 2 WHERE id = $1" ) .bind ( r.id) .execute ( & h.app.db) .await.unwrap ( ) ;
2872
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
2873
let build = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) .into_iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) ;
2874
assert! ( build.waiting.contains ( " earth-9 has a free slot (1 of 2) but not the memory or disk for it now" ) , " {}" , build.waiting) ;
2875
}
2876
794ff94f Andy McCall 12 days ago ↶
2877
2878
2879
# [ tokio ::test ( flavor = " multi_thread" ) ]
2880
async fn an_expedited_run_moves_the_queue_at_once ( ) {
2881
let h = hub ( ) .await;
2882
for name in [ " first" , " urgent" ] {
2883
create ( & h.app, " andy" , name) .await;
2884
let work = tempfile:: tempdir( ) .unwrap ( ) ;
2885
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
2886
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
2887
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
2888
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
2889
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
2890
2891
assert_eq! ( runs_of ( & h.app, " andy" , name) .await.len ( ) , 1 ) ;
2892
}
2893
2894
let r = db:: runners:: configured( & h.app.db, & blake3:: hash( b " busy" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
2895
db:: runners:: seen( & h.app.db, r.id, None , None ) .await.unwrap ( ) ;
2896
db:: runners:: set_offer( & h.app.db, r.id, & [ " microvm" .to_string ( ) ] , Some ( & [ ROOTFS .to_string ( ) ] ) , false , " p" ) .await.unwrap ( ) ;
2897
let first = runs_of ( & h.app, " andy" , " first" ) .await.remove ( 0 ) ;
2898
sqlx:: query(
2899
" UPDATE flow_jobs SET status = 'running', runner_id = $1, runner = 'earth-9', started_ms = $2 WHERE run_id = $3 AND name = 'lint'" ,
2900
)
2901
.bind ( r.id)
2902
.bind ( db:: now_ms( ) )
2903
.bind ( first.id)
2904
.execute ( & h.app.db)
2905
.await
2906
.unwrap ( ) ;
2907
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
2908
let db = h.app.db.clone ( ) ;
2909
let build = | run : i64 | {
2910
let db = db.clone ( ) ;
2911
async move { db:: flow:: jobs( & db, run) .await.unwrap ( ) .into_iter ( ) .find ( | j | j.name = = " build" ) .unwrap ( ) }
2912
} ;
2913
let urgent = runs_of ( & h.app, " andy" , " urgent" ) .await.remove ( 0 ) ;
2914
assert! ( build ( urgent.id) .await.waiting.contains ( " 1 ready job comes first" ) , " {}" , build ( urgent.id) .await.waiting) ;
2915
2916
2917
let res = oxygenhub:: router( h.app.clone ( ) )
2918
.oneshot (
2919
Request:: post( " /register" )
2920
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
2921
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
2922
.unwrap ( ) ,
2923
)
2924
.await
2925
.unwrap ( ) ;
2926
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
2927
let page = oxygenhub:: router( h.app.clone ( ) )
2928
.oneshot ( Request:: get( " /andy/urgent/flow/1" ) .header ( header:: COOKIE , cookie.clone ( ) ) .body ( Body:: empty( ) ) .unwrap ( ) )
2929
.await
2930
.unwrap ( ) ;
2931
let page = String :: from_utf8_lossy( & page.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
2932
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
2933
let csrf = page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
2934
let res = oxygenhub:: router( h.app.clone ( ) )
2935
.oneshot (
2936
Request:: post( " /andy/urgent/flow/1/expedite" )
2937
.header ( header:: COOKIE , cookie.clone ( ) )
2938
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
2939
.body ( Body:: from( format! ( " _csrf={csrf} &reason=incident" ) ) )
2940
.unwrap ( ) ,
2941
)
2942
.await
2943
.unwrap ( ) ;
2944
assert! ( res.status ( ) .is_redirection ( ) , " {}" , res.status ( ) ) ;
2945
assert! ( ! build ( urgent.id) .await.waiting.contains ( " come" ) , " first now: {}" , build ( urgent.id) .await.waiting) ;
2946
let w = build ( first.id) .await.waiting;
2947
assert! ( w.contains ( " 2 ready jobs come first (after expedited run andy/urgent #1)" ) , " {w}" ) ;
2948
}
2949
92a797b2 Andy McCall 25 days ago ↶
2950
async fn put_bytes ( app : & Shared, path : & str , bearer : & str , bytes : Vec< u8 > ) -> ( StatusCode, String) {
2951
let req = Request:: put( path)
2952
.header ( header:: AUTHORIZATION , format! ( " Bearer {bearer} " ) )
2953
.header ( header:: CONTENT_TYPE , " application/octet-stream" )
2954
.body ( Body:: from( bytes) )
2955
.unwrap ( ) ;
2956
let res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
2957
let st = res.status ( ) ;
2958
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
2959
}
2960
2961
2962
2963
2964
2965
# [ tokio ::test ( flavor = " multi_thread" ) ]
2966
async fn images_are_sent_by_chunk_and_offered_to_runners_that_fetch ( ) {
2967
let h = hub ( ) .await;
2968
2969
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
2970
2971
let mut image: Vec< u8 > = ( 0 .. 400_000 u32 ) .map ( | i | ( i % 251 ) as u8 ) .collect ( ) ;
2972
image.extend ( std:: iter:: repeat_n( 0 u8 , 2 < < 20 ) ) ;
2973
image.extend ( ( 0 .. 100_000 u32 ) .map ( | i | ( i % 239 ) as u8 ) ) ;
2974
let mut chunks = std:: collections:: HashMap:: new( ) ;
2975
let m = jet:: images:: scan( & image[ .. ] , Some ( " x86_64" .into ( ) ) , | c , b | {
2976
chunks.insert ( c.hash ( ) .to_string ( ) , b.to_vec ( ) ) ;
2977
Ok ( ( ) )
2978
} )
2979
.unwrap ( ) ;
2980
let hash = m.hash.clone ( ) ;
2981
2982
2983
let ask = serde_json:: json! ( { " chunks" : m.needed ( ) } ) .to_string ( ) ;
2984
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/images/missing" , Some ( " site" ) , Some ( ask.clone ( ) ) ) .await;
2985
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
2986
let missing: Vec< String> = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " missing" ]
2987
.as_array ( )
2988
.unwrap ( )
2989
.iter ( )
2990
.map ( | v | v.as_str ( ) .unwrap ( ) .to_string ( ) )
2991
.collect ( ) ;
2992
assert_eq! ( missing.len ( ) , m.needed ( ) .len ( ) ) ;
2993
let manifest = serde_json:: to_string( & m) .unwrap ( ) ;
2994
let ( st, body) = call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " site" ) , Some ( manifest.clone ( ) ) ) .await;
2995
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " was not sent" ) , " {st} {body}" ) ;
2996
2997
let first = & missing[ 0 ] ;
2998
assert_eq! ( put_bytes ( & h.app, & format! ( " /api/v1/flow/images/chunks/{first} " ) , " site" , vec! [ 1 , 2 , 3 ] ) .await.0 , StatusCode:: BAD_REQUEST ) ;
2999
assert_ne! (
3000
put_bytes ( & h.app, & format! ( " /api/v1/flow/images/chunks/{first} " ) , " runner-secret" , chunks[ first] .clone ( ) ) .await.0 ,
3001
StatusCode:: NO_CONTENT
3002
) ;
3003
for c in & missing {
3004
let ( st, body) = put_bytes ( & h.app, & format! ( " /api/v1/flow/images/chunks/{c} " ) , " site" , chunks[ c] .clone ( ) ) .await;
3005
assert_eq! ( st, StatusCode:: NO_CONTENT , " {body}" ) ;
3006
}
3007
let ( _ , body) = call ( & h.app, " POST" , " /api/v1/flow/images/missing" , Some ( " site" ) , Some ( ask) ) .await;
3008
assert! ( body.contains ( " \" missing\" :[]" ) , " {body}" ) ;
750579ec Andy McCall 24 days ago ↶
3009
3010
let many: Vec< String> = ( 0 .. 60_000 u32 ) .map ( | i | blake3:: hash( & i.to_le_bytes ( ) ) .to_hex ( ) .to_string ( ) ) .collect ( ) ;
3011
let ( st, body) =
3012
call ( & h.app, " POST" , " /api/v1/flow/images/missing" , Some ( " site" ) , Some ( serde_json:: json! ( { " chunks" : many } ) .to_string ( ) ) ) .await;
3013
assert_eq! ( st, StatusCode:: OK , " {}" , & body[ .. body.len ( ) .min ( 200 ) ] ) ;
3014
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " missing" ] .as_array ( ) .unwrap ( ) .len ( ) , 60_000 ) ;
92a797b2 Andy McCall 25 days ago ↶
3015
3016
let mut wrong = m.clone ( ) ;
3017
wrong.hash = " ab" .repeat ( 32 ) ;
3018
let ( st, _ ) =
3019
call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{} " , wrong.hash) , Some ( " site" ) , Some ( serde_json:: to_string( & wrong) .unwrap ( ) ) )
3020
.await;
3021
assert_eq! ( st, StatusCode:: CONFLICT , " chunks that make another image" ) ;
3022
let ( st, body) = call ( & h.app, " PUT" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " site" ) , Some ( manifest) ) .await;
3023
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
3024
3025
3026
let ( st, body) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{hash} " ) , Some ( " runner-secret" ) , None ) .await;
3027
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3028
let got: jet:: images:: Manifest = serde_json:: from_str( & body) .unwrap ( ) ;
3029
assert_eq! ( got, m) ;
3030
let dir = tempfile:: tempdir( ) .unwrap ( ) ;
3031
let out = dir.path ( ) .join ( " img.ext4" ) ;
3032
let mut fetched = Vec :: new( ) ;
3033
for c in got.needed ( ) {
3034
let req = Request:: get( format! ( " /api/v1/flow/images/chunks/{c} " ) )
3035
.header ( header:: AUTHORIZATION , " Bearer runner-secret" )
3036
.body ( Body:: empty( ) )
3037
.unwrap ( ) ;
3038
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
3039
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
3040
let bytes = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) ;
3041
fetched.push ( ( c.to_string ( ) , bytes) ) ;
3042
}
3043
jet:: images:: assemble( & got, & out, | c | Ok ( fetched.iter ( ) .find ( | ( h , _) | h = = c.hash ( ) ) .unwrap ( ) .1. clone ( ) ) ) .unwrap ( ) ;
3044
assert_eq! ( std:: fs:: read( & out) .unwrap ( ) , image) ;
3045
assert_eq! (
3046
call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{} " , " cd" .repeat ( 32 ) ) , Some ( " runner-secret" ) , None ) .await.0 ,
3047
StatusCode:: NOT_FOUND
3048
) ;
3049
assert_ne! ( call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{hash} " ) , None , None ) .await.0 , StatusCode:: OK , " not anonymous" ) ;
3050
3051
3052
3053
create ( & h.app, " andy" , " demo" ) .await;
3054
let url = format! ( " {} /andy/demo" , h.base) ;
3055
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3056
let c = client ( & url, work.path ( ) ) ;
3057
write (
3058
work.path ( ) ,
3059
" .flow/pipeline.kdl" ,
3060
& format! ( " schema-version 1\n sandbox \" img\" {{ \n rootfs \" cas://blob/{hash} \" \n }} \n job \" build\" {{ \n sandbox \" img\" \n exec \" make\" \n }} \n " ) ,
3061
) ;
3062
c.commit ( " Build on the image" , & [ ] ) .unwrap ( ) ;
3063
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3064
runs_of ( & h.app, " andy" , " demo" ) .await;
1c8276b9 Andy McCall 24 days ago ↶
3065
3066
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
3067
let run1 = db:: flow:: run( & h.app.db, row.id, 1 ) .await.unwrap ( ) .unwrap ( ) ;
3068
let checks = checks_when ( & h.app, row.id, & run1.state, 1 ) .await;
3069
assert! ( checks.iter ( ) .all ( | c | c.name ! = " pipeline/images" ) , " {checks:?}" ) ;
92a797b2 Andy McCall 25 days ago ↶
3070
let ask = | fetches : bool | format! ( r # "{{ "runner":"test-1","runtimes":["microvm"],"images":[],"fetches_images":{fetches} }} "# ) ;
3071
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( false ) ) ) .await;
3072
assert_eq! ( st, StatusCode:: NO_CONTENT , " it lacks the image and does not fetch" ) ;
3073
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3074
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
3075
assert! ( body.contains ( " does not hold image" ) , " {body}" ) ;
3076
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( true ) ) ) .await;
3077
assert_eq! ( st, StatusCode:: OK , " it fetches, and the hub holds the image: {body}" ) ;
7829b7c7 Andy McCall 25 days ago ↶
3078
3079
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
3080
assert! ( ! body.contains ( " waiting" ) , " a running job keeps no waiting reason: {body}" ) ;
92a797b2 Andy McCall 25 days ago ↶
3081
}
40dd3f86 Andy McCall 25 days ago ↶
3082
3083
3084
3085
3086
# [ tokio ::test ( flavor = " multi_thread" ) ]
3087
async fn coverage_uploaded_from_a_workstation_says_so ( ) {
3088
let h = hub ( ) .await;
3089
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3090
create ( & h.app, " andy" , " vk" ) .await;
3091
let url = format! ( " {} /andy/vk" , h.base) ;
3092
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3093
let c = client ( & url, work.path ( ) ) ;
3094
write ( work.path ( ) , " src/game.c" , " int a;\n int b;\n int c;\n int d;\n " ) ;
3095
c.commit ( " Game" , & [ ] ) .unwrap ( ) ;
3096
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3097
3098
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 " ;
3099
let body = serde_json:: json! ( { " bookmark" : " main" , " content" : lcov } ) .to_string ( ) ;
3100
let ( st, out) = call ( & h.app, " POST" , " /api/v1/repos/andy/vk/coverage" , Some ( " site" ) , Some ( body) ) .await;
3101
assert_eq! ( st, StatusCode:: CREATED , " {out}" ) ;
3102
let v: serde_json:: Value = serde_json:: from_str( & out) .unwrap ( ) ;
3103
assert_eq! ( ( v[ " uploaded_by" ] .as_str ( ) , v[ " lines_hit" ] .as_i64 ( ) , v[ " lines_found" ] .as_i64 ( ) ) , ( Some ( " andy" ) , Some ( 3 ) , Some ( 4 ) ) ) ;
3104
assert_eq! ( v[ " dropped" ] , 1 , " a file outside the state is left out" ) ;
3105
assert_eq! ( v[ " bookmark" ] , " main" ) ;
3106
3107
let ( _ , page) = call ( & h.app, " GET" , " /andy/vk/coverage" , None , None ) .await;
3108
assert! ( page.contains ( " uploaded by andy" ) & & page.contains ( " 75.0%" ) , " the Coverage page says where it came from" ) ;
3109
let ( _ , page) = call ( & h.app, " GET" , " /andy/vk/reports" , None , None ) .await;
3110
assert! ( page.contains ( " uploaded by andy" ) , " and the Reports card" ) ;
3111
let row = db:: repos:: get( & h.app.db, " andy" , " vk" ) .await.unwrap ( ) .unwrap ( ) ;
3112
let state = v[ " state" ] .as_str ( ) .unwrap ( ) ;
3113
let checks = db:: hooks:: checks_for_state( & h.app.db, row.id, state) .await.unwrap ( ) ;
3114
assert! ( checks.iter ( ) .all ( | c | c.name ! = " coverage" ) , " an upload sets no check: {checks:?}" ) ;
3115
3116
3117
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/vk/coverage" , None , Some ( r # "{"content":"x"}"# .into ( ) ) ) .await;
3118
assert_ne! ( st, StatusCode:: CREATED ) ;
3119
let ( st, _ ) = call (
3120
& h.app,
3121
" POST" ,
3122
" /api/v1/repos/andy/vk/coverage" ,
3123
Some ( " site" ) ,
3124
Some ( r # "{"bookmark":"nope","content":"SF:a\nend_of_record\n"}"# .into ( ) ) ,
3125
)
3126
.await;
3127
assert_eq! ( st, StatusCode:: BAD_REQUEST ) ;
3128
}
2fb70d01 Andy McCall 25 days ago ↶
3129
3130
3131
# [ tokio ::test ( flavor = " multi_thread" ) ]
3132
async fn a_runners_facts_are_kept_from_its_claim ( ) {
3133
let h = hub ( ) .await;
3134
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3135
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"}}"# ;
3136
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body.into ( ) ) ) .await;
3137
assert_eq! ( st, StatusCode:: NO_CONTENT , " nothing to run, but the facts are kept" ) ;
3138
let ( st, out) = call ( & h.app, " GET" , " /api/v1/runners" , Some ( " site" ) , None ) .await;
3139
assert_eq! ( st, StatusCode:: OK , " {out}" ) ;
3140
let v: serde_json:: Value = serde_json:: from_str( & out) .unwrap ( ) ;
3141
let r = & v[ 0 ] ;
3142
assert_eq! (
3143
( r[ " speed" ] .as_i64 ( ) , r[ " speed_class" ] .as_i64 ( ) , r[ " free_disk_mib" ] .as_i64 ( ) , r[ " load_x100" ] .as_i64 ( ) ) ,
3144
( Some ( 100 ) , Some ( 3 ) , Some ( 42000 ) , Some ( 112 ) )
3145
) ;
3146
let row = db:: runners:: list( & h.app.db) .await.unwrap ( ) .remove ( 0 ) ;
3147
assert_eq! ( row.facts_line ( ) , " speed 100 (class 3) · load 1.12 · 41 GiB free" ) ;
3148
}
44a3d2eb Andy McCall 25 days ago ↶
3149
3150
3151
3152
# [ tokio ::test ( flavor = " multi_thread" ) ]
3153
async fn a_jobs_next_run_needs_the_disk_its_last_took ( ) {
3154
let h = hub ( ) .await;
3155
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3156
create ( & h.app, " andy" , " demo" ) .await;
3157
let url = format! ( " {} /andy/demo" , h.base) ;
3158
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3159
let c = client ( & url, work.path ( ) ) ;
3160
write (
3161
work.path ( ) ,
3162
" .flow/pipeline.kdl" ,
3163
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
3164
) ;
3165
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
3166
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3167
runs_of ( & h.app, " andy" , " demo" ) .await;
3168
let claim_with = | free : u64 | format! ( r # "{{ "runner":"test-1","runtimes":["microvm"],"facts":{{ "free_disk_mib":{free} }} }} "# ) ;
3169
3170
3171
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( claim_with ( 500 ) ) ) .await;
3172
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3173
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3174
let done = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " peak_disk_mib" : 8000 , " peak_memory_mib" : 900 } ) ;
3175
assert_eq! ( finish ( & h.app, id, done) .await, StatusCode:: NO_CONTENT ) ;
3176
let job = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
3177
assert_eq! ( ( job.peak_disk_mib, job.peak_memory_mib) , ( Some ( 8000 ) , Some ( 900 ) ) ) ;
3178
3179
3180
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
3181
assert_eq! ( st, StatusCode:: CREATED ) ;
3182
let repo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
3183
let run2 = db:: flow:: run( & h.app.db, repo.id, 2 ) .await.unwrap ( ) .unwrap ( ) ;
3184
let next = db:: flow:: jobs( & h.app.db, run2.id) .await.unwrap ( ) .remove ( 0 ) ;
3185
assert_eq! ( next.need_disk_mib, Some ( 8000 + 2000 + 64 ) ) ;
3186
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( claim_with ( 9000 ) ) ) .await;
3187
assert_eq! ( st, StatusCode:: NO_CONTENT , " 9000 MiB free is not enough" ) ;
3188
oxygenhub:: flow:: explain_waits( & h.app) .await.unwrap ( ) ;
3189
let waiting = db:: flow:: job( & h.app.db, next.id) .await.unwrap ( ) .unwrap ( ) .waiting;
3190
assert! ( waiting.contains ( " 8.8 GiB free; the job needs ~9.8 GiB" ) , " {waiting}" ) ;
3191
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( claim_with ( 20_000 ) ) ) .await;
3192
assert_eq! ( st, StatusCode:: OK , " a roomier runner takes it: {body}" ) ;
3193
}
501d8b26 Andy McCall 25 days ago ↶
3194
3195
3196
3197
3198
# [ tokio ::test ( flavor = " multi_thread" ) ]
3199
async fn a_job_waits_briefly_for_a_runner_that_holds_its_image ( ) {
3200
let h = hub ( ) .await;
3201
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3202
3203
db:: runners:: configured( & h.app.db, & blake3:: hash( b " other-secret" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
3204
let ask = | images : & str | {
3205
format! (
3206
r # "{{ "runner":"r","runtimes":["microvm"],"images":[{images} ],"fetches_images":true,"facts":{{ "speed":100,"speed_class":3,"free_disk_mib":50000}} }} "#
3207
)
3208
} ;
3209
let holder = ask ( & format! ( " \" {ROOTFS} \" " ) ) ;
3210
3211
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder.clone ( ) ) ) .await;
3212
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
3213
3b1bc27f Andy McCall 20 days ago
3214
sqlx:: query( " INSERT INTO jet_images (hash, size, arch, chunks, uploader, created_ms) VALUES ($1, 1, 'x86_64', 1, 'andy', 0)" )
501d8b26 Andy McCall 25 days ago ↶
3215
.bind ( ROOTFS )
3216
.execute ( & h.app.db)
3217
.await
3218
.unwrap ( ) ;
3219
3220
create ( & h.app, " andy" , " demo" ) .await;
3221
let url = format! ( " {} /andy/demo" , h.base) ;
3222
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3223
let c = client ( & url, work.path ( ) ) ;
3224
write (
3225
work.path ( ) ,
3226
" .flow/pipeline.kdl" ,
3227
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
3228
) ;
3229
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
3230
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3231
runs_of ( & h.app, " andy" , " demo" ) .await;
3232
3233
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( " " ) ) ) .await;
3234
assert_eq! ( st, StatusCode:: NO_CONTENT , " held for the runner that holds the image" ) ;
3235
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder) ) .await;
3236
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3237
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3238
let placed = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .placed;
3239
assert! ( placed.starts_with ( " holds the image, idle, speed 100 (class 3)" ) , " {placed}" ) ;
3240
assert! ( placed.contains ( " held for a better runner" ) , " {placed}" ) ;
3241
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/demo/flow/runs/1" , Some ( " site" ) , None ) .await;
3242
assert! ( body.contains ( " \" placed\" :\" holds the image" ) , " {body}" ) ;
3243
}
20b8fe85 Andy McCall 25 days ago ↶
3244
4fa0da12 Andy McCall 23 days ago ↶
3245
3246
3247
# [ tokio ::test ( flavor = " multi_thread" ) ]
3248
async fn a_job_waits_briefly_for_the_runner_holding_its_build_cache ( ) {
3249
let h = hub ( ) .await;
3250
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3251
db:: runners:: configured( & h.app.db, & blake3:: hash( b " other-secret" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
3252
let slot = flow_core:: api:: cache_slot( " andy/demo" , " build" ) ;
3253
let ask = | caches : & str | {
3254
format! (
3255
r # "{{ "runner":"r","runtimes":["microvm"],"images":["{ROOTFS} "],"fetches_images":true,"caches":[{caches} ],"facts":{{ "speed":100,"speed_class":3,"free_disk_mib":50000}} }} "#
3256
)
3257
} ;
3258
let holder = ask ( & format! ( " \" {slot} \" " ) ) ;
3259
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder.clone ( ) ) ) .await;
3260
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
3261
3262
create ( & h.app, " andy" , " demo" ) .await;
3263
let url = format! ( " {} /andy/demo" , h.base) ;
3264
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3265
let c = client ( & url, work.path ( ) ) ;
3266
write (
3267
work.path ( ) ,
3268
" .flow/pipeline.kdl" ,
3269
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
3270
) ;
3271
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
3272
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3273
runs_of ( & h.app, " andy" , " demo" ) .await;
3274
3275
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( " " ) ) ) .await;
3276
assert_eq! ( st, StatusCode:: NO_CONTENT , " held for the runner holding the build cache" ) ;
3277
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( holder) ) .await;
3278
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3279
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3280
let placed = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .placed;
3281
assert! ( placed.starts_with ( " holds its build cache, holds the image" ) , " {placed}" ) ;
3282
}
3283
c2be148c Andy McCall 22 days ago ↶
3284
3285
3286
# [ tokio ::test ( flavor = " multi_thread" ) ]
3287
async fn a_job_waits_briefly_for_the_fresher_build_cache ( ) {
3288
let h = hub ( ) .await;
3289
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3290
db:: runners:: configured( & h.app.db, & blake3:: hash( b " other-secret" ) .to_hex ( ) , " earth-9" ) .await.unwrap ( ) ;
3291
let slot = flow_core:: api:: cache_slot( " andy/demo" , " build" ) ;
3292
let now = db:: now_ms( ) ;
3293
let ask = | kept : i64 | {
3294
format! (
3295
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}} }} "#
3296
)
3297
} ;
3298
3299
let fresh = ask ( now - 60_000 ) ;
3300
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( fresh.clone ( ) ) ) .await;
3301
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
3302
3303
create ( & h.app, " andy" , " demo" ) .await;
3304
let url = format! ( " {} /andy/demo" , h.base) ;
3305
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3306
let c = client ( & url, work.path ( ) ) ;
3307
write (
3308
work.path ( ) ,
3309
" .flow/pipeline.kdl" ,
3310
& format! ( " schema-version 1\n sandbox \" alpine\" {{ \n rootfs \" cas://blob/{ROOTFS} \" \n }} \n job \" build\" {{ \n sandbox \" alpine\" \n exec \" make\" \n }} \n " ) ,
3311
) ;
3312
c.commit ( " Build" , & [ ] ) .unwrap ( ) ;
3313
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3314
runs_of ( & h.app, " andy" , " demo" ) .await;
3315
3316
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( ask ( now - 86_400_000 ) ) ) .await;
3317
assert_eq! ( st, StatusCode:: NO_CONTENT , " held for the fresher copy" ) ;
3318
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " other-secret" ) , Some ( fresh) ) .await;
3319
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3320
let id = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3321
let placed = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .placed;
3322
assert! ( placed.starts_with ( " holds its build cache (kept 1 min ago), holds the image" ) , " {placed}" ) ;
3323
}
3324
20b8fe85 Andy McCall 25 days ago ↶
3325
3326
3327
3328
# [ tokio ::test ( flavor = " multi_thread" ) ]
3329
async fn report_files_expire_under_retention ( ) {
3330
let h = hub ( ) .await;
3331
create ( & h.app, " andy" , " demo" ) .await;
3332
let repo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
3333
let add = | bookmark : & 'static str , at : i64 | {
3334
let db = h.app.db.clone ( ) ;
3335
async move {
3336
let id: i64 = sqlx:: query_scalar(
3337
" INSERT INTO reports (repo_id, name, kind, title, summary, context, job, bookmark, state, files, bytes, created_ms) \
3b1bc27f Andy McCall 20 days ago
3338
VALUES ($1, 'perf', 'performance', 'Perf', '', '{}', 'perf', $2, 's', 1, 1, $3) RETURNING id" ,
20b8fe85 Andy McCall 25 days ago ↶
3339
)
3340
.bind ( repo.id)
3341
.bind ( bookmark)
3342
.bind ( at)
3343
.fetch_one ( & db)
3344
.await
3345
.unwrap ( ) ;
3b1bc27f Andy McCall 20 days ago
3346
sqlx:: query( " INSERT INTO report_files (report_id, path, blob, size) VALUES ($1, 'report.toml', $2, 1)" )
20b8fe85 Andy McCall 25 days ago ↶
3347
.bind ( id)
3348
.bind ( format! ( " {:064x}" , id) )
3349
.execute ( & db)
3350
.await
3351
.unwrap ( ) ;
3b1bc27f Andy McCall 20 days ago
3352
sqlx:: query( " INSERT INTO report_metrics (report_id, key, value, unit, better) VALUES ($1, 'ms', 1, '', '')" )
20b8fe85 Andy McCall 25 days ago ↶
3353
.bind ( id)
3354
.execute ( & db)
3355
.await
3356
.unwrap ( ) ;
3357
id
3358
}
3359
} ;
3360
let main_old = add ( " main" , 1 ) .await;
3361
add ( " main" , 2 ) .await;
3362
let mut feature = Vec :: new( ) ;
3363
for at in 0 .. 52 {
3364
feature.push ( add ( " feature" , 10 + at) .await) ;
3365
}
3366
assert_eq! ( oxygenhub:: reports:: expire( & h.app.db) .await.unwrap ( ) , 2 , " the two oldest feature runs" ) ;
3367
let files = | id : i64 | {
3368
let db = h.app.db.clone ( ) ;
3369
async move {
3b1bc27f Andy McCall 20 days ago
3370
sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM report_files WHERE report_id = $1" ) .bind ( id) .fetch_one ( & db) .await.unwrap ( )
20b8fe85 Andy McCall 25 days ago ↶
3371
}
3372
} ;
3373
assert_eq! ( ( files ( feature[ 0 ] ) .await, files ( feature[ 1 ] ) .await, files ( feature[ 2 ] ) .await) , ( 0 , 0 , 1 ) ) ;
3374
assert_eq! ( files ( main_old) .await, 1 , " the default bookmark keeps every run" ) ;
3375
assert_eq! ( oxygenhub:: reports:: metrics( & h.app.db, feature[ 0 ] ) .await.unwrap ( ) .len ( ) , 1 , " metrics are kept" ) ;
3376
assert_eq! ( oxygenhub:: reports:: expire( & h.app.db) .await.unwrap ( ) , 0 , " once" ) ;
3377
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/demo/reports/perf?id={} " , feature[ 0 ] ) , None , None ) .await;
3378
assert! ( page.contains ( " files have expired" ) , " the page says so" ) ;
1c8276b9 Andy McCall 24 days ago ↶
3379
}
3380
3381
3382
3383
3384
# [ tokio ::test ( flavor = " multi_thread" ) ]
3385
async fn a_pipeline_pinning_an_image_nobody_holds_fails_a_check ( ) {
3386
let h = hub ( ) .await;
3387
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3388
3389
let none = r # "{"runner":"test-1","runtimes":["microvm"],"images":[]}"# ;
3390
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( none.into ( ) ) ) .await;
3391
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
3392
create ( & h.app, " andy" , " demo" ) .await;
3393
let url = format! ( " {} /andy/demo" , h.base) ;
3394
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3395
let c = client ( & url, work.path ( ) ) ;
3396
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3397
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3398
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
3399
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3400
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
3401
let checks = checks_when ( & h.app, run.repo_id, & run.state, 4 ) .await;
3402
let check = checks.iter ( ) .find ( | c | c.name = = " pipeline/images" ) .expect ( " an images check" ) ;
3403
assert! ( check.status = = " failed" & & check.summary.contains ( & ROOTFS [ .. 12 ] ) & & check.summary.contains ( " flow rootfs push" ) , " {check:?}" ) ;
3404
3405
let holds = format! ( r # "{{ "runner":"test-1","runtimes":["microvm"],"images":["{ROOTFS} "]}} "# ) ;
3406
call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( holds) ) .await;
3407
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
3408
assert_eq! ( st, StatusCode:: CREATED ) ;
3409
let checks = db:: hooks:: checks_for_state( & h.app.db, run.repo_id, & run.state) .await.unwrap ( ) ;
3410
let check = checks.iter ( ) .find ( | c | c.name = = " pipeline/images" ) .unwrap ( ) ;
3411
assert_eq! ( check.status, " passed" , " {check:?}" ) ;
20b8fe85 Andy McCall 25 days ago ↶
3412
}
a46d15e7 Andy McCall 24 days ago ↶
3413
3414
3415
3416
3417
# [ tokio ::test ( flavor = " multi_thread" ) ]
3418
async fn the_tracker_names_peers_in_the_same_site_and_signs_a_ticket ( ) {
3419
let h = hub ( ) .await;
3420
let app = & h.app;
3421
let image = " ab" .repeat ( 32 ) ;
3422
let claim_as = | token : & 'static str , name : & 'static str , body : serde_json:: Value| async move {
3423
let mut b = serde_json:: json! ( { " runner" : name, " runtimes" : [ " microvm" ] } ) ;
3424
b.as_object_mut ( ) .unwrap ( ) .extend ( body.as_object ( ) .unwrap ( ) .clone ( ) ) ;
3425
let ( st, out) = call ( app, " POST" , " /api/v1/flow/claim" , Some ( token) , Some ( b.to_string ( ) ) ) .await;
3426
assert_eq! ( st, StatusCode:: NO_CONTENT , " {out}" ) ;
3427
} ;
3428
let announce = | token : & 'static str , state : & 'static str | {
3429
let body = serde_json:: json! ( { " image" : image, " state" : state } ) .to_string ( ) ;
3430
async move { call ( app, " POST" , " /api/v1/flow/swarm/announce" , Some ( token) , Some ( body) ) .await }
3431
} ;
3432
6adcd68b Andy McCall 24 days ago ↶
3433
claim_as (
3434
" runner-secret" ,
3435
" a" ,
3436
serde_json:: json! ( { " images" : [ image] , " swarm" : { " addr" : " 10.0.0.1:7401" , " site" : " lab" , " cert" : " 11" .repeat ( 32 ) } } ) ,
3437
)
3438
.await;
3439
claim_as (
3440
" runner-two" ,
3441
" b" ,
3442
serde_json:: json! ( { " images" : [ ] , " swarm" : { " addr" : " 10.0.0.2:7401" , " site" : " lab" , " cert" : " 22" .repeat ( 32 ) } } ) ,
3443
)
3444
.await;
3445
claim_as (
3446
" runner-three" ,
3447
" c" ,
3448
serde_json:: json! ( { " images" : [ ] , " swarm" : { " addr" : " 10.0.0.3:7401" , " site" : " lab" , " cert" : " 33" .repeat ( 32 ) } } ) ,
3449
)
3450
.await;
a46d15e7 Andy McCall 24 days ago ↶
3451
3452
let ( st, body) = announce ( " runner-two" , " fetching" ) .await;
3453
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3454
let a: flow_core:: api:: Announced = serde_json:: from_str( & body) .unwrap ( ) ;
3455
let names: Vec< ( & str , bool ) > = a.peers.iter ( ) .map ( | p | ( p.addr.as_str ( ) , p.complete) ) .collect ( ) ;
3456
assert_eq! ( names, vec! [ ( " 10.0.0.1:7401" , true ) ] , " only A holds it so far" ) ;
6adcd68b Andy McCall 24 days ago ↶
3457
assert_eq! ( a.peers[ 0 ] .cert, " 11" .repeat ( 32 ) , " with the fingerprint peers check it by" ) ;
a46d15e7 Andy McCall 24 days ago ↶
3458
let t = jet:: swarm:: Ticket:: open( & a.ticket, & app.hub_key.identity, oxygenhub:: db:: now_ms( ) as u64 ) .unwrap ( ) ;
3459
assert_eq! ( ( t.name.as_str ( ) , t.site.as_str ( ) ) , ( " b" , " lab" ) ) ;
3460
3461
3462
let ( _ , body) = announce ( " runner-three" , " fetching" ) .await;
3463
let a: flow_core:: api:: Announced = serde_json:: from_str( & body) .unwrap ( ) ;
3464
let mut peers: Vec< ( & str , bool ) > = a.peers.iter ( ) .map ( | p | ( p.addr.as_str ( ) , p.complete) ) .collect ( ) ;
3465
peers.sort ( ) ;
3466
assert_eq! ( peers, vec! [ ( " 10.0.0.1:7401" , true ) , ( " 10.0.0.2:7401" , false ) ] ) ;
3467
3468
3469
announce ( " runner-two" , " done" ) .await;
6adcd68b Andy McCall 24 days ago ↶
3470
claim_as (
3471
" runner-three" ,
3472
" c" ,
3473
serde_json:: json! ( { " images" : [ ] , " swarm" : { " addr" : " 10.0.0.3:7401" , " site" : " elsewhere" , " cert" : " 33" .repeat ( 32 ) } } ) ,
3474
)
3475
.await;
a46d15e7 Andy McCall 24 days ago ↶
3476
let ( _ , body) = announce ( " runner-three" , " fetching" ) .await;
3477
let a: flow_core:: api:: Announced = serde_json:: from_str( & body) .unwrap ( ) ;
3478
assert! ( a.peers.is_empty ( ) , " {:?}" , a.peers) ;
6adcd68b Andy McCall 24 days ago ↶
3479
3480
3481
claim_as ( " runner-secret" , " a" , serde_json:: json! ( { " images" : [ image] , " swarm" : { " addr" : " 10.0.0.1:7401" , " site" : " elsewhere" } } ) )
3482
.await;
3483
let ( st, _ ) = announce ( " runner-secret" , " fetching" ) .await;
3484
assert_eq! ( st, StatusCode:: CONFLICT ) ;
a46d15e7 Andy McCall 24 days ago ↶
3485
3486
3487
claim_as ( " runner-two" , " b" , serde_json:: json! ( { " images" : [ ] } ) ) .await;
3488
let ( st, body) = announce ( " runner-two" , " fetching" ) .await;
3489
assert_eq! ( st, StatusCode:: CONFLICT ) ;
3490
assert! ( body.contains ( " [runner.swarm]" ) , " {body}" ) ;
3491
let ( st, _ ) =
3492
call ( app, " POST" , " /api/v1/flow/swarm/announce" , Some ( " runner-secret" ) , Some ( r # "{"image":"nope","state":"fetching"}"# .into ( ) ) )
3493
.await;
3494
assert_eq! ( st, StatusCode:: BAD_REQUEST ) ;
3495
let ( st, _ ) = call (
3496
app,
3497
" POST" ,
3498
" /api/v1/flow/swarm/announce" ,
3499
None ,
3500
Some ( serde_json:: json! ( { " image" : image, " state" : " fetching" } ) .to_string ( ) ) ,
3501
)
3502
.await;
3503
assert_eq! ( st, StatusCode:: UNAUTHORIZED ) ;
3504
}
6ca0b971 Andy McCall 24 days ago ↶
3505
3506
3507
# [ tokio ::test ( flavor = " multi_thread" ) ]
3508
async fn the_images_page_says_where_each_image_is_and_what_uses_it ( ) {
3509
let h = hub ( ) .await;
3510
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3511
create ( & h.app, " andy" , " demo" ) .await;
3512
let work = tempfile:: tempdir( ) .unwrap ( ) ;
3513
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
3514
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3515
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
3516
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
3517
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3518
3519
let held = " cd" .repeat ( 32 ) ;
3520
let kept = " ef" .repeat ( 32 ) ;
3521
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 ) } } ) ;
3522
let ( st, out) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( body.to_string ( ) ) ) .await;
3523
assert! ( st = = StatusCode:: NO_CONTENT | | st = = StatusCode:: OK , " {st} {out}" ) ;
3b1bc27f Andy McCall 20 days ago
3524
sqlx:: query( " INSERT INTO jet_images (hash, size, arch, chunks, uploader, created_ms) VALUES ($1, 1048576, 'x86_64', 9, 'andy', 1)" )
6ca0b971 Andy McCall 24 days ago ↶
3525
.bind ( & kept)
3526
.execute ( & h.app.db)
3527
.await
3528
.unwrap ( ) ;
3529
3530
let res = oxygenhub:: router( h.app.clone ( ) )
3531
.oneshot (
3532
Request:: post( " /register" )
3533
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
c8684d44 Andy McCall 20 days ago
3534
.body ( Body:: from( " username=carol&email=carol%40example.com&password=correct-horse-battery&setup=test-setup" ) )
6ca0b971 Andy McCall 24 days ago ↶
3535
.unwrap ( ) ,
3536
)
3537
.await
3538
.unwrap ( ) ;
3539
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
3540
let carol = db:: accounts:: account_by_name( & h.app.db, " carol" ) .await.unwrap ( ) .unwrap ( ) ;
3541
db:: accounts:: set_admin( & h.app.db, carol.id, true ) .await.unwrap ( ) ;
3542
let res = oxygenhub:: router( h.app.clone ( ) )
3543
.oneshot ( Request:: get( " /admin/runners/images" ) .header ( header:: COOKIE , cookie) .body ( Body:: empty( ) ) .unwrap ( ) )
3544
.await
3545
.unwrap ( ) ;
3546
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
3547
let page = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
3548
let row = | hash : & str | {
3549
let at = page.find ( & format! ( " title=\" {hash} \" " ) ) .unwrap_or_else ( | | panic! ( " {hash} is not on the page" ) ) ;
3550
let end = page[ at.. ] .find ( " </tr>" ) .unwrap ( ) ;
3551
page[ at.. at + end] .to_string ( )
3552
} ;
3553
let pinned = row ( ROOTFS ) ;
3554
assert! ( pinned.contains ( " nowhere" ) & & pinned.contains ( " andy/demo" ) & & pinned.contains ( " 3 jobs" ) , " {pinned}" ) ;
3555
let on_runner = row ( & held) ;
3556
assert! ( on_runner.contains ( " img-holds" ) & & on_runner.contains ( " earth-1" ) & & ! on_runner.contains ( " nowhere" ) , " {on_runner}" ) ;
3557
let on_hub = row ( & kept) ;
3558
assert! ( on_hub.contains ( " img-hub" ) & & on_hub.contains ( " 1.0 MiB" ) & & on_hub.contains ( " x86_64" ) , " {on_hub}" ) ;
3559
assert! ( page.contains ( " earth-1 (home)" ) , " the swarm is named" ) ;
6246d1e5 Andy McCall 23 days ago ↶
3560
3561
assert! ( page.contains ( r # "{"images":"# ) & & page.contains ( r # ""scene":"swarm""# ) & & page.contains ( r # ""earth-1""# ) , " the swarm's data" ) ;
6ca0b971 Andy McCall 24 days ago ↶
3562
assert_eq! ( call ( & h.app, " GET" , " /admin/runners/images" , None , None ) .await.0 , StatusCode:: NOT_FOUND , " administrators only" ) ;
3563
}
f677d627 Andy McCall 24 days ago ↶
3564
3565
3566
3567
3568
# [ tokio ::test ( flavor = " multi_thread" ) ]
3569
async fn consumers_of_a_subrepo_are_indexed_with_how_far_behind_they_are ( ) {
3570
let h = hub ( ) .await;
e7158350 Andy McCall 24 days ago ↶
3571
3572
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
f677d627 Andy McCall 24 days ago ↶
3573
create ( & h.app, " andy" , " api" ) .await;
3574
create ( & h.app, " andy" , " game" ) .await;
3575
let api_work = tempfile:: tempdir( ) .unwrap ( ) ;
3576
let api = client ( & format! ( " {} /andy/api" , h.base) , api_work.path ( ) ) ;
3577
write ( api_work.path ( ) , " api.h" , " v1\n " ) ;
3578
api.snapshot ( " t" ) .unwrap ( ) ;
3579
api.commit ( " api v1" , & [ ] ) .unwrap ( ) ;
3580
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3581
3582
let game_work = tempfile:: tempdir( ) .unwrap ( ) ;
3583
let game = client ( & format! ( " {} /andy/game" , h.base) , game_work.path ( ) ) ;
3584
game.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
3585
write ( game_work.path ( ) , " game.c" , " int main;\n " ) ;
3586
game.snapshot ( " t" ) .unwrap ( ) ;
3587
game.commit ( " game on api v1" , & [ ] ) .unwrap ( ) ;
3588
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3589
3590
let consumers = | | async {
3591
let ( st, body) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , Some ( " site" ) , None ) .await;
3592
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3593
serde_json:: from_str:: < Vec< serde_json:: Value> > ( & body) .unwrap ( )
3594
} ;
3595
3596
3597
let settle = | want : & 'static ( dyn Fn( & [serde_json:: Value]) -> bool + Sync) | {
3598
let consumers = & consumers;
3599
async move {
3600
for _ in 0 .. 100 {
3601
let list = consumers ( ) .await;
3602
if want ( & list) {
3603
return list;
3604
}
3605
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3606
}
3607
consumers ( ) .await
3608
}
3609
} ;
3610
let list = settle ( & | l| ! l.is_empty ( ) ) .await;
3611
assert_eq! ( list.len ( ) , 1 , " {list:?}" ) ;
3612
assert_eq! (
3613
( list[ 0 ] [ " name" ] .as_str ( ) , list[ 0 ] [ " path" ] .as_str ( ) , list[ 0 ] [ " bookmark" ] .as_str ( ) ) ,
3614
( Some ( " game" ) , Some ( " deps/api" ) , Some ( " main" ) )
3615
) ;
3616
assert_eq! ( list[ 0 ] [ " behind" ] , 0 ) ;
3617
3618
3619
write ( api_work.path ( ) , " api.c" , " v2\n " ) ;
3620
api.snapshot ( " t" ) .unwrap ( ) ;
3621
api.commit ( " api v2" , & [ ] ) .unwrap ( ) ;
3622
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3623
assert_eq! ( settle ( & | l| l.first ( ) .is_some_and ( | c | c[ " behind" ] = = 1 ) ) .await[ 0 ] [ " behind" ] , 1 ) ;
3624
let ( _ , page) = call ( & h.app, " GET" , " /andy/api" , None , None ) .await;
c9aacc0e Andy McCall 23 days ago ↶
3625
assert! ( page.contains ( " Diffuses to" ) & & page.contains ( " andy/game" ) & & page.contains ( " 1 behind" ) , " the page shows it" ) ;
f677d627 Andy McCall 24 days ago ↶
3626
3627
3628
sqlx:: query( " UPDATE repositories SET visibility = 'private' WHERE name = 'game'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
3629
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , None , None ) .await;
3630
assert_eq! ( body.trim ( ) , " []" ) ;
3631
let ( _ , page) = call ( & h.app, " GET" , " /andy/api" , None , None ) .await;
3632
assert! ( ! page.contains ( " andy/game" ) ) ;
3633
sqlx:: query( " UPDATE repositories SET visibility = 'public' WHERE name = 'game'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
e7158350 Andy McCall 24 days ago ↶
3634
1d634f69 Andy McCall 23 days ago ↶
3635
3636
3637
let game_row = db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
3638
let api_row = db:: repos:: get( & h.app.db, " andy" , " api" ) .await.unwrap ( ) .unwrap ( ) ;
3639
let api_heads = api.bookmark_heads ( ) .unwrap ( ) ;
3640
let bumped = oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, api_heads.clone ( ) ) .await.unwrap ( ) ;
3641
let oxygenhub:: diffusion:: Bumped:: Moved { bookmark, heads, .. } = bumped else { panic! ( " {bumped:?}" ) } ;
3642
assert_eq! ( bookmark, " diffusion/deps/api" ) ;
3643
game.fetch ( " origin" ) .unwrap ( ) ;
3644
let bump_state = game.load_state ( & heads) .unwrap ( ) .materialize ( ) ;
3645
let pin = match & bump_state.files[ " deps/api" ] .content {
3646
oxy_core:: state:: FileContent:: Opaque { blobs } => oxy_core:: subrepo:: Pin:: decode( & game.read_blob ( & blobs[ 0 ] ) .unwrap ( ) ) .unwrap ( ) ,
3647
other => panic! ( " {other:?}" ) ,
3648
} ;
3649
assert! (
3650
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 } ) ,
3651
" {pin:?}"
3652
) ;
3653
assert! ( bump_state.files.contains_key ( " game.c" ) , " the rest of the game is as it was" ) ;
9aaa88b2 Andy McCall 23 days ago ↶
3654
assert! ( matches! (
1d634f69 Andy McCall 23 days ago ↶
3655
oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, api_heads.clone ( ) ) .await.unwrap ( ) ,
9aaa88b2 Andy McCall 23 days ago ↶
3656
oxygenhub:: diffusion:: Bumped:: Already { .. }
3657
) ) ;
7a14bdae Andy McCall 23 days ago ↶
3658
3659
3660
1d634f69 Andy McCall 23 days ago ↶
3661
write ( api_work.path ( ) , " other.c" , " v3\n " ) ;
3662
api.snapshot ( " t" ) .unwrap ( ) ;
3663
let v3 = api.commit ( " api v3, on its own" , & [ ] ) .unwrap ( ) ;
3664
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3665
let only_v3 = api.load_state ( & [ v3] ) .unwrap ( ) .heads ( ) .to_vec ( ) ;
7a14bdae Andy McCall 23 days ago ↶
3666
let combined = oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, only_v3) .await.unwrap ( ) ;
3667
let oxygenhub:: diffusion:: Bumped:: Moved { heads: ch, .. } = combined else { panic! ( " {combined:?}" ) } ;
3668
game.fetch ( " origin" ) .unwrap ( ) ;
3669
let st = game.load_state ( & ch) .unwrap ( ) .materialize ( ) ;
3670
let pin = match & st.files[ " deps/api" ] .content {
3671
oxy_core:: state:: FileContent:: Opaque { blobs } => oxy_core:: subrepo:: Pin:: decode( & game.read_blob ( & blobs[ 0 ] ) .unwrap ( ) ) .unwrap ( ) ,
3672
other => panic! ( " {other:?}" ) ,
3673
} ;
3674
let oxy_core:: subrepo:: Pin:: Oxy { heads: pinned, .. } = pin else { panic! ( ) } ;
3675
let all = api.load_state ( & pinned) .unwrap ( ) ;
3676
3677
assert! ( all.contains ( & v3) & & all.len ( ) = = 2 , " v1 and v3, {} changes" , all.len ( ) ) ;
1d634f69 Andy McCall 23 days ago ↶
3678
3679
let newer = api.bookmark_heads ( ) .unwrap ( ) ;
3680
let again = oxygenhub:: diffusion:: bump( & h.app, & game_row, " main" , " deps/api" , & api_row, newer) .await.unwrap ( ) ;
3681
assert! ( matches! ( again, oxygenhub:: diffusion:: Bumped:: Moved { ref bookmark, .. } if bookmark = = " diffusion/deps/api" ) , " {again:?}" ) ;
3682
e7158350 Andy McCall 24 days ago ↶
3683
3684
let put =
3685
| body : & 'static str , who : Option< & 'static str > | call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , who, Some ( body.into ( ) ) ) ;
3686
let ( st, body) = put ( r # "{"follow":"green"}"# , Some ( " site" ) ) .await;
3687
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3688
assert! ( body.contains ( " \" follow\" :\" green\" " ) & & body.contains ( " \" from\" :\" setting\" " ) , " {body}" ) ;
3689
assert_eq! ( put ( r # "{"follow":"sometimes"}"# , Some ( " site" ) ) .await.0 , StatusCode:: BAD_REQUEST ) ;
3690
assert_ne! ( put ( r # "{"follow":"any"}"# , None ) .await.0 , StatusCode:: OK , " not without a token" ) ;
3691
let ( _ , body) = put ( r # "{"follow":"default"}"# , Some ( " site" ) ) .await;
3692
assert! ( body.contains ( " \" follow\" :\" off\" " ) & & body.contains ( " \" from\" :\" site\" " ) , " {body}" ) ;
f677d627 Andy McCall 24 days ago ↶
3693
3694
3695
game.submodule_remove ( " deps/api" ) .unwrap ( ) ;
3696
std:: fs:: remove_dir_all( game_work.path ( ) .join ( " deps/api" ) ) .unwrap ( ) ;
3697
game.snapshot ( " t" ) .unwrap ( ) ;
3698
game.commit ( " game without api" , & [ ] ) .unwrap ( ) ;
3699
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3700
let left = settle ( & | l| l.is_empty ( ) ) .await;
3701
assert! ( left.is_empty ( ) , " {left:?}" ) ;
3702
}
a7e6e8e6 Andy McCall 23 days ago ↶
3703
3704
3705
3706
3707
# [ tokio ::test ( flavor = " multi_thread" ) ]
3708
async fn diffusion_proposes_a_bump_when_the_upstream_moves ( ) {
3709
let h = hub ( ) .await;
3710
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
3711
create ( & h.app, " andy" , " api" ) .await;
3712
create ( & h.app, " andy" , " game" ) .await;
180dc43a Andy McCall 23 days ago ↶
3713
3714
let seen: std:: sync:: Mutex< Vec< serde_json:: Value> > = std:: sync:: Mutex:: new( Vec :: new( ) ) ;
3715
let seen = & seen;
3716
let hub_app = & h.app;
3717
let pass_all = move | | async move {
3718
let h_app = hub_app;
a7e6e8e6 Andy McCall 23 days ago ↶
3719
for _ in 0 .. 20 {
180dc43a Andy McCall 23 days ago ↶
3720
let Some ( a) = claim ( h_app) .await else {
a7e6e8e6 Andy McCall 23 days ago ↶
3721
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
180dc43a Andy McCall 23 days ago ↶
3722
if ! jobs_left ( h_app) .await {
a7e6e8e6 Andy McCall 23 days ago ↶
3723
break ;
3724
}
3725
continue ;
3726
} ;
180dc43a Andy McCall 23 days ago ↶
3727
seen.lock ( ) .unwrap ( ) .push ( a.clone ( ) ) ;
a7e6e8e6 Andy McCall 23 days ago ↶
3728
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
3729
let ( name, ph) = ( a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
f7bcee49 Andy McCall 22 days ago ↶
3730
let st = finish ( h_app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, 0 ) } ) ) .await;
3731
3732
3733
3734
if st = = StatusCode:: CONFLICT {
3735
let job = db:: flow:: job( & h_app.db, id) .await.unwrap ( ) .unwrap ( ) ;
3736
assert_eq! ( job.status, " cancelled" , " refused only because a newer run cancelled it" ) ;
3737
continue ;
3738
}
3739
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
a7e6e8e6 Andy McCall 23 days ago ↶
3740
}
3741
} ;
3742
let api_work = tempfile:: tempdir( ) .unwrap ( ) ;
3743
let api = client ( & format! ( " {} /andy/api" , h.base) , api_work.path ( ) ) ;
3744
write ( api_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3745
write ( api_work.path ( ) , " test.sh" , " echo ok\n " ) ;
3746
write ( api_work.path ( ) , " api.h" , " v1\n " ) ;
3747
api.snapshot ( " t" ) .unwrap ( ) ;
3748
api.commit ( " api v1" , & [ ] ) .unwrap ( ) ;
3749
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3750
pass_all ( ) .await;
3751
3752
let game_work = tempfile:: tempdir( ) .unwrap ( ) ;
3753
let game = client ( & format! ( " {} /andy/game" , h.base) , game_work.path ( ) ) ;
3754
game.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
3755
write ( game_work.path ( ) , " game.c" , " int main;\n " ) ;
f01cb865 Andy McCall 23 days ago ↶
3756
write ( game_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
3757
write ( game_work.path ( ) , " test.sh" , " echo ok\n " ) ;
a7e6e8e6 Andy McCall 23 days ago ↶
3758
game.snapshot ( " t" ) .unwrap ( ) ;
3759
game.commit ( " game on api v1" , & [ ] ) .unwrap ( ) ;
3760
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3761
let game_row = db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
3762
let bump = | | async {
3763
for _ in 0 .. 100 {
3764
let open = db:: proposals:: list( & h.app.db, game_row.id, & [ " open" ] ) .await.unwrap ( ) ;
3765
if let Some ( p) = open.into_iter ( ) .find ( | p | p.source_bookmark = = " diffusion/deps/api" ) {
3766
return Some ( p) ;
3767
}
3768
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3769
}
3770
None
3771
} ;
3772
3773
3774
write ( api_work.path ( ) , " a2.c" , " v2\n " ) ;
3775
api.snapshot ( " t" ) .unwrap ( ) ;
3776
api.commit ( " api v2: faster sprites" , & [ ] ) .unwrap ( ) ;
3777
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3778
pass_all ( ) .await;
3779
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
3780
assert! ( db:: proposals:: list( & h.app.db, game_row.id, & [ ] ) .await.unwrap ( ) .is_empty ( ) , " off proposes nothing" ) ;
3781
3776b70d Andy McCall 23 days ago ↶
3782
3783
a7e6e8e6 Andy McCall 23 days ago ↶
3784
let put = | body : & 'static str | call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , Some ( body.into ( ) ) ) ;
3785
assert_eq! ( put ( r # "{"follow":"any"}"# ) .await.0 , StatusCode:: OK ) ;
3776b70d Andy McCall 23 days ago ↶
3786
let first = bump ( ) .await.expect ( " caught up when turned on" ) ;
3787
assert! ( first.title.contains ( " 1 change" ) & & first.body.contains ( " api v2: faster sprites" ) , " {:?}" , first.title) ;
f01cb865 Andy McCall 23 days ago ↶
3788
3789
let mut ran = false ;
3790
for _ in 0 .. 100 {
3791
if runs_of ( & h.app, " andy" , " game" ) .await.iter ( ) .any ( | r | r.bookmark = = " diffusion/deps/api" ) {
3792
ran = true ;
3793
break ;
3794
}
3795
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3796
}
3797
assert! ( ran, " a run of the bump" ) ;
3798
pass_all ( ) .await;
a7e6e8e6 Andy McCall 23 days ago ↶
3799
write ( api_work.path ( ) , " a3.c" , " v3\n " ) ;
3800
api.snapshot ( " t" ) .unwrap ( ) ;
3801
api.commit ( " api v3: save slots past ten" , & [ ] ) .unwrap ( ) ;
3802
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3776b70d Andy McCall 23 days ago ↶
3803
let mut p = first.clone ( ) ;
3804
for _ in 0 .. 100 {
3805
p = bump ( ) .await.unwrap ( ) ;
3806
if p.body.contains ( " api v3" ) {
3807
break ;
3808
}
3809
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3810
}
3811
assert_eq! ( p.number, first.number, " the same proposal" ) ;
a7e6e8e6 Andy McCall 23 days ago ↶
3812
assert_eq! ( ( p.author.as_str ( ) , p.target.as_str ( ) ) , ( " diffusion" , " main" ) ) ;
3813
assert! ( p.title.contains ( " deps/api" ) & & p.title.contains ( " andy/api" ) & & p.title.contains ( " 2 changes" ) , " {}" , p.title) ;
3814
assert! ( p.body.contains ( " api v2: faster sprites" ) & & p.body.contains ( " api v3: save slots past ten" ) , " {}" , p.body) ;
3815
let labels = db:: issues:: proposal_labels( & h.app.db, p.id) .await.unwrap ( ) ;
3816
assert! ( labels.iter ( ) .any ( | l | l.name = = " dependencies" ) ) ;
3817
pass_all ( ) .await;
3818
3819
3820
3821
assert_eq! ( put ( r # "{"follow":"green"}"# ) .await.0 , StatusCode:: OK ) ;
3822
write ( api_work.path ( ) , " a4.c" , " v4\n " ) ;
3823
api.snapshot ( " t" ) .unwrap ( ) ;
3824
api.commit ( " api v4: rsm_set cheaper" , & [ ] ) .unwrap ( ) ;
3825
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3826
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 300 ) ) .await;
3827
assert! ( ! bump ( ) .await.unwrap ( ) .body.contains ( " api v4" ) , " not before its run passes" ) ;
3828
pass_all ( ) .await;
3829
let mut updated = None ;
3830
for _ in 0 .. 100 {
3831
let p2 = bump ( ) .await.unwrap ( ) ;
3832
if p2.body.contains ( " api v4" ) {
3833
updated = Some ( p2) ;
3834
break ;
3835
}
3836
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3837
}
3838
let p2 = updated.expect ( " brought up to date once the run passed" ) ;
3839
assert_eq! ( p2.number, p.number, " the same proposal" ) ;
3840
assert! ( p2.title.contains ( " 3 changes" ) , " {}" , p2.title) ;
3841
assert_eq! ( db:: proposals:: list( & h.app.db, game_row.id, & [ ] ) .await.unwrap ( ) .len ( ) , 1 , " never a pile of bumps" ) ;
3776b70d Andy McCall 23 days ago ↶
3842
757195ad Andy McCall 23 days ago ↶
3843
3844
3845
pass_all ( ) .await;
3846
let ( st, page) = call ( & h.app, " GET" , " /andy/api/diffusion" , None , None ) .await;
3847
assert_eq! ( st, StatusCode:: OK ) ;
c9aacc0e Andy McCall 23 days ago ↶
3848
assert! ( page.contains ( " Diffuses to" ) & & page.contains ( " andy/game" ) & & page.contains ( & format! ( " #{} " , p2.number) ) , " the bump is listed" ) ;
757195ad Andy McCall 23 days ago ↶
3849
assert! ( page.contains ( " df-green" ) , " a passing run of a checked upstream is green" ) ;
6246d1e5 Andy McCall 23 days ago ↶
3850
3851
assert! (
3852
page.contains ( r # ""scene":"ripples""# ) & & page.contains ( r # "["andy/game","# ) & & page.contains ( r # ""repo":"andy/api""# ) ,
3853
" the ripples' data"
3854
) ;
757195ad Andy McCall 23 days ago ↶
3855
let ( _ , page) = call ( & h.app, " GET" , " /andy/game/diffusion" , None , None ) .await;
c9aacc0e Andy McCall 23 days ago ↶
3856
assert! ( page.contains ( " Takes from" ) & & page.contains ( " andy/api" ) ) ;
757195ad Andy McCall 23 days ago ↶
3857
let ( _ , overview) = call ( & h.app, " GET" , " /andy/game" , None , None ) .await;
c9aacc0e Andy McCall 23 days ago ↶
3858
assert! ( overview.contains ( " Takes from" ) & & overview.contains ( " >Diffusion</a>" ) , " the tab and the panel" ) ;
eb2a3502 Andy McCall 23 days ago ↶
3859
3860
let ( st, body) = call ( & h.app, " GET" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , None ) .await;
3861
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3862
let d: hub_api:: Diffusion = serde_json:: from_str( & body) .unwrap ( ) ;
3863
assert! ( d.diffuses_to.is_empty ( ) , " nothing pins the game" ) ;
3864
let pin = d.takes_from.first ( ) .expect ( " the game takes from the api" ) ;
3865
assert_eq! ( ( pin.upstream.as_str ( ) , pin.consumer.as_str ( ) ) , ( " andy/api" , " andy/game" ) ) ;
3866
assert_eq! ( pin.bump, Some ( p2.number) ) ;
3867
assert_eq! ( pin.upstream_state.tone, " green" , " {body}" ) ;
3868
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/diffusion" , Some ( " site" ) , None ) .await;
3869
let d: hub_api:: Diffusion = serde_json:: from_str( & body) .unwrap ( ) ;
3870
assert! ( d.takes_from.is_empty ( ) & & d.diffuses_to.iter ( ) .any ( | p | p.consumer = = " andy/game" ) , " {body}" ) ;
757195ad Andy McCall 23 days ago ↶
3871
let ( st, _ ) = call ( & h.app, " GET" , " /admin/diffusion" , None , None ) .await;
3872
assert_eq! ( st, StatusCode:: NOT_FOUND , " administrators only" ) ;
3873
3874
5d54cf74 Andy McCall 20 days ago ↶
3875
3876
3877
let game = oxygenhub:: db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
3878
oxygenhub:: diffusion:: fail_next_records( & h.app, game.id, 1 ) ;
757195ad Andy McCall 23 days ago ↶
3879
let ( st, body) =
3880
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/game/proposals/{} /land" , p2.number) , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
3881
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
3882
let mut behind = serde_json:: Value:: Null;
3883
for _ in 0 .. 100 {
3884
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , Some ( " site" ) , None ) .await;
3885
let list: Vec< serde_json:: Value> = serde_json:: from_str( & list) .unwrap ( ) ;
3886
behind = list.iter ( ) .find ( | c | c[ " name" ] = = " game" ) .map ( | c | c[ " behind" ] .clone ( ) ) .unwrap_or_default ( ) ;
3887
if behind = = 0 {
3888
break ;
3889
}
3890
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3891
}
3892
assert_eq! ( behind, 0 , " the index followed the landing" ) ;
5d54cf74 Andy McCall 20 days ago ↶
3893
assert_eq! ( oxygenhub:: diffusion:: forced_left( & h.app, game.id) , 0 , " the first write did fail" ) ;
0e04dfc2 Andy McCall 20 days ago ↶
3894
3895
3896
3897
let pins_of = | | async {
3b1bc27f Andy McCall 20 days ago
3898
sqlx:: query_scalar:: < _ , String> ( " SELECT heads FROM diffusion_pins WHERE consumer_id = $1" )
0e04dfc2 Andy McCall 20 days ago ↶
3899
.bind ( game.id)
3900
.fetch_one ( & h.app.db)
3901
.await
3902
.unwrap ( )
3903
} ;
3904
let right = pins_of ( ) .await;
3b1bc27f Andy McCall 20 days ago
3905
sqlx:: query( " UPDATE diffusion_pins SET heads = 'stale' WHERE consumer_id = $1" ) .bind ( game.id) .execute ( & h.app.db) .await.unwrap ( ) ;
3906
sqlx:: query( " DELETE FROM diffusion_indexed WHERE consumer_id = $1" ) .bind ( game.id) .execute ( & h.app.db) .await.unwrap ( ) ;
0e04dfc2 Andy McCall 20 days ago ↶
3907
let r = oxygenhub:: diffusion:: reconcile( & h.app) .await;
3908
assert! ( r.reindexed > = 1 , " {r:?}" ) ;
3909
assert_eq! ( pins_of ( ) .await, right, " the pin read again from the game's bookmark" ) ;
3910
assert_eq! ( oxygenhub:: diffusion:: reconcile( & h.app) .await.reindexed, 0 , " nothing left to mend" ) ;
757195ad Andy McCall 23 days ago ↶
3911
180dc43a Andy McCall 23 days ago ↶
3912
3913
3914
sqlx:: query( " UPDATE repositories SET visibility = 'private' WHERE name = 'api'" ) .execute ( & h.app.db) .await.unwrap ( ) ;
3915
seen.lock ( ) .unwrap ( ) .clear ( ) ;
3916
write ( api_work.path ( ) , " a6.c" , " v6\n " ) ;
3917
api.snapshot ( " t" ) .unwrap ( ) ;
3918
api.commit ( " api v6" , & [ ] ) .unwrap ( ) ;
3919
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4fa0da12 Andy McCall 23 days ago ↶
3920
3921
3922
let mut bump_job = None ;
3923
for _ in 0 .. 100 {
3924
pass_all ( ) .await;
3925
bump_job = seen.lock ( ) .unwrap ( ) .iter ( ) .find ( | c | c[ " repo" ] = = " andy/game" & & c[ " bookmark" ] = = " diffusion/deps/api" ) .cloned ( ) ;
3926
if bump_job.is_some ( ) {
3927
break ;
3928
}
3929
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 100 ) ) .await;
3930
}
3931
let bump_job = bump_job.expect ( " the bump's job ran" ) ;
180dc43a Andy McCall 23 days ago ↶
3932
let subs: Vec< & str > = bump_job[ " subrepos" ] .as_array ( ) .unwrap ( ) .iter ( ) .filter_map ( | s | s[ " path" ] .as_str ( ) ) .collect ( ) ;
3933
assert_eq! ( subs, vec! [ " deps/api" ] , " {bump_job}" ) ;
3934
3776b70d Andy McCall 23 days ago ↶
3935
3936
3937
create ( & h.app, " andy" , " game2" ) .await;
3938
let g2_work = tempfile:: tempdir( ) .unwrap ( ) ;
3939
let g2 = client ( & format! ( " {} /andy/game2" , h.base) , g2_work.path ( ) ) ;
3940
g2.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
3941
g2.snapshot ( " t" ) .unwrap ( ) ;
3942
g2.commit ( " game2 on api" , & [ ] ) .unwrap ( ) ;
3943
assert! ( g2.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
3944
3945
write ( api_work.path ( ) , " a5.c" , " v5\n " ) ;
3946
api.snapshot ( " t" ) .unwrap ( ) ;
3947
api.commit ( " api v5" , & [ ] ) .unwrap ( ) ;
7e01a766 Andy McCall 20 days ago ↶
3948
let api_row = db:: repos:: get( & h.app.db, " andy" , " api" ) .await.unwrap ( ) .unwrap ( ) ;
3949
let runs_before = db:: flow:: runs( & h.app.db, api_row.id, 100 , None ) .await.unwrap ( ) .len ( ) ;
3776b70d Andy McCall 23 days ago ↶
3950
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
7e01a766 Andy McCall 20 days ago ↶
3951
3952
3953
for _ in 0 .. 300 {
3954
if db:: flow:: runs( & h.app.db, api_row.id, 100 , None ) .await.unwrap ( ) .len ( ) > runs_before {
3955
break ;
3956
}
3957
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 20 ) ) .await;
3958
}
3776b70d Andy McCall 23 days ago ↶
3959
pass_all ( ) .await;
3960
let g2_row = db:: repos:: get( & h.app.db, " andy" , " game2" ) .await.unwrap ( ) .unwrap ( ) ;
3961
for _ in 0 .. 100 {
3b1bc27f Andy McCall 20 days ago
3962
if sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = $1" )
3776b70d Andy McCall 23 days ago ↶
3963
.bind ( g2_row.id)
3964
.fetch_one ( & h.app.db)
3965
.await
3966
.unwrap ( )
3967
> 0
3968
{
3969
break ;
3970
}
3971
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
3972
}
3973
assert! ( db:: proposals:: list( & h.app.db, g2_row.id, & [ ] ) .await.unwrap ( ) .is_empty ( ) , " off until turned on" ) ;
3974
let ( st, _ ) = call ( & h.app, " PUT" , " /api/v1/repos/andy/game2/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green"}"# .into ( ) ) ) .await;
3975
assert_eq! ( st, StatusCode:: OK ) ;
3976
let mut caught = None ;
5d54cf74 Andy McCall 20 days ago ↶
3977
3978
for _ in 0 .. 300 {
3776b70d Andy McCall 23 days ago ↶
3979
if let Some ( p) = db:: proposals:: list( & h.app.db, g2_row.id, & [ " open" ] ) .await.unwrap ( ) .into_iter ( ) .next ( ) {
3980
caught = Some ( p) ;
3981
break ;
3982
}
3983
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
3984
}
3985
let caught = caught.expect ( " bumped as soon as it was turned on" ) ;
3986
assert! ( caught.body.contains ( " api v5" ) & & caught.title.contains ( " 1 change" ) , " {:?} {:?}" , caught.title, caught.body) ;
0e04dfc2 Andy McCall 20 days ago ↶
3987
3b1bc27f Andy McCall 20 days ago
3988
sqlx:: query( " UPDATE proposals SET state = 'closed' WHERE id = $1" ) .bind ( caught.id) .execute ( & h.app.db) .await.unwrap ( ) ;
62cdf34d Andy McCall 20 days ago ↶
3989
3990
0e04dfc2 Andy McCall 20 days ago ↶
3991
let r = oxygenhub:: diffusion:: reconcile( & h.app) .await;
62cdf34d Andy McCall 20 days ago ↶
3992
assert_eq! ( r.caught_up, 0 , " the reconciler respects the closed bump: {r:?}" ) ;
a7e6e8e6 Andy McCall 23 days ago ↶
3993
}
3994
3995
3996
async fn jobs_left ( app : & Shared) -> bool {
3997
sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM flow_jobs WHERE status IN ('queued', 'running')" )
3998
.fetch_one ( & app.db)
3999
.await
4000
.unwrap_or ( 0 )
4001
> 0
9aaa88b2 Andy McCall 23 days ago ↶
4002
}
4003
4004
4005
4006
# [ tokio ::test ( flavor = " multi_thread" ) ]
4007
async fn bumps_from_an_upstream_without_checks_are_marked_unverified ( ) {
4008
let h = hub ( ) .await;
4009
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4010
create ( & h.app, " andy" , " lib" ) .await;
4011
create ( & h.app, " andy" , " app" ) .await;
4012
let lib_work = tempfile:: tempdir( ) .unwrap ( ) ;
4013
let lib = client ( & format! ( " {} /andy/lib" , h.base) , lib_work.path ( ) ) ;
4014
write ( lib_work.path ( ) , " lib.h" , " v1\n " ) ;
4015
lib.snapshot ( " t" ) .unwrap ( ) ;
4016
lib.commit ( " lib v1" , & [ ] ) .unwrap ( ) ;
4017
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4018
let app_work = tempfile:: tempdir( ) .unwrap ( ) ;
4019
let app = client ( & format! ( " {} /andy/app" , h.base) , app_work.path ( ) ) ;
4020
app.submodule_add ( & format! ( " {} /andy/lib" , h.base) , " deps/lib" , Some ( " main" ) ) .unwrap ( ) ;
c4e432da Andy McCall 23 days ago ↶
4021
4022
let modules = std:: fs:: read_to_string( app_work.path ( ) .join ( " .oxymodules" ) ) .unwrap ( ) ;
4023
write ( app_work.path ( ) , " .oxymodules" , & format! ( " {modules} watch = lib.c\n " ) ) ;
9aaa88b2 Andy McCall 23 days ago ↶
4024
app.snapshot ( " t" ) .unwrap ( ) ;
4025
app.commit ( " app on lib" , & [ ] ) .unwrap ( ) ;
4026
assert! ( app.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4027
let app_row = db:: repos:: get( & h.app.db, " andy" , " app" ) .await.unwrap ( ) .unwrap ( ) ;
4028
for _ in 0 .. 100 {
3b1bc27f Andy McCall 20 days ago
4029
if sqlx:: query_scalar:: < _ , i64 > ( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = $1" )
9aaa88b2 Andy McCall 23 days ago ↶
4030
.bind ( app_row.id)
4031
.fetch_one ( & h.app.db)
4032
.await
4033
.unwrap ( )
4034
> 0
4035
{
4036
break ;
4037
}
4038
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4039
}
4040
let ( st, _ ) = call ( & h.app, " PUT" , " /api/v1/repos/andy/app/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green"}"# .into ( ) ) ) .await;
4041
assert_eq! ( st, StatusCode:: OK ) ;
4042
write ( lib_work.path ( ) , " lib.c" , " v2\n " ) ;
4043
lib.snapshot ( " t" ) .unwrap ( ) ;
4044
lib.commit ( " lib v2" , & [ ] ) .unwrap ( ) ;
4045
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4046
let mut found = None ;
4047
for _ in 0 .. 100 {
4048
if let Some ( p) = db:: proposals:: list( & h.app.db, app_row.id, & [ " open" ] ) .await.unwrap ( ) .into_iter ( ) .next ( ) {
4049
found = Some ( p) ;
4050
break ;
4051
}
4052
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4053
}
4054
let p = found.expect ( " a bump although the upstream never goes green" ) ;
4055
assert! ( p.body.contains ( " Upstream unverified" ) , " {}" , p.body) ;
c4e432da Andy McCall 23 days ago ↶
4056
assert! ( p.body.contains ( " Touches what you watch" ) & & p.body.contains ( " touches `lib.c`" ) & & p.body.contains ( " ⚑" ) , " {}" , p.body) ;
9aaa88b2 Andy McCall 23 days ago ↶
4057
let mut labels = Vec :: new( ) ;
4058
for _ in 0 .. 100 {
4059
labels = db:: issues:: proposal_labels( & h.app.db, p.id) .await.unwrap ( ) .into_iter ( ) .map ( | l | l.name ) .collect:: < Vec< String> > ( ) ;
4060
if labels.len ( ) = = 2 {
4061
break ;
4062
}
4063
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4064
}
4065
assert! ( labels.contains ( & " upstream unverified" .to_string ( ) ) & & labels.contains ( & " dependencies" .to_string ( ) ) , " {labels:?}" ) ;
a7e6e8e6 Andy McCall 23 days ago ↶
4066
}
d943160d Andy McCall 23 days ago ↶
4067
4068
4069
4070
# [ tokio ::test ( flavor = " multi_thread" ) ]
4071
async fn a_moved_target_runs_its_open_proposals_again ( ) {
4072
let h = hub ( ) .await;
4073
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4074
create ( & h.app, " andy" , " demo" ) .await;
4075
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4076
let mut c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
4077
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4078
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
4079
c.snapshot ( " t" ) .unwrap ( ) ;
4080
let base = c.commit ( " base" , & [ ] ) .unwrap ( ) ;
4081
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4082
4083
for ( b, file) in [ ( " fix-a" , " a.txt" ) , ( " fix-b" , " b.txt" ) ] {
4084
c.bookmark_create ( b, Some ( & [ base] ) ) .unwrap ( ) ;
4085
c.bookmark_switch ( b, true ) .unwrap ( ) ;
4086
write ( work.path ( ) , file, " x\n " ) ;
4087
c.snapshot ( " t" ) .unwrap ( ) ;
4088
c.commit ( & format! ( " add {file} " ) , & [ ] ) .unwrap ( ) ;
4089
assert! ( c.push_bookmark ( " origin" , b, false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4090
let body = serde_json:: json! ( { " title" : b, " target" : " main" , " source" : b } ) .to_string ( ) ;
4091
let ( st, out) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/proposals" , Some ( " site" ) , Some ( body) ) .await;
4092
assert_eq! ( st, StatusCode:: CREATED , " {out}" ) ;
4093
}
4094
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
4095
let props = db:: proposals:: list( & h.app.db, row.id, & [ " open" ] ) .await.unwrap ( ) ;
4096
let a = props.iter ( ) .find ( | p | p.source_bookmark = = " fix-a" ) .unwrap ( ) .number;
4097
let b = props.iter ( ) .find ( | p | p.source_bookmark = = " fix-b" ) .unwrap ( ) .number;
4098
4099
let ( st, out) = call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/demo/proposals/{a} /land" ) , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
4100
assert_eq! ( st, StatusCode:: OK , " {out}" ) ;
4101
let label = format! ( " proposal/{b} " ) ;
4102
let mut found = None ;
4103
for _ in 0 .. 100 {
4104
if let Some ( r) = runs_of ( & h.app, " andy" , " demo" ) .await.into_iter ( ) .find ( | r | r.bookmark = = label ) {
4105
found = Some ( r) ;
4106
break ;
4107
}
4108
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4109
}
4110
let run = found.expect ( " a run of fix-b's new projected state" ) ;
4111
let projected = db:: proposals:: get( & h.app.db, row.id, b) .await.unwrap ( ) .unwrap ( ) ;
4112
assert_eq! ( ( run.trigger.as_str ( ) , run.state.as_str ( ) ) , ( " proposal" , projected.projected_state.as_str ( ) ) , " on its projected state" ) ;
4113
4114
let before = runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) ;
4115
oxygenhub:: web:: proposals:: settle_after_push( & h.app, & row, " main" , " andy" ) .await;
4116
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , before, " not again for the same state" ) ;
4117
}
93501da2 Andy McCall 22 days ago ↶
4118
4119
4120
4121
async fn send_image ( app : & Shared, image : & [u8 ]) -> ( String, serde_json:: Value) {
4122
let mut chunks = std:: collections:: HashMap:: new( ) ;
4123
let m = jet:: images:: scan( image, None , | c , b | {
4124
chunks.insert ( c.hash ( ) .to_string ( ) , b.to_vec ( ) ) ;
4125
Ok ( ( ) )
4126
} )
4127
.unwrap ( ) ;
4128
for c in m.needed ( ) {
4129
let ( st, body) = put_bytes ( app, & format! ( " /api/v1/flow/images/chunks/{c} " ) , " site" , chunks[ c] .clone ( ) ) .await;
4130
assert_eq! ( st, StatusCode:: NO_CONTENT , " {body}" ) ;
4131
}
4132
let ( st, body) =
4133
call ( app, " PUT" , & format! ( " /api/v1/flow/images/{} " , m.hash) , Some ( " site" ) , Some ( serde_json:: to_string( & m) .unwrap ( ) ) ) .await;
4134
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
4135
( m.hash.clone ( ) , serde_json:: from_str( & body) .unwrap ( ) )
4136
}
4137
4138
4139
4140
4141
4142
# [ tokio ::test ( flavor = " multi_thread" ) ]
4143
async fn an_image_names_its_own_architecture_and_a_pass_teaches_the_rest ( ) {
4144
use oxy_core:: identity:: SecretKey;
4145
let h = hub ( ) .await;
4146
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4147
4148
let dir = tempfile:: tempdir( ) .unwrap ( ) ;
4149
let tree = dir.path ( ) .join ( " tree" ) ;
4150
std:: fs:: create_dir_all( tree.join ( " bin" ) ) .unwrap ( ) ;
4151
let mut elf = vec! [ 0 u8 ; 64 ] ;
4152
elf[ .. 7 ] .copy_from_slice ( b " \x7f ELF\x02 \x01 \x01 " ) ;
4153
elf[ 18 .. 20 ] .copy_from_slice ( & 0x3E u16 .to_le_bytes ( ) ) ;
4154
std:: fs:: write( tree.join ( " bin/sh" ) , & elf) .unwrap ( ) ;
4155
let img = dir.path ( ) .join ( " rootfs.ext4" ) ;
4156
let made = std:: process:: Command:: new( " mkfs.ext4" ) .args ( [ " -q" , " -F" , " -d" ] ) .arg ( & tree) .arg ( & img) .arg ( " 8M" ) .status ( ) ;
4157
if ! made.is_ok_and ( | s | s.success ( ) ) {
4158
eprintln! ( " mkfs.ext4 not found: skipped" ) ;
4159
return ;
4160
}
4161
let ( x86, answer) = send_image ( & h.app, & std:: fs:: read( & img) .unwrap ( ) ) .await;
4162
assert_eq! ( answer[ " arch" ] , " x86_64" , " read from its shell: {answer}" ) ;
4163
let ( _ , body) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/images/{x86} " ) , Some ( " runner-secret" ) , None ) .await;
4164
assert_eq! ( serde_json:: from_str:: < jet:: images:: Manifest> ( & body) .unwrap ( ) .arch.as_deref ( ) , Some ( " x86_64" ) , " runners fetch it named" ) ;
4165
4166
let blob: Vec< u8 > = ( 0 .. 300_000 u32 ) .map ( | i | ( i % 241 ) as u8 ) .collect ( ) ;
4167
let ( unread, answer) = send_image ( & h.app, & blob) .await;
4168
assert! ( answer[ " arch" ] .is_null ( ) , " {answer}" ) ;
4169
let archs = db:: flow:: image_archs( & h.app.db) .await.unwrap ( ) ;
4170
assert_eq! ( ( archs.get ( & x86) .map ( String :: as_str) , archs.get ( & unread) ) , ( Some ( " x86_64" ) , None ) ) ;
4171
4172
sqlx:: query( " UPDATE jet_images SET arch = ''" ) .execute ( & h.app.db) .await.unwrap ( ) ;
4173
sqlx:: query( " DELETE FROM flow_images" ) .execute ( & h.app.db) .await.unwrap ( ) ;
4174
assert_eq! ( oxygenhub:: images:: learn_archs( & h.app) .await.unwrap ( ) , 1 ) ;
4175
assert_eq! ( db:: flow:: image_archs( & h.app.db) .await.unwrap ( ) .get ( & x86) .map ( String :: as_str) , Some ( " x86_64" ) ) ;
4176
4177
4178
create ( & h.app, " andy" , " arch" ) .await;
4179
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4180
let c = client ( & format! ( " {} /andy/arch" , h.base) , work.path ( ) ) ;
4181
let kdl = format! (
4182
" schema-version 1\n sandbox \" unread\" {{ \n rootfs \" cas://blob/{unread} \" \n }} \n sandbox \" x86\" {{ \n rootfs \" cas://blob/{x86} \" \n }} \n \
4183
job \" first\" {{ \n sandbox \" unread\" \n exec \" make\" \n }} \n job \" second\" {{ \n sandbox \" x86\" \n exec \" make\" \n depends-on \" first\" \n }} \n \
4184
job \" third\" {{ \n sandbox \" unread\" \n exec \" make\" \n depends-on \" first\" \n }} \n "
4185
) ;
4186
write ( work.path ( ) , " .flow/pipeline.kdl" , & kdl) ;
4187
c.commit ( " Jobs on two images" , & [ ] ) .unwrap ( ) ;
4188
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4189
runs_of ( & h.app, " andy" , " arch" ) .await;
4190
4191
4192
let x86_info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
4193
let x86_claim =
4194
serde_json:: json! ( { " runner" : " earth" , " runtimes" : [ " microvm" ] , " images" : [ ] , " fetches_images" : true , " info" : x86_info } ) ;
4195
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( x86_claim.to_string ( ) ) ) .await;
4196
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4197
let first: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
4198
assert_eq! ( first[ " job" ] [ " name" ] , " first" ) ;
4199
let ph = first[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) ;
4200
let id = first[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4201
assert_eq! (
4202
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : receipt ( " first" , ph, 0 ) } ) ) .await,
4203
StatusCode:: NO_CONTENT
4204
) ;
4205
assert_eq! ( db:: flow:: image_archs( & h.app.db) .await.unwrap ( ) .get ( & unread) .map ( String :: as_str) , Some ( " x86_64" ) , " learned" ) ;
4206
4207
4208
let token = " flowreg_narvi" ;
4209
db:: runners:: add_registration( & h.app.db, & blake3:: hash( token.as_bytes ( ) ) .to_hex ( ) , " admin" , & Default :: default( ) ) .await.unwrap ( ) ;
4210
let identity = SecretKey:: generate( ) .identity ( " narvi" , " " , 1 ) ;
4211
let info = serde_json:: json! ( { " arch" : " riscv64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
4212
let reg = serde_json:: json! ( { " token" : token, " name" : " narvi" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
4213
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
4214
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4215
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
4216
let narvi =
4217
serde_json:: json! ( { " runner" : " narvi" , " runtimes" : [ " microvm" ] , " images" : [ ] , " fetches_images" : true , " info" : info } ) .to_string ( ) ;
4218
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( & credential) , Some ( narvi) ) .await;
4219
assert_eq! ( st, StatusCode:: NO_CONTENT , " the second job's image is x86_64 and the third's learned so: {body}" ) ;
4220
for _ in 0 .. 2 {
4221
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( " runner-secret" ) , Some ( x86_claim.to_string ( ) ) ) .await;
4222
assert_eq! ( st, StatusCode:: OK , " both go to x86_64: {body}" ) ;
4223
}
4224
}
f4d6ef41 Andy McCall 22 days ago ↶
4225
4226
4227
4228
4229
4230
# [ tokio ::test ( flavor = " multi_thread" ) ]
4231
async fn a_consumer_that_chose_auto_lands_green_bumps_itself ( ) {
4232
let h = hub ( ) .await;
4233
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4234
create ( & h.app, " andy" , " api" ) .await;
4235
create ( & h.app, " andy" , " game" ) .await;
4236
let hub_app = & h.app;
4237
4238
let pass_all = move | | async move {
4239
for _ in 0 .. 20 {
4240
let Some ( a) = claim ( hub_app) .await else {
4241
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4242
if ! jobs_left ( hub_app) .await {
4243
break ;
4244
}
4245
continue ;
4246
} ;
4247
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4248
let ( name, ph) = ( a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4249
assert_eq! (
4250
finish ( hub_app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, 0 ) } ) ) .await,
4251
StatusCode:: NO_CONTENT
4252
) ;
4253
}
4254
} ;
4255
let api_work = tempfile:: tempdir( ) .unwrap ( ) ;
4256
let api = client ( & format! ( " {} /andy/api" , h.base) , api_work.path ( ) ) ;
4257
write ( api_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4258
write ( api_work.path ( ) , " test.sh" , " echo ok\n " ) ;
4259
api.snapshot ( " t" ) .unwrap ( ) ;
4260
api.commit ( " api v1" , & [ ] ) .unwrap ( ) ;
4261
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4262
pass_all ( ) .await;
4263
let game_work = tempfile:: tempdir( ) .unwrap ( ) ;
4264
let game = client ( & format! ( " {} /andy/game" , h.base) , game_work.path ( ) ) ;
4265
game.submodule_add ( & format! ( " {} /andy/api" , h.base) , " deps/api" , Some ( " main" ) ) .unwrap ( ) ;
4266
write ( game_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4267
write ( game_work.path ( ) , " test.sh" , " echo ok\n " ) ;
4268
game.snapshot ( " t" ) .unwrap ( ) ;
4269
game.commit ( " game on api v1" , & [ ] ) .unwrap ( ) ;
4270
assert! ( game.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4271
pass_all ( ) .await;
4272
let game_row = db:: repos:: get( & h.app.db, " andy" , " game" ) .await.unwrap ( ) .unwrap ( ) ;
4273
for _ in 0 .. 100 {
3b1bc27f Andy McCall 20 days ago
4274
let n: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = $1" )
f4d6ef41 Andy McCall 22 days ago ↶
4275
.bind ( game_row.id)
4276
.fetch_one ( & h.app.db)
4277
.await
4278
.unwrap ( ) ;
4279
if n > 0 {
4280
break ;
4281
}
4282
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4283
}
4284
let ( st, body) =
4285
call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green","land":"auto"}"# .into ( ) ) ) .await;
4286
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4287
assert! ( body.contains ( r # ""land":"auto""# ) , " {body}" ) ;
4288
let ( st, _ ) =
4289
call ( & h.app, " PUT" , " /api/v1/repos/andy/game/diffusion" , Some ( " site" ) , Some ( r # "{"follow":"green","land":"soon"}"# .into ( ) ) ) .await;
4290
assert_eq! ( st, StatusCode:: BAD_REQUEST ) ;
4291
4292
4293
4294
write ( api_work.path ( ) , " a2.c" , " v2\n " ) ;
4295
api.snapshot ( " t" ) .unwrap ( ) ;
4296
api.commit ( " api v2: faster sprites" , & [ ] ) .unwrap ( ) ;
4297
assert! ( api.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4298
let mut landed = None ;
4299
for _ in 0 .. 100 {
4300
pass_all ( ) .await;
4301
let all = db:: proposals:: list( & h.app.db, game_row.id, & [ ] ) .await.unwrap ( ) ;
4302
if let Some ( p) = all.into_iter ( ) .find ( | p | p.source_bookmark = = " diffusion/deps/api" & & p.state = = " landed" ) {
4303
landed = Some ( p) ;
4304
break ;
4305
}
4306
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4307
}
4308
let p = landed.expect ( " landed by itself once both halves were green" ) ;
4309
assert! ( p.body.contains ( " lands its bumps itself" ) , " {}" , p.body) ;
a2ef53c5 Andy McCall 20 days ago ↶
4310
4311
let mut events = vec! [ ] ;
4312
for _ in 0 .. 100 {
4313
events = db:: proposals:: events( & h.app.db, p.id) .await.unwrap ( ) ;
4314
if events.iter ( ) .any ( | e | e.event = = " landed" ) {
4315
break ;
4316
}
4317
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 20 ) ) .await;
4318
}
f4d6ef41 Andy McCall 22 days ago ↶
4319
let landing = events.iter ( ) .find ( | e | e.event = = " landed" ) .expect ( " a landing on the timeline" ) ;
4320
assert_eq! ( landing.actor, " diffusion" ) ;
4321
assert! ( landing.detail.contains ( " Diffusion, both halves green" ) , " {}" , landing.detail) ;
4322
4323
let mut behind = serde_json:: Value:: Null;
4324
for _ in 0 .. 100 {
4325
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/api/consumers" , Some ( " site" ) , None ) .await;
4326
let list: Vec< serde_json:: Value> = serde_json:: from_str( & list) .unwrap ( ) ;
4327
behind = list.iter ( ) .find ( | c | c[ " name" ] = = " game" ) .map ( | c | c[ " behind" ] .clone ( ) ) .unwrap_or_default ( ) ;
4328
if behind = = 0 {
4329
break ;
4330
}
4331
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4332
}
4333
assert_eq! ( behind, 0 , " main moved to the bump" ) ;
4334
4335
4336
4337
create ( & h.app, " andy" , " lib" ) .await;
4338
create ( & h.app, " andy" , " app" ) .await;
4339
let lib_work = tempfile:: tempdir( ) .unwrap ( ) ;
4340
let lib = client ( & format! ( " {} /andy/lib" , h.base) , lib_work.path ( ) ) ;
4341
write ( lib_work.path ( ) , " lib.h" , " v1\n " ) ;
4342
lib.snapshot ( " t" ) .unwrap ( ) ;
4343
lib.commit ( " lib v1" , & [ ] ) .unwrap ( ) ;
4344
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4345
let app_work = tempfile:: tempdir( ) .unwrap ( ) ;
4346
let app = client ( & format! ( " {} /andy/app" , h.base) , app_work.path ( ) ) ;
4347
app.submodule_add ( & format! ( " {} /andy/lib" , h.base) , " deps/lib" , Some ( " main" ) ) .unwrap ( ) ;
4348
let modules = std:: fs:: read_to_string( app_work.path ( ) .join ( " .oxymodules" ) ) .unwrap ( ) ;
4349
write ( app_work.path ( ) , " .oxymodules" , & format! ( " {modules} follow = green\n land = auto\n " ) ) ;
4350
write ( app_work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4351
write ( app_work.path ( ) , " test.sh" , " echo ok\n " ) ;
4352
app.snapshot ( " t" ) .unwrap ( ) ;
4353
app.commit ( " app on lib" , & [ ] ) .unwrap ( ) ;
4354
assert! ( app.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4355
pass_all ( ) .await;
4356
let app_row = db:: repos:: get( & h.app.db, " andy" , " app" ) .await.unwrap ( ) .unwrap ( ) ;
4357
for _ in 0 .. 100 {
3b1bc27f Andy McCall 20 days ago
4358
let n: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM diffusion_pins WHERE consumer_id = $1" )
f4d6ef41 Andy McCall 22 days ago ↶
4359
.bind ( app_row.id)
4360
.fetch_one ( & h.app.db)
4361
.await
4362
.unwrap ( ) ;
4363
if n > 0 {
4364
break ;
4365
}
4366
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4367
}
4368
write ( lib_work.path ( ) , " lib.c" , " v2\n " ) ;
4369
lib.snapshot ( " t" ) .unwrap ( ) ;
4370
lib.commit ( " lib v2" , & [ ] ) .unwrap ( ) ;
4371
assert! ( lib.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4372
let mut passed = false ;
4373
for _ in 0 .. 100 {
4374
pass_all ( ) .await;
4375
let s: Option< String> = sqlx:: query_scalar(
3b1bc27f Andy McCall 20 days ago
4376
" SELECT status FROM flow_runs WHERE repo_id = $1 AND bookmark = 'diffusion/deps/lib' ORDER BY id DESC LIMIT 1" ,
f4d6ef41 Andy McCall 22 days ago ↶
4377
)
4378
.bind ( app_row.id)
4379
.fetch_optional ( & h.app.db)
4380
.await
4381
.unwrap ( ) ;
4382
if s.as_deref ( ) = = Some ( " passed" ) {
4383
passed = true ;
4384
break ;
4385
}
4386
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4387
}
4388
assert! ( passed, " the app's run on the bump passed" ) ;
4389
assert_eq! ( oxygenhub:: diffusion:: auto_land( & h.app, & app_row, " deps/lib" ) .await, Err ( oxygenhub:: diffusion:: Held:: Unverified) ) ;
4390
let open = db:: proposals:: list( & h.app.db, app_row.id, & [ " open" ] ) .await.unwrap ( ) ;
4391
assert! ( open.iter ( ) .any ( | p | p.source_bookmark = = " diffusion/deps/lib" ) , " amber waits for a person" ) ;
4392
}
e8fa5369 Andy McCall 22 days ago ↶
4393
4394
4395
4396
4397
4398
# [ tokio ::test ( flavor = " multi_thread" ) ]
4399
async fn one_job_reruns_with_what_depends_on_it ( ) {
4400
let h = hub ( ) .await;
4401
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4402
create ( & h.app, " andy" , " flaky" ) .await;
4403
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4404
let c = client ( & format! ( " {} /andy/flaky" , h.base) , work.path ( ) ) ;
4405
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4406
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
4407
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
4408
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4409
let run = runs_of ( & h.app, " andy" , " flaky" ) .await[ 0 ] .clone ( ) ;
4410
4411
let ( st, _ ) =
4412
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/test/rerun" , run.number) , Some ( " site" ) , None ) .await;
4413
assert_eq! ( st, StatusCode:: CONFLICT , " not while the run is live" ) ;
4414
while let Some ( j) = claim ( & h.app) .await {
4415
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4416
let ( name, ph) = ( j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4417
let exit = if name = = " test" { 1 } else { 0 } ;
d9b3b846 Andy McCall 22 days ago ↶
4418
let log = serde_json:: json! ( { " records" : [
4419
{ " t" : 1 , " stream" : " out" , " text" : format! ( " {name} ran" ) } ,
4420
{ " t" : 2 , " stream" : " out" , " text" : " ::group::compile" } ,
4421
{ " t" : 5 , " stream" : " out" , " text" : " cc a.c" } ,
4422
{ " t" : 1502 , " stream" : " out" , " text" : " ::endgroup::" } ,
4423
] } ) ;
e8fa5369 Andy McCall 22 days ago ↶
4424
call ( & h.app, " POST" , & format! ( " /api/v1/flow/jobs/{id} /log" ) , Some ( " runner-secret" ) , Some ( log.to_string ( ) ) ) .await;
4425
assert_eq! (
4426
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : exit, " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, exit as u32 ) } ) )
4427
.await,
4428
StatusCode:: NO_CONTENT
4429
) ;
4430
}
d9b3b846 Andy McCall 22 days ago ↶
4431
4432
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/flaky/flow/{} " , run.number) , None , None ) .await;
4433
assert! ( page.contains ( r # "<details class="log-group" data-from="2">"# ) , " {page}" ) ;
4434
assert! ( page.contains ( r # "compile <span class="took">1.5s</span></summary>"# ) & & ! page.contains ( " ::endgroup::" ) , " {page}" ) ;
e8fa5369 Andy McCall 22 days ago ↶
4435
let status = | n : & 'static str | {
4436
let ( app, run_id) = ( h.app.clone ( ) , run.id) ;
4437
async move { db:: flow:: jobs( & app.db, run_id) .await.unwrap ( ) .into_iter ( ) .find ( | j | j.name = = n ) .unwrap ( ) }
4438
} ;
4439
assert_eq! ( db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) .status, " failed" ) ;
4440
let failed_test = status ( " test" ) .await;
4441
4442
4443
let ( st, body) =
4444
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/test/rerun" , run.number) , Some ( " site" ) , None ) .await;
4445
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4446
assert! ( body.contains ( r # ""queued":1"# ) , " {body}" ) ;
4447
assert_eq! (
4448
( status ( " build" ) .await.status, status ( " lint" ) .await.status, status ( " test" ) .await.status) ,
4449
( " passed" .into ( ) , " passed" .into ( ) , " queued" .into ( ) )
4450
) ;
4451
assert! ( db:: flow:: log( & h.app.db, failed_test.id, 0 , 100 ) .await.unwrap ( ) .is_empty ( ) , " its old log is gone" ) ;
4452
assert! ( db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) .live ( ) ) ;
4453
let checks = checks_when ( & h.app, run.repo_id, & run.state, 3 ) .await;
4454
assert_eq! ( checks.iter ( ) .find ( | c | c.name = = " flow/test" ) .map ( | c | c.status.as_str ( ) ) , Some ( " queued" ) ) ;
4455
let j = claim ( & h.app) .await.expect ( " the test, queued again" ) ;
4456
assert_eq! ( j[ " job" ] [ " name" ] , " test" ) ;
4457
let ph = j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
4458
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4459
assert_eq! (
4460
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( " test" , & ph, 0 ) } ) ) .await,
4461
StatusCode:: NO_CONTENT
4462
) ;
4463
assert_eq! ( db:: flow:: run_by_id( & h.app.db, run.id) .await.unwrap ( ) .unwrap ( ) .status, " passed" , " the run is whole again" ) ;
bff9106e Andy McCall 22 days ago ↶
4464
4465
let andy_id = db:: accounts:: account_by_name( & h.app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) .id;
4466
let kinds: Vec< String> = db:: notify:: inbox( & h.app.db, andy_id, 20 ) .await.unwrap ( ) .into_iter ( ) .map ( | n | n.kind ) .collect ( ) ;
4467
assert! ( kinds.contains ( & " flow.failed" .to_string ( ) ) & & kinds.contains ( & " flow.fixed" .to_string ( ) ) , " {kinds:?}" ) ;
e8fa5369 Andy McCall 22 days ago ↶
4468
4469
4470
let ( _ , body) =
4471
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/build/rerun" , run.number) , Some ( " site" ) , None ) .await;
4472
assert! ( body.contains ( r # ""queued":2"# ) , " {body}" ) ;
4473
assert_eq! (
4474
( status ( " build" ) .await.status, status ( " lint" ) .await.status, status ( " test" ) .await.status) ,
4475
( " queued" .into ( ) , " passed" .into ( ) , " queued" .into ( ) )
4476
) ;
4477
let ( st, _ ) =
4478
call ( & h.app, " POST" , & format! ( " /api/v1/repos/andy/flaky/flow/runs/{} /jobs/deploy/rerun" , run.number) , Some ( " site" ) , None ) .await;
4479
assert_eq! ( st, StatusCode:: CONFLICT , " live again, and no such job either way" ) ;
4480
}
4a4400c5 Andy McCall 22 days ago ↶
4481
4482
4483
4484
4485
4486
# [ tokio ::test ( flavor = " multi_thread" ) ]
4487
async fn failed_runs_are_told_and_an_erroring_runner_is_drained ( ) {
4488
let h = hub ( ) .await;
4489
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4490
let bob = db:: accounts:: create_account( & h.app.db, " bob" , " [email protected] " , " x" , " salt" , false ) .await.unwrap ( ) ;
4491
create ( & h.app, " andy" , " alerts" ) .await;
4492
let row = db:: repos:: get( & h.app.db, " andy" , " alerts" ) .await.unwrap ( ) .unwrap ( ) ;
4493
db:: notify:: set_watch( & h.app.db, bob.id, row.id, true ) .await.unwrap ( ) ;
4494
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4495
let c = client ( & format! ( " {} /andy/alerts" , h.base) , work.path ( ) ) ;
4496
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4497
write ( work.path ( ) , " test.sh" , " exit 1\n " ) ;
4498
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
4499
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4500
let run = runs_of ( & h.app, " andy" , " alerts" ) .await[ 0 ] .clone ( ) ;
3b1bc27f Andy McCall 20 days ago
4501
sqlx:: query( " UPDATE flow_runs SET actor = 'andy' WHERE id = $1" ) .bind ( run.id) .execute ( & h.app.db) .await.unwrap ( ) ;
4a4400c5 Andy McCall 22 days ago ↶
4502
while let Some ( j) = claim ( & h.app) .await {
4503
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4504
let ( name, ph) = ( j[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4505
let exit = if name = = " test" { 1 } else { 0 } ;
4506
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : exit, " duration_ms" : 10 , " receipt" : receipt ( & name, & ph, exit as u32 ) } ) ) .await;
4507
}
4508
let told = | id : i64 | {
4509
let db = h.app.db.clone ( ) ;
4510
async move { db:: notify:: inbox( & db, id, 20 ) .await.unwrap ( ) .into_iter ( ) .filter ( | n | n.kind = = " flow.failed" ) .collect:: < Vec< _ > > ( ) }
4511
} ;
4512
let ( for_andy, for_bob) = ( told ( andy.id) .await, told ( bob.id) .await) ;
4513
assert_eq! ( ( for_andy.len ( ) , for_bob.len ( ) ) , ( 1 , 1 ) , " the pusher and a watcher" ) ;
4514
assert! ( for_andy[ 0 ] .title.contains ( & format! ( " run #{} on main failed (test)" , run.number) ) , " {}" , for_andy[ 0 ] .title) ;
4515
assert_eq! ( for_andy[ 0 ] .url, format! ( " /andy/alerts/flow/{} " , run.number) ) ;
4516
4517
4518
let runner_id = | | {
4519
let db = h.app.db.clone ( ) ;
4520
async move { sqlx:: query_scalar:: < _ , i64 > ( " SELECT id FROM flow_runners WHERE name = 'test-1'" ) .fetch_one ( & db) .await.unwrap ( ) }
4521
} ;
4522
let mut errors = 0 ;
4523
while errors < 5 {
4524
let ( st, _ ) = call ( & h.app, " POST" , " /api/v1/repos/andy/alerts/flow/runs" , Some ( " site" ) , Some ( " {}" .into ( ) ) ) .await;
4525
assert_eq! ( st, StatusCode:: CREATED ) ;
4526
while let Some ( j) = claim ( & h.app) .await {
4527
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4528
let st = finish (
4529
& h.app,
4530
id,
4531
serde_json:: json! ( { " exit_code" : null, " duration_ms" : 10 , " error" : " the microVM did not start: /dev/kvm busy" } ) ,
4532
)
4533
.await;
4534
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
4535
errors + = 1 ;
4536
if errors = = 5 {
4537
break ;
4538
}
4539
}
4540
}
4541
let state = | id : i64 | {
4542
let db = h.app.db.clone ( ) ;
4543
async move { db:: runners:: get( & db, id) .await.unwrap ( ) .unwrap ( ) .state }
4544
} ;
4545
let id = runner_id ( ) .await;
4546
assert_eq! ( state ( id) .await, " draining" ) ;
4547
let drained: Vec< _ > =
4548
db:: notify:: inbox( & h.app.db, andy.id, 50 ) .await.unwrap ( ) .into_iter ( ) .filter ( | n | n.kind = = " flow.runner_drained" ) .collect ( ) ;
4549
assert_eq! ( drained.len ( ) , 1 , " the administrator is told" ) ;
4550
assert! ( drained[ 0 ] .title.contains ( " /dev/kvm busy" ) , " {}" , drained[ 0 ] .title) ;
4551
assert! (
4552
! told ( bob.id) .await.is_empty ( )
4553
& & db:: notify:: inbox( & h.app.db, bob.id, 50 ) .await.unwrap ( ) .iter ( ) .all ( | n | n.kind ! = " flow.runner_drained" )
4554
) ;
4555
4556
db:: runners:: set_state( & h.app.db, id, " active" ) .await.unwrap ( ) ;
4557
assert_eq! ( db:: runners:: erroring( & h.app.db, id, 5 ) .await.unwrap ( ) , None ) ;
f7bcee49 Andy McCall 22 days ago ↶
4558
}
4559
4560
4561
4562
# [ tokio ::test ( flavor = " multi_thread" ) ]
4563
async fn a_landed_proposal_runs_its_targets_pipeline ( ) {
4564
let h = hub ( ) .await;
4565
create ( & h.app, " andy" , " demo" ) .await;
4566
let url = format! ( " {} /andy/demo" , h.base) ;
4567
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4568
let mut c = client ( & url, work.path ( ) ) ;
4569
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4570
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
4571
c.snapshot ( " test" ) .unwrap ( ) ;
4572
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
4573
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4574
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , 1 ) ;
4575
c.bookmark_create ( " feature" , None ) .unwrap ( ) ;
4576
c.bookmark_switch ( " feature" , false ) .unwrap ( ) ;
4577
write ( work.path ( ) , " b.txt" , " more\n " ) ;
4578
c.snapshot ( " test" ) .unwrap ( ) ;
4579
c.commit ( " More" , & [ ] ) .unwrap ( ) ;
4580
assert! ( c.push_bookmark ( " origin" , " feature" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4581
let runs_before =
4582
db:: flow:: runs( & h.app.db, db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) .id, 10 , None ) .await.unwrap ( ) .len ( ) ;
4583
4584
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4585
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
4586
db:: proposals:: create(
4587
& h.app.db,
4588
db:: proposals:: NewProposal {
4589
repo_id: row.id,
4590
title: " More" ,
4591
body: " " ,
4592
author_id: Some ( andy.id) ,
4593
author: " andy" ,
4594
draft: false ,
4595
target: " main" ,
4596
source_kind: " bookmark" ,
4597
source_repo_id: Some ( row.id) ,
4598
source_bookmark: " feature" ,
4599
source_changes: " " ,
4600
} ,
4601
)
4602
.await
4603
.unwrap ( ) ;
4604
let repo = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
2a8b6216 Andy McCall 18 days ago ↶
4605
let state = oxygenhub:: web:: proposals:: land_as( & h.app, & repo, & row, & andy, 1 , " web" , false , None ) .await.ok ( ) .expect ( " landed" ) ;
f7bcee49 Andy McCall 22 days ago ↶
4606
let mut runs = vec! [ ] ;
4607
for _ in 0 .. 100 {
4608
runs = db:: flow:: runs( & h.app.db, row.id, 10 , None ) .await.unwrap ( ) ;
4609
if runs.len ( ) > runs_before {
4610
break ;
4611
}
4612
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 50 ) ) .await;
4613
}
4614
let landed = runs.iter ( ) .find ( | r | r.bookmark = = " main" & & r.state = = state ) .expect ( " a run of the landed state on main" ) ;
4615
assert_eq! ( ( landed.trigger.as_str ( ) , landed.actor.as_str ( ) ) , ( " push" , " andy" ) ) ;
4a4400c5 Andy McCall 22 days ago ↶
4616
}
1870e4fa Andy McCall 21 days ago ↶
4617
4618
4619
4620
# [ tokio ::test ( flavor = " multi_thread" ) ]
4621
async fn owners_run_flow_on_their_own_runners_only ( ) {
4622
use oxy_core:: identity:: SecretKey;
4623
let h = hub ( ) .await;
4624
let register = | name : & 'static str | {
4625
let app = h.app.clone ( ) ;
4626
async move {
c8684d44 Andy McCall 20 days ago
4627
let body = format! ( " username={name} &email={name} %40example.com&password=correct-horse-battery&setup=test-setup" ) ;
1870e4fa Andy McCall 21 days ago ↶
4628
let res = oxygenhub:: router( app)
4629
.oneshot (
4630
Request:: post( " /register" )
4631
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
4632
.body ( Body:: from( body) )
4633
.unwrap ( ) ,
4634
)
4635
.await
4636
.unwrap ( ) ;
4637
res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( )
4638
}
4639
} ;
4640
register ( " andy" ) .await;
4641
let bob = register ( " bob" ) .await;
4642
create ( & h.app, " bob" , " game" ) .await;
4643
let web = | method : & 'static str , path : & str , body : Option< String> | {
4644
let ( app, cookie, path) = ( h.app.clone ( ) , bob.clone ( ) , path.to_string ( ) ) ;
4645
async move {
c7067c7b Andy McCall 16 days ago
4646
let mut req = Request:: builder( ) .method ( method) .uri ( path) .header ( header:: COOKIE , cookie.clone ( ) ) ;
1870e4fa Andy McCall 21 days ago ↶
4647
if body.is_some ( ) {
4648
req = req.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ;
4649
}
c7067c7b Andy McCall 16 days ago
4650
let req = req.body ( body.map ( Body:: from) .unwrap_or_else ( Body:: empty) ) .unwrap ( ) ;
4651
let mut res = oxygenhub:: router( app.clone ( ) ) .oneshot ( req) .await.unwrap ( ) ;
4652
4653
let back =
4654
res.headers ( ) .get ( header:: LOCATION ) .and_then ( | v | v.to_str ( ) .ok ( ) ) .filter ( | l | l.contains ( " /settings/" ) ) .map ( String :: from) ;
4655
if let Some ( to) = back {
4656
let req = Request:: get( to) .header ( header:: COOKIE , cookie) .body ( Body:: empty( ) ) .unwrap ( ) ;
4657
res = oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) ;
4658
}
1870e4fa Andy McCall 21 days ago ↶
4659
String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( )
4660
}
4661
} ;
4662
let csrf_of = | page : & str | {
4663
let i = page.find ( " name=\" _csrf\" value=\" " ) .unwrap ( ) + " name=\" _csrf\" value=\" " .len ( ) ;
4664
page[ i.. ] .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( )
4665
} ;
4666
c7067c7b Andy McCall 16 days ago
4667
let page = web ( " GET" , " /bob/game/settings/flow" , None ) .await;
1870e4fa Andy McCall 21 days ago ↶
4668
assert! ( page.contains ( " register a runner for this repository" ) & & ! page.contains ( " value=\" own\" " ) , " {page}" ) ;
4669
let page = web ( " POST" , " /bob/game/settings/flow" , Some ( format! ( " _csrf={} &flow=own" , csrf_of ( & page) ) ) ) .await;
4670
assert! ( page.contains ( " Register a runner for this repository" ) , " {page}" ) ;
4671
4672
let page = web ( " GET" , " /bob/game/settings" , None ) .await;
4673
let page = web ( " POST" , " /bob/game/settings/runners/token" , Some ( format! ( " _csrf={} " , csrf_of ( & page) ) ) ) .await;
4674
let i = page.find ( " --token " ) .unwrap ( ) + " --token " .len ( ) ;
4675
let token: String = page[ i.. ] .chars ( ) .take_while ( | c | ! c.is_whitespace ( ) ) .collect ( ) ;
4676
let info = serde_json:: json! ( { " arch" : " x86_64" , " os" : " linux" , " version" : " 0.1.0" , " labels" : [ ] , " max_jobs" : 1 } ) ;
4677
let identity = SecretKey:: generate( ) .identity ( " bobs" , " " , 1 ) ;
4678
let reg = serde_json:: json! ( { " token" : token, " name" : " bobs" , " identity" : hex:: encode( identity.encode ( ) ) , " info" : info } ) ;
4679
let ( st, body) = call ( & h.app, " POST" , " /api/v1/flow/runners/register" , None , Some ( reg.to_string ( ) ) ) .await;
4680
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4681
let credential = serde_json:: from_str:: < serde_json:: Value> ( & body) .unwrap ( ) [ " credential" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
4682
c7067c7b Andy McCall 16 days ago
4683
let page = web ( " GET" , " /bob/game/settings/flow" , None ) .await;
1870e4fa Andy McCall 21 days ago ↶
4684
assert! ( page.contains ( " value=\" own\" " ) & & ! page.contains ( " value=\" on\" " ) , " {page}" ) ;
4685
let page = web ( " POST" , " /bob/game/settings/flow" , Some ( format! ( " _csrf={} &flow=on" , csrf_of ( & page) ) ) ) .await;
4686
assert! ( page.contains ( " Only site administrators let the site's runners" ) , " {page}" ) ;
4687
let page = web ( " POST" , " /bob/game/settings/flow" , Some ( format! ( " _csrf={} &flow=own" , csrf_of ( & page) ) ) ) .await;
4688
assert! ( page.contains ( " value=\" own\" checked" ) & & page.contains ( " start runs" ) , " {page}" ) ;
4689
assert_eq! ( db:: repos:: get( & h.app.db, " bob" , " game" ) .await.unwrap ( ) .unwrap ( ) .flow, " own" ) ;
4690
4691
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4692
let c = client ( & format! ( " {} /bob/game" , h.base) , work.path ( ) ) ;
4693
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
4694
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
4695
c.snapshot ( " t" ) .unwrap ( ) ;
4696
c.commit ( " A pipeline" , & [ ] ) .unwrap ( ) ;
4697
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4698
assert_eq! ( runs_of ( & h.app, " bob" , " game" ) .await.len ( ) , 1 , " a run started" ) ;
4699
assert! ( claim ( & h.app) .await.is_none ( ) , " the site's runner is not offered it" ) ;
4700
let body = serde_json:: json! ( { " runner" : " ignored" , " runtimes" : [ " microvm" ] , " info" : info } ) .to_string ( ) ;
4701
let ( st, job) = call ( & h.app, " POST" , " /api/v1/flow/claim" , Some ( & credential) , Some ( body) ) .await;
4702
assert_eq! ( st, StatusCode:: OK , " {job}" ) ;
4703
assert_eq! ( serde_json:: from_str:: < serde_json:: Value> ( & job) .unwrap ( ) [ " repo" ] , " bob/game" ) ;
4704
}
cd437c4f Andy McCall 20 days ago ↶
4705
4706
# [ tokio ::test ( flavor = " multi_thread" ) ]
4707
async fn a_job_takes_the_artifact_its_producer_kept ( ) {
4708
let h = hub ( ) .await;
4709
create ( & h.app, " andy" , " demo" ) .await;
4710
let url = format! ( " {} /andy/demo" , h.base) ;
4711
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4712
let c = client ( & url, work.path ( ) ) ;
4713
write (
4714
work.path ( ) ,
4715
" .flow/pipeline.kdl" ,
4716
& format! (
4717
" 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 "
4718
) ,
4719
) ;
4720
c.snapshot ( " test" ) .unwrap ( ) ;
4721
c.commit ( " Plan, then apply that plan" , & [ ] ) .unwrap ( ) ;
4722
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4723
4724
4725
let runs = runs_of ( & h.app, " andy" , " demo" ) .await;
4726
assert_eq! ( runs.len ( ) , 1 , " a run" ) ;
4727
let jobs = db:: flow:: jobs( & h.app.db, runs[ 0 ] .id) .await.unwrap ( ) ;
4728
assert! ( ! jobs.is_empty ( ) , " jobs: {:?}" , runs[ 0 ] ) ;
4729
let p = claim ( & h.app)
4730
.await
4731
.unwrap_or_else ( | | panic! ( " nothing to claim: {:?}" , jobs.iter ( ) .map ( | j | ( & j.name, & j.status, & j.error) ) .collect:: < Vec< _ > > ( ) ) ) ;
4732
assert_eq! ( p[ " job" ] [ " name" ] , " plan" ) ;
4733
let plan_id = p[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4734
let plan = b " the reviewed plan" .to_vec ( ) ;
4735
let ( st, text) = upload ( & h.app, plan_id, " tfplan" , & plan) .await;
4736
assert_eq! ( st, StatusCode:: OK , " {text}" ) ;
4737
let blob = oxy_core:: repo:: Repo:: blob_ref( & plan) .to_hex ( ) ;
4738
let mut r = receipt ( " plan" , p[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
4739
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
4740
assert_eq! (
4741
finish ( & h.app, plan_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : r } ) ) .await,
4742
StatusCode:: NO_CONTENT
4743
) ;
4744
4745
4746
4747
let a = claim ( & h.app) .await.unwrap ( ) ;
4748
assert_eq! ( a[ " job" ] [ " name" ] , " apply" ) ;
4749
let apply_id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4750
assert_eq! (
4751
a[ " inputs" ] ,
4752
serde_json:: json! ( [ { " artifact" : " tfplan" , " from" : " plan" , " path" : " /src/tfplan" , " blob" : blob } ] ) ,
4753
" the claim names the producer's blob"
4754
) ;
4755
let req = Request:: get( format! ( " /api/v1/flow/jobs/{apply_id} /inputs/0" ) ) .header ( header:: AUTHORIZATION , " Bearer runner-secret" ) ;
4756
let res = oxygenhub:: router( h.app.clone ( ) ) .oneshot ( req.body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
4757
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
4758
assert_eq! ( res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) .to_vec ( ) , plan) ;
4759
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/jobs/{apply_id} /inputs/1" ) , Some ( " runner-secret" ) , None ) .await;
4760
assert_eq! ( st, StatusCode:: NOT_FOUND , " no second input" ) ;
4761
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/jobs/{apply_id} /inputs/0" ) , None , None ) .await;
4762
assert_eq! ( st, StatusCode:: UNAUTHORIZED , " runners only" ) ;
4763
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /api/v1/flow/jobs/{plan_id} /inputs/0" ) , Some ( " runner-secret" ) , None ) .await;
4764
assert_eq! ( st, StatusCode:: CONFLICT , " only a running job's" ) ;
4765
}
417184c7 Andy McCall 20 days ago ↶
4766
4767
# [ tokio ::test ( flavor = " multi_thread" ) ]
4768
async fn a_job_waits_for_approval_and_is_approved_rejected_or_expires ( ) {
4769
let h = hub ( ) .await;
4770
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4771
let bob = db:: accounts:: create_account( & h.app.db, " bob" , " [email protected] " , " x" , " salt" , false ) .await.unwrap ( ) ;
4772
let carol = db:: accounts:: create_account( & h.app.db, " carol" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4773
create ( & h.app, " andy" , " gated" ) .await;
4774
let row = db:: repos:: get( & h.app.db, " andy" , " gated" ) .await.unwrap ( ) .unwrap ( ) ;
4775
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4776
let c = client ( & format! ( " {} /andy/gated" , h.base) , work.path ( ) ) ;
4777
write (
4778
work.path ( ) ,
4779
" .flow/pipeline.kdl" ,
4780
& format! (
47e37402 Andy McCall 20 days ago ↶
4781
" 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 20 days ago ↶
4782
) ,
4783
) ;
4784
let mut n = 0 ;
4785
let mut push = | | {
4786
n + = 1 ;
4787
write ( work.path ( ) , " n.txt" , & format! ( " {n} \n " ) ) ;
4788
c.snapshot ( " test" ) .unwrap ( ) ;
4789
c.commit ( & format! ( " Push {n} " ) , & [ ] ) .unwrap ( ) ;
4790
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4791
} ;
4792
4793
let plan_passes = | number : i64 | {
4794
let app = h.app.clone ( ) ;
4795
let row = row.clone ( ) ;
4796
async move {
4797
let mut run = None ;
4798
for _ in 0 .. 100 {
4799
if let Some ( r) = db:: flow:: run( & app.db, row.id, number) .await.unwrap ( ) {
4800
run = Some ( r) ;
4801
break ;
4802
}
4803
tokio:: time:: sleep( std:: time:: Duration:: from_millis( 30 ) ) .await;
4804
}
4805
let run = run.expect ( " the run" ) ;
3b1bc27f Andy McCall 20 days ago
4806
sqlx:: query( " UPDATE flow_runs SET actor = 'andy' WHERE id = $1" ) .bind ( run.id) .execute ( & app.db) .await.unwrap ( ) ;
417184c7 Andy McCall 20 days ago ↶
4807
let j = claim ( & app) .await.expect ( " plan is claimed" ) ;
4808
assert_eq! ( j[ " job" ] [ " name" ] , " plan" ) ;
4809
let ( id, ph) = ( j[ " job_id" ] .as_i64 ( ) .unwrap ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4810
finish ( & app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( " plan" , & ph, 0 ) } ) ) .await;
4811
db:: flow:: run( & app.db, row.id, number) .await.unwrap ( ) .unwrap ( )
4812
}
4813
} ;
4814
4815
4816
push ( ) ;
4817
let run = runs_of ( & h.app, " andy" , " gated" ) .await[ 0 ] .clone ( ) ;
7e01a766 Andy McCall 20 days ago ↶
4818
33d98147 Andy McCall 17 days ago
4819
let heard = | topic : String| match h.app.bus.live.since ( 0 , & [ topic] ) {
7e01a766 Andy McCall 20 days ago ↶
4820
oxygenhub:: events:: Replay:: Events( e) => e.into_iter ( ) .map ( | e | e.kind ) .collect:: < Vec< _ > > ( ) ,
4821
oxygenhub:: events:: Replay:: Reload => vec! [ " reload" .into ( ) ] ,
4822
} ;
4823
assert! ( heard ( oxygenhub:: events:: topic:: repo( row.id) ) .contains ( & " push" .to_string ( ) ) , " the push is on the repository's topic" ) ;
3b1bc27f Andy McCall 20 days ago
4824
sqlx:: query( " UPDATE flow_runs SET actor = 'andy' WHERE id = $1" ) .bind ( run.id) .execute ( & h.app.db) .await.unwrap ( ) ;
05f1a85a Andy McCall 18 days ago ↶
4825
let early = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & carol, true , None ) .await.unwrap_err ( ) ;
417184c7 Andy McCall 20 days ago ↶
4826
assert! ( early.contains ( " waits for plan first" ) , " {early}" ) ;
4827
let run = plan_passes ( run.number) .await;
4828
assert! ( claim ( & h.app) .await.is_none ( ) , " apply waits for a person, so no runner gets it" ) ;
7e01a766 Andy McCall 20 days ago ↶
4829
assert! ( heard ( oxygenhub:: events:: topic:: run( run.id) ) .contains ( & " jobs" .to_string ( ) ) , " the run's topic hears its jobs" ) ;
dc59d1b5 Andy McCall 20 days ago ↶
4830
4831
4832
let told = | id : i64 | {
4833
let db = h.app.db.clone ( ) ;
4834
async move { db:: notify:: inbox( & db, id, 20 ) .await.unwrap ( ) .into_iter ( ) .filter ( | n | n.kind = = " flow.approval" ) .count ( ) }
4835
} ;
4836
assert_eq! ( ( told ( carol.id) .await, told ( andy.id) .await, told ( bob.id) .await) , ( 1 , 0 , 0 ) ) ;
417184c7 Andy McCall 20 days ago ↶
4837
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/gated/flow/{} " , run.number) , None , None ) .await;
4838
assert! ( page.contains ( " waits for approval by someone with the <em>maintain</em> role, other than andy" ) , " the run page says so" ) ;
05f1a85a Andy McCall 18 days ago ↶
4839
let own = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & andy, true , None ) .await.unwrap_err ( ) ;
417184c7 Andy McCall 20 days ago ↶
4840
assert! ( own.contains ( " someone other than andy" ) , " {own}" ) ;
05f1a85a Andy McCall 18 days ago ↶
4841
let no_role = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & bob, true , None ) .await.unwrap_err ( ) ;
417184c7 Andy McCall 20 days ago ↶
4842
assert! ( no_role.contains ( " takes the maintain role" ) , " {no_role}" ) ;
05f1a85a Andy McCall 18 days ago ↶
4843
assert_eq! ( oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & carol, true , None ) .await.unwrap ( ) , " apply: approved by carol" ) ;
dc59d1b5 Andy McCall 20 days ago ↶
4844
assert_eq! ( told ( carol.id) .await, 1 , " told once" ) ;
417184c7 Andy McCall 20 days ago ↶
4845
let j = claim ( & h.app) .await.expect ( " approved, it runs" ) ;
4846
assert_eq! ( j[ " job" ] [ " name" ] , " apply" ) ;
b644e175 Andy McCall 20 days ago ↶
4847
4848
let token =
4849
j[ " secrets" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | s | s[ " path" ] = = " /run/secrets/cloud" ) .unwrap ( ) [ " value" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
4850
let claims = h.app.oidc ( ) .await.unwrap ( ) .verify ( & token, " https://sts.example" , db:: now_ms( ) ) .expect ( " a valid token" ) ;
4851
assert_eq! ( claims[ " environment" ] , " prod" ) ;
417184c7 Andy McCall 20 days ago ↶
4852
let ( id, ph) = ( j[ " job_id" ] .as_i64 ( ) .unwrap ( ) , j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
4853
finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : receipt ( " apply" , & ph, 0 ) } ) ) .await;
4854
let audit = db:: accounts:: audit_rows( & h.app.db, 50 ) .await.unwrap ( ) ;
4855
assert! ( audit.iter ( ) .any ( | r | r.action = = " flow.approved" & & r.actor = = " carol" & & r.target.ends_with ( " #1 apply" ) ) , " audited" ) ;
4856
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/gated/flow/{} " , run.number) , None , None ) .await;
4857
assert! ( page.contains ( " approved by carol" ) , " the run says who" ) ;
b644e175 Andy McCall 20 days ago ↶
4858
let ( _ , page) = call ( & h.app, " GET" , " /andy/gated/deployments" , None , None ) .await;
4859
assert! ( page.contains ( " <strong>prod</strong>" ) & & page.contains ( " >#1</a>" ) & & page.contains ( " <td>carol</td>" ) , " the Deployments page" ) ;
4860
let ( _ , list) = call ( & h.app, " GET" , " /api/v1/repos/andy/gated/deployments" , Some ( " site" ) , None ) .await;
4861
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
4862
assert_eq! ( ( list[ 0 ] [ " environment" ] .as_str ( ) , list[ 0 ] [ " approved_by" ] .as_str ( ) ) , ( Some ( " prod" ) , Some ( " carol" ) ) ) ;
417184c7 Andy McCall 20 days ago ↶
4863
05f1a85a Andy McCall 18 days ago ↶
4864
4865
4866
417184c7 Andy McCall 20 days ago ↶
4867
push ( ) ;
4868
let run = plan_passes ( 2 ) .await;
05f1a85a Andy McCall 18 days ago ↶
4869
let ( secret, hash) = oxygenhub:: auth:: new_token( ) ;
4870
let tok = db:: accounts:: create_token( & h.app.db, carol.id, " claude-code" , & hash, " repo:write" , " " , None ) .await.unwrap ( ) ;
4871
db:: accounts:: set_token_agent( & h.app.db, carol.id, tok.id, true ) .await.unwrap ( ) ;
4a68f106 Andy McCall 18 days ago ↶
4872
4873
05f1a85a Andy McCall 18 days ago ↶
4874
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/gated/flow/runs/2/jobs/apply/reject" , Some ( & secret) , Some ( " {}" .into ( ) ) ) .await;
4a68f106 Andy McCall 18 days ago ↶
4875
assert_eq! ( st, StatusCode:: CONFLICT , " {body}" ) ;
4876
assert! ( body.contains ( " decided by a person here" ) & & body.contains ( " Settings › OxygenFlow" ) , " {body}" ) ;
4877
let gated = db:: repos:: get( & h.app.db, " andy" , " gated" ) .await.unwrap ( ) .unwrap ( ) ;
4878
db:: repos:: set_agents_decide_gates( & h.app.db, gated.id, true ) .await.unwrap ( ) ;
4879
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 18 days ago ↶
4880
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
4881
assert! ( body.contains ( " rejected by carol via claude-code" ) , " {body}" ) ;
417184c7 Andy McCall 20 days ago ↶
4882
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
4883
let apply = jobs.iter ( ) .find ( | j | j.name = = " apply" ) .unwrap ( ) ;
05f1a85a Andy McCall 18 days ago ↶
4884
assert_eq! ( ( apply.status.as_str ( ) , apply.error.as_str ( ) ) , ( " cancelled" , " rejected by carol via claude-code" ) ) ;
4885
assert_eq! ( ( apply.approved_by.as_str ( ) , apply.approved_via.as_str ( ) ) , ( " carol" , " claude-code" ) ) ;
4886
let audit = db:: accounts:: audit_rows( & h.app.db, 50 ) .await.unwrap ( ) ;
4887
assert! (
4888
audit.iter ( ) .any ( | r | r.action = = " flow.rejected" & & r.actor = = " carol" & & r.detail.contains ( " via claude-code" ) ) ,
4889
" audited with the agent"
4890
) ;
417184c7 Andy McCall 20 days ago ↶
4891
assert! ( claim ( & h.app) .await.is_none ( ) ) ;
4892
4893
4894
push ( ) ;
4895
let run = plan_passes ( 3 ) .await;
4896
assert_eq! ( oxygenhub:: flow:: expire_approvals( & h.app, db:: now_ms( ) + 3_600_000 ) .await.unwrap ( ) , 0 , " not yet" ) ;
4897
assert_eq! ( oxygenhub:: flow:: expire_approvals( & h.app, db:: now_ms( ) + 25 * 3_600_000 ) .await.unwrap ( ) , 1 ) ;
4898
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
4899
let apply = jobs.iter ( ) .find ( | j | j.name = = " apply" ) .unwrap ( ) ;
4900
assert_eq! ( ( apply.status.as_str ( ) , apply.error.as_str ( ) ) , ( " cancelled" , " no approval within 24h" ) ) ;
4901
}
67be8ac0 Andy McCall 20 days ago ↶
4902
4903
# [ tokio ::test ( flavor = " multi_thread" ) ]
4904
async fn live_streams_only_topics_the_viewer_may_hear_and_replays ( ) {
4905
let h = hub ( ) .await;
4906
create ( & h.app, " andy" , " open" ) .await;
4907
h.app.repos.create ( & RepoName:: parse( " andy/shut" ) .unwrap ( ) , None ) .unwrap ( ) ;
4908
db:: repos:: create( & h.app.db, " andy" , " shut" , " " , hub_api:: Visibility:: Private) .await.unwrap ( ) ;
4909
let open = db:: repos:: get( & h.app.db, " andy" , " open" ) .await.unwrap ( ) .unwrap ( ) ;
4910
let shut = db:: repos:: get( & h.app.db, " andy" , " shut" ) .await.unwrap ( ) .unwrap ( ) ;
33d98147 Andy McCall 17 days ago
4911
let live = & h.app.bus.live;
67be8ac0 Andy McCall 20 days ago ↶
4912
let first = live.publish ( & oxygenhub:: events:: topic:: repo( open.id) , " push" ) ;
4913
live.publish ( & oxygenhub:: events:: topic:: repo( shut.id) , " push" ) ;
4914
live.publish ( & oxygenhub:: events:: topic:: proposal( open.id, 3 ) , " review" ) ;
4915
4916
4917
let read = | path : String, last : Option< u64 > | {
4918
let app = h.app.clone ( ) ;
4919
async move {
4920
let mut req = Request:: get( & path) ;
4921
if let Some ( l) = last {
4922
req = req.header ( " last-event-id" , l.to_string ( ) ) ;
4923
}
4924
let res = oxygenhub:: router( app) .oneshot ( req.body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
4925
let status = res.status ( ) ;
4926
let mut body = res.into_body ( ) ;
4927
let mut text = String :: new( ) ;
4928
while let Ok ( Some ( Ok ( frame) ) ) = tokio:: time:: timeout( std:: time:: Duration:: from_millis( 300 ) , body.frame ( ) ) .await {
4929
if let Some ( d) = frame.data_ref ( ) {
4930
text.push_str ( & String :: from_utf8_lossy( d) ) ;
4931
}
4932
}
4933
( status, text)
4934
}
4935
} ;
4936
let topics = format! ( " /live?topics=repo:{} ,repo:{} /proposal:3" , open.id, open.id) ;
4937
4938
let ( st, text) = read ( topics.clone ( ) , Some ( first.id) ) .await;
4939
assert_eq! ( st, StatusCode:: OK ) ;
4940
assert! ( text.contains ( " event: change" ) & & text.contains ( & format! ( " \" topic\" :\" repo:{} /proposal:3\" " , open.id) ) , " {text}" ) ;
4941
assert! ( ! text.contains ( & format! ( " \" topic\" :\" repo:{} \" " , shut.id) ) , " never another topic: {text}" ) ;
4942
assert! ( ! text.contains ( & format! ( " id: {} \n " , first.id) ) , " not what was already seen: {text}" ) ;
4943
4944
let ( st, _ ) = read ( format! ( " /live?topics=repo:{} ,repo:{} " , open.id, shut.id) , None ) .await;
4945
assert_eq! ( st, StatusCode:: FORBIDDEN ) ;
4946
assert_eq! ( read ( " /live?topics=user:1" .into ( ) , None ) .await.0 , StatusCode:: FORBIDDEN ) ;
4947
assert_eq! ( read ( " /live?topics=nonsense" .into ( ) , None ) .await.0 , StatusCode:: BAD_REQUEST ) ;
4948
assert_eq! ( read ( " /live?topics=" .into ( ) , None ) .await.0 , StatusCode:: BAD_REQUEST ) ;
4949
4950
for _ in 0 .. oxygenhub:: events:: REPLAY {
4951
live.publish ( " repo:999999" , " push" ) ;
4952
}
4953
let ( _ , text) = read ( topics, Some ( first.id) ) .await;
4954
assert! ( text.contains ( " event: reload" ) , " {text}" ) ;
4955
}
47e37402 Andy McCall 20 days ago ↶
4956
4957
# [ tokio ::test ( flavor = " multi_thread" ) ]
4958
async fn a_sealed_approval_releases_exactly_the_reviewed_plan ( ) {
4959
use oxy_core:: approval:: { SignedApproval, Statement} ;
4960
use oxy_core:: hash:: ObjectRef;
4961
use oxy_core:: identity:: SecretKey;
4962
let h = hub ( ) .await;
4963
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4964
let carol = db:: accounts:: create_account( & h.app.db, " carol" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
4965
let sk = SecretKey:: generate( ) ;
4966
let carol_id = sk.identity ( " Carol" , " [email protected] " , 1 ) ;
4967
db:: accounts:: link_identity( & h.app.db, carol.id, & carol_id.hash ( ) .to_hex ( ) , " Carol" , " [email protected] " , & carol_id.encode ( ) )
4968
.await
4969
.unwrap ( ) ;
4970
create ( & h.app, " andy" , " infra" ) .await;
4971
let row = db:: repos:: get( & h.app.db, " andy" , " infra" ) .await.unwrap ( ) .unwrap ( ) ;
4972
let work = tempfile:: tempdir( ) .unwrap ( ) ;
4973
let c = client ( & format! ( " {} /andy/infra" , h.base) , work.path ( ) ) ;
4974
write (
4975
work.path ( ) ,
4976
" .flow/pipeline.kdl" ,
4977
& format! (
4978
" 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 "
4979
) ,
4980
) ;
4981
c.snapshot ( " test" ) .unwrap ( ) ;
4982
c.commit ( " Plan, then apply" , & [ ] ) .unwrap ( ) ;
4983
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
4984
let run = runs_of ( & h.app, " andy" , " infra" ) .await[ 0 ] .clone ( ) ;
4985
let p = claim ( & h.app) .await.unwrap ( ) ;
4986
let plan_id = p[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
4987
let plan = b " the reviewed plan" .to_vec ( ) ;
4988
upload ( & h.app, plan_id, " tfplan" , & plan) .await;
4989
let mut r = receipt ( " plan" , p[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
4990
r[ " outputs" ] = serde_json:: json! ( [ oxy_core:: repo:: Repo:: blob_ref( & plan) .to_hex ( ) ] ) ;
4991
finish ( & h.app, plan_id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 10 , " receipt" : r } ) ) .await;
4992
let run = db:: flow:: run( & h.app.db, row.id, run.number) .await.unwrap ( ) .unwrap ( ) ;
4993
4994
05f1a85a Andy McCall 18 days ago ↶
4995
let plain = oxygenhub:: flow:: decide( & h.app, & row, & run, " apply" , & carol, true , None ) .await.unwrap_err ( ) ;
47e37402 Andy McCall 20 days ago ↶
4996
assert! ( plain.contains ( & format! ( " oxy seal --flow {} /andy/infra/flow/{} --job apply" , h.base, run.number) ) , " {plain}" ) ;
4997
4998
let ( st, body) =
4999
call ( & h.app, " GET" , & format! ( " /api/v1/repos/andy/infra/flow/runs/{} /jobs/apply/approval" , run.number) , Some ( " site" ) , None ) .await;
5000
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5001
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
5002
assert_eq! ( v[ " environment" ] , " prod" ) ;
5003
assert_eq! ( v[ " inputs" ] , serde_json:: json! ( [ oxy_core:: repo:: Repo:: blob_ref( & plan) .to_hex ( ) ] ) , " the reviewed plan" ) ;
5004
let mut repo = [ 0 u8 ; 16 ] ;
5005
repo.copy_from_slice ( & hex:: decode( v[ " repo" ] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ) ;
5006
let oref = | k : & str | ObjectRef:: from_hex( v[ k] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ;
5007
let statement = Statement {
5008
repo,
5009
run: v[ " run" ] .as_str ( ) .unwrap ( ) .into ( ) ,
5010
job: " apply" .into ( ) ,
5011
job_hash: oref ( " job_hash" ) ,
5012
state: oref ( " state" ) ,
5013
inputs: v[ " inputs" ] .as_array ( ) .unwrap ( ) .iter ( ) .map ( | i | ObjectRef:: from_hex( i.as_str ( ) .unwrap ( ) ) .unwrap ( ) ) .collect ( ) ,
5014
environment: " prod" .into ( ) ,
5015
approve: true ,
5016
} ;
5017
let seal = | s : & Statement, key : & SecretKey, id : & oxy_core:: identity:: Identity| {
5018
let body = serde_json:: json! ( { " signed" : hex:: encode( SignedApproval:: sign( s, id, key) .encode ( ) ) } ) .to_string ( ) ;
5019
let app = h.app.clone ( ) ;
5020
let path = format! ( " /api/v1/repos/andy/infra/flow/runs/{} /jobs/apply/seal" , run.number) ;
5021
async move { call ( & app, " POST" , & path, Some ( " site" ) , Some ( body) ) .await }
5022
} ;
5023
5024
let other_plan = Statement { inputs: vec! [ ObjectRef( [ 9 ; 32 ] ) ] , .. statement.clone ( ) } ;
5025
let ( st, body) = seal ( & other_plan, & sk, & carol_id) .await;
5026
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " approves something else" ) , " {st} {body}" ) ;
5027
let agent = SecretKey:: generate( ) ;
5028
let agent_id = agent.identity ( " Claude Code" , " [email protected] " , 1 ) ;
5029
let ( st, body) = seal ( & statement, & agent, & agent_id) .await;
5030
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " not linked to an account" ) , " {st} {body}" ) ;
5031
assert! ( claim ( & h.app) .await.is_none ( ) , " still waiting" ) ;
5032
317d776c Andy McCall 18 days ago ↶
5033
5034
let forged = oxy_core:: identity:: Identity { previous: Some ( carol_id.hash ( ) ) , .. agent_id.clone ( ) } ;
5035
let ( st, body) = seal ( & statement, & agent, & forged) .await;
5036
assert! ( st = = StatusCode:: CONFLICT & & body.contains ( " not linked" ) , " {st} {body}" ) ;
5037
5038
5039
5040
5041
let renewed = carol_id.successor ( vec! [ ] , None , 2 ) ;
5042
let ( st, body) = seal ( & statement, & sk, & renewed) .await;
47e37402 Andy McCall 20 days ago ↶
5043
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
317d776c Andy McCall 18 days ago ↶
5044
assert! (
5045
db:: accounts:: identity_by_key( & h.app.db, & renewed.hash ( ) .to_hex ( ) ) .await.unwrap ( ) .is_some_and ( | l | l.account_id = = carol.id ) ,
5046
" linked now"
5047
) ;
47e37402 Andy McCall 20 days ago ↶
5048
assert! ( body.contains ( " approved by carol, sealed" ) , " {body}" ) ;
5049
let name = oxygenhub:: repos:: RepoName:: parse( " andy/infra" ) .unwrap ( ) ;
5050
let state = statement.state;
5051
let kept = h.app.repos.read ( & name, move | r, _ | Ok ( r.store ( ) .approvals_for ( & state) ? ) ) .await.unwrap ( ) ;
5052
assert_eq! ( kept.len ( ) , 1 , " the approval is in the repository" ) ;
5053
let j = claim ( & h.app) .await.expect ( " released" ) ;
5054
assert_eq! ( j[ " job" ] [ " name" ] , " apply" ) ;
5055
let token =
5056
j[ " secrets" ] .as_array ( ) .unwrap ( ) .iter ( ) .find ( | s | s[ " path" ] = = " /run/secrets/cloud" ) .unwrap ( ) [ " value" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5057
let claims = h.app.oidc ( ) .await.unwrap ( ) .verify ( & token, " https://sts.example" , db:: now_ms( ) ) .unwrap ( ) ;
5058
assert_eq! ( ( claims[ " environment" ] .as_str ( ) , claims[ " approved_by" ] .as_str ( ) ) , ( Some ( " prod" ) , Some ( " carol" ) ) ) ;
5059
assert_eq! ( claims[ " approval" ] .as_str ( ) , Some ( kept[ 0 ] .to_hex ( ) .as_str ( ) ) ) ;
5060
let ( _ , page) = call ( & h.app, " GET" , & format! ( " /andy/infra/flow/{} " , run.number) , None , None ) .await;
5061
assert! ( page.contains ( " sealed with their key" ) , " the run page says so" ) ;
5062
}
55673c65 Andy McCall 20 days ago ↶
5063
5064
5065
5066
# [ tokio ::test ( flavor = " multi_thread" ) ]
5067
async fn repositories_take_turns_on_the_runners ( ) {
5068
let h = hub ( ) .await;
5069
for name in [ " busy" , " quiet" ] {
5070
create ( & h.app, " andy" , name) .await;
5071
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5072
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
5073
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5074
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5075
c.snapshot ( " test" ) .unwrap ( ) ;
5076
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5077
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5078
assert_eq! ( runs_of ( & h.app, " andy" , name) .await.len ( ) , 1 ) ;
5079
}
5080
5081
5082
let order: Vec< String> = {
5083
let mut out = Vec :: new( ) ;
5084
for _ in 0 .. 3 {
5085
let c = claim ( & h.app) .await.expect ( " a job to claim" ) ;
5086
out.push ( c[ " repo" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
5087
}
5088
out
5089
} ;
5090
assert_eq! ( order, [ " andy/busy" , " andy/quiet" , " andy/busy" ] ) ;
5091
}
107eaba5 Andy McCall 20 days ago ↶
5092
53a0fb2e Andy McCall 19 days ago ↶
5093
5094
5095
5096
5097
# [ tokio ::test ( flavor = " multi_thread" ) ]
5098
async fn a_pipeline_lowers_its_jobs_and_expediting_still_wins ( ) {
5099
let h = hub ( ) .await;
5100
for ( name, extra) in [ ( " nightly" , " priority \" low\" \n " ) , ( " everyday" , " " ) ] {
5101
create ( & h.app, " andy" , name) .await;
5102
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5103
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
5104
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( extra) ) ;
5105
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5106
c.snapshot ( " test" ) .unwrap ( ) ;
5107
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5108
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5109
assert_eq! ( runs_of ( & h.app, " andy" , name) .await.len ( ) , 1 ) ;
5110
}
5111
let nightly = db:: repos:: get( & h.app.db, " andy" , " nightly" ) .await.unwrap ( ) .unwrap ( ) ;
5112
db:: repos:: set_flow_priority( & h.app.db, nightly.id, " high" ) .await.unwrap ( ) ;
5113
let q = db:: flow:: queue( & h.app.db) .await.unwrap ( ) ;
5114
assert! (
5115
q.iter ( ) .filter ( | e | e.name = = " nightly" ) .all ( | e | e.priority ( ) = = " low" & & e.priority_note = = " pipeline: low" ) ,
5116
" {:?}" ,
5117
q.iter ( ) .map ( | e | ( & e.name, e.priority ( ) , & e.priority_note) ) .collect:: < Vec< _ > > ( )
5118
) ;
5119
let repo_of = | c : serde_json:: Value| c[ " repo" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5120
5121
5122
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/everyday" ) ;
5123
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/everyday" ) ;
5124
let run = & runs_of ( & h.app, " andy" , " nightly" ) .await[ 0 ] ;
5125
assert! ( db:: flow:: expedite( & h.app.db, run.id, " expedited by andy: needed now" ) .await.unwrap ( ) ) ;
5126
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/nightly" , " an expedited run is urgent whatever its jobs say" ) ;
5127
}
5128
107eaba5 Andy McCall 20 days ago ↶
5129
5130
5131
# [ tokio ::test ( flavor = " multi_thread" ) ]
5132
async fn priority_then_turns_and_an_expedited_run_first ( ) {
5133
let h = hub ( ) .await;
5134
for name in [ " early" , " later" ] {
5135
create ( & h.app, " andy" , name) .await;
5136
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5137
let c = client ( & format! ( " {} /andy/{name} " , h.base) , work.path ( ) ) ;
5138
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5139
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5140
c.snapshot ( " test" ) .unwrap ( ) ;
5141
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5142
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5143
assert_eq! ( runs_of ( & h.app, " andy" , name) .await.len ( ) , 1 ) ;
5144
}
5145
let early = db:: repos:: get( & h.app.db, " andy" , " early" ) .await.unwrap ( ) .unwrap ( ) ;
5146
db:: repos:: set_flow_priority( & h.app.db, early.id, " low" ) .await.unwrap ( ) ;
5147
5148
let repo_of = | c : serde_json:: Value| c[ " repo" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5149
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/later" ) ;
5150
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/later" ) ;
5151
5152
let run = & runs_of ( & h.app, " andy" , " early" ) .await[ 0 ] ;
5153
assert! ( db:: flow:: expedite( & h.app.db, run.id, " expedited by andy: an incident" ) .await.unwrap ( ) ) ;
76f93eae Andy McCall 19 days ago ↶
5154
5155
assert_eq! ( db:: flow:: expedited_live( & h.app.db, " andy" ) .await.unwrap ( ) , 1 ) ;
5156
assert_eq! ( db:: flow:: expedited_live( & h.app.db, " someone-else" ) .await.unwrap ( ) , 0 ) ;
107eaba5 Andy McCall 20 days ago ↶
5157
assert_eq! ( repo_of ( claim ( & h.app) .await.unwrap ( ) ) , " andy/early" ) ;
5158
let q = db:: flow:: queue( & h.app.db) .await.unwrap ( ) ;
5159
assert! (
5160
q.iter ( ) .any ( | e | e.name = = " early" & & e.priority ( ) = = " urgent" & & e.priority_note.contains ( " an incident" ) ) ,
5161
" {:?}" ,
5162
q.iter ( ) .map ( | e | ( & e.name, e.priority ( ) ) ) .collect:: < Vec< _ > > ( )
5163
) ;
5164
5165
db:: accounts:: set_setting( & h.app.db, " flow_priority" , " high" ) .await.unwrap ( ) ;
5166
let q = db:: flow:: queue( & h.app.db) .await.unwrap ( ) ;
5167
assert! ( q.iter ( ) .filter ( | e | e.name = = " later" ) .all ( | e | e.priority ( ) = = " high" ) ) ;
5168
}
026a76d2 Andy McCall 19 days ago ↶
5169
5170
5171
5172
5173
5174
5175
# [ tokio ::test ( flavor = " multi_thread" ) ]
5176
async fn a_running_job_gets_a_short_certificate_for_a_fresh_key ( ) {
5177
use oxy_core:: cert:: { certify_challenge, SignedCertificate} ;
5178
use oxy_core:: identity:: { Scope, SecretKey} ;
5179
let h = hub ( ) .await;
5180
create ( & h.app, " andy" , " demo" ) .await;
5181
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5182
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
5183
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5184
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5185
c.snapshot ( " test" ) .unwrap ( ) ;
5186
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5187
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5188
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , 1 ) ;
5189
let job = claim ( & h.app) .await.expect ( " a job" ) ;
5190
let token = job[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5191
let job_id = job[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
5192
5193
let sk = SecretKey:: generate( ) ;
5194
let id = sk.identity ( " job" , " [email protected] " , 1 ) ;
5195
let ask = | credential : & str , signed_for : & str | {
5196
let proof = sk.sign_challenge ( & id.hash ( ) , & certify_challenge ( signed_for) ) .unwrap ( ) ;
5197
let body = serde_json:: json! ( {
5198
" identity" : oxy_core:: dsse:: base64( & id.encode ( ) ) ,
5199
" proof" : oxy_core:: dsse:: base64( & oxy_core:: cbor:: encode( & proof.to_value ( ) ) ) ,
5200
" scope" : " changes,refs" ,
5201
} ) ;
5202
let ( credential, app) = ( credential.to_string ( ) , h.app.clone ( ) ) ;
5203
async move { call ( & app, " POST" , " /api/v1/keyless/certify" , Some ( & credential) , Some ( body.to_string ( ) ) ) .await }
5204
} ;
5205
let ( st, body) = ask ( & token, & token) .await;
5206
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5207
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
5208
let sc = SignedCertificate:: decode( & oxy_core:: dsse:: base64_decode( v[ " certificate" ] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ) .unwrap ( ) ;
5209
assert! ( sc.verify ( & h.app.hub_key.identity) .is_ok ( ) , " signed with the hub's key" ) ;
5210
assert_eq! ( sc.cert.key_id ( ) , id.hash ( ) ) ;
5211
assert_eq! ( sc.cert.subject, " repo:andy/demo:bookmark:main" ) ;
5212
assert_eq! ( sc.cert.claim ( " repository" ) , Some ( " andy/demo" ) ) ;
5213
assert! ( sc.cert.claim ( " job" ) .is_some ( ) & & sc.cert.claim ( " run" ) = = Some ( " 1" ) ) ;
5214
assert_eq! ( sc.cert.scope, Scope:: CHANGES | Scope:: REFS ) ;
5215
assert! ( sc.cert.expires - sc.cert.not_before < = 10 * 60_000 + 30_000 , " ten minutes" ) ;
5216
5217
5218
let ( st, body) = ask ( & token, " flowjob_someone_else" ) .await;
5219
assert_eq! ( st, StatusCode:: UNAUTHORIZED , " {body}" ) ;
5220
assert! ( body.contains ( " the proof does not show" ) , " {body}" ) ;
5221
5222
5223
5224
let issuer = h.app.oidc ( ) .await.unwrap ( ) ;
5225
let now = oxygenhub:: db:: now_ms( ) ;
5226
let aud = oxygenhub:: keyless:: audience( h.app.config.base_url ( ) ) ;
5227
let good = issuer.mint_for ( h.app.config.base_url ( ) , " agent:claude@andy" , & aud, now, 600 ) ;
5228
let ( st, body) = ask ( & good, & good) .await;
5229
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5230
assert! ( body.contains ( " \" subject\" :\" agent:claude@andy\" " ) , " {body}" ) ;
5231
let other = issuer.mint_for ( h.app.config.base_url ( ) , " agent:claude@andy" , " sts.amazonaws.com" , now, 600 ) ;
5232
let ( st, _ ) = ask ( & other, & other) .await;
5233
assert_eq! ( st, StatusCode:: UNAUTHORIZED ) ;
5234
5235
5236
sqlx:: query( " UPDATE flow_jobs SET status = 'passed' WHERE id = $1" ) .bind ( job_id) .execute ( & h.app.db) .await.unwrap ( ) ;
5237
let ( st, body) = ask ( & token, & token) .await;
5238
assert_eq! ( st, StatusCode:: UNAUTHORIZED , " {body}" ) ;
5239
}
2c31915f Andy McCall 19 days ago ↶
5240
5241
5242
5243
5244
5245
# [ tokio ::test ( flavor = " multi_thread" ) ]
5246
async fn a_receipt_signed_by_a_certified_key_is_kept ( ) {
5247
use oxy_core:: cert:: { certify_challenge, SignedCertificate} ;
5248
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectType} ;
5249
use oxy_core:: identity:: SecretKey;
5250
use oxy_core:: objects:: StateObject;
5251
use oxy_core:: receipt:: SignedReceipt;
5252
let h = hub ( ) .await;
5253
create ( & h.app, " andy" , " demo" ) .await;
5254
let url = format! ( " {} /andy/demo" , h.base) ;
5255
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5256
let c = client ( & url, work.path ( ) ) ;
5257
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5258
c.snapshot ( " test" ) .unwrap ( ) ;
5259
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5260
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5261
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
5262
let heads: Vec< ChangeHash> = run.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
5263
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads.clone ( ) ) .encode ( ) ) ;
5264
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
5265
5266
5267
5268
let certify = | token : String| {
5269
let app = h.app.clone ( ) ;
5270
async move {
5271
let sk = SecretKey:: generate( ) ;
5272
let id = sk.identity ( " test-1 job" , " " , 1 ) ;
5273
let proof = sk.sign_challenge ( & id.hash ( ) , & certify_challenge ( & token) ) .unwrap ( ) ;
5274
let body = serde_json:: json! ( {
5275
" identity" : oxy_core:: dsse:: base64( & id.encode ( ) ) ,
5276
" proof" : oxy_core:: dsse:: base64( & oxy_core:: cbor:: encode( & proof.to_value ( ) ) ) ,
5277
" scope" : " seal" ,
5278
} ) ;
5279
let ( st, body) = call ( & app, " POST" , " /api/v1/keyless/certify" , Some ( & token) , Some ( body.to_string ( ) ) ) .await;
5280
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5281
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
5282
let cert = SignedCertificate:: decode( & oxy_core:: dsse:: base64_decode( v[ " certificate" ] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ) .unwrap ( ) ;
5283
( sk, id, cert)
5284
}
5285
} ;
5286
let sign = | r : & serde_json:: Value,
5287
sk: & SecretKey,
5288
id: & oxy_core:: identity:: Identity,
5289
cert: SignedCertificate,
5290
issuer: oxy_core:: identity:: Identity| {
5291
let r: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
5292
let run_url = format! ( " {url} /flow/{} " , run.number) ;
5293
hex:: encode( SignedReceipt:: sign( r.to_oxy ( ) , repo_id, state, & run_url, id, sk) .with_certificate ( cert, issuer) .encode ( ) )
5294
} ;
5295
let hub_id = h.app.hub_key.identity.clone ( ) ;
5296
5297
let a = claim ( & h.app) .await.unwrap ( ) ;
5298
let b = claim ( & h.app) .await.unwrap ( ) ;
5299
let ph = a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5300
let ( a_name, b_name) = ( a[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) , b[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) ;
5301
let ( a_id, b_id) = ( a[ " job_id" ] .as_i64 ( ) .unwrap ( ) , b[ " job_id" ] .as_i64 ( ) .unwrap ( ) ) ;
5302
let ( sk_a, id_a, cert_a) = certify ( a[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) .await;
5303
5304
5305
let rb = receipt ( & b_name, & ph, 0 ) ;
5306
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 ( ) ) } ) ;
5307
assert_eq! ( finish ( & h.app, b_id, body) .await, StatusCode:: NO_CONTENT ) ;
5308
5309
let ra = receipt ( & a_name, & ph, 0 ) ;
5310
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 ( ) ) } ) ;
5311
assert_eq! ( finish ( & h.app, a_id, body) .await, StatusCode:: NO_CONTENT ) ;
5312
5313
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
5314
let job = | n : & str | jobs.iter ( ) .find ( | j | j.name = = n ) .unwrap ( ) .clone ( ) ;
5315
assert_eq! ( job ( & a_name) .receipt_attest.len ( ) , 64 , " {}" , job ( & a_name) .receipt_note) ;
5316
assert! ( job ( & a_name) .receipt_signer.ends_with ( " (short-lived key)" ) , " {}" , job ( & a_name) .receipt_signer) ;
5317
assert! ( job ( & b_name) .receipt_attest.is_empty ( ) ) ;
5318
assert! ( job ( & b_name) .receipt_note.contains ( " another run or job" ) , " {}" , job ( & b_name) .receipt_note) ;
5319
5320
let kept = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .receipts ( & heads) .unwrap ( ) ;
5321
assert_eq! ( kept.len ( ) , 1 ) ;
5322
assert! ( kept[ 0 ] .problem.is_none ( ) & & kept[ 0 ] .signed.certificate.is_some ( ) ) ;
5323
5324
5325
let t = claim ( & h.app) .await.unwrap ( ) ;
5326
let t_name = t[ " job" ] [ " name" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5327
let fake_sk = SecretKey:: generate( ) ;
5328
let fake = fake_sk.identity ( " not the hub" , " " , 1 ) ;
5329
let ( sk_t , id_t , mut cert_t ) = certify ( t[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ) .await;
5330
cert_t .cert.issuer = fake.hash ( ) ;
5331
let cert_t = SignedCertificate:: sign( cert_t .cert, & fake, & fake_sk) ;
5332
let rt = receipt ( & t_name, & ph, 0 ) ;
5333
let body =
5334
serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 500 , " receipt" : rt, " signed_receipt" : sign ( & rt, & sk_t , & id_t , cert_t , fake) } ) ;
5335
assert_eq! ( finish ( & h.app, t[ " job_id" ] .as_i64 ( ) .unwrap ( ) , body) .await, StatusCode:: NO_CONTENT ) ;
5336
let jobs = db:: flow:: jobs( & h.app.db, run.id) .await.unwrap ( ) ;
5337
let t_job = jobs.iter ( ) .find ( | j | j.name = = t_name ) .unwrap ( ) ;
5338
assert! ( t_job.receipt_note.contains ( " not issued by this hub" ) , " {}" , t_job.receipt_note) ;
5339
}
3eb491e9 Andy McCall 19 days ago ↶
5340
24472413 Andy McCall 19 days ago ↶
5341
5342
5343
5344
5345
# [ tokio ::test ( flavor = " multi_thread" ) ]
5346
async fn a_release_built_by_a_certified_key_is_attested ( ) {
5347
use oxy_core:: cert:: { certify_challenge, SignedCertificate} ;
5348
use oxy_core:: hash:: { hash_object, ChangeHash, ObjectType} ;
5349
use oxy_core:: identity:: SecretKey;
5350
use oxy_core:: objects:: StateObject;
5351
use oxy_core:: receipt:: SignedReceipt;
5352
let h = hub ( ) .await;
5353
create ( & h.app, " andy" , " demo" ) .await;
5354
let url = format! ( " {} /andy/demo" , h.base) ;
5355
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5356
let mut c = client ( & url, work.path ( ) ) ;
5357
c.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
5358
write (
5359
work.path ( ) ,
5360
" .flow/pipeline.kdl" ,
5361
& format! (
5362
" 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 "
5363
) ,
5364
) ;
5365
c.snapshot ( " test" ) .unwrap ( ) ;
5366
c.commit ( " Build a tool" , & [ ] ) .unwrap ( ) ;
5367
let heads = c.bookmark_heads ( ) .unwrap ( ) ;
5368
c.tag_create ( " v1" , & heads, " first" ) .unwrap ( ) ;
5369
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5370
5371
let run = runs_of ( & h.app, " andy" , " demo" ) .await.remove ( 0 ) ;
5372
let a = claim ( & h.app) .await.unwrap ( ) ;
5373
let id = a[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
5374
let token = a[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5375
let body: Vec< u8 > = b " #!/bin/sh\n echo tool\n " .to_vec ( ) ;
5376
let ( _ , text) = upload ( & h.app, id, " tool" , & body) .await;
5377
let blob = serde_json:: from_str:: < serde_json:: Value> ( & text) .unwrap ( ) [ " blob" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5378
let mut r = receipt ( " build" , a[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) , 0 ) ;
5379
r[ " outputs" ] = serde_json:: json! ( [ blob] ) ;
5380
let heads_run: Vec< ChangeHash> = run.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
5381
let state = hash_object ( ObjectType:: State, & StateObject:: new( heads_run) .encode ( ) ) ;
5382
let repo_id = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) .id ( ) .unwrap ( ) ;
5383
5384
let sk = SecretKey:: generate( ) ;
5385
let key = sk.identity ( " test-1 job" , " " , 1 ) ;
5386
let proof = sk.sign_challenge ( & key.hash ( ) , & certify_challenge ( & token) ) .unwrap ( ) ;
5387
let ask = serde_json:: json! ( {
5388
" identity" : oxy_core:: dsse:: base64( & key.encode ( ) ) ,
5389
" proof" : oxy_core:: dsse:: base64( & oxy_core:: cbor:: encode( & proof.to_value ( ) ) ) ,
5390
" scope" : " seal" ,
5391
} ) ;
5392
let ( st, answer) = call ( & h.app, " POST" , " /api/v1/keyless/certify" , Some ( & token) , Some ( ask.to_string ( ) ) ) .await;
5393
assert_eq! ( st, StatusCode:: OK , " {answer}" ) ;
5394
let v: serde_json:: Value = serde_json:: from_str( & answer) .unwrap ( ) ;
5395
let cert = SignedCertificate:: decode( & oxy_core:: dsse:: base64_decode( v[ " certificate" ] .as_str ( ) .unwrap ( ) ) .unwrap ( ) ) .unwrap ( ) ;
5396
let fr: flow_core:: Receipt = serde_json:: from_value( r.clone ( ) ) .unwrap ( ) ;
5397
let signed = hex:: encode(
5398
SignedReceipt:: sign( fr.to_oxy ( ) , repo_id, state, & format! ( " {url} /flow/{} " , run.number) , & key, & sk)
5399
.with_certificate ( cert, h.app.hub_key.identity.clone ( ) )
5400
.encode ( ) ,
5401
) ;
5402
let done = serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 900 , " receipt" : r, " signed_receipt" : signed } ) ;
5403
assert_eq! ( finish ( & h.app, id, done) .await, StatusCode:: NO_CONTENT ) ;
5404
5405
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
5406
let rel = db:: releases:: create( & h.app.db, row.id, " v1" , " One" , " " , false , None ) .await.unwrap ( ) ;
5407
oxygenhub:: web:: releases:: store_asset( & h.app, rel.id, " tool" , " " , & body) .await.unwrap ( ) ;
5408
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/releases/trust/v1" , None , None ) .await;
5409
assert_eq! ( st, StatusCode:: OK ) ;
5410
for want in [
5411
" tool: signed receipt from run #1 build" ,
5412
" a short-lived key this hub certified for repo:andy/demo:bookmark:main" ,
5413
" 1 of 1 assets built and attested by OxygenFlow" ,
5414
] {
5415
assert! ( page.contains ( want) , " missing {want:?}" ) ;
5416
}
5417
assert! ( ! page.contains ( " not one this hub" ) , " not an unknown key" ) ;
5418
}
5419
965b950f Andy McCall 19 days ago ↶
5420
5421
5422
5423
5424
# [ tokio ::test ( flavor = " multi_thread" ) ]
5425
async fn a_push_is_logged_and_the_log_checks_out ( ) {
5426
use oxy_core:: tlog:: { self , Checkpoint, Entry, Hash, Note, Tile} ;
5427
let h = hub ( ) .await;
5428
create ( & h.app, " andy" , " demo" ) .await;
5429
let url = format! ( " {} /andy/demo" , h.base) ;
5430
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5431
let mut c = client ( & url, work.path ( ) ) ;
5432
c.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
5433
write ( work.path ( ) , " a.txt" , " a\n " ) ;
5434
c.commit ( " a" , & [ ] ) .unwrap ( ) ;
5435
c.policy_init ( ) .unwrap ( ) ;
5436
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5437
let host = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
5438
let ( attest, _ ) = host.latest_ref_attest ( " main" ) .unwrap ( ) .unwrap ( ) ;
5439
5440
5441
let ( _ , sk) = oxygenhub:: keys:: load_secret( & h.app.config.keys_dir ( ) ) .unwrap ( ) ;
5442
h.app.tlog.refresh_checkpoint ( & h.app.hub_key.identity, & sk) .unwrap ( ) ;
5443
let ( st, note) = call ( & h.app, " GET" , " /log/checkpoint" , None , None ) .await;
5444
assert_eq! ( st, StatusCode:: OK , " {note}" ) ;
5445
let origin = oxygenhub:: tlog:: origin_of( h.app.config.base_url ( ) ) ;
5446
let cp = Checkpoint:: verify( & Note:: parse( & note) .unwrap ( ) , & origin, & h.app.hub_key.identity) .unwrap ( ) ;
5447
assert! ( cp.size > = 3 , " ref, seal and policy: {}" , cp.size) ;
5448
5449
5450
let bundle_path = Tile { level: 0 , n: 0 , width: cp.size as u32 } .entries_path ( ) ;
5451
let ( st, _ ) = call ( & h.app, " GET" , & format! ( " /log/{bundle_path} " ) , None , None ) .await;
5452
assert_eq! ( st, StatusCode:: OK ) ;
5453
let res =
5454
oxygenhub:: router( h.app.clone ( ) ) .oneshot ( Request:: get( format! ( " /log/{bundle_path} " ) ) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
5455
assert_eq! ( res.headers ( ) [ header:: CACHE_CONTROL ] , " public, max-age=5" , " a partial tile changes" ) ;
5456
let bytes = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
5457
let entries: Vec< Entry> = tlog:: decode_bundle( & bytes) .unwrap ( ) .iter ( ) .map ( | b | Entry:: decode( b) .unwrap ( ) ) .collect ( ) ;
5458
let kinds: Vec< & str > = entries.iter ( ) .map ( | e | e.kind.as_str ( ) ) .collect ( ) ;
5459
assert! ( kinds.contains ( & " ref" ) & & kinds.contains ( & " seal" ) & & kinds.contains ( & " policy" ) , " {kinds:?}" ) ;
5460
let r = entries.iter ( ) .find ( | e | e.kind = = " ref" ) .unwrap ( ) ;
5461
assert_eq! ( r.object, attest) ;
5462
assert_eq! ( r.repo, c.id ( ) .unwrap ( ) ) ;
5463
5464
5465
5466
let mut stored: Vec< Hash> = Vec :: new( ) ;
5467
for ( n, e) in entries.iter ( ) .enumerate ( ) {
5468
stored.extend ( tlog:: stored_hashes_for_leaf( n as u64 , e.leaf_hash ( ) , & stored) .unwrap ( ) ) ;
5469
}
5470
assert_eq! ( tlog:: tree_hash( cp.size, & stored) .unwrap ( ) , cp.root) ;
5471
let t = Tile { level: 0 , n: 0 , width: cp.size as u32 } ;
5472
let res =
5473
oxygenhub:: router( h.app.clone ( ) ) .oneshot ( Request:: get( format! ( " /log/{} " , t.path ( ) ) ) .body ( Body:: empty( ) ) .unwrap ( ) ) .await.unwrap ( ) ;
5474
assert_eq! ( res.status ( ) , StatusCode:: OK ) ;
5475
let data = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
5476
assert_eq! ( data.to_vec ( ) , t.read_data ( & stored) .unwrap ( ) ) ;
5477
let ( st, _ ) = call ( & h.app, " GET" , " /log/tile/0/000.p/200" , None , None ) .await;
5478
assert_eq! ( st, StatusCode:: NOT_FOUND , " not held yet" ) ;
5479
5480
5481
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5482
assert_eq! ( h.app.tlog.size ( ) , cp.size) ;
5483
let ( st, page) = call ( & h.app, " GET" , " /log" , None , None ) .await;
5484
assert_eq! ( st, StatusCode:: OK ) ;
5485
assert! ( page.contains ( " Transparency log" ) & & page.contains ( " andy/demo" ) & & page.contains ( & origin) , " {page}" ) ;
5486
}
5487
e5b97ac3 Andy McCall 18 days ago ↶
5488
5489
5490
5491
5492
5493
# [ tokio ::test ( flavor = " multi_thread" ) ]
5494
async fn a_fetch_checks_the_hub_s_log_and_refuses_a_fork ( ) {
5495
use oxy_core:: tlog:: { Checkpoint, Note} ;
5496
let h = hub ( ) .await;
5497
create ( & h.app, " andy" , " demo" ) .await;
5498
let url = format! ( " {} /andy/demo" , h.base) ;
5499
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5500
let mut a = client ( & url, & work.path ( ) .join ( " a" ) ) ;
5501
a.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
5502
let ( _ , sk) = oxygenhub:: keys:: load_secret( & h.app.config.keys_dir ( ) ) .unwrap ( ) ;
5503
let hub_id = h.app.hub_key.identity.clone ( ) ;
5504
let checkpoint = | | h.app.tlog.refresh_checkpoint ( & hub_id, & sk) .unwrap ( ) ;
5505
let mut n = 0 ;
5506
let mut push = | a : & Repo| {
5507
n + = 1 ;
5508
write ( & work.path ( ) .join ( " a" ) , " a.txt" , & format! ( " {n} \n " ) ) ;
5509
a.commit ( & format! ( " change {n} " ) , & [ ] ) .unwrap ( ) ;
5510
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5511
} ;
5512
push ( & a) ;
5513
checkpoint ( ) ;
5514
5515
let home = work.path ( ) .join ( " home-b" ) ;
5516
let b = Repo:: clone( & url, & work.path ( ) .join ( " b" ) ) .unwrap ( ) .with_home ( home.clone ( ) ) ;
5517
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5518
assert! ( report.log_notes.is_empty ( ) , " {:?}" , report.log_notes) ;
5519
let pin = std:: fs:: read_dir( home.join ( " tlog" ) ) .unwrap ( ) .next ( ) .unwrap ( ) .unwrap ( ) .path ( ) ;
5520
let seen = | p : & std:: path:: Path| {
5521
let t = std:: fs:: read_to_string( p) .unwrap ( ) ;
5522
Checkpoint:: parse( & Note:: parse( t.split_once ( ' \n ' ) .unwrap ( ) .1 ) .unwrap ( ) .text) .unwrap ( )
5523
} ;
5524
let first = seen ( & pin) ;
5525
5526
5527
push ( & a) ;
5528
checkpoint ( ) ;
5529
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5530
assert! ( report.log_notes.is_empty ( ) , " {:?}" , report.log_notes) ;
5531
assert! ( seen ( & pin) .size > first.size) ;
5532
5533
5534
push ( & a) ;
5535
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5536
assert! ( report.log_notes.iter ( ) .any ( | n | n.contains ( " not in its transparency log yet" ) ) , " {:?}" , report.log_notes) ;
5537
7dbb41b0 Andy McCall 18 days ago ↶
5538
5539
5540
let text = std:: fs:: read_to_string( & pin) .unwrap ( ) ;
5541
let other_sk = oxy_core:: identity:: SecretKey:: generate( ) ;
5542
let other = other_sk.identity ( " another hub" , " [email protected] " , 1 ) ;
5543
let note = text.split_once ( ' \n ' ) .unwrap ( ) .1 ;
5544
std:: fs:: write( & pin, format! ( " {} \n {note} " , oxy_core:: dsse:: base64( & other.encode ( ) ) ) ) .unwrap ( ) ;
5545
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5546
assert! ( report.log_notes.iter ( ) .any ( | n | n.contains ( " signed by another key" ) ) , " {:?}" , report.log_notes) ;
5547
let report = b.fetch ( " origin" ) .unwrap ( ) ;
5548
assert! ( ! report.log_notes.iter ( ) .any ( | n | n.contains ( " another key" ) ) , " pinned now: {:?}" , report.log_notes) ;
5549
e5b97ac3 Andy McCall 18 days ago ↶
5550
5551
5552
let size = seen ( & pin) .size;
5553
let forged = Checkpoint { origin: oxygenhub:: tlog:: origin_of( h.app.config.base_url ( ) ) , size, root: [ 7 ; 32 ] , extensions: vec! [ ] } ;
5554
let text = std:: fs:: read_to_string( & pin) .unwrap ( ) ;
5555
let key = text.split_once ( ' \n ' ) .unwrap ( ) .0 ;
5556
std:: fs:: write( & pin, format! ( " {key} \n {} " , forged.sign ( & hub_id, & sk) .unwrap ( ) .encode ( ) ) ) .unwrap ( ) ;
5557
checkpoint ( ) ;
5558
let err = b.fetch ( " origin" ) .unwrap_err ( ) .to_string ( ) ;
5559
assert! ( err.contains ( " showed two histories" ) & & err.contains ( " does not extend" ) , " {err}" ) ;
5560
}
5561
4f40eed0 Andy McCall 18 days ago ↶
5562
5563
5564
5565
5566
5567
# [ tokio ::test ( flavor = " multi_thread" ) ]
5568
async fn a_hub_witnesses_another_hub_s_log ( ) {
5569
use oxy_core:: tlog:: { AddCheckpoint, Checkpoint, Note} ;
5570
let log = hub ( ) .await;
5571
let witness = hub ( ) .await;
5572
let origin = oxygenhub:: tlog:: origin_of( log.app.config.base_url ( ) ) ;
5573
witness.app.tlog.add_witness_for ( & origin, log.app.hub_key.identity.clone ( ) ) ;
5574
let name = witness.app.tlog.witness_name ( ) .to_string ( ) ;
5575
log.app.tlog.set_witnesses ( vec! [ oxygenhub:: tlog:: Peer {
5576
name: name.clone ( ) ,
5577
url: format! ( " {} /log/witness/add-checkpoint" , witness.base) ,
5578
identity: witness.app.hub_key.identity.clone ( ) ,
5579
} ] ) ;
5580
create ( & log.app, " andy" , " demo" ) .await;
5581
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5582
let mut c = client ( & format! ( " {} /andy/demo" , log.base) , work.path ( ) ) ;
5583
c.create_identity ( " Andy" , " [email protected] " , oxy_core:: repo:: IdentityScope:: Repo) .unwrap ( ) ;
5584
let ( _ , sk) = oxygenhub:: keys:: load_secret( & log.app.config.keys_dir ( ) ) .unwrap ( ) ;
5585
let log_id = log.app.hub_key.identity.clone ( ) ;
5586
let wid = witness.app.hub_key.identity.clone ( ) ;
5587
for n in 0 .. 2 {
5588
write ( work.path ( ) , " a.txt" , & format! ( " {n} \n " ) ) ;
5589
c.commit ( & format! ( " change {n} " ) , & [ ] ) .unwrap ( ) ;
5590
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5591
log.app.tlog.refresh_checkpoint ( & log_id, & sk) .unwrap ( ) ;
5592
oxygenhub:: tlog:: collect( & log.app) .await;
5593
let note = Note:: parse( & log.app.tlog.checkpoint ( ) .unwrap ( ) ) .unwrap ( ) ;
5594
assert! ( note.cosigned_by ( & name, & wid) .is_ok ( ) , " round {n}: {}" , note.encode ( ) ) ;
5595
5596
let ( _ , served) = call ( & log.app, " GET" , " /log/checkpoint" , None , None ) .await;
5597
assert! ( Note:: parse( & served) .unwrap ( ) .cosigned_by ( & name, & wid) .is_ok ( ) ) ;
5598
}
5599
let cp = Checkpoint:: parse( & Note:: parse( & log.app.tlog.checkpoint ( ) .unwrap ( ) ) .unwrap ( ) .text) .unwrap ( ) ;
13b27cb2 Andy McCall 18 days ago ↶
5600
5601
5602
5603
5604
let ( _ , listed) = call ( & log.app, " GET" , " /log/witnesses" , None , None ) .await;
5605
assert! ( listed.starts_with ( & format! ( " {name} " ) ) , " {listed}" ) ;
5606
let url = format! ( " {} /andy/demo" , log.base) ;
5607
let refs = tokio:: task:: spawn_blocking( move | | {
5608
let opts = RemoteOptions { token: Some ( " site" .into ( ) ) , .. Default :: default( ) } ;
5609
open_remote ( & url, & opts) .unwrap ( ) .refs ( ) .unwrap ( )
5610
} )
5611
.await
5612
.unwrap ( ) ;
5613
assert_eq! ( refs.witnesses.len ( ) , 1 ) ;
5614
assert_eq! ( refs.witnesses[ 0 ] .0 , name) ;
5615
let main = refs.refs.iter ( ) .find ( | r | r.name = = " main" ) .expect ( " main" ) ;
5616
let logged = main.logged.as_ref ( ) .expect ( " logged" ) ;
5617
assert! ( ! main.integration.is_empty ( ) , " a witnessed checkpoint holds it" ) ;
5618
let cosigned: Vec< ( & str , & [[u8 ; 32]]) > = main.integration.iter ( ) .map ( | i | ( i.note.as_str ( ) , i.proof.as_slice ( ) ) ) .collect ( ) ;
5619
let at = oxy_core:: tlog:: integrated_time( & logged.entry, logged.index, & cosigned, & origin, & log_id, & [ ( name.clone ( ) , wid.clone ( ) ) ] )
5620
.expect ( " an integrated time" ) ;
5621
assert_eq! ( at.witness, name) ;
5622
assert! ( at.size > logged.index) ;
4f40eed0 Andy McCall 18 days ago ↶
5623
5624
let ask = | req : AddCheckpoint| {
5625
let app = witness.app.clone ( ) ;
5626
async move { call ( & app, " POST" , " /log/witness/add-checkpoint" , None , Some ( req.encode ( ) ) ) .await }
5627
} ;
5628
5629
let forked = Checkpoint { root: [ 7 ; 32 ] , .. cp.clone ( ) } .sign ( & log_id, & sk) .unwrap ( ) .encode ( ) ;
5630
let ( st, body) = ask ( AddCheckpoint { old_size: cp.size, proof: vec! [ ] , note: forked.clone ( ) } ) .await;
5631
assert_eq! ( st, StatusCode:: UNPROCESSABLE_ENTITY , " {body}" ) ;
5632
5633
let ( st, body) = ask ( AddCheckpoint { old_size: 1 , proof: vec! [ ] , note: forked } ) .await;
5634
assert_eq! ( ( st, body.trim ( ) ) , ( StatusCode:: CONFLICT , cp.size.to_string ( ) .as_str ( ) ) ) ;
5635
5636
let stranger = oxy_core:: identity:: SecretKey:: generate( ) ;
5637
let sid = stranger.identity ( " elsewhere" , " [email protected] " , 1 ) ;
5638
let other = Checkpoint { origin: " elsewhere.example/log" .into ( ) , .. cp.clone ( ) } .sign ( & sid, & stranger) .unwrap ( ) .encode ( ) ;
5639
let ( st, _ ) = ask ( AddCheckpoint { old_size: 0 , proof: vec! [ ] , note: other } ) .await;
5640
assert_eq! ( st, StatusCode:: NOT_FOUND ) ;
5641
}
5642
8dcaeeb8 Andy McCall 18 days ago ↶
5643
5644
5645
5646
5647
# [ tokio ::test ( flavor = " multi_thread" ) ]
5648
async fn a_job_keeps_its_terraform_state_on_the_hub ( ) {
5649
let h = hub ( ) .await;
5650
create ( & h.app, " andy" , " infra" ) .await;
5651
create ( & h.app, " andy" , " other" ) .await;
5652
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5653
let c = client ( & format! ( " {} /andy/infra" , h.base) , work.path ( ) ) ;
5654
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5655
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5656
c.snapshot ( " test" ) .unwrap ( ) ;
5657
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5658
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5659
assert_eq! ( runs_of ( & h.app, " andy" , " infra" ) .await.len ( ) , 1 ) ;
5660
let job = claim ( & h.app) .await.expect ( " a job" ) ;
5661
let token = job[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5662
let basic = format! ( " Basic {} " , oxy_core:: dsse:: base64( format! ( " flow:{token} " ) .as_bytes ( ) ) ) ;
5663
let path = " /api/v1/repos/andy/infra/terraform/state/prod" ;
5664
let send = | method : & str , uri : String, auth : Option< String> , body : & str | {
5665
let mut req = Request:: builder( ) .method ( method) .uri ( uri) ;
5666
if let Some ( a) = auth {
5667
req = req.header ( header:: AUTHORIZATION , a) ;
5668
}
5669
let req = req.body ( Body:: from( body.to_string ( ) ) ) .unwrap ( ) ;
5670
let app = h.app.clone ( ) ;
5671
async move {
5672
let res = oxygenhub:: router( app) .oneshot ( req) .await.unwrap ( ) ;
5673
let st = res.status ( ) ;
5674
let b = res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ;
5675
( st, String :: from_utf8_lossy( & b) .into_owned ( ) )
5676
}
5677
} ;
5678
let lock = r # "{"ID":"lock-1","Operation":"OperationTypeApply","Who":"flow@runner","Version":"1.8.0"}"# ;
5679
5680
assert_eq! ( send ( " GET" , path.into ( ) , None , " " ) .await.0 , StatusCode:: UNAUTHORIZED ) ;
5681
assert_eq! ( send ( " GET" , path.into ( ) , Some ( basic.clone ( ) ) , " " ) .await.0 , StatusCode:: NO_CONTENT ) ;
5682
5683
let ( st, body) = send ( " LOCK" , path.into ( ) , Some ( basic.clone ( ) ) , lock) .await;
5684
assert_eq! ( st, StatusCode:: OK , " {body}" ) ;
5685
let other_lock = r # "{"ID":"lock-2","Operation":"OperationTypePlan"}"# ;
5686
let ( st, held) = send ( " LOCK" , path.into ( ) , Some ( basic.clone ( ) ) , other_lock) .await;
5687
assert_eq! ( st, StatusCode:: LOCKED ) ;
5688
assert! ( held.contains ( " lock-1" ) , " the holder's lock info: {held}" ) ;
5689
let state1 = r # "{"version":4,"serial":1,"lineage":"l","resources":[{"secret":"hunter2"}]}"# ;
5690
assert_eq! ( send ( " POST" , path.into ( ) , Some ( basic.clone ( ) ) , state1) .await.0 , StatusCode:: LOCKED , " not under its lock" ) ;
5691
assert_eq! ( send ( " POST" , format! ( " {path} ?ID=lock-1" ) , Some ( basic.clone ( ) ) , state1) .await.0 , StatusCode:: OK ) ;
5692
assert_eq! ( send ( " UNLOCK" , path.into ( ) , Some ( basic.clone ( ) ) , other_lock) .await.0 , StatusCode:: CONFLICT , " not its lock" ) ;
5693
assert_eq! ( send ( " UNLOCK" , path.into ( ) , Some ( basic.clone ( ) ) , lock) .await.0 , StatusCode:: OK ) ;
5694
let ( st, got) = send ( " GET" , path.into ( ) , Some ( basic.clone ( ) ) , " " ) .await;
5695
assert_eq! ( ( st, got.as_str ( ) ) , ( StatusCode:: OK , state1) ) ;
5696
5697
let state2 = state1.replace ( " \" serial\" :1" , " \" serial\" :2" ) ;
5698
assert_eq! ( send ( " POST" , path.into ( ) , Some ( basic.clone ( ) ) , & state2) .await.0 , StatusCode:: OK ) ;
5699
let row = db:: repos:: get( & h.app.db, " andy" , " infra" ) .await.unwrap ( ) .unwrap ( ) ;
5700
assert_eq! ( db:: terraform:: versions( & h.app.db, row.id, " prod" ) .await.unwrap ( ) , 1 ) ;
5701
let kept = db:: terraform:: get( & h.app.db, row.id, " prod" ) .await.unwrap ( ) .unwrap ( ) ;
5702
assert_eq! ( kept.serial, 2 ) ;
5703
assert! ( ! String :: from_utf8_lossy( & kept.body) .contains ( " hunter2" ) , " sealed at rest" ) ;
5704
5705
let ( st, _ ) = send ( " GET" , " /api/v1/repos/andy/other/terraform/state/prod" .into ( ) , Some ( basic.clone ( ) ) , " " ) .await;
5706
assert_eq! ( st, StatusCode:: FORBIDDEN ) ;
5707
assert_eq! (
5708
send ( " GET" , " /api/v1/repos/andy/infra/terraform/state/..x" .into ( ) , Some ( basic.clone ( ) ) , " " ) .await.0 ,
5709
StatusCode:: BAD_REQUEST
5710
) ;
bb018142 Andy McCall 18 days ago ↶
5711
5712
5713
let mallory = db:: accounts:: create_account( & h.app.db, " mallory" , " [email protected] " , " x" , " salt" , false ) .await.unwrap ( ) ;
5714
let ( secret, hash) = oxygenhub:: auth:: new_token( ) ;
5715
db:: accounts:: create_token( & h.app.db, mallory.id, " cli" , & hash, " repo:write" , " " , None ) .await.unwrap ( ) ;
5716
let ( st, got) = send ( " GET" , path.into ( ) , Some ( format! ( " Bearer {secret} " ) ) , " " ) .await;
5717
assert_eq! ( st, StatusCode:: FORBIDDEN , " {got}" ) ;
5718
assert! ( ! got.contains ( " hunter2" ) ) ;
5719
let ( _ , page) = call ( & h.app, " GET" , " /andy/infra/terraform" , Some ( & secret) , None ) .await;
5720
assert! ( ! page.contains ( " <strong>prod</strong>" ) & & page.contains ( " shown to people who can write" ) , " {page}" ) ;
8dcaeeb8 Andy McCall 18 days ago ↶
5721
5722
let ( st, _ ) = send ( " GET" , path.into ( ) , Some ( " Bearer site" .into ( ) ) , " " ) .await;
5723
assert_eq! ( st, StatusCode:: UNAUTHORIZED , " the site token is not a person's token" ) ;
5724
5725
5726
let issuer = h.app.oidc ( ) .await.unwrap ( ) ;
5727
let now = db:: now_ms( ) ;
5728
let aud = oxygenhub:: terraform:: audience( h.app.config.base_url ( ) ) ;
5729
let own = issuer.mint_for ( h.app.config.base_url ( ) , " repo:andy/infra:bookmark:main" , & aud, now, 600 ) ;
5730
let ( st, got) = send ( " GET" , path.into ( ) , Some ( format! ( " Bearer {own} " ) ) , " " ) .await;
5731
assert_eq! ( ( st, got.contains ( " \" serial\" :2" ) ) , ( StatusCode:: OK , true ) ) ;
5732
let other = issuer.mint_for ( h.app.config.base_url ( ) , " repo:andy/other:bookmark:main" , & aud, now, 600 ) ;
5733
assert_eq! ( send ( " GET" , path.into ( ) , Some ( format! ( " Bearer {other} " ) ) , " " ) .await.0 , StatusCode:: FORBIDDEN ) ;
5734
let elsewhere = issuer.mint_for ( h.app.config.base_url ( ) , " repo:andy/infra:bookmark:main" , " sts.amazonaws.com" , now, 600 ) ;
5735
assert_eq! ( send ( " GET" , path.into ( ) , Some ( format! ( " Bearer {elsewhere} " ) ) , " " ) .await.0 , StatusCode:: UNAUTHORIZED ) ;
16ee5d19 Andy McCall 18 days ago ↶
5736
bb018142 Andy McCall 18 days ago ↶
5737
5738
let res = oxygenhub:: router( h.app.clone ( ) )
5739
.oneshot (
5740
Request:: post( " /register" )
5741
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
5742
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
5743
.unwrap ( ) ,
5744
)
5745
.await
5746
.unwrap ( ) ;
5747
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
5748
let res = oxygenhub:: router( h.app.clone ( ) )
67523e06 Andy McCall 18 days ago ↶
5749
.oneshot ( Request:: get( " /andy/infra/terraform" ) .header ( header:: COOKIE , cookie.clone ( ) ) .body ( Body:: empty( ) ) .unwrap ( ) )
bb018142 Andy McCall 18 days ago ↶
5750
.await
5751
.unwrap ( ) ;
5752
let st = res.status ( ) ;
5753
let page = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
16ee5d19 Andy McCall 18 days ago ↶
5754
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
5755
assert! ( page.contains ( " <strong>prod</strong>" ) & & page.contains ( " /api/v1/repos/andy/infra/terraform/state/NAME" ) , " {page}" ) ;
5756
assert! ( page.contains ( " allow "" ) | | page.contains ( " allow \" " ) , " the egress line names the hub" ) ;
67523e06 Andy McCall 18 days ago ↶
5757
assert! ( page.contains ( " 1 kept version" ) & & page.contains ( " >restore</button>" ) , " its versions, restorable by its owner: {page}" ) ;
5758
5759
5760
5761
5762
let andy = db:: accounts:: account_by_name( & h.app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
5763
let ( pat, hash) = oxygenhub:: auth:: new_token( ) ;
5764
db:: accounts:: create_token( & h.app.db, andy.id, " cli" , & hash, " repo:write" , " " , None ) .await.unwrap ( ) ;
5765
let bearer = format! ( " Bearer {pat} " ) ;
5766
let ( st, list) = send ( " GET" , format! ( " {path} /versions" ) , Some ( bearer.clone ( ) ) , " " ) .await;
5767
assert_eq! ( st, StatusCode:: OK , " {list}" ) ;
5768
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
5769
assert_eq! ( ( list.as_array ( ) .unwrap ( ) .len ( ) , list[ 0 ] [ " serial" ] .as_i64 ( ) ) , ( 1 , Some ( 1 ) ) , " {list}" ) ;
5770
let first = list[ 0 ] [ " id" ] .as_i64 ( ) .unwrap ( ) ;
5771
let ( st, got) = send ( " GET" , format! ( " {path} /versions/{first} " ) , Some ( bearer.clone ( ) ) , " " ) .await;
5772
assert_eq! ( ( st, got.as_str ( ) ) , ( StatusCode:: OK , state1) ) ;
5773
assert_eq! ( send ( " GET" , format! ( " {path} /versions" ) , Some ( format! ( " Bearer {secret} " ) ) , " " ) .await.0 , StatusCode:: FORBIDDEN , " a stranger" ) ;
5774
let restore = | id : i64 , auth : String| send ( " POST" , format! ( " {path} /versions/{id} /restore" ) , Some ( auth) , " " ) ;
5775
assert_eq! ( restore ( first, basic.clone ( ) ) .await.0 , StatusCode:: FORBIDDEN , " a job does not restore" ) ;
5776
assert_eq! ( restore ( first, format! ( " Bearer {secret} " ) ) .await.0 , StatusCode:: FORBIDDEN , " nor a stranger" ) ;
5777
assert_eq! ( restore ( 9999 , bearer.clone ( ) ) .await.0 , StatusCode:: NOT_FOUND ) ;
5778
assert_eq! ( send ( " LOCK" , path.into ( ) , Some ( basic.clone ( ) ) , lock) .await.0 , StatusCode:: OK ) ;
5779
assert_eq! ( restore ( first, bearer.clone ( ) ) .await.0 , StatusCode:: LOCKED , " not under someone's lock" ) ;
5780
assert_eq! ( send ( " UNLOCK" , path.into ( ) , Some ( basic.clone ( ) ) , lock) .await.0 , StatusCode:: OK ) ;
5781
let ( st, got) = restore ( first, bearer.clone ( ) ) .await;
5782
assert_eq! ( ( st, got.contains ( " \" serial\" :3" ) ) , ( StatusCode:: OK , true ) , " {got}" ) ;
5783
let ( _ , now) = send ( " GET" , path.into ( ) , Some ( bearer.clone ( ) ) , " " ) .await;
5784
let now: serde_json:: Value = serde_json:: from_str( & now) .unwrap ( ) ;
5785
assert_eq! (
5786
( now[ " serial" ] .as_i64 ( ) , now[ " lineage" ] .as_str ( ) , now[ " resources" ] [ 0 ] [ " secret" ] .as_str ( ) ) ,
5787
( Some ( 3 ) , Some ( " l" ) , Some ( " hunter2" ) )
5788
) ;
5789
5790
let ( _ , list) = send ( " GET" , format! ( " {path} /versions" ) , Some ( bearer.clone ( ) ) , " " ) .await;
5791
let list: serde_json:: Value = serde_json:: from_str( & list) .unwrap ( ) ;
5792
let serials: Vec< i64 > = list.as_array ( ) .unwrap ( ) .iter ( ) .map ( | v | v[ " serial" ] .as_i64 ( ) .unwrap ( ) ) .collect ( ) ;
5793
assert_eq! ( serials, vec! [ 2 , 1 ] , " {list}" ) ;
5794
let ( st, got) = restore ( list[ 0 ] [ " id" ] .as_i64 ( ) .unwrap ( ) , bearer.clone ( ) ) .await;
5795
assert_eq! ( ( st, got.contains ( " \" serial\" :4" ) ) , ( StatusCode:: OK , true ) , " {got}" ) ;
5796
5797
5798
let page_with = | req : axum:: http:: request:: Builder, body : Body| {
5799
let ( app, cookie) = ( h.app.clone ( ) , cookie.clone ( ) ) ;
5800
async move {
5801
let res = oxygenhub:: router( app) .oneshot ( req.header ( header:: COOKIE , cookie) .body ( body) .unwrap ( ) ) .await.unwrap ( ) ;
5802
let st = res.status ( ) ;
5803
( st, String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) )
5804
}
5805
} ;
5806
let ( st, file) = page_with ( Request:: get( format! ( " /andy/infra/terraform/prod/download?version={first} " ) ) , Body:: empty( ) ) .await;
5807
assert_eq! ( ( st, file.as_str ( ) ) , ( StatusCode:: OK , state1) ) ;
5808
let ( _ , page) = page_with ( Request:: get( " /andy/infra/terraform" ) , Body:: empty( ) ) .await;
5809
let csrf = page.split ( " name=\" _csrf\" value=\" " ) .nth ( 1 ) .unwrap ( ) .split ( ' "' ) .next ( ) .unwrap ( ) .to_string ( ) ;
5810
let ( st, page) = page_with (
5811
Request:: post( " /andy/infra/terraform/prod/restore" ) .header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" ) ,
5812
Body:: from( format! ( " _csrf={csrf} &version={first} " ) ) ,
5813
)
5814
.await;
5815
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
5816
assert! ( page.contains ( " Restored prod: it is now serial 5." ) , " {page}" ) ;
5817
let audit = db:: accounts:: audit_rows( & h.app.db, 50 ) .await.unwrap ( ) ;
5818
assert_eq! ( audit.iter ( ) .filter ( | r | r.action = = " terraform.restored" ) .count ( ) , 3 , " every restore audited" ) ;
8dcaeeb8 Andy McCall 18 days ago ↶
5819
}
5820
5821
5822
5823
5824
5825
# [ tokio ::test ( flavor = " multi_thread" ) ]
5826
# [ ignore ]
5827
async fn tofu_keeps_its_state_on_the_hub ( ) {
5828
let h = hub ( ) .await;
5829
create ( & h.app, " andy" , " infra" ) .await;
5830
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5831
let c = client ( & format! ( " {} /andy/infra" , h.base) , work.path ( ) ) ;
5832
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5833
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5834
c.snapshot ( " test" ) .unwrap ( ) ;
5835
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5836
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5837
assert_eq! ( runs_of ( & h.app, " andy" , " infra" ) .await.len ( ) , 1 ) ;
5838
let job = claim ( & h.app) .await.expect ( " a job" ) ;
5839
let token = job[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
5840
let tf = tempfile:: tempdir( ) .unwrap ( ) ;
5841
let address = format! ( " {} /api/v1/repos/andy/infra/terraform/state/prod" , h.base) ;
5842
std:: fs:: write(
5843
tf.path ( ) .join ( " main.tf" ) ,
5844
format! (
5845
" 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 "
5846
) ,
5847
)
5848
.unwrap ( ) ;
5849
let tofu = | args : & [& str ]| {
5850
let out = std:: process:: Command:: new( " docker" )
5851
.args ( [ " run" , " --rm" , " --network" , " host" , " -v" ] )
5852
.arg ( format! ( " {} :/w" , tf.path ( ) .display ( ) ) )
5853
.args ( [ " -w" , " /w" , " -e" ] )
5854
.arg ( format! ( " TF_HTTP_PASSWORD={token} " ) )
5855
.args ( [ " --entrypoint" , " tofu" , " oxygenflow-tofu:latest" ] )
5856
.args ( args)
5857
.output ( )
5858
.expect ( " docker" ) ;
5859
let text = format! ( " {} {} " , String :: from_utf8_lossy( & out.stdout) , String :: from_utf8_lossy( & out.stderr) ) ;
5860
assert! ( out.status.success ( ) , " tofu {args:?}: {text}" ) ;
5861
text
5862
} ;
5863
tofu ( & [ " init" , " -input=false" , " -no-color" ] ) ;
5864
let applied = tofu ( & [ " apply" , " -auto-approve" , " -input=false" , " -no-color" ] ) ;
5865
assert! ( applied.contains ( " 1 added" ) , " {applied}" ) ;
5866
let again = tofu ( & [ " apply" , " -auto-approve" , " -input=false" , " -no-color" ] ) ;
5867
assert! ( again.contains ( " No changes" ) , " the state was kept: {again}" ) ;
5868
let row = db:: repos:: get( & h.app.db, " andy" , " infra" ) .await.unwrap ( ) .unwrap ( ) ;
5869
let kept = db:: terraform:: get( & h.app.db, row.id, " prod" ) .await.unwrap ( ) .unwrap ( ) ;
5870
assert! ( kept.lock_id.is_empty ( ) , " unlocked after each run" ) ;
5871
assert! ( kept.serial > = 1 ) ;
5872
}
5873
8a48dd83 Andy McCall 18 days ago ↶
5874
5875
5876
# [ tokio ::test ( flavor = " multi_thread" ) ]
5877
async fn metrics_are_private_and_count_flow_jobs ( ) {
5878
let h = hub ( ) .await;
5879
let ( st, _ ) = call ( & h.app, " GET" , " /metrics" , None , None ) .await;
5880
assert_eq! ( st, StatusCode:: NOT_FOUND , " off by default" ) ;
5881
let text = oxygenhub:: metrics:: render( & h.app) .await;
5882
for want in [
5883
" # TYPE oxyhub_uptime_seconds gauge" ,
5884
" oxyhub_flow_jobs{state=\" queued\" } 0" ,
5885
" oxyhub_bucket_requests_total{kind=\" list\" }" ,
5886
" oxyhub_tlog_entries" ,
a6d824c5 Andy McCall 18 days ago ↶
5887
" oxyhub_db_connections{idle=\" true\" }" ,
5888
" oxyhub_pushes_last_day{result=\" refused\" } 0" ,
5889
" # TYPE oxyhub_backup_last_run_ok gauge" ,
2c47eecf Andy McCall 18 days ago ↶
5890
" # TYPE oxyhub_flow_runner_last_seen_seconds gauge" ,
5891
" oxyhub_flow_runs_last_day{result=\" failed\" } 0" ,
5892
" oxyhub_mail_total{result=\" sent\" }" ,
8a48dd83 Andy McCall 18 days ago ↶
5893
] {
5894
assert! ( text.contains ( want) , " missing {want}:\n {text}" ) ;
5895
}
5896
create ( & h.app, " andy" , " demo" ) .await;
5897
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5898
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
5899
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5900
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5901
c.snapshot ( " test" ) .unwrap ( ) ;
5902
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5903
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5904
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , 1 ) ;
5905
let text = oxygenhub:: metrics:: render( & h.app) .await;
5906
assert! ( ! text.contains ( " oxyhub_flow_jobs{state=\" queued\" } 0" ) , " the run's jobs are queued:\n {text}" ) ;
a6d824c5 Andy McCall 18 days ago ↶
5907
assert! ( text.contains ( " oxyhub_pushes_last_day{result=\" accepted\" }" ) , " {text}" ) ;
85ec74fd Andy McCall 18 days ago ↶
5908
5909
5910
let pushed = text
5911
.lines ( )
5912
.find_map ( | l | l.strip_prefix ( " oxyhub_protocol_bytes_total{endpoint=\" push\" ,direction=\" received\" } " ) )
5913
.and_then ( | v | v.parse:: < f64 > ( ) .ok ( ) )
5914
.unwrap_or ( 0. 0 ) ;
5915
assert! ( pushed > 0. 0 , " {text}" ) ;
b9b74d99 Andy McCall 18 days ago ↶
5916
5917
call ( & h.app, " GET" , " /andy/demo" , None , None ) .await;
5918
let text = oxygenhub:: metrics:: render( & h.app) .await;
5919
assert! ( text.contains ( " oxyhub_http_requests_total{route=\" page:repository\" ,method=\" GET\" ,status=\" 2xx\" }" ) , " {text}" ) ;
5920
assert! ( text.contains ( " oxyhub_http_request_duration_seconds_bucket{route=\" page:repository\" ,method=\" GET\" ,le=\" +Inf\" }" ) ) ;
8a48dd83 Andy McCall 18 days ago ↶
5921
}
5922
e97500b7 Andy McCall 18 days ago ↶
5923
5924
5925
5926
5927
# [ tokio ::test ( flavor = " multi_thread" ) ]
5928
async fn insights_chart_pickup_from_when_a_job_could_run ( ) {
5929
let h = hub ( ) .await;
5930
create ( & h.app, " andy" , " demo" ) .await;
5931
let work = tempfile:: tempdir( ) .unwrap ( ) ;
5932
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
5933
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
5934
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
5935
c.snapshot ( " test" ) .unwrap ( ) ;
5936
c.commit ( " Add a pipeline" , & [ ] ) .unwrap ( ) ;
5937
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
5938
assert_eq! ( runs_of ( & h.app, " andy" , " demo" ) .await.len ( ) , 1 ) ;
5939
let mut finished = std:: collections:: HashMap:: new( ) ;
5940
for want in [ " build" , " lint" , " test" ] {
5941
let j = claim ( & h.app) .await.expect ( " a job" ) ;
5942
assert_eq! ( j[ " job" ] [ " name" ] .as_str ( ) , Some ( want) ) ;
5943
let id = j[ " job_id" ] .as_i64 ( ) .unwrap ( ) ;
5944
let row = db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) ;
5945
let ( queued, ready, started) = ( row.queued_ms.unwrap ( ) , row.ready_ms.unwrap ( ) , row.started_ms.unwrap ( ) ) ;
5946
assert! ( queued < = ready & & ready < = started, " {want}: {queued} {ready} {started}" ) ;
5947
if want = = " test" {
5948
assert_eq! ( Some ( ready) , finished[ " build" ] , " ready when build passed, not when it was queued" ) ;
5949
}
5950
let ph = j[ " pipeline_hash" ] .as_str ( ) .unwrap ( ) ;
5951
let st = finish ( & h.app, id, serde_json:: json! ( { " exit_code" : 0 , " duration_ms" : 700 , " receipt" : receipt ( want, ph, 0 ) } ) ) .await;
5952
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
5953
finished.insert ( want, db:: flow:: job( & h.app.db, id) .await.unwrap ( ) .unwrap ( ) .finished_ms) ;
5954
}
5955
let ( st, page) = call ( & h.app, " GET" , " /andy/demo/insights" , None , None ) .await;
5956
assert_eq! ( st, StatusCode:: OK , " {page}" ) ;
5957
assert! ( page.contains ( " <span class=\" kpi-v\" >3</span><span class=\" kpi-l\" >jobs finished" ) , " {page}" ) ;
5958
assert! ( page.contains ( " 100% passed" ) , " {page}" ) ;
5959
assert_eq! ( page.matches ( " <svg class=\" chart\" " ) .count ( ) , 5 , " five charts" ) ;
5960
assert! ( page.contains ( " class=\" bar s-ok\" " ) , " the passed jobs drawn" ) ;
5961
assert! ( page.contains ( " data-refresh=\" 30\" data-fragment=\" insights\" " ) , " it refreshes itself" ) ;
5962
assert! ( page.contains ( " <summary class=\" on\" >More</summary>" ) , " the bar's More menu holds it" ) ;
5963
let ( _ , wall) = call ( & h.app, " GET" , " /andy/demo/insights?range=7d&wall=1" , None , None ) .await;
5964
assert! ( wall.contains ( " <body class=\" wall\" " ) & & wall.contains ( " the last 7 days" ) & & wall.contains ( " leave the wall view" ) , " {wall}" ) ;
5965
5966
assert_eq! ( call ( & h.app, " GET" , " /insights/andy" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
5967
assert_eq! ( call ( & h.app, " GET" , " /admin/insights" , None , None ) .await.0 , StatusCode:: NOT_FOUND ) ;
5968
let res = oxygenhub:: router( h.app.clone ( ) )
5969
.oneshot (
5970
Request:: post( " /register" )
5971
.header ( header:: CONTENT_TYPE , " application/x-www-form-urlencoded" )
5972
.body ( Body:: from( " username=andy&email=a%40example.com&password=correct-horse-battery&setup=test-setup" ) )
5973
.unwrap ( ) ,
5974
)
5975
.await
5976
.unwrap ( ) ;
5977
let cookie = res.headers ( ) .get ( header:: SET_COOKIE ) .unwrap ( ) .to_str ( ) .unwrap ( ) .split ( ' ;' ) .next ( ) .unwrap ( ) .to_string ( ) ;
5978
let andy = db:: accounts:: account_by_name( & h.app.db, " andy" ) .await.unwrap ( ) .unwrap ( ) ;
5979
db:: accounts:: set_admin( & h.app.db, andy.id, true ) .await.unwrap ( ) ;
5980
for path in [ " /insights/andy" , " /admin/insights?runner=test-1&range=1h" ] {
5981
let res = oxygenhub:: router( h.app.clone ( ) )
5982
.oneshot ( Request:: get( path) .header ( header:: COOKIE , cookie.clone ( ) ) .body ( Body:: empty( ) ) .unwrap ( ) )
5983
.await
5984
.unwrap ( ) ;
5985
let st = res.status ( ) ;
5986
let body = String :: from_utf8_lossy( & res.into_body ( ) .collect ( ) .await.unwrap ( ) .to_bytes ( ) ) .into_owned ( ) ;
5987
assert_eq! ( st, StatusCode:: OK , " {path}: {body}" ) ;
5988
assert! ( body.contains ( " <svg class=\" chart\" " ) , " {path}" ) ;
5989
}
5990
}
5991
7c06f5b2 Andy McCall 18 days ago ↶
5992
5993
5994
5995
# [ tokio ::test ( flavor = " multi_thread" ) ]
5996
async fn reads_do_not_wait_for_a_writer ( ) {
5997
use std:: time:: Duration;
5998
let h = hub ( ) .await;
5999
create ( & h.app, " andy" , " demo" ) .await;
6000
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6001
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
6002
write ( work.path ( ) , " a.txt" , " a\n " ) ;
6003
c.commit ( " A change" , & [ ] ) .unwrap ( ) ;
6004
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6005
let name = RepoName:: parse( " andy/demo" ) .unwrap ( ) ;
6006
let writer = h.app.repos.write_guard ( & name) .await;
6007
for path in [ " /andy/demo" , " /andy/demo/changes/main" , " /api/v1/repos/andy/demo/bookmarks" ] {
6008
let ( st, body) = tokio:: time:: timeout( Duration:: from_secs( 10 ) , call ( & h.app, " GET" , path, None , None ) )
6009
.await
6010
.unwrap_or_else ( | _| panic! ( " {path} waited for the writer" ) ) ;
6011
assert_eq! ( st, StatusCode:: OK , " {path}: {body}" ) ;
6012
}
6013
let refs = tokio:: time:: timeout( Duration:: from_secs( 10 ) , call ( & h.app, " POST" , " /andy/demo/oxy/v1/refs" , Some ( " site" ) , None ) ) .await;
6014
assert! ( refs.is_ok ( ) , " a fetch's refs waited for the writer" ) ;
6015
assert! ( tokio:: time:: timeout( Duration:: from_millis( 300 ) , h.app.repos.write_guard ( & name) ) .await.is_err ( ) , " writers take turns" ) ;
6016
assert! ( tokio:: time:: timeout( Duration:: from_millis( 300 ) , h.app.repos.exclusive_guard ( & name) ) .await.is_err ( ) , " a delete waits" ) ;
6017
drop ( writer) ;
6018
let all = tokio:: time:: timeout( Duration:: from_secs( 5 ) , h.app.repos.exclusive_guard ( & name) ) .await.expect ( " then it runs" ) ;
6019
assert! (
6020
tokio:: time:: timeout( Duration:: from_millis( 300 ) , h.app.repos.read_guard ( & name) ) .await.is_err ( ) ,
6021
" nothing reads a repository being deleted"
6022
) ;
6023
drop ( all) ;
6024
}
6025
a10446c6 Andy McCall 18 days ago ↶
6026
6027
6028
6029
6030
# [ tokio ::test ( flavor = " multi_thread" ) ]
6031
async fn the_issue_overview_and_diffusion_pages_listen_on_their_topics ( ) {
6032
let h = hub ( ) .await;
6033
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
6034
create ( & h.app, " andy" , " demo" ) .await;
6035
let row = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
6036
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6037
let c = client ( & format! ( " {} /andy/demo" , h.base) , work.path ( ) ) ;
6038
write ( work.path ( ) , " a.txt" , " a\n " ) ;
6039
c.snapshot ( " t" ) .unwrap ( ) ;
6040
c.commit ( " a" , & [ ] ) .unwrap ( ) ;
6041
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6042
let ( st, body) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/issues" , Some ( " site" ) , Some ( r # "{"title":"Broken"}"# .into ( ) ) ) .await;
6043
assert! ( st.is_success ( ) , " {st} {body}" ) ;
6044
let repo = format! ( " repo:{} " , row.id) ;
6045
let ( _ , issue) = call ( & h.app, " GET" , " /andy/demo/issue/1" , None , None ) .await;
6046
assert! ( issue.contains ( & format! ( " data-live=\" {repo} /issue:1 {repo} \" data-fragment=\" timeline\" " ) ) , " the issue's timeline" ) ;
6047
let ( _ , overview) = call ( & h.app, " GET" , " /andy/demo" , None , None ) .await;
6048
assert! ( overview.contains ( & format! ( " data-live=\" {repo} \" data-fragment=\" latest\" " ) ) , " the latest change" ) ;
6049
assert! ( overview.contains ( & format! ( " data-live=\" {repo} \" data-fragment=\" bookmarks\" " ) ) , " the bookmarks" ) ;
6050
let ( _ , diffusion) = call ( & h.app, " GET" , " /andy/demo/diffusion" , None , None ) .await;
6051
assert! ( diffusion.contains ( & format! ( " data-live=\" diffusion:{} {repo} \" data-fragment=\" diffusion\" " , row.id) ) , " the Diffusion tab" ) ;
6052
}
6053
3eb491e9 Andy McCall 19 days ago ↶
6054
6055
6056
6057
6058
# [ tokio ::test ( flavor = " multi_thread" ) ]
6059
async fn the_hub_countersigns_an_update_by_a_key_it_certified ( ) {
6060
use oxy_core:: attest:: AttestBody;
6061
use oxy_core:: cert:: { Certificate, SignedCertificate} ;
6062
use oxy_core:: identity:: Scope;
6063
use oxy_core:: repo:: IdentityScope;
6064
let h = hub ( ) .await;
6065
create ( & h.app, " andy" , " demo" ) .await;
6066
let url = format! ( " {} /andy/demo" , h.base) ;
6067
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6068
let mut a = client ( & url, & work.path ( ) .join ( " a" ) ) .with_home ( work.path ( ) .join ( " home-a" ) ) ;
6069
a.create_identity ( " Andy" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6070
write ( & work.path ( ) .join ( " a" ) , " a.txt" , " a\n " ) ;
6071
a.commit ( " a" , & [ ] ) .unwrap ( ) ;
6072
a.policy_init ( ) .unwrap ( ) ;
6073
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6074
let hub_id = h.app.hub_key.identity.clone ( ) ;
6075
a.policy_grant_subject ( " agent:claude@andy" , hub_id.clone ( ) , Scope:: CHANGES | Scope:: REFS , None ) .unwrap ( ) ;
6076
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .any ( | r | r.name = = " policy" & & r.ok ) ) ;
6077
6078
6079
let mut b = Repo:: clone( & url, & work.path ( ) .join ( " b" ) ) .unwrap ( ) .with_home ( work.path ( ) .join ( " home-b" ) ) ;
6080
b.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
6081
b.save_config ( ) .unwrap ( ) ;
6082
let ( _ , key) = b.create_identity ( " claude" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6083
let ( _ , secret) = oxygenhub:: keys:: load_secret( & h.app.config.keys_dir ( ) ) .unwrap ( ) ;
6084
let now = db:: now_ms( ) as u64 ;
6085
let cert = SignedCertificate:: sign(
6086
Certificate {
6087
key,
6088
issuer: hub_id.hash ( ) ,
6089
subject: " agent:claude@andy" .into ( ) ,
6090
claims: vec! [ ] ,
6091
scope: Scope:: CHANGES | Scope:: REFS ,
6092
not_before: now - 60_000 ,
6093
expires: now + 600_000 ,
8aa5ef38 Andy McCall 19 days ago ↶
6094
grant: None ,
3eb491e9 Andy McCall 19 days ago ↶
6095
} ,
6096
& hub_id,
6097
& secret,
6098
) ;
6099
b.store ( ) .put_identity ( & hub_id) .unwrap ( ) ;
6100
b.store ( ) .put_certificate ( & cert) .unwrap ( ) ;
6101
write ( & work.path ( ) .join ( " b" ) , " b.txt" , " agent work\n " ) ;
6102
b.commit ( " work" , & [ ] ) .unwrap ( ) ;
6103
let r = b.push ( " origin" , false ) .unwrap ( ) ;
6104
assert! ( r.iter ( ) .all ( | x | x.ok ) , " {r:?}" ) ;
6105
6106
6107
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6108
let main = refs.refs.iter ( ) .find ( | r | r.name = = " main" ) .unwrap ( ) ;
6109
let ( attest, acc) = ( main.attestation.clone ( ) .unwrap ( ) , main.accepted.clone ( ) .expect ( " an acceptance" ) ) ;
6110
assert! ( matches! ( acc.attest.body, AttestBody:: Accepted { attest: r } if r = = attest.attest.hash ( ) ) ) ;
6111
assert_eq! ( acc.sigs[ 0 ] .key, hub_id.hash ( ) ) ;
6112
assert! ( acc.attest.hlc.wall_ms > = now) ;
6113
6114
a.pull ( " origin" ) .unwrap ( ) ;
6115
write ( & work.path ( ) .join ( " a" ) , " a.txt" , " again\n " ) ;
6116
a.commit ( " again" , & [ ] ) .unwrap ( ) ;
6117
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6118
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6119
assert! ( refs.refs.iter ( ) .find ( | r | r.name = = " main" ) .unwrap ( ) .accepted.is_none ( ) ) ;
6120
}
8aa5ef38 Andy McCall 19 days ago ↶
6121
b5c199bd Andy McCall 16 days ago ↶
6122
6123
6124
6125
6126
# [ tokio ::test ( flavor = " multi_thread" ) ]
6127
async fn a_tag_by_a_key_the_hub_certified_is_accepted_and_fetched ( ) {
6128
use oxy_core:: attest:: AttestBody;
6129
use oxy_core:: cert:: { Certificate, SignedCertificate} ;
6130
use oxy_core:: identity:: Scope;
6131
use oxy_core:: repo:: IdentityScope;
6132
use oxy_core:: tlog:: Entry;
6133
let h = hub ( ) .await;
6134
create ( & h.app, " andy" , " demo" ) .await;
6135
let url = format! ( " {} /andy/demo" , h.base) ;
6136
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6137
let mut a = client ( & url, & work.path ( ) .join ( " a" ) ) .with_home ( work.path ( ) .join ( " home-a" ) ) ;
6138
a.create_identity ( " Andy" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6139
write ( & work.path ( ) .join ( " a" ) , " a.txt" , " a\n " ) ;
6140
a.commit ( " a" , & [ ] ) .unwrap ( ) ;
6141
a.policy_init ( ) .unwrap ( ) ;
6142
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6143
let hub_id = h.app.hub_key.identity.clone ( ) ;
6144
a.policy_grant_subject ( " agent:claude@andy" , hub_id.clone ( ) , Scope:: CHANGES | Scope:: REFS , None ) .unwrap ( ) ;
6145
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .any ( | r | r.name = = " policy" & & r.ok ) ) ;
6146
6147
6148
let mut b = Repo:: clone( & url, & work.path ( ) .join ( " b" ) ) .unwrap ( ) .with_home ( work.path ( ) .join ( " home-b" ) ) ;
6149
b.config.set ( " remote.origin.token" , " site" ) .unwrap ( ) ;
6150
b.save_config ( ) .unwrap ( ) ;
6151
let ( _ , key) = b.create_identity ( " claude" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6152
let ( _ , secret) = oxygenhub:: keys:: load_secret( & h.app.config.keys_dir ( ) ) .unwrap ( ) ;
6153
let now = db:: now_ms( ) as u64 ;
6154
let cert = SignedCertificate:: sign(
6155
Certificate {
6156
key,
6157
issuer: hub_id.hash ( ) ,
6158
subject: " agent:claude@andy" .into ( ) ,
6159
claims: vec! [ ] ,
6160
scope: Scope:: CHANGES | Scope:: REFS ,
6161
not_before: now - 60_000 ,
6162
expires: now + 600_000 ,
6163
grant: None ,
6164
} ,
6165
& hub_id,
6166
& secret,
6167
) ;
6168
b.store ( ) .put_identity ( & hub_id) .unwrap ( ) ;
6169
b.store ( ) .put_certificate ( & cert) .unwrap ( ) ;
6170
let heads = b.bookmark_heads ( ) .unwrap ( ) ;
6171
b.tag_create ( " v0.2.0" , & heads, " OxygenFlow 0.2.0" ) .unwrap ( ) ;
6172
let r = b.push ( " origin" , false ) .unwrap ( ) ;
6173
assert! ( r.iter ( ) .any ( | x | x.name = = " tags/v0.2.0" & & x.ok ) , " {r:?}" ) ;
6174
6175
6176
6177
h.app.tlog.refresh_checkpoint ( & hub_id, & secret) .unwrap ( ) ;
6178
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6179
let tag = refs.tags.iter ( ) .find ( | t | t.name = = " v0.2.0" ) .expect ( " the tag" ) ;
6180
let ( attest, acc) = ( tag.attestation.clone ( ) .unwrap ( ) , tag.accepted.clone ( ) .expect ( " an acceptance" ) ) ;
6181
assert! ( matches! ( acc.attest.body, AttestBody:: Accepted { attest: r } if r = = attest.attest.hash ( ) ) ) ;
6182
assert_eq! ( acc.sigs[ 0 ] .key, hub_id.hash ( ) ) ;
6183
assert! ( acc.attest.hlc.wall_ms > = now) ;
6184
let logged = tag.logged.as_ref ( ) .expect ( " the tag's place in the log" ) ;
6185
let entry = Entry:: decode( & logged.entry) .unwrap ( ) ;
6186
assert_eq! ( ( entry.kind.as_str ( ) , entry.object) , ( " tag" , attest.attest.hash ( ) ) ) ;
6187
let logged_acceptance = db:: tlog:: entry_of( & h.app.db, & acc.attest.hash ( ) .to_hex ( ) , " accepted" ) .await.unwrap ( ) ;
6188
assert! ( logged_acceptance.is_some ( ) , " the acceptance is logged too" ) ;
6189
6190
6191
let c = Repo:: clone( & url, & work.path ( ) .join ( " c" ) ) .unwrap ( ) .with_home ( work.path ( ) .join ( " home-c" ) ) ;
6192
assert_eq! ( c.tag_heads ( " v0.2.0" ) .unwrap ( ) , Some ( heads) ) ;
6193
let report = c.fetch ( " origin" ) .unwrap ( ) ;
6194
assert! ( report.skipped.is_empty ( ) , " {:?}" , report.skipped) ;
6195
6196
a.pull ( " origin" ) .unwrap ( ) ;
6197
let t = a.tags ( ) .unwrap ( ) .into_iter ( ) .find ( | t | t.name = = " v0.2.0" ) .expect ( " pulled" ) ;
6198
assert_eq! ( t.signer.as_deref ( ) , Some ( " claude" ) ) ;
a0a1601e Andy McCall 16 days ago ↶
6199
6200
6201
6202
6203
a.tag_create ( " v0.1.0" , & a.bookmark_heads ( ) .unwrap ( ) , " by Andy" ) .unwrap ( ) ;
6204
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .any ( | r | r.name = = " tags/v0.1.0" & & r.ok ) ) ;
6205
let bare = oxy_core:: protocol:: http_get( & format! ( " {url} /oxy/v1/refs" ) , & RemoteOptions:: default( ) ) .unwrap ( ) ;
6206
let ( _ , old) = oxy_core:: protocol:: wire:: decode_refs( & bare) .unwrap ( ) ;
6207
let names: Vec< String> = old.tags.into_iter ( ) .map ( | t | t.name ) .collect ( ) ;
6208
assert_eq! ( names, [ " v0.1.0" ] , " no Oxy-Features header" ) ;
6209
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6210
let mut names: Vec< String> = refs.tags.into_iter ( ) .map ( | t | t.name ) .collect ( ) ;
6211
names.sort ( ) ;
6212
assert_eq! ( names, [ " v0.1.0" , " v0.2.0" ] , " a current client declares certified-tags" ) ;
b5c199bd Andy McCall 16 days ago ↶
6213
}
6214
8aa5ef38 Andy McCall 19 days ago ↶
6215
6216
6217
6218
6219
6220
# [ tokio ::test ( flavor = " multi_thread" ) ]
6221
async fn an_agent_with_a_grant_pushes_in_its_owner_s_name ( ) {
6222
use oxy_core:: cert:: { Grant, SignedGrant} ;
6223
use oxy_core:: identity:: { Identity, Scope, SecretKey} ;
6224
use oxy_core:: repo:: { IdentityScope, SignatureStatus} ;
6225
let h = hub ( ) .await;
6226
let andy = db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
6227
let ( pat, pat_hash) = oxygenhub:: auth:: new_token( ) ;
6228
db:: accounts:: create_token( & h.app.db, andy.id, " cli" , & pat_hash, " repo:write" , " " , None ) .await.unwrap ( ) ;
6229
create ( & h.app, " andy" , " demo" ) .await;
6230
let url = format! ( " {} /andy/demo" , h.base) ;
6231
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6232
6233
let mut a = client ( & url, & work.path ( ) .join ( " a" ) ) .with_home ( work.path ( ) .join ( " home-a" ) ) ;
6234
let ( andy_key, andy_id) = a.create_identity ( " Andy" , " [email protected] " , IdentityScope:: User) .unwrap ( ) ;
6235
db:: accounts:: link_identity( & h.app.db, andy.id, & andy_key.to_hex ( ) , " Andy" , " [email protected] " , & andy_id.encode ( ) ) .await.unwrap ( ) ;
6236
write ( & work.path ( ) .join ( " a" ) , " a.txt" , " a\n " ) ;
6237
a.commit ( " a" , & [ ] ) .unwrap ( ) ;
6238
a.policy_init ( ) .unwrap ( ) ;
6239
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6240
let hub_id = h.app.hub_key.identity.clone ( ) ;
6241
a.policy_grant_subject ( " agent:claude@andy" , hub_id.clone ( ) , Scope:: CHANGES | Scope:: REFS , None ) .unwrap ( ) ;
6242
assert! ( a.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6243
6244
6245
let ( _ , andy_sk) = a.secret_key ( ) .unwrap ( ) .unwrap ( ) ;
6246
let now = db:: now_ms( ) as u64 ;
6247
let grant = | by : & Identity, sk : & SecretKey, subject : & str | {
6248
SignedGrant:: sign(
6249
Grant {
6250
grantor: by.hash ( ) ,
6251
subject: subject.into ( ) ,
6252
issuer: hub_id.hash ( ) ,
6253
scope: Scope:: CHANGES | Scope:: SEAL | Scope:: REFS ,
6254
not_before: now - 60_000 ,
6255
expires: now + 86_400_000 ,
6256
} ,
6257
by,
6258
sk,
6259
)
6260
} ;
6261
let post = | g : SignedGrant| {
6262
let ( app, pat) = ( h.app.clone ( ) , pat.clone ( ) ) ;
6263
let body = serde_json:: json! ( { " grant" : oxy_core:: dsse:: base64( & g.encode ( ) ) } ) .to_string ( ) ;
6264
async move { call ( & app, " POST" , " /api/v1/agents/grants" , Some ( & pat) , Some ( body) ) .await }
6265
} ;
6266
let stray_sk = SecretKey:: generate( ) ;
6267
let stray = stray_sk.identity ( " stray" , " [email protected] " , 1 ) ;
6268
let ( st, body) = post ( grant ( & stray, & stray_sk, " agent:claude@andy" ) ) .await;
6269
assert_eq! ( st, StatusCode:: FORBIDDEN , " a key not linked to the account: {body}" ) ;
6270
let ( st, body) = post ( grant ( & andy_id, & andy_sk, " agent:claude@mallory" ) ) .await;
6271
assert_eq! ( st, StatusCode:: BAD_REQUEST , " another account's agent: {body}" ) ;
6272
let ( st, body) = post ( grant ( & andy_id, & andy_sk, " agent:claude@andy" ) ) .await;
6273
assert_eq! ( st, StatusCode:: CREATED , " {body}" ) ;
6274
let v: serde_json:: Value = serde_json:: from_str( & body) .unwrap ( ) ;
6275
let token = v[ " token" ] .as_str ( ) .unwrap ( ) .to_string ( ) ;
6276
assert! ( token.starts_with ( " oxyagent_" ) ) ;
6277
let grant_id = v[ " id" ] .as_i64 ( ) .unwrap ( ) ;
205d8f86 Andy McCall 18 days ago ↶
6278
6279
6280
let ( st, me) = call ( & h.app, " GET" , " /api/v1/user" , Some ( & token) , None ) .await;
6281
assert! ( st = = StatusCode:: OK & & me.contains ( " \" andy\" " ) , " {st} {me}" ) ;
6282
let ( st, _ ) = call ( & h.app, " GET" , " /api/v1/agents/grants" , Some ( & token) , None ) .await;
6283
assert_eq! ( st, StatusCode:: FORBIDDEN , " an agent does not manage grants" ) ;
d4a0043f Andy McCall 18 days ago ↶
6284
6285
6286
let repo = db:: repos:: get( & h.app.db, " andy" , " demo" ) .await.unwrap ( ) .unwrap ( ) ;
6287
db:: notify:: set_watch( & h.app.db, andy.id, repo.id, true ) .await.unwrap ( ) ;
6288
let body = serde_json:: json! ( { " title" : " Filed by the agent" , " body" : " " } ) .to_string ( ) ;
6289
let ( st, out) = call ( & h.app, " POST" , " /api/v1/repos/andy/demo/issues" , Some ( & token) , Some ( body) ) .await;
6290
assert_eq! ( st, StatusCode:: CREATED , " {out}" ) ;
6291
let actor: String = sqlx:: query_scalar( " SELECT actor FROM notifications WHERE account_id = $1 AND title LIKE '%Filed by the agent%'" )
6292
.bind ( andy.id)
6293
.fetch_one ( & h.app.db)
6294
.await
6295
.expect ( " told of the agent's issue" ) ;
6296
assert! ( actor.starts_with ( " andy (agent token " ) , " {actor}" ) ;
8aa5ef38 Andy McCall 19 days ago ↶
6297
6298
6299
let agent_home = work.path ( ) .join ( " home-claude" ) ;
6300
oxy_core:: credentials:: Credentials:: at( & agent_home) .save_grant ( & h.base, & token) .unwrap ( ) ;
6301
let agent = | | {
6302
let n = std:: time:: SystemTime:: now( ) .duration_since ( std:: time:: UNIX_EPOCH ) .unwrap ( ) .as_nanos ( ) ;
6303
let dir = work.path ( ) .join ( format! ( " claude-{n} " ) ) ;
6304
let mut r = Repo:: clone( & url, & dir) .unwrap ( ) .with_home ( agent_home.clone ( ) ) ;
559c3920 Andy McCall 19 days ago ↶
6305
6306
r.config.set ( " remote.origin.token" , & token) .unwrap ( ) ;
8aa5ef38 Andy McCall 19 days ago ↶
6307
r.save_config ( ) .unwrap ( ) ;
6308
r.set_user_config ( " author.key" , & andy_key.to_hex ( ) ) .unwrap ( ) ;
6309
r.set_user_config ( " author.signer" , " keyless" ) .unwrap ( ) ;
6310
( r, dir)
6311
} ;
6312
let ( claude, dir) = agent ( ) ;
6313
assert! ( ! agent_home.join ( " keys" ) .exists ( ) , " no key on disk" ) ;
6314
write ( & dir, " work.txt" , " agent work\n " ) ;
6315
let change = claude.commit ( " Agent work" , & [ ] ) .unwrap ( ) ;
6316
let r = claude.push ( " origin" , false ) .unwrap ( ) ;
6317
assert! ( r.iter ( ) .all ( | x | x.ok ) , " {r:?}" ) ;
6318
let signer = claude.signing_key ( ) .expect ( " a certified key" ) ;
6319
assert_ne! ( signer, andy_key) ;
6320
assert! ( ! agent_home.join ( " keys" ) .exists ( ) , " still no key on disk" ) ;
6321
6322
6323
6324
let host = h.app.repos.open ( & RepoName:: parse( " andy/demo" ) .unwrap ( ) ) .unwrap ( ) ;
6325
assert_eq! ( host.store ( ) .get_change_meta ( & change) .unwrap ( ) .author, andy_key) ;
6326
assert! ( matches! (
6327
host.authenticate_change ( & change) .unwrap ( ) ,
6328
SignatureStatus:: Valid { key, sealed: true , .. } if key = = signer
6329
) ) ;
6330
let refs = open_remote ( & url, & RemoteOptions:: default( ) ) .unwrap ( ) .refs ( ) .unwrap ( ) ;
6331
assert! ( refs.refs.iter ( ) .find ( | r | r.name = = " main" ) .unwrap ( ) .accepted.is_some ( ) ) ;
6332
a.pull ( " origin" ) .unwrap ( ) ;
6333
assert! ( matches! ( a.authenticate_change ( & change) .unwrap ( ) , SignatureStatus:: Valid { .. } ) ) ;
6334
a66c05a6 Andy McCall 12 days ago ↶
6335
6336
6337
6338
create ( & h.app, " andy" , " fresh" ) .await;
6339
let fresh_url = format! ( " {} /andy/fresh" , h.base) ;
6340
let fresh_dir = work.path ( ) .join ( " claude-fresh" ) ;
6341
let mut fresh = client ( & fresh_url, & fresh_dir) .with_home ( agent_home.clone ( ) ) ;
6342
fresh.config.set ( " remote.origin.token" , & token) .unwrap ( ) ;
6343
fresh.save_config ( ) .unwrap ( ) ;
6344
fresh.set_user_config ( " author.key" , & andy_key.to_hex ( ) ) .unwrap ( ) ;
6345
fresh.set_user_config ( " author.signer" , " keyless" ) .unwrap ( ) ;
6346
assert! ( fresh.store ( ) .get_identity ( & andy_key) .is_err ( ) , " the fresh repository has never seen Andy" ) ;
6347
write ( & fresh_dir, " first.txt" , " first\n " ) ;
6348
let first = fresh.commit ( " First, by the agent" , & [ ] ) .unwrap ( ) ;
6349
let r = fresh.push ( " origin" , false ) .unwrap ( ) ;
6350
assert! ( r.iter ( ) .all ( | x | x.ok ) , " {r:?}" ) ;
6351
assert! ( matches! ( fresh.authenticate_change ( & first) .unwrap ( ) , SignatureStatus:: Valid { .. } ) , " locally" ) ;
6352
let fresh_host = h.app.repos.open ( & RepoName:: parse( " andy/fresh" ) .unwrap ( ) ) .unwrap ( ) ;
6353
assert! (
6354
matches! ( fresh_host.authenticate_change ( & first) .unwrap ( ) , SignatureStatus:: Valid { sealed: true , .. } ) ,
6355
" on the hub: {:?}" ,
6356
fresh_host.authenticate_change ( & first) .unwrap ( )
6357
) ;
6358
8aa5ef38 Andy McCall 19 days ago ↶
6359
6360
let ( st, _ ) = call ( & h.app, " DELETE" , & format! ( " /api/v1/agents/grants/{grant_id} " ) , Some ( & pat) , None ) .await;
6361
assert_eq! ( st, StatusCode:: NO_CONTENT ) ;
6362
let ( late, dir) = agent ( ) ;
6363
write ( & dir, " late.txt" , " after the revocation\n " ) ;
6364
late.commit ( " Late" , & [ ] ) .unwrap ( ) ;
6365
let err = late.push ( " origin" , false ) .unwrap_err ( ) .to_string ( ) ;
559c3920 Andy McCall 19 days ago ↶
6366
assert! ( err.contains ( " not valid" ) | | err.contains ( " would not certify" ) , " {err}" ) ;
6367
6368
let by_agent: i64 = sqlx:: query_scalar( " SELECT COUNT(*) FROM pushes WHERE via = 'claude'" ) .fetch_one ( & h.app.db) .await.unwrap ( ) ;
6369
assert! ( by_agent > = 1 , " recorded as via the agent" ) ;
8aa5ef38 Andy McCall 19 days ago ↶
6370
let ( _ , body) = call ( & h.app, " GET" , " /api/v1/agents/grants" , Some ( & pat) , None ) .await;
6371
let list: Vec< serde_json:: Value> = serde_json:: from_str( & body) .unwrap ( ) ;
6372
assert! ( list[ 0 ] [ " revoked_ms" ] .is_i64 ( ) & & list[ 0 ] .get ( " token" ) .is_none ( ) , " {body}" ) ;
6373
}
a9f1c0bf Andy McCall 17 days ago ↶
6374
6375
6376
6377
6378
6379
# [ tokio ::test ( flavor = " multi_thread" ) ]
6380
async fn newest_run_wins ( ) {
6381
use oxy_core:: hash:: ChangeHash;
6382
let h = hub ( ) .await;
6383
db:: accounts:: create_account( & h.app.db, " andy" , " [email protected] " , " x" , " salt" , true ) .await.unwrap ( ) ;
6384
create ( & h.app, " andy" , " newest" ) .await;
6385
let url = format! ( " {} /andy/newest" , h.base) ;
6386
let work = tempfile:: tempdir( ) .unwrap ( ) ;
6387
let c = client ( & url, work.path ( ) ) ;
6388
write ( work.path ( ) , " .flow/pipeline.kdl" , & pipeline ( " " ) ) ;
6389
write ( work.path ( ) , " test.sh" , " echo ok\n " ) ;
6390
c.commit ( " Start" , & [ ] ) .unwrap ( ) ;
6391
assert! ( c.push ( " origin" , false ) .unwrap ( ) .iter ( ) .all ( | r | r.ok ) ) ;
6392
let pushed = runs_of ( & h.app, " andy" , " newest" ) .await[ 0 ] .clone ( ) ;
6393
assert_eq! ( ( pushed.number, pushed.trigger.as_str ( ) ) , ( 1 , " push" ) ) ;
6394
let heads: Vec< ChangeHash> = pushed.head_list ( ) .into_iter ( ) .filter_map ( ChangeHash:: from_hex) .collect ( ) ;
6395
let start = | row : db:: repos:: RepoRow, bookmark : & 'static str , trigger : & 'static str | {
6396
let ( app, state, heads) = ( h.app.clone ( ) , pushed.state.clone ( ) , heads.clone ( ) ) ;
6397
async move {
6398
oxygenhub:: flow:: start_run_scoped( & app, & row, bookmark, & state, & heads, " andy" , trigger, None ) .await.unwrap ( ) .expect ( " a run" )
6399
}
6400
} ;
6401
let row = db:: repos:: get( & h.app.db, " andy" , " newest" ) .await.unwrap ( ) .unwrap ( ) ;
6402
let run = | n : i64 | {
6403
let ( app, id) = ( h.app.clone ( ) , row.id) ;
6404
async move { db:: flow:: run( & app.db, id, n) .await.unwrap ( ) .unwrap ( ) }
6405
} ;
6406
let superseded_by = | n : i64 , by : i64 | {
6407
let app = h.app.clone ( ) ;
6408
let run = run ( n) ;
6409
async move {
6410
let r = run.await;
6411
assert_eq! ( r.status, " cancelled" , " run #{n} is superseded" ) ;
6412
let jobs = db:: flow:: jobs( & app.db, r.id) .await.unwrap ( ) ;
6413
let why = format! ( " superseded by run #{by} " ) ;
6414
assert! ( jobs.iter ( ) .all ( | j | j.status = = " cancelled" & & j.error = = why ) , " {jobs:?}" ) ;
6415
}
6416
} ;
6417
6418
6419
6420
assert_eq! ( start ( row.clone ( ) , " proposal/7" , " proposal" ) .await, 2 ) ;
6421
assert_eq! ( start ( row.clone ( ) , " proposal/7" , " proposal" ) .await, 3 ) ;
6422
superseded_by ( 2 , 3 ) .await;
6423
assert_eq! ( start ( row.clone ( ) , " proposal/8" , " proposal" ) .await, 4 ) ;
6424
assert! ( run ( 3 ) .await.live ( ) , " another proposal's run is not superseded" ) ;
6425
assert! ( run ( 1 ) .await.live ( ) , " a proposal's run leaves push runs alone" ) ;
6426
6427
6428
6429
assert_eq! ( start ( row.clone ( ) , " main" , " manual" ) .await, 5 ) ;
6430
assert_eq! ( start ( row.clone ( ) , " main" , " rerun:1:push" ) .await, 6 ) ;
6431
assert! ( run ( 1 ) .await.live ( ) , " a manual run or a rerun supersedes nothing" ) ;
6432
assert_eq! ( start ( row.clone ( ) , " main" , " push" ) .await, 7 ) ;
6433
superseded_by ( 1 , 7 ) .await;
6434
assert! ( run ( 5 ) .await.live ( ) & & run ( 6 ) .await.live ( ) , " a push leaves manual runs and reruns alone" ) ;
6435
6436
6437
6438
db:: repos:: set_flow_keep_running( & h.app.db, row.id, true ) .await.unwrap ( ) ;
6439
let kept = db:: repos:: get( & h.app.db, " andy" , " newest" ) .await.unwrap ( ) .unwrap ( ) ;
6440
assert! ( kept.flow_keep_running) ;
6441
let three = run ( 3 ) .await;
6442
sqlx:: query( " UPDATE flow_runs SET status = 'running' WHERE id = $1" ) .bind ( three.id) .execute ( & h.app.db) .await.unwrap ( ) ;
6443
sqlx:: query( " UPDATE flow_jobs SET status = 'running' WHERE run_id = $1 AND name = 'build'" )
6444
.bind ( three.id)
6445
.execute ( & h.app.db)
6446
.await
6447
.unwrap ( ) ;
6448
assert_eq! ( start ( kept.clone ( ) , " proposal/7" , " proposal" ) .await, 8 ) ;
6449
assert_eq! ( start ( kept.clone ( ) , " proposal/7" , " proposal" ) .await, 9 ) ;
6450
assert_eq! ( run ( 3 ) .await.status, " running" , " a running run is kept" ) ;
6451
superseded_by ( 8 , 9 ) .await;
6452
6453
db:: repos:: set_flow_keep_running( & h.app.db, row.id, false ) .await.unwrap ( ) ;
6454
let row = db:: repos:: get( & h.app.db, " andy" , " newest" ) .await.unwrap ( ) .unwrap ( ) ;
6455
assert_eq! ( start ( row.clone ( ) , " proposal/7" , " proposal" ) .await, 10 ) ;
6456
superseded_by ( 3 , 10 ) .await;
6457
superseded_by ( 9 , 10 ) .await;
6458
}