-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: store documents when ingesting a dataset
Ingesting a dataset did not store the source document, of the ingested documents, in storage backend. This change fixes this.
- Loading branch information
Showing
8 changed files
with
119 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#![allow(clippy::unwrap_used)] | ||
|
||
use bytes::BytesMut; | ||
use futures_util::StreamExt; | ||
use std::{ | ||
io::{Cursor, Write}, | ||
time::Instant, | ||
}; | ||
use test_context::test_context; | ||
use test_log::test; | ||
use tracing::instrument; | ||
use trustify_common::id::Id; | ||
use trustify_module_fundamental::sbom::service::SbomService; | ||
use trustify_module_storage::service::StorageBackend; | ||
use trustify_test_context::TrustifyContext; | ||
use zip::write::FileOptions; | ||
|
||
/// Test ingesting a dataset. | ||
#[test_context(TrustifyContext, skip_teardown)] | ||
#[test(tokio::test)] | ||
#[instrument] | ||
async fn ingest(ctx: TrustifyContext) -> anyhow::Result<()> { | ||
let service = SbomService::new(ctx.db.clone()); | ||
let storage = &ctx.storage; | ||
|
||
let start = Instant::now(); | ||
|
||
// create dataset ad-hoc | ||
|
||
let base = ctx.absolute_path("../datasets/ds3")?; | ||
let mut data = vec![]; | ||
let mut dataset = zip::write::ZipWriter::new(Cursor::new(&mut data)); | ||
for entry in walkdir::WalkDir::new(&base) { | ||
let entry = entry?; | ||
let Ok(path) = entry.path().strip_prefix(&base) else { | ||
continue; | ||
}; | ||
|
||
if entry.file_type().is_file() { | ||
dataset.start_file_from_path(path, FileOptions::<()>::default())?; | ||
dataset.write_all(&(std::fs::read(entry.path())?))?; | ||
} else if entry.file_type().is_dir() { | ||
dataset.add_directory_from_path(path, FileOptions::<()>::default())?; | ||
} | ||
} | ||
dataset.finish()?; | ||
|
||
// ingest | ||
|
||
let result = ctx.ingestor.ingest_dataset(&data, ()).await?; | ||
|
||
let ingest_time = start.elapsed(); | ||
|
||
// check ingest results | ||
|
||
log::info!("ingest: {}", humantime::Duration::from(ingest_time)); | ||
|
||
assert!(result.warnings.is_empty()); | ||
assert_eq!(result.files.len(), 64); | ||
|
||
// get a document | ||
|
||
let sbom = &result.files["spdx/quarkus-bom-2.13.8.Final-redhat-00004.json.bz2"]; | ||
assert!(matches!(sbom.id, Id::Uuid(_))); | ||
|
||
let sbom_summary = service.fetch_sbom_summary(sbom.id.clone(), ()).await?; | ||
assert!(sbom_summary.is_some()); | ||
let sbom_summary = sbom_summary.unwrap(); | ||
assert_eq!(sbom_summary.head.name, "quarkus-bom"); | ||
|
||
// test source document | ||
|
||
let stream = storage | ||
.retrieve(sbom_summary.head.hashes.try_into()?) | ||
.await?; | ||
assert!(stream.is_some()); | ||
let mut stream = stream.unwrap(); | ||
let mut content = BytesMut::new(); | ||
while let Some(data) = stream.next().await { | ||
content.extend(&data?); | ||
} | ||
|
||
assert_eq!(content.len(), 1174356); | ||
|
||
// done | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters