forge
Explore
Log in

Don't you dare go Hollow

Ci store tests share their store and merge into tables

Browse files at this commit

0fef3ae060b630e169179ce0c898a41c5e0c1341Ahmet Kaan GümüşParent: 5099461
  • test
    • ci store tests share their store and merge into tables
Show full message
✅ test: ci store tests share their store and merge into tables

Nine store scenarios that each work in their own repo and never claim
a job now run in one test over one store. The scenarios are helpers
that keep their asserts. Store tests in ci.rs go from 33 to 25.
A failure still points at the helper's line, and each helper is named
after its scenario.

Left separate: the runner tests (list_runners is global), the claim
and reap tests (claim_job and reap_lost_jobs see every repo's rows),
purge_old and delete_repo (global counts), and secrets_need_the_key
(needs a store without a secret key).
crates/forge-core/src/store/ci.rsmodified+74 -80
@@ -2296,17 +2296,16 @@ mod tests {
22962296 ));
22972297 }
22982298
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;
23022301 let other = fx
23032302 .store
23042303 .create_repo(&fx.tahinli.id, "other", "", Visibility::Public, "main")
23052304 .await
23062305 .unwrap();
23072306 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;
23102309 let o1 = create(&fx.store, &other.id, &fx.tahinli.id, &jobs, false).await;
23112310 assert_eq!((r1.number, r2.number, o1.number), (1, 2, 1));
23122311 assert_eq!(r1.state, RunState::Queued);
@@ -2320,7 +2319,7 @@ mod tests {
23202319 );
23212320 assert_eq!(
23222321 fx.store
2323- .run_by_number(&fx.repo.id, 2)
2322+ .run_by_number(&repo.id, 2)
23242323 .await
23252324 .unwrap()
23262325 .unwrap()
@@ -2331,7 +2330,7 @@ mod tests {
23312330 let broken = fx
23322331 .store
23332332 .create_run(
2334- &fx.repo.id,
2333+ &repo.id,
23352334 "push",
23362335 "refs/heads/main",
23372336 "abc123",
@@ -2348,32 +2347,31 @@ mod tests {
23482347 assert!(broken.finished_at.is_some());
23492348 assert!(fx.store.jobs_for_run(&broken.id).await.unwrap().is_empty());
23502349
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();
23522351 assert_eq!(total, 3);
23532352 assert_eq!(
23542353 page.iter().map(|r| r.number).collect::<Vec<_>>(),
23552354 vec![3, 2]
23562355 );
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();
23582357 assert_eq!(page2.iter().map(|r| r.number).collect::<Vec<_>>(), vec![1]);
23592358 }
23602359
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;
23642362 let jobs = [job("build", &[], &[])];
23652363 assert!(
23662364 fx.store
2367- .latest_run_on_ref(&fx.repo.id, "refs/heads/main")
2365+ .latest_run_on_ref(&repo.id, "refs/heads/main")
23682366 .await
23692367 .unwrap()
23702368 .is_none()
23712369 );
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;
23732371 let dev = fx
23742372 .store
23752373 .create_run(
2376- &fx.repo.id,
2374+ &repo.id,
23772375 "push",
23782376 "refs/heads/dev",
23792377 "abc123",
@@ -2385,10 +2383,10 @@ mod tests {
23852383 )
23862384 .await
23872385 .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;
23892387 let latest = fx
23902388 .store
2391- .latest_run_on_ref(&fx.repo.id, "refs/heads/main")
2389+ .latest_run_on_ref(&repo.id, "refs/heads/main")
23922390 .await
23932391 .unwrap()
23942392 .unwrap();
@@ -2396,25 +2394,24 @@ mod tests {
23962394 assert_ne!(latest.id, main1.id);
23972395 let on_dev = fx
23982396 .store
2399- .latest_run_on_ref(&fx.repo.id, "refs/heads/dev")
2397+ .latest_run_on_ref(&repo.id, "refs/heads/dev")
24002398 .await
24012399 .unwrap()
24022400 .unwrap();
24032401 assert_eq!(on_dev.id, dev.id);
24042402 assert!(
24052403 fx.store
2406- .latest_run_on_ref(&fx.repo.id, "refs/heads/nope")
2404+ .latest_run_on_ref(&repo.id, "refs/heads/nope")
24072405 .await
24082406 .unwrap()
24092407 .is_none()
24102408 );
24112409 }
24122410
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;
24162413 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;
24182415 assert_eq!(run.state, RunState::WaitingApproval);
24192416 assert_eq!(
24202417 job_named(&fx.store, &run.id, "build").await.state,
@@ -3011,11 +3008,10 @@ mod tests {
30113008 ));
30123009 }
30133010
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;
30173013 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;
30193015 let again = fx.store.rerun(&first.id, &fx.tahinli.id).await.unwrap();
30203016 assert_eq!(again.number, 2);
30213017 assert_eq!(again.commit_oid, first.commit_oid);
@@ -3038,16 +3034,15 @@ mod tests {
30383034 ));
30393035 }
30403036
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;
30443039 let jobs = [
30453040 job("a", &[], &[]),
30463041 job("b", &["a"], &[]),
30473042 job("c", &["b"], &[]),
30483043 job("d", &[], &[]),
30493044 ];
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;
30513046 assert!(matches!(
30523047 fx.store.rerun_failed(&first.id, &fx.tahinli.id).await,
30533048 Err(Error::Conflict(_))
@@ -3108,11 +3103,10 @@ mod tests {
31083103 ));
31093104 }
31103105
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;
31143108 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;
31163110 assert!(fx.store.run_warnings(&run.id).await.unwrap().is_empty());
31173111 let warnings = vec!["unknown key `on.schedule`".to_owned(), "second".to_owned()];
31183112 fx.store.set_run_warnings(&run.id, &warnings).await.unwrap();
@@ -3121,27 +3115,26 @@ mod tests {
31213115 assert_eq!(fx.store.run_warnings(&again.id).await.unwrap(), warnings);
31223116 }
31233117
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;
31273120 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;
31293122 let found = fx
31303123 .store
3131- .superseded_candidates(&fx.repo.id, "refs/heads/main", "def456")
3124+ .superseded_candidates(&repo.id, "refs/heads/main", "def456")
31323125 .await
31333126 .unwrap();
31343127 assert_eq!(found.len(), 1);
31353128 assert_eq!(found[0].id, first.id);
31363129 let same = fx
31373130 .store
3138- .superseded_candidates(&fx.repo.id, "refs/heads/main", "abc123")
3131+ .superseded_candidates(&repo.id, "refs/heads/main", "abc123")
31393132 .await
31403133 .unwrap();
31413134 assert!(same.is_empty());
31423135 let elsewhere = fx
31433136 .store
3144- .superseded_candidates(&fx.repo.id, "refs/heads/other", "def456")
3137+ .superseded_candidates(&repo.id, "refs/heads/other", "def456")
31453138 .await
31463139 .unwrap();
31473140 assert!(elsewhere.is_empty());
@@ -3228,12 +3221,11 @@ mod tests {
32283221 assert!(logs2.is_empty() && artifacts2.is_empty());
32293222 }
32303223
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;
32343226 let run = create(
32353227 &fx.store,
3236- &fx.repo.id,
3228+ &repo.id,
32373229 &fx.tahinli.id,
32383230 &[job("a", &[], &[])],
32393231 false,
@@ -3260,16 +3252,15 @@ mod tests {
32603252 );
32613253 }
32623254
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;
32663257 let plain = "hunter2-very-secret-value";
32673258 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())
32693260 .await
32703261 .unwrap();
32713262 fx.store
3272- .set_ci_secret(&fx.repo.id, "TOKEN", b"second")
3263+ .set_ci_secret(&repo.id, "TOKEN", b"second")
32733264 .await
32743265 .unwrap();
32753266
@@ -3279,7 +3270,7 @@ mod tests {
32793270 let raw = fetch_opt(
32803271 &conn,
32813272 "SELECT sealed FROM ci_secrets WHERE repo_id = ?1 AND name = 'DEPLOY_KEY'",
3282- (fx.repo.id.clone(),),
3273+ (repo.id.clone(),),
32833274 |row| col_blob(row, 0),
32843275 )
32853276 .await
@@ -3288,7 +3279,7 @@ mod tests {
32883279 assert!(!raw.windows(plain.len()).any(|w| w == plain.as_bytes()));
32893280 }
32903281
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();
32923283 assert_eq!(
32933284 names.iter().map(|(n, _)| n.as_str()).collect::<Vec<_>>(),
32943285 vec!["DEPLOY_KEY", "TOKEN"]
@@ -3299,10 +3290,7 @@ mod tests {
32993290
33003291 let values = fx
33013292 .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()])
33063294 .await
33073295 .unwrap();
33083296 assert_eq!(values.len(), 1);
@@ -3310,20 +3298,16 @@ mod tests {
33103298
33113299 // Replacing keeps one row.
33123300 fx.store
3313- .set_ci_secret(&fx.repo.id, "DEPLOY_KEY", b"rotated")
3301+ .set_ci_secret(&repo.id, "DEPLOY_KEY", b"rotated")
33143302 .await
33153303 .unwrap();
33163304 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(),
33223306 2
33233307 );
33243308 let rotated = fx
33253309 .store
3326- .ci_secret_values(&fx.repo.id, &["DEPLOY_KEY".to_string()])
3310+ .ci_secret_values(&repo.id, &["DEPLOY_KEY".to_string()])
33273311 .await
33283312 .unwrap();
33293313 assert_eq!(
@@ -3331,30 +3315,40 @@ mod tests {
33313315 Some("rotated")
33323316 );
33333317
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());
33463320 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,
33483322 Err(Error::Invalid(_))
33493323 ));
33503324 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,
33543326 Err(Error::Invalid(_))
33553327 ));
33563328 }
33573329
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+
33583352 #[tokio::test]
33593353 async fn secrets_need_the_key() {
33603354 let dir = tempfile::tempdir().unwrap();