forge
Explore
Log in

Don't you dare go Hollow

Ci page tests share their fixtures and merge into tables

Browse files at this commit

9d648536cec7a7512b0a773e881921bdc8101c5dAhmet Kaan GümüşParent: a7d2b6c
  • test
    • ci page tests share their fixtures and merge into tables
Show full message
✅ test: ci page tests share their fixtures and merge into tables

api_tests 32 -> 26, schedule 17 -> 11, release 10 -> 8 (59 -> 45).
Folded checks that needed no fresh store into the test that already had
one (unknown secret, blank runner name, missing log offset, memory
overrides). Merged the OOM, dependant, self-update, cancel, glob, manual,
pull request and failed-run cases into one test each, one branch, tag or
run number per scenario. The runner binary stream test is unchanged.
crates/forge-web/src/ci/api_tests.rsmodified+56 -73
@@ -224,20 +224,10 @@ async fn register_good_and_reused_tokens() {
224 224
225 let reused = send(&env.router, "POST", uri, &[], json_body(&body)).await;225 let reused = send(&env.router, "POST", uri, &[], json_body(&body)).await;
226 assert_eq!(reused.status, 401, "{}", reused.json());226 assert_eq!(reused.status, 401, "{}", reused.json());
227-}
228 227
229-#[tokio::test]228+ // A blank runner name is refused before the token is looked at.
230-async fn register_rejects_bad_names() {229+ let bad = json!({ "token": "unused", "name": " ", "labels": [] });
231- let env = bare(|_| {}).await;230+ let sent = send(&env.router, "POST", uri, &[], json_body(&bad)).await;
232- let body = json!({ "token": "unused", "name": " ", "labels": [] });
233- let sent = send(
234- &env.router,
235- "POST",
236- "/api/v1/ci/runners/register",
237- &[],
238- json_body(&body),
239- )
240- .await;
241 assert_eq!(sent.status, 422, "{}", sent.json());231 assert_eq!(sent.status, 422, "{}", sent.json());
242 }232 }
243 233
@@ -246,11 +236,14 @@ async fn register_rejects_bad_names() {
246 // ---------------------------------------------------------------------------------------------236 // ---------------------------------------------------------------------------------------------
247 237
248 #[tokio::test]238 #[tokio::test]
249-async fn claim_is_204_without_work_and_200_with_work() {239+async fn claim_answers_204_200_and_401_by_state() {
250 let env = bare(|_| {}).await;240 let env = bare(|_| {}).await;
251 let (_, secret) = runner(&env, &["linux"]).await;241 let (_, secret) = runner(&env, &["linux"]).await;
252 let sent = claim(&env.router, &secret).await;242 let sent = claim(&env.router, &secret).await;
253 assert_eq!(sent.status, 204);243 assert_eq!(sent.status, 204);
244+ // A secret that matches no runner is refused.
245+ let stranger = claim(&env.router, "not-a-secret").await;
246+ assert_eq!(stranger.status, 401);
254 247
255 make_run(&env, "push", None, COMMIT, &[job("build", spec(&[], &[]))]).await;248 make_run(&env, "push", None, COMMIT, &[job("build", spec(&[], &[]))]).await;
256 let assignment = claim_job(&env.router, &secret).await;249 let assignment = claim_job(&env.router, &secret).await;
@@ -298,13 +291,6 @@ async fn disabled_runner_gets_403() {
298 assert_eq!(sent.status, 403, "{}", sent.json());291 assert_eq!(sent.status, 403, "{}", sent.json());
299 }292 }
300 293
301-#[tokio::test]
302-async fn unknown_runner_secret_is_401() {
303- let env = bare(|_| {}).await;
304- let sent = claim(&env.router, "not-a-secret").await;
305- assert_eq!(sent.status, 401);
306-}
307-
308 // ---------------------------------------------------------------------------------------------294 // ---------------------------------------------------------------------------------------------
309 // Secrets on fork-PR runs295 // Secrets on fork-PR runs
310 // ---------------------------------------------------------------------------------------------296 // ---------------------------------------------------------------------------------------------
@@ -399,21 +385,13 @@ async fn logs_append_retry_mismatch_and_mask() {
399 std::fs::read_to_string(&path).unwrap(),385 std::fs::read_to_string(&path).unwrap(),
400 "hello *** world\nmore\n"386 "hello *** world\nmore\n"
401 );387 );
402-}
403 388
404-#[tokio::test]389+ // A chunk without the offset header is a bad request.
405-async fn log_offset_header_is_required() {
406- let env = bare(|_| {}).await;
407- let (_, secret) = runner(&env, &[]).await;
408- make_run(&env, "push", None, COMMIT, &[job("a", spec(&[], &[]))]).await;
409- let assignment = claim_job(&env.router, &secret).await;
410- let job_id = assignment["job_id"].as_str().unwrap();
411- let token = assignment["job_token"].as_str().unwrap();
412 let sent = send(390 let sent = send(
413 &env.router,391 &env.router,
414 "POST",392 "POST",
415 &format!("/api/v1/ci/jobs/{job_id}/logs"),393 &format!("/api/v1/ci/jobs/{job_id}/logs"),
416- &[bearer(token)],394+ &[bearer(&token)],
417 b"text".to_vec(),395 b"text".to_vec(),
418 )396 )
419 .await;397 .await;
@@ -556,6 +534,7 @@ async fn claim_with_memory(router: &Router, secret: &str, free_gib: u64) -> Sent
556 async fn claim_hands_out_only_a_job_that_fits_the_free_memory() {534 async fn claim_hands_out_only_a_job_that_fits_the_free_memory() {
557 let env = bare(|_| {}).await;535 let env = bare(|_| {}).await;
558 let (runner, secret) = runner(&env, &[]).await;536 let (runner, secret) = runner(&env, &[]).await;
537+ // Scenario 1: jobs are sized against the free memory the runner reports.
559 make_run(538 make_run(
560 &env,539 &env,
561 "push",540 "push",
@@ -587,6 +566,28 @@ async fn claim_hands_out_only_a_job_that_fits_the_free_memory() {
587 let sent = claim_with_memory(&env.router, &secret, 5).await;566 let sent = claim_with_memory(&env.router, &secret, 5).await;
588 assert_eq!(sent.json()["job_name"], "big");567 assert_eq!(sent.json()["job_name"], "big");
589 assert_eq!(sent.json()["memory_bytes"], 4u64 << 30);568 assert_eq!(sent.json()["memory_bytes"], 4u64 << 30);
569+
570+ // Memory overrides from the admin travel on the claim answer. None before they are set.
571+ let sent = claim_with_memory(&env.router, &secret, 6).await;
572+ assert!(sent.headers.get("Forge-Runner-Memory-Budget").is_none());
573+ assert!(sent.headers.get("Forge-Runner-Reserve-Memory").is_none());
574+ env.store
575+ .set_runner_field(&runner.id, "limits.memory_budget", Some("5g"))
576+ .await
577+ .unwrap();
578+ env.store
579+ .set_runner_field(&runner.id, "limits.reserve_memory", Some("2g"))
580+ .await
581+ .unwrap();
582+ let sent = claim_with_memory(&env.router, &secret, 6).await;
583+ assert_eq!(
584+ sent.headers.get("Forge-Runner-Memory-Budget").unwrap(),
585+ "5g"
586+ );
587+ assert_eq!(
588+ sent.headers.get("Forge-Runner-Reserve-Memory").unwrap(),
589+ "2g"
590+ );
590 }591 }
591 592
592 #[tokio::test]593 #[tokio::test]
@@ -626,32 +627,6 @@ async fn an_old_runner_without_memory_fields_still_gets_jobs() {
626 assert_eq!(listed.reported.memory_budget, None);627 assert_eq!(listed.reported.memory_budget, None);
627 }628 }
628 629
629-#[tokio::test]
630-async fn memory_overrides_from_the_admin_travel_on_the_claim_answer() {
631- let env = bare(|_| {}).await;
632- let (runner, secret) = runner(&env, &[]).await;
633- let sent = claim_with_memory(&env.router, &secret, 6).await;
634- assert!(sent.headers.get("Forge-Runner-Memory-Budget").is_none());
635- assert!(sent.headers.get("Forge-Runner-Reserve-Memory").is_none());
636- env.store
637- .set_runner_field(&runner.id, "limits.memory_budget", Some("5g"))
638- .await
639- .unwrap();
640- env.store
641- .set_runner_field(&runner.id, "limits.reserve_memory", Some("2g"))
642- .await
643- .unwrap();
644- let sent = claim_with_memory(&env.router, &secret, 6).await;
645- assert_eq!(
646- sent.headers.get("Forge-Runner-Memory-Budget").unwrap(),
647- "5g"
648- );
649- assert_eq!(
650- sent.headers.get("Forge-Runner-Reserve-Memory").unwrap(),
651- "2g"
652- );
653-}
654-
655 async fn post_claim(router: &Router, secret: &str, boot: &str) -> Sent {630 async fn post_claim(router: &Router, secret: &str, boot: &str) -> Sent {
656 let body = serde_json::json!({ "labels": [], "boot_id": boot, "capabilities": ["restart"] });631 let body = serde_json::json!({ "labels": [], "boot_id": boot, "capabilities": ["restart"] });
657 send(632 send(
@@ -891,11 +866,13 @@ async fn requeue_hands_the_job_back_and_it_can_be_claimed_again() {
891 }866 }
892 867
893 #[tokio::test]868 #[tokio::test]
894-async fn finish_stores_the_oom_kills_the_runner_saw() {869+async fn finish_stores_oom_kills_and_old_bodies_leave_them_empty() {
895 let env = bare(|_| {}).await;870 let env = bare(|_| {}).await;
896 let (_runner, secret) = runner(&env, &[]).await;871 let (_runner, secret) = runner(&env, &[]).await;
872+
873+ // Scenario 1: cargo exited 101 because its rustc was killed: nothing in the exit code or
874+ // message. The OOM kill count the runner saw is stored.
897 make_run(&env, "push", None, COMMIT, &[job("build", spec(&[], &[]))]).await;875 make_run(&env, "push", None, COMMIT, &[job("build", spec(&[], &[]))]).await;
898- // cargo exited 101 because its rustc was killed: nothing in the exit code or message.
899 let (id, _) = finish_with(876 let (id, _) = finish_with(
900 &env,877 &env,
901 &secret,878 &secret,
@@ -906,12 +883,8 @@ async fn finish_stores_the_oom_kills_the_runner_saw() {
906 let stored = env.store.job_by_id(&id).await.unwrap().unwrap();883 let stored = env.store.job_by_id(&id).await.unwrap().unwrap();
907 assert_eq!(stored.oom_kills, Some(2));884 assert_eq!(stored.oom_kills, Some(2));
908 assert_eq!(stored.exit_code, Some(101));885 assert_eq!(stored.exit_code, Some(101));
909-}
910 886
911-#[tokio::test]887+ // Scenario 2: an old runner's finish body has no OOM fields, and the job still finishes.
912-async fn an_old_runner_finish_body_without_oom_fields_still_works() {
913- let env = bare(|_| {}).await;
914- let (_runner, secret) = runner(&env, &[]).await;
915 make_run(&env, "push", None, COMMIT, &[job("build", spec(&[], &[]))]).await;888 make_run(&env, "push", None, COMMIT, &[job("build", spec(&[], &[]))]).await;
916 let (id, _) = finish_with(889 let (id, _) = finish_with(
917 &env,890 &env,
@@ -921,7 +894,8 @@ async fn an_old_runner_finish_body_without_oom_fields_still_works() {
921 .await;894 .await;
922 assert_eq!(895 assert_eq!(
923 env.store.job_by_id(&id).await.unwrap().unwrap().oom_kills,896 env.store.job_by_id(&id).await.unwrap().unwrap().oom_kills,
924- None897+ None,
898+ "scenario old runner"
925 );899 );
926 }900 }
927 901
@@ -944,9 +918,11 @@ async fn two_step_run(env: &Bare) -> CiRun {
944 }918 }
945 919
946 #[tokio::test]920 #[tokio::test]
947-async fn finish_queues_dependants_on_success() {921+async fn finish_queues_dependants_on_success_and_skips_them_on_failure() {
948 let env = bare(|_| {}).await;922 let env = bare(|_| {}).await;
949 let (_, secret) = runner(&env, &[]).await;923 let (_, secret) = runner(&env, &[]).await;
924+
925+ // Scenario 1: a green `a` queues `b`, which is then claimed and finished.
950 let run = two_step_run(&env).await;926 let run = two_step_run(&env).await;
951 let assignment = claim_job(&env.router, &secret).await;927 let assignment = claim_job(&env.router, &secret).await;
952 assert_eq!(assignment["job_name"], "a");928 assert_eq!(assignment["job_name"], "a");
@@ -966,16 +942,23 @@ async fn finish_queues_dependants_on_success() {
966 let jobs = env.store.jobs_for_run(&run.id).await.unwrap();942 let jobs = env.store.jobs_for_run(&run.id).await.unwrap();
967 let state_of =943 let state_of =
968 |name: &str| -> JobState { jobs.iter().find(|j: &&CiJob| j.name == name).unwrap().state };944 |name: &str| -> JobState { jobs.iter().find(|j: &&CiJob| j.name == name).unwrap().state };
969- assert_eq!(state_of("a"), JobState::Success);945+ assert_eq!(state_of("a"), JobState::Success, "success scenario");
970- assert_eq!(state_of("b"), JobState::Queued);946+ assert_eq!(state_of("b"), JobState::Queued, "success scenario");
971 let next = claim_job(&env.router, &secret).await;947 let next = claim_job(&env.router, &secret).await;
972 assert_eq!(next["job_name"], "b");948 assert_eq!(next["job_name"], "b");
973-}949+ let b_id = next["job_id"].as_str().unwrap();
950+ let b_token = next["job_token"].as_str().unwrap();
951+ let sent = send(
952+ &env.router,
953+ "POST",
954+ &format!("/api/v1/ci/jobs/{b_id}/finish"),
955+ &[bearer(b_token)],
956+ finish_body("success"),
957+ )
958+ .await;
959+ assert_eq!(sent.status, 200, "{}", sent.json());
974 960
975-#[tokio::test]961+ // Scenario 2: a failed `a` skips `b`, and the run ends in failure.
976-async fn finish_skips_dependants_on_failure() {
977- let env = bare(|_| {}).await;
978- let (_, secret) = runner(&env, &[]).await;
979 let run = two_step_run(&env).await;962 let run = two_step_run(&env).await;
980 let assignment = claim_job(&env.router, &secret).await;963 let assignment = claim_job(&env.router, &secret).await;
981 let job_id = assignment["job_id"].as_str().unwrap();964 let job_id = assignment["job_id"].as_str().unwrap();
crates/forge-web/src/ci/release.rsmodified+21 -26
@@ -600,29 +600,29 @@ title = "forge {tag}"
600 assert_eq!(asset_names(&fx, &release).await.len(), 1);600 assert_eq!(asset_names(&fx, &release).await.len(), 1);
601 }601 }
602 602
603+ /// Failed runs never publish. Each scenario has its own tag, so its run is number 1, 2 and 3.
604+ /// Scenario 1: `docs` fails after the jobs run in order. Scenario 2: `build` finishes green
605+ /// while `docs` still runs, then `docs` fails. Scenario 3: `docs` fails fast while `build`
606+ /// has not finished, so `build` is cancelled at once (the running-job path is covered in the
607+ /// store tests). No release is created in any scenario.
603 #[tokio::test]608 #[tokio::test]
604- async fn failed_run_creates_no_release() {609+ async fn failed_runs_create_no_release() {
605 let fx = fixture().await;610 let fx = fixture().await;
611+
612+ // Scenario 1.
606 push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await;613 push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await;
607 let run = run_of(&fx, 1).await;614 let run = run_of(&fx, 1).await;
608 let build = job_of(&fx, &run, "build").await;615 let build = job_of(&fx, &run, "build").await;
609 add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"binary").await;616 add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"binary").await;
610 finish_all(&fx, &run.id, Some("docs")).await;617 finish_all(&fx, &run.id, Some("docs")).await;
611 let run = run_of(&fx, 1).await;618 let run = run_of(&fx, 1).await;
612- assert_eq!(run.state, RunState::Failure);619+ assert_eq!(run.state, RunState::Failure, "in order");
613-
614 on_run_finished(&fx.state, &run).await;620 on_run_finished(&fx.state, &run).await;
621+ assert!(release_named(&fx, "v1.0").await.is_none(), "in order");
615 622
616- assert!(release_named(&fx, "v1.0").await.is_none());623+ // Scenario 2.
617- }624+ push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v2.0").await;
618-625+ let run = run_of(&fx, 2).await;
619- /// Parallel jobs: `build` finishes green while `docs` still runs, then `docs` fails. The
620- /// release hook runs after each finish, as `finish_job` does, and never publishes.
621- #[tokio::test]
622- async fn build_green_first_then_other_job_fails_creates_no_release() {
623- let fx = fixture().await;
624- push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await;
625- let run = run_of(&fx, 1).await;
626 let build = job_of(&fx, &run, "build").await;626 let build = job_of(&fx, &run, "build").await;
627 let docs = job_of(&fx, &run, "docs").await;627 let docs = job_of(&fx, &run, "docs").await;
628 add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"binary").await;628 add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"binary").await;
@@ -635,7 +635,7 @@ title = "forge {tag}"
635 .expect("finish build");635 .expect("finish build");
636 assert_eq!(after_build.state, RunState::Running, "docs is still queued");636 assert_eq!(after_build.state, RunState::Running, "docs is still queued");
637 on_run_finished(&fx.state, &after_build).await;637 on_run_finished(&fx.state, &after_build).await;
638- assert!(release_named(&fx, "v1.0").await.is_none());638+ assert!(release_named(&fx, "v2.0").await.is_none(), "parallel");
639 639
640 let after_docs = fx640 let after_docs = fx
641 .state641 .state
@@ -643,18 +643,13 @@ title = "forge {tag}"
643 .finish_job(&docs.id, JobState::Failure, Some(1), None)643 .finish_job(&docs.id, JobState::Failure, Some(1), None)
644 .await644 .await
645 .expect("finish docs");645 .expect("finish docs");
646- assert_eq!(after_docs.state, RunState::Failure);646+ assert_eq!(after_docs.state, RunState::Failure, "parallel");
647 on_run_finished(&fx.state, &after_docs).await;647 on_run_finished(&fx.state, &after_docs).await;
648- assert!(release_named(&fx, "v1.0").await.is_none());648+ assert!(release_named(&fx, "v2.0").await.is_none(), "parallel");
649- }
650 649
651- /// Fail fast: `docs` fails while `build` has not finished. `build` is cancelled at once (the650+ // Scenario 3.
652- /// running-job path is covered in the store tests), the run fails, and no release is created.651+ push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v3.0").await;
653- #[tokio::test]652+ let run = run_of(&fx, 3).await;
654- async fn failing_job_cancels_build_and_creates_no_release() {
655- let fx = fixture().await;
656- push(&fx, &[(CONFIG_PATH, RELEASE)], "refs/tags/v1.0").await;
657- let run = run_of(&fx, 1).await;
658 let build = job_of(&fx, &run, "build").await;653 let build = job_of(&fx, &run, "build").await;
659 let docs = job_of(&fx, &run, "docs").await;654 let docs = job_of(&fx, &run, "docs").await;
660 add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"binary").await;655 add_artifact(&fx, &build, "dist_forge-linux-x86_64", b"binary").await;
@@ -665,10 +660,10 @@ title = "forge {tag}"
665 .finish_job(&docs.id, JobState::Failure, Some(1), None)660 .finish_job(&docs.id, JobState::Failure, Some(1), None)
666 .await661 .await
667 .expect("finish docs");662 .expect("finish docs");
668- assert_eq!(after_docs.state, RunState::Failure);663+ assert_eq!(after_docs.state, RunState::Failure, "fail fast");
669 assert_eq!(job_of(&fx, &run, "build").await.state, JobState::Cancelled);664 assert_eq!(job_of(&fx, &run, "build").await.state, JobState::Cancelled);
670 on_run_finished(&fx.state, &after_docs).await;665 on_run_finished(&fx.state, &after_docs).await;
671- assert!(release_named(&fx, "v1.0").await.is_none());666+ assert!(release_named(&fx, "v3.0").await.is_none(), "fail fast");
672 }667 }
673 668
674 #[tokio::test]669 #[tokio::test]
crates/forge-web/src/ci/schedule.rsmodified+84 -84
@@ -908,9 +908,11 @@ run = "make"
908 assert!(!config.description.as_deref().unwrap_or("").is_empty());908 assert!(!config.description.as_deref().unwrap_or("").is_empty());
909 }909 }
910 910
911+ /// Branch globs pick the branches that run (scenario 1), and tag globs pick the tags (scenario 2).
911 #[tokio::test]912 #[tokio::test]
912- async fn branch_globs_select_runs() {913+ async fn branch_and_tag_globs_select_runs() {
913 let fx = fixture().await;914 let fx = fixture().await;
915+ // Scenario 1: branches.
914 push(&fx, &[(CONFIG_PATH, CONFIG_GLOBS)], "refs/heads/main").await;916 push(&fx, &[(CONFIG_PATH, CONFIG_GLOBS)], "refs/heads/main").await;
915 assert_eq!(runs_total(&fx).await, 0, "main is not in the globs");917 assert_eq!(runs_total(&fx).await, 0, "main is not in the globs");
916 918
@@ -929,20 +931,17 @@ run = "make"
929 .expect("query")931 .expect("query")
930 .expect("run");932 .expect("run");
931 assert_eq!(run.git_ref, "refs/heads/release/1.0");933 assert_eq!(run.git_ref, "refs/heads/release/1.0");
932- }
933 934
934- #[tokio::test]935+ // Scenario 2: tags.
935- async fn tag_push_creates_tag_run() {
936- let fx = fixture().await;
937 push(&fx, &[(CONFIG_PATH, CONFIG_TAGS)], "refs/tags/nightly").await;936 push(&fx, &[(CONFIG_PATH, CONFIG_TAGS)], "refs/tags/nightly").await;
938- assert_eq!(runs_total(&fx).await, 0, "nightly does not match v*");937+ assert_eq!(runs_total(&fx).await, 1, "nightly does not match v*");
939 938
940 push(&fx, &[(CONFIG_PATH, CONFIG_TAGS)], "refs/tags/v1.0").await;939 push(&fx, &[(CONFIG_PATH, CONFIG_TAGS)], "refs/tags/v1.0").await;
941- assert_eq!(runs_total(&fx).await, 1);940+ assert_eq!(runs_total(&fx).await, 2);
942 let run = fx941 let run = fx
943 .state942 .state
944 .store943 .store
945- .run_by_number(&fx.repo.id, 1)944+ .run_by_number(&fx.repo.id, 2)
946 .await945 .await
947 .expect("query")946 .expect("query")
948 .expect("run");947 .expect("run");
@@ -979,10 +978,15 @@ run = "make"
979 pr978 pr
980 }979 }
981 980
981+ /// Pull requests wait for approval when the author is a fork (scenario 1) or cannot write to
982+ /// the repo (scenario 2). A writer's pull request runs at once (scenario 3). Each scenario has
983+ /// its own head branch, and its pull request run is number 1, 2 and 3.
982 #[tokio::test]984 #[tokio::test]
983- async fn fork_pull_request_waits_for_approval() {985+ async fn pull_requests_wait_for_approval_unless_the_author_writes_the_repo() {
984 let fx = fixture().await;986 let fx = fixture().await;
985- let head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/feature").await;987+
988+ // Scenario 1: a fork's pull request.
989+ let fork_head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/feature-fork").await;
986 let bob = fx990 let bob = fx
987 .state991 .state
988 .store992 .store
@@ -995,7 +999,7 @@ run = "make"
995 .create_repo(&bob.id, "proj", "", Visibility::Public, "main")999 .create_repo(&bob.id, "proj", "", Visibility::Public, "main")
996 .await1000 .await
997 .expect("fork");1001 .expect("fork");
998- let pr = open_pull(&fx, &fx.owner.id, &fork.id, "feature", &head).await;1002+ let pr = open_pull(&fx, &fx.owner.id, &fork.id, "feature-fork", &fork_head).await;
999 1003
1000 let run = fx1004 let run = fx
1001 .state1005 .state
@@ -1005,58 +1009,54 @@ run = "make"
1005 .expect("query")1009 .expect("query")
1006 .expect("run");1010 .expect("run");
1007 assert_eq!(run.trigger, "pull_request");1011 assert_eq!(run.trigger, "pull_request");
1008- assert_eq!(run.state, RunState::WaitingApproval);1012+ assert_eq!(run.state, RunState::WaitingApproval, "fork");
1009 assert_eq!(run.pull_number, Some(pr.issue.number));1013 assert_eq!(run.pull_number, Some(pr.issue.number));
1010 1014
1011 let statuses = fx1015 let statuses = fx
1012 .state1016 .state
1013 .store1017 .store
1014- .statuses_for(&fx.repo.id, &head)1018+ .statuses_for(&fx.repo.id, &fork_head)
1015 .await1019 .await
1016 .expect("statuses");1020 .expect("statuses");
1017 let build = status(&statuses, "ci/build").expect("ci/build");1021 let build = status(&statuses, "ci/build").expect("ci/build");
1018 assert_eq!(build.state, StatusState::Pending);1022 assert_eq!(build.state, StatusState::Pending);
1019 assert_eq!(build.description.as_deref(), Some("Waiting for approval"));1023 assert_eq!(build.description.as_deref(), Some("Waiting for approval"));
1020- }
1021 1024
1022- #[tokio::test]1025+ // Scenario 2: a same-repo pull request from a user who cannot write to it.
1023- async fn non_writer_pull_request_waits_for_approval() {1026+ let carol = fx
1024- let fx = fixture().await;
1025- let head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/feature").await;
1026- let bob = fx
1027 .state1027 .state
1028 .store1028 .store
1029- .create_user("bob", None, "unused", false)1029+ .create_user("carol", None, "unused", false)
1030 .await1030 .await
1031 .expect("user");1031 .expect("user");
1032- open_pull(&fx, &bob.id, &fx.repo.id, "feature", &head).await;1032+ let head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/feature-carol").await;
1033+ open_pull(&fx, &carol.id, &fx.repo.id, "feature-carol", &head).await;
1033 let run = fx1034 let run = fx
1034 .state1035 .state
1035 .store1036 .store
1036- .run_by_number(&fx.repo.id, 1)1037+ .run_by_number(&fx.repo.id, 2)
1037 .await1038 .await
1038 .expect("query")1039 .expect("query")
1039 .expect("run");1040 .expect("run");
1040- assert_eq!(run.state, RunState::WaitingApproval);1041+ assert_eq!(run.state, RunState::WaitingApproval, "non-writer");
1041- }
1042 1042
1043- #[tokio::test]1043+ // Scenario 3: the owner's pull request runs at once.
1044- async fn writer_pull_request_runs_at_once() {1044+ let head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/feature-writer").await;
1045- let fx = fixture().await;1045+ open_pull(&fx, &fx.owner.id, &fx.repo.id, "feature-writer", &head).await;
1046- let head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/feature").await;
1047- open_pull(&fx, &fx.owner.id, &fx.repo.id, "feature", &head).await;
1048 let run = fx1046 let run = fx
1049 .state1047 .state
1050 .store1048 .store
1051- .run_by_number(&fx.repo.id, 1)1049+ .run_by_number(&fx.repo.id, 3)
1052 .await1050 .await
1053 .expect("query")1051 .expect("query")
1054 .expect("run");1052 .expect("run");
1055- assert_ne!(run.state, RunState::WaitingApproval);1053+ assert_ne!(run.state, RunState::WaitingApproval, "writer");
1056 }1054 }
1057 1055
1056+ /// Manual runs use the branch head (scenario 1). A missing branch is an error, and a branch
1057+ /// whose config sets `manual = false` is refused (scenario 2).
1058 #[tokio::test]1058 #[tokio::test]
1059- async fn manual_run_uses_branch_head() {1059+ async fn manual_runs_use_the_branch_head_and_refuse_when_asked() {
1060 let fx = fixture().await;1060 let fx = fixture().await;
1061 let head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/main").await;1061 let head = push(&fx, &[(CONFIG_PATH, CONFIG)], "refs/heads/main").await;
1062 let before = runs_total(&fx).await;1062 let before = runs_total(&fx).await;
@@ -1067,19 +1067,15 @@ run = "make"
1067 assert_eq!(run.git_ref, "refs/heads/main");1067 assert_eq!(run.git_ref, "refs/heads/main");
1068 assert_eq!(run.commit_oid, head);1068 assert_eq!(run.commit_oid, head);
1069 assert_eq!(runs_total(&fx).await, before + 1);1069 assert_eq!(runs_total(&fx).await, before + 1);
1070- }
1071 1070
1072- #[tokio::test]1071+ push(&fx, &[(CONFIG_PATH, CONFIG_NO_MANUAL)], "refs/heads/quiet").await;
1073- async fn manual_run_refuses_missing_branch_and_disabled_manual() {
1074- let fx = fixture().await;
1075- push(&fx, &[(CONFIG_PATH, CONFIG_NO_MANUAL)], "refs/heads/main").await;
1076 let before = runs_total(&fx).await;1072 let before = runs_total(&fx).await;
1077 assert!(1073 assert!(
1078 manual_run(&fx.state, &fx.repo, "nope", &fx.owner.id)1074 manual_run(&fx.state, &fx.repo, "nope", &fx.owner.id)
1079 .await1075 .await
1080 .is_err()1076 .is_err()
1081 );1077 );
1082- let refused = manual_run(&fx.state, &fx.repo, "main", &fx.owner.id).await;1078+ let refused = manual_run(&fx.state, &fx.repo, "quiet", &fx.owner.id).await;
1083 assert!(matches!(refused, Err(forge_core::Error::Invalid(_))));1079 assert!(matches!(refused, Err(forge_core::Error::Invalid(_))));
1084 assert_eq!(runs_total(&fx).await, before);1080 assert_eq!(runs_total(&fx).await, before);
1085 }1081 }
@@ -1172,54 +1168,58 @@ image = "alpine:3"
1172 run = "make"1168 run = "make"
1173 "#;1169 "#;
1174 1170
1171+ /// A newer push cancels the older live run on the same ref, unless the config keeps it. Each
1172+ /// case has its own branch, since superseding only looks at one ref. The runs of a case are
1173+ /// numbered `first` and `first + 1`, because every push on these branches starts one run.
1175 #[tokio::test]1174 #[tokio::test]
1176- async fn a_newer_push_cancels_older_live_runs_when_asked() {1175+ async fn a_newer_push_cancels_older_live_runs_unless_the_config_keeps_them() {
1177 let fx = fixture().await;1176 let fx = fixture().await;
1178- push(&fx, &[(CONFIG_PATH, CONFIG_SUPERSEDE)], "refs/heads/main").await;1177+ // Each config triggers on its own branch, so its pushes make runs there.
1179- push(&fx, &[("a.txt", "a")], "refs/heads/main").await;1178+ let on = |config: &str, branch: &str| {
1180- let store = &fx.state.store;1179+ config.replace("push = [\"main\"]", &format!("push = [\"{branch}\"]"))
1181- let first = store.run_by_number(&fx.repo.id, 1).await.unwrap().unwrap();1180+ };
1182- let second = store.run_by_number(&fx.repo.id, 2).await.unwrap().unwrap();1181+ // (branch, config, the older run is cancelled, number of the older run)
1183- assert_eq!(1182+ let cases: [(&str, String, bool, i64); 3] = [
1184- first.state,1183+ ("default", on(CONFIG_ONE_JOB, "default"), true, 1),
1185- RunState::Cancelled,1184+ ("asked", on(CONFIG_SUPERSEDE, "asked"), true, 3),
1186- "the older run is cancelled"1185+ (
1187- );1186+ "kept",
1188- assert_eq!(second.state, RunState::Queued, "the newest run keeps going");1187+ on(
1189- let jobs = store.jobs_for_run(&first.id).await.unwrap();1188+ &CONFIG_SUPERSEDE.replace("cancel_superseded = true", "cancel_superseded = false"),
1190- assert_eq!(state_of(&jobs, "build"), Some(JobState::Cancelled));1189+ "kept",
1191- }1190+ ),
1192-1191+ false,
1193- #[tokio::test]1192+ 5,
1194- async fn a_newer_push_cancels_older_live_runs_by_default() {1193+ ),
1195- let fx = fixture().await;1194+ ];
1196- push(&fx, &[(CONFIG_PATH, CONFIG_ONE_JOB)], "refs/heads/main").await;1195+ for (branch, config, cancelled, first_number) in cases {
1197- push(&fx, &[("a.txt", "a")], "refs/heads/main").await;1196+ let git_ref = format!("refs/heads/{branch}");
1198- let first = fx1197+ let case = format!("case {branch}");
1199- .state1198+ push(&fx, &[(CONFIG_PATH, config.as_str())], &git_ref).await;
1200- .store1199+ push(&fx, &[("a.txt", "a")], &git_ref).await;
1201- .run_by_number(&fx.repo.id, 1)1200+ let store = &fx.state.store;
1202- .await1201+ let first = store
1203- .unwrap()1202+ .run_by_number(&fx.repo.id, first_number)
1204- .unwrap();1203+ .await
1205- assert_eq!(first.state, RunState::Cancelled);1204+ .unwrap()
1206- }1205+ .unwrap();
1207-1206+ let second = store
1208- #[tokio::test]1207+ .run_by_number(&fx.repo.id, first_number + 1)
1209- async fn older_runs_are_kept_when_cancel_superseded_is_false() {1208+ .await
1210- let fx = fixture().await;1209+ .unwrap()
1211- let config =1210+ .unwrap();
1212- CONFIG_SUPERSEDE.replace("cancel_superseded = true", "cancel_superseded = false");1211+ let want = if cancelled {
1213- push(&fx, &[(CONFIG_PATH, &config)], "refs/heads/main").await;1212+ RunState::Cancelled
1214- push(&fx, &[("a.txt", "a")], "refs/heads/main").await;1213+ } else {
1215- let first = fx1214+ RunState::Queued
1216- .state1215+ };
1217- .store1216+ assert_eq!(first.state, want, "{case}: the older run");
1218- .run_by_number(&fx.repo.id, 1)1217+ assert_eq!(second.state, RunState::Queued, "{case}: the newest run");
1219- .await1218+ if cancelled {
1220- .unwrap()1219+ let jobs = store.jobs_for_run(&first.id).await.unwrap();
1221- .unwrap();1220+ assert_eq!(state_of(&jobs, "build"), Some(JobState::Cancelled), "{case}");
1222- assert_eq!(first.state, RunState::Queued);1221+ }
1222+ }
1223 }1223 }
1224 1224
1225 #[tokio::test]1225 #[tokio::test]