[tests] Move era_restore tests to Rust

This commit is contained in:
Ming-Hung Tsai 2021-10-01 23:31:32 +08:00
parent 4cfe93570c
commit 8d3f65244d
3 changed files with 144 additions and 71 deletions

View File

@ -46,76 +46,6 @@
;;;----------------------------------------------------------- ;;;-----------------------------------------------------------
;;; era_restore scenarios ;;; 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) (define-scenario (era-restore quiet-fail)
"No output with --quiet (failing)" "No output with --quiet (failing)"

View File

@ -26,7 +26,7 @@ pub fn mk_valid_md(td: &mut TestDir) -> Result<PathBuf> {
write_xml(&xml, &mut gen)?; write_xml(&xml, &mut gen)?;
let _file = file_utils::create_sized_file(&md, 4096 * 4096); 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) Ok(md)
} }

143
tests/era_restore.rs Normal file
View File

@ -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 <FILE> --output <FILE>
FLAGS:
-q, --quiet Suppress output messages, return only exit code.
-h, --help Prints help information
-V, --version Prints version information
OPTIONS:
-i, --input <FILE> Specify the input xml
-o, --output <FILE> Specify the output device to check";
//------------------------------------------
struct EraRestore;
impl<'a> Program<'a> for EraRestore {
fn name() -> &'a str {
"era_restore"
}
fn cmd<I>(args: I) -> Command
where
I: IntoIterator,
I::Item: Into<std::ffi::OsString>,
{
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<std::path::PathBuf> {
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(())
}
//-----------------------------------------