[tests] Pull out common tests on i/o options into reusable modules

- Introduce modules for testing input/output options
- Provide macros for generating test cases
- Hide details of subprocess execution
This commit is contained in:
Ming-Hung Tsai
2021-07-01 22:27:37 +08:00
parent 6660fde3c4
commit 12ef69c31b
13 changed files with 1077 additions and 784 deletions

View File

@@ -1,98 +1,64 @@
use anyhow::Result;
use thinp::version::tools_version;
mod common;
use common::common_args::*;
use common::input_arg::*;
use common::output_option::*;
use common::test_dir::*;
use common::*;
//------------------------------------------
#[test]
fn accepts_v() -> Result<()> {
let stdout = thin_metadata_unpack!("-V").read()?;
assert!(stdout.contains(tools_version()));
Ok(())
}
const USAGE: &str = concat!(
"thin_metadata_unpack ",
include_str!("../VERSION"),
"Unpack a compressed file of thin metadata.\n\
\n\
USAGE:\n \
thin_metadata_unpack -i <DEV> -o <FILE>\n\
\n\
FLAGS:\n \
-h, --help Prints help information\n \
-V, --version Prints version information\n\
\n\
OPTIONS:\n \
-i <DEV> Specify thinp metadata binary device/file\n \
-o <FILE> Specify packed output file"
);
#[test]
fn accepts_version() -> Result<()> {
let stdout = thin_metadata_unpack!("--version").read()?;
assert!(stdout.contains(tools_version()));
Ok(())
}
//------------------------------------------
const USAGE: &str = "thin_metadata_unpack 0.9.0\nUnpack a compressed file of thin metadata.\n\nUSAGE:\n thin_metadata_unpack -i <DEV> -o <FILE>\n\nFLAGS:\n -h, --help Prints help information\n -V, --version Prints version information\n\nOPTIONS:\n -i <DEV> Specify thinp metadata binary device/file\n -o <FILE> Specify packed output file";
test_accepts_help!(THIN_METADATA_UNPACK, USAGE);
test_accepts_version!(THIN_METADATA_UNPACK);
test_rejects_bad_option!(THIN_METADATA_UNPACK);
#[test]
fn accepts_h() -> Result<()> {
let stdout = thin_metadata_unpack!("-h").read()?;
assert_eq!(stdout, USAGE);
Ok(())
}
test_missing_input_option!(THIN_METADATA_PACK);
test_input_file_not_found!(THIN_METADATA_UNPACK, OPTION);
test_corrupted_input_data!(THIN_METADATA_UNPACK, OPTION);
#[test]
fn accepts_help() -> Result<()> {
let stdout = thin_metadata_unpack!("--help").read()?;
assert_eq!(stdout, USAGE);
Ok(())
}
test_missing_output_option!(THIN_METADATA_UNPACK, mk_valid_md);
#[test]
fn rejects_bad_option() -> Result<()> {
let stderr = run_fail(thin_metadata_unpack!("--hedgehogs-only"))?;
assert!(stderr.contains("Found argument \'--hedgehogs-only\'"));
Ok(())
}
//------------------------------------------
#[test]
fn missing_input_file() -> Result<()> {
let mut td = TestDir::new()?;
let md = mk_zeroed_md(&mut td)?;
let stderr = run_fail(thin_metadata_unpack!("-o", &md))?;
assert!(
stderr.contains("error: The following required arguments were not provided:\n -i <DEV>")
);
Ok(())
}
#[test]
fn no_such_input_file() -> Result<()> {
let mut td = TestDir::new()?;
let md = mk_zeroed_md(&mut td)?;
let stderr = run_fail(thin_metadata_unpack!("-i", "no-such-file", "-o", &md))?;
assert!(stderr.contains("Couldn't find input file"));
Ok(())
}
#[test]
fn missing_output_file() -> Result<()> {
let mut td = TestDir::new()?;
let md = mk_zeroed_md(&mut td)?;
let stderr = run_fail(thin_metadata_unpack!("-i", &md))?;
assert!(stderr
.contains("error: The following required arguments were not provided:\n -o <FILE>"));
Ok(())
}
#[test]
fn garbage_input_file() -> Result<()> {
let mut td = TestDir::new()?;
let md = mk_zeroed_md(&mut td)?;
let stderr = run_fail(thin_metadata_unpack!("-i", &md, "-o", "junk"))?;
assert!(stderr.contains("Not a pack file."));
Ok(())
}
// TODO: share with thin_restore/cache_restore/era_restore
#[test]
fn end_to_end() -> Result<()> {
let mut td = TestDir::new()?;
let md_in = mk_valid_md(&mut td)?;
let md_out = mk_zeroed_md(&mut td)?;
thin_metadata_pack!("-i", &md_in, "-o", "meta.pack").run()?;
thin_metadata_unpack!("-i", "meta.pack", "-o", &md_out).run()?;
run_ok(
THIN_METADATA_PACK,
&["-i", md_in.to_str().unwrap(), "-o", "meta.pack"],
)?;
run_ok(
THIN_METADATA_UNPACK,
&["-i", "meta.pack", "-o", md_out.to_str().unwrap()],
)?;
let dump1 = thin_dump!(&md_in).read()?;
let dump2 = thin_dump!(&md_out).read()?;
let dump1 = run_ok(THIN_DUMP, &[md_in.to_str().unwrap()])?;
let dump2 = run_ok(THIN_DUMP, &[md_out.to_str().unwrap()])?;
assert_eq!(dump1, dump2);
Ok(())
}