2020-08-04 16:00:46 +05:30
|
|
|
use anyhow::Result;
|
|
|
|
|
|
|
|
mod common;
|
2021-07-01 19:57:37 +05:30
|
|
|
|
|
|
|
use common::common_args::*;
|
|
|
|
use common::input_arg::*;
|
|
|
|
use common::output_option::*;
|
2020-08-07 19:00:00 +05:30
|
|
|
use common::test_dir::*;
|
2021-05-04 13:40:20 +05:30
|
|
|
use common::*;
|
2020-08-04 16:00:46 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
const USAGE: &str = "Usage: thin_repair [options] {device|file}\n\
|
|
|
|
Options:\n \
|
|
|
|
{-h|--help}\n \
|
|
|
|
{-i|--input} <input metadata (binary format)>\n \
|
|
|
|
{-o|--output} <output metadata (binary format)>\n \
|
|
|
|
{--transaction-id} <natural>\n \
|
|
|
|
{--data-block-size} <natural>\n \
|
|
|
|
{--nr-data-blocks} <natural>\n \
|
|
|
|
{-V|--version}";
|
2020-08-04 16:00:46 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//-----------------------------------------
|
2020-08-04 16:00:46 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
test_accepts_help!(THIN_REPAIR, USAGE);
|
|
|
|
test_accepts_version!(THIN_REPAIR);
|
|
|
|
test_rejects_bad_option!(THIN_REPAIR);
|
2020-08-04 16:00:46 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
test_input_file_not_found!(THIN_REPAIR, OPTION);
|
|
|
|
test_corrupted_input_data!(THIN_REPAIR, OPTION);
|
2020-08-04 16:00:46 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
test_missing_output_option!(THIN_REPAIR, mk_valid_md);
|
|
|
|
|
|
|
|
//-----------------------------------------
|
|
|
|
// test output to a small file
|
|
|
|
|
|
|
|
// TODO: share with thin_restore
|
2020-08-04 16:00:46 +05:30
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_repair_xml() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_zeroed_md(&mut td)?;
|
|
|
|
let xml = mk_valid_xml(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let xml_path = xml.to_str().unwrap();
|
|
|
|
let md_path = md.to_str().unwrap();
|
|
|
|
run_fail(THIN_REPAIR, &["-i", xml_path, "-o", md_path])?;
|
2020-08-04 16:00:46 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//-----------------------------------------
|
2020-08-04 16:00:46 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
// TODO: share with thin_dump
|
2020-08-04 16:00:46 +05:30
|
|
|
|
|
|
|
fn override_thing(flag: &str, val: &str, pattern: &str) -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md1 = mk_valid_md(&mut td)?;
|
|
|
|
let md2 = mk_zeroed_md(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md1_path = md1.to_str().unwrap();
|
|
|
|
let md2_path = md2.to_str().unwrap();
|
|
|
|
let output = run_ok_raw(THIN_REPAIR, &[flag, val, "-i", md1_path, "-o", md2_path])?;
|
2020-08-04 16:00:46 +05:30
|
|
|
assert_eq!(output.stderr.len(), 0);
|
2021-07-01 19:57:37 +05:30
|
|
|
let md2_path = md2.to_str().unwrap();
|
|
|
|
let output = run_ok(THIN_DUMP, &[md2_path])?;
|
|
|
|
assert!(output.contains(pattern));
|
2020-08-04 16:00:46 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_transaction_id() -> Result<()> {
|
|
|
|
override_thing("--transaction-id", "2345", "transaction=\"2345\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_data_block_size() -> Result<()> {
|
|
|
|
override_thing("--data-block-size", "8192", "data_block_size=\"8192\"")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn override_nr_data_blocks() -> Result<()> {
|
|
|
|
override_thing("--nr-data-blocks", "234500", "nr_data_blocks=\"234500\"")
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
// FIXME: that's repair_superblock in thin_dump.rs
|
2020-08-04 16:00:46 +05:30
|
|
|
#[test]
|
|
|
|
fn superblock_succeeds() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md1 = mk_valid_md(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md1_path = md1.to_str().unwrap();
|
|
|
|
let original = run_ok_raw(
|
|
|
|
THIN_DUMP,
|
|
|
|
&[
|
|
|
|
"--transaction-id=5",
|
|
|
|
"--data-block-size=128",
|
|
|
|
"--nr-data-blocks=4096000",
|
|
|
|
md1_path,
|
|
|
|
],
|
|
|
|
)?;
|
2020-08-04 16:00:46 +05:30
|
|
|
assert_eq!(original.stderr.len(), 0);
|
|
|
|
damage_superblock(&md1)?;
|
|
|
|
let md2 = mk_zeroed_md(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md2_path = md2.to_str().unwrap();
|
|
|
|
run_ok(
|
|
|
|
THIN_REPAIR,
|
|
|
|
&[
|
|
|
|
"--transaction-id=5",
|
|
|
|
"--data-block-size=128",
|
|
|
|
"--nr-data-blocks=4096000",
|
|
|
|
"-i",
|
|
|
|
md1_path,
|
|
|
|
"-o",
|
|
|
|
md2_path,
|
|
|
|
],
|
|
|
|
)?;
|
|
|
|
let repaired = run_ok_raw(THIN_DUMP, &[md2_path])?;
|
2020-08-04 16:00:46 +05:30
|
|
|
assert_eq!(repaired.stderr.len(), 0);
|
|
|
|
assert_eq!(original.stdout, repaired.stdout);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//-----------------------------------------
|
|
|
|
|
|
|
|
// TODO: share with thin_dump
|
|
|
|
|
2021-05-04 13:40:20 +05:30
|
|
|
fn missing_thing(flag1: &str, flag2: &str, pattern: &str) -> Result<()> {
|
2020-08-04 16:00:46 +05:30
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md1 = mk_valid_md(&mut td)?;
|
|
|
|
damage_superblock(&md1)?;
|
|
|
|
let md2 = mk_zeroed_md(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let stderr = run_fail(
|
|
|
|
THIN_REPAIR,
|
|
|
|
&[
|
|
|
|
flag1,
|
|
|
|
flag2,
|
|
|
|
"-i",
|
|
|
|
md1.to_str().unwrap(),
|
|
|
|
"-o",
|
|
|
|
md2.to_str().unwrap(),
|
|
|
|
],
|
|
|
|
)?;
|
2020-08-04 16:00:46 +05:30
|
|
|
assert!(stderr.contains(pattern));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_transaction_id() -> Result<()> {
|
2021-05-04 13:40:20 +05:30
|
|
|
missing_thing(
|
|
|
|
"--data-block-size=128",
|
|
|
|
"--nr-data-blocks=4096000",
|
|
|
|
"transaction id",
|
|
|
|
)
|
2020-08-04 16:00:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_data_block_size() -> Result<()> {
|
2021-05-04 13:40:20 +05:30
|
|
|
missing_thing(
|
|
|
|
"--transaction-id=5",
|
|
|
|
"--nr-data-blocks=4096000",
|
|
|
|
"data block size",
|
|
|
|
)
|
2020-08-04 16:00:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn missing_nr_data_blocks() -> Result<()> {
|
2021-05-04 13:40:20 +05:30
|
|
|
missing_thing(
|
|
|
|
"--transaction-id=5",
|
|
|
|
"--data-block-size=128",
|
|
|
|
"nr data blocks",
|
|
|
|
)
|
2020-08-04 16:00:46 +05:30
|
|
|
}
|
2021-07-01 19:57:37 +05:30
|
|
|
|
|
|
|
//-----------------------------------------
|