[tests] Port cache_restore tests

This commit is contained in:
Ming-Hung Tsai 2021-09-01 11:39:11 +08:00
parent 9253117132
commit 59e44667a9
3 changed files with 168 additions and 96 deletions

View File

@ -49,102 +49,6 @@
;; to run. ;; to run.
(define (register-cache-tests) #t) (define (register-cache-tests) #t)
;;;-----------------------------------------------------------
;;; cache_restore scenarios
;;;-----------------------------------------------------------
(define-scenario (cache-restore v)
"print version (-V flag)"
(run-ok-rcv (stdout _) (cache-restore "-V")
(assert-equal tools-version stdout)))
(define-scenario (cache-restore version)
"print version (--version flags)"
(run-ok-rcv (stdout _) (cache-restore "--version")
(assert-equal tools-version stdout)))
(define-scenario (cache-restore h)
"cache_restore -h"
(run-ok-rcv (stdout _) (cache-restore "-h")
(assert-equal cache-restore-help stdout)))
(define-scenario (cache-restore help)
"cache_restore --help"
(run-ok-rcv (stdout _) (cache-restore "--help")
(assert-equal cache-restore-help stdout)))
(define-scenario (cache-restore no-input-file)
"forget to specify an input file"
(with-empty-metadata (md)
(run-fail-rcv (_ stderr) (cache-restore "-o" md)
(assert-starts-with "No input file provided." stderr))))
(define-scenario (cache-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) (cache-restore "-i" bad-path "-o" md)
(assert-superblock-all-zeroes md)
(assert-starts-with
(string-append bad-path ": No such file or directory")
stderr)))))
(define-scenario (cache-restore garbage-input-file)
"the input file is just zeroes"
(with-empty-metadata (md)
(with-temp-file-sized ((xml "cache.xml" 4096))
(run-fail-rcv (_ stderr) (cache-restore "-i" xml "-o" md)
(assert-superblock-all-zeroes md)))))
(define-scenario (cache-restore missing-output-file)
"the output file can't be found"
(with-cache-xml (xml)
(run-fail-rcv (_ stderr) (cache-restore "-i" xml)
(assert-starts-with "No output file provided." stderr))))
(define-scenario (cache-restore tiny-output-file)
"Fails if the output file is too small."
(with-temp-file-sized ((md "cache.bin" (* 1024 4)))
(with-cache-xml (xml)
(run-fail-rcv (_ stderr) (cache-restore "-i" xml "-o" md)
(assert-starts-with cache-restore-outfile-too-small-text stderr)))))
(define-scenario (cache-restore successfully-restores)
"Restore succeeds."
(with-empty-metadata (md)
(with-cache-xml (xml)
(run-ok (cache-restore "-i" xml "-o" md)))))
(define-scenario (cache-restore q)
"cache_restore accepts -q"
(with-empty-metadata (md)
(with-cache-xml (xml)
(run-ok-rcv (stdout stderr) (cache-restore "-i" xml "-o" md "-q")
(assert-eof stdout)
(assert-eof stderr)))))
(define-scenario (cache-restore quiet)
"cache_restore accepts --quiet"
(with-empty-metadata (md)
(with-cache-xml (xml)
(run-ok-rcv (stdout stderr) (cache-restore "-i" xml "-o" md "--quiet")
(assert-eof stdout)
(assert-eof stderr)))))
(define-scenario (cache-restore override-metadata-version)
"we can set any metadata version"
(with-empty-metadata (md)
(with-cache-xml (xml)
(run-ok
(cache-restore "-i" xml "-o" md "--debug-override-metadata-version 10298")))))
(define-scenario (cache-restore omit-clean-shutdown)
"accepts --omit-clean-shutdown"
(with-empty-metadata (md)
(with-cache-xml (xml)
(run-ok
(cache-restore "-i" xml "-o" md "--omit-clean-shutdown")))))
;;;----------------------------------------------------------- ;;;-----------------------------------------------------------
;;; cache_dump scenarios ;;; cache_dump scenarios
;;;----------------------------------------------------------- ;;;-----------------------------------------------------------

167
tests/cache_restore.rs Normal file
View File

@ -0,0 +1,167 @@
use anyhow::Result;
mod common;
use common::cache::*;
use common::common_args::*;
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 = "Usage: cache_restore [options]\n\
Options:\n \
{-h|--help}\n \
{-i|--input} <input xml file>\n \
{-o|--output} <output device or file>\n \
{-q|--quiet}\n \
{--metadata-version} <1 or 2>\n \
{-V|--version}\n\
\n \
{--debug-override-metadata-version} <integer>\n \
{--omit-clean-shutdown}";
//------------------------------------------
struct CacheRestore;
impl<'a> Program<'a> for CacheRestore {
fn name() -> &'a str {
"thin_restore"
}
fn path() -> &'a std::ffi::OsStr {
CACHE_RESTORE.as_ref()
}
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 CacheRestore {
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 CacheRestore {
fn missing_output_arg() -> &'a str {
msg::MISSING_OUTPUT_ARG
}
}
impl<'a> MetadataWriter<'a> for CacheRestore {
fn file_not_found() -> &'a str {
msg::FILE_NOT_FOUND
}
}
//-----------------------------------------
test_accepts_help!(CacheRestore);
test_accepts_version!(CacheRestore);
test_missing_input_option!(CacheRestore);
test_input_file_not_found!(CacheRestore);
test_corrupted_input_data!(CacheRestore);
test_missing_output_option!(CacheRestore);
test_tiny_output_file!(CacheRestore);
test_unwritable_output_file!(CacheRestore);
//-----------------------------------------
// 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(CACHE_RESTORE, 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(CACHE_RESTORE, args!["-i", &xml, "-o", &md])?;
Ok(())
}
#[test]
fn override_metadata_version() -> Result<()> {
let mut td = TestDir::new()?;
let xml = mk_valid_xml(&mut td)?;
let md = mk_zeroed_md(&mut td)?;
run_ok(
CACHE_RESTORE,
args![
"-i",
&xml,
"-o",
&md,
"--debug-override-metadata-version",
"10298"
],
)?;
Ok(())
}
#[test]
fn accepts_omit_clean_shutdown() -> Result<()> {
let mut td = TestDir::new()?;
let xml = mk_valid_xml(&mut td)?;
let md = mk_zeroed_md(&mut td)?;
run_ok(
CACHE_RESTORE,
args!["-i", &xml, "-o", &md, "--omit-clean-shutdown"],
)?;
Ok(())
}
//-----------------------------------------

View File

@ -34,6 +34,7 @@ macro_rules! path_to {
pub const CACHE_CHECK: &str = path_to!("cache_check"); pub const CACHE_CHECK: &str = path_to!("cache_check");
pub const CACHE_DUMP: &str = path_to!("cache_dump"); pub const CACHE_DUMP: &str = path_to!("cache_dump");
pub const CACHE_RESTORE: &str = path_to!("cache_restore");
pub const THIN_CHECK: &str = path_to!("thin_check"); pub const THIN_CHECK: &str = path_to!("thin_check");
pub const THIN_DELTA: &str = path_to_cpp!("thin_delta"); // TODO: rust version pub const THIN_DELTA: &str = path_to_cpp!("thin_delta"); // TODO: rust version