From 8d3f65244d61d7a1c93ecac97f3fe0a065d37c98 Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Fri, 1 Oct 2021 23:31:32 +0800 Subject: [PATCH] [tests] Move era_restore tests to Rust --- functional-tests/era-functional-tests.scm | 70 ----------- tests/common/era.rs | 2 +- tests/era_restore.rs | 143 ++++++++++++++++++++++ 3 files changed, 144 insertions(+), 71 deletions(-) create mode 100644 tests/era_restore.rs diff --git a/functional-tests/era-functional-tests.scm b/functional-tests/era-functional-tests.scm index f15c599..3847b82 100644 --- a/functional-tests/era-functional-tests.scm +++ b/functional-tests/era-functional-tests.scm @@ -46,76 +46,6 @@ ;;;----------------------------------------------------------- ;;; era_restore scenarios ;;;----------------------------------------------------------- - (define-scenario (era-restore v) - "era_restore -V" - (run-ok-rcv (stdout _) (era-restore "-V") - (assert-equal tools-version stdout))) - - (define-scenario (era-restore version) - "era_restore --version" - (run-ok-rcv (stdout _) (era-restore "--version") - (assert-equal tools-version stdout))) - - (define-scenario (era-restore h) - "era_restore -h" - (run-ok-rcv (stdout _) (era-restore "-h") - (assert-equal era-restore-help stdout))) - - (define-scenario (era-restore help) - "era_restore --help" - (run-ok-rcv (stdout _) (era-restore "--help") - (assert-equal era-restore-help stdout))) - - (define-scenario (era-restore input-unspecified) - "Fails if no xml specified" - (with-empty-metadata (md) - (run-fail-rcv (_ stderr) (era-restore "-o" md) - (assert-starts-with "No input file provided." stderr)))) - - (define-scenario (era-restore missing-input-file) - "the input file can't be found" - (with-empty-metadata (md) - (let ((bad-path "no-such-file")) - (run-fail-rcv (_ stderr) (era-restore "-i no-such-file -o" md) - (assert-superblock-all-zeroes md) - (assert-starts-with - (string-append bad-path ": No such file or directory") - stderr))))) - - (define-scenario (era-restore garbage-input-file) - "the input file is just zeroes" - (with-empty-metadata (md) - (with-temp-file-sized ((xml "era.xml" 4096)) - (run-fail-rcv (_ stderr) (era-restore "-i " xml "-o" md) - (assert-superblock-all-zeroes md))))) - - (define-scenario (era-restore output-unspecified) - "Fails if no metadata dev specified" - (with-era-xml (xml) - (run-fail-rcv (_ stderr) (era-restore "-i" xml) - (assert-starts-with "No output file provided." stderr)))) - - (define-scenario (era-restore success) - "Succeeds with xml and metadata" - (with-era-xml (xml) - (with-empty-metadata (md) - (run-ok (era-restore "-i" xml "-o" md))))) - - (define-scenario (era-restore quiet) - "No output with --quiet (succeeding)" - (with-era-xml (xml) - (with-empty-metadata (md) - (run-ok-rcv (stdout stderr) (era-restore "--quiet" "-i" xml "-o" md) - (assert-eof stdout) - (assert-eof stderr))))) - - (define-scenario (era-restore q) - "No output with -q (succeeding)" - (with-era-xml (xml) - (with-empty-metadata (md) - (run-ok-rcv (stdout stderr) (era-restore "-q" "-i" xml "-o" md) - (assert-eof stdout) - (assert-eof stderr))))) (define-scenario (era-restore quiet-fail) "No output with --quiet (failing)" diff --git a/tests/common/era.rs b/tests/common/era.rs index c7b66a0..12406dc 100644 --- a/tests/common/era.rs +++ b/tests/common/era.rs @@ -26,7 +26,7 @@ pub fn mk_valid_md(td: &mut TestDir) -> Result { write_xml(&xml, &mut gen)?; let _file = file_utils::create_sized_file(&md, 4096 * 4096); - run_ok(ERA_RESTORE, args!["-i", &xml, "-o", &md])?; + run_ok(era_restore_cmd(args!["-i", &xml, "-o", &md]))?; Ok(md) } diff --git a/tests/era_restore.rs b/tests/era_restore.rs new file mode 100644 index 0000000..eadbe10 --- /dev/null +++ b/tests/era_restore.rs @@ -0,0 +1,143 @@ +use anyhow::Result; + +mod common; + +use common::common_args::*; +use common::era::*; +use common::fixture::*; +use common::input_arg::*; +use common::output_option::*; +use common::process::*; +use common::program::*; +use common::target::*; +use common::test_dir::*; + +//------------------------------------------ + +const USAGE: &str = "era_restore 0.9.0 +Convert XML format metadata to binary. + +USAGE: + era_restore [FLAGS] --input --output + +FLAGS: + -q, --quiet Suppress output messages, return only exit code. + -h, --help Prints help information + -V, --version Prints version information + +OPTIONS: + -i, --input Specify the input xml + -o, --output Specify the output device to check"; + +//------------------------------------------ + +struct EraRestore; + +impl<'a> Program<'a> for EraRestore { + fn name() -> &'a str { + "era_restore" + } + + fn cmd(args: I) -> Command + where + I: IntoIterator, + I::Item: Into, + { + era_restore_cmd(args) + } + + fn usage() -> &'a str { + USAGE + } + + fn arg_type() -> ArgType { + ArgType::IoOptions + } + + fn bad_option_hint(option: &str) -> String { + msg::bad_option_hint(option) + } +} + +impl<'a> InputProgram<'a> for EraRestore { + fn mk_valid_input(td: &mut TestDir) -> Result { + mk_valid_xml(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 { + "" // we don't intent to verify error messages of XML parsing + } +} + +impl<'a> OutputProgram<'a> for EraRestore { + fn missing_output_arg() -> &'a str { + msg::MISSING_OUTPUT_ARG + } +} + +impl<'a> MetadataWriter<'a> for EraRestore { + fn file_not_found() -> &'a str { + msg::FILE_NOT_FOUND + } +} + +//----------------------------------------- + +test_accepts_help!(EraRestore); +test_accepts_version!(EraRestore); + +test_missing_input_option!(EraRestore); +test_input_file_not_found!(EraRestore); +test_corrupted_input_data!(EraRestore); + +test_missing_output_option!(EraRestore); +test_tiny_output_file!(EraRestore); + +test_unwritable_output_file!(EraRestore); + +//----------------------------------------- + +// TODO: share with thin_restore, era_restore + +fn quiet_flag(flag: &str) -> Result<()> { + let mut td = TestDir::new()?; + let xml = mk_valid_xml(&mut td)?; + let md = mk_zeroed_md(&mut td)?; + + let output = run_ok_raw(era_restore_cmd(args!["-i", &xml, "-o", &md, flag]))?; + + assert_eq!(output.stdout.len(), 0); + assert_eq!(output.stderr.len(), 0); + Ok(()) +} + +#[test] +fn accepts_q() -> Result<()> { + quiet_flag("-q") +} + +#[test] +fn accepts_quiet() -> Result<()> { + quiet_flag("--quiet") +} + +//----------------------------------------- + +#[test] +fn successfully_restores() -> Result<()> { + let mut td = TestDir::new()?; + let xml = mk_valid_xml(&mut td)?; + let md = mk_zeroed_md(&mut td)?; + run_ok(era_restore_cmd(args!["-i", &xml, "-o", &md]))?; + Ok(()) +} + +//-----------------------------------------