2020-07-31 21:01:10 +05:30
|
|
|
use anyhow::Result;
|
|
|
|
use std::fs::OpenOptions;
|
2021-05-04 13:40:20 +05:30
|
|
|
use std::io::Write;
|
2020-07-31 21:01:10 +05:30
|
|
|
use std::str::from_utf8;
|
|
|
|
|
|
|
|
mod common;
|
2021-07-01 19:57:37 +05:30
|
|
|
|
|
|
|
use common::common_args::*;
|
|
|
|
use common::input_arg::*;
|
2020-08-07 19:00:00 +05:30
|
|
|
use common::test_dir::*;
|
2021-05-04 13:40:20 +05:30
|
|
|
use common::*;
|
2020-07-31 21:01:10 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
const USAGE: &str = "Usage: thin_dump [options] {device|file}\n\
|
|
|
|
Options:\n \
|
|
|
|
{-h|--help}\n \
|
|
|
|
{-f|--format} {xml|human_readable|custom}\n \
|
|
|
|
{-r|--repair}\n \
|
|
|
|
{-m|--metadata-snap} [block#]\n \
|
|
|
|
{-o <xml file>}\n \
|
|
|
|
{--dev-id} <dev-id>\n \
|
|
|
|
{--skip-mappings}\n \
|
|
|
|
{-V|--version}";
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
//-----------------------------------------
|
|
|
|
|
|
|
|
struct ThinDump;
|
|
|
|
|
|
|
|
impl<'a> Program<'a> for ThinDump {
|
|
|
|
fn name() -> &'a str {
|
|
|
|
"thin_dump"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path() -> &'a str {
|
|
|
|
THIN_DUMP
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ThinDump {
|
|
|
|
fn mk_valid_input(td: &mut TestDir) -> Result<std::path::PathBuf> {
|
|
|
|
mk_valid_md(td)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
test_accepts_help!(ThinDump);
|
|
|
|
test_accepts_version!(ThinDump);
|
|
|
|
test_rejects_bad_option!(ThinDump);
|
2021-07-01 19:57:37 +05:30
|
|
|
|
2021-07-06 07:21:27 +05:30
|
|
|
test_missing_input_arg!(ThinDump);
|
|
|
|
test_input_file_not_found!(ThinDump);
|
|
|
|
test_input_cannot_be_a_directory!(ThinDump);
|
|
|
|
test_unreadable_input_file!(ThinDump);
|
2021-07-01 19:57:37 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// test dump & restore cycle
|
2020-07-31 21:01:10 +05:30
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dump_restore_cycle() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md_path = md.to_str().unwrap();
|
|
|
|
let output = run_ok_raw(THIN_DUMP, &[md_path])?;
|
2020-07-31 21:01:10 +05:30
|
|
|
|
|
|
|
let xml = td.mk_path("meta.xml");
|
2021-05-04 13:40:20 +05:30
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
.read(false)
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.open(&xml)?;
|
2020-07-31 21:01:10 +05:30
|
|
|
file.write_all(&output.stdout[0..])?;
|
|
|
|
drop(file);
|
|
|
|
|
|
|
|
let md2 = mk_zeroed_md(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md2_path = md2.to_str().unwrap();
|
|
|
|
let xml_path = xml.to_str().unwrap();
|
|
|
|
run_ok(THIN_RESTORE, &["-i", xml_path, "-o", md2_path])?;
|
2020-07-31 21:01:10 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
let output2 = run_ok_raw(THIN_DUMP, &[md2_path])?;
|
2020-07-31 21:01:10 +05:30
|
|
|
assert_eq!(output.stdout, output2.stdout);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test no stderr with a normal dump
|
|
|
|
|
2020-07-31 21:01:10 +05:30
|
|
|
#[test]
|
|
|
|
fn no_stderr() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md_path = md.to_str().unwrap();
|
|
|
|
let output = run_ok_raw(THIN_DUMP, &[md_path])?;
|
2020-07-31 21:01:10 +05:30
|
|
|
|
|
|
|
assert_eq!(output.stderr.len(), 0);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test superblock overriding & repair
|
|
|
|
// TODO: share with thin_repair
|
|
|
|
|
2020-07-31 21:01:10 +05:30
|
|
|
fn override_something(flag: &str, value: &str, pattern: &str) -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md_path = md.to_str().unwrap();
|
|
|
|
let output = run_ok_raw(THIN_DUMP, &[md_path, flag, value])?;
|
2020-07-31 21:01:10 +05:30
|
|
|
|
|
|
|
assert_eq!(output.stderr.len(), 0);
|
|
|
|
assert!(from_utf8(&output.stdout[0..])?.contains(pattern));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_transaction_id() -> Result<()> {
|
2021-05-04 13:40:20 +05:30
|
|
|
override_something("--transaction-id", "2345", "transaction=\"2345\"")
|
2020-07-31 21:01:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_data_block_size() -> Result<()> {
|
|
|
|
override_something("--data-block-size", "8192", "data_block_size=\"8192\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_nr_data_blocks() -> Result<()> {
|
|
|
|
override_something("--nr-data-blocks", "234500", "nr_data_blocks=\"234500\"")
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
// FIXME: duplicate with superblock_succeeds in thin_repair.rs
|
2020-07-31 21:01:10 +05:30
|
|
|
#[test]
|
|
|
|
fn repair_superblock() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let before = run_ok_raw(
|
|
|
|
THIN_DUMP,
|
|
|
|
&[
|
|
|
|
"--transaction-id=5",
|
|
|
|
"--data-block-size=128",
|
|
|
|
"--nr-data-blocks=4096000",
|
|
|
|
md.to_str().unwrap(),
|
|
|
|
],
|
|
|
|
)?;
|
2020-07-31 21:01:10 +05:30
|
|
|
damage_superblock(&md)?;
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
let after = run_ok_raw(
|
|
|
|
THIN_DUMP,
|
|
|
|
&[
|
|
|
|
"--repair",
|
|
|
|
"--transaction-id=5",
|
|
|
|
"--data-block-size=128",
|
|
|
|
"--nr-data-blocks=4096000",
|
|
|
|
md.to_str().unwrap(),
|
|
|
|
],
|
|
|
|
)?;
|
2020-07-31 21:01:10 +05:30
|
|
|
assert_eq!(after.stderr.len(), 0);
|
|
|
|
assert_eq!(before.stdout, after.stdout);
|
2021-05-04 13:40:20 +05:30
|
|
|
|
2020-07-31 21:01:10 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test compatibility between options
|
|
|
|
// TODO: share with thin_repair
|
|
|
|
|
2020-07-31 21:01:10 +05:30
|
|
|
#[test]
|
|
|
|
fn missing_transaction_id() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
|
|
|
damage_superblock(&md)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let stderr = run_fail(
|
|
|
|
THIN_DUMP,
|
|
|
|
&[
|
|
|
|
"--repair",
|
|
|
|
"--data-block-size=128",
|
|
|
|
"--nr-data-blocks=4096000",
|
|
|
|
md.to_str().unwrap(),
|
|
|
|
],
|
|
|
|
)?;
|
2020-07-31 21:01:10 +05:30
|
|
|
assert!(stderr.contains("transaction id"));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_data_block_size() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
|
|
|
damage_superblock(&md)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let stderr = run_fail(
|
|
|
|
THIN_DUMP,
|
|
|
|
&[
|
|
|
|
"--repair",
|
|
|
|
"--transaction-id=5",
|
|
|
|
"--nr-data-blocks=4096000",
|
|
|
|
md.to_str().unwrap(),
|
|
|
|
],
|
|
|
|
)?;
|
2020-07-31 21:01:10 +05:30
|
|
|
assert!(stderr.contains("data block size"));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_nr_data_blocks() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
|
|
|
damage_superblock(&md)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let stderr = run_fail(
|
|
|
|
THIN_DUMP,
|
|
|
|
&[
|
|
|
|
"--repair",
|
|
|
|
"--transaction-id=5",
|
|
|
|
"--data-block-size=128",
|
|
|
|
md.to_str().unwrap(),
|
|
|
|
],
|
|
|
|
)?;
|
2020-07-31 21:01:10 +05:30
|
|
|
assert!(stderr.contains("nr data blocks"));
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-07-01 19:57:37 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|