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: xwb1136021767 <[email protected]>
  • Loading branch information
xwb1136021767 authored and jiangliu committed Jul 10, 2023
1 parent 1ae9800 commit 0c225ca
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 additions & 5 deletions builder/src/core/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ impl Blob {
Ok(())
}

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

pub(crate) fn dump_meta_data(
ctx: &BuildContext,
blob_ctx: &mut BlobContext,
Expand Down Expand Up @@ -187,11 +195,7 @@ impl Blob {
header.set_separate_blob(true);
};

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 @@ -299,3 +303,45 @@ 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 = BuildContext {
conversion_type: ConversionType::TarToRef,
..ctx
};
let compressor = Blob::get_compression_algorithm_for_meta(&ctx);
assert_eq!(compressor, compress::Algorithm::Zstd);

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

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

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

0 comments on commit 0c225ca

Please sign in to comment.