Skip to content

Commit

Permalink
nydus-image: add unit test for setting default compression algorithm
Browse files Browse the repository at this point in the history
Signed-off-by: Xue Weibin <[email protected]>
  • Loading branch information
xwb1136021767 committed Jul 5, 2023
1 parent a508ddd commit 673c692
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions src/bin/nydus-image/core/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ impl Blob {
Ok(())
}

fn get_compression_algorithm_for_meta(ctx: &BuildContext) -> compress::Algorithm {
let compressor = if ctx.conversion_type.is_to_ref() {
compress::Algorithm::Zstd
} else {
ctx.compressor
};

compressor
}

pub(crate) fn dump_meta_data(
ctx: &BuildContext,
blob_ctx: &mut BlobContext,
Expand Down Expand Up @@ -155,11 +165,8 @@ impl Blob {
header.set_ci_zran(false);
};

let mut compressor = if ctx.conversion_type.is_to_ref() {
compress::Algorithm::Zstd
} else {
ctx.compressor
};
let mut compressor = Self::get_compression_algorithm_for_meta(ctx);

let (compressed_data, compressed) = compress::compress(ci_data, compressor)
.with_context(|| "failed to compress blob chunk info array".to_string())?;
if !compressed {
Expand Down Expand Up @@ -260,3 +267,34 @@ impl Blob {
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;


#[test]
fn test_default_compression_algorithm_for_meta_ci() {
let mut ctx = BuildContext::default();

//TarToRef
ctx.conversion_type = ConversionType::TarToRef;
let compressor = Blob::get_compression_algorithm_for_meta(&ctx);
assert_eq!(compressor, compress::Algorithm::Zstd);

//EStargzIndexToRef
ctx.conversion_type = ConversionType::EStargzIndexToRef;
let compressor = Blob::get_compression_algorithm_for_meta(&ctx);
assert_eq!(compressor, compress::Algorithm::Zstd);

//TargzToRef
ctx.conversion_type = ConversionType::TargzToRef;
let compressor = Blob::get_compression_algorithm_for_meta(&ctx);
assert_eq!(compressor, compress::Algorithm::Zstd);

//TarToRef
ctx.conversion_type = ConversionType::TarToRef;
let compressor = Blob::get_compression_algorithm_for_meta(&ctx);
assert_eq!(compressor, compress::Algorithm::Zstd);
}
}

0 comments on commit 673c692

Please sign in to comment.