From 9253117132ac07d6f84b6b47ea746a083639a348 Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Tue, 31 Aug 2021 22:28:12 +0800 Subject: [PATCH] [tests] Add cache fixtures - Generate temp xml and metadata files - Correct existing tests to use cache fixtures - Fix cache xml generation --- tests/cache_check.rs | 3 ++- tests/cache_dump.rs | 4 ++-- tests/common/cache.rs | 35 +++++++++++++++++++++++++++++ tests/common/cache_xml_generator.rs | 16 ++++++------- tests/common/mod.rs | 1 + 5 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 tests/common/cache.rs diff --git a/tests/cache_check.rs b/tests/cache_check.rs index 3aa724f..b676742 100644 --- a/tests/cache_check.rs +++ b/tests/cache_check.rs @@ -2,6 +2,7 @@ use anyhow::Result; mod common; +use common::cache::*; use common::common_args::*; use common::fixture::*; use common::input_arg::*; @@ -51,7 +52,7 @@ impl<'a> Program<'a> for CacheCheck { impl<'a> InputProgram<'a> for CacheCheck { fn mk_valid_input(td: &mut TestDir) -> Result { - common::thin::mk_valid_md(td) // FIXME: create cache metadata + mk_valid_md(td) } fn file_not_found() -> &'a str { diff --git a/tests/cache_dump.rs b/tests/cache_dump.rs index f1b0f25..da85b5c 100644 --- a/tests/cache_dump.rs +++ b/tests/cache_dump.rs @@ -2,9 +2,9 @@ use anyhow::Result; mod common; +use common::cache::*; use common::common_args::*; use common::input_arg::*; - use common::program::*; use common::target::*; use common::test_dir::*; @@ -46,7 +46,7 @@ impl<'a> Program<'a> for CacheDump { impl<'a> InputProgram<'a> for CacheDump { fn mk_valid_input(td: &mut TestDir) -> Result { - common::thin::mk_valid_md(td) // FIXME: generate cache metadata + mk_valid_md(td) } fn file_not_found() -> &'a str { diff --git a/tests/common/cache.rs b/tests/common/cache.rs new file mode 100644 index 0000000..449f00f --- /dev/null +++ b/tests/common/cache.rs @@ -0,0 +1,35 @@ +use anyhow::Result; +use std::path::PathBuf; + +use thinp::file_utils; +//use thinp::io_engine::*; + +use crate::args; +use crate::common::cache_xml_generator::{write_xml, CacheGen}; +use crate::common::process::*; +use crate::common::target::*; +use crate::common::test_dir::TestDir; + +//----------------------------------------------- + +pub fn mk_valid_xml(td: &mut TestDir) -> Result { + let xml = td.mk_path("meta.xml"); + let mut gen = CacheGen::new(512, 128, 1024, 80, 50); // bs, cblocks, oblocks, res, dirty + write_xml(&xml, &mut gen)?; + Ok(xml) +} + +pub fn mk_valid_md(td: &mut TestDir) -> Result { + let xml = td.mk_path("meta.xml"); + let md = td.mk_path("meta.bin"); + + let mut gen = CacheGen::new(512, 4096, 32768, 80, 50); + write_xml(&xml, &mut gen)?; + + let _file = file_utils::create_sized_file(&md, 4096 * 4096); + run_ok(CACHE_RESTORE, args!["-i", &xml, "-o", &md])?; + + Ok(md) +} + +//----------------------------------------------- diff --git a/tests/common/cache_xml_generator.rs b/tests/common/cache_xml_generator.rs index e7df242..27c9c6f 100644 --- a/tests/common/cache_xml_generator.rs +++ b/tests/common/cache_xml_generator.rs @@ -60,26 +60,26 @@ impl XmlGen for CacheGen { hint_width: 4, })?; - let mut cblocks = Vec::new(); - for n in 0..self.nr_cache_blocks { - cblocks.push(n); - } + let nr_resident = (self.nr_cache_blocks * self.percent_resident as u32) / 100u32; + let mut cblocks = (0..self.nr_cache_blocks).collect::>(); cblocks.shuffle(&mut rand::thread_rng()); + cblocks.truncate(nr_resident as usize); + cblocks.sort(); v.mappings_b()?; { - let nr_resident = (self.nr_cache_blocks * 100u32) / (self.percent_resident as u32); let mut used = HashSet::new(); - for n in 0..nr_resident { + let mut rng = rand::thread_rng(); + for cblock in cblocks { let mut oblock = 0u64; while used.contains(&oblock) { - oblock = rand::thread_rng().gen(); + oblock = rng.gen_range(0..self.nr_origin_blocks); } used.insert(oblock); // FIXME: dirty should vary v.mapping(&ir::Map { - cblock: cblocks[n as usize], + cblock, oblock, dirty: false, })?; diff --git a/tests/common/mod.rs b/tests/common/mod.rs index a8e1717..93f6db4 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -2,6 +2,7 @@ // https://github.com/rust-lang/rust/issues/46379 #![allow(dead_code)] +pub mod cache; pub mod cache_xml_generator; pub mod common_args; pub mod fixture;