thin-provisioning-tools/tests/cache_dump.rs

120 lines
2.6 KiB
Rust
Raw Normal View History

2021-07-07 22:01:09 +05:30
use anyhow::Result;
2021-09-10 13:53:53 +05:30
use std::fs::OpenOptions;
use std::io::Write;
2021-07-07 22:01:09 +05:30
mod common;
use common::cache::*;
2021-07-07 22:01:09 +05:30
use common::common_args::*;
2021-09-10 13:53:53 +05:30
use common::fixture::*;
2021-07-07 22:01:09 +05:30
use common::input_arg::*;
2021-09-10 13:53:53 +05:30
use common::process::*;
use common::program::*;
use common::target::*;
2021-07-07 22:01:09 +05:30
use common::test_dir::*;
//------------------------------------------
const USAGE: &str = "cache_dump 0.9.0
Dump the cache metadata to stdout in XML format
USAGE:
cache_dump [FLAGS] [OPTIONS] <INPUT>
FLAGS:
-r, --repair Repair the metadata whilst dumping it
-h, --help Prints help information
-V, --version Prints version information
OPTIONS:
-o, --output <FILE> Specify the output file rather than stdout
ARGS:
<INPUT> Specify the input device to dump";
2021-07-07 22:01:09 +05:30
//------------------------------------------
struct CacheDump;
impl<'a> Program<'a> for CacheDump {
fn name() -> &'a str {
"cache_dump"
}
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>,
{
cache_dump_cmd(args)
2021-07-07 22:01:09 +05:30
}
fn usage() -> &'a str {
USAGE
}
fn arg_type() -> ArgType {
ArgType::InputArg
}
fn bad_option_hint(option: &str) -> String {
msg::bad_option_hint(option)
}
}
impl<'a> InputProgram<'a> for CacheDump {
fn mk_valid_input(td: &mut TestDir) -> Result<std::path::PathBuf> {
mk_valid_md(td)
2021-07-07 22:01:09 +05:30
}
fn file_not_found() -> &'a str {
msg::FILE_NOT_FOUND
}
fn missing_input_arg() -> &'a str {
msg::MISSING_INPUT_ARG
}
fn corrupted_input() -> &'a str {
msg::BAD_SUPERBLOCK
}
}
//------------------------------------------
test_accepts_help!(CacheDump);
test_accepts_version!(CacheDump);
test_rejects_bad_option!(CacheDump);
test_missing_input_arg!(CacheDump);
test_input_file_not_found!(CacheDump);
test_input_cannot_be_a_directory!(CacheDump);
test_unreadable_input_file!(CacheDump);
//------------------------------------------
2021-09-10 13:53:53 +05:30
// TODO: share with thin_dump
#[test]
fn dump_restore_cycle() -> Result<()> {
let mut td = TestDir::new()?;
let md = mk_valid_md(&mut td)?;
let output = run_ok_raw(cache_dump_cmd(args![&md]))?;
2021-09-10 13:53:53 +05:30
let xml = td.mk_path("meta.xml");
let mut file = OpenOptions::new()
.read(false)
.write(true)
.create(true)
.open(&xml)?;
file.write_all(&output.stdout[0..])?;
drop(file);
let md2 = mk_zeroed_md(&mut td)?;
run_ok(cache_restore_cmd(args!["-i", &xml, "-o", &md2]))?;
2021-09-10 13:53:53 +05:30
let output2 = run_ok_raw(cache_dump_cmd(args![&md2]))?;
2021-09-10 13:53:53 +05:30
assert_eq!(output.stdout, output2.stdout);
Ok(())
}