From c50e5a9caf61625f522722ad76e59be0609180d7 Mon Sep 17 00:00:00 2001 From: Helio Frota <00hf11@gmail.com> Date: Thu, 28 Nov 2024 09:07:08 -0300 Subject: [PATCH] test: adds the concurrent test to mimic the upload behavior --- .../src/advisory/endpoints/test.rs | 53 ++++++++++++++++++- modules/ingestor/src/graph/organization.rs | 10 +++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/modules/fundamental/src/advisory/endpoints/test.rs b/modules/fundamental/src/advisory/endpoints/test.rs index 1f9149387..4f16f6fa9 100644 --- a/modules/fundamental/src/advisory/endpoints/test.rs +++ b/modules/fundamental/src/advisory/endpoints/test.rs @@ -8,7 +8,8 @@ use hex::ToHex; use jsonpath_rust::JsonPathQuery; use serde_json::{json, Value}; use sha2::{Digest, Sha256}; -use test_context::test_context; +use std::rc::Rc; +use test_context::{futures, test_context}; use test_log::test; use time::OffsetDateTime; use trustify_common::{db::Transactional, hashing::Digests, id::Id, model::PaginatedResults}; @@ -387,6 +388,56 @@ async fn upload_default_csaf_format_multiple(ctx: &TrustifyContext) -> Result<() Ok(()) } +#[test_context(TrustifyContext)] +#[test(actix_web::test)] +#[ignore] +async fn upload_default_csaf_format_multiple_concurrent( + ctx: &TrustifyContext, +) -> Result<(), anyhow::Error> { + let app = Rc::new(caller(ctx).await?); + let files = vec![ + "csaf/cve-2023-0044.json", + "csaf/rhsa-2023_5835.json", + "csaf/rhsa-2024_2776.json", + "csaf/CVE-2023-20862.json", + "csaf/rhsa-2024_2049.json", + "csaf/cve-2023-33201.json", + "csaf/rhsa-2024_2784.json", + "csaf/rhsa-2024_2054.json", + "csaf/rhsa-2024_3351.json", + "csaf/CVE-2024-5154.json", + "csaf/rhsa-2024_2071.json", + "csaf/rhsa-2024_3666.json", + "csaf/RHBA-2024_1440.json", + "csaf/rhsa-2024-2705.json", + ]; + + let uri = "/api/v1/advisory"; + + let tasks = files.into_iter().map(|file| { + let app = app.clone(); + async move { + let payload = document_bytes(file).await?; + let request = TestRequest::post() + .uri(uri) + .set_payload(payload) + .to_request(); + + let result: IngestResult = app.call_and_read_body_json(request).await; + log::debug!("{result:?}"); + assert!(matches!(result.id, Id::Uuid(_))); + Ok::<(), anyhow::Error>(()) + } + }); + + let results = futures::future::join_all(tasks).await; + for result in results { + result?; + } + + Ok(()) +} + #[test_context(TrustifyContext)] #[test(actix_web::test)] async fn upload_osv_format(ctx: &TrustifyContext) -> Result<(), anyhow::Error> { diff --git a/modules/ingestor/src/graph/organization.rs b/modules/ingestor/src/graph/organization.rs index 2c7b6d6e5..e269d30a3 100644 --- a/modules/ingestor/src/graph/organization.rs +++ b/modules/ingestor/src/graph/organization.rs @@ -83,8 +83,14 @@ impl Graph { let mut entity = organization::ActiveModel::from(found.organization); entity.website = Set(information.website); entity.cpe_key = Set(information.cpe_key); - let model = entity.update(&self.connection(&tx)).await?; - Ok(OrganizationContext::new(found.graph, model)) + let model = match entity.update(&self.connection(&tx)).await { + Ok(model) => Ok(model), + Err(e) => { + log::debug!("Failed to update organization: {e}"); + Err(Error::from(e)) + } + }; + Ok(OrganizationContext::new(found.graph, model?)) } else { Ok(found) }