Ci store tests share their store and merge into tables
Browse files at this commit0fef3ae060b630e169179ce0c898a41c5e0c1341Parent: 5099461- test
- ci store tests share their store and merge into tables
1 changed file +74 -80
crates/forge-core/src/store/ci.rsmodified+74 -80
| @@ -2296,17 +2296,16 @@ mod tests { | ||
| 2296 | 2296 | )); |
| 2297 | 2297 | } |
| 2298 | 2298 | |
| 2299 | - #[tokio::test] | |
| 2300 | - async fn create_run_numbers_per_repo() { | |
| 2301 | - let fx = fixture().await; | |
| 2299 | + async fn numbering_is_per_repo(fx: &Fx) { | |
| 2300 | + let repo = repo_named(fx, "numbers").await; | |
| 2302 | 2301 | let other = fx |
| 2303 | 2302 | .store |
| 2304 | 2303 | .create_repo(&fx.tahinli.id, "other", "", Visibility::Public, "main") |
| 2305 | 2304 | .await |
| 2306 | 2305 | .unwrap(); |
| 2307 | 2306 | let jobs = [job("build", &[], &[]), job("test", &["build"], &[])]; |
| 2308 | - let r1 = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 2309 | - let r2 = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 2307 | + let r1 = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 2308 | + let r2 = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 2310 | 2309 | let o1 = create(&fx.store, &other.id, &fx.tahinli.id, &jobs, false).await; |
| 2311 | 2310 | assert_eq!((r1.number, r2.number, o1.number), (1, 2, 1)); |
| 2312 | 2311 | assert_eq!(r1.state, RunState::Queued); |
| @@ -2320,7 +2319,7 @@ mod tests { | ||
| 2320 | 2319 | ); |
| 2321 | 2320 | assert_eq!( |
| 2322 | 2321 | fx.store |
| 2323 | - .run_by_number(&fx.repo.id, 2) | |
| 2322 | + .run_by_number(&repo.id, 2) | |
| 2324 | 2323 | .await |
| 2325 | 2324 | .unwrap() |
| 2326 | 2325 | .unwrap() |
| @@ -2331,7 +2330,7 @@ mod tests { | ||
| 2331 | 2330 | let broken = fx |
| 2332 | 2331 | .store |
| 2333 | 2332 | .create_run( |
| 2334 | - &fx.repo.id, | |
| 2333 | + &repo.id, | |
| 2335 | 2334 | "push", |
| 2336 | 2335 | "refs/heads/main", |
| 2337 | 2336 | "abc123", |
| @@ -2348,32 +2347,31 @@ mod tests { | ||
| 2348 | 2347 | assert!(broken.finished_at.is_some()); |
| 2349 | 2348 | assert!(fx.store.jobs_for_run(&broken.id).await.unwrap().is_empty()); |
| 2350 | 2349 | |
| 2351 | - let (page, total) = fx.store.list_runs(&fx.repo.id, 1, 2).await.unwrap(); | |
| 2350 | + let (page, total) = fx.store.list_runs(&repo.id, 1, 2).await.unwrap(); | |
| 2352 | 2351 | assert_eq!(total, 3); |
| 2353 | 2352 | assert_eq!( |
| 2354 | 2353 | page.iter().map(|r| r.number).collect::<Vec<_>>(), |
| 2355 | 2354 | vec![3, 2] |
| 2356 | 2355 | ); |
| 2357 | - let (page2, _) = fx.store.list_runs(&fx.repo.id, 2, 2).await.unwrap(); | |
| 2356 | + let (page2, _) = fx.store.list_runs(&repo.id, 2, 2).await.unwrap(); | |
| 2358 | 2357 | assert_eq!(page2.iter().map(|r| r.number).collect::<Vec<_>>(), vec![1]); |
| 2359 | 2358 | } |
| 2360 | 2359 | |
| 2361 | - #[tokio::test] | |
| 2362 | - async fn latest_run_on_ref_picks_newest_per_branch() { | |
| 2363 | - let fx = fixture().await; | |
| 2360 | + async fn newest_run_per_ref(fx: &Fx) { | |
| 2361 | + let repo = repo_named(fx, "latest").await; | |
| 2364 | 2362 | let jobs = [job("build", &[], &[])]; |
| 2365 | 2363 | assert!( |
| 2366 | 2364 | fx.store |
| 2367 | - .latest_run_on_ref(&fx.repo.id, "refs/heads/main") | |
| 2365 | + .latest_run_on_ref(&repo.id, "refs/heads/main") | |
| 2368 | 2366 | .await |
| 2369 | 2367 | .unwrap() |
| 2370 | 2368 | .is_none() |
| 2371 | 2369 | ); |
| 2372 | - let main1 = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 2370 | + let main1 = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 2373 | 2371 | let dev = fx |
| 2374 | 2372 | .store |
| 2375 | 2373 | .create_run( |
| 2376 | - &fx.repo.id, | |
| 2374 | + &repo.id, | |
| 2377 | 2375 | "push", |
| 2378 | 2376 | "refs/heads/dev", |
| 2379 | 2377 | "abc123", |
| @@ -2385,10 +2383,10 @@ mod tests { | ||
| 2385 | 2383 | ) |
| 2386 | 2384 | .await |
| 2387 | 2385 | .unwrap(); |
| 2388 | - let main2 = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 2386 | + let main2 = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 2389 | 2387 | let latest = fx |
| 2390 | 2388 | .store |
| 2391 | - .latest_run_on_ref(&fx.repo.id, "refs/heads/main") | |
| 2389 | + .latest_run_on_ref(&repo.id, "refs/heads/main") | |
| 2392 | 2390 | .await |
| 2393 | 2391 | .unwrap() |
| 2394 | 2392 | .unwrap(); |
| @@ -2396,25 +2394,24 @@ mod tests { | ||
| 2396 | 2394 | assert_ne!(latest.id, main1.id); |
| 2397 | 2395 | let on_dev = fx |
| 2398 | 2396 | .store |
| 2399 | - .latest_run_on_ref(&fx.repo.id, "refs/heads/dev") | |
| 2397 | + .latest_run_on_ref(&repo.id, "refs/heads/dev") | |
| 2400 | 2398 | .await |
| 2401 | 2399 | .unwrap() |
| 2402 | 2400 | .unwrap(); |
| 2403 | 2401 | assert_eq!(on_dev.id, dev.id); |
| 2404 | 2402 | assert!( |
| 2405 | 2403 | fx.store |
| 2406 | - .latest_run_on_ref(&fx.repo.id, "refs/heads/nope") | |
| 2404 | + .latest_run_on_ref(&repo.id, "refs/heads/nope") | |
| 2407 | 2405 | .await |
| 2408 | 2406 | .unwrap() |
| 2409 | 2407 | .is_none() |
| 2410 | 2408 | ); |
| 2411 | 2409 | } |
| 2412 | 2410 | |
| 2413 | - #[tokio::test] | |
| 2414 | - async fn approval_flow() { | |
| 2415 | - let fx = fixture().await; | |
| 2411 | + async fn approval_holds_the_run(fx: &Fx) { | |
| 2412 | + let repo = repo_named(fx, "approval").await; | |
| 2416 | 2413 | let jobs = [job("build", &[], &[]), job("test", &["build"], &[])]; |
| 2417 | - let run = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, true).await; | |
| 2414 | + let run = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, true).await; | |
| 2418 | 2415 | assert_eq!(run.state, RunState::WaitingApproval); |
| 2419 | 2416 | assert_eq!( |
| 2420 | 2417 | job_named(&fx.store, &run.id, "build").await.state, |
| @@ -3011,11 +3008,10 @@ mod tests { | ||
| 3011 | 3008 | )); |
| 3012 | 3009 | } |
| 3013 | 3010 | |
| 3014 | - #[tokio::test] | |
| 3015 | - async fn rerun_copies_commit_and_specs() { | |
| 3016 | - let fx = fixture().await; | |
| 3011 | + async fn rerun_copies_commit_and_specs(fx: &Fx) { | |
| 3012 | + let repo = repo_named(fx, "rerun").await; | |
| 3017 | 3013 | let jobs = [job("build", &[], &["linux"]), job("test", &["build"], &[])]; |
| 3018 | - let first = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 3014 | + let first = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 3019 | 3015 | let again = fx.store.rerun(&first.id, &fx.tahinli.id).await.unwrap(); |
| 3020 | 3016 | assert_eq!(again.number, 2); |
| 3021 | 3017 | assert_eq!(again.commit_oid, first.commit_oid); |
| @@ -3038,16 +3034,15 @@ mod tests { | ||
| 3038 | 3034 | )); |
| 3039 | 3035 | } |
| 3040 | 3036 | |
| 3041 | - #[tokio::test] | |
| 3042 | - async fn partial_reruns_take_failed_jobs_and_their_dependants() { | |
| 3043 | - let fx = fixture().await; | |
| 3037 | + async fn partial_reruns_take_failed_jobs_and_their_dependants(fx: &Fx) { | |
| 3038 | + let repo = repo_named(fx, "partial").await; | |
| 3044 | 3039 | let jobs = [ |
| 3045 | 3040 | job("a", &[], &[]), |
| 3046 | 3041 | job("b", &["a"], &[]), |
| 3047 | 3042 | job("c", &["b"], &[]), |
| 3048 | 3043 | job("d", &[], &[]), |
| 3049 | 3044 | ]; |
| 3050 | - let first = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 3045 | + let first = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 3051 | 3046 | assert!(matches!( |
| 3052 | 3047 | fx.store.rerun_failed(&first.id, &fx.tahinli.id).await, |
| 3053 | 3048 | Err(Error::Conflict(_)) |
| @@ -3108,11 +3103,10 @@ mod tests { | ||
| 3108 | 3103 | )); |
| 3109 | 3104 | } |
| 3110 | 3105 | |
| 3111 | - #[tokio::test] | |
| 3112 | - async fn warnings_are_stored_and_carried_into_reruns() { | |
| 3113 | - let fx = fixture().await; | |
| 3106 | + async fn warnings_are_stored_and_carried_into_reruns(fx: &Fx) { | |
| 3107 | + let repo = repo_named(fx, "warnings").await; | |
| 3114 | 3108 | let jobs = [job("a", &[], &[])]; |
| 3115 | - let run = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 3109 | + let run = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 3116 | 3110 | assert!(fx.store.run_warnings(&run.id).await.unwrap().is_empty()); |
| 3117 | 3111 | let warnings = vec!["unknown key `on.schedule`".to_owned(), "second".to_owned()]; |
| 3118 | 3112 | fx.store.set_run_warnings(&run.id, &warnings).await.unwrap(); |
| @@ -3121,27 +3115,26 @@ mod tests { | ||
| 3121 | 3115 | assert_eq!(fx.store.run_warnings(&again.id).await.unwrap(), warnings); |
| 3122 | 3116 | } |
| 3123 | 3117 | |
| 3124 | - #[tokio::test] | |
| 3125 | - async fn superseded_candidates_are_older_live_push_runs_on_the_ref() { | |
| 3126 | - let fx = fixture().await; | |
| 3118 | + async fn superseded_candidates_are_older_live_push_runs_on_the_ref(fx: &Fx) { | |
| 3119 | + let repo = repo_named(fx, "superseded").await; | |
| 3127 | 3120 | let jobs = [job("a", &[], &[])]; |
| 3128 | - let first = create(&fx.store, &fx.repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 3121 | + let first = create(&fx.store, &repo.id, &fx.tahinli.id, &jobs, false).await; | |
| 3129 | 3122 | let found = fx |
| 3130 | 3123 | .store |
| 3131 | - .superseded_candidates(&fx.repo.id, "refs/heads/main", "def456") | |
| 3124 | + .superseded_candidates(&repo.id, "refs/heads/main", "def456") | |
| 3132 | 3125 | .await |
| 3133 | 3126 | .unwrap(); |
| 3134 | 3127 | assert_eq!(found.len(), 1); |
| 3135 | 3128 | assert_eq!(found[0].id, first.id); |
| 3136 | 3129 | let same = fx |
| 3137 | 3130 | .store |
| 3138 | - .superseded_candidates(&fx.repo.id, "refs/heads/main", "abc123") | |
| 3131 | + .superseded_candidates(&repo.id, "refs/heads/main", "abc123") | |
| 3139 | 3132 | .await |
| 3140 | 3133 | .unwrap(); |
| 3141 | 3134 | assert!(same.is_empty()); |
| 3142 | 3135 | let elsewhere = fx |
| 3143 | 3136 | .store |
| 3144 | - .superseded_candidates(&fx.repo.id, "refs/heads/other", "def456") | |
| 3137 | + .superseded_candidates(&repo.id, "refs/heads/other", "def456") | |
| 3145 | 3138 | .await |
| 3146 | 3139 | .unwrap(); |
| 3147 | 3140 | assert!(elsewhere.is_empty()); |
| @@ -3228,12 +3221,11 @@ mod tests { | ||
| 3228 | 3221 | assert!(logs2.is_empty() && artifacts2.is_empty()); |
| 3229 | 3222 | } |
| 3230 | 3223 | |
| 3231 | - #[tokio::test] | |
| 3232 | - async fn artifacts_are_unique_per_job_and_listed() { | |
| 3233 | - let fx = fixture().await; | |
| 3224 | + async fn artifacts_are_unique_per_job_and_listed(fx: &Fx) { | |
| 3225 | + let repo = repo_named(fx, "artifacts").await; | |
| 3234 | 3226 | let run = create( |
| 3235 | 3227 | &fx.store, |
| 3236 | - &fx.repo.id, | |
| 3228 | + &repo.id, | |
| 3237 | 3229 | &fx.tahinli.id, |
| 3238 | 3230 | &[job("a", &[], &[])], |
| 3239 | 3231 | false, |
| @@ -3260,16 +3252,15 @@ mod tests { | ||
| 3260 | 3252 | ); |
| 3261 | 3253 | } |
| 3262 | 3254 | |
| 3263 | - #[tokio::test] | |
| 3264 | - async fn secrets_seal_round_trip_and_list_never_returns_values() { | |
| 3265 | - let fx = fixture().await; | |
| 3255 | + async fn secrets_seal_round_trip_and_list_never_returns_values(fx: &Fx) { | |
| 3256 | + let repo = repo_named(fx, "secrets").await; | |
| 3266 | 3257 | let plain = "hunter2-very-secret-value"; |
| 3267 | 3258 | fx.store |
| 3268 | - .set_ci_secret(&fx.repo.id, "DEPLOY_KEY", plain.as_bytes()) | |
| 3259 | + .set_ci_secret(&repo.id, "DEPLOY_KEY", plain.as_bytes()) | |
| 3269 | 3260 | .await |
| 3270 | 3261 | .unwrap(); |
| 3271 | 3262 | fx.store |
| 3272 | - .set_ci_secret(&fx.repo.id, "TOKEN", b"second") | |
| 3263 | + .set_ci_secret(&repo.id, "TOKEN", b"second") | |
| 3273 | 3264 | .await |
| 3274 | 3265 | .unwrap(); |
| 3275 | 3266 | |
| @@ -3279,7 +3270,7 @@ mod tests { | ||
| 3279 | 3270 | let raw = fetch_opt( |
| 3280 | 3271 | &conn, |
| 3281 | 3272 | "SELECT sealed FROM ci_secrets WHERE repo_id = ?1 AND name = 'DEPLOY_KEY'", |
| 3282 | - (fx.repo.id.clone(),), | |
| 3273 | + (repo.id.clone(),), | |
| 3283 | 3274 | |row| col_blob(row, 0), |
| 3284 | 3275 | ) |
| 3285 | 3276 | .await |
| @@ -3288,7 +3279,7 @@ mod tests { | ||
| 3288 | 3279 | assert!(!raw.windows(plain.len()).any(|w| w == plain.as_bytes())); |
| 3289 | 3280 | } |
| 3290 | 3281 | |
| 3291 | - let names = fx.store.list_ci_secret_names(&fx.repo.id).await.unwrap(); | |
| 3282 | + let names = fx.store.list_ci_secret_names(&repo.id).await.unwrap(); | |
| 3292 | 3283 | assert_eq!( |
| 3293 | 3284 | names.iter().map(|(n, _)| n.as_str()).collect::<Vec<_>>(), |
| 3294 | 3285 | vec!["DEPLOY_KEY", "TOKEN"] |
| @@ -3299,10 +3290,7 @@ mod tests { | ||
| 3299 | 3290 | |
| 3300 | 3291 | let values = fx |
| 3301 | 3292 | .store |
| 3302 | - .ci_secret_values( | |
| 3303 | - &fx.repo.id, | |
| 3304 | - &["DEPLOY_KEY".to_string(), "MISSING".to_string()], | |
| 3305 | - ) | |
| 3293 | + .ci_secret_values(&repo.id, &["DEPLOY_KEY".to_string(), "MISSING".to_string()]) | |
| 3306 | 3294 | .await |
| 3307 | 3295 | .unwrap(); |
| 3308 | 3296 | assert_eq!(values.len(), 1); |
| @@ -3310,20 +3298,16 @@ mod tests { | ||
| 3310 | 3298 | |
| 3311 | 3299 | // Replacing keeps one row. |
| 3312 | 3300 | fx.store |
| 3313 | - .set_ci_secret(&fx.repo.id, "DEPLOY_KEY", b"rotated") | |
| 3301 | + .set_ci_secret(&repo.id, "DEPLOY_KEY", b"rotated") | |
| 3314 | 3302 | .await |
| 3315 | 3303 | .unwrap(); |
| 3316 | 3304 | assert_eq!( |
| 3317 | - fx.store | |
| 3318 | - .list_ci_secret_names(&fx.repo.id) | |
| 3319 | - .await | |
| 3320 | - .unwrap() | |
| 3321 | - .len(), | |
| 3305 | + fx.store.list_ci_secret_names(&repo.id).await.unwrap().len(), | |
| 3322 | 3306 | 2 |
| 3323 | 3307 | ); |
| 3324 | 3308 | let rotated = fx |
| 3325 | 3309 | .store |
| 3326 | - .ci_secret_values(&fx.repo.id, &["DEPLOY_KEY".to_string()]) | |
| 3310 | + .ci_secret_values(&repo.id, &["DEPLOY_KEY".to_string()]) | |
| 3327 | 3311 | .await |
| 3328 | 3312 | .unwrap(); |
| 3329 | 3313 | assert_eq!( |
| @@ -3331,30 +3315,40 @@ mod tests { | ||
| 3331 | 3315 | Some("rotated") |
| 3332 | 3316 | ); |
| 3333 | 3317 | |
| 3334 | - assert!( | |
| 3335 | - fx.store | |
| 3336 | - .delete_ci_secret(&fx.repo.id, "TOKEN") | |
| 3337 | - .await | |
| 3338 | - .unwrap() | |
| 3339 | - ); | |
| 3340 | - assert!( | |
| 3341 | - !fx.store | |
| 3342 | - .delete_ci_secret(&fx.repo.id, "TOKEN") | |
| 3343 | - .await | |
| 3344 | - .unwrap() | |
| 3345 | - ); | |
| 3318 | + assert!(fx.store.delete_ci_secret(&repo.id, "TOKEN").await.unwrap()); | |
| 3319 | + assert!(!fx.store.delete_ci_secret(&repo.id, "TOKEN").await.unwrap()); | |
| 3346 | 3320 | assert!(matches!( |
| 3347 | - fx.store.set_ci_secret(&fx.repo.id, "lower", b"x").await, | |
| 3321 | + fx.store.set_ci_secret(&repo.id, "lower", b"x").await, | |
| 3348 | 3322 | Err(Error::Invalid(_)) |
| 3349 | 3323 | )); |
| 3350 | 3324 | assert!(matches!( |
| 3351 | - fx.store | |
| 3352 | - .set_ci_secret(&fx.repo.id, "BAD", &[0xff, 0xfe]) | |
| 3353 | - .await, | |
| 3325 | + fx.store.set_ci_secret(&repo.id, "BAD", &[0xff, 0xfe]).await, | |
| 3354 | 3326 | Err(Error::Invalid(_)) |
| 3355 | 3327 | )); |
| 3356 | 3328 | } |
| 3357 | 3329 | |
| 3330 | + async fn repo_named(fx: &Fx, name: &str) -> Repo { | |
| 3331 | + fx.store | |
| 3332 | + .create_repo(&fx.tahinli.id, name, "", Visibility::Public, "main") | |
| 3333 | + .await | |
| 3334 | + .unwrap() | |
| 3335 | + } | |
| 3336 | + | |
| 3337 | + /// Each scenario works in its own repo and leaves its jobs unclaimed, so they share one store. | |
| 3338 | + #[tokio::test] | |
| 3339 | + async fn repo_scenarios_share_one_store() { | |
| 3340 | + let fx = fixture().await; | |
| 3341 | + numbering_is_per_repo(&fx).await; | |
| 3342 | + newest_run_per_ref(&fx).await; | |
| 3343 | + approval_holds_the_run(&fx).await; | |
| 3344 | + rerun_copies_commit_and_specs(&fx).await; | |
| 3345 | + partial_reruns_take_failed_jobs_and_their_dependants(&fx).await; | |
| 3346 | + warnings_are_stored_and_carried_into_reruns(&fx).await; | |
| 3347 | + superseded_candidates_are_older_live_push_runs_on_the_ref(&fx).await; | |
| 3348 | + artifacts_are_unique_per_job_and_listed(&fx).await; | |
| 3349 | + secrets_seal_round_trip_and_list_never_returns_values(&fx).await; | |
| 3350 | + } | |
| 3351 | + | |
| 3358 | 3352 | #[tokio::test] |
| 3359 | 3353 | async fn secrets_need_the_key() { |
| 3360 | 3354 | let dir = tempfile::tempdir().unwrap(); |