Skip to content

Commit

Permalink
test: add smoke test for "nydus-image inspect"
Browse files Browse the repository at this point in the history
We use the output result of request_mode for verification.

In ./tests/texture/image-inspect/*.result, there stores the output
result of executing cmd
`nydus-image inspect -B ./tests/texture/bootstrap/image_v2.boot -R *`
with `nydus-image` that has not been refactored.

Then we load the result and compare them with result using the
refactored `nydus-image` to do the smoke test.

Signed-off-by: YushuoEdge <[email protected]>
  • Loading branch information
YushuoEdge committed Sep 18, 2022
1 parent c4b51fa commit 117022f
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 0 deletions.
86 changes: 86 additions & 0 deletions tests/inspect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::{fs::File, io::Read};
use nydus_utils::exec;
use serde_json::Value::{self, Null};

pub fn test_image_inspect_cmd(cmd: &str, bootstrap_path: &str) {
let nydus_image = std::env::var("NYDUS_IMAGE").unwrap_or_else(|_| String::from("./target/debug/nydus-image"));

let output = exec(
format!("{} inspect -B {} -R {}", nydus_image, bootstrap_path, cmd).as_str(),
true,
b"",
).unwrap();
let mut expected = String::new();

let mut texture = File::open(format!("./tests/texture/image-inspect/{}.result", cmd)).unwrap();
texture.read_to_string(&mut expected).unwrap();
is_right(cmd, output, expected);
}

fn is_right(cmd: &str, output: String, expected: String) {
match cmd {
"stats" => is_right_stats(output, expected),
"prefetch" => is_right_prefetch(output, expected),
"blobs" => is_right_blobs(output, expected),
_ => println!("This cmd is not supported now!")
}
}

fn is_right_stats(output: String, expected: String) {
let output_value: Value = serde_json::from_str(output.as_str()).unwrap();
let expected_value: Value = serde_json::from_str(expected.as_str()).unwrap();
assert_eq!(output_value["inodes_count".to_string()], expected_value["inodes_count".to_string()]);
}

fn is_right_prefetch(output: String, expected: String) {
let output_value: Vec<Value> = serde_json::from_str(output.as_str()).unwrap();
let expected_value: Vec<Value> = serde_json::from_str(expected.as_str()).unwrap();

assert_eq!(output_value.len(), expected_value.len());
for index in 0..expected_value.len() {
let expected_v = expected_value.get(index).unwrap();
let mut matched = false;
for output_v in output_value.iter() {
if output_v["inode"] == expected_v["inode"] {
assert_eq!(output_v["path"], expected_v["path"]);
matched = true;
break;
}
}
assert!(matched);
}
}

fn is_right_blobs(output: String, expected: String) {
let output_value: Vec<Value> = serde_json::from_str(output.as_str()).unwrap();
let expected_value: Vec<Value> = serde_json::from_str(expected.as_str()).unwrap();

assert_eq!(output_value.len(), expected_value.len());
for index in 0..expected_value.len() {
let expected_v = expected_value.get(index).unwrap();
let mut matched = false;
for output_v in output_value.iter() {
if output_v["blob_id"] == expected_v["blob_id"] {
if expected_v["compressed_size"] == Null {
assert_eq!(0 as i32, output_v["compressed_size"]);
} else {
assert_eq!(expected_v["compressed_size"], output_v["compressed_size"])
}

if expected_v["decompressed_size"] == Null {
assert_eq!(0 as i32, output_v["decompressed_size"]);
} else {
assert_eq!(expected_v["decompressed_size"], output_v["decompressed_size"]);
}

assert_eq!(expected_v["readahead_offset"], output_v["readahead_offset"]);
assert_eq!(expected_v["readahead_size"], output_v["readahead_size"]);

matched = true;
break;
}
}

assert!(matched);
}
}
11 changes: 11 additions & 0 deletions tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use vmm_sys_util::tempdir::TempDir;

mod builder;
mod nydusd;
mod inspect;

use crate::inspect::test_image_inspect_cmd;

const COMPAT_BOOTSTRAPS: [&str; 2] = [
"blake3-lz4_block-non_repeatable",
Expand Down Expand Up @@ -396,3 +399,11 @@ fn test_unpack(work_dir: &Path, version: &str) {

assert_eq!(ret.trim(), expected.trim());
}

#[test]
fn test_image_inspect() {
let bootstrap_path = "./tests/texture/bootstrap/image_v2.boot";
test_image_inspect_cmd("stats", bootstrap_path);
test_image_inspect_cmd("prefetch", bootstrap_path);
test_image_inspect_cmd("blobs", bootstrap_path);
}
128 changes: 128 additions & 0 deletions tests/texture/image-inspect/blobs.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
[
{
"blob_id":"7fe907a0c9c7f35538f23f40baae5f2e8d148a3a6186f0f443f62d04b5e2d731",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"c7f5eac86f051f76ac2e73a04972d3b7c68df2f35f024210176396146528af49",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"69413c426fe9a8ccda13b6fa0ff714d888c658c619d02f1919816c874dd58f3b",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"6c43518f1a9a0ad1bfeece04a47aec7ea7c4eb548b9611d72c7c65be304d12e2",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"c9cab663e59704f082a1f8884b84597fe413c02b513faf29f468e020b8fc4e73",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"fdd69d7b4b1323933ba02f1b5283af3f1725c4438f5e160abe5d8de91450171a",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"556d1d13c5dd752ca8713f77fdbf8cb90d14d2f5c1d16cfdfa82d371c698fe0f",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"5162ec807e5c3d98819913d9453bb72311815126effe9438e7928360694df46f",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"55095f163928ef439f65f3273358ced8f79a275fff83a4d843ebf4f155b145aa",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"16c95369ce1f06d73a71f83dbf1749a3b4779c818132465c3277aca1de64c411",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"8b83283b43ce86cd2b394d1f6a8176fed66223e057fb87de7e57bcd7325a2142",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"c9b22bc8909a271b762a7f4492a3d257eeb01741a105a639a64132f505b316ea",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"cc3c05c66ca02f1e145c842fd13d210ce05a482700ff5e80222957c27c7ec2f0",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"0ef1b08e3b4010d5cd634cf509d0043dc6d0944f643a4fa4348efc65210f0d2b",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"073c6c98265ed3c848f2c856af0eddf5b8ddcb36407d418059452be32d654952",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"b0f406bbb861055ce702c3b4e529ce4d29e504db91db58cf992cbd8c3d260518",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"475690527e1a4e4dac7be65ac66346e5e82597a83aedf4693fdd383a325ad209",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
},
{
"blob_id":"fc877ac9133dd30ba549aabb6a605e37317a8ef7a2f2fd91a2b85e6fd85280a6",
"compressed_size":null,
"decompressed_size":null,
"readahead_offset":0,
"readahead_size":0
}
]
1 change: 1 addition & 0 deletions tests/texture/image-inspect/prefetch.result
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inode":1,"path":"/"}]
1 change: 1 addition & 0 deletions tests/texture/image-inspect/stats.result
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"inodes_count":43082}

0 comments on commit 117022f

Please sign in to comment.