From 9ea75ba113e3fea8c3c15af5da910079fd87dedb Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Tue, 12 Oct 2021 14:36:16 +0800 Subject: [PATCH] [tests] Move cache_metadata_size tests to Rust Changes for the Rust version in corresponding to command line changes: - Disable tests 'all_args_agree' and 'conradictory_info_fails' - Test conflicts between --nr-blocks and {--device-size|--block-size} --- functional-tests/cache-functional-tests.scm | 72 -------- tests/cache_metadata_size.rs | 186 ++++++++++++++++++++ tests/common/process.rs | 6 +- tests/common/target.rs | 8 + 4 files changed, 199 insertions(+), 73 deletions(-) create mode 100644 tests/cache_metadata_size.rs diff --git a/functional-tests/cache-functional-tests.scm b/functional-tests/cache-functional-tests.scm index f99606f..48fe646 100644 --- a/functional-tests/cache-functional-tests.scm +++ b/functional-tests/cache-functional-tests.scm @@ -58,76 +58,4 @@ (with-temp-file-sized ((md "cache.bin" 512)) (run-fail (cache-dump md)))) - - ;;;----------------------------------------------------------- - ;;; cache_metadata_size scenarios - ;;;----------------------------------------------------------- - - (define-scenario (cache-metadata-size v) - "cache_metadata_size -V" - (run-ok-rcv (stdout _) (cache-metadata-size "-V") - (assert-equal tools-version stdout))) - - (define-scenario (cache-metadata-size version) - "cache_metadata_size --version" - (run-ok-rcv (stdout _) (cache-metadata-size "--version") - (assert-equal tools-version stdout))) - - (define-scenario (cache-metadata-size h) - "cache_metadata_size -h" - (run-ok-rcv (stdout _) (cache-metadata-size "-h") - (assert-equal cache-metadata-size-help stdout))) - - (define-scenario (cache-metadata-size help) - "cache_metadata_size --help" - (run-ok-rcv (stdout _) (cache-metadata-size "--help") - (assert-equal cache-metadata-size-help stdout))) - - (define-scenario (cache-metadata-size no-args) - "No arguments specified causes fail" - (run-fail-rcv (_ stderr) (cache-metadata-size) - (assert-equal "Please specify either --device-size and --block-size, or --nr-blocks." - stderr))) - - (define-scenario (cache-metadata-size device-size-only) - "Just --device-size causes fail" - (run-fail-rcv (_ stderr) (cache-metadata-size "--device-size" (to-bytes (meg 100))) - (assert-equal "If you specify --device-size you must also give --block-size." - stderr))) - - (define-scenario (cache-metadata-size block-size-only) - "Just --block-size causes fail" - (run-fail-rcv (_ stderr) (cache-metadata-size "--block-size" 128) - (assert-equal "If you specify --block-size you must also give --device-size." - stderr))) - - (define-scenario (cache-metadata-size conradictory-info-fails) - "Contradictory info causes fail" - (run-fail-rcv (_ stderr) (cache-metadata-size "--device-size 102400 --block-size 1000 --nr-blocks 6") - (assert-equal "Contradictory arguments given, --nr-blocks doesn't match the --device-size and --block-size." - stderr))) - - (define-scenario (cache-metadata-size all-args-agree) - "All args agreeing succeeds" - (run-ok-rcv (stdout stderr) (cache-metadata-size "--device-size" 102400 "--block-size" 100 "--nr-blocks" 1024) - (assert-equal "8248 sectors" stdout) - (assert-eof stderr))) - - (define-scenario (cache-metadata-size nr-blocks-alone) - "Just --nr-blocks succeeds" - (run-ok-rcv (stdout stderr) (cache-metadata-size "--nr-blocks" 1024) - (assert-equal "8248 sectors" stdout) - (assert-eof stderr))) - - (define-scenario (cache-metadata-size dev-size-and-block-size-succeeds) - "Specifying --device-size with --block-size succeeds" - (run-ok-rcv (stdout stderr) (cache-metadata-size "--device-size" 102400 "--block-size" 100) - (assert-equal "8248 sectors" stdout) - (assert-eof stderr))) - - (define-scenario (cache-metadata-size big-config) - "A big configuration succeeds" - (run-ok-rcv (stdout stderr) (cache-metadata-size "--nr-blocks 67108864") - (assert-equal "3678208 sectors" stdout) - (assert-eof stderr))) ) diff --git a/tests/cache_metadata_size.rs b/tests/cache_metadata_size.rs new file mode 100644 index 0000000..22ea6cf --- /dev/null +++ b/tests/cache_metadata_size.rs @@ -0,0 +1,186 @@ +use anyhow::Result; + +mod common; + +use common::common_args::*; +use common::process::*; +use common::program::*; +use common::target::*; + +//------------------------------------------ + +const USAGE: &str = "cache_metadata_size 0.9.0 +Estimate the size of the metadata device needed for a given configuration. + +USAGE: + cache_metadata_size [OPTIONS] <--device-size --block-size | --nr-blocks > + +FLAGS: + -h, --help Prints help information + -V, --version Prints version information + +OPTIONS: + --block-size Specify the size of each cache block + --device-size Specify total size of the fast device used in the cache + --max-hint-width Specity the per-block hint width [default: 4] + --nr-blocks Specify the number of cache blocks"; + +//------------------------------------------ + +struct CacheMetadataSize; + +impl<'a> Program<'a> for CacheMetadataSize { + fn name() -> &'a str { + "cache_metadata_size" + } + + fn cmd(args: I) -> Command + where + I: IntoIterator, + I::Item: Into, + { + cache_metadata_size_cmd(args) + } + + fn usage() -> &'a str { + USAGE + } + + fn arg_type() -> ArgType { + ArgType::InputArg + } + + fn bad_option_hint(option: &str) -> String { + msg::bad_option_hint(option) + } +} + +//------------------------------------------ + +test_accepts_help!(CacheMetadataSize); +test_accepts_version!(CacheMetadataSize); +test_rejects_bad_option!(CacheMetadataSize); + +//------------------------------------------ + +#[test] +fn no_args() -> Result<()> { + let _stderr = run_fail(cache_metadata_size_cmd([""; 0]))?; + Ok(()) +} + +#[test] +fn device_size_only() -> Result<()> { + let _stderr = run_fail(cache_metadata_size_cmd(args!["--device-size", "204800"]))?; + Ok(()) +} + +#[test] +fn block_size_only() -> Result<()> { + let _stderr = run_fail(cache_metadata_size_cmd(args!["--block-size", "128"]))?; + Ok(()) +} + +/* +#[test] +fn conradictory_info_fails() -> Result<()> { + let stderr = run_fail(cache_metadata_size_cmd( + args![ + "--device-size", + "102400", + "--block-size", + "1000", + "--nr-blocks", + "6" + ], + ))?; + assert_eq!(stderr, "Contradictory arguments given, --nr-blocks doesn't match the --device-size and --block-size."); + Ok(()) +} + +#[test] +fn all_args_agree() -> Result<()> { + let out = run_ok_raw(cache_metadata_size_cmd( + args![ + "--device-size", + "102400", + "--block-size", + "100", + "--nr-blocks", + "1024" + ], + ))?; + let stdout = std::str::from_utf8(&out.stdout[..]) + .unwrap() + .trim_end_matches(|c| c == '\n' || c == '\r') + .to_string(); + assert_eq!(stdout, "8248 sectors"); + assert_eq!(out.stderr.len(), 0); + Ok(()) +} +*/ + +#[test] +fn dev_size_and_nr_blocks_conflicts() -> Result<()> { + run_fail(cache_metadata_size_cmd(args![ + "--device-size", + "102400", + "--nr-blocks", + "1024" + ]))?; + Ok(()) +} + +#[test] +fn block_size_and_nr_blocks_conflicts() -> Result<()> { + run_fail(cache_metadata_size_cmd(args![ + "--block-size", + "100", + "--nr-blocks", + "1024" + ]))?; + Ok(()) +} + +#[test] +fn nr_blocks_alone() -> Result<()> { + let out = run_ok_raw(cache_metadata_size_cmd(args!["--nr-blocks", "1024"]))?; + let stdout = std::str::from_utf8(&out.stdout[..]) + .unwrap() + .trim_end_matches(|c| c == '\n' || c == '\r') + .to_string(); + assert_eq!(stdout, "8248 sectors"); + assert_eq!(out.stderr.len(), 0); + Ok(()) +} + +#[test] +fn dev_size_and_block_size_succeeds() -> Result<()> { + let out = run_ok_raw(cache_metadata_size_cmd(args![ + "--device-size", + "102400", + "--block-size", + "100" + ]))?; + let stdout = std::str::from_utf8(&out.stdout[..]) + .unwrap() + .trim_end_matches(|c| c == '\n' || c == '\r') + .to_string(); + assert_eq!(stdout, "8248 sectors"); + assert_eq!(out.stderr.len(), 0); + Ok(()) +} + +#[test] +fn large_nr_blocks() -> Result<()> { + let out = run_ok_raw(cache_metadata_size_cmd(args!["--nr-blocks", "67108864"]))?; + let stdout = std::str::from_utf8(&out.stdout[..]) + .unwrap() + .trim_end_matches(|c| c == '\n' || c == '\r') + .to_string(); + assert_eq!(stdout, "3678208 sectors"); + assert_eq!(out.stderr.len(), 0); + Ok(()) +} + +//------------------------------------------ diff --git a/tests/common/process.rs b/tests/common/process.rs index 790b9ef..1cbea92 100644 --- a/tests/common/process.rs +++ b/tests/common/process.rs @@ -100,7 +100,11 @@ pub fn run_fail(command: Command) -> Result { let output = command.unchecked().run()?; log_output(&output); assert!(!output.status.success()); - let stderr = std::str::from_utf8(&output.stderr[..]).unwrap().to_string(); + let stderr = std::str::from_utf8(&output.stderr[..]) + .unwrap() + .trim_end_matches(|c| c == '\n' || c == '\r') + .to_string(); + Ok(stderr) } diff --git a/tests/common/target.rs b/tests/common/target.rs index fa8ffc0..f688c22 100644 --- a/tests/common/target.rs +++ b/tests/common/target.rs @@ -143,6 +143,14 @@ where rust_cmd("cache_dump", args) } +pub fn cache_metadata_size_cmd(args: I) -> Command +where + I: IntoIterator, + I::Item: Into, +{ + rust_cmd("cache_metadata_size", args) +} + pub fn cache_restore_cmd(args: I) -> Command where I: IntoIterator,