[tests] Do not perform Path to str conversion for compatibility

This commit is contained in:
Ming-Hung Tsai
2021-07-21 02:11:46 +08:00
parent 16190f0f9a
commit 222b4f0902
13 changed files with 131 additions and 196 deletions

View File

@@ -2,6 +2,7 @@ use anyhow::Result;
use thinp::version::tools_version;
use crate::args;
use crate::common::process::*;
use crate::common::program::*;
@@ -12,7 +13,7 @@ pub fn test_help_short<'a, P>() -> Result<()>
where
P: Program<'a>,
{
let stdout = run_ok(P::path(), &["-h"])?;
let stdout = run_ok(P::path(), args!["-h"])?;
assert_eq!(stdout, P::usage());
Ok(())
}
@@ -21,7 +22,7 @@ pub fn test_help_long<'a, P>() -> Result<()>
where
P: Program<'a>,
{
let stdout = run_ok(P::path(), &["--help"])?;
let stdout = run_ok(P::path(), args!["--help"])?;
assert_eq!(stdout, P::usage());
Ok(())
}
@@ -48,7 +49,7 @@ pub fn test_version_short<'a, P>() -> Result<()>
where
P: Program<'a>,
{
let stdout = run_ok(P::path(), &["-V"])?;
let stdout = run_ok(P::path(), args!["-V"])?;
assert!(stdout.contains(tools_version()));
Ok(())
}
@@ -57,7 +58,7 @@ pub fn test_version_long<'a, P>() -> Result<()>
where
P: Program<'a>,
{
let stdout = run_ok(P::path(), &["--version"])?;
let stdout = run_ok(P::path(), args!["--version"])?;
assert!(stdout.contains(tools_version()));
Ok(())
}
@@ -84,7 +85,7 @@ where
P: Program<'a>,
{
let option = "--hedgehogs-only";
let stderr = run_fail(P::path(), &[option])?;
let stderr = run_fail(P::path(), args![option])?;
assert!(stderr.contains(&P::bad_option_hint(option)));
Ok(())
}

View File

@@ -1,6 +1,9 @@
use anyhow::Result;
use std::ffi::OsStr;
use thinp::file_utils;
use crate::args;
use crate::common::fixture::*;
use crate::common::process::*;
use crate::common::program::*;
@@ -10,38 +13,38 @@ use crate::common::thin_xml_generator::{write_xml, FragmentedS};
//------------------------------------------
// wrappers
type ArgsBuilder = fn(&mut TestDir, &str, &dyn Fn(&[&str]) -> Result<()>) -> Result<()>;
type ArgsBuilder = fn(&mut TestDir, &OsStr, &dyn Fn(&[&OsStr]) -> Result<()>) -> Result<()>;
fn with_output_md_untouched(
td: &mut TestDir,
input: &str,
thunk: &dyn Fn(&[&str]) -> Result<()>,
input: &OsStr,
thunk: &dyn Fn(&[&OsStr]) -> Result<()>,
) -> Result<()> {
let output = mk_zeroed_md(td)?;
ensure_untouched(&output, || {
let args = ["-i", input, "-o", output.to_str().unwrap()];
let args = args!["-i", input, "-o", &output];
thunk(&args)
})
}
fn with_output_superblock_zeroed(
td: &mut TestDir,
input: &str,
thunk: &dyn Fn(&[&str]) -> Result<()>,
input: &OsStr,
thunk: &dyn Fn(&[&OsStr]) -> Result<()>,
) -> Result<()> {
let output = mk_zeroed_md(td)?;
ensure_superblock_zeroed(&output, || {
let args = ["-i", input, "-o", output.to_str().unwrap()];
let args = args!["-i", input, "-o", &output];
thunk(&args)
})
}
fn input_arg_only(
_td: &mut TestDir,
input: &str,
thunk: &dyn Fn(&[&str]) -> Result<()>,
input: &OsStr,
thunk: &dyn Fn(&[&OsStr]) -> Result<()>,
) -> Result<()> {
let args = [input];
let args = args![input];
thunk(&args)
}
@@ -82,7 +85,7 @@ where
let mut td = TestDir::new()?;
let output = mk_zeroed_md(&mut td)?;
ensure_untouched(&output, || {
let args = ["-o", output.to_str().unwrap()];
let args = args!["-o", &output];
let stderr = run_fail(P::path(), &args)?;
assert!(stderr.contains(P::missing_input_arg()));
Ok(())
@@ -106,7 +109,7 @@ where
let mut td = TestDir::new()?;
let wrapper = build_args_fn(P::arg_type())?;
wrapper(&mut td, "no-such-file", &|args: &[&str]| {
wrapper(&mut td, "no-such-file".as_ref(), &|args: &[&OsStr]| {
let stderr = run_fail(P::path(), args)?;
assert!(stderr.contains(P::file_not_found()));
Ok(())
@@ -130,7 +133,7 @@ where
let mut td = TestDir::new()?;
let wrapper = build_args_fn(P::arg_type())?;
wrapper(&mut td, "/tmp", &|args: &[&str]| {
wrapper(&mut td, "/tmp".as_ref(), &|args: &[&OsStr]| {
let stderr = run_fail(P::path(), args)?;
assert!(stderr.contains("Not a block device or regular file"));
Ok(())
@@ -158,7 +161,7 @@ where
duct::cmd!("chmod", "-r", &input).run()?;
let wrapper = build_args_fn(P::arg_type())?;
wrapper(&mut td, input.to_str().unwrap(), &|args: &[&str]| {
wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
let stderr = run_fail(P::path(), args)?;
assert!(stderr.contains("Permission denied"));
Ok(())
@@ -188,7 +191,7 @@ where
file_utils::create_sized_file(&input, 1024)?;
let wrapper = build_args_fn(P::arg_type())?;
wrapper(&mut td, input.to_str().unwrap(), &|args: &[&str]| {
wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
let stderr = run_fail(P::path(), args)?;
assert!(stderr.contains("Metadata device/file too small. Is this binary metadata?"));
Ok(())
@@ -217,7 +220,7 @@ where
write_xml(&input, &mut gen)?;
let wrapper = build_args_fn(P::arg_type())?;
wrapper(&mut td, input.to_str().unwrap(), &|args: &[&str]| {
wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
let stderr = run_fail(P::path(), args)?;
eprintln!("{}", stderr);
let msg = format!(
@@ -250,7 +253,7 @@ where
ArgType::InputArg => input_arg_only,
ArgType::IoOptions => with_output_superblock_zeroed,
};
wrapper(&mut td, input.to_str().unwrap(), &|args: &[&str]| {
wrapper(&mut td, input.as_ref(), &|args: &[&OsStr]| {
let stderr = run_fail(P::path(), args)?;
assert!(stderr.contains(P::corrupted_input()));
Ok(())

View File

@@ -2,6 +2,7 @@ use anyhow::Result;
use thinp::file_utils;
use crate::args;
use crate::common::process::*;
use crate::common::program::*;
use crate::common::test_dir::*;
@@ -15,7 +16,7 @@ where
{
let mut td = TestDir::new()?;
let input = P::mk_valid_input(&mut td)?;
let stderr = run_fail(P::path(), &["-i", input.to_str().unwrap()])?;
let stderr = run_fail(P::path(), args!["-i", &input])?;
assert!(stderr.contains(P::missing_output_arg()));
Ok(())
}
@@ -36,10 +37,7 @@ where
{
let mut td = TestDir::new()?;
let input = P::mk_valid_input(&mut td)?;
let stderr = run_fail(
P::path(),
&["-i", input.to_str().unwrap(), "-o", "no-such-file"],
)?;
let stderr = run_fail(P::path(), args!["-i", &input, "-o", "no-such-file"])?;
assert!(stderr.contains(<P as OutputProgram>::file_not_found()));
Ok(())
}
@@ -60,7 +58,7 @@ where
{
let mut td = TestDir::new()?;
let input = P::mk_valid_input(&mut td)?;
let stderr = run_fail(P::path(), &["-i", input.to_str().unwrap(), "-o", "/tmp"])?;
let stderr = run_fail(P::path(), args!["-i", &input, "-o", "/tmp"])?;
assert!(stderr.contains("Not a block device or regular file"));
Ok(())
}
@@ -86,15 +84,7 @@ where
let _file = file_utils::create_sized_file(&output, 4096);
duct::cmd!("chmod", "-w", &output).run()?;
let stderr = run_fail(
P::path(),
&[
"-i",
input.to_str().unwrap(),
"-o",
output.to_str().unwrap(),
],
)?;
let stderr = run_fail(P::path(), args!["-i", &input, "-o", &output])?;
assert!(stderr.contains("Permission denied"));
Ok(())
}
@@ -123,15 +113,7 @@ where
let output = td.mk_path("meta.bin");
let _file = file_utils::create_sized_file(&output, 4096);
let stderr = run_fail(
P::path(),
&[
"-i",
input.to_str().unwrap(),
"-o",
output.to_str().unwrap(),
],
)?;
let stderr = run_fail(P::path(), args!["-i", &input, "-o", &output])?;
assert!(stderr.contains("Output file too small"));
Ok(())
}

View File

@@ -3,6 +3,17 @@ use std::ffi::{OsStr, OsString};
//------------------------------------------
#[macro_export]
macro_rules! args {
( $( $arg: expr ),* ) => {
{
use std::ffi::OsStr;
let args = [$( OsStr::new($arg) ),*];
args
}
};
}
// Returns stdout. The command must return zero.
pub fn run_ok<S, I>(program: S, args: I) -> Result<String>
where

View File

@@ -4,6 +4,7 @@ use std::path::PathBuf;
use thinp::file_utils;
use thinp::io_engine::*;
use crate::args;
use crate::common::fixture::*;
use crate::common::process::*;
use crate::common::target::*;
@@ -27,7 +28,7 @@ pub fn mk_valid_md(td: &mut TestDir) -> Result<PathBuf> {
write_xml(&xml, &mut gen)?;
let _file = file_utils::create_sized_file(&md, 4096 * 4096);
let args = ["-i", xml.to_str().unwrap(), "-o", md.to_str().unwrap()];
let args = args!["-i", &xml, "-o", &md];
run_ok(THIN_RESTORE, &args)?;
Ok(md)
@@ -38,27 +39,21 @@ pub fn mk_valid_md(td: &mut TestDir) -> Result<PathBuf> {
// FIXME: replace mk_valid_md with this?
pub fn prep_metadata(td: &mut TestDir) -> Result<PathBuf> {
let md = mk_zeroed_md(td)?;
let args = [
"-o",
md.to_str().unwrap(),
"--format",
"--nr-data-blocks",
"102400",
];
let args = args!["-o", &md, "--format", "--nr-data-blocks", "102400"];
run_ok(THIN_GENERATE_METADATA, &args)?;
// Create a 2GB device
let args = ["-o", md.to_str().unwrap(), "--create-thin", "1"];
let args = args!["-o", &md, "--create-thin", "1"];
run_ok(THIN_GENERATE_METADATA, &args)?;
let args = [
let args = args![
"-o",
md.to_str().unwrap(),
&md,
"--dev-id",
"1",
"--size",
"2097152",
"--rw=randwrite",
"--seq-nr=16",
"--seq-nr=16"
];
run_ok(THIN_GENERATE_MAPPINGS, &args)?;
@@ -66,20 +61,14 @@ pub fn prep_metadata(td: &mut TestDir) -> Result<PathBuf> {
let mut snap_id = 2;
for _i in 0..10 {
// take a snapshot
let args = [
"-o",
md.to_str().unwrap(),
"--create-snap",
&snap_id.to_string(),
"--origin",
"1",
];
let snap_id_str = snap_id.to_string();
let args = args!["-o", &md, "--create-snap", &snap_id_str, "--origin", "1"];
run_ok(THIN_GENERATE_METADATA, &args)?;
// partially overwrite the origin (64MB)
let args = [
let args = args![
"-o",
md.to_str().unwrap(),
&md,
"--dev-id",
"1",
"--size",
@@ -87,7 +76,7 @@ pub fn prep_metadata(td: &mut TestDir) -> Result<PathBuf> {
"--io-size",
"131072",
"--rw=randwrite",
"--seq-nr=16",
"--seq-nr=16"
];
run_ok(THIN_GENERATE_MAPPINGS, &args)?;
snap_id += 1;
@@ -97,7 +86,7 @@ pub fn prep_metadata(td: &mut TestDir) -> Result<PathBuf> {
}
pub fn set_needs_check(md: &PathBuf) -> Result<()> {
let args = ["-o", md.to_str().unwrap(), "--set-needs-check"];
let args = args!["-o", &md, "--set-needs-check"];
run_ok(THIN_GENERATE_METADATA, &args)?;
Ok(())
}
@@ -108,16 +97,19 @@ pub fn generate_metadata_leaks(
expected: u32,
actual: u32,
) -> Result<()> {
let args = [
let nr_blocks_str = nr_blocks.to_string();
let expected_str = expected.to_string();
let actual_str = actual.to_string();
let args = args![
"-o",
md.to_str().unwrap(),
&md,
"--create-metadata-leaks",
"--nr-blocks",
&nr_blocks.to_string(),
&nr_blocks_str,
"--expected",
&expected.to_string(),
&expected_str,
"--actual",
&actual.to_string(),
&actual_str
];
run_ok(THIN_GENERATE_DAMAGE, &args)?;