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 15, 2023
1 parent 3181b31 commit 19d5b12
Showing 1 changed file with 47 additions and 5 deletions.
52 changes: 47 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,14 @@ impl Blob {
Ok(())
}

fn get_meta_compressor(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 @@ -155,11 +163,7 @@ 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_meta_compressor(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 +264,41 @@ impl Blob {
Ok(())
}
}

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

#[test]
fn test_get_meta_compressor() {
let mut ctx = BuildContext::default();
let conversion_types = vec![
ConversionType::DirectoryToRafs,
ConversionType::DirectoryToStargz,
ConversionType::DirectoryToTargz,
ConversionType::EStargzToRafs,
ConversionType::EStargzToRef,
ConversionType::EStargzIndexToRef,
ConversionType::TargzToRafs,
ConversionType::TargzToStargz,
ConversionType::TargzToRef,
ConversionType::TarToStargz,
ConversionType::TarToRafs,
ConversionType::TarToRef,
];

for c_type in conversion_types {
ctx = BuildContext {
conversion_type: c_type,
..ctx
};

let compressor = Blob::get_meta_compressor(&ctx);
if ctx.conversion_type.is_to_ref() {
assert_eq!(compressor, compress::Algorithm::Zstd);
} else {
assert_eq!(compressor, compress::Algorithm::None);
}
}
}
}

0 comments on commit 19d5b12

Please sign in to comment.