mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
pvapconfig/tests: use a temporary directory for tests
Use the `TemporaryDirectory` type to create a temporary directory. This
type has the advantage that the directory is automatically removed once
it goes out of scope. The old implementation in test_sysfs_write_i32
leaks a directory if a previous subtest failed. Also, using a temporary
directory for the LockFile test fixes the following error:
$ RUST_BACKTRACE=1 cargo test -- helper::tests::test_lockfile
...
running 1 test
test helper::tests::test_lockfile ... FAILED
failures:
---- helper::tests::test_lockfile stdout ----
thread 'helper::tests::test_lockfile' panicked at pvapconfig/src/helper.rs:265:9:
assertion failed: r1.is_ok()
stack backtrace:
0: rust_begin_unwind
at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/panicking.rs:597:5
1: core::panicking::panic_fmt
at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/panicking.rs:72:14
2: core::panicking::panic
at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/panicking.rs:127:5
3: pvapconfig::helper::tests::test_lockfile
at ./src/helper.rs:265:9
4: pvapconfig::helper::tests::test_lockfile::{{closure}}
at ./src/helper.rs:263:24
5: core::ops::function::FnOnce::call_once
at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/ops/function.rs:250:5
6: core::ops::function::FnOnce::call_once
at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Reviewed-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-By: Harald Freudenberger <freude@de.ibm.com>
Signed-off-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
committed by
Jan Höppner
parent
e56acf4f14
commit
e40a3e0621
@@ -198,8 +198,8 @@ impl Drop for LockFile {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
use pv_core::misc::TemporaryDirectory;
|
||||
|
||||
// Only very simple tests
|
||||
|
||||
@@ -245,28 +245,35 @@ mod tests {
|
||||
}
|
||||
#[test]
|
||||
fn test_sysfs_write_i32() {
|
||||
const TEST_PATH: &str = "/tmp/test_sysfs_write_i32";
|
||||
let mut file = File::create(TEST_PATH).unwrap();
|
||||
let temp_dir =
|
||||
TemporaryDirectory::new("pvtests").expect("creating a temporary directory should work");
|
||||
let test_path = temp_dir.path().join("test");
|
||||
let test_path = test_path.as_os_str().to_str().expect("should work");
|
||||
let mut file = File::create(test_path).unwrap();
|
||||
let _ = file.write_all(b"XYZ");
|
||||
drop(file);
|
||||
let r = sysfs_read_i32(TEST_PATH);
|
||||
let r = sysfs_read_i32(test_path);
|
||||
assert!(r.is_err());
|
||||
let r = sysfs_write_i32(TEST_PATH, 999);
|
||||
let r = sysfs_write_i32(test_path, 999);
|
||||
assert!(r.is_ok());
|
||||
let r = sysfs_read_i32(TEST_PATH);
|
||||
let r = sysfs_read_i32(test_path);
|
||||
assert!(r.is_ok());
|
||||
let v = r.unwrap();
|
||||
assert!(v == 999);
|
||||
let _ = fs::remove_file(TEST_PATH);
|
||||
}
|
||||
#[test]
|
||||
fn test_lockfile() {
|
||||
let r1 = LockFile::try_lock(PATH_PVAPCONFIG_LOCK);
|
||||
let temp_dir =
|
||||
TemporaryDirectory::new("pvtests").expect("creating a temporary directory should work");
|
||||
let file_path = temp_dir.path().join("my.lock");
|
||||
let file_path_str = file_path.to_str().expect("should work");
|
||||
|
||||
let r1 = LockFile::try_lock(file_path_str);
|
||||
assert!(r1.is_ok());
|
||||
let r2 = LockFile::try_lock(PATH_PVAPCONFIG_LOCK);
|
||||
let r2 = LockFile::try_lock(file_path_str);
|
||||
assert!(r2.is_err());
|
||||
drop(r1);
|
||||
let r3 = LockFile::try_lock(PATH_PVAPCONFIG_LOCK);
|
||||
let r3 = LockFile::try_lock(file_path_str);
|
||||
assert!(r3.is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user