2020-07-31 15:34:12 +05:30
|
|
|
use anyhow::Result;
|
|
|
|
|
|
|
|
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::*;
|
2020-08-13 18:19:11 +05:30
|
|
|
use common::*;
|
2020-07-31 15:34:12 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
const USAGE: &str = "Usage: thin_check [options] {device|file}\n\
|
|
|
|
Options:\n \
|
|
|
|
{-q|--quiet}\n \
|
|
|
|
{-h|--help}\n \
|
|
|
|
{-V|--version}\n \
|
|
|
|
{-m|--metadata-snap}\n \
|
|
|
|
{--auto-repair}\n \
|
|
|
|
{--override-mapping-root}\n \
|
|
|
|
{--clear-needs-check-flag}\n \
|
|
|
|
{--ignore-non-fatal-errors}\n \
|
|
|
|
{--skip-mappings}\n \
|
|
|
|
{--super-block-only}";
|
2020-07-31 15:34:12 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
2020-07-31 15:34:12 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
test_accepts_help!(THIN_CHECK, USAGE);
|
|
|
|
test_accepts_version!(THIN_CHECK);
|
|
|
|
test_rejects_bad_option!(THIN_CHECK);
|
2020-07-31 15:34:12 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
test_missing_input_arg!(THIN_CHECK);
|
|
|
|
test_input_file_not_found!(THIN_CHECK, ARG);
|
|
|
|
test_input_cannot_be_a_directory!(THIN_CHECK, ARG);
|
|
|
|
test_unreadable_input_file!(THIN_CHECK, ARG);
|
2020-07-31 15:34:12 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
test_help_message_for_tiny_input_file!(THIN_CHECK, ARG);
|
|
|
|
test_spot_xml_data!(THIN_CHECK, "thin_check", ARG);
|
|
|
|
test_corrupted_input_data!(THIN_CHECK, ARG);
|
2020-07-31 15:34:12 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test exclusive flags
|
|
|
|
|
|
|
|
fn accepts_flag(flag: &str) -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_valid_md(&mut td)?;
|
|
|
|
let md_path = md.to_str().unwrap();
|
|
|
|
run_ok(THIN_CHECK, &[flag, md_path])?;
|
2020-07-31 15:34:12 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn accepts_superblock_only() -> Result<()> {
|
|
|
|
accepts_flag("--super-block-only")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn accepts_skip_mappings() -> Result<()> {
|
|
|
|
accepts_flag("--skip-mappings")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn accepts_ignore_non_fatal_errors() -> Result<()> {
|
|
|
|
accepts_flag("--ignore-non-fatal-errors")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn accepts_clear_needs_check_flag() -> Result<()> {
|
|
|
|
accepts_flag("--clear-needs-check-flag")
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
#[test]
|
|
|
|
fn accepts_auto_repair() -> Result<()> {
|
|
|
|
accepts_flag("--auto-repair")
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// test the --quiet flag
|
|
|
|
|
2020-07-31 15:34:12 +05:30
|
|
|
#[test]
|
|
|
|
fn accepts_quiet() -> Result<()> {
|
2020-07-31 18:56:22 +05:30
|
|
|
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();
|
2020-07-31 15:34:12 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
let output = run_ok_raw(THIN_CHECK, &["--quiet", md_path])?;
|
2020-07-31 15:34:12 +05:30
|
|
|
assert_eq!(output.stdout.len(), 0);
|
|
|
|
assert_eq!(output.stderr.len(), 0);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test superblock-block-only
|
2020-08-12 17:33:01 +05:30
|
|
|
|
2020-07-31 15:34:12 +05:30
|
|
|
#[test]
|
|
|
|
fn detects_corrupt_superblock_with_superblock_only() -> Result<()> {
|
2020-07-31 18:56:22 +05:30
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = mk_zeroed_md(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md_path = md.to_str().unwrap();
|
|
|
|
let _stderr = run_fail(THIN_CHECK, &["--super-block-only", md_path])?;
|
2020-07-31 15:34:12 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test info outputs
|
2020-07-31 15:34:12 +05:30
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn prints_info_fields() -> Result<()> {
|
2020-07-31 18:56:22 +05:30
|
|
|
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 stdout = run_ok(THIN_CHECK, &[md_path])?;
|
2020-07-31 15:34:12 +05:30
|
|
|
assert!(stdout.contains("TRANSACTION_ID="));
|
|
|
|
assert!(stdout.contains("METADATA_FREE_BLOCKS="));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test compatibility between options
|
|
|
|
|
2020-08-12 17:33:01 +05:30
|
|
|
#[test]
|
|
|
|
fn auto_repair_incompatible_opts() -> 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();
|
|
|
|
run_fail(THIN_CHECK, &["--auto-repair", "-m", md_path])?;
|
|
|
|
run_fail(
|
|
|
|
THIN_CHECK,
|
|
|
|
&["--auto-repair", "--override-mapping-root", "123", md_path],
|
|
|
|
)?;
|
|
|
|
run_fail(
|
|
|
|
THIN_CHECK,
|
|
|
|
&["--auto-repair", "--super-block-only", md_path],
|
|
|
|
)?;
|
|
|
|
run_fail(THIN_CHECK, &["--auto-repair", "--skip-mappings", md_path])?;
|
|
|
|
run_fail(
|
|
|
|
THIN_CHECK,
|
|
|
|
&["--auto-repair", "--ignore-non-fatal-errors", md_path],
|
|
|
|
)?;
|
2020-08-12 17:33:01 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn clear_needs_check_incompatible_opts() -> 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();
|
|
|
|
run_fail(THIN_CHECK, &["--clear-needs-check-flag", "-m", md_path])?;
|
|
|
|
run_fail(
|
|
|
|
THIN_CHECK,
|
|
|
|
&[
|
|
|
|
"--clear-needs-check-flag",
|
|
|
|
"--override-mapping-root",
|
|
|
|
"123",
|
|
|
|
md_path,
|
|
|
|
],
|
|
|
|
)?;
|
|
|
|
run_fail(
|
|
|
|
THIN_CHECK,
|
|
|
|
&["--clear-needs-check-flag", "--super-block-only", md_path],
|
|
|
|
)?;
|
|
|
|
run_fail(
|
|
|
|
THIN_CHECK,
|
|
|
|
&[
|
|
|
|
"--clear-needs-check-flag",
|
|
|
|
"--ignore-non-fatal-errors",
|
|
|
|
md_path,
|
|
|
|
],
|
|
|
|
)?;
|
2020-08-13 18:19:11 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
2021-07-01 19:57:37 +05:30
|
|
|
// test clear-needs-check
|
2020-08-13 18:19:11 +05:30
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn clear_needs_check() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md_path = md.to_str().unwrap();
|
|
|
|
|
2020-08-13 18:19:11 +05:30
|
|
|
set_needs_check(&md)?;
|
|
|
|
|
|
|
|
assert!(get_needs_check(&md)?);
|
2021-07-01 19:57:37 +05:30
|
|
|
run_ok(THIN_CHECK, &["--clear-needs-check-flag", md_path])?;
|
2020-08-13 18:19:11 +05:30
|
|
|
assert!(!get_needs_check(&md)?);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_clear_needs_check_if_error() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md_path = md.to_str().unwrap();
|
|
|
|
|
2020-08-13 18:19:11 +05:30
|
|
|
set_needs_check(&md)?;
|
|
|
|
generate_metadata_leaks(&md, 1, 0, 1)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
run_fail(THIN_CHECK, &["--clear-needs-check-flag", md_path])?;
|
2020-08-13 18:19:11 +05:30
|
|
|
assert!(get_needs_check(&md)?);
|
2020-08-12 17:33:01 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
2020-08-13 18:19:11 +05:30
|
|
|
|
2020-08-21 15:56:48 +05:30
|
|
|
#[test]
|
|
|
|
fn clear_needs_check_if_skip_mappings() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md_path = md.to_str().unwrap();
|
|
|
|
|
2020-08-21 15:56:48 +05:30
|
|
|
set_needs_check(&md)?;
|
|
|
|
generate_metadata_leaks(&md, 1, 0, 1)?;
|
|
|
|
|
|
|
|
assert!(get_needs_check(&md)?);
|
2021-07-01 19:57:37 +05:30
|
|
|
run_ok(
|
|
|
|
THIN_CHECK,
|
|
|
|
&["--clear-needs-check-flag", "--skip-mappings", md_path],
|
|
|
|
)?;
|
2020-08-21 15:56:48 +05:30
|
|
|
assert!(!get_needs_check(&md)?);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test ignore-non-fatal-errors
|
|
|
|
|
2020-08-13 18:19:11 +05:30
|
|
|
#[test]
|
|
|
|
fn metadata_leaks_are_non_fatal() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md_path = md.to_str().unwrap();
|
2020-08-13 18:19:11 +05:30
|
|
|
generate_metadata_leaks(&md, 1, 0, 1)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
run_fail(THIN_CHECK, &[md_path])?;
|
|
|
|
run_ok(THIN_CHECK, &["--ignore-non-fatal-errors", md_path])?;
|
2020-08-13 18:19:11 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fatal_errors_cant_be_ignored() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md_path = md.to_str().unwrap();
|
|
|
|
|
2020-08-13 18:19:11 +05:30
|
|
|
generate_metadata_leaks(&md, 1, 1, 0)?;
|
|
|
|
ensure_untouched(&md, || {
|
2021-07-01 19:57:37 +05:30
|
|
|
run_fail(THIN_CHECK, &["--ignore-non-fatal-errors", md_path])?;
|
2020-08-13 18:19:11 +05:30
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
//------------------------------------------
|
|
|
|
// test auto-repair
|
|
|
|
|
2020-08-13 18:19:11 +05:30
|
|
|
#[test]
|
|
|
|
fn auto_repair() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md_path = md.to_str().unwrap();
|
2020-08-13 18:19:11 +05:30
|
|
|
|
|
|
|
// auto-repair should have no effect on good metadata.
|
|
|
|
ensure_untouched(&md, || {
|
2021-07-01 19:57:37 +05:30
|
|
|
run_ok(THIN_CHECK, &["--auto-repair", md_path])?;
|
2020-08-13 18:19:11 +05:30
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
|
|
|
|
generate_metadata_leaks(&md, 16, 0, 1)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
run_fail(THIN_CHECK, &[md_path])?;
|
|
|
|
run_ok(THIN_CHECK, &["--auto-repair", md_path])?;
|
|
|
|
run_ok(THIN_CHECK, &[md_path])?;
|
2020-08-13 18:19:11 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn auto_repair_has_limits() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md_path = md.to_str().unwrap();
|
2020-08-13 18:19:11 +05:30
|
|
|
|
2021-07-01 19:57:37 +05:30
|
|
|
generate_metadata_leaks(&md, 16, 1, 0)?;
|
2020-08-13 18:19:11 +05:30
|
|
|
ensure_untouched(&md, || {
|
2021-07-01 19:57:37 +05:30
|
|
|
run_fail(THIN_CHECK, &["--auto-repair", md_path])?;
|
2020-08-13 18:19:11 +05:30
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn auto_repair_clears_needs_check() -> Result<()> {
|
|
|
|
let mut td = TestDir::new()?;
|
|
|
|
let md = prep_metadata(&mut td)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
let md_path = md.to_str().unwrap();
|
|
|
|
|
2020-08-13 18:19:11 +05:30
|
|
|
set_needs_check(&md)?;
|
2021-07-01 19:57:37 +05:30
|
|
|
run_ok(THIN_CHECK, &["--auto-repair", md_path])?;
|
2020-08-13 18:19:11 +05:30
|
|
|
assert!(!get_needs_check(&md)?);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-07-31 15:34:12 +05:30
|
|
|
//------------------------------------------
|