Evan Benn has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/68739 )
Change subject: flashrom_tester: Use tempdir crate for test data ......................................................................
flashrom_tester: Use tempdir crate for test data
Use tempdir crate to create a tempdir that is deleted when the tester closes.
BUG=b:243460685 BRANCH=None TEST=/usr/bin/flashrom_tester --flashrom_binary /usr/sbin/flashrom host
Change-Id: I14089ef69c3f509cb6f01cc44c7924c244ab7d73 Signed-off-by: Evan Benn evanbenn@chromium.org --- M util/flashrom_tester/Cargo.toml M util/flashrom_tester/src/tester.rs M util/flashrom_tester/src/tests.rs 3 files changed, 31 insertions(+), 8 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/39/68739/1
diff --git a/util/flashrom_tester/Cargo.toml b/util/flashrom_tester/Cargo.toml index b57f04e..364a223 100644 --- a/util/flashrom_tester/Cargo.toml +++ b/util/flashrom_tester/Cargo.toml @@ -23,6 +23,7 @@ rand = "0.6.4" serde_json = "1" sys-info = "0.9" +tempdir = "0.3.7"
[build-dependencies] built = { version = "0.5", features = ["chrono"] } diff --git a/util/flashrom_tester/src/tester.rs b/util/flashrom_tester/src/tester.rs index 6ce3889..8324ade 100644 --- a/util/flashrom_tester/src/tester.rs +++ b/util/flashrom_tester/src/tester.rs @@ -46,6 +46,7 @@ use std::path::PathBuf; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Mutex; +use tempdir::TempDir;
// type-signature comes from the return type of lib.rs workers. type TestError = Box<dyn std::error::Error>; @@ -67,6 +68,8 @@ random_data: PathBuf, /// The path to a file containing layout data. pub layout_file: PathBuf, + /// A directory for temporary test data, including the above files. + pub tmp_dir: TempDir, }
impl<'a> TestEnv<'a> { @@ -76,14 +79,16 @@ print_layout: bool, ) -> Result<Self, FlashromError> { let rom_sz = cmd.get_size()?; + let tmp_dir = TempDir::new("flashrom_tester").map_err(|e| e.to_string())?; let out = TestEnv { chip_type, cmd, layout: utils::get_layout_sizes(rom_sz)?, wp: WriteProtectState::from_hardware(cmd, chip_type)?, - original_flash_contents: "/tmp/flashrom_tester_golden.bin".into(), - random_data: "/tmp/random_content.bin".into(), - layout_file: create_layout_file(rom_sz, Path::new("/tmp/"), print_layout), + original_flash_contents: tmp_dir.path().join("flashrom_tester_golden.bin"), + random_data: tmp_dir.path().join("random_content.bin"), + layout_file: create_layout_file(rom_sz, tmp_dir.path(), print_layout), + tmp_dir, };
info!("Stashing golden image for verification/recovery on completion"); diff --git a/util/flashrom_tester/src/tests.rs b/util/flashrom_tester/src/tests.rs index af34208..94fe14d 100644 --- a/util/flashrom_tester/src/tests.rs +++ b/util/flashrom_tester/src/tests.rs @@ -43,8 +43,6 @@ use std::io::BufRead; use std::sync::atomic::AtomicBool;
-const ELOG_FILE: &str = "/tmp/elog.file"; - /// Iterate over tests, yielding only those tests with names matching filter_names. /// /// If filter_names is None, all tests will be run. None is distinct from Some(∅); @@ -223,15 +221,17 @@ env.ensure_golden()?;
const ELOG_RW_REGION_NAME: &str = "RW_ELOG"; + let elog_file = env.tmp_dir.path().join("elog.file"); + env.cmd - .read_region_into_file(ELOG_FILE.as_ref(), ELOG_RW_REGION_NAME)?; + .read_region_into_file(&elog_file, ELOG_RW_REGION_NAME)?;
// Just checking for the magic numer // TODO: improve this test to read the events - if fs::metadata(ELOG_FILE)?.len() < 4 { + if fs::metadata(&elog_file)?.len() < 4 { return Err("ELOG contained no data".into()); } - let data = fs::read(ELOG_FILE)?; + let data = fs::read(&elog_file)?; if u32::from_le_bytes(data[0..4].try_into()?) != 0x474f4c45 { return Err("ELOG had bad magic number".into()); }