Skip to content

Commit d827350

Browse files
committed
test: create a test for m0002010 using DS4
1 parent f53bb68 commit d827350

File tree

11 files changed

+366
-119
lines changed

11 files changed

+366
-119
lines changed

Cargo.lock

Lines changed: 33 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ actix-web-static-files = "4.0.1"
4242
anyhow = "1.0.72"
4343
async-compression = "0.4.13"
4444
async-recursion = "1"
45+
async-tar = { version = "0.6", default-features = false, features = ["runtime-tokio"] }
4546
async-trait = "0.1.74"
4647
aws-config = { version = "1.8.14", features = ["behavior-version-latest"] }
4748
aws-sdk-s3 = { version = "1.124.0", features = ["behavior-version-latest"] }
@@ -86,6 +87,7 @@ json-merge-patch = "0.0.1"
8687
jsonpath-rust = "1.0.1"
8788
lenient_semver = "0.4.2"
8889
liblzma = "0.4"
90+
lzma-rust2 = "0.16.1"
8991
libz-sys = "*"
9092
log = "0.4.19"
9193
mime = "0.3.17"

common/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ rust-version.workspace = true
99
[dependencies]
1010
actix-web = { workspace = true }
1111
anyhow = { workspace = true }
12+
async-compression = { workspace = true, features = ["gzip", "lzma"] }
1213
bytes = { workspace = true }
1314
bytesize = { workspace = true, features = ["serde"] }
1415
chrono = { workspace = true }
1516
clap = { workspace = true, features = ["derive", "env"] }
1617
cpe = { workspace = true }
1718
deepsize = { workspace = true }
19+
flate2 = { workspace = true }
1820
hex = { workspace = true }
1921
hide = { workspace = true }
2022
human-date-parser = { workspace = true }
2123
humantime = { workspace = true }
2224
itertools = { workspace = true }
2325
lenient_semver = { workspace = true }
2426
log = { workspace = true }
27+
lzma-rust2 = { workspace = true }
2528
native-tls = { workspace = true }
2629
packageurl = { workspace = true }
2730
pem = { workspace = true }

common/db/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ trustify-migration = { workspace = true }
1111
trustify-common = { workspace = true }
1212

1313
anyhow = { workspace = true }
14-
async-compression = { workspace = true, features = ["tokio", "lzma"] }
14+
async-compression = { workspace = true, features = ["tokio", "lzma", "gzip"] }
1515
log = { workspace = true }
1616
postgresql_commands = { workspace = true, features = ["tokio"] }
1717
postgresql_embedded = { workspace = true, features = ["blocking", "tokio"] }

common/db/src/embedded.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
use anyhow::Context;
22
use postgresql_embedded::{PostgreSQL, Settings, VersionReq};
3-
use std::{
4-
path::{Path, PathBuf},
5-
pin::Pin,
6-
};
7-
use tokio::io::{AsyncRead, BufReader};
3+
use std::path::{Path, PathBuf};
84
use tracing::{Instrument, info_span};
9-
use trustify_common::db::Database;
5+
use trustify_common::{db::Database, decompress::decompress_async_read};
106

117
/// Create common default settings for the embedded database
128
pub fn default_settings() -> anyhow::Result<Settings> {
@@ -92,17 +88,7 @@ pub async fn create_for(
9288
Source::Import(path) => {
9389
log::info!("Importing from: {}", path.display());
9490

95-
let source = tokio::fs::File::open(&path).await?;
96-
let source = BufReader::new(source);
97-
98-
let source: Pin<Box<dyn AsyncRead + Send>> = match path
99-
.extension()
100-
.and_then(|ext| ext.to_str())
101-
{
102-
None | Some("sql") => Box::pin(source),
103-
Some("xz") => Box::pin(async_compression::tokio::bufread::LzmaDecoder::new(source)),
104-
Some(ext) => anyhow::bail!("Unsupported file type ({ext})"),
105-
};
91+
let source = decompress_async_read(path).await?;
10692

10793
super::Database::import(&config, source)
10894
.await

common/src/decompress.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
use actix_web::http::header;
22
use anyhow::anyhow;
33
use bytes::Bytes;
4-
use tokio::{runtime::Handle, task::JoinError};
4+
use std::{io::Read, path::Path, pin::Pin};
5+
use tokio::{
6+
io::{AsyncRead, BufReader},
7+
runtime::Handle,
8+
task::JoinError,
9+
};
510
use tracing::instrument;
611
use walker_common::compression::{Compression, DecompressionOptions, Detector};
712

@@ -95,6 +100,36 @@ fn detect(content_type: Option<header::ContentType>, bytes: &[u8]) -> Result<Com
95100
})
96101
}
97102

103+
/// Take a file, return a wrapped [`AsyncRead`], and wrap that with the required compression decoder.
104+
pub async fn decompress_async_read(
105+
path: impl AsRef<Path>,
106+
) -> anyhow::Result<Pin<Box<dyn AsyncRead + Send>>> {
107+
let path = path.as_ref();
108+
let source = tokio::fs::File::open(path).await?;
109+
let source = BufReader::new(source);
110+
111+
Ok(match path.extension().and_then(|ext| ext.to_str()) {
112+
None | Some("sql") => Box::pin(source),
113+
Some("xz") => Box::pin(async_compression::tokio::bufread::LzmaDecoder::new(source)),
114+
Some("gz") => Box::pin(async_compression::tokio::bufread::GzipDecoder::new(source)),
115+
Some(ext) => anyhow::bail!("Unsupported file type ({ext})"),
116+
})
117+
}
118+
119+
/// Take a file, return a wrapped [`Read`], and wrap that with the required compression decoder.
120+
pub fn decompress_read(path: impl AsRef<Path>) -> anyhow::Result<Box<dyn Read + Send>> {
121+
let path = path.as_ref();
122+
let source = std::fs::File::open(path)?;
123+
let source = std::io::BufReader::new(source);
124+
125+
Ok(match path.extension().and_then(|ext| ext.to_str()) {
126+
None | Some("sql") => Box::new(source),
127+
Some("xz") => Box::new(lzma_rust2::XzReader::new(source, false)),
128+
Some("gz") => Box::new(flate2::read::GzDecoder::new(source)),
129+
Some(ext) => anyhow::bail!("Unsupported file type ({ext})"),
130+
})
131+
}
132+
98133
#[cfg(test)]
99134
mod test {
100135
use crate::decompress::decompress_async;

migration/tests/data/m0002010.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use migration::data::{Database, Direction, MigrationWithData, Options, Runner};
44
use sea_orm_migration::MigratorTrait;
55
use test_context::test_context;
66
use test_log::test;
7-
use trustify_test_context::{TrustifyMigrationContext, commit, ctx::DumpId};
7+
use trustify_test_context::{TrustifyMigrationContext, commit, dump};
88

99
commit!(Commit("6d3ea814b4b44fe16ea8f21724dda5abb0fc7932"));
1010

@@ -42,3 +42,24 @@ async fn examples(
4242

4343
Ok(())
4444
}
45+
46+
// A dump created before merging the SBOM CVSS enhancements
47+
dump!(Ds4(
48+
"https://trustify-dumps-ds4.s3.eu-west-1.amazonaws.com/20251104T095645Z",
49+
storage = "dump.tar.gz",
50+
no_digests,
51+
));
52+
53+
/// Test the performance of applying the data migration of `m0002010`.
54+
#[test_context(TrustifyMigrationContext<Ds4>)]
55+
#[test(tokio::test)]
56+
async fn performance(ctx: &TrustifyMigrationContext<Ds4>) -> Result<(), anyhow::Error> {
57+
MigrationWithData::run_with_test(ctx.storage.clone(), Options::default(), async {
58+
MigratorTest::up(&ctx.db, None).await
59+
})
60+
.await?;
61+
62+
// done
63+
64+
Ok(())
65+
}

test-context/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ trustify-module-storage = { workspace = true }
1818
actix-http = { workspace = true }
1919
actix-web = { workspace = true }
2020
anyhow = { workspace = true }
21+
async-tar = { workspace = true }
2122
bytes = { workspace = true }
2223
futures = { workspace = true }
2324
git2 = { workspace = true }
25+
indicatif = { workspace = true }
2426
log = { workspace = true }
2527
peak_alloc = { workspace = true }
2628
postgresql_embedded = { workspace = true }
@@ -38,6 +40,7 @@ tracing = { workspace = true }
3840
tracing-flame = { workspace = true }
3941
tracing-log = { workspace = true }
4042
tracing-subscriber = { workspace = true }
43+
urlencoding = { workspace = true }
4144
utoipa-actix-web = { workspace = true }
4245
walkdir = { workspace = true }
4346
zip = { workspace = true }

0 commit comments

Comments
 (0)