diff --git a/.github/actions/perform_smoke_test/action.yaml b/.github/actions/perform_smoke_test/action.yaml index 129a14e5..f9c12a73 100644 --- a/.github/actions/perform_smoke_test/action.yaml +++ b/.github/actions/perform_smoke_test/action.yaml @@ -123,24 +123,6 @@ runs: --token ${{ inputs.staging-api-token }} \ --test-collection-id Gtnr7BWl # analytics-cli-repo-wide: https://app.trunk-staging.io/trunk-staging-org/flaky-tests/collections/Gtnr7BWl/monitors - - name: Upload to staging from a forked pull request - id: staging-upload-forked-pr - if: always() && steps.staging-test-run.outcome == 'success' && (inputs.environment-type == 'staging' || inputs.environment-type == 'both') - env: - TRUNK_PUBLIC_API_ADDRESS: https://api.trunk-staging.io - shell: bash - run: | - ./${{ inputs.cli-binary-location }} upload \ - --org-url-slug trunk-staging-org \ - --junit-paths "${{ github.workspace }}/target/**/*junit.xml" \ - --allow-forked-pr-uploads \ - --use-uncloned-repo \ - --repo-url https://github.com/trunk-io/flake-farm-staging-fork-prs.git \ - --repo-head-sha ${{ github.sha }} \ - --repo-head-branch refs/heads/main \ - --repo-head-author-name "Trunk Smoke Test" \ - --test-collection-id z2dzjLHm - # upload with variant should fail because we treat variant as a separate test - name: Upload to staging with variant id: staging-upload-with-variant @@ -171,9 +153,9 @@ runs: shell: bash run: | if [ "${{ inputs.test-variants }}" == "true" ]; then - echo "staging-success=${{ (steps.staging-upload.outcome == 'success' && steps.staging-upload-repo-wide.outcome == 'success' && steps.staging-upload-forked-pr.outcome == 'success' && steps.staging-upload-with-variant.outcome == 'success' && (inputs.environment-type == 'staging' || inputs.environment-type == 'both')) || inputs.environment-type == 'production' }}" >> $GITHUB_OUTPUT + echo "staging-success=${{ (steps.staging-upload.outcome == 'success' && steps.staging-upload-repo-wide.outcome == 'success' && steps.staging-upload-with-variant.outcome == 'success' && (inputs.environment-type == 'staging' || inputs.environment-type == 'both')) || inputs.environment-type == 'production' }}" >> $GITHUB_OUTPUT else - echo "staging-success=${{ (steps.staging-upload.outcome == 'success' && steps.staging-upload-repo-wide.outcome == 'success' && steps.staging-upload-forked-pr.outcome == 'success' && (inputs.environment-type == 'staging' || inputs.environment-type == 'both')) || inputs.environment-type == 'production' }}" >> $GITHUB_OUTPUT + echo "staging-success=${{ (steps.staging-upload.outcome == 'success' && steps.staging-upload-repo-wide.outcome == 'success' && (inputs.environment-type == 'staging' || inputs.environment-type == 'both')) || inputs.environment-type == 'production' }}" >> $GITHUB_OUTPUT fi - name: Run tests on production @@ -230,24 +212,6 @@ runs: --token ${{ inputs.production-api-token }} \ --test-collection-id 4B2wfoEU - - name: Upload to production from a forked pull request - id: production-upload-forked-pr - if: always() && steps.production-test-run.outcome == 'success' && (inputs.environment-type == 'production' || inputs.environment-type == 'both') - env: - TRUNK_PUBLIC_API_ADDRESS: https://api.trunk.io - shell: bash - run: | - ./${{ inputs.cli-binary-location }} upload \ - --org-url-slug trunk \ - --junit-paths "${{ github.workspace }}/target/**/*junit.xml" \ - --allow-forked-pr-uploads \ - --use-uncloned-repo \ - --repo-url https://github.com/trunk-io/flake-farm-staging-fork-prs.git \ - --repo-head-sha ${{ github.sha }} \ - --repo-head-branch refs/heads/main \ - --repo-head-author-name "Trunk Smoke Test" \ - --test-collection-id 4B2wfoEU - # upload with variant should fail because we treat variant as a separate test - name: Upload to production with variant id: production-upload-with-variant @@ -278,7 +242,7 @@ runs: shell: bash run: | if [ "${{ inputs.test-variants }}" == "true" ]; then - echo "production-success=${{ (steps.production-upload.outcome == 'success' && steps.production-upload-forked-pr.outcome == 'success' && steps.production-upload-with-variant.outcome == 'success' && (inputs.environment-type == 'production' || inputs.environment-type == 'both')) || inputs.environment-type == 'staging' }}" >> $GITHUB_OUTPUT + echo "production-success=${{ (steps.production-upload.outcome == 'success' && steps.production-upload-with-variant.outcome == 'success' && (inputs.environment-type == 'production' || inputs.environment-type == 'both')) || inputs.environment-type == 'staging' }}" >> $GITHUB_OUTPUT else - echo "production-success=${{ (steps.production-upload.outcome == 'success' && steps.production-upload-forked-pr.outcome == 'success' && (inputs.environment-type == 'production' || inputs.environment-type == 'both')) || inputs.environment-type == 'staging' }}" >> $GITHUB_OUTPUT + echo "production-success=${{ (steps.production-upload.outcome == 'success' && (inputs.environment-type == 'production' || inputs.environment-type == 'both')) || inputs.environment-type == 'staging' }}" >> $GITHUB_OUTPUT fi diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ad980d96..0a682efe 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -61,7 +61,7 @@ You can generate sample junit files by running cargo run --bin junit-mock . ``` -You can change the API endpoint by setting `TRUNK_PUBLIC_API_ADDRESS=https://api.trunk.io`. To use localhost, you should use `TRUNK_PUBLIC_API_ADDRESS=http://localhost:9010 DEBUG_STRIP_VERSION_PREFIX=true` +You can change the API endpoint by setting `TRUNK_PUBLIC_API_ADDRESS=https://api.trunk.io`. The CLI calls only the Trunk API v2 routes under it (`/v2/test-collections/...`). To use localhost, use `TRUNK_PUBLIC_API_ADDRESS=http://localhost:9010`. ## Test diff --git a/Cargo.lock b/Cargo.lock index 0fc7f7af..68103c23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5455,11 +5455,13 @@ dependencies = [ "anyhow", "api", "axum 0.7.9", + "context", "flate2", "git2", "prost", "proto", "sentry", + "serde", "serde_json", "tar", "tempfile", diff --git a/api/src/call_api.rs b/api/src/call_api.rs index e66dde7d..e5295305 100644 --- a/api/src/call_api.rs +++ b/api/src/call_api.rs @@ -8,7 +8,7 @@ use http::StatusCode; use tokio::time::{self, Instant}; use tokio_retry::{Action, strategy::ExponentialBackoff}; -use crate::client::{NOT_FOUND_CONTEXT, UNAUTHORIZED_CONTEXT}; +use crate::client::UNAUTHORIZED_CONTEXT; // Tokio-retry uses base ^ retry * factor formula. // This will give us 2s, 4s, 8s, 16s, 32s @@ -104,7 +104,6 @@ impl AbortableRetry for anyhow::Error { fn should_retry(&self) -> bool { let self_text = format!("{self}"); !self_text.contains(UNAUTHORIZED_CONTEXT) - && !self_text.contains(NOT_FOUND_CONTEXT) && self.chain().fold(true, |acc: bool, cause| { let cause_should_retry = if let Some(reqwest_error) = cause.downcast_ref::() { @@ -280,7 +279,7 @@ mod tests { RETRY_COUNT_DEFAULT, retry_with_deadline, }; use crate::client::{ - CheckNotFound, CheckUnauthorized, ClientErrorExplanation, StatusCodeHelp, status_code_help, + CheckUnauthorized, ClientErrorExplanation, StatusCodeHelp, status_code_help, }; #[derive(Debug, Serialize, Clone, Deserialize, PartialEq, Eq)] struct EmptyResponse {} @@ -476,7 +475,6 @@ mod tests { response, StatusCodeHelp { check_unauthorized: CheckUnauthorized::DoNotCheck, - check_not_found: CheckNotFound::Check, create_error_message: |_e| String::from("Test message"), api_host: "mock_host", org_url_slug: "mock_url_slug", @@ -503,7 +501,6 @@ mod tests { Response::from(http_response), StatusCodeHelp { check_unauthorized: CheckUnauthorized::Check, - check_not_found: CheckNotFound::Check, create_error_message: |_e| String::from("Test message"), api_host: "mock_host", org_url_slug: "mock_url_slug", @@ -519,7 +516,7 @@ mod tests { #[tokio::test] async fn carries_the_api_client_error_message() { - let body = json!({"code": "SOME_CODE", "message": "Pass --test-collection-id."}); + let body = json!({"error": {"code": "SOME_CODE", "message": "Pass --test-collection-id."}}); let error = status_code_help_for(400, body.to_string()).await; assert_eq!(error.to_string(), "Pass --test-collection-id."); @@ -543,8 +540,8 @@ mod tests { #[tokio::test] async fn names_the_code_when_the_api_sends_no_message() { for body in [ - json!({"code": "SOME_CODE"}), - json!({"code": "SOME_CODE", "message": " "}), + json!({"error": {"code": "SOME_CODE"}}), + json!({"error": {"code": "SOME_CODE", "message": " "}}), ] { let error = status_code_help_for(400, body.to_string()).await; @@ -564,7 +561,7 @@ mod tests { #[tokio::test] async fn truncates_an_overlong_client_error_message() { - let body = json!({"code": "SOME_CODE", "message": "é".repeat(3000)}); + let body = json!({"error": {"code": "SOME_CODE", "message": "é".repeat(3000)}}); let error = status_code_help_for(400, body.to_string()).await; assert_eq!(error.to_string(), format!("{}…", "é".repeat(2048))); @@ -574,7 +571,8 @@ mod tests { async fn leaves_a_body_without_a_code_or_message_unexplained() { for body in [ json!({}).to_string(), - json!({"code": " ", "message": ""}).to_string(), + json!({"error": {"code": " ", "message": ""}}).to_string(), + json!({"code": "SOME_CODE", "message": "Not the envelope."}).to_string(), String::from("Bad Request"), String::new(), ] { diff --git a/api/src/client/mod.rs b/api/src/client/mod.rs index e87d7e9f..e5be9f96 100644 --- a/api/src/client/mod.rs +++ b/api/src/client/mod.rs @@ -10,10 +10,9 @@ use serde::de::DeserializeOwned; use tokio::fs; use crate::call_api::CallApi; -use crate::message::{self, UploadApiVersion}; +use crate::message; use crate::quarantine::{QuarantineLookup, QuarantinedTestSet}; -mod v1; mod v2; #[derive(Debug)] @@ -51,22 +50,20 @@ impl std::error::Error for ApiErrorEndpoint {} pub struct ApiClient { pub api_host: String, s3_client: Client, - trunk_api_client: Client, - /// Holds the run's v2 credential: an API token from the start, or a fork pull request's Trunk + /// Holds the run's credential: an API token from the start, or a fork pull request's Trunk /// login once it has one. trunk_api_v2_client: Option, /// `--allow-forked-pr-uploads` with no token. credential_free: bool, - /// A credential-free GitHub Actions fork PR run, which on v2 swaps its `GITHUB_TOKEN` for a login. + /// A credential-free GitHub Actions fork PR run, which swaps its `GITHUB_TOKEN` for a login. github_fork_pull_request: Option, + /// Why a fork pull request's login failed, for the error every later call returns. + github_fork_login_failure: Option, /// The run's API token, for the CLI telemetry route; `None` without one (a fork pull request /// has no token, and the gateway refuses its GitHub Actions login there). telemetry_client: Option, - version_path_prefix: String, org_url_slug: String, render_sender: Option>, - upload_api_version: UploadApiVersion, - missing_test_collection: bool, } /// A GitHub Actions run of a pull request from a fork, as its event payload names it. @@ -129,12 +126,8 @@ pub fn get_api_host() -> String { impl ApiClient { const TRUNK_API_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30); - // Two attempts at this, so a slow answer holds up a customer's CI run for at most 4s. - const UPLOAD_API_VERSION_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(2); - const UPLOAD_API_VERSION_ATTEMPTS: usize = 2; // This should always be fast const TRUNK_TELEMETRY_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(1); - const TRUNK_API_TOKEN_HEADER: &'static str = "x-api-token"; const TRUNK_ALLOW_FORKED_PR_UPLOADS_HEADER: &'static str = "x-trunk-allow-forked-pr-uploads"; pub fn new( @@ -146,46 +139,15 @@ impl ApiClient { let org_url_slug = String::from(org_url_slug.as_ref()); let api_token = api_token.filter(|token| !token.trim().is_empty()); - // The forked-PR lane presents no credential at all on v1: the collection id already in - // the request authorizes it, gated on that collection's opt-in server-side. if api_token.is_none() && !allow_forked_pr_uploads { return Err(anyhow::anyhow!( "Either a Trunk API token or --allow-forked-pr-uploads is required." )); } - let api_token_header_value = api_token - .as_deref() - .map(|token| { - HeaderValue::from_str(token) - .map_err(|_| anyhow::Error::msg("Trunk API token is not ASCII")) - }) - .transpose()?; - let api_host = get_api_host(); tracing::debug!("Using public api address {}", api_host); - let mut trunk_api_client_default_headers = HeaderMap::new(); - trunk_api_client_default_headers.append( - header::CONTENT_TYPE, - HeaderValue::from_static("application/json"), - ); - if let Some(api_token_header_value) = api_token_header_value.clone() { - trunk_api_client_default_headers - .append(Self::TRUNK_API_TOKEN_HEADER, api_token_header_value); - } - if allow_forked_pr_uploads { - trunk_api_client_default_headers.append( - Self::TRUNK_ALLOW_FORKED_PR_UPLOADS_HEADER, - HeaderValue::from_static("true"), - ); - } - - let trunk_api_client = Client::builder() - .timeout(Self::TRUNK_API_TIMEOUT) - .default_headers(trunk_api_client_default_headers) - .build()?; - let trunk_api_v2_client = api_token .clone() .map(|token| V2Credential::ApiKey(token).client()) @@ -208,138 +170,33 @@ impl ApiClient { .default_headers(s3_client_default_headers) .build()?; - let version_path_prefix = if std::env::var("DEBUG_STRIP_VERSION_PREFIX").is_ok() { - String::from("") - } else { - String::from("/v1") - }; - Ok(ApiClient { api_host, s3_client, - trunk_api_client, trunk_api_v2_client, credential_free, github_fork_pull_request: None, + github_fork_login_failure: None, telemetry_client, - version_path_prefix, org_url_slug, render_sender, - upload_api_version: UploadApiVersion::V1, - missing_test_collection: false, }) } - /// Trunk has no test collection for this run, so it makes no quarantine lookup and no upload: - /// v1 refuses both for a fully migrated org. - pub fn missing_test_collection(&self) -> bool { - self.missing_test_collection - } - - pub fn set_missing_test_collection(&mut self, missing_test_collection: bool) { - self.missing_test_collection = missing_test_collection; - } - - pub fn upload_api_version(&self) -> UploadApiVersion { - self.upload_api_version - } - - /// Lets a credential-free run of a GitHub Actions fork PR ask for v2. Ignored for any other run. + /// Lets a credential-free run of a GitHub Actions fork PR log in. Ignored for any other run. pub fn set_github_fork_pull_request(&mut self, pull_request: GithubForkPullRequest) { if self.credential_free { self.github_fork_pull_request = Some(pull_request); } } - /// Which API the run's quarantine lookup and upload use. Every later call follows it. - pub fn set_upload_api_version(&mut self, upload_api_version: UploadApiVersion) { - self.upload_api_version = upload_api_version; - } - - /// Asks Trunk whether this run uses v2. Anything but an explicit `v2`, a failed call - /// included, is v1, so this never fails a run. A timeout, a connection error or a 5xx is - /// retried once; any other answer is final. Only a run with an API token or a credential-free - /// GitHub Actions fork PR run asks. - /// - /// A run that names no collection asks too, by its repository: Trunk may answer `v2` with the - /// collection it inferred. A `v2` that names none is v1, since every v2 route needs one. A `v1` - /// with `missingTestCollection` means Trunk has no collection for the run at all. - pub async fn get_upload_api_version( - &self, - request: &message::GetUploadApiVersionRequest, - ) -> message::UploadApiDecision { - let requested = request - .test_collection_short_id - .as_deref() - .filter(|id| !id.trim().is_empty()); - let has_repo = request - .repo - .as_ref() - .and_then(message::named_repo) - .is_some(); - let can_use_v2 = - self.trunk_api_v2_client.is_some() || self.github_fork_pull_request.is_some(); - let decision = if !can_use_v2 || (requested.is_none() && !has_repo) { - message::UploadApiDecision::default() - } else { - match self.request_upload_api_version(request).await { - Ok(response) => message::UploadApiDecision::from_response(response, requested), - Err(error) => { - tracing::debug!("Could not choose the Trunk API version, using v1: {error:?}"); - message::UploadApiDecision::default() - } - } - }; - tracing::info!("Using Trunk API {}", decision.version.as_str()); - decision - } - - async fn request_upload_api_version( - &self, - request: &message::GetUploadApiVersionRequest, - ) -> anyhow::Result { - let mut attempt = 1; - loop { - match self.request_upload_api_version_once(request).await { - Err(error) - if attempt < Self::UPLOAD_API_VERSION_ATTEMPTS && is_retryable(&error) => - { - tracing::debug!("Retrying the Trunk API version check: {error:?}"); - attempt += 1; - } - result => return result, - } - } - } - - async fn request_upload_api_version_once( - &self, - request: &message::GetUploadApiVersionRequest, - ) -> anyhow::Result { - Ok(self - .trunk_api_client - .post(format!( - "{}{}/metrics/getUploadApiVersion", - self.api_host, self.version_path_prefix - )) - .timeout(Self::UPLOAD_API_VERSION_TIMEOUT) - .json(request) - .send() - .await? - .error_for_status()? - .json::() - .await?) - } - pub async fn create_bundle_upload( &self, request: &message::CreateBundleUploadRequest, ) -> anyhow::Result { - match self.upload_api_version { - UploadApiVersion::V2 => self.create_test_upload(request).await, - UploadApiVersion::V1 => self.create_bundle_upload_v1(request).await, - } - .map_err(|e| e.context(ApiErrorEndpoint::CreateBundleUpload)) + self.create_test_upload(request) + .await + .map_err(|e| e.context(ApiErrorEndpoint::CreateBundleUpload)) } /// Looks up the run's failing tests. A test runner checking a test at a time wants the whole @@ -348,35 +205,20 @@ impl ApiClient { &self, request: &message::GetQuarantineConfigRequest, ) -> anyhow::Result { - match self.upload_api_version { - UploadApiVersion::V2 => self.get_quarantine_status(request).await, - UploadApiVersion::V1 => self - .get_quarantine_config_v1(request) - .await - .map(QuarantineLookup::from), - } - .map_err(|e| e.context(ApiErrorEndpoint::GetQuarantiningConfig)) + self.get_quarantine_status(request) + .await + .map_err(|e| e.context(ApiErrorEndpoint::GetQuarantiningConfig)) } - /// Every quarantined test, for a test runner that checks each test as it runs. On v1 that is - /// the lookup with no identifiers; on v2 it is the paged `all-quarantined-tests`. + /// Every quarantined test, for a test runner that checks each test as it runs: the paged + /// `all-quarantined-tests`. pub async fn get_quarantined_test_set( &self, request: &message::GetQuarantineConfigRequest, ) -> anyhow::Result { - match self.upload_api_version { - UploadApiVersion::V2 => self.get_all_quarantined_tests(request).await, - UploadApiVersion::V1 => { - let request = message::GetQuarantineConfigRequest { - test_identifiers: Vec::new(), - ..request.clone() - }; - self.get_quarantine_config_v1(&request) - .await - .map(QuarantinedTestSet::from) - } - } - .map_err(|e| e.context(ApiErrorEndpoint::GetQuarantiningConfig)) + self.get_all_quarantined_tests(request) + .await + .map_err(|e| e.context(ApiErrorEndpoint::GetQuarantiningConfig)) } pub async fn put_bundle_to_s3, B: AsRef>( @@ -401,7 +243,6 @@ impl ApiClient { response, StatusCodeHelp { check_unauthorized: CheckUnauthorized::DoNotCheck, - check_not_found: CheckNotFound::DoNotCheck, create_error_message: |_| String::from("Failed to upload bundle to S3."), api_host: &self.api_host, org_url_slug: &self.org_url_slug, @@ -504,22 +345,11 @@ pub(crate) enum CheckUnauthorized { DoNotCheck, } -#[derive(Debug, Clone, Copy)] -pub(crate) enum CheckNotFound { - Check, - DoNotCheck, -} - pub(crate) const UNAUTHORIZED_CONTEXT: &str = concat!( "Your Trunk token may be incorrect - find it on the Trunk app ", "(Settings -> Manage Organization -> Organization API Token -> View).", ); -pub(crate) const NOT_FOUND_CONTEXT: &str = concat!( - "Your Trunk organization URL slug may be incorrect - find it on the Trunk app ", - "(Settings -> Manage Organization -> Organization Slug).", -); - /// The API's explanation of a client error, kept in the error chain so the error report can /// print it beneath the endpoint context the callers add. #[derive(Debug, Clone, PartialEq, Eq)] @@ -545,16 +375,6 @@ impl std::error::Error for ClientErrorExplanation {} const MAX_CLIENT_ERROR_MESSAGE_LEN: usize = 2048; -fn is_retryable(error: &anyhow::Error) -> bool { - error.downcast_ref::().is_some_and(|error| { - error.is_timeout() - || error.is_connect() - || error - .status() - .is_some_and(|status| status.is_server_error()) - }) -} - fn non_empty(value: Option) -> Option { value .map(|value| value.trim().to_owned()) @@ -570,7 +390,9 @@ fn truncate_message(message: String) -> String { async fn client_error_explanation(response: Response) -> Option { let body = response.text().await.ok()?; - let message::ClientErrorBody { code, message } = serde_json::from_str(&body).ok()?; + let message::ClientErrorEnvelope { + error: message::ClientErrorBody { code, message }, + } = serde_json::from_str(&body).ok()?; let explanation = ClientErrorExplanation { code: non_empty(code), message: non_empty(message).map(truncate_message), @@ -580,7 +402,6 @@ async fn client_error_explanation(response: Response) -> Option String> { pub check_unauthorized: CheckUnauthorized, - pub check_not_found: CheckNotFound, pub create_error_message: T, pub api_host: &'a str, pub org_url_slug: &'a str, @@ -590,7 +411,6 @@ pub(crate) async fn status_code_help String>( response: Response, StatusCodeHelp { check_unauthorized, - check_not_found, mut create_error_message, api_host, org_url_slug, @@ -611,15 +431,12 @@ pub(crate) async fn status_code_help String>( .collect::>() }) .next(); - let error_message = match (response.status(), check_unauthorized, check_not_found) { - (StatusCode::UNAUTHORIZED, CheckUnauthorized::Check, _) => add_settings_url_to_context( + let error_message = match (response.status(), check_unauthorized) { + (StatusCode::UNAUTHORIZED, CheckUnauthorized::Check) => add_settings_url_to_context( UNAUTHORIZED_CONTEXT, domain, &String::from(org_url_slug), ), - (StatusCode::NOT_FOUND, _, CheckNotFound::Check) => { - add_settings_url_to_context(NOT_FOUND_CONTEXT, domain, &String::from(org_url_slug)) - } _ => base_error_message, }; @@ -687,21 +504,43 @@ fn does_not_add_settings_if_domain_absent() { #[cfg(test)] mod tests { use std::sync::{ - Arc, Mutex, + Arc, atomic::{AtomicUsize, Ordering}, }; - use axum::{ - http::{HeaderMap, StatusCode}, - response::Response, - }; - use context; + use axum::{http::StatusCode, response::Response}; use lazy_static::lazy_static; use test_utils::mock_server::MockServerBuilder; use super::ApiClient; use crate::message; + fn lookup_one_failure() -> message::GetQuarantineConfigRequest { + message::GetQuarantineConfigRequest { + repo: context::repo::RepoUrlParts { + host: String::from("host"), + owner: String::from("owner"), + name: String::from("name"), + }, + org_url_slug: String::from("org_url_slug"), + test_identifiers: vec![bundle::Test { + id: String::from("00000000-0000-4000-8000-000000000000"), + name: String::from("test"), + parent_name: String::from("Suite"), + class_name: None, + file: None, + timestamp_millis: None, + is_quarantined: false, + failure_message: None, + failure_message_attribute: None, + failure_text: None, + variant: None, + }], + remote_urls: vec![], + test_collection_short_id: Some(String::from("abc12345")), + } + } + #[tokio::test(start_paused = true)] async fn does_not_retry_on_ok_501() { let mut mock_server_builder = MockServerBuilder::new(); @@ -710,17 +549,15 @@ mod tests { static ref CALL_COUNT: Arc = Arc::new(AtomicUsize::new(0)); } - let quarantining_config_handler = move || async { + mock_server_builder.set_quarantine_status_handler(move || async { CALL_COUNT.fetch_add(1, Ordering::Relaxed); Response::builder() .status(StatusCode::NOT_IMPLEMENTED) .body(String::from( - r#"{ "status_code": 501, "error": "we broke" }"#, + r#"{"error":{"code":"INTERNAL_ERROR","message":"we broke"}}"#, )) .unwrap() - }; - - mock_server_builder.set_get_quarantining_config_handler(quarantining_config_handler); + }); let state = mock_server_builder.spawn_mock_server().await; @@ -728,26 +565,11 @@ mod tests { ApiClient::new(Some(String::from("mock-token")), false, "mock-org", None).unwrap(); api_client.api_host.clone_from(&state.host); - assert!( - format!( - "{:?}", - api_client - .get_quarantining_config(&message::GetQuarantineConfigRequest { - repo: context::repo::RepoUrlParts { - host: String::from("host"), - owner: String::from("owner"), - name: String::from("name"), - }, - org_url_slug: String::from("org_url_slug"), - test_identifiers: vec![], - remote_urls: vec![], - test_collection_short_id: None, - }) - .await - .unwrap_err() - ) - .contains("501 Not Implemented") - ); + let error = api_client + .get_quarantining_config(&lookup_one_failure()) + .await + .unwrap_err(); + assert!(format!("{error:?}").contains("501 Not Implemented")); assert_eq!(CALL_COUNT.load(Ordering::Relaxed), 1); } @@ -759,17 +581,15 @@ mod tests { static ref CALL_COUNT: Arc = Arc::new(AtomicUsize::new(0)); } - let quarantining_config_handler = move || async { + mock_server_builder.set_quarantine_status_handler(move || async { CALL_COUNT.fetch_add(1, Ordering::Relaxed); Response::builder() .status(StatusCode::INTERNAL_SERVER_ERROR) .body(String::from( - r#"{ "status_code": 500, "error": "we broke" }"#, + r#"{"error":{"code":"INTERNAL_ERROR","message":"we broke"}}"#, )) .unwrap() - }; - - mock_server_builder.set_get_quarantining_config_handler(quarantining_config_handler); + }); let state = mock_server_builder.spawn_mock_server().await; @@ -777,26 +597,11 @@ mod tests { ApiClient::new(Some(String::from("mock-token")), false, "mock-org", None).unwrap(); api_client.api_host.clone_from(&state.host); - assert!( - format!( - "{:?}", - api_client - .get_quarantining_config(&message::GetQuarantineConfigRequest { - repo: context::repo::RepoUrlParts { - host: String::from("api_host"), - owner: String::from("owner"), - name: String::from("name"), - }, - remote_urls: vec![], - org_url_slug: String::from("org_url_slug"), - test_identifiers: vec![], - test_collection_short_id: None, - }) - .await - .unwrap_err() - ) - .contains("500 Internal Server Error") - ); + let error = api_client + .get_quarantining_config(&lookup_one_failure()) + .await + .unwrap_err(); + assert!(format!("{error:?}").contains("500 Internal Server Error")); assert_eq!(CALL_COUNT.load(Ordering::Relaxed), 6); } @@ -804,16 +609,14 @@ mod tests { async fn get_quarantining_config_not_found() { let mut mock_server_builder = MockServerBuilder::new(); - async fn quarantining_config_not_found_handler() -> Response { + mock_server_builder.set_quarantine_status_handler(|| async { Response::builder() .status(StatusCode::NOT_FOUND) .body(String::from( - r#"{ "status_code": 404, "error": "not found" }"#, + r#"{"error":{"code":"TEST_COLLECTION_NOT_FOUND","message":"Test collection not found"}}"#, )) .unwrap() - } - mock_server_builder - .set_get_quarantining_config_handler(quarantining_config_not_found_handler); + }); let state = mock_server_builder.spawn_mock_server().await; @@ -821,26 +624,11 @@ mod tests { ApiClient::new(Some(String::from("mock-token")), false, "mock-org", None).unwrap(); api_client.api_host.clone_from(&state.host); - assert!( - format!( - "{:?}", - api_client - .get_quarantining_config(&message::GetQuarantineConfigRequest { - repo: context::repo::RepoUrlParts { - host: String::from("api_host"), - owner: String::from("owner"), - name: String::from("name"), - }, - remote_urls: vec![], - org_url_slug: String::from("org_url_slug"), - test_identifiers: vec![], - test_collection_short_id: None, - }) - .await - .unwrap_err() - ) - .contains("404 Not Found") - ); + let error = api_client + .get_quarantining_config(&lookup_one_failure()) + .await + .unwrap_err(); + assert!(format!("{error:?}").contains("404 Not Found")); } #[test] @@ -859,64 +647,28 @@ mod tests { assert!(ApiClient::new(Some(String::from("token")), false, "mock-org", None).is_ok()); } - #[tokio::test] - async fn sends_allow_forked_pr_uploads_header_with_no_credential() { - let mut mock_server_builder = MockServerBuilder::new(); - - lazy_static! { - static ref CAPTURED_HEADERS: Arc> = - Arc::new(Mutex::new(HeaderMap::new())); - } - - let quarantining_config_handler = move |headers: HeaderMap| async move { - *CAPTURED_HEADERS.lock().unwrap() = headers; - Response::builder() - .status(StatusCode::NOT_FOUND) - .body(String::from( - r#"{ "status_code": 404, "error": "not found" }"#, - )) - .unwrap() - }; - mock_server_builder.set_get_quarantining_config_handler(quarantining_config_handler); - - let state = mock_server_builder.spawn_mock_server().await; + #[test] + fn allow_forked_pr_uploads_satisfies_the_credential_requirement() { + assert!(ApiClient::new(None, true, "mock-org", None).is_ok()); + } + #[tokio::test] + async fn a_credential_free_run_that_is_not_a_fork_pr_makes_no_request() { + let state = MockServerBuilder::new().spawn_mock_server().await; let mut api_client = ApiClient::new(None, true, "mock-org", None).unwrap(); api_client.api_host.clone_from(&state.host); - let _ = api_client - .get_quarantining_config(&message::GetQuarantineConfigRequest { - repo: context::repo::RepoUrlParts { - host: String::from("host"), - owner: String::from("owner"), - name: String::from("name"), - }, - org_url_slug: String::from("org_url_slug"), - test_identifiers: vec![], - remote_urls: vec![], - test_collection_short_id: Some(String::from("abc12345")), - }) - .await; - - let headers = CAPTURED_HEADERS.lock().unwrap(); - assert_eq!( - headers - .get(super::ApiClient::TRUNK_ALLOW_FORKED_PR_UPLOADS_HEADER) - .and_then(|value| value.to_str().ok()), - Some("true") - ); - // The lane presents no credential at all — the collection id in the body is what the - // server authorizes, against that collection's own opt-in. + api_client.log_in_github_fork_pull_request().await; + let error = api_client + .get_quarantining_config(&lookup_one_failure()) + .await + .unwrap_err(); + assert!( - headers - .get(super::ApiClient::TRUNK_API_TOKEN_HEADER) - .is_none(), - "no credential header should be sent on the forked-PR lane" + format!("{error:?}") + .contains("from a GitHub Actions run of a pull request from a fork"), + "{error:?}" ); - } - - #[test] - fn allow_forked_pr_uploads_satisfies_the_credential_requirement() { - assert!(ApiClient::new(None, true, "mock-org", None).is_ok()); + assert!(state.requests.lock().unwrap().is_empty()); } } diff --git a/api/src/client/v1.rs b/api/src/client/v1.rs deleted file mode 100644 index 67512776..00000000 --- a/api/src/client/v1.rs +++ /dev/null @@ -1,91 +0,0 @@ -use reqwest::StatusCode; - -use super::{ApiClient, CheckNotFound, CheckUnauthorized, StatusCodeHelp, status_code_help}; -use crate::call_api::CallApi; -use crate::message; - -impl ApiClient { - pub(super) async fn create_bundle_upload_v1( - &self, - request: &message::CreateBundleUploadRequest, - ) -> anyhow::Result { - CallApi { - action: || async { - let response = self - .trunk_api_client - .post(format!("{}{}/metrics/createBundleUpload", self.api_host, self.version_path_prefix)) - .json(&request) - .send() - .await?; - - let response = status_code_help( - response, - StatusCodeHelp { - check_unauthorized: CheckUnauthorized::Check, - check_not_found: CheckNotFound::Check, - create_error_message: |_| String::from("Failed to create bundle upload."), - api_host: &self.api_host, - org_url_slug: &self.org_url_slug, - }, - ) - .await?; - - self.deserialize_response::(response).await - }, - log_progress_message: |time_elapsed, _| { - format!("Reporting bundle upload initiation to Trunk services is taking longer than expected. It has taken {} seconds so far.", time_elapsed.as_secs()) - }, - report_slow_progress_message: |time_elapsed| { - format!("Creating a Trunk upload intent is taking longer than {} seconds", time_elapsed.as_secs()) - }, - render_sender: self.render_sender.clone(), - } - .call_api() - .await - } - - pub(super) async fn get_quarantine_config_v1( - &self, - request: &message::GetQuarantineConfigRequest, - ) -> anyhow::Result { - CallApi { - action: || async { - let response = self - .trunk_api_client - .post(format!("{}{}/metrics/getQuarantineConfig", self.api_host, self.version_path_prefix)) - .json(&request) - .send() - .await?; - - let response = status_code_help( - response, - StatusCodeHelp { - check_unauthorized: CheckUnauthorized::Check, - check_not_found: CheckNotFound::DoNotCheck, - create_error_message: |response| -> String { - if response.status() == StatusCode::NOT_FOUND { - String::from("Quarantining config not found.") - } else { - String::from("Failed to get quarantine bulk test.") - } - }, - api_host: &self.api_host, - org_url_slug: &self.org_url_slug, - }, - ) - .await?; - - self.deserialize_response::(response).await - }, - log_progress_message: |time_elapsed, _| { - format!("Getting quarantine configuration from Trunk services is taking longer than expected. It has taken {} seconds so far.", time_elapsed.as_secs()) - }, - report_slow_progress_message: |time_elapsed| { - format!("Getting a Trunk quarantine configuration is taking longer than {} seconds", time_elapsed.as_secs()) - }, - render_sender: self.render_sender.clone(), - } - .call_api() - .await - } -} diff --git a/api/src/client/v2.rs b/api/src/client/v2.rs index 8608fb46..fd093f46 100644 --- a/api/src/client/v2.rs +++ b/api/src/client/v2.rs @@ -4,46 +4,44 @@ use base64::{Engine as _, engine::general_purpose::STANDARD}; use reqwest::{Client, StatusCode}; use super::{ - ApiClient, CheckNotFound, CheckUnauthorized, GithubForkPullRequest, StatusCodeHelp, - V2Credential, status_code_help, + ApiClient, CheckUnauthorized, GithubForkPullRequest, StatusCodeHelp, V2Credential, + status_code_help, }; use crate::call_api::CallApi; -use crate::message::{self, UploadApiVersion}; -use crate::quarantine::{QuarantineLookup, QuarantinedTestSet, QuarantinedTests}; +use crate::message; +use crate::quarantine::{QuarantineLookup, QuarantinedTestSet}; use crate::urls::TestCaseGuidScope; impl ApiClient { const QUARANTINE_STATUS_MAX_IDENTIFIERS: usize = 2_000; + const CREDENTIAL_FREE_NOT_A_FORK_PR: &'static str = "--allow-forked-pr-uploads without a Trunk API token uploads only from a GitHub Actions run of a pull request from a fork; test results will not be uploaded."; const ALL_QUARANTINED_TESTS_PAGE_SIZE: usize = 25_000; - /// Sets the API the run uses, taking the decision route's answer as given. The one exception is a - /// GitHub Actions fork PR run, which holds no v2 credential: on v2 it swaps the job's - /// `GITHUB_TOKEN` for a Trunk login, and stays on v1's collection lane if that fails. - pub async fn use_upload_api_version(&mut self, version: UploadApiVersion) { - self.upload_api_version = match &self.github_fork_pull_request { - Some(pull_request) - if version == UploadApiVersion::V2 && self.trunk_api_v2_client.is_none() => - { - match self.log_in_github_fork_pull_request(pull_request).await { - Ok(client) => { - self.trunk_api_v2_client = Some(client); - UploadApiVersion::V2 - } - Err(reason) => { - tracing::warn!( - "Could not log in to the Trunk API v2 for this fork pull request ({reason}); uploading with Trunk API v1" - ); - UploadApiVersion::V1 - } - } - } - _ => version, + /// Swaps a credential-free GitHub Actions fork PR run's `GITHUB_TOKEN` for a Trunk login. Any + /// other run already holds its credential, or has none to swap. A failed swap is kept, and + /// every later call fails with its reason. + pub async fn log_in_github_fork_pull_request(&mut self) { + if self.trunk_api_v2_client.is_some() { + return; + } + let Some(pull_request) = &self.github_fork_pull_request else { + tracing::warn!("{}", Self::CREDENTIAL_FREE_NOT_A_FORK_PR); + return; }; + match self.github_actions_login(pull_request).await { + Ok(client) => self.trunk_api_v2_client = Some(client), + Err(reason) => { + tracing::warn!( + "Could not log in to Trunk for this fork pull request ({reason}); test results will not be uploaded" + ); + self.github_fork_login_failure = Some(reason); + } + } } /// `POST /v2/auth/github-actions/login` with the job's `GITHUB_TOKEN`. The error is a reason /// fit to log: it never holds the token, the login, or a parse error that could quote them. - async fn log_in_github_fork_pull_request( + async fn github_actions_login( &self, pull_request: &GithubForkPullRequest, ) -> Result { @@ -74,7 +72,7 @@ impl ApiClient { let status = response.status(); if status != StatusCode::OK { let code = response - .json::() + .json::() .await .ok() .and_then(|refusal| refusal.error.code); @@ -96,9 +94,16 @@ impl ApiClient { } fn v2_client(&self) -> anyhow::Result<&Client> { - self.trunk_api_v2_client - .as_ref() - .ok_or_else(|| anyhow::anyhow!("The Trunk API v2 needs a Trunk API token.")) + if let Some(client) = &self.trunk_api_v2_client { + return Ok(client); + } + Err(match &self.github_fork_login_failure { + Some(reason) => { + anyhow::anyhow!("Could not log in to Trunk for this fork pull request ({reason}).") + } + None if self.credential_free => anyhow::anyhow!(Self::CREDENTIAL_FREE_NOT_A_FORK_PR), + None => anyhow::anyhow!("The Trunk API needs a Trunk API token."), + }) } fn v2_test_collection_url( @@ -132,7 +137,6 @@ impl ApiClient { response, StatusCodeHelp { check_unauthorized: CheckUnauthorized::Check, - check_not_found: CheckNotFound::DoNotCheck, create_error_message: |response| { if response.status() == StatusCode::NOT_FOUND { String::from("Test collection not found.") @@ -167,7 +171,6 @@ impl ApiClient { id: upload.id.clone(), id_v2, url: upload.upload_url, - key: String::new(), test_collection_bundle_meta_id: Some(upload.id), test_collection_bundle_meta_created_at: Some(upload.created_at), repo_id: Some(upload.repository_id), @@ -227,7 +230,6 @@ impl ApiClient { response, StatusCodeHelp { check_unauthorized: CheckUnauthorized::Check, - check_not_found: CheckNotFound::DoNotCheck, create_error_message: |response| { if response.status() == StatusCode::NOT_FOUND { String::from("Test collection not found.") @@ -285,7 +287,6 @@ impl ApiClient { response, StatusCodeHelp { check_unauthorized: CheckUnauthorized::Check, - check_not_found: CheckNotFound::DoNotCheck, create_error_message: |response| { if response.status() == StatusCode::NOT_FOUND { String::from("Test collection not found.") @@ -332,10 +333,8 @@ impl ApiClient { is_disabled: !*quarantine_enabled, quarantine_resolution_mode: message::QuarantineResolutionMode::TestCollection, - tests: QuarantinedTests::ByTestCaseGuid { - scope: scope.clone(), - guids, - }, + scope: scope.clone(), + guids, }); } } diff --git a/api/src/message.rs b/api/src/message.rs index d456b0b6..71912baa 100644 --- a/api/src/message.rs +++ b/api/src/message.rs @@ -20,7 +20,6 @@ pub struct CreateBundleUploadResponse { pub id: String, pub id_v2: String, pub url: String, - pub key: String, pub test_collection_bundle_meta_id: Option, pub test_collection_bundle_meta_created_at: Option, #[serde(default)] @@ -29,16 +28,6 @@ pub struct CreateBundleUploadResponse { pub test_collection_id: Option, } -#[derive(Debug, Serialize, Clone, Deserialize, Default)] -#[serde(rename_all = "camelCase")] -pub struct GetQuarantineConfigResponse { - pub is_disabled: bool, - #[serde(rename = "testIds")] - pub quarantined_tests: Vec, - #[serde(default)] - pub quarantine_resolution_mode: QuarantineResolutionMode, -} - #[derive(Debug, Serialize, Clone, Deserialize, PartialEq, Eq)] #[serde(rename_all = "camelCase")] pub struct GetQuarantineConfigRequest { @@ -50,83 +39,24 @@ pub struct GetQuarantineConfigRequest { pub test_collection_short_id: Option, } -#[derive(Debug, Serialize, Clone, Deserialize, PartialEq, Eq)] -#[serde(rename_all = "camelCase")] -pub struct CreateBundleUploadIntentRequest { - pub repo: RepoUrlParts, - pub org_url_slug: String, - pub client_version: String, -} - -#[derive(Debug, Serialize, Clone, Deserialize, PartialEq, Eq)] -#[serde(rename_all = "camelCase")] -pub struct CreateBundleUploadIntentResponse { - pub repo: RepoUrlParts, - pub org_url_slug: String, - pub client_version: String, -} - #[derive(Debug, Serialize, Clone, Deserialize, PartialEq)] pub struct TelemetryUploadMetricsRequest { pub upload_metrics: proto::upload_metrics::trunk::UploadMetrics, } -/// A 4xx body the API sends for an error it explains itself; the CLI prints `message` as given. +/// The API's 4xx envelope, `{"error": {"code", "message"}}`. #[derive(Debug, Deserialize)] -pub struct ClientErrorBody { - #[serde(default)] - pub code: Option, - #[serde(default)] - pub message: Option, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub enum UploadApiVersion { - #[default] - V1, - V2, -} - -impl UploadApiVersion { - pub fn as_str(&self) -> &'static str { - match self { - Self::V1 => "v1", - Self::V2 => "v2", - } - } -} - -#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)] -#[serde(rename_all = "kebab-case")] -pub enum UploadApiCaller { - Cli, - TestRunnerPlugin, -} - -#[derive(Debug, Serialize, Clone, PartialEq, Eq)] -#[serde(rename_all = "camelCase")] -pub struct GetUploadApiVersionRequest { - pub cli_version: String, - pub org_slug: String, - pub test_collection_short_id: Option, - pub repo: Option, - pub ci_provider: Option, - pub is_fork_pr: bool, - pub caller: UploadApiCaller, +pub struct ClientErrorEnvelope { + pub error: ClientErrorBody, } +/// An error the API explains itself; the CLI prints `message` as given. #[derive(Debug, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct GetUploadApiVersionResponse { - pub api_version: String, - /// Sent only with `v2`, for a run that named no collection, when Trunk inferred one from the - /// repository. Older servers never send it. +pub struct ClientErrorBody { #[serde(default)] - pub test_collection_short_id: Option, - /// Sent only with `v1`, for a fully migrated org's run that named no collection when Trunk - /// could not infer one. Trunk would drop a v1 upload of that run. Older servers never send it. + pub code: Option, #[serde(default)] - pub missing_test_collection: bool, + pub message: Option, } /// The fork lane's body for `POST /v2/auth/github-actions/login`. It holds the job's @@ -146,89 +76,6 @@ pub struct GithubActionsLogin { pub expires_at: String, } -#[derive(Debug, Deserialize)] -pub struct GithubActionsLoginRefusal { - pub error: ClientErrorBody, -} - -/// Which API a run uses and, on v2 for a run that named no test collection, the collection Trunk -/// inferred from its repository. -#[derive(Debug, Clone, PartialEq, Eq, Default)] -pub struct UploadApiDecision { - pub version: UploadApiVersion, - pub inferred_test_collection_short_id: Option, - /// The run named no collection and Trunk has none for it, so the upload is skipped. - pub missing_test_collection: bool, -} - -impl UploadApiDecision { - /// A run that named its collection takes the answer's version as it is. Only a run that named - /// none reads the other fields: the inferred collection with `v2`, `missingTestCollection` with - /// `v1`. - pub fn from_response(response: GetUploadApiVersionResponse, requested: Option<&str>) -> Self { - let v2 = response.api_version == "v2"; - if requested.is_some_and(|id| !id.trim().is_empty()) { - return Self::from(if v2 { - UploadApiVersion::V2 - } else { - UploadApiVersion::V1 - }); - } - if !v2 { - return Self { - missing_test_collection: response.missing_test_collection, - ..Self::default() - }; - } - match response - .test_collection_short_id - .filter(|id| !id.trim().is_empty()) - { - Some(inferred) => Self { - version: UploadApiVersion::V2, - inferred_test_collection_short_id: Some(inferred), - ..Self::default() - }, - None => { - tracing::debug!( - "Trunk chose v2 but named no test collection for this run, using v1" - ); - Self::default() - } - } - } - - /// The inferred collection, when the run named none of its own and so uses it. - pub fn inferred_for(&self, requested: Option<&str>) -> Option<&str> { - match (self.version, requested.filter(|id| !id.trim().is_empty())) { - (UploadApiVersion::V2, None) => self - .inferred_test_collection_short_id - .as_deref() - .filter(|id| !id.trim().is_empty()), - _ => None, - } - } - - /// Every v2 route is scoped to a collection, so v2 with none named and none inferred is v1. - pub fn version_for(&self, requested: Option<&str>) -> UploadApiVersion { - let has_collection = requested.is_some_and(|id| !id.trim().is_empty()) - || self.inferred_for(requested).is_some(); - match self.version { - UploadApiVersion::V2 if has_collection => UploadApiVersion::V2, - _ => UploadApiVersion::V1, - } - } -} - -impl From for UploadApiDecision { - fn from(version: UploadApiVersion) -> Self { - Self { - version, - ..Self::default() - } - } -} - #[derive(Debug, Serialize, Clone, PartialEq, Eq)] #[serde(rename_all = "camelCase")] pub struct CreateTestUploadRequest<'a> { @@ -250,7 +97,7 @@ impl<'a> From<&'a CreateBundleUploadRequest> for CreateTestUploadRequest<'a> { } } -#[derive(Debug, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone)] #[serde(rename_all = "camelCase")] pub struct CreatedTestUpload { pub id: String, @@ -304,7 +151,7 @@ impl From<&Test> for QuarantineStatusIdentifier { } } -#[derive(Debug, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone)] #[serde(rename_all = "camelCase")] pub struct QuarantineStatus { pub test_collection_uuid: String, @@ -313,14 +160,14 @@ pub struct QuarantineStatus { pub tests: Vec, } -#[derive(Debug, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone)] #[serde(rename_all = "camelCase")] pub struct QuarantineStatusTest { pub client_test_id: Option, pub quarantined: bool, } -#[derive(Debug, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone)] #[serde(rename_all = "camelCase")] pub struct QuarantinedTestList { pub data: Vec, @@ -330,7 +177,7 @@ pub struct QuarantinedTestList { pub quarantine_enabled: bool, } -#[derive(Debug, Deserialize, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone)] #[serde(rename_all = "camelCase")] pub struct QuarantinedTest { pub test_id: String, diff --git a/api/src/quarantine.rs b/api/src/quarantine.rs index 034dba19..f183b58f 100644 --- a/api/src/quarantine.rs +++ b/api/src/quarantine.rs @@ -1,9 +1,9 @@ -//! What a run needs from a quarantine lookup, whichever API answered it. The wire types stay in -//! `message`, one per API, and convert into these. +//! What a run needs from a quarantine lookup. The wire types stay in `message` and convert into +//! these. use std::collections::HashSet; -use crate::message::{GetQuarantineConfigResponse, QuarantineResolutionMode}; +use crate::message::QuarantineResolutionMode; use crate::urls::TestCaseGuidScope; /// The CLI's lookup of its failing tests. @@ -13,47 +13,17 @@ pub struct QuarantineLookup { /// The CLI's ids of the looked-up tests that are quarantined. pub quarantined_tests: Vec, pub quarantine_resolution_mode: QuarantineResolutionMode, - /// Set on v2, for the tests' GUID links. + /// For the tests' GUID links. `None` when there was nothing to look up. pub test_case_guid_scope: Option, } -impl From for QuarantineLookup { - fn from(response: GetQuarantineConfigResponse) -> Self { - Self { - is_disabled: response.is_disabled, - quarantined_tests: response.quarantined_tests, - quarantine_resolution_mode: response.quarantine_resolution_mode, - test_case_guid_scope: None, - } - } -} - /// The whole quarantined set, which a test runner checks each test it runs against. #[derive(Debug, Clone, PartialEq, Eq)] pub struct QuarantinedTestSet { pub is_disabled: bool, pub quarantine_resolution_mode: QuarantineResolutionMode, - pub tests: QuarantinedTests, -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum QuarantinedTests { - /// v1: the CLI's test ids. - ByTestId(HashSet), - /// v2: the quarantined tests' `testId`s (test-case GUIDs). A test is quarantined when the GUID - /// computed from `scope` and its own test id is in `guids`. - ByTestCaseGuid { - scope: TestCaseGuidScope, - guids: HashSet, - }, -} - -impl From for QuarantinedTestSet { - fn from(response: GetQuarantineConfigResponse) -> Self { - Self { - is_disabled: response.is_disabled, - quarantine_resolution_mode: response.quarantine_resolution_mode, - tests: QuarantinedTests::ByTestId(response.quarantined_tests.into_iter().collect()), - } - } + /// A test is quarantined when the GUID computed from `scope` and its own test id is in + /// `guids`, the quarantined tests' `testId`s. + pub scope: TestCaseGuidScope, + pub guids: HashSet, } diff --git a/api/src/urls.rs b/api/src/urls.rs index df9b97de..828b740f 100644 --- a/api/src/urls.rs +++ b/api/src/urls.rs @@ -7,8 +7,7 @@ use serde::{Deserialize, Serialize}; use url::{ParseError, Url, form_urlencoded}; use uuid::Uuid; -/// The server-resolved half of the test-case GUID tuple: from `CreateBundleUploadResponse` on -/// v1, and from any v2 quarantine or upload response. +/// The server-resolved half of the test-case GUID tuple, from any quarantine or upload response. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct TestCaseGuidScope { pub test_collection_id: String, diff --git a/api/tests/v2_client.rs b/api/tests/v2_client.rs index 9cf07313..6839ef75 100644 --- a/api/tests/v2_client.rs +++ b/api/tests/v2_client.rs @@ -1,8 +1,8 @@ -use std::{collections::HashSet, time::Duration}; +use std::collections::HashSet; use api::client::{ApiClient, GithubForkPullRequest}; -use api::message::{self, UploadApiCaller, UploadApiDecision, UploadApiVersion}; -use api::quarantine::{QuarantineLookup, QuarantinedTestSet, QuarantinedTests}; +use api::message; +use api::quarantine::{QuarantineLookup, QuarantinedTestSet}; use api::urls::TestCaseGuidScope; use axum::{ Json, @@ -14,13 +14,12 @@ use axum::{ use base64::{Engine as _, engine::general_purpose::STANDARD}; use bundle::Test; use serde_json::{Value, json}; -use test_utils::mock_server::{MockServerBuilder, RequestPayload, SharedMockServerState}; +use test_utils::mock_server::{MockServerBuilder, SharedMockServerState}; const SHORT_ID: &str = "aB3xY9kQ"; const COLLECTION_UUID: &str = "204b6c93-1c51-4e0f-a13c-e07b9bc60688"; const REPO_ID: &str = "7a1f0e3d-2b4c-4d5e-8f90-123456789abc"; -const VERSION_ROUTE: &str = "/v1/metrics/getUploadApiVersion"; const UPLOADS_ROUTE: &str = "/v2/test-collections/:id/uploads"; const STATUS_ROUTE: &str = "/v2/test-collections/:id/quarantine-status"; const LIST_ROUTE: &str = "/v2/test-collections/:id/all-quarantined-tests"; @@ -42,9 +41,7 @@ fn client(state: &SharedMockServerState, token: Option<&str>) -> ApiClient { } fn v2_client(state: &SharedMockServerState) -> ApiClient { - let mut client = client(state, Some("org-key")); - client.set_upload_api_version(UploadApiVersion::V2); - client + client(state, Some("org-key")) } fn uris(state: &SharedMockServerState) -> Vec { @@ -85,18 +82,6 @@ fn repo() -> context::repo::RepoUrlParts { } } -fn version_request(short_id: Option<&str>) -> message::GetUploadApiVersionRequest { - message::GetUploadApiVersionRequest { - cli_version: String::from("0.17.0"), - org_slug: String::from("acme"), - test_collection_short_id: short_id.map(String::from), - repo: Some(repo()), - ci_provider: Some(String::from("GitHubActions")), - is_fork_pr: false, - caller: UploadApiCaller::TestRunnerPlugin, - } -} - fn quarantine_request(test_identifiers: Vec) -> message::GetQuarantineConfigRequest { message::GetQuarantineConfigRequest { repo: repo(), @@ -130,34 +115,6 @@ fn scope() -> TestCaseGuidScope { } } -async fn decide(answer: (StatusCode, &'static str)) -> (UploadApiVersion, SharedMockServerState) { - let (decision, state) = decide_for(Some(SHORT_ID), answer).await; - (decision.version, state) -} - -async fn decide_for( - short_id: Option<&str>, - answer: (StatusCode, &'static str), -) -> (UploadApiDecision, SharedMockServerState) { - let state = serve(vec![( - VERSION_ROUTE, - post( - move |State(state): State, - headers: HeaderMap, - uri: Uri, - Json(body): Json| async move { - state.record_route_request(&headers, &uri, Some(body)); - answer - }, - ), - )]) - .await; - let decision = client(&state, Some("org-key")) - .get_upload_api_version(&version_request(short_id)) - .await; - (decision, state) -} - fn quarantine_status_route( quarantine_enabled: bool, ) -> (&'static str, MethodRouter) { @@ -230,205 +187,6 @@ fn all_quarantined_tests_route( ) } -#[tokio::test] -async fn sends_the_run_and_reads_an_explicit_v2() { - let (version, state) = decide((StatusCode::OK, r#"{"apiVersion":"v2"}"#)).await; - - assert_eq!(version, UploadApiVersion::V2); - assert_eq!( - bodies(&state), - vec![json!({ - "cliVersion": "0.17.0", - "orgSlug": "acme", - "testCollectionShortId": SHORT_ID, - "repo": {"host": "github.com", "owner": "acme", "name": "web"}, - "ciProvider": "GitHubActions", - "isForkPr": false, - "caller": "test-runner-plugin", - })] - ); -} - -#[tokio::test] -async fn reads_any_other_answer_as_v1() { - for (answer, attempts) in [ - ((StatusCode::OK, r#"{"apiVersion":"v1"}"#), 1), - ((StatusCode::OK, r#"{"apiVersion":"v3"}"#), 1), - ((StatusCode::OK, "not json"), 1), - ((StatusCode::UNAUTHORIZED, ""), 1), - // Only a 5xx, a timeout or a failed connection is worth a second attempt. - ( - (StatusCode::INTERNAL_SERVER_ERROR, r#"{"apiVersion":"v2"}"#), - 2, - ), - ] { - let (version, state) = decide(answer).await; - assert_eq!(version, UploadApiVersion::V1, "{answer:?}"); - assert_eq!(bodies(&state).len(), attempts, "{answer:?}"); - } -} - -#[tokio::test] -async fn retries_a_failed_check_once() { - let state = serve(vec![( - VERSION_ROUTE, - post( - |State(state): State, headers: HeaderMap, uri: Uri| async move { - state.record_route_request(&headers, &uri, None); - if state.route_requests.lock().unwrap().len() == 1 { - StatusCode::SERVICE_UNAVAILABLE.into_response() - } else { - Json(json!({"apiVersion": "v2"})).into_response() - } - }, - ), - )]) - .await; - - let version = client(&state, Some("org-key")) - .get_upload_api_version(&version_request(Some(SHORT_ID))) - .await - .version; - - assert_eq!((version, uris(&state).len()), (UploadApiVersion::V2, 2)); -} - -#[tokio::test(start_paused = true)] -async fn reads_a_slow_answer_as_v1() { - let state = serve(vec![( - VERSION_ROUTE, - post(|| async { - tokio::time::sleep(Duration::from_secs(3)).await; - Json(json!({"apiVersion": "v2"})) - }), - )]) - .await; - - let version = client(&state, Some("org-key")) - .get_upload_api_version(&version_request(Some(SHORT_ID))) - .await - .version; - - assert_eq!(version, UploadApiVersion::V1); -} - -#[tokio::test] -async fn does_not_ask_without_a_token_or_a_collection_and_a_repo() { - let state = serve(vec![( - VERSION_ROUTE, - post( - |State(state): State, headers: HeaderMap, uri: Uri| async move { - state.record_route_request(&headers, &uri, None); - Json(json!({"apiVersion": "v2", "testCollectionShortId": SHORT_ID})) - }, - ), - )]) - .await; - - let mut credential_free_client = ApiClient::new(None, true, "acme", None).unwrap(); - credential_free_client.api_host.clone_from(&state.host); - assert_eq!( - credential_free_client - .get_upload_api_version(&version_request(Some(SHORT_ID))) - .await, - UploadApiDecision::default() - ); - // Without a collection, only the repository lets Trunk infer one. - for repo in [ - None, - Some(context::repo::RepoUrlParts { - name: String::new(), - ..repo() - }), - ] { - let request = message::GetUploadApiVersionRequest { - repo, - ..version_request(None) - }; - assert_eq!( - client(&state, Some("org-key")) - .get_upload_api_version(&request) - .await, - UploadApiDecision::default() - ); - } - assert!(uris(&state).is_empty()); -} - -#[tokio::test] -async fn uses_the_collection_inferred_for_a_run_without_one() { - for short_id in [None, Some("")] { - let (decision, state) = decide_for( - short_id, - ( - StatusCode::OK, - r#"{"apiVersion":"v2","testCollectionShortId":"zQ8nP2mW","futureField":1}"#, - ), - ) - .await; - - assert_eq!( - decision, - UploadApiDecision { - version: UploadApiVersion::V2, - inferred_test_collection_short_id: Some(String::from("zQ8nP2mW")), - missing_test_collection: false, - } - ); - assert_eq!( - bodies(&state)[0]["repo"], - json!({"host": "github.com", "owner": "acme", "name": "web"}) - ); - } -} - -#[tokio::test] -async fn reads_a_missing_collection_for_a_run_without_one() { - let answer = r#"{"apiVersion":"v1","missingTestCollection":true}"#; - for short_id in [None, Some("")] { - let (decision, _) = decide_for(short_id, (StatusCode::OK, answer)).await; - assert_eq!( - decision, - UploadApiDecision { - version: UploadApiVersion::V1, - inferred_test_collection_short_id: None, - missing_test_collection: true, - }, - "{short_id:?}" - ); - } - let (decision, _) = decide_for(Some(SHORT_ID), (StatusCode::OK, answer)).await; - assert_eq!(decision, UploadApiDecision::default()); -} - -#[tokio::test] -async fn reads_v2_without_a_collection_as_v1() { - for answer in [ - r#"{"apiVersion":"v2"}"#, - r#"{"apiVersion":"v2","testCollectionShortId":null}"#, - r#"{"apiVersion":"v2","testCollectionShortId":" "}"#, - r#"{"apiVersion":"v1","testCollectionShortId":"zQ8nP2mW"}"#, - ] { - let (decision, state) = decide_for(None, (StatusCode::OK, answer)).await; - assert_eq!(decision, UploadApiDecision::default(), "{answer}"); - assert_eq!(bodies(&state).len(), 1, "{answer}"); - } -} - -#[tokio::test] -async fn ignores_an_inferred_collection_when_the_run_names_one() { - let (decision, _) = decide_for( - Some(SHORT_ID), - ( - StatusCode::OK, - r#"{"apiVersion":"v2","testCollectionShortId":"zQ8nP2mW"}"#, - ), - ) - .await; - - assert_eq!(decision, UploadApiDecision::from(UploadApiVersion::V2)); -} - #[tokio::test] async fn creates_a_v2_upload() { let state = serve(vec![( @@ -586,13 +344,11 @@ async fn pages_the_whole_quarantined_set_with_no_status_call() { QuarantinedTestSet { is_disabled: false, quarantine_resolution_mode: message::QuarantineResolutionMode::TestCollection, - tests: QuarantinedTests::ByTestCaseGuid { - scope: scope(), - guids: HashSet::from([ - String::from("bfeebcf4-72d1-887d-8bcd-788d0dec7f97"), - String::from("0f7f61f2-0000-8000-8000-000000000000"), - ]), - }, + scope: scope(), + guids: HashSet::from([ + String::from("bfeebcf4-72d1-887d-8bcd-788d0dec7f97"), + String::from("0f7f61f2-0000-8000-8000-000000000000"), + ]), } ); assert_eq!( @@ -640,14 +396,8 @@ async fn reads_quarantining_off_from_the_list() { .unwrap(); assert_eq!( - (set.is_disabled, set.tests), - ( - true, - QuarantinedTests::ByTestCaseGuid { - scope: scope(), - guids: HashSet::new(), - }, - ) + (set.is_disabled, set.scope, set.guids), + (true, scope(), HashSet::new()) ); assert_eq!(uris(&state).len(), 1); } @@ -681,7 +431,7 @@ async fn stops_paging_on_a_repeated_cursor() { } #[tokio::test] -async fn reports_a_failed_v2_lookup_without_falling_back_to_v1() { +async fn reports_a_failed_lookup() { let state = serve(vec![( STATUS_ROUTE, post( @@ -706,15 +456,6 @@ async fn reports_a_failed_v2_lookup_without_falling_back_to_v1() { uris(&state), vec![format!("/v2/test-collections/{SHORT_ID}/quarantine-status")] ); - // The builder serves v1's lookup, so a fallback would have reached it. - assert!( - !state - .requests - .lock() - .unwrap() - .iter() - .any(|request| matches!(request, RequestPayload::GetQuarantineConfig(_))) - ); } const LOGIN_ROUTE: &str = "/v2/auth/github-actions/login"; @@ -735,28 +476,6 @@ fn fork_pr_client(state: &SharedMockServerState, github_token: Option<&str>) -> client } -fn version_route( - answer: (StatusCode, &'static str), -) -> (&'static str, MethodRouter) { - ( - VERSION_ROUTE, - post( - move |State(state): State, headers: HeaderMap, uri: Uri| async move { - // A fork PR run asks as v1's collection lane does: the opt-in and no credential. - assert_eq!( - headers - .get("x-trunk-allow-forked-pr-uploads") - .and_then(|value| value.to_str().ok()), - Some("true") - ); - assert!(headers.get("x-api-token").is_none()); - state.record_route_request(&headers, &uri, None); - answer - }, - ), - ) -} - fn login_route( answer: (StatusCode, &'static str), ) -> (&'static str, MethodRouter) { @@ -817,9 +536,8 @@ fn upload_request() -> message::CreateBundleUploadRequest { } #[tokio::test] -async fn a_fork_pr_run_on_v2_swaps_its_github_token_and_sends_the_login_on_x_trunk_token() { +async fn a_fork_pr_run_swaps_its_github_token_and_sends_the_login_on_x_trunk_token() { let state = serve(vec![ - version_route((StatusCode::OK, r#"{"apiVersion":"v2"}"#)), login_route((StatusCode::OK, LOGIN)), quarantine_status_route(true), all_quarantined_tests_route(true), @@ -828,11 +546,7 @@ async fn a_fork_pr_run_on_v2_swaps_its_github_token_and_sends_the_login_on_x_tru .await; let mut client = fork_pr_client(&state, Some("ghs_job-token")); - let decision = client - .get_upload_api_version(&version_request(Some(SHORT_ID))) - .await; - client.use_upload_api_version(decision.version).await; - assert_eq!(client.upload_api_version(), UploadApiVersion::V2); + client.log_in_github_fork_pull_request().await; client .get_quarantining_config(&quarantine_request(vec![failure(0)])) .await @@ -859,7 +573,6 @@ async fn a_fork_pr_run_on_v2_swaps_its_github_token_and_sends_the_login_on_x_tru )) .collect::>(), vec![ - (String::from(VERSION_ROUTE), None, None), (String::from(LOGIN_ROUTE), None, None), with_login(format!("/v2/test-collections/{SHORT_ID}/quarantine-status")), with_login(format!( @@ -872,7 +585,7 @@ async fn a_fork_pr_run_on_v2_swaps_its_github_token_and_sends_the_login_on_x_tru ] ); assert_eq!( - requests[1].body, + requests[0].body, Some(json!({ "githubToken": "ghs_job-token", "pullRequestNumber": 42, @@ -882,98 +595,83 @@ async fn a_fork_pr_run_on_v2_swaps_its_github_token_and_sends_the_login_on_x_tru } #[tokio::test] -async fn a_fork_pr_run_stays_on_v1_when_the_swap_is_refused() { - for answer in [ +async fn a_fork_pr_run_whose_swap_is_refused_fails_every_call_with_the_reason() { + for (answer, reason) in [ ( - StatusCode::BAD_REQUEST, - r#"{"error":{"code":"INVALID_ARGUMENT","message":"bad"}}"#, + ( + StatusCode::BAD_REQUEST, + r#"{"error":{"code":"INVALID_ARGUMENT","message":"bad"}}"#, + ), + "Trunk answered 400 Bad Request, INVALID_ARGUMENT", ), ( - StatusCode::UNAUTHORIZED, - r#"{"error":{"code":"UNAUTHENTICATED","message":"no"}}"#, + ( + StatusCode::UNAUTHORIZED, + r#"{"error":{"code":"UNAUTHENTICATED","message":"no"}}"#, + ), + "Trunk answered 401 Unauthorized, UNAUTHENTICATED", ), ( - StatusCode::UNPROCESSABLE_ENTITY, - r#"{"error":{"code":"SHA_NOT_PR_HEAD","message":"moved"}}"#, + ( + StatusCode::UNPROCESSABLE_ENTITY, + r#"{"error":{"code":"SHA_NOT_PR_HEAD","message":"moved"}}"#, + ), + "Trunk answered 422 Unprocessable Entity, SHA_NOT_PR_HEAD", + ), + ( + (StatusCode::BAD_GATEWAY, ""), + "Trunk answered 502 Bad Gateway", + ), + ( + (StatusCode::SERVICE_UNAVAILABLE, ""), + "Trunk answered 503 Service Unavailable", + ), + ( + (StatusCode::TOO_MANY_REQUESTS, ""), + "Trunk answered 429 Too Many Requests", + ), + ( + (StatusCode::OK, r#"{"expiresAt":"2026-11-06T17:15:00Z"}"#), + "Trunk's answer could not be read", ), - (StatusCode::BAD_GATEWAY, ""), - (StatusCode::SERVICE_UNAVAILABLE, ""), - (StatusCode::TOO_MANY_REQUESTS, ""), - (StatusCode::OK, r#"{"expiresAt":"2026-11-06T17:15:00Z"}"#), ] { - let state = serve(vec![login_route(answer)]).await; + let state = serve(vec![login_route(answer), uploads_route()]).await; let mut client = fork_pr_client(&state, Some("ghs_job-token")); - client.use_upload_api_version(UploadApiVersion::V2).await; + client.log_in_github_fork_pull_request().await; + let error = client + .create_bundle_upload(&upload_request()) + .await + .unwrap_err(); - assert_eq!( - client.upload_api_version(), - UploadApiVersion::V1, - "{answer:?}" + assert!( + format!("{error:?}").contains(reason), + "{answer:?}: {error:?}" ); assert_eq!(uris(&state), vec![LOGIN_ROUTE], "{answer:?}"); } } #[tokio::test] -async fn a_fork_pr_run_without_github_token_stays_on_v1_without_a_swap() { +async fn a_fork_pr_run_without_github_token_makes_no_request() { for github_token in [None, Some(""), Some(" ")] { - let state = serve(vec![login_route((StatusCode::OK, LOGIN))]).await; + let state = serve(vec![login_route((StatusCode::OK, LOGIN)), uploads_route()]).await; let mut client = fork_pr_client(&state, github_token); - client.use_upload_api_version(UploadApiVersion::V2).await; - - assert_eq!(client.upload_api_version(), UploadApiVersion::V1); - assert!(uris(&state).is_empty(), "{github_token:?}"); - } -} + client.log_in_github_fork_pull_request().await; + let error = client + .create_bundle_upload(&upload_request()) + .await + .unwrap_err(); -#[tokio::test] -async fn a_fork_pr_run_does_not_swap_on_v1() { - for answer in [ - (StatusCode::OK, r#"{"apiVersion":"v1"}"#), - (StatusCode::UNAUTHORIZED, ""), - ] { - let state = serve(vec![ - version_route(answer), - login_route((StatusCode::OK, LOGIN)), - ]) - .await; - let mut client = fork_pr_client(&state, Some("ghs_job-token")); - - let decision = client - .get_upload_api_version(&version_request(Some(SHORT_ID))) - .await; - client.use_upload_api_version(decision.version).await; - - assert_eq!( - client.upload_api_version(), - UploadApiVersion::V1, - "{answer:?}" + assert!( + format!("{error:?}").contains("GITHUB_TOKEN is not set"), + "{github_token:?}: {error:?}" ); - assert_eq!(uris(&state), vec![VERSION_ROUTE], "{answer:?}"); + assert!(uris(&state).is_empty(), "{github_token:?}"); } } -#[tokio::test] -async fn a_credential_free_run_that_is_not_a_fork_pr_never_asks() { - let state = serve(vec![ - version_route((StatusCode::OK, r#"{"apiVersion":"v2"}"#)), - login_route((StatusCode::OK, LOGIN)), - ]) - .await; - let mut client = ApiClient::new(None, true, "acme", None).unwrap(); - client.api_host.clone_from(&state.host); - - let decision = client - .get_upload_api_version(&version_request(Some(SHORT_ID))) - .await; - client.use_upload_api_version(decision.version).await; - - assert_eq!(client.upload_api_version(), UploadApiVersion::V1); - assert!(uris(&state).is_empty()); -} - #[tokio::test] async fn a_run_with_a_token_never_swaps() { let state = serve(vec![login_route((StatusCode::OK, LOGIN)), uploads_route()]).await; @@ -981,7 +679,7 @@ async fn a_run_with_a_token_never_swaps() { client.api_host.clone_from(&state.host); client.set_github_fork_pull_request(fork_pull_request(Some("ghs_job-token"))); - client.use_upload_api_version(UploadApiVersion::V2).await; + client.log_in_github_fork_pull_request().await; client .create_bundle_upload(&upload_request()) .await diff --git a/cli/src/context_quarantine.rs b/cli/src/context_quarantine.rs index 4112b651..8fba801a 100644 --- a/cli/src/context_quarantine.rs +++ b/cli/src/context_quarantine.rs @@ -345,33 +345,31 @@ pub async fn gather_quarantine_context( }); } - let (quarantine_config, quarantine_fetch_status) = if api_client.missing_test_collection() { - tracing::debug!("Skipping quarantine check: Trunk has no test collection for this run."); - (None, QuarantineFetchStatus::FetchSkipped) - } else if !failed_tests_extractor.failed_tests().is_empty() - && file_set_builder - .file_sets() - .iter() - // internal files track quarantine status directly, so we don't need to check them - .any(|file_set| file_set.file_set_type == FileSetType::Junit) - { - tracing::info!("Checking if failed tests can be quarantined"); - match api_client.get_quarantining_config(request).await { - anyhow::Result::Ok(response) => { - if let Some(line) = response - .quarantine_resolution_mode - .resolution_log_line(request.test_collection_short_id.as_deref(), &request.repo) - { - tracing::info!("{line}"); + let (quarantine_config, quarantine_fetch_status) = + if !failed_tests_extractor.failed_tests().is_empty() + && file_set_builder + .file_sets() + .iter() + // internal files track quarantine status directly, so we don't need to check them + .any(|file_set| file_set.file_set_type == FileSetType::Junit) + { + tracing::info!("Checking if failed tests can be quarantined"); + match api_client.get_quarantining_config(request).await { + anyhow::Result::Ok(response) => { + if let Some(line) = response.quarantine_resolution_mode.resolution_log_line( + request.test_collection_short_id.as_deref(), + &request.repo, + ) { + tracing::info!("{line}"); + } + (Some(response), QuarantineFetchStatus::FetchSucceeded) } - (Some(response), QuarantineFetchStatus::FetchSucceeded) + anyhow::Result::Err(error) => (None, QuarantineFetchStatus::FetchFailed(error)), } - anyhow::Result::Err(error) => (None, QuarantineFetchStatus::FetchFailed(error)), - } - } else { - tracing::debug!("Skipping quarantine check."); - (None, QuarantineFetchStatus::FetchSkipped) - }; + } else { + tracing::debug!("Skipping quarantine check."); + (None, QuarantineFetchStatus::FetchSkipped) + }; let quarantine_resolution_mode = quarantine_config .as_ref() @@ -502,8 +500,7 @@ fn log_failure( api_client: &ApiClient, guid_scope: Option<&TestCaseGuidScope>, ) { - // On v1 the upload has not been created yet, so there are no ids to mint a GUID from; - // v2's quarantine lookup returns them. + // The upload has not been created yet; the quarantine lookup returns the ids a GUID needs. let url = request .test_collection_short_id .as_deref() diff --git a/cli/src/summary_output/notice.rs b/cli/src/summary_output/notice.rs index d59eb2e1..2bef9830 100644 --- a/cli/src/summary_output/notice.rs +++ b/cli/src/summary_output/notice.rs @@ -11,29 +11,11 @@ pub struct Notice { #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] #[serde(rename_all = "SCREAMING_SNAKE_CASE")] pub enum NoticeKind { - /// The run relies on something 1.0.0 removes. - Deprecation, /// Part of the Bazel BEP file couldn't be parsed. BepParseError, - /// The results weren't uploaded: the run named no test collection and Trunk has none for it. - UploadSkipped, } impl Notice { - pub fn deprecation>(message: T) -> Self { - Self { - kind: NoticeKind::Deprecation, - message: message.into(), - } - } - - pub fn upload_skipped>(message: T) -> Self { - Self { - kind: NoticeKind::UploadSkipped, - message: message.into(), - } - } - pub fn bep_parse_error>(message: T) -> Self { Self { kind: NoticeKind::BepParseError, diff --git a/cli/src/summary_output/text.rs b/cli/src/summary_output/text.rs index 1ef0571f..9914ab49 100644 --- a/cli/src/summary_output/text.rs +++ b/cli/src/summary_output/text.rs @@ -566,7 +566,6 @@ pub fn render_notices(notices: &[Notice]) -> anyhow::Result> { let mut output: Vec = Vec::new(); for notice in notices { let prefix = match notice.kind { - NoticeKind::Deprecation | NoticeKind::UploadSkipped => "⚠️ ", NoticeKind::BepParseError => "⚠️ BEP file had a parse error: ", }; output.push(Line::from_iter([ diff --git a/cli/src/summary_output/upload.rs b/cli/src/summary_output/upload.rs index 0aee6386..eacf16c5 100644 --- a/cli/src/summary_output/upload.rs +++ b/cli/src/summary_output/upload.rs @@ -49,7 +49,7 @@ pub struct UploadReport { /// The code the CLI exits with. Left out by the RSpec gem, where RSpec decides the exit code. #[serde(skip_serializing_if = "Option::is_none")] pub exit_code: Option, - /// Warnings that didn't stop the run, such as a deprecated flag. + /// Warnings that didn't stop the run, such as a Bazel BEP parse error. pub notices: Vec, } diff --git a/cli/src/upload_command.rs b/cli/src/upload_command.rs index 0d0543c4..c6ffa365 100644 --- a/cli/src/upload_command.rs +++ b/cli/src/upload_command.rs @@ -4,13 +4,11 @@ use std::path::PathBuf; use std::sync::mpsc::Sender; use api::client::{ApiClient, ApiErrorEndpoint, GithubForkPullRequest}; -use api::message::{GetUploadApiVersionRequest, UploadApiCaller, UploadApiDecision}; use api::urls::TestCaseGuidScope; use bundle::{BundleMeta, BundlerUtil, QuarantineResolutionMode, unzip_tarball}; use clap::{ArgAction, Args}; use codeowners::OwnersSource; use context::bazel_bep::common::BepParseResult; -use context::env::parser::CIPlatform; use display::message::DisplayMessage; use tempfile::TempDir; @@ -112,7 +110,7 @@ pub struct UploadArgs { #[arg( long, env = constants::TRUNK_ALLOW_FORKED_PR_UPLOADS_ENV, - help = "Upload with no Trunk credential, as a forked pull request must since it cannot read repository secrets. Works with no credential on Trunk API v1. On Trunk API v2 it works only for a fork pull request on GitHub Actions, using the job's GITHUB_TOKEN: trunk-io/analytics-uploader passes it by default; when running the CLI directly, set `GITHUB_TOKEN: ${{ github.token }}` on the step. The collection must have forked-PR uploads enabled in Trunk." + help = "Upload with no Trunk credential, as a forked pull request must since it cannot read repository secrets. Works only for a fork pull request on GitHub Actions, using the job's GITHUB_TOKEN: trunk-io/analytics-uploader passes it by default; when running the CLI directly, set `GITHUB_TOKEN: ${{ github.token }}` on the step. The collection must have forked-PR uploads enabled in Trunk." )] pub allow_forked_pr_uploads: bool, #[arg( @@ -451,10 +449,17 @@ pub struct RunUploadOptions { pub quarantine_query_result_override: Option, pub quarantine_resolution_mode_override: Option, - /// The API a caller already chose for this run, so its quarantine lookup and the upload - /// use the same one (and the same inferred collection). `None` asks Trunk. - pub upload_api_version: Option, - pub caller: UploadApiCaller, + pub caller: UploadCaller, +} + +/// Who runs the upload, which decides how its messages name a setting. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum UploadCaller { + /// `trunk-analytics-cli upload` or `test`, which take flags. + #[default] + Cli, + /// A test-runner plugin, which reads the environment. + TestRunnerPlugin, } impl Default for RunUploadOptions { @@ -465,14 +470,13 @@ impl Default for RunUploadOptions { render_sender: None, quarantine_query_result_override: None, quarantine_resolution_mode_override: None, - upload_api_version: None, - caller: UploadApiCaller::Cli, + caller: UploadCaller::Cli, } } } pub async fn run_upload( - mut upload_args: UploadArgs, + upload_args: UploadArgs, options: RunUploadOptions, ) -> anyhow::Result { let RunUploadOptions { @@ -481,7 +485,6 @@ pub async fn run_upload( render_sender, quarantine_query_result_override, quarantine_resolution_mode_override, - upload_api_version, caller, } = options; // grab the exec start if provided (`test` subcommand) or use the current time @@ -540,42 +543,10 @@ pub async fn run_upload( )? }; - let upload_api_decision = match upload_api_version { - Some(upload_api_decision) => upload_api_decision, - // A dry run uploads nothing, so it has nothing to choose. - None if upload_args.dry_run => UploadApiDecision::default(), - None => { - api_client - .get_upload_api_version(&upload_api_version_request( - &upload_args, - &meta.base_props.repo.repo, - &meta.base_props.envs, - caller, - )) - .await - } - }; - let requested_test_collection_short_id = upload_args.test_collection_short_id.as_deref(); - let upload_api_version = upload_api_decision.version_for(requested_test_collection_short_id); - let skip_upload = upload_api_decision.missing_test_collection - && requested_test_collection_short_id - .is_none_or(|test_collection_short_id| test_collection_short_id.trim().is_empty()); - if let Some(inferred) = upload_api_decision - .inferred_for(requested_test_collection_short_id) - .map(String::from) - { - let message = inferred_test_collection_message(&inferred, caller); - tracing::warn!("{message}"); - notices.push(Notice::deprecation(message)); - upload_args.test_collection_short_id = Some(inferred); - } - if skip_upload { - let message = missing_test_collection_message(&meta.base_props.repo.repo, caller); - tracing::warn!("{message}"); - notices.push(Notice::upload_skipped(message)); + // A dry run makes no call, so it has nothing to log in for. + if !upload_args.dry_run { + api_client.log_in_github_fork_pull_request().await; } - api_client.use_upload_api_version(upload_api_version).await; - api_client.set_missing_test_collection(skip_upload); if let Some(bep_result) = &bep_result { notices.extend(bep_result.errors.iter().map(Notice::bep_parse_error)); @@ -675,22 +646,16 @@ pub async fn run_upload( meta.failed_tests = quarantine_context.failures.clone(); let upload_started_at = chrono::Utc::now(); - let upload_bundle_result = if skip_upload { - None - } else { - tracing::info!("Uploading test results..."); - Some( - upload_bundle( - &mut meta, - upload_args.test_collection_short_id.clone(), - &api_client, - bep_result, - quarantine_context.exit_code, - upload_args.dry_run, - ) - .await, - ) - }; + tracing::info!("Uploading test results..."); + let upload_bundle_result = upload_bundle( + &mut meta, + upload_args.test_collection_short_id.clone(), + &api_client, + bep_result, + quarantine_context.exit_code, + upload_args.dry_run, + ) + .await; let quarantine_query_result = quarantine_query_result_override.unwrap_or_else(|| { quarantine_query_result(upload_args.disable_quarantining, &quarantine_context) }); @@ -718,9 +683,7 @@ pub async fn run_upload( .into(), }; let mut request = api::message::TelemetryUploadMetricsRequest { upload_metrics }; - if !upload_args.dry_run - && let Some(upload_bundle_result) = &upload_bundle_result - { + if !upload_args.dry_run { let telemetry_response; if let Some(err) = upload_bundle_result.as_ref().err() { request.upload_metrics.failed = true; @@ -739,12 +702,11 @@ pub async fn run_upload( } } - if matches!(upload_bundle_result, Some(Err(_))) { + if upload_bundle_result.is_err() { tracing::error!("Failed to upload bundle"); } let (guid_scope, error_report) = match upload_bundle_result { - None => (None, None), - Some(Ok(uploaded)) => { + Ok(uploaded) => { if upload_args.dry_run { let curr_dir = env::current_dir()?; let bundle_file = curr_dir.join(DRY_RUN_OUTPUT_DIR); @@ -752,7 +714,7 @@ pub async fn run_upload( } (uploaded.guid_scope, None) } - Some(Err(e)) => ( + Err(e) => ( None, Some(ErrorReport::new( e, @@ -789,10 +751,10 @@ pub async fn run_upload( }) } -pub fn missing_test_collection_id_message(caller: UploadApiCaller) -> String { +pub fn missing_test_collection_id_message(caller: UploadCaller) -> String { let how_to_pass = match caller { - UploadApiCaller::Cli => String::from("Pass --test-collection-id"), - UploadApiCaller::TestRunnerPlugin => { + UploadCaller::Cli => String::from("Pass --test-collection-id"), + UploadCaller::TestRunnerPlugin => { format!("Set {}", constants::TRUNK_TEST_COLLECTION_ID_ENV) } }; @@ -801,79 +763,6 @@ pub fn missing_test_collection_id_message(caller: UploadApiCaller) -> String { ) } -/// The run goes on as if the user had named the collection Trunk inferred, until 1.0.0 requires it. -pub fn inferred_test_collection_message(short_id: &str, caller: UploadApiCaller) -> String { - let how_to_pass = match caller { - UploadApiCaller::Cli => format!("Pass --test-collection-id {short_id}"), - UploadApiCaller::TestRunnerPlugin => { - format!("Set {}={short_id}", constants::TRUNK_TEST_COLLECTION_ID_ENV) - } - }; - format!( - "Uploading to test collection {short_id}, inferred from the repository. {how_to_pass}: analytics-cli 1.0.0 requires it." - ) -} - -/// Trunk would drop the upload, since the org files every upload under a test collection. -pub fn missing_test_collection_message( - repo: &context::repo::RepoUrlParts, - caller: UploadApiCaller, -) -> String { - let how_to_pass = match caller { - UploadApiCaller::Cli => String::from("Pass --test-collection-id"), - UploadApiCaller::TestRunnerPlugin => { - format!("Set {}", constants::TRUNK_TEST_COLLECTION_ID_ENV) - } - }; - let repo_name = if repo.owner.is_empty() && repo.name.is_empty() { - String::from("this repository") - } else { - format!("{}/{}", repo.owner, repo.name) - }; - format!( - "Test results were not uploaded: this run names no test collection, and Trunk has none for {repo_name}. {how_to_pass} to upload them." - ) -} - -pub fn upload_api_version_request( - upload_args: &UploadArgs, - repo: &context::repo::RepoUrlParts, - envs: &context::env::EnvVars, - caller: UploadApiCaller, -) -> GetUploadApiVersionRequest { - let ci_provider: &str = CIPlatform::from(envs).into(); - GetUploadApiVersionRequest { - cli_version: env!("CARGO_PKG_VERSION").to_string(), - org_slug: upload_args.org_url_slug.clone(), - test_collection_short_id: upload_args - .test_collection_short_id - .clone() - .filter(|id| !id.is_empty()), - repo: Some(repo.clone()), - ci_provider: Some(ci_provider.to_string()), - // The captured envs leave the event path out, so it is read from the process too. - is_fork_pr: is_fork_pr( - envs.get("GITHUB_EVENT_PATH") - .cloned() - .or_else(|| env::var("GITHUB_EVENT_PATH").ok()) - .as_deref(), - envs, - ), - caller, - } -} - -/// Whether the run builds a pull request from another repository. Fork runs usually have no API -/// token and so never ask; this catches the ones that do, such as `pull_request_target`. -pub fn is_fork_pr(github_event_path: Option<&str>, envs: &context::env::EnvVars) -> bool { - if let Some(event_path) = github_event_path { - return github_event_is_fork_pr(event_path).unwrap_or(false); - } - // CircleCI sets CIRCLE_PR_NUMBER only for a pull request from a fork. - envs.get("CIRCLE_PR_NUMBER") - .is_some_and(|number| !number.is_empty()) -} - fn github_event_pull_request(event_path: &str) -> Option { let mut event: serde_json::Value = serde_json::from_reader(std::fs::File::open(event_path).ok()?).ok()?; @@ -886,10 +775,6 @@ fn pull_request_is_from_fork(pull_request: &serde_json::Value) -> Option { Some(!head.eq_ignore_ascii_case(base)) } -fn github_event_is_fork_pr(event_path: &str) -> Option { - pull_request_is_from_fork(&github_event_pull_request(event_path)?) -} - /// The fork pull request a GitHub Actions run builds, with the job's `GITHUB_TOKEN`; `None` for /// any other run. fn github_fork_pull_request(event_path: &str) -> Option { @@ -984,9 +869,7 @@ pub fn get_bundle_upload_id_message(bundle_upload_id: &str) -> String { #[cfg(test)] mod fork_pr_tests { - use std::collections::HashMap; - - use super::is_fork_pr; + use super::github_fork_pull_request; fn github_event(head: &str, base: &str) -> tempfile::NamedTempFile { let file = tempfile::NamedTempFile::new().unwrap(); @@ -994,7 +877,8 @@ mod fork_pr_tests { file.path(), serde_json::json!({ "pull_request": { - "head": {"repo": {"full_name": head}}, + "number": 42, + "head": {"sha": "ABCDEF0123", "repo": {"full_name": head}}, "base": {"repo": {"full_name": base}}, } }) @@ -1004,13 +888,6 @@ mod fork_pr_tests { file } - fn envs(pairs: &[(&str, &str)]) -> HashMap { - pairs - .iter() - .map(|(key, value)| (key.to_string(), value.to_string())) - .collect() - } - #[test] fn reads_a_github_pull_request_from_another_repository_as_a_fork() { let fork = github_event("someone/web", "acme/web"); @@ -1019,20 +896,11 @@ mod fork_pr_tests { std::fs::write(push.path(), r#"{"ref": "refs/heads/main"}"#).unwrap(); assert_eq!( - [&fork, &same, &push].map(|event| is_fork_pr(event.path().to_str(), &HashMap::new())), - [true, false, false] - ); - } - - #[test] - fn reads_circleci_pr_number_as_a_fork() { - assert_eq!( - [ - envs(&[("CIRCLE_PR_NUMBER", "42")]), - envs(&[("CIRCLE_PR_NUMBER", "")]), - ] - .map(|envs| is_fork_pr(None, &envs)), - [true, false] + [&fork, &same, &push].map(|event| github_fork_pull_request( + event.path().to_str().unwrap() + ) + .map(|pull_request| (pull_request.number, pull_request.head_sha))), + [Some((42, String::from("abcdef0123"))), None, None] ); } } diff --git a/cli/tests/summary/upload.rs b/cli/tests/summary/upload.rs index c2382096..39e2226a 100644 --- a/cli/tests/summary/upload.rs +++ b/cli/tests/summary/upload.rs @@ -229,11 +229,8 @@ fn prints_a_passing_run() { fn prints_notices_apart_from_the_summary() { let mut result = upload_result(5); result.notices = vec![ - Notice::deprecation( - "Uploading to test collection zQ8nP2mW, inferred from the repository. Pass --test-collection-id zQ8nP2mW: analytics-cli 1.0.0 requires it.", - ), - Notice::upload_skipped("Test results were not uploaded."), Notice::bep_parse_error("unexpected EOF"), + Notice::bep_parse_error("invalid wire type"), ]; let report = UploadReport::new(&result, UploadCommand::Upload); @@ -244,9 +241,8 @@ fn prints_notices_apart_from_the_summary() { .map(Line::to_unstyled) .collect::>(), [ - "⚠️ Uploading to test collection zQ8nP2mW, inferred from the repository. Pass --test-collection-id zQ8nP2mW: analytics-cli 1.0.0 requires it.", - "⚠️ Test results were not uploaded.", "⚠️ BEP file had a parse error: unexpected EOF", + "⚠️ BEP file had a parse error: invalid wire type", "", ] ); diff --git a/cli/tests/test.rs b/cli/tests/test.rs index 5f01847b..1e85b53a 100644 --- a/cli/tests/test.rs +++ b/cli/tests/test.rs @@ -3,12 +3,7 @@ use std::{ io::BufReader, }; -use api::message::{ - CreateBundleUploadRequest, CreateBundleUploadResponse, GetQuarantineConfigRequest, - GetQuarantineConfigResponse, -}; use assert_matches::assert_matches; -use axum::{Json, extract::State}; use bundle::BundleMeta; mod common; @@ -22,9 +17,7 @@ use common::{ use context::{bazel_bep::parser::BazelBepParser, junit::parser::JunitParser}; use predicates::prelude::*; use tempfile::tempdir; -use test_utils::mock_server::{ - MOCK_REPO_ID, MOCK_TEST_COLLECTION_ID, MockServerBuilder, RequestPayload, SharedMockServerState, -}; +use test_utils::mock_server::{MockServerBuilder, RequestPayload, quarantine_all_handler}; use trunk_analytics_cli::exit_code::ExitCode; // NOTE: must be multi threaded to start a mock server @@ -107,7 +100,7 @@ async fn test_run_test_with_really_long_command() { // First request should be create bundle upload assert_matches!( requests_iter.next().unwrap(), - RequestPayload::CreateBundleUpload(_) + RequestPayload::CreateTestUpload(_) ); // Second request should be s3 upload @@ -168,7 +161,7 @@ async fn test_command_fails_with_no_junit_files_no_quarantine_successful_upload( assert!(matches!( requests_iter.next().unwrap(), - RequestPayload::CreateBundleUpload(..) + RequestPayload::CreateTestUpload(..) )); let tar_extract_directory = @@ -253,7 +246,7 @@ async fn test_command_succeeds_with_bundle_using_bep() { let requests = state.requests.lock().unwrap().clone(); assert_eq!(requests.len(), 3); - assert_matches!(requests[0], RequestPayload::CreateBundleUpload(_)); + assert_matches!(requests[0], RequestPayload::CreateTestUpload(_)); let tar_extract_directory = assert_matches!(&requests[1], RequestPayload::S3Upload(d) => d); assert_matches!(requests[2], RequestPayload::TelemetryUploadMetrics(_)); @@ -282,44 +275,8 @@ async fn quarantining_resets_fail_code() { let mut mock_server_builder = MockServerBuilder::new(); - mock_server_builder.set_get_quarantining_config_handler( - |Json(get_quarantine_bulk_test_status_request): Json| { - let test_ids = get_quarantine_bulk_test_status_request - .test_identifiers - .into_iter() - .map(|t| t.id) - .collect::>(); - async move { - Json(GetQuarantineConfigResponse { - is_disabled: false, - quarantined_tests: test_ids, - ..Default::default() - }) - } - }, - ); + mock_server_builder.set_quarantine_status_handler(quarantine_all_handler); - mock_server_builder.set_create_bundle_handler( - |State(state): State, _: Json| { - async move { - let host = &state.host.clone(); - Ok::, String>(Json(CreateBundleUploadResponse { - id: String::from("test-bundle-upload-id"), - id_v2: String::from("test-bundle-upload-id-v2"), - url: format!("{host}/s3upload"), - key: String::from("unused"), - test_collection_bundle_meta_id: Some(String::from( - "82c6a6e5-f8ea-4d93-9a26-b8ab6ff8f6bc", - )), - test_collection_bundle_meta_created_at: Some(String::from( - "2026-05-10T12:34:56.000Z", - )), - repo_id: Some(String::from(MOCK_REPO_ID)), - test_collection_id: Some(String::from(MOCK_TEST_COLLECTION_ID)), - })) - } - }, - ); let state = mock_server_builder.spawn_mock_server().await; CommandBuilder::test( @@ -345,44 +302,8 @@ async fn quarantining_not_active_when_disable_quarantining_set() { let mut mock_server_builder = MockServerBuilder::new(); - mock_server_builder.set_get_quarantining_config_handler( - |Json(get_quarantine_bulk_test_status_request): Json| { - let test_ids = get_quarantine_bulk_test_status_request - .test_identifiers - .into_iter() - .map(|t| t.id) - .collect::>(); - async move { - Json(GetQuarantineConfigResponse { - is_disabled: false, - quarantined_tests: test_ids, - ..Default::default() - }) - } - }, - ); + mock_server_builder.set_quarantine_status_handler(quarantine_all_handler); - mock_server_builder.set_create_bundle_handler( - |State(state): State, _: Json| { - async move { - let host = &state.host.clone(); - Ok::, String>(Json(CreateBundleUploadResponse { - id: String::from("test-bundle-upload-id"), - id_v2: String::from("test-bundle-upload-id-v2"), - url: format!("{host}/s3upload"), - key: String::from("unused"), - test_collection_bundle_meta_id: Some(String::from( - "82c6a6e5-f8ea-4d93-9a26-b8ab6ff8f6bc", - )), - test_collection_bundle_meta_created_at: Some(String::from( - "2026-05-10T12:34:56.000Z", - )), - repo_id: Some(String::from(MOCK_REPO_ID)), - test_collection_id: Some(String::from(MOCK_TEST_COLLECTION_ID)), - })) - } - }, - ); let state = mock_server_builder.spawn_mock_server().await; CommandBuilder::test( @@ -409,44 +330,8 @@ async fn quarantining_not_active_when_disable_true_but_use_true() { let mut mock_server_builder = MockServerBuilder::new(); - mock_server_builder.set_get_quarantining_config_handler( - |Json(get_quarantine_bulk_test_status_request): Json| { - let test_ids = get_quarantine_bulk_test_status_request - .test_identifiers - .into_iter() - .map(|t| t.id) - .collect::>(); - async move { - Json(GetQuarantineConfigResponse { - is_disabled: false, - quarantined_tests: test_ids, - ..Default::default() - }) - } - }, - ); + mock_server_builder.set_quarantine_status_handler(quarantine_all_handler); - mock_server_builder.set_create_bundle_handler( - |State(state): State, _: Json| { - async move { - let host = &state.host.clone(); - Ok::, String>(Json(CreateBundleUploadResponse { - id: String::from("test-bundle-upload-id"), - id_v2: String::from("test-bundle-upload-id-v2"), - url: format!("{host}/s3upload"), - key: String::from("unused"), - test_collection_bundle_meta_id: Some(String::from( - "82c6a6e5-f8ea-4d93-9a26-b8ab6ff8f6bc", - )), - test_collection_bundle_meta_created_at: Some(String::from( - "2026-05-10T12:34:56.000Z", - )), - repo_id: Some(String::from(MOCK_REPO_ID)), - test_collection_id: Some(String::from(MOCK_TEST_COLLECTION_ID)), - })) - } - }, - ); let state = mock_server_builder.spawn_mock_server().await; CommandBuilder::test( diff --git a/cli/tests/upload.rs b/cli/tests/upload.rs index 0276c4fb..22761b3e 100644 --- a/cli/tests/upload.rs +++ b/cli/tests/upload.rs @@ -3,16 +3,17 @@ use std::sync::{Arc, Mutex}; use std::time::Duration; use std::{fs, io::BufReader}; -use api::message::{ - CreateBundleUploadRequest, CreateBundleUploadResponse, GetQuarantineConfigRequest, - GetQuarantineConfigResponse, -}; use api::otlp::DURATION_BOUNDS_SECONDS; use assert_cmd::{Command, cargo}; use assert_matches::assert_matches; use axum::body::{Body, Bytes}; use axum::response::IntoResponse; -use axum::{Json, extract::State, http::StatusCode, response::Response}; +use axum::{ + Json, + extract::{Path, State}, + http::{HeaderMap, StatusCode}, + response::Response, +}; use bundle::{ BundleMeta, FileSetType, INTERNAL_BIN_FILENAME, QuarantineResolutionMode, TestCollectionProps, }; @@ -56,8 +57,9 @@ use test_utils::inputs::unpack_archive_to_dir; use test_utils::{ inputs::get_test_file_path, mock_server::{ - MOCK_REPO_ID, MOCK_TEST_COLLECTION_ID, MockServerBuilder, RequestPayload, - SharedMockServerState, TelemetryUploadMetrics, + MOCK_REPO_ID, MOCK_TEST_COLLECTION_ID, MOCK_UPLOAD_CREATED_AT, MOCK_UPLOAD_ID, + MockServerBuilder, QuarantineStatusLookup, RequestPayload, SharedMockServerState, + TelemetryUploadMetrics, created_test_upload, quarantine_all_handler, quarantine_status, }, }; use trunk_analytics_cli::exit_code::ExitCode; @@ -117,43 +119,39 @@ async fn upload_bundle() { let quarantine_request = requests_iter.next().unwrap(); let mut failure_count = 0; - assert_matches!(quarantine_request, RequestPayload::GetQuarantineConfig(req) => { - assert_eq!(req.repo.host, "github.com"); - assert_eq!(req.repo.owner, "trunk-io"); - assert_eq!(req.repo.name, "analytics-cli"); - assert_eq!(req.org_url_slug, "test-org"); + assert_matches!(quarantine_request, RequestPayload::QuarantineStatus(req) => { assert_eq!( - req.test_collection_short_id, - Some(String::from("tc_123")), - "--test-collection-id must reach the getQuarantineConfig request" + req.repo, + Some(Repo { + host: String::from("github.com"), + owner: String::from("trunk-io"), + name: String::from("analytics-cli"), + }) + ); + assert_eq!( + req.test_collection_short_id, "tc_123", + "--test-collection-id must name the quarantine-status route" ); assert!( !req.test_identifiers.is_empty(), "test_identifiers should not be empty" ); for test in &req.test_identifiers { - assert!(!test.name.is_empty(), "Test name should not be empty"); - assert!( - !test.parent_name.is_empty(), - "Parent name should not be empty" - ); assert!(test.id.len() == 36, "Test ID should be a valid UUID"); - if test.failure_message.is_some() { - failure_count += 1; - } + failure_count += 1; } }); - let upload_request = assert_matches!(requests_iter.next().unwrap(), RequestPayload::CreateBundleUpload(ur) => ur); + let upload_request = + assert_matches!(requests_iter.next().unwrap(), RequestPayload::CreateTestUpload(ur) => ur); assert_eq!( upload_request.repo, - Repo { + Some(Repo { host: String::from("github.com"), owner: String::from("trunk-io"), name: String::from("analytics-cli"), - } + }) ); - assert_eq!(upload_request.org_url_slug, "test-org"); assert!( upload_request .client_version @@ -162,10 +160,7 @@ async fn upload_bundle() { assert!(upload_request.client_version.contains(" git=")); assert!(upload_request.client_version.contains(" rustc=")); assert!(upload_request.external_id.is_some()); - assert_eq!( - upload_request.test_collection_short_id, - Some(String::from("tc_123")) - ); + assert_eq!(upload_request.test_collection_short_id, "tc_123"); let tar_extract_directory = assert_matches!(requests_iter.next().unwrap(), RequestPayload::S3Upload(d) => d); @@ -206,13 +201,13 @@ async fn upload_bundle() { base_props.repo.repo_head_author_email, "your.email@example.com" ); - assert_eq!(base_props.bundle_upload_id, "test-bundle-upload-id"); + assert_eq!(base_props.bundle_upload_id, MOCK_UPLOAD_ID); assert_eq!( base_props.test_collection, Some(TestCollectionProps { short_id: String::from("tc_123"), - bundle_meta_id: String::from("82c6a6e5-f8ea-4d93-9a26-b8ab6ff8f6bc"), - bundle_meta_created_at: String::from("2026-05-10T12:34:56.000Z"), + bundle_meta_id: String::from(MOCK_UPLOAD_ID), + bundle_meta_created_at: String::from(MOCK_UPLOAD_CREATED_AT), }) ); assert_eq!(base_props.tags, &[]); @@ -232,11 +227,14 @@ async fn upload_bundle() { assert!(base_props.quarantined_tests.is_empty()); assert_eq!( base_props.quarantine_resolution_mode, - QuarantineResolutionMode::Unspecified, - "a server reporting no resolution mode must land as unspecified" + QuarantineResolutionMode::TestCollection, + "quarantine status always resolves per test collection" ); assert_eq!(bundle_meta.failed_tests.len(), failure_count); - assert_eq!(bundle_meta.bundle_upload_id_v2, "test-bundle-upload-id-v2"); + assert_eq!( + bundle_meta.bundle_upload_id_v2, + "eyJpZCI6IjgyYzZhNmU1LWY4ZWEtNGQ5My05YTI2LWI4YWI2ZmY4ZjZiYyIsImNyZWF0ZWRBdCI6IjIwMjYtMDUtMTBUMTI6MzQ6NTYuMDAwWiIsInJlcG9JZCI6IjdhMWYwZTNkLTJiNGMtNGQ1ZS04ZjkwLTEyMzQ1Njc4OWFiYyJ9" + ); assert_eq!( base_props.codeowners, Some(CodeOwners { @@ -340,50 +338,6 @@ async fn upload_bundle_prints_test_collection_links() { .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_falls_back_to_short_links_without_collection_ids() { - let temp_dir = tempdir().unwrap(); - generate_mock_git_repo(&temp_dir); - generate_mock_valid_junit_xmls(&temp_dir); - - async fn create_bundle_without_collection_ids( - State(state): State, - ) -> Json { - let host = &state.host; - Json(CreateBundleUploadResponse { - id: String::from("test-bundle-upload-id"), - id_v2: String::from("test-bundle-upload-id-v2"), - url: format!("{host}/s3upload"), - key: String::from("unused"), - test_collection_bundle_meta_id: Some(String::from( - "82c6a6e5-f8ea-4d93-9a26-b8ab6ff8f6bc", - )), - test_collection_bundle_meta_created_at: Some(String::from("2026-05-10T12:34:56.000Z")), - repo_id: None, - test_collection_id: None, - }) - } - - let mut builder = MockServerBuilder::new(); - builder.set_create_bundle_handler(create_bundle_without_collection_ids); - let state = builder.spawn_mock_server().await; - - let assert = CommandBuilder::upload(temp_dir.path(), state.host.clone()) - .command() - .arg("--test-collection-id") - .arg("tc_123") - .assert() - .failure(); - - assert - .stdout(predicate::str::contains( - "/test-org/flaky-tests/collections/tc_123/t/", - )) - .stdout(predicate::str::contains("?repo=trunk-io%2Fanalytics-cli")) - .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() { @@ -399,10 +353,6 @@ async fn failed_v2_upload_links_tests_by_the_quarantine_lookup_ids() { .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 { @@ -564,18 +514,7 @@ async fn upload_bundle_records_quarantine_resolution_mode() { generate_mock_git_repo(&temp_dir); generate_mock_valid_junit_xmls(&temp_dir); - let mut mock_server_builder = MockServerBuilder::new(); - mock_server_builder.set_get_quarantining_config_handler( - |Json(_): Json| async move { - Json(GetQuarantineConfigResponse { - is_disabled: false, - quarantined_tests: Vec::new(), - quarantine_resolution_mode: QuarantineResolutionMode::TestCollection, - ..Default::default() - }) - }, - ); - let state = mock_server_builder.spawn_mock_server().await; + let state = MockServerBuilder::new().spawn_mock_server().await; CommandBuilder::upload(temp_dir.path(), state.host.clone()) .command() @@ -621,7 +560,7 @@ async fn upload_bundle_using_bep() { let requests = state.requests.lock().unwrap().clone(); assert_eq!(requests.len(), 3); - assert_matches!(requests[0], RequestPayload::CreateBundleUpload(_)); + assert_matches!(requests[0], RequestPayload::CreateTestUpload(_)); let tar_extract_directory = assert_matches!(&requests[1], RequestPayload::S3Upload(d) => d); assert_matches!(requests[2], RequestPayload::TelemetryUploadMetrics(_)); @@ -809,80 +748,6 @@ async fn upload_bundle_using_bep_without_bazel_label_codeowners_fallback() { ); } -#[tokio::test(flavor = "multi_thread")] -async fn upload_bundle_without_canonical_test_collection_metadata_keeps_bundle_group_empty() { - let temp_dir = tempdir().unwrap(); - generate_mock_git_repo(&temp_dir); - generate_mock_valid_junit_xmls(&temp_dir); - generate_mock_codeowners(&temp_dir); - - let mut mock_server_builder = MockServerBuilder::new(); - mock_server_builder.set_create_bundle_handler( - |State(state): State, - Json(create_bundle_upload_request): Json| async move { - state - .requests - .lock() - .unwrap() - .push(RequestPayload::CreateBundleUpload( - create_bundle_upload_request, - )); - - let host = &state.host; - Ok::, String>(Json(CreateBundleUploadResponse { - id: String::from("test-bundle-upload-id"), - id_v2: String::from("test-bundle-upload-id-v2"), - url: format!("{host}/s3upload"), - key: String::from("unused"), - test_collection_bundle_meta_id: None, - test_collection_bundle_meta_created_at: None, - repo_id: None, - test_collection_id: None, - })) - }, - ); - let state = mock_server_builder.spawn_mock_server().await; - - CommandBuilder::upload(temp_dir.path(), state.host.clone()) - .command() - .arg("--test-collection-id") - .arg("tc_123") - .assert() - .failure(); - - let requests = state.requests.lock().unwrap().clone(); - let upload_request = requests - .into_iter() - .find_map(|request| match request { - RequestPayload::CreateBundleUpload(upload_request) => Some(upload_request), - _ => None, - }) - .unwrap(); - assert_eq!( - upload_request.test_collection_short_id, - Some(String::from("tc_123")) - ); - - let tar_extract_directory = state - .requests - .lock() - .unwrap() - .clone() - .into_iter() - .find_map(|request| match request { - RequestPayload::S3Upload(path) => Some(path), - _ => None, - }) - .unwrap(); - - let file = fs::File::open(tar_extract_directory.join("meta.json")).unwrap(); - let reader = BufReader::new(file); - let bundle_meta: BundleMeta = serde_json::from_reader(reader).unwrap(); - - assert_eq!(bundle_meta.base_props.test_collection, None); - assert_eq!(bundle_meta.bundle_upload_id_v2, "test-bundle-upload-id-v2"); -} - // The declaration path is exercised end-to-end here too, so a bundle that uploads cleanly // today cannot start failing behind the flag without this noticing. #[cfg(target_os = "macos")] @@ -910,7 +775,7 @@ async fn upload_bundle_using_xcresult(#[case] extra_args: &[&str]) { let requests = state.requests.lock().unwrap().clone(); assert_eq!(requests.len(), 3); - assert_matches!(requests[0], RequestPayload::CreateBundleUpload(_)); + assert_matches!(requests[0], RequestPayload::CreateTestUpload(_)); let tar_extract_directory = assert_matches!(&requests[1], RequestPayload::S3Upload(d) => d); assert_matches!(requests[2], RequestPayload::TelemetryUploadMetrics(_)); @@ -1028,28 +893,7 @@ async fn dry_run_quarantines_failing_test_without_uploading() { "#).unwrap(); let mut mock_server_builder = MockServerBuilder::new(); - #[axum::debug_handler] - async fn quarantine_all_handler( - State(state): State, - Json(request): Json, - ) -> Json { - let quarantined_tests = request - .test_identifiers - .iter() - .map(|t| t.id.clone()) - .collect(); - state - .requests - .lock() - .unwrap() - .push(RequestPayload::GetQuarantineConfig(request)); - Json(GetQuarantineConfigResponse { - is_disabled: false, - quarantined_tests, - ..Default::default() - }) - } - mock_server_builder.set_get_quarantining_config_handler(quarantine_all_handler); + mock_server_builder.set_quarantine_status_handler(quarantine_all_handler); let state = mock_server_builder.spawn_mock_server().await; let assert = CommandBuilder::upload(temp_dir.path(), state.host.clone()) @@ -1064,7 +908,7 @@ async fn dry_run_quarantines_failing_test_without_uploading() { // overriding the exit code to 0 — but no upload or telemetry requests are made. let requests = state.requests.lock().unwrap().clone(); assert_eq!(requests.len(), 1); - assert_matches!(requests[0], RequestPayload::GetQuarantineConfig(..)); + assert_matches!(requests[0], RequestPayload::QuarantineStatus(..)); let output_dir = temp_dir.path().join(DRY_RUN_OUTPUT_DIR); let meta_json = fs::File::open(output_dir.join("meta.json")).unwrap(); @@ -1111,7 +955,7 @@ async fn upload_bundle_success_status_code() { let requests = state.requests.lock().unwrap().clone(); assert_eq!(requests.len(), 3); - assert_matches!(requests[0], RequestPayload::CreateBundleUpload(_)); + assert_matches!(requests[0], RequestPayload::CreateTestUpload(_)); let tar_extract_directory = assert_matches!(&requests[1], RequestPayload::S3Upload(d) => d); assert_matches!(requests[2], RequestPayload::TelemetryUploadMetrics(_)); @@ -1227,7 +1071,7 @@ async fn upload_bundle_bep_with_unknown_fields() { let requests = state.requests.lock().unwrap().clone(); assert_eq!(requests.len(), 3); - assert_matches!(requests[0], RequestPayload::CreateBundleUpload(_)); + assert_matches!(requests[0], RequestPayload::CreateTestUpload(_)); let tar_extract_directory = assert_matches!(&requests[1], RequestPayload::S3Upload(d) => d); assert_matches!(requests[2], RequestPayload::TelemetryUploadMetrics(_)); @@ -1267,7 +1111,7 @@ async fn falls_back_to_binary_file() { let requests = state.requests.lock().unwrap().clone(); assert_eq!(requests.len(), 3); - assert_matches!(requests[0], RequestPayload::CreateBundleUpload(_)); + assert_matches!(requests[0], RequestPayload::CreateTestUpload(_)); let tar_extract_directory = assert_matches!(&requests[1], RequestPayload::S3Upload(d) => d); assert_matches!(requests[2], RequestPayload::TelemetryUploadMetrics(_)); @@ -1319,7 +1163,7 @@ async fn upload_bundle_success_preceding_failure() { let requests = state.requests.lock().unwrap().clone(); assert_eq!(requests.len(), 3); - assert_matches!(requests[0], RequestPayload::CreateBundleUpload(_)); + assert_matches!(requests[0], RequestPayload::CreateTestUpload(_)); let tar_extract_directory = assert_matches!(&requests[1], RequestPayload::S3Upload(d) => d); assert_matches!(requests[2], RequestPayload::TelemetryUploadMetrics(_)); @@ -1703,7 +1547,7 @@ async fn upload_writes_a_dry_run_to_summary_output_file() { let requests = state.requests.lock().unwrap().clone(); assert_eq!(requests.len(), 1); - assert_matches!(requests[0], RequestPayload::GetQuarantineConfig(..)); + assert_matches!(requests[0], RequestPayload::QuarantineStatus(..)); let summary = read_summary_output_file(&summary_path); assert_eq!(summary["dryRun"], true); @@ -1792,10 +1636,10 @@ async fn upload_prints_only_the_json_summary_on_stdout() { let mut mock_server_builder = MockServerBuilder::new(); // Slow enough to show a progress message. - mock_server_builder.set_get_quarantining_config_handler( - |Json(_): Json| async move { + mock_server_builder.set_quarantine_status_handler( + |Json(lookup): Json| async move { tokio::time::sleep(Duration::from_secs(3)).await; - Json(GetQuarantineConfigResponse::default()) + Json(quarantine_status(&lookup, true, Vec::::new())) }, ); let state = mock_server_builder.spawn_mock_server().await; @@ -1911,13 +1755,13 @@ async fn quarantines_tests_regardless_of_upload() { Some, All, } - mock_server_builder.set_get_quarantining_config_handler( - |Json(get_quarantine_bulk_test_status_request): Json| { - let mut test_ids = get_quarantine_bulk_test_status_request - .test_identifiers - .into_iter() - .map(|t| t.id) - .collect::>(); + mock_server_builder.set_quarantine_status_handler( + |State(state): State, + Path(test_collection_short_id): Path, + headers: HeaderMap, + Json(lookup): Json| { + let lookup = state.record_quarantine_status(headers, test_collection_short_id, lookup); + let mut test_ids = lookup.test_ids(); let quarantine_config_response = *QUARANTINE_CONFIG_RESPONSE.lock().unwrap(); let quarantined_tests = match quarantine_config_response { QuarantineConfigResponse::Disabled => Vec::new(), @@ -1925,16 +1769,16 @@ async fn quarantines_tests_regardless_of_upload() { QuarantineConfigResponse::Some => test_ids.split_off(1), QuarantineConfigResponse::All => test_ids, }; - let is_disabled = matches!( + let quarantine_enabled = !matches!( quarantine_config_response, QuarantineConfigResponse::Disabled ); async move { - Json(GetQuarantineConfigResponse { - is_disabled, + Json(quarantine_status( + &lookup, + quarantine_enabled, quarantined_tests, - ..Default::default() - }) + )) } }, ); @@ -1957,27 +1801,12 @@ async fn quarantines_tests_regardless_of_upload() { match create_bundle_response { CreateBundleResponse::Error => StatusCode::INTERNAL_SERVER_ERROR.into_response(), CreateBundleResponse::Success => { - let host = &state.host; - Json(CreateBundleUploadResponse { - id: String::from("test-bundle-upload-id"), - id_v2: String::from("test-bundle-upload-id-v2"), - url: format!("{host}/s3upload"), - key: String::from("unused"), - test_collection_bundle_meta_id: Some(String::from( - "82c6a6e5-f8ea-4d93-9a26-b8ab6ff8f6bc", - )), - test_collection_bundle_meta_created_at: Some(String::from( - "2026-05-10T12:34:56.000Z", - )), - repo_id: Some(String::from(MOCK_REPO_ID)), - test_collection_id: Some(String::from(MOCK_TEST_COLLECTION_ID)), - }) - .into_response() + (StatusCode::CREATED, Json(created_test_upload(&state.host))).into_response() } } } - mock_server_builder.set_create_bundle_handler(create_bundle_handler); + mock_server_builder.set_create_test_upload_handler(create_bundle_handler); let state = mock_server_builder.spawn_mock_server().await; let mut command = CommandBuilder::upload(temp_dir.path(), state.host.clone()).command(); @@ -2022,17 +1851,9 @@ async fn is_ok_on_unauthorized() { let mut mock_server_builder = MockServerBuilder::new(); - mock_server_builder.set_get_quarantining_config_handler( - |Json(_): Json| async { - Err::, StatusCode>(StatusCode::UNAUTHORIZED) - }, - ); + mock_server_builder.set_quarantine_status_handler(|| async { StatusCode::UNAUTHORIZED }); - mock_server_builder.set_create_bundle_handler( - |State(_): State, _: Json| async { - Err::, StatusCode>(StatusCode::UNAUTHORIZED) - }, - ); + mock_server_builder.set_create_test_upload_handler(|| async { StatusCode::UNAUTHORIZED }); let state = mock_server_builder.spawn_mock_server().await; let mut command = CommandBuilder::upload(temp_dir.path(), state.host.clone()).command(); @@ -2052,17 +1873,9 @@ async fn is_ok_on_forbidden() { let mut mock_server_builder = MockServerBuilder::new(); - mock_server_builder.set_get_quarantining_config_handler( - |Json(_): Json| async { - Err::, StatusCode>(StatusCode::FORBIDDEN) - }, - ); + mock_server_builder.set_quarantine_status_handler(|| async { StatusCode::FORBIDDEN }); - mock_server_builder.set_create_bundle_handler( - |State(_): State, _: Json| async { - Err::, StatusCode>(StatusCode::FORBIDDEN) - }, - ); + mock_server_builder.set_create_test_upload_handler(|| async { StatusCode::FORBIDDEN }); let state = mock_server_builder.spawn_mock_server().await; let mut command = CommandBuilder::upload(temp_dir.path(), state.host.clone()).command(); @@ -2082,17 +1895,9 @@ async fn is_not_ok_on_bad_request() { let mut mock_server_builder = MockServerBuilder::new(); - mock_server_builder.set_get_quarantining_config_handler( - |Json(_): Json| async { - Err::, StatusCode>(StatusCode::BAD_REQUEST) - }, - ); + mock_server_builder.set_quarantine_status_handler(|| async { StatusCode::BAD_REQUEST }); - mock_server_builder.set_create_bundle_handler( - |State(_): State, _: Json| async { - Err::, StatusCode>(StatusCode::BAD_REQUEST) - }, - ); + mock_server_builder.set_create_test_upload_handler(|| async { StatusCode::BAD_REQUEST }); let state = mock_server_builder.spawn_mock_server().await; let mut command = CommandBuilder::upload(temp_dir.path(), state.host.clone()).command(); @@ -2127,7 +1932,7 @@ async fn telemetry_upload_metrics_on_upload_failure() { async fn handle(_: State, _: Bytes) -> Response { StatusCode::BAD_REQUEST.into_response() } - mock_server_builder.set_create_bundle_handler(handle); + mock_server_builder.set_create_test_upload_handler(handle); let state = mock_server_builder.spawn_mock_server().await; let assert = CommandBuilder::upload(temp_dir.path(), state.host.clone()) @@ -2215,11 +2020,8 @@ async fn telemetry_upload_metrics_quarantine_query_result_failure() { generate_mock_valid_junit_xmls_with_failures(&temp_dir); let mut mock_server_builder = MockServerBuilder::new(); - mock_server_builder.set_get_quarantining_config_handler( - |Json(_): Json| async { - Err::, StatusCode>(StatusCode::INTERNAL_SERVER_ERROR) - }, - ); + mock_server_builder + .set_quarantine_status_handler(|| async { StatusCode::INTERNAL_SERVER_ERROR }); let state = mock_server_builder.spawn_mock_server().await; CommandBuilder::upload(temp_dir.path(), state.host.clone()) @@ -2305,7 +2107,7 @@ async fn telemetry_does_not_impact_return() { let requests = state.requests.lock().unwrap().clone(); assert_eq!(requests.len(), 2); - assert_matches!(requests[0], RequestPayload::CreateBundleUpload(_)); + assert_matches!(requests[0], RequestPayload::CreateTestUpload(_)); assert_matches!(requests[1], RequestPayload::S3Upload(_)); // HINT: View CLI output with `cargo test -- --nocapture` @@ -2333,12 +2135,12 @@ async fn test_variant_propagation() { assert_matches!( requests_iter.next().unwrap(), - RequestPayload::GetQuarantineConfig(_) + RequestPayload::QuarantineStatus(_) ); assert_matches!( requests_iter.next().unwrap(), - RequestPayload::CreateBundleUpload(_) + RequestPayload::CreateTestUpload(_) ); let tar_extract_directory = @@ -2406,7 +2208,7 @@ async fn test_can_upload_with_uncloned_repo() { assert_matches!( requests_iter.next().unwrap(), - RequestPayload::CreateBundleUpload(_) + RequestPayload::CreateTestUpload(_) ); let tar_extract_directory = @@ -2584,7 +2386,7 @@ async fn test_can_use_manual_overrides_on_cloned_repo() { assert_matches!( requests_iter.next().unwrap(), - RequestPayload::CreateBundleUpload(_) + RequestPayload::CreateTestUpload(_) ); let tar_extract_directory = @@ -2657,13 +2459,13 @@ async fn do_not_quarantines_tests_when_quarantine_disabled_set() { static ref QUARANTINE_CONFIG_RESPONSE: Arc> = Arc::new(Mutex::new(QuarantineConfigResponse::None)); } - mock_server_builder.set_get_quarantining_config_handler( - |Json(get_quarantine_bulk_test_status_request): Json| { - let mut test_ids = get_quarantine_bulk_test_status_request - .test_identifiers - .into_iter() - .map(|t| t.id) - .collect::>(); + mock_server_builder.set_quarantine_status_handler( + |State(state): State, + Path(test_collection_short_id): Path, + headers: HeaderMap, + Json(lookup): Json| { + let lookup = state.record_quarantine_status(headers, test_collection_short_id, lookup); + let mut test_ids = lookup.test_ids(); let quarantine_config_response = *QUARANTINE_CONFIG_RESPONSE.lock().unwrap(); let quarantined_tests = match quarantine_config_response { QuarantineConfigResponse::Disabled => Vec::new(), @@ -2671,16 +2473,16 @@ async fn do_not_quarantines_tests_when_quarantine_disabled_set() { QuarantineConfigResponse::Some => test_ids.split_off(1), QuarantineConfigResponse::All => test_ids, }; - let is_disabled = matches!( + let quarantine_enabled = !matches!( quarantine_config_response, QuarantineConfigResponse::Disabled ); async move { - Json(GetQuarantineConfigResponse { - is_disabled, + Json(quarantine_status( + &lookup, + quarantine_enabled, quarantined_tests, - ..Default::default() - }) + )) } }, ); @@ -2688,27 +2490,13 @@ async fn do_not_quarantines_tests_when_quarantine_disabled_set() { static ref CREATE_BUNDLE_RESPONSE: Arc> = Arc::new(Mutex::new(CreateBundleResponse::Error)); } - mock_server_builder.set_create_bundle_handler( - |State(state): State, _: Json| { + mock_server_builder.set_create_test_upload_handler( + |State(state): State| { let create_bundle_response = *CREATE_BUNDLE_RESPONSE.lock().unwrap(); let result = match create_bundle_response { CreateBundleResponse::Error => Err(String::from("Server is down")), CreateBundleResponse::Success => { - let host = &state.host; - Ok(Json(CreateBundleUploadResponse { - id: String::from("test-bundle-upload-id"), - id_v2: String::from("test-bundle-upload-id-v2"), - url: format!("{host}/s3upload"), - key: String::from("unused"), - test_collection_bundle_meta_id: Some(String::from( - "82c6a6e5-f8ea-4d93-9a26-b8ab6ff8f6bc", - )), - test_collection_bundle_meta_created_at: Some(String::from( - "2026-05-10T12:34:56.000Z", - )), - repo_id: Some(String::from(MOCK_REPO_ID)), - test_collection_id: Some(String::from(MOCK_TEST_COLLECTION_ID)), - })) + Ok((StatusCode::CREATED, Json(created_test_upload(&state.host)))) } }; async { result } @@ -2773,28 +2561,8 @@ async fn still_quarantines_if_upload_to_s3_fails() { generate_mock_valid_junit_xmls(&temp_dir); let mut mock_server_builder = MockServerBuilder::new(); - mock_server_builder.set_s3_upload_handler( - |_: State, _: Json| async { - String::from("test") - //Err::(String::from("Upload is broke")) - }, - ); - mock_server_builder.set_get_quarantining_config_handler( - |Json(get_quarantine_bulk_test_status_request): Json| { - let test_ids = get_quarantine_bulk_test_status_request - .test_identifiers - .into_iter() - .map(|t| t.id) - .collect::>(); - async move { - Json(GetQuarantineConfigResponse { - is_disabled: false, - quarantined_tests: test_ids, - ..Default::default() - }) - } - }, - ); + mock_server_builder.set_s3_upload_handler(|| async { StatusCode::INTERNAL_SERVER_ERROR }); + mock_server_builder.set_quarantine_status_handler(quarantine_all_handler); let state = mock_server_builder.spawn_mock_server().await; let assert = CommandBuilder::upload(temp_dir.path(), state.host.clone()) @@ -2815,27 +2583,10 @@ async fn still_quarantines_if_upload_endpoint_fails() { generate_mock_valid_junit_xmls(&temp_dir); let mut mock_server_builder = MockServerBuilder::new(); - mock_server_builder.set_create_bundle_handler( - |_: State, _: Json| async { - Err::(String::from("Upload is broke")) - }, - ); - mock_server_builder.set_get_quarantining_config_handler( - |Json(get_quarantine_bulk_test_status_request): Json| { - let test_ids = get_quarantine_bulk_test_status_request - .test_identifiers - .into_iter() - .map(|t| t.id) - .collect::>(); - async move { - Json(GetQuarantineConfigResponse { - is_disabled: false, - quarantined_tests: test_ids, - ..Default::default() - }) - } - }, - ); + mock_server_builder.set_create_test_upload_handler(|| async { + Err::(String::from("Upload is broke")) + }); + mock_server_builder.set_quarantine_status_handler(quarantine_all_handler); let state = mock_server_builder.spawn_mock_server().await; let assert = CommandBuilder::upload(temp_dir.path(), state.host.clone()) @@ -2861,22 +2612,7 @@ async fn presents_error_message_if_you_dont_have_permission_for_s3_upload() { StatusCode::UNAUTHORIZED.into_response() } mock_server_builder.set_s3_upload_handler(handle); - mock_server_builder.set_get_quarantining_config_handler( - |Json(get_quarantine_bulk_test_status_request): Json| { - let test_ids = get_quarantine_bulk_test_status_request - .test_identifiers - .into_iter() - .map(|t| t.id) - .collect::>(); - async move { - Json(GetQuarantineConfigResponse { - is_disabled: false, - quarantined_tests: test_ids, - ..Default::default() - }) - } - }, - ); + mock_server_builder.set_quarantine_status_handler(quarantine_all_handler); let state = mock_server_builder.spawn_mock_server().await; let assert = CommandBuilder::upload(temp_dir.path(), state.host.clone()) @@ -2902,23 +2638,8 @@ async fn presents_error_message_if_you_dont_have_permission_for_upload_endpoint( async fn handle(_: State, _: Bytes) -> Response { StatusCode::UNAUTHORIZED.into_response() } - mock_server_builder.set_create_bundle_handler(handle); - mock_server_builder.set_get_quarantining_config_handler( - |Json(get_quarantine_bulk_test_status_request): Json| { - let test_ids = get_quarantine_bulk_test_status_request - .test_identifiers - .into_iter() - .map(|t| t.id) - .collect::>(); - async move { - Json(GetQuarantineConfigResponse { - is_disabled: false, - quarantined_tests: test_ids, - ..Default::default() - }) - } - }, - ); + mock_server_builder.set_create_test_upload_handler(handle); + mock_server_builder.set_quarantine_status_handler(quarantine_all_handler); let state = mock_server_builder.spawn_mock_server().await; let assert = CommandBuilder::upload(temp_dir.path(), state.host.clone()) @@ -3030,11 +2751,8 @@ async fn reports_failures_even_if_cannot_get_quarantine_context() { let mut mock_server_builder = MockServerBuilder::new(); - mock_server_builder.set_get_quarantining_config_handler( - |Json(_): Json| async { - Err::, StatusCode>(StatusCode::INTERNAL_SERVER_ERROR) - }, - ); + mock_server_builder + .set_quarantine_status_handler(|| async { StatusCode::INTERNAL_SERVER_ERROR }); let state = mock_server_builder.spawn_mock_server().await; let mut command = CommandBuilder::upload(temp_dir.path(), state.host.clone()).command(); @@ -3200,7 +2918,7 @@ async fn test_user_supplied_repo_params_precede_github_actions_env_vars() { assert_matches!( requests_iter.next().unwrap(), - RequestPayload::CreateBundleUpload(_) + RequestPayload::CreateTestUpload(_) ); let tar_extract_directory = @@ -3704,14 +3422,16 @@ async fn a_swift_test_xunit_file_reached_twice_is_uploaded_once() { assert_declarations_resolved(cases); } -const V2_UPLOAD_ID: &str = "f47ac10b-58cc-4372-a567-0e02b2c3d479"; -const V2_CREATED_AT: &str = "2026-10-06T16:00:00.000Z"; - -type V2Call = (String, String, Option, Option); -// (route, collection short id, authorization header, x-trunk-token header) per v2 call -type V2Calls = Arc>>; - -fn v2_call(route: &str, id: String, headers: &axum::http::HeaderMap) -> V2Call { +/// The credential headers the last call to `endpoint` sent: `authorization`, `x-trunk-token` and +/// the legacy `x-api-token`. +fn credential_headers( + state: &SharedMockServerState, + endpoint: &str, +) -> (Option, Option, Option) { + let headers = state.request_headers.lock().unwrap(); + let headers = headers + .get(endpoint) + .unwrap_or_else(|| panic!("no call to {endpoint}")); let header = |name| { headers .get(name) @@ -3719,94 +3439,27 @@ fn v2_call(route: &str, id: String, headers: &axum::http::HeaderMap) -> V2Call { .map(String::from) }; ( - String::from(route), - id, header("authorization"), header("x-trunk-token"), + header("x-api-token"), ) } -/// A server whose decision route answers `decision`, and which serves the v2 quarantine lookup -/// and upload, quarantining every failure. -fn v2_mock_server(decision: serde_json::Value) -> (MockServerBuilder, V2Calls) { - use axum::{extract::Path, routing::post}; - use serde_json::{Value, json}; - - const UPLOAD_ID: &str = V2_UPLOAD_ID; - const CREATED_AT: &str = V2_CREATED_AT; - - let v2_calls: V2Calls = Arc::default(); - let mut mock_server_builder = MockServerBuilder::new(); - mock_server_builder.route( - "/v1/metrics/getUploadApiVersion", - post(move || async move { Json(decision) }), - ); - let calls = v2_calls.clone(); - mock_server_builder.route( - "/v2/test-collections/:id/quarantine-status", - post(move |Path(id): Path, headers: axum::http::HeaderMap, Json(body): Json| { - let calls = calls.clone(); - async move { - calls - .lock() - .unwrap() - .push(v2_call("quarantine-status", id, &headers)); - let tests: Vec = body["testIdentifiers"] - .as_array() - .unwrap() - .iter() - .map(|identifier| { - json!({"clientTestId": identifier["id"], "testId": null, "quarantined": true}) - }) - .collect(); - Json(json!({ - "testCollectionUuid": MOCK_TEST_COLLECTION_ID, - "repositoryId": MOCK_REPO_ID, - "quarantineEnabled": true, - "tests": tests, - })) +/// The collection each quarantine lookup and upload named, in order. +fn collection_calls(state: &SharedMockServerState) -> Vec<(&'static str, String)> { + state + .requests + .lock() + .unwrap() + .iter() + .filter_map(|request| match request { + RequestPayload::QuarantineStatus(lookup) => { + Some(("quarantine-status", lookup.test_collection_short_id.clone())) } - }), - ); - let calls = v2_calls.clone(); - mock_server_builder.route( - "/v2/test-collections/:id/uploads", - post( - move |State(state): State, - Path(id): Path, - headers: axum::http::HeaderMap| { - let calls = calls.clone(); - async move { - calls.lock().unwrap().push(v2_call("uploads", id, &headers)); - ( - StatusCode::CREATED, - Json(json!({ - "id": UPLOAD_ID, - "status": "awaitingData", - "uploadUrl": format!("{}/s3upload", state.host), - "expiresAt": "2026-10-06T17:00:00.000Z", - "testCollectionUuid": MOCK_TEST_COLLECTION_ID, - "repositoryId": MOCK_REPO_ID, - "createdAt": CREATED_AT, - })), - ) - } - }, - ), - ); - (mock_server_builder, v2_calls) -} - -fn v2_calls_for(short_id: &str) -> Vec { - ["quarantine-status", "uploads"] - .into_iter() - .map(|route| { - ( - String::from(route), - String::from(short_id), - Some(String::from("Bearer test-token")), - None, - ) + RequestPayload::CreateTestUpload(upload) => { + Some(("uploads", upload.test_collection_short_id.clone())) + } + _ => None, }) .collect() } @@ -3852,32 +3505,15 @@ fn github_pull_request_event(temp_dir: &tempfile::TempDir, head: &str) -> String path.to_string_lossy().into_owned() } -fn assert_no_v1_calls(state: &SharedMockServerState) { - let requests = state.requests.lock().unwrap().clone(); - assert!( - !requests.iter().any(|request| matches!( - request, - RequestPayload::CreateBundleUpload(_) | RequestPayload::GetQuarantineConfig(_) - )), - "a v2 run must not call the v1 routes" - ); -} - -const INFERRED_SHORT_ID: &str = "zQ8nP2mW"; - // NOTE: must be multi threaded to start a mock server #[tokio::test(flavor = "multi_thread")] -async fn upload_on_v2_when_trunk_chooses_it() { +async fn upload_names_the_collection_and_sends_the_token_as_a_bearer() { let temp_dir = tempdir().unwrap(); generate_mock_git_repo(&temp_dir); generate_mock_valid_junit_xmls_with_failures(&temp_dir); - // A run that names its collection ignores any the server infers. - let (mock_server_builder, v2_calls) = v2_mock_server(serde_json::json!({ - "apiVersion": "v2", - "testCollectionShortId": INFERRED_SHORT_ID, - })); - + let mut mock_server_builder = MockServerBuilder::new(); + mock_server_builder.set_quarantine_status_handler(quarantine_all_handler); let state = mock_server_builder.spawn_mock_server().await; CommandBuilder::upload(temp_dir.path(), state.host.clone()) @@ -3886,11 +3522,22 @@ async fn upload_on_v2_when_trunk_chooses_it() { .arg("aB3xY9kQ") .assert() // every failure is quarantined - .success() - .stderr(predicate::str::contains("inferred from the repository").not()); + .success(); - assert_eq!(v2_calls.lock().unwrap().clone(), v2_calls_for("aB3xY9kQ")); - assert_no_v1_calls(&state); + assert_eq!( + collection_calls(&state), + vec![ + ("quarantine-status", String::from("aB3xY9kQ")), + ("uploads", String::from("aB3xY9kQ")), + ] + ); + for endpoint in ["quarantineStatus", "uploads"] { + assert_eq!( + credential_headers(&state, endpoint), + (Some(String::from("Bearer test-token")), None, None), + "{endpoint}" + ); + } let requests = state.requests.lock().unwrap().clone(); let tar_extract_directory = requests .iter() @@ -3908,13 +3555,13 @@ async fn upload_on_v2_when_trunk_chooses_it() { bundle_meta.base_props.test_collection, ), ( - V2_UPLOAD_ID, + MOCK_UPLOAD_ID, // base64 of {"id":…,"createdAt":…,"repoId":…}, which ingestion decodes - "eyJpZCI6ImY0N2FjMTBiLTU4Y2MtNDM3Mi1hNTY3LTBlMDJiMmMzZDQ3OSIsImNyZWF0ZWRBdCI6IjIwMjYtMTAtMDZUMTY6MDA6MDAuMDAwWiIsInJlcG9JZCI6IjdhMWYwZTNkLTJiNGMtNGQ1ZS04ZjkwLTEyMzQ1Njc4OWFiYyJ9", + "eyJpZCI6IjgyYzZhNmU1LWY4ZWEtNGQ5My05YTI2LWI4YWI2ZmY4ZjZiYyIsImNyZWF0ZWRBdCI6IjIwMjYtMDUtMTBUMTI6MzQ6NTYuMDAwWiIsInJlcG9JZCI6IjdhMWYwZTNkLTJiNGMtNGQ1ZS04ZjkwLTEyMzQ1Njc4OWFiYyJ9", Some(TestCollectionProps { short_id: String::from("aB3xY9kQ"), - bundle_meta_id: String::from(V2_UPLOAD_ID), - bundle_meta_created_at: String::from(V2_CREATED_AT), + bundle_meta_id: String::from(MOCK_UPLOAD_ID), + bundle_meta_created_at: String::from(MOCK_UPLOAD_CREATED_AT), }), ) ); @@ -3924,34 +3571,15 @@ const GITHUB_ACTIONS_LOGIN: &str = r#"{"jwt":"trunk-jwt","expiresAt":"2026-11-06 const FORK_HEAD_SHA: &str = "0123456789abcdef0123456789abcdef01234567"; const GITHUB_TOKEN: &str = "ghs_job-token"; -fn assert_uploaded_on_the_v1_collection_lane(state: &SharedMockServerState) { - let requests = state.requests.lock().unwrap().clone(); - assert!( - requests - .iter() - .any(|request| matches!(request, RequestPayload::CreateBundleUpload(_))), - "the run must have uploaded on v1" - ); - let headers = state.request_headers.lock().unwrap(); - let upload_headers = headers.get("createBundleUpload").unwrap(); - assert_eq!( - upload_headers - .get("x-trunk-allow-forked-pr-uploads") - .and_then(|value| value.to_str().ok()), - Some("true") - ); - assert!(upload_headers.get("x-api-token").is_none()); -} - // NOTE: must be multi threaded to start a mock server #[tokio::test(flavor = "multi_thread")] -async fn upload_a_fork_pr_run_on_v2_with_its_github_actions_login() { +async fn upload_a_fork_pr_run_with_its_github_actions_login() { let temp_dir = tempdir().unwrap(); generate_mock_git_repo(&temp_dir); generate_mock_valid_junit_xmls_with_failures(&temp_dir); - let (mut mock_server_builder, v2_calls) = - v2_mock_server(serde_json::json!({"apiVersion": "v2"})); + let mut mock_server_builder = MockServerBuilder::new(); + mock_server_builder.set_quarantine_status_handler(quarantine_all_handler); let logins = github_actions_login_route( &mut mock_server_builder, (StatusCode::OK, GITHUB_ACTIONS_LOGIN), @@ -3984,18 +3612,19 @@ async fn upload_a_fork_pr_run_on_v2_with_its_github_actions_login() { })] ); assert_eq!( - v2_calls.lock().unwrap().clone(), - ["quarantine-status", "uploads"] - .into_iter() - .map(|route| ( - String::from(route), - String::from("aB3xY9kQ"), - None, - Some(String::from("trunk-jwt")), - )) - .collect::>() + collection_calls(&state), + vec![ + ("quarantine-status", String::from("aB3xY9kQ")), + ("uploads", String::from("aB3xY9kQ")), + ] ); - assert_no_v1_calls(&state); + for endpoint in ["quarantineStatus", "uploads"] { + assert_eq!( + credential_headers(&state, endpoint), + (None, Some(String::from("trunk-jwt")), None), + "{endpoint}" + ); + } } #[rstest::rstest] @@ -4012,7 +3641,7 @@ async fn upload_a_fork_pr_run_on_v2_with_its_github_actions_login() { #[case::github_down(StatusCode::SERVICE_UNAVAILABLE, "", "503 Service Unavailable")] #[case::rate_limited(StatusCode::TOO_MANY_REQUESTS, "", "429 Too Many Requests")] #[tokio::test(flavor = "multi_thread")] -async fn upload_a_fork_pr_run_on_v1_when_its_swap_is_refused( +async fn upload_a_fork_pr_run_whose_login_is_refused_uploads_nothing( #[case] status: StatusCode, #[case] body: &'static str, #[case] reason: &str, @@ -4021,8 +3650,7 @@ async fn upload_a_fork_pr_run_on_v1_when_its_swap_is_refused( generate_mock_git_repo(&temp_dir); generate_mock_valid_junit_xmls(&temp_dir); - let (mut mock_server_builder, v2_calls) = - v2_mock_server(serde_json::json!({"apiVersion": "v2"})); + let mut mock_server_builder = MockServerBuilder::new(); let logins = github_actions_login_route(&mut mock_server_builder, (status, body)); let state = mock_server_builder.spawn_mock_server().await; @@ -4032,28 +3660,29 @@ async fn upload_a_fork_pr_run_on_v1_when_its_swap_is_refused( .command() .arg("--test-collection-id") .arg("aB3xY9kQ") - .env("GITHUB_EVENT_PATH", github_pull_request_event(&temp_dir, "someone/web")) + .env( + "GITHUB_EVENT_PATH", + github_pull_request_event(&temp_dir, "someone/web"), + ) .env("GITHUB_TOKEN", GITHUB_TOKEN) .assert() .stderr(predicate::str::contains(format!( - "Could not log in to the Trunk API v2 for this fork pull request (Trunk answered {reason}); uploading with Trunk API v1" + "Could not log in to Trunk for this fork pull request (Trunk answered {reason})" ))) .stderr(predicate::str::contains(GITHUB_TOKEN).not()); assert_eq!(logins.lock().unwrap().len(), 1); - assert!(v2_calls.lock().unwrap().is_empty()); - assert_uploaded_on_the_v1_collection_lane(&state); + assert!(collection_calls(&state).is_empty()); } // NOTE: must be multi threaded to start a mock server #[tokio::test(flavor = "multi_thread")] -async fn upload_a_fork_pr_run_without_github_token_on_v1() { +async fn upload_a_fork_pr_run_without_github_token_uploads_nothing() { let temp_dir = tempdir().unwrap(); generate_mock_git_repo(&temp_dir); generate_mock_valid_junit_xmls(&temp_dir); - let (mut mock_server_builder, v2_calls) = - v2_mock_server(serde_json::json!({"apiVersion": "v2"})); + let mut mock_server_builder = MockServerBuilder::new(); let logins = github_actions_login_route( &mut mock_server_builder, (StatusCode::OK, GITHUB_ACTIONS_LOGIN), @@ -4075,19 +3704,17 @@ async fn upload_a_fork_pr_run_without_github_token_on_v1() { .stderr(predicate::str::contains("(GITHUB_TOKEN is not set)")); assert!(logins.lock().unwrap().is_empty()); - assert!(v2_calls.lock().unwrap().is_empty()); - assert_uploaded_on_the_v1_collection_lane(&state); + assert!(collection_calls(&state).is_empty()); } // NOTE: must be multi threaded to start a mock server #[tokio::test(flavor = "multi_thread")] -async fn upload_a_credential_free_run_that_is_not_a_fork_pr_on_v1() { +async fn upload_a_credential_free_run_that_is_not_a_fork_pr_uploads_nothing() { let temp_dir = tempdir().unwrap(); generate_mock_git_repo(&temp_dir); generate_mock_valid_junit_xmls(&temp_dir); - let (mut mock_server_builder, v2_calls) = - v2_mock_server(serde_json::json!({"apiVersion": "v2"})); + let mut mock_server_builder = MockServerBuilder::new(); let logins = github_actions_login_route( &mut mock_server_builder, (StatusCode::OK, GITHUB_ACTIONS_LOGIN), @@ -4105,12 +3732,13 @@ async fn upload_a_credential_free_run_that_is_not_a_fork_pr_on_v1() { github_pull_request_event(&temp_dir, "acme/web"), ) .env("GITHUB_TOKEN", GITHUB_TOKEN) - .assert(); + .assert() + .stderr(predicate::str::contains( + "--allow-forked-pr-uploads without a Trunk API token uploads only from a GitHub Actions run of a pull request from a fork", + )); assert!(logins.lock().unwrap().is_empty()); - assert!(v2_calls.lock().unwrap().is_empty()); - // Trunk would have answered v2, which this run has no credential for: it never asked. - assert_uploaded_on_the_v1_collection_lane(&state); + assert!(collection_calls(&state).is_empty()); } // NOTE: must be multi threaded to start a mock server @@ -4120,8 +3748,8 @@ async fn upload_with_a_token_and_allow_forked_pr_uploads_never_swaps() { generate_mock_git_repo(&temp_dir); generate_mock_valid_junit_xmls_with_failures(&temp_dir); - let (mut mock_server_builder, v2_calls) = - v2_mock_server(serde_json::json!({"apiVersion": "v2"})); + let mut mock_server_builder = MockServerBuilder::new(); + mock_server_builder.set_quarantine_status_handler(quarantine_all_handler); let logins = github_actions_login_route( &mut mock_server_builder, (StatusCode::OK, GITHUB_ACTIONS_LOGIN), @@ -4142,7 +3770,10 @@ async fn upload_with_a_token_and_allow_forked_pr_uploads_never_swaps() { .success(); assert!(logins.lock().unwrap().is_empty()); - assert_eq!(v2_calls.lock().unwrap().clone(), v2_calls_for("aB3xY9kQ")); + assert_eq!( + credential_headers(&state, "uploads"), + (Some(String::from("Bearer test-token")), None, None) + ); } // A debug log emitted while setting up the API client, so it only shows up with `--verbose`. diff --git a/test_report/src/report.rs b/test_report/src/report.rs index ebe22721..e5e9d9b2 100644 --- a/test_report/src/report.rs +++ b/test_report/src/report.rs @@ -9,10 +9,7 @@ use std::{ use api::{ client::ApiClient, - message::{ - self, QuarantineResolutionMode, UploadApiCaller, UploadApiDecision, UploadApiVersion, - }, - quarantine::QuarantinedTests, + message::{self, QuarantineResolutionMode}, urls::TestCaseGuidScope, }; use bundle::BundleMetaDebugProps; @@ -34,11 +31,11 @@ use trunk_analytics_cli::{ context::gather_initial_test_context, error_report::ErrorReport, summary_output::{ - ErrorSummary, NoticeKind, Quarantining, UploadCommand, UploadReport, + ErrorSummary, Quarantining, UploadCommand, UploadReport, json::{self, Report}, text::SummaryFileText, }, - upload_command::{RunUploadOptions, run_upload}, + upload_command::{RunUploadOptions, UploadCaller, run_upload}, }; use uuid::Uuid; @@ -48,81 +45,12 @@ pub fn quarantine_disk_cache_dir() -> PathBuf { .unwrap_or_else(|_| env::temp_dir().join(constants::CACHE_DIR)) } -fn upload_api_version_cache_file_path( - org_url_slug: &str, - repo: &RepoUrlParts, - test_collection_short_id: Option<&str>, -) -> PathBuf { - let cache_key = Uuid::new_v5( - &Uuid::NAMESPACE_URL, - format!( - "{org_url_slug}#{}/{}/{}#{}", - repo.host, - repo.owner, - repo.name, - test_collection_short_id.unwrap_or_default() - ) - .as_bytes(), - ); - quarantine_disk_cache_dir().join(format!("upload_api_version_{cache_key}.json")) -} - fn requested_test_collection_short_id() -> Option { env::var(constants::TRUNK_TEST_COLLECTION_ID_ENV) .ok() .filter(|id| !id.trim().is_empty()) } -fn now_secs() -> Option { - SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .ok() - .map(|duration| duration.as_secs()) -} - -fn load_upload_api_version(cache_path: &Path, ttl_secs: u64) -> Option { - let entry: UploadApiVersionDiskCacheEntry = - serde_json::from_reader(fs::File::open(cache_path).ok()?).ok()?; - if now_secs()?.saturating_sub(entry.cached_at_secs) >= ttl_secs { - return None; - } - Some(UploadApiDecision { - version: if entry.v2 { - UploadApiVersion::V2 - } else { - UploadApiVersion::V1 - }, - inferred_test_collection_short_id: entry.inferred_test_collection_short_id, - missing_test_collection: entry.missing_test_collection, - }) -} - -fn save_upload_api_version(cache_path: &Path, upload_api_decision: &UploadApiDecision) { - let Some(cached_at_secs) = now_secs() else { - return; - }; - let entry = UploadApiVersionDiskCacheEntry { - v2: upload_api_decision.version == UploadApiVersion::V2, - inferred_test_collection_short_id: upload_api_decision - .inferred_test_collection_short_id - .clone(), - missing_test_collection: upload_api_decision.missing_test_collection, - cached_at_secs, - }; - let written = cache_path - .parent() - .map(fs::create_dir_all) - .transpose() - .and_then(|_| { - fs::write( - cache_path, - serde_json::to_string(&entry).map_err(std::io::Error::other)?, - ) - }); - if let Err(err) = written { - tracing::warn!("Failed to write upload API version cache file: {:?}", err); - } -} #[cfg(feature = "wasm")] use wasm_bindgen::prelude::wasm_bindgen; @@ -133,7 +61,7 @@ struct QuarantineConfig { quarantined_tests: HashMap, #[serde(default)] quarantine_resolution_mode: QuarantineResolutionMode, - /// Set on v2, where `quarantined_tests` is keyed by test-case GUID rather than test id. + /// What `quarantined_tests`' test-case GUIDs are computed from. `None` when the lookup failed. #[serde(default)] test_case_guid_scope: Option, } @@ -144,17 +72,6 @@ struct QuarantineConfigDiskCacheEntry { cached_at_secs: u64, } -#[derive(Debug, Clone, Serialize, Deserialize)] -struct UploadApiVersionDiskCacheEntry { - v2: bool, - /// The collection Trunk inferred for a run that names none, so every worker uses the same. - #[serde(default, skip_serializing_if = "Option::is_none")] - inferred_test_collection_short_id: Option, - #[serde(default, skip_serializing_if = "std::ops::Not::not")] - missing_test_collection: bool, - cached_at_secs: u64, -} - #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum QuarantineLookupSource { Api, @@ -172,7 +89,6 @@ pub struct TestReport { quarantine_lookup_source: Option, quarantine_lookup_error: Option, quarantined_tests_disk_cache_ttl: Duration, - upload_api_version: Option, codeowners: Option, variant: Option, repo: Option, @@ -351,7 +267,6 @@ impl MutTestReport { quarantine_lookup_source: None, quarantine_lookup_error: None, quarantined_tests_disk_cache_ttl, - upload_api_version: None, codeowners, repo, variant: variant.clone(), @@ -518,18 +433,7 @@ impl MutTestReport { use_uncloned_repo, ); match (api_client, bundle_repo) { - (Ok(mut api_client), Ok(bundle_repo)) => { - let upload_api_decision = self.upload_api_version(&api_client, &bundle_repo.repo); - if upload_api_decision.missing_test_collection { - tracing::debug!( - "Not checking quarantine status: Trunk has no test collection for this run" - ); - return IsQuarantinedResult::default(); - } - api_client.set_upload_api_version( - upload_api_decision - .version_for(requested_test_collection_short_id().as_deref()), - ); + (Ok(api_client), Ok(bundle_repo)) => { let variant = env::var(constants::TRUNK_VARIANT_ENV).unwrap_or_else(|_| "".to_string()); let test_identifier = Test::new( @@ -550,12 +454,11 @@ impl MutTestReport { org_url_slug, ); if let Some(quarantine_config) = self.0.borrow().quarantine_config.as_ref() { - let key = match quarantine_config.test_case_guid_scope.as_ref() { - Some(scope) => scope - .test_case_guid(&test_identifier.id) - .map(|guid| guid.to_string()), - None => Some(test_identifier.id.clone()), - }; + let key = quarantine_config + .test_case_guid_scope + .as_ref() + .and_then(|scope| scope.test_case_guid(&test_identifier.id)) + .map(|guid| guid.to_string()); return IsQuarantinedResult { test_is_quarantined: key.is_some_and(|key| { quarantine_config.quarantined_tests.contains_key(&key) @@ -586,14 +489,8 @@ impl MutTestReport { repo_url: &str, test_collection_short_id: Option<&str>, ) -> PathBuf { - // v1 and v2 entries key their tests differently, so they must not share a file. - let version_suffix = - match self.0.borrow().upload_api_version.as_ref().map(|decision| { - decision.version_for(requested_test_collection_short_id().as_deref()) - }) { - Some(UploadApiVersion::V2) => "#v2", - _ => "", - }; + // `#v2` keeps an entry from 0.17.x's v1 lane, keyed by test id, from ever being read. + let version_suffix = "#v2"; // The collection id is part of the key: quarantine status now resolves // per test collection, so two runs in the same repo with different ids // must not share a cache entry. @@ -719,7 +616,7 @@ impl MutTestReport { return; } - let test_collection_short_id = self.test_collection_short_id(); + let test_collection_short_id = requested_test_collection_short_id(); if let Some(cache_entry) = self.load_quarantine_config_from_disk_cache( &org_url_slug, @@ -756,14 +653,8 @@ impl MutTestReport { let (quarantining_disabled, quarantine_lookup_failed) = match response { Ok(response) => { let is_disabled = response.is_disabled; - let keys = match response.tests { - QuarantinedTests::ByTestId(test_ids) => test_ids, - QuarantinedTests::ByTestCaseGuid { scope, guids } => { - test_case_guid_scope = Some(scope); - guids - } - }; - quarantined_tests.extend(keys.into_iter().map(|key| (key, true))); + test_case_guid_scope = Some(response.scope); + quarantined_tests.extend(response.guids.into_iter().map(|guid| (guid, true))); resolution_mode = response.quarantine_resolution_mode; if let Some(line) = resolution_mode.resolution_log_line(test_collection_short_id.as_deref(), repo) @@ -808,64 +699,6 @@ impl MutTestReport { } } - /// Chosen once per run, so the quarantine lookups and the upload use the same API. Kept on - /// disk with the quarantine cache's TTL, because a runner may check quarantine in several - /// worker processes and upload from another. - fn upload_api_version(&self, api_client: &ApiClient, repo: &RepoUrlParts) -> UploadApiDecision { - if let Some(upload_api_decision) = self.0.borrow().upload_api_version.clone() { - return upload_api_decision; - } - let org_url_slug = self.get_org_url_slug(); - let test_collection_short_id = env::var(constants::TRUNK_TEST_COLLECTION_ID_ENV).ok(); - let cache_path = upload_api_version_cache_file_path( - &org_url_slug, - repo, - test_collection_short_id.as_deref(), - ); - let ttl = self.0.borrow().quarantined_tests_disk_cache_ttl.as_secs(); - if let Some(upload_api_decision) = load_upload_api_version(&cache_path, ttl) { - self.0.borrow_mut().upload_api_version = Some(upload_api_decision.clone()); - return upload_api_decision; - } - let mut upload_args = trunk_analytics_cli::upload_command::UploadArgs::new( - self.get_token(), - org_url_slug, - vec![], - None, - true, - ); - upload_args.test_collection_short_id = test_collection_short_id; - let envs: HashMap = env::vars().collect(); - let request = trunk_analytics_cli::upload_command::upload_api_version_request( - &upload_args, - repo, - &envs, - UploadApiCaller::TestRunnerPlugin, - ); - let upload_api_decision = tokio::runtime::Builder::new_multi_thread() - .enable_all() - .build() - .unwrap() - .block_on(api_client.get_upload_api_version(&request)); - self.0.borrow_mut().upload_api_version = Some(upload_api_decision.clone()); - save_upload_api_version(&cache_path, &upload_api_decision); - upload_api_decision - } - - /// The collection the run's quarantine lookups use: `TRUNK_TEST_COLLECTION_ID`, else the one - /// Trunk inferred from the repository. The upload applies the same rule in `run_upload`. - fn test_collection_short_id(&self) -> Option { - let requested = requested_test_collection_short_id(); - if requested.is_some() { - return requested; - } - self.0 - .borrow() - .upload_api_version - .as_ref() - .and_then(|decision| decision.inferred_for(None).map(String::from)) - } - fn get_org_url_slug(&self) -> String { env::var(constants::TRUNK_ORG_URL_SLUG_ENV).unwrap_or_default() } @@ -1000,22 +833,6 @@ impl MutTestReport { command_stdout: String::from(""), command_stderr: String::from(""), }; - if let Ok(bundle_repo) = BundleRepo::new( - upload_args.repo_root.clone(), - upload_args.repo_url.clone(), - upload_args.repo_head_sha.clone(), - upload_args.repo_head_branch.clone(), - upload_args.repo_head_commit_epoch.clone(), - upload_args.repo_head_author_name.clone(), - upload_args.use_uncloned_repo, - ) && let Ok(api_client) = ApiClient::new( - upload_args.token.clone(), - false, - upload_args.org_url_slug.clone(), - None, - ) { - self.upload_api_version(&api_client, &bundle_repo.repo); - } let result = match gather_initial_test_context(upload_args.clone(), debug_props) { Ok(pre_test_context) => { match tokio::runtime::Builder::new_multi_thread() @@ -1033,19 +850,13 @@ impl MutTestReport { quarantine_resolution_mode_override: Some( self.quarantine_resolution_mode(), ), - upload_api_version: self.0.borrow().upload_api_version.clone(), - caller: UploadApiCaller::TestRunnerPlugin, + caller: UploadCaller::TestRunnerPlugin, ..Default::default() }, )) { Ok(upload_result) => { // On stderr, as `write_summary_file` does: the gem's logs reach the // console only at error level. - for notice in &upload_result.notices { - if notice.kind == NoticeKind::Deprecation { - eprintln!("{}", notice.message); - } - } if let Some(path) = &summary_output_file { let mut report = UploadReport::new(&upload_result, UploadCommand::RSpec); diff --git a/test_report/tests/publish.rs b/test_report/tests/publish.rs index f50f2399..fbae6a54 100644 --- a/test_report/tests/publish.rs +++ b/test_report/tests/publish.rs @@ -11,7 +11,6 @@ use std::{ thread, }; -use api::message::{GetQuarantineConfigRequest, GetQuarantineConfigResponse}; use assert_matches::assert_matches; use axum::{Json, http::StatusCode}; use bundle::{BundleMeta, FileSetType}; @@ -32,7 +31,9 @@ use serial_test::serial; use tempfile::{TempDir, tempdir}; use test_report::report::{MutTestReport, Status}; use test_utils::mock_git_repo::setup_repo_with_commit; -use test_utils::mock_server::{MockServerBuilder, RequestPayload}; +use test_utils::mock_server::{ + MOCK_UPLOAD_ID, MockServerBuilder, RequestPayload, quarantined_test_list, +}; fn generate_mock_codeowners>(directory: T) { const CODEOWNERS: &str = r#" @@ -122,23 +123,18 @@ async fn publish_uploads_test_report() { let requests = state.requests.lock().unwrap().clone(); assert_eq!(requests.len(), 4); let mut requests_iter = requests.iter(); - let get_quarantine_config_request = assert_matches!(&requests_iter.next().unwrap(), RequestPayload::GetQuarantineConfig(d) => d); - assert_eq!(get_quarantine_config_request.org_url_slug, "test-org"); + let all_quarantined_tests_request = assert_matches!(&requests_iter.next().unwrap(), RequestPayload::AllQuarantinedTests(d) => d); assert_eq!( - get_quarantine_config_request.repo, - RepoUrlParts { + all_quarantined_tests_request.repo, + Some(RepoUrlParts { host: "github.com".into(), owner: "trunk-io".into(), name: "analytics-cli".into() - } - ); - assert_eq!(get_quarantine_config_request.test_identifiers, vec![]); - assert_eq!( - get_quarantine_config_request.remote_urls, - vec!["https://github.com/trunk-io/analytics-cli.git"] + }) ); - // validate we only send one get quarantine config request - assert_matches!(&requests_iter.next().unwrap(), RequestPayload::CreateBundleUpload(d) => d); + assert_eq!(all_quarantined_tests_request.cursor, None); + // validate we only send one quarantine lookup + assert_matches!(&requests_iter.next().unwrap(), RequestPayload::CreateTestUpload(d) => d); let tar_extract_directory = assert_matches!(&requests_iter.next().unwrap(), RequestPayload::S3Upload(d) => d); let file = fs::File::open(tar_extract_directory.join("meta.json")).unwrap(); @@ -166,7 +162,7 @@ async fn publish_uploads_test_report() { base_props.repo.repo_head_author_email, "your.email@example.com" ); - assert_eq!(base_props.bundle_upload_id, "test-bundle-upload-id"); + assert_eq!(base_props.bundle_upload_id, MOCK_UPLOAD_ID); assert_eq!(base_props.tags, &[]); assert_eq!(base_props.file_sets.len(), 1); assert_eq!(base_props.envs.get("CI"), Some(&String::from("1"))); @@ -454,11 +450,11 @@ async fn publish_environment_variable_overrides() { thread_join_handle.join().unwrap(); let requests = state.requests.lock().unwrap().clone(); - // Should have: CreateBundleUpload, S3Upload, TelemetryUploadMetrics + // Should have: CreateTestUpload, S3Upload, TelemetryUploadMetrics assert!(requests.len() >= 2, "Expected at least 2 requests"); let mut requests_iter = requests.iter(); - assert_matches!(&requests_iter.next().unwrap(), RequestPayload::CreateBundleUpload(d) => d); + assert_matches!(&requests_iter.next().unwrap(), RequestPayload::CreateTestUpload(d) => d); let tar_extract_directory = assert_matches!(&requests_iter.next().unwrap(), RequestPayload::S3Upload(d) => d); @@ -588,7 +584,7 @@ async fn publish_variant_priority_constructor_over_env() { let mut requests_iter = requests.iter(); assert_matches!( &requests_iter.next().unwrap(), - RequestPayload::CreateBundleUpload(_) + RequestPayload::CreateTestUpload(_) ); assert_matches!(&requests_iter.next().unwrap(), RequestPayload::S3Upload(_)); @@ -705,11 +701,7 @@ async fn publish_writes_a_failed_quarantine_lookup_to_summary_output_file() { let temp_dir = tempdir().unwrap(); let state = { let mut builder = MockServerBuilder::new(); - builder.set_get_quarantining_config_handler( - |Json(_): Json| async { - Err::, StatusCode>(StatusCode::BAD_REQUEST) - }, - ); + builder.set_all_quarantined_tests_handler(|| async { StatusCode::BAD_REQUEST }); builder.spawn_mock_server().await }; let summary_path = set_up_summary_output_file(&temp_dir, &state.host); @@ -728,14 +720,9 @@ async fn publish_writes_that_quarantining_is_disabled_for_the_repo_to_summary_ou let temp_dir = tempdir().unwrap(); let state = { let mut builder = MockServerBuilder::new(); - builder.set_get_quarantining_config_handler( - |Json(_): Json| async { - Json(GetQuarantineConfigResponse { - is_disabled: true, - ..Default::default() - }) - }, - ); + builder.set_all_quarantined_tests_handler(|| async { + Json(quarantined_test_list(false, Vec::::new())) + }); builder.spawn_mock_server().await }; let summary_path = set_up_summary_output_file(&temp_dir, &state.host); diff --git a/test_report/tests/quarantine.rs b/test_report/tests/quarantine.rs index 2cd0e518..d778512e 100644 --- a/test_report/tests/quarantine.rs +++ b/test_report/tests/quarantine.rs @@ -2,8 +2,11 @@ mod common; use std::{env, fs, thread}; -use api::message::{GetQuarantineConfigRequest, GetQuarantineConfigResponse}; -use axum::{Json, extract::State, http::StatusCode}; +use axum::{ + Json, + extract::{Path, Query, State}, + http::{HeaderMap, StatusCode}, +}; use bundle::Test; use common::{clean_up_cache_files, cleanup_env_vars, setup_quarantine_disk_cache_dir}; use constants::{ @@ -18,7 +21,10 @@ use serial_test::serial; use tempfile::tempdir; use test_report::report::MutTestReport; use test_utils::mock_git_repo::setup_repo_with_commit; -use test_utils::mock_server::{MockServerBuilder, RequestPayload, SharedMockServerState}; +use test_utils::mock_server::{ + AllQuarantinedTestsLookup, MockServerBuilder, RequestPayload, SharedMockServerState, + quarantined_test_list, +}; #[tokio::test(flavor = "multi_thread")] #[serial] @@ -107,21 +113,16 @@ async fn quarantine_variant_impacts_quarantining() { // Create a custom mock server handler that returns quarantined tests // We need to return both IDs: one for "without ID" case and one for "with ID" case - use api::message::GetQuarantineConfigResponse; let state = { let mut builder = MockServerBuilder::new(); - let expected_id_v1 = expected_test_id_variant1.clone(); - let expected_id_v1_from_base = expected_test_id_variant1_from_base.clone(); - builder.set_get_quarantining_config_handler( - move |_state: State, - _req: Json| async move { - Json(GetQuarantineConfigResponse { - is_disabled: false, - quarantined_tests: vec![expected_id_v1, expected_id_v1_from_base], - ..Default::default() - }) - }, - ); + let quarantined = [ + expected_test_id_variant1.clone(), + expected_test_id_variant1_from_base.clone(), + ]; + builder.set_all_quarantined_tests_handler(move || { + let quarantined = quarantined.clone(); + async move { Json(quarantined_test_list(true, quarantined)) } + }); builder.spawn_mock_server().await }; @@ -384,27 +385,23 @@ async fn quarantine_disk_cache() { } // test_id_1 and test_id_2 are quarantined - use api::message::GetQuarantineConfigResponse; - let computed_test_id_1_clone = computed_test_id_1.clone(); - let computed_test_id_2_clone = computed_test_id_2.clone(); + let quarantined = [computed_test_id_1.clone(), computed_test_id_2.clone()]; let state = { let mut builder = MockServerBuilder::new(); - builder.set_get_quarantining_config_handler( - move |state: State, - req: Json| async move { - state - .requests - .lock() - .unwrap() - .push(RequestPayload::GetQuarantineConfig(req.0)); - Json(GetQuarantineConfigResponse { - is_disabled: false, - quarantined_tests: vec![ - computed_test_id_1_clone.clone(), - computed_test_id_2_clone.clone(), - ], - ..Default::default() - }) + builder.set_all_quarantined_tests_handler( + move |State(state): State, + Path(test_collection_short_id): Path, + Query(query): Query>, + headers: HeaderMap, + Json(lookup): Json| { + state.record_all_quarantined_tests( + headers, + test_collection_short_id, + query, + lookup, + ); + let quarantined = quarantined.clone(); + async move { Json(quarantined_test_list(true, quarantined)) } }, ); builder.spawn_mock_server().await @@ -467,21 +464,20 @@ async fn quarantine_disk_cache() { let requests = state.requests.lock().unwrap(); let get_quarantine_config_count = requests .iter() - .filter(|req| matches!(req, RequestPayload::GetQuarantineConfig(_))) + .filter(|req| matches!(req, RequestPayload::AllQuarantinedTests(_))) .count(); assert_eq!( get_quarantine_config_count, 1, - "get_quarantine_config should be called only once across two test reports with the same repo" + "all-quarantined-tests should be called only once across two test reports with the same repo" ); let get_quarantine_config_request = requests .iter() .find_map(|req| match req { - RequestPayload::GetQuarantineConfig(req) => Some(req), + RequestPayload::AllQuarantinedTests(req) => Some(req), _ => None, }) .unwrap(); - assert_eq!(get_quarantine_config_request.org_url_slug, "test-org"); - assert_eq!(get_quarantine_config_request.repo, repo_1,); + assert_eq!(get_quarantine_config_request.repo, Some(repo_1)); } // third test report with different repo - should make a new API call due to cache miss @@ -516,21 +512,21 @@ async fn quarantine_disk_cache() { let requests = state.requests.lock().unwrap(); let get_quarantine_config_count = requests .iter() - .filter(|req| matches!(req, RequestPayload::GetQuarantineConfig(_))) + .filter(|req| matches!(req, RequestPayload::AllQuarantinedTests(_))) .count(); assert_eq!( get_quarantine_config_count, 2, - "get_quarantine_config should be called twice: once for test-repo-1 and once for test-repo-2" + "all-quarantined-tests should be called twice: once for test-repo-1 and once for test-repo-2" ); let get_quarantine_config_requests: Vec<_> = requests .iter() .filter_map(|req| match req { - RequestPayload::GetQuarantineConfig(req) => Some(req), + RequestPayload::AllQuarantinedTests(req) => Some(req), _ => None, }) .collect(); assert_eq!(get_quarantine_config_requests.len(), 2); - assert_eq!(get_quarantine_config_requests[1].repo, repo_2,); + assert_eq!(get_quarantine_config_requests[1].repo, Some(repo_2)); } } #[tokio::test(flavor = "multi_thread")] @@ -559,16 +555,9 @@ async fn quarantine_disabled_for_repo() { let state = { let mut builder = MockServerBuilder::new(); - builder.set_get_quarantining_config_handler( - move |_state: State, - _req: Json| async move { - Json(GetQuarantineConfigResponse { - is_disabled: true, - quarantined_tests: vec![], - ..Default::default() - }) - }, - ); + builder.set_all_quarantined_tests_handler(|| async { + Json(quarantined_test_list(false, Vec::::new())) + }); builder.spawn_mock_server().await }; @@ -634,13 +623,7 @@ async fn quarantine_lookup_failed_when_endpoint_fails() { let state = { let mut builder = MockServerBuilder::new(); - builder.set_get_quarantining_config_handler( - |Json(_): Json| async { - Err::, StatusCode>( - StatusCode::INTERNAL_SERVER_ERROR, - ) - }, - ); + builder.set_all_quarantined_tests_handler(|| async { StatusCode::INTERNAL_SERVER_ERROR }); builder.spawn_mock_server().await }; @@ -688,7 +671,7 @@ async fn quarantine_lookup_failed_when_endpoint_fails() { #[tokio::test(flavor = "multi_thread")] #[serial] async fn v2_matches_a_test_by_its_computed_test_id() { - use axum::routing::{get, post}; + use axum::routing::post; use context::meta::id::gen_test_case_guid; use serde_json::json; use uuid::Uuid; @@ -730,13 +713,6 @@ async fn v2_matches_a_test_by_its_computed_test_id() { let state = { let mut builder = MockServerBuilder::new(); - builder.set_get_quarantining_config_handler(|| async { - (StatusCode::INTERNAL_SERVER_ERROR, "v1 must not be called") - }); - builder.route( - "/v1/metrics/getUploadApiVersion", - post(|| async { Json(json!({"apiVersion": "v2"})) }), - ); // The list carries the scope, so a runner makes no status call. builder.route( "/v2/test-collections/:id/quarantine-status", @@ -800,37 +776,12 @@ async fn v2_matches_a_test_by_its_computed_test_id() { #[tokio::test(flavor = "multi_thread")] #[serial] async fn makes_no_calls_without_a_test_collection_id() { - use std::sync::{ - Arc, - atomic::{AtomicUsize, Ordering}, - }; - - use axum::routing::post; - use serde_json::json; - cleanup_env_vars(); let temp_dir = tempdir().unwrap(); setup_quarantine_disk_cache_dir(&temp_dir); let _ = env::set_current_dir(&temp_dir); - let calls = Arc::new(AtomicUsize::new(0)); - let state = { - let mut builder = MockServerBuilder::new(); - let quarantine_calls = calls.clone(); - builder.set_get_quarantining_config_handler(move || { - quarantine_calls.fetch_add(1, Ordering::SeqCst); - async { (StatusCode::INTERNAL_SERVER_ERROR, "must not be called") } - }); - let version_calls = calls.clone(); - builder.route( - "/v1/metrics/getUploadApiVersion", - post(move || { - version_calls.fetch_add(1, Ordering::SeqCst); - async { Json(json!({"apiVersion": "v1"})) } - }), - ); - builder.spawn_mock_server().await - }; + let state = MockServerBuilder::new().spawn_mock_server().await; unsafe { env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, &state.host); @@ -865,70 +816,5 @@ async fn makes_no_calls_without_a_test_collection_id() { assert!(!result.test_is_quarantined); assert!(!result.quarantine_lookup_failed); assert!(!published); - assert_eq!(calls.load(Ordering::SeqCst), 0); assert!(state.requests.lock().unwrap().is_empty()); } - -#[tokio::test(flavor = "multi_thread")] -#[serial] -async fn shares_the_api_version_choice_across_processes() { - use std::sync::{ - Arc, - atomic::{AtomicUsize, Ordering}, - }; - - use axum::routing::post; - use serde_json::json; - - cleanup_env_vars(); - let temp_dir = tempdir().unwrap(); - setup_quarantine_disk_cache_dir(&temp_dir); - let _ = env::set_current_dir(&temp_dir); - - let version_calls = Arc::new(AtomicUsize::new(0)); - let state = { - let mut builder = MockServerBuilder::new(); - let calls = version_calls.clone(); - builder.route( - "/v1/metrics/getUploadApiVersion", - post(move || { - calls.fetch_add(1, Ordering::SeqCst); - async { Json(json!({"apiVersion": "v1"})) } - }), - ); - builder.spawn_mock_server().await - }; - - unsafe { - env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, &state.host); - env::set_var(TRUNK_API_TOKEN_ENV, "test-token"); - env::set_var(TRUNK_ORG_URL_SLUG_ENV, "test-org"); - env::set_var( - TRUNK_REPO_URL_ENV, - "https://github.com/test-org/test-repo.git", - ); - env::set_var(TRUNK_USE_UNCLONED_REPO_ENV, "true"); - env::set_var(TRUNK_REPO_HEAD_SHA_ENV, ""); - env::set_var(TRUNK_REPO_HEAD_BRANCH_ENV, ""); - env::set_var(TRUNK_REPO_HEAD_AUTHOR_NAME_ENV, ""); - env::set_var(TRUNK_QUARANTINED_TESTS_DISK_CACHE_TTL_SECS_ENV, "300"); - env::set_var(constants::TRUNK_TEST_COLLECTION_ID_ENV, "aB3xY9kQ"); - } - - // Each report stands for a worker process: it shares nothing with the other but the disk. - for _ in 0..2 { - thread::spawn(|| { - MutTestReport::new("test".into(), "test-command".into(), None).is_quarantined( - None, - Some("test-name".to_string()), - Some("test-parent-name".to_string()), - None, - None, - ) - }) - .join() - .unwrap(); - } - - assert_eq!(version_calls.load(Ordering::SeqCst), 1); -} diff --git a/test_report/tests/telemetry.rs b/test_report/tests/telemetry.rs index 7299f511..0189c312 100644 --- a/test_report/tests/telemetry.rs +++ b/test_report/tests/telemetry.rs @@ -2,8 +2,7 @@ mod common; use std::{env, thread}; -use api::message::{GetQuarantineConfigRequest, GetQuarantineConfigResponse}; -use axum::{Json, extract::State, http::StatusCode}; +use axum::{Json, http::StatusCode}; use common::{clean_up_cache_files, cleanup_env_vars, setup_quarantine_disk_cache_dir}; use constants::{ TRUNK_API_CLIENT_RETRY_COUNT_ENV, TRUNK_API_TOKEN_ENV, TRUNK_DISABLE_QUARANTINING_ENV, @@ -16,7 +15,7 @@ use serial_test::serial; use tempfile::tempdir; use test_report::report::{MutTestReport, Status}; use test_utils::mock_git_repo::setup_repo_with_commit; -use test_utils::mock_server::{MockServerBuilder, RequestPayload, SharedMockServerState}; +use test_utils::mock_server::{MockServerBuilder, RequestPayload, quarantined_test_list}; fn assert_telemetry_quarantine_query_result(requests: &[RequestPayload], expected: &str) { let quarantine_query_result = requests @@ -171,7 +170,7 @@ async fn telemetry_query_result_cached_on_publish() { let requests = state.requests.lock().unwrap().clone(); let get_quarantine_config_count = requests .iter() - .filter(|req| matches!(req, RequestPayload::GetQuarantineConfig(_))) + .filter(|req| matches!(req, RequestPayload::AllQuarantinedTests(_))) .count(); assert_eq!( get_quarantine_config_count, 1, @@ -194,15 +193,9 @@ async fn telemetry_query_result_disabled_on_publish() { let state = { let mut builder = MockServerBuilder::new(); - builder.set_get_quarantining_config_handler( - |_state: State, _req: Json| async move { - Json(GetQuarantineConfigResponse { - is_disabled: true, - quarantined_tests: vec![], - ..Default::default() - }) - }, - ); + builder.set_all_quarantined_tests_handler(|| async { + Json(quarantined_test_list(false, Vec::::new())) + }); builder.spawn_mock_server().await }; @@ -272,13 +265,7 @@ async fn telemetry_query_result_failure_on_publish() { let state = { let mut builder = MockServerBuilder::new(); - builder.set_get_quarantining_config_handler( - |Json(_): Json| async { - Err::, StatusCode>( - StatusCode::INTERNAL_SERVER_ERROR, - ) - }, - ); + builder.set_all_quarantined_tests_handler(|| async { StatusCode::INTERNAL_SERVER_ERROR }); builder.spawn_mock_server().await }; diff --git a/test_utils/Cargo.toml b/test_utils/Cargo.toml index 8637a56f..b49914f2 100644 --- a/test_utils/Cargo.toml +++ b/test_utils/Cargo.toml @@ -6,10 +6,12 @@ edition = "2024" [dependencies] anyhow = "1.0.44" api = { path = "../api" } +context = { path = "../context" } axum = { version = "0.7.5", features = ["macros"] } git2 = "0.20.4" proto = { version = "0.0.0", path = "../proto" } sentry = "=0.36.0" +serde = { version = "1.0.215", default-features = false, features = ["derive"] } serde_json = "1.0.133" tar = { version = "0.4.46", default-features = false } tempfile = "3.2.0" diff --git a/test_utils/src/mock_server.rs b/test_utils/src/mock_server.rs index e626a390..5b466ab6 100644 --- a/test_utils/src/mock_server.rs +++ b/test_utils/src/mock_server.rs @@ -1,5 +1,5 @@ use std::{ - collections::HashMap, + collections::{HashMap, HashSet}, fs, io::Read, path::PathBuf, @@ -7,28 +7,37 @@ use std::{ }; use api::message::{ - CreateBundleUploadRequest, CreateBundleUploadResponse, GetQuarantineConfigRequest, - GetQuarantineConfigResponse, + CreatedTestUpload, QuarantineStatus, QuarantineStatusTest, QuarantinedTest, QuarantinedTestList, }; +use api::urls::TestCaseGuidScope; use axum::{ Json, Router, body::Bytes, - extract::State, + extract::{Path, Query, State}, handler::Handler, http::{HeaderMap, StatusCode}, response::{IntoResponse, Response}, routing::{MethodRouter, any, post, put}, }; +use context::repo::RepoUrlParts; +use serde::Deserialize; use tempfile::tempdir; use tokio::{net::TcpListener, spawn}; pub const MOCK_REPO_ID: &str = "7a1f0e3d-2b4c-4d5e-8f90-123456789abc"; pub const MOCK_TEST_COLLECTION_ID: &str = "018f6d3a-6f2e-4c4a-9b1e-2f3a4b5c6d7e"; +pub const MOCK_UPLOAD_ID: &str = "82c6a6e5-f8ea-4d93-9a26-b8ab6ff8f6bc"; +pub const MOCK_UPLOAD_CREATED_AT: &str = "2026-05-10T12:34:56.000Z"; + +pub const UPLOADS_ROUTE: &str = "/v2/test-collections/:id/uploads"; +pub const QUARANTINE_STATUS_ROUTE: &str = "/v2/test-collections/:id/quarantine-status"; +pub const ALL_QUARANTINED_TESTS_ROUTE: &str = "/v2/test-collections/:id/all-quarantined-tests"; #[derive(Debug, Clone, PartialEq)] pub enum RequestPayload { - CreateBundleUpload(CreateBundleUploadRequest), - GetQuarantineConfig(GetQuarantineConfigRequest), + CreateTestUpload(CreateTestUpload), + QuarantineStatus(QuarantineStatusLookup), + AllQuarantinedTests(AllQuarantinedTestsLookup), S3Upload(PathBuf), TelemetryUploadMetrics(TelemetryUploadMetrics), } @@ -72,6 +81,131 @@ impl DurationObservation { } } +/// `POST /v2/test-collections/{id}/uploads`, with the collection from its path. +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct CreateTestUpload { + #[serde(skip)] + pub test_collection_short_id: String, + pub repo: Option, + pub remote_urls: Vec, + pub client_version: String, + #[serde(default)] + pub external_id: Option, +} + +/// `POST /v2/test-collections/{id}/quarantine-status`, with the collection from its path. +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct QuarantineStatusLookup { + #[serde(skip)] + pub test_collection_short_id: String, + pub repo: Option, + pub test_identifiers: Vec, +} + +impl QuarantineStatusLookup { + pub fn test_ids(&self) -> Vec { + self.test_identifiers + .iter() + .map(|identifier| identifier.id.clone()) + .collect() + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +pub struct QuarantineStatusIdentifier { + pub id: String, +} + +/// `POST /v2/test-collections/{id}/all-quarantined-tests`, with the collection from its path and +/// the cursor from its query. +#[derive(Debug, Clone, PartialEq, Eq, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct AllQuarantinedTestsLookup { + #[serde(skip)] + pub test_collection_short_id: String, + #[serde(skip)] + pub cursor: Option, + pub repo: Option, +} + +/// The scope every mock response names, which a test runner's GUIDs are computed from. +pub fn mock_test_case_guid_scope() -> TestCaseGuidScope { + TestCaseGuidScope { + test_collection_id: String::from(MOCK_TEST_COLLECTION_ID), + repo_id: String::from(MOCK_REPO_ID), + } +} + +/// The GUID the mock scope gives a test id, as `all-quarantined-tests` lists it. +pub fn mock_test_case_guid(test_id: &str) -> String { + mock_test_case_guid_scope() + .test_case_guid(test_id) + .expect("a test id is a UUID") + .to_string() +} + +pub fn created_test_upload(host: &str) -> CreatedTestUpload { + CreatedTestUpload { + id: String::from(MOCK_UPLOAD_ID), + upload_url: format!("{host}/s3upload"), + test_collection_uuid: String::from(MOCK_TEST_COLLECTION_ID), + repository_id: String::from(MOCK_REPO_ID), + created_at: String::from(MOCK_UPLOAD_CREATED_AT), + } +} + +/// A `quarantine-status` answer naming each looked-up test, quarantined when it is in +/// `quarantined_test_ids`. +pub fn quarantine_status( + lookup: &QuarantineStatusLookup, + quarantine_enabled: bool, + quarantined_test_ids: I, +) -> QuarantineStatus +where + I: IntoIterator, + S: Into, +{ + let quarantined: HashSet = quarantined_test_ids.into_iter().map(Into::into).collect(); + QuarantineStatus { + test_collection_uuid: String::from(MOCK_TEST_COLLECTION_ID), + repository_id: String::from(MOCK_REPO_ID), + quarantine_enabled, + tests: lookup + .test_ids() + .into_iter() + .map(|id| QuarantineStatusTest { + quarantined: quarantined.contains(&id), + client_test_id: Some(id), + }) + .collect(), + } +} + +/// A one-page `all-quarantined-tests` answer listing the GUIDs of `quarantined_test_ids`. +pub fn quarantined_test_list( + quarantine_enabled: bool, + quarantined_test_ids: I, +) -> QuarantinedTestList +where + I: IntoIterator, + S: AsRef, +{ + QuarantinedTestList { + data: quarantined_test_ids + .into_iter() + .map(|id| QuarantinedTest { + test_id: mock_test_case_guid(id.as_ref()), + }) + .collect(), + next_cursor: None, + test_collection_uuid: String::from(MOCK_TEST_COLLECTION_ID), + repository_id: String::from(MOCK_REPO_ID), + quarantine_enabled, + } +} + pub struct RespError { pub code: StatusCode, pub msg: String, @@ -86,7 +220,7 @@ impl IntoResponse for RespError { pub struct MockServerState { pub requests: Mutex>, /// Request headers captured per endpoint (keyed by endpoint name, e.g. - /// "createBundleUpload"), so tests can assert on auth headers. + /// "uploads", "quarantineStatus" or "allQuarantinedTests"), so tests can assert on auth headers. pub request_headers: Mutex>, /// Requests to the routes added with `MockServerBuilder::route`, in arrival order. Those /// handlers are the test's own, so each pushes its request with `record_route_request`. @@ -126,8 +260,9 @@ impl MockServerState { #[derive(Debug, Clone)] pub struct MockServerBuilder { - create_bundle_handler: MethodRouter, - get_quarantining_config_handler: MethodRouter, + create_test_upload_handler: MethodRouter, + quarantine_status_handler: MethodRouter, + all_quarantined_tests_handler: MethodRouter, s3_upload_handler: MethodRouter, telemetry_upload_metrics: MethodRouter, routes: Vec<(&'static str, MethodRouter)>, @@ -136,15 +271,16 @@ pub struct MockServerBuilder { impl MockServerBuilder { pub fn new() -> Self { Self { - create_bundle_handler: post(create_bundle_handler), - get_quarantining_config_handler: post(get_quarantining_config_handler), + create_test_upload_handler: post(create_test_upload_handler), + quarantine_status_handler: post(quarantine_status_handler), + all_quarantined_tests_handler: post(all_quarantined_tests_handler), s3_upload_handler: put(s3_upload_handler), telemetry_upload_metrics: post(telemetry_upload_metrics_handler), routes: Vec::new(), } } - /// Serves `path` as well, such as a v2 route, which the builder has no default for. + /// Serves `path` with `method_router`, in place of the builder's default for it if it has one. pub fn route( &mut self, path: &'static str, @@ -153,20 +289,28 @@ impl MockServerBuilder { self.routes.push((path, method_router)); } - pub fn set_create_bundle_handler(&mut self, handler: H) + pub fn set_create_test_upload_handler(&mut self, handler: H) + where + H: Handler, + T: 'static, + { + self.create_test_upload_handler = post(handler); + } + + pub fn set_quarantine_status_handler(&mut self, handler: H) where H: Handler, T: 'static, { - self.create_bundle_handler = post(handler); + self.quarantine_status_handler = post(handler); } - pub fn set_get_quarantining_config_handler(&mut self, handler: H) + pub fn set_all_quarantined_tests_handler(&mut self, handler: H) where H: Handler, T: 'static, { - self.get_quarantining_config_handler = post(handler); + self.all_quarantined_tests_handler = post(handler); } pub fn set_s3_upload_handler(&mut self, handler: H) @@ -196,18 +340,26 @@ impl MockServerBuilder { ..Default::default() }); - let mut app = Router::new() - .route("/v1/metrics/createBundleUpload", self.create_bundle_handler) - .route( - "/v1/metrics/getQuarantineConfig", - self.get_quarantining_config_handler, - ) - .route("/s3upload", self.s3_upload_handler) - .route( + let defaults = [ + (UPLOADS_ROUTE, self.create_test_upload_handler), + (QUARANTINE_STATUS_ROUTE, self.quarantine_status_handler), + ( + ALL_QUARANTINED_TESTS_ROUTE, + self.all_quarantined_tests_handler, + ), + ("/s3upload", self.s3_upload_handler), + ( api::otlp::CLI_TELEMETRY_METRICS_PATH, self.telemetry_upload_metrics, - ); - + ), + ]; + let mut app = Router::new(); + // A route the test serves itself replaces the default for that path. + for (path, method_router) in defaults { + if !self.routes.iter().any(|(route, _)| *route == path) { + app = app.route(path, method_router); + } + } for (path, method_router) in self.routes { app = app.route(path, method_router); } @@ -242,60 +394,125 @@ impl Default for MockServerBuilder { pub type SharedMockServerState = Arc; +impl MockServerState { + fn record(&self, endpoint: &str, headers: HeaderMap, payload: RequestPayload) { + self.request_headers + .lock() + .unwrap() + .insert(String::from(endpoint), headers); + self.requests.lock().unwrap().push(payload); + } + + /// Records a create-upload request the way the default handler does, for a test's own handler. + pub fn record_create_test_upload( + &self, + headers: HeaderMap, + test_collection_short_id: String, + mut request: CreateTestUpload, + ) { + request.test_collection_short_id = test_collection_short_id; + self.record( + "uploads", + headers, + RequestPayload::CreateTestUpload(request), + ); + } + + /// Records a quarantine-status request the way the default handler does, for a test's own + /// handler. + pub fn record_quarantine_status( + &self, + headers: HeaderMap, + test_collection_short_id: String, + mut lookup: QuarantineStatusLookup, + ) -> QuarantineStatusLookup { + lookup.test_collection_short_id = test_collection_short_id; + self.record( + "quarantineStatus", + headers, + RequestPayload::QuarantineStatus(lookup.clone()), + ); + lookup + } + + /// Records an all-quarantined-tests request the way the default handler does, for a test's + /// own handler. + pub fn record_all_quarantined_tests( + &self, + headers: HeaderMap, + test_collection_short_id: String, + query: HashMap, + mut lookup: AllQuarantinedTestsLookup, + ) { + lookup.test_collection_short_id = test_collection_short_id; + lookup.cursor = query.get("cursor").cloned(); + self.record( + "allQuarantinedTests", + headers, + RequestPayload::AllQuarantinedTests(lookup), + ); + } +} + #[axum::debug_handler] -pub async fn create_bundle_handler( +pub async fn create_test_upload_handler( State(state): State, + Path(test_collection_short_id): Path, headers: HeaderMap, - Json(create_bundle_upload_request): Json, -) -> Json { - state - .request_headers - .lock() - .unwrap() - .insert(String::from("createBundleUpload"), headers); - state - .requests - .lock() - .unwrap() - .push(RequestPayload::CreateBundleUpload( - create_bundle_upload_request, - )); - let host = &state.host; - Json(CreateBundleUploadResponse { - id: String::from("test-bundle-upload-id"), - id_v2: String::from("test-bundle-upload-id-v2"), - url: format!("{host}/s3upload"), - key: String::from("unused"), - test_collection_bundle_meta_id: Some(String::from("82c6a6e5-f8ea-4d93-9a26-b8ab6ff8f6bc")), - test_collection_bundle_meta_created_at: Some(String::from("2026-05-10T12:34:56.000Z")), - repo_id: Some(String::from(MOCK_REPO_ID)), - test_collection_id: Some(String::from(MOCK_TEST_COLLECTION_ID)), - }) + Json(request): Json, +) -> (StatusCode, Json) { + state.record_create_test_upload(headers, test_collection_short_id, request); + (StatusCode::CREATED, Json(created_test_upload(&state.host))) } +/// Answers that quarantining is on and no looked-up test is quarantined. #[axum::debug_handler] -pub async fn get_quarantining_config_handler( +pub async fn quarantine_status_handler( State(state): State, + Path(test_collection_short_id): Path, headers: HeaderMap, - Json(get_quarantine_config_request): Json, -) -> Json { - state - .request_headers - .lock() - .unwrap() - .insert(String::from("getQuarantineConfig"), headers); - state - .requests - .lock() - .unwrap() - .push(RequestPayload::GetQuarantineConfig( - get_quarantine_config_request, - )); - Json(GetQuarantineConfigResponse { - is_disabled: false, - quarantined_tests: Vec::new(), - ..Default::default() - }) + Json(lookup): Json, +) -> Json { + let lookup = state.record_quarantine_status(headers, test_collection_short_id, lookup); + Json(quarantine_status(&lookup, true, Vec::::new())) +} + +/// Answers that quarantining is on and every looked-up test is quarantined. +#[axum::debug_handler] +pub async fn quarantine_all_handler( + State(state): State, + Path(test_collection_short_id): Path, + headers: HeaderMap, + Json(lookup): Json, +) -> Json { + let lookup = state.record_quarantine_status(headers, test_collection_short_id, lookup); + let test_ids = lookup.test_ids(); + Json(quarantine_status(&lookup, true, test_ids)) +} + +/// Answers that quarantining is off. +#[axum::debug_handler] +pub async fn quarantining_disabled_handler( + State(state): State, + Path(test_collection_short_id): Path, + headers: HeaderMap, + Json(lookup): Json, +) -> Json { + let lookup = state.record_quarantine_status(headers, test_collection_short_id, lookup); + Json(quarantine_status(&lookup, false, Vec::::new())) +} + +/// Answers that quarantining is on and no test is quarantined. +#[axum::debug_handler] +pub async fn all_quarantined_tests_handler( + State(state): State, + Path(test_collection_short_id): Path, + Query(query): Query>, + headers: HeaderMap, + Json(lookup): Json, +) -> Json { + state.record_all_quarantined_tests(headers, test_collection_short_id, query, lookup); + Json(quarantined_test_list(true, Vec::::new())) } #[axum::debug_handler]