diff --git a/api/src/urls.rs b/api/src/urls.rs index 265f7e17..df9b97de 100644 --- a/api/src/urls.rs +++ b/api/src/urls.rs @@ -236,6 +236,23 @@ mod tests { ); } + /// ADR-0019's `--no-repo` vector: the server names no repository with the nil UUID. + #[test] + fn test_collection_guid_url_generated_without_a_repo() { + let test_case = test_case_with_id("88e5353c-190c-5dce-9d06-0e66c3e062b1"); + let ids = TestCaseGuidScope { + test_collection_id: String::from(COLLECTION_ID), + repo_id: Uuid::nil().to_string(), + }; + + assert_eq!( + test_case_url(&test_case, "tc_123", Some(&ids)), + Ok(String::from( + "https://app.trunk-staging.io/bad-app-org/flaky-tests/collections/tc_123/tests/943a80af-66b0-84bb-ad01-56b3b72fe363" + )), + ); + } + #[test] fn test_collection_guid_url_falls_back_for_an_unparseable_server_id() { let ids = TestCaseGuidScope { diff --git a/cli/src/context.rs b/cli/src/context.rs index 4de65ed7..7256862b 100644 --- a/cli/src/context.rs +++ b/cli/src/context.rs @@ -859,6 +859,7 @@ pub async fn gather_exit_code_and_quarantined_tests_context( org_url_slug: String::default(), fetch_status: QuarantineFetchStatus::FetchSkipped, quarantine_resolution_mode: api::message::QuarantineResolutionMode::Unspecified, + guid_scope: None, } } else { // default to success if no test run result (i.e. `upload`) diff --git a/cli/src/context_quarantine.rs b/cli/src/context_quarantine.rs index 62673594..4112b651 100644 --- a/cli/src/context_quarantine.rs +++ b/cli/src/context_quarantine.rs @@ -45,6 +45,7 @@ pub struct QuarantineContext { pub org_url_slug: String, pub fetch_status: QuarantineFetchStatus, pub quarantine_resolution_mode: QuarantineResolutionMode, + pub guid_scope: Option, } impl QuarantineContext { pub fn quarantining(&self, disable_quarantining: bool) -> Quarantining { @@ -70,6 +71,7 @@ impl QuarantineContext { org_url_slug: String::default(), fetch_status: QuarantineFetchStatus::FetchSkipped, quarantine_resolution_mode: QuarantineResolutionMode::Unspecified, + guid_scope: None, } } @@ -82,6 +84,7 @@ impl QuarantineContext { org_url_slug: String::default(), fetch_status: QuarantineFetchStatus::FetchFailed(error), quarantine_resolution_mode: QuarantineResolutionMode::Unspecified, + guid_scope: None, } } } @@ -338,6 +341,7 @@ pub async fn gather_quarantine_context( failures: Vec::default(), fetch_status: QuarantineFetchStatus::FetchSkipped, quarantine_resolution_mode: QuarantineResolutionMode::Unspecified, + guid_scope: None, }); } @@ -395,6 +399,7 @@ pub async fn gather_quarantine_context( org_url_slug: request.org_url_slug.clone(), fetch_status: quarantine_fetch_status, quarantine_resolution_mode, + guid_scope, }); } else { // quarantining is enabled, continue with quarantine process and update exit code @@ -487,6 +492,7 @@ pub async fn gather_quarantine_context( org_url_slug: request.org_url_slug.clone(), fetch_status: quarantine_fetch_status, quarantine_resolution_mode, + guid_scope, }) } @@ -711,6 +717,7 @@ mod tests { org_url_slug: String::new(), fetch_status: QuarantineFetchStatus::FetchSucceeded, quarantine_resolution_mode: QuarantineResolutionMode::Unspecified, + guid_scope: None, }; assert_eq!( quarantine_query_result(false, &success_ctx), @@ -741,6 +748,7 @@ mod tests { repo: RepoUrlParts::default(), org_url_slug: String::new(), quarantine_resolution_mode: QuarantineResolutionMode::Unspecified, + guid_scope: None, }; assert_eq!( quarantine_query_result(false, &repo_disabled_ctx), diff --git a/cli/src/upload_command.rs b/cli/src/upload_command.rs index b4c64642..0d0543c4 100644 --- a/cli/src/upload_command.rs +++ b/cli/src/upload_command.rs @@ -764,6 +764,8 @@ pub async fn run_upload( )), ), }; + // A dry run or a failed upload has no upload ids, but a v2 quarantine lookup returns the same pair. + let guid_scope = guid_scope.or_else(|| quarantine_context.guid_scope.clone()); if guid_scope.is_none() && upload_args.test_collection_short_id.is_some() { tracing::debug!( "No test collection ids returned for this upload; test links will use the short-link form" diff --git a/cli/tests/upload.rs b/cli/tests/upload.rs index 4eb7dac7..09bb297d 100644 --- a/cli/tests/upload.rs +++ b/cli/tests/upload.rs @@ -384,6 +384,65 @@ async fn upload_bundle_falls_back_to_short_links_without_collection_ids() { .stdout(predicate::str::contains("/collections/tc_123/tests/").not()); } +// NOTE: must be multi threaded to start a mock server +#[tokio::test(flavor = "multi_thread")] +async fn failed_v2_upload_links_tests_by_the_quarantine_lookup_ids() { + use axum::routing::post; + use serde_json::{Value, json}; + + let temp_dir = tempdir().unwrap(); + generate_mock_git_repo(&temp_dir); + fs::copy( + get_test_file_path("test_fixtures/junit0_fail.xml"), + temp_dir.path().join("junit.xml"), + ) + .unwrap(); + + let mut builder = MockServerBuilder::new(); + builder.route( + "/v1/metrics/getUploadApiVersion", + post(|| async { Json(json!({"apiVersion": "v2"})) }), + ); + builder.route( + "/v2/test-collections/:id/quarantine-status", + post(|Json(body): Json| async move { + let tests: Vec = body["testIdentifiers"] + .as_array() + .unwrap() + .iter() + .map(|identifier| json!({"clientTestId": identifier["id"], "quarantined": false})) + .collect(); + Json(json!({ + "testCollectionUuid": MOCK_TEST_COLLECTION_ID, + "repositoryId": MOCK_REPO_ID, + "quarantineEnabled": true, + "tests": tests, + })) + }), + ); + builder.route( + "/v2/test-collections/:id/uploads", + post(|| async { (StatusCode::BAD_REQUEST, Json(json!({}))) }), + ); + let state = builder.spawn_mock_server().await; + + // The GUID hashes MOCK_TEST_COLLECTION_ID, MOCK_REPO_ID and the id of junit0_fail.xml's one test. + CommandBuilder::upload(temp_dir.path(), state.host.clone()) + .junit_paths("junit.xml") + .command() + .arg("--test-collection-id") + .arg("tc_123") + .assert() + .failure() + .stdout(predicate::str::contains(format!( + "{}/test-org/flaky-tests/collections/tc_123/tests/ae979ba6-6f93-8657-9f84-0265e5ac404c", + state.host + ))) + .stdout(predicate::str::contains("/tests/").count(1)) + .stdout(predicate::str::contains("/collections/tc_123/t/").not()) + .stdout(predicate::str::contains("?repo=trunk-io%2Fanalytics-cli").not()); +} + // NOTE: must be multi threaded to start a mock server #[tokio::test(flavor = "multi_thread")] async fn upload_bundle_prints_test_collection_upload_link() {