From baf1fe325f73f55c1d39c85f6bf49506a3566e4f Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Fri, 31 Jul 2020 16:31:10 +0100 Subject: [PATCH] [functional-tests] Move thin_dump tests to Rust. cargo test --- functional-tests/thin-functional-tests.scm | 72 ----------- tests/common/mod.rs | 18 ++- tests/thin_check.rs | 4 - tests/thin_dump.rs | 136 +++++++++++++++++++++ tests/thin_restore.rs | 3 - tests/thin_shrink.rs | 3 +- 6 files changed, 150 insertions(+), 86 deletions(-) create mode 100644 tests/thin_dump.rs diff --git a/functional-tests/thin-functional-tests.scm b/functional-tests/thin-functional-tests.scm index 4887ff7..78ab791 100644 --- a/functional-tests/thin-functional-tests.scm +++ b/functional-tests/thin-functional-tests.scm @@ -67,78 +67,6 @@ ;; to run. (define (register-thin-tests) #t) - ;;;----------------------------------------------------------- - ;;; thin_dump scenarios - ;;;----------------------------------------------------------- - - (define-scenario (thin-dump small-input-file) - "Fails with small input file" - (with-temp-file-sized ((md "thin.bin" 512)) - (run-fail (thin-dump md)))) - - (define-scenario (thin-dump restore-is-noop) - "thin_dump followed by thin_restore is a noop." - (with-valid-metadata (md) - (run-ok-rcv (d1-stdout _) (thin-dump md) - (with-temp-file-containing ((xml "thin.xml" d1-stdout)) - (run-ok (thin-restore "-i" xml "-o" md)) - (run-ok-rcv (d2-stdout _) (thin-dump md) - (assert-equal d1-stdout d2-stdout)))))) - - (define-scenario (thin-dump no-stderr) - "thin_dump of clean data does not output error messages to stderr" - (with-valid-metadata (md) - (run-ok-rcv (stdout stderr) (thin-dump md) - (assert-eof stderr)))) - - (define-scenario (thin-dump override transaction-id) - "thin_dump obeys the --transaction-id override" - (with-valid-metadata (md) - (run-ok-rcv (stdout stderr) (thin-dump "--transaction-id 2345" md) - (assert-eof stderr) - (assert-matches ".*transaction=\"2345\"" stdout)))) - - (define-scenario (thin-dump override data-block-size) - "thin_dump obeys the --data-block-size override" - (with-valid-metadata (md) - (run-ok-rcv (stdout stderr) (thin-dump "--data-block-size 8192" md) - (assert-eof stderr) - (assert-matches ".*data_block_size=\"8192\"" stdout)))) - - (define-scenario (thin-dump override nr-data-blocks) - "thin_dump obeys the --nr-data-blocks override" - (with-valid-metadata (md) - (run-ok-rcv (stdout stderr) (thin-dump "--nr-data-blocks 234500" md) - (assert-eof stderr) - (assert-matches ".*nr_data_blocks=\"234500\"" stdout)))) - - (define-scenario (thin-dump repair-superblock succeeds) - "thin_dump can restore a missing superblock" - (with-valid-metadata (md) - (run-ok-rcv (expected-xml stderr) (thin-dump "--transaction-id=5" "--data-block-size=128" "--nr-data-blocks=4096000" md) - (damage-superblock md) - (run-ok-rcv (repaired-xml stderr) (thin-dump "--repair" "--transaction-id=5" "--data-block-size=128" "--nr-data-blocks=4096000" md) - (assert-eof stderr) - (assert-equal expected-xml repaired-xml))))) - - (define-scenario (thin-dump repair-superblock missing-transaction-id) - "--transaction-id is mandatory if the superblock is damaged" - (with-damaged-superblock (md) - (run-fail-rcv (_ stderr) (thin-dump "--repair" "--data-block-size=128" "--nr-data-blocks=4096000" md) - (assert-matches ".*transaction id.*" stderr)))) - - (define-scenario (thin-dump repair-superblock missing-data-block-size) - "--data-block-size is mandatory if the superblock is damaged" - (with-damaged-superblock (md) - (run-fail-rcv (_ stderr) (thin-dump "--repair" "--transaction-id=5" "--nr-data-blocks=4096000" md) - (assert-matches ".*data block size.*" stderr)))) - - (define-scenario (thin-dump repair-superblock missing-nr-data-blocks) - "--nr-data-blocks is mandatory if the superblock is damaged" - (with-damaged-superblock (md) - (run-fail-rcv (_ stderr) (thin-dump "--repair" "--transaction-id=5" "--data-block-size=128" md) - (assert-matches ".*nr data blocks.*" stderr)))) - ;;;----------------------------------------------------------- ;;; thin_rmap scenarios ;;;----------------------------------------------------------- diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 52ee3e1..37a3348 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,14 +1,14 @@ use anyhow::Result; use duct::{cmd, Expression}; use std::fs::OpenOptions; -use std::path::{Path, PathBuf}; +use std::io::{Read, Write}; +use std::path::{Display, PathBuf}; use std::str::from_utf8; use tempfile::{tempdir, TempDir}; use thinp::file_utils; -use std::io::{Read}; pub mod xml_generator; -use crate::common::xml_generator::{write_xml, FragmentedS, SingleThinS}; +use crate::common::xml_generator::{write_xml, SingleThinS}; //------------------------------------------ @@ -56,7 +56,7 @@ pub struct TestDir { impl TestDir { pub fn new() -> Result { let dir = tempdir()?; - Ok(TestDir {dir, file_count: 0}) + Ok(TestDir { dir, file_count: 0 }) } pub fn mk_path(&mut self, file: &str) -> PathBuf { @@ -96,6 +96,7 @@ pub fn mk_valid_md(td: &mut TestDir) -> Result { pub fn mk_zeroed_md(td: &mut TestDir) -> Result { let md = td.mk_path("meta.bin"); + eprintln!("path = {:?}", md); let _file = file_utils::create_sized_file(&md, 4096 * 4096); Ok(md) } @@ -116,8 +117,15 @@ pub fn superblock_all_zeroes(path: &PathBuf) -> Result { return Ok(false); } } - + Ok(true) } +pub fn damage_superblock(path: &PathBuf) -> Result<()> { + let mut output = OpenOptions::new().read(false).write(true).open(path)?; + let buf = [0u8; 512]; + output.write_all(&buf)?; + Ok(()) +} + //------------------------------------------ diff --git a/tests/thin_check.rs b/tests/thin_check.rs index 42caba6..52c7696 100644 --- a/tests/thin_check.rs +++ b/tests/thin_check.rs @@ -1,8 +1,4 @@ use anyhow::Result; -use duct::{cmd, Expression}; -use std::path::{Path, PathBuf}; -use std::str::from_utf8; -use tempfile::{tempdir, TempDir}; use thinp::file_utils; use thinp::version::TOOLS_VERSION; diff --git a/tests/thin_dump.rs b/tests/thin_dump.rs new file mode 100644 index 0000000..f71c840 --- /dev/null +++ b/tests/thin_dump.rs @@ -0,0 +1,136 @@ +use anyhow::Result; +use thinp::file_utils; +use std::fs::OpenOptions; +use std::io::{Write}; +use std::str::from_utf8; + +mod common; + +use common::xml_generator::{write_xml, FragmentedS, SingleThinS}; +use common::*; + +//------------------------------------------ + +#[test] +fn small_input_file() -> Result<()> { + let mut td = TestDir::new()?; + let md = td.mk_path("meta.bin"); + file_utils::create_sized_file(&md, 512)?; + let _stderr = run_fail(thin_dump!(&md))?; + Ok(()) +} + +#[test] +fn dump_restore_cycle() -> Result<()> { + let mut td = TestDir::new()?; + + let md = mk_valid_md(&mut td)?; + let output = thin_dump!(&md).run()?; + + 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)?; + thin_restore!("-i", &xml, "-o", &md2).run()?; + + let output2 = thin_dump!(&md2).run()?; + assert_eq!(output.stdout, output2.stdout); + + Ok(()) +} + +#[test] +fn no_stderr() -> Result<()> { + let mut td = TestDir::new()?; + + let md = mk_valid_md(&mut td)?; + let output = thin_dump!(&md).run()?; + + assert_eq!(output.stderr.len(), 0); + Ok(()) +} + +fn override_something(flag: &str, value: &str, pattern: &str) -> Result<()> { + let mut td = TestDir::new()?; + let md = mk_valid_md(&mut td)?; + let output = thin_dump!(&md, flag, value).run()?; + + assert_eq!(output.stderr.len(), 0); + assert!(from_utf8(&output.stdout[0..])?.contains(pattern)); + Ok(()) +} + +#[test] +fn override_transaction_id() -> Result<()> { + override_something("--transaction-id", "2345", "transaction=\"2345\"") +} + +#[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\"") +} + +#[test] +fn repair_superblock() -> Result<()> { + let mut td = TestDir::new()?; + let md = mk_valid_md(&mut td)?; + let before = thin_dump!("--transaction-id=5", "--data-block-size=128", "--nr-data-blocks=4096000", &md).run()?; + damage_superblock(&md)?; + + let after = thin_dump!("--repair", "--transaction-id=5", "--data-block-size=128", "--nr-data-blocks=4096000", &md).run()?; + assert_eq!(after.stderr.len(), 0); + assert_eq!(before.stdout, after.stdout); + + Ok(()) +} + +#[test] +fn missing_transaction_id() -> Result<()> { + let mut td = TestDir::new()?; + let md = mk_valid_md(&mut td)?; + damage_superblock(&md)?; + let stderr = run_fail(thin_dump!("--repair", "--data-block-size=128", "--nr-data-blocks=4096000", &md))?; + 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)?; + let stderr = run_fail(thin_dump!("--repair", "--transaction-id=5", "--nr-data-blocks=4096000", &md))?; + 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)?; + let stderr = run_fail(thin_dump!("--repair", "--transaction-id=5", "--data-block-size=128", &md))?; + assert!(stderr.contains("nr data blocks")); + Ok(()) +} + + +// (define-scenario (thin-dump repair-superblock missing-data-block-size) +// "--data-block-size is mandatory if the superblock is damaged" +// (with-damaged-superblock (md) +// (run-fail-rcv (_ stderr) (thin-dump "--repair" "--transaction-id=5" "--nr-data-blocks=4096000" md) +// (assert-matches ".*data block size.*" stderr)))) +// +// (define-scenario (thin-dump repair-superblock missing-nr-data-blocks) +// "--nr-data-blocks is mandatory if the superblock is damaged" +// (with-damaged-superblock (md) +// (run-fail-rcv (_ stderr) (thin-dump "--repair" "--transaction-id=5" "--data-block-size=128" md) +// (assert-matches ".*nr data blocks.*" stderr)))) +// diff --git a/tests/thin_restore.rs b/tests/thin_restore.rs index e8861bc..daca8e9 100644 --- a/tests/thin_restore.rs +++ b/tests/thin_restore.rs @@ -1,8 +1,5 @@ use anyhow::Result; -use duct::{cmd, Expression}; -use std::path::{Path, PathBuf}; use std::str::from_utf8; -use tempfile::{tempdir, TempDir}; use thinp::file_utils; use thinp::version::TOOLS_VERSION; diff --git a/tests/thin_shrink.rs b/tests/thin_shrink.rs index 8efae51..3397d20 100644 --- a/tests/thin_shrink.rs +++ b/tests/thin_shrink.rs @@ -3,8 +3,7 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use rand::prelude::*; use std::fs::OpenOptions; use std::io::{Cursor, Read, Seek, SeekFrom, Write}; -use std::path::{Path, PathBuf}; -use tempfile::tempdir; +use std::path::{Path}; use thinp::file_utils; use thinp::thin::xml::{self, Visit};