qcow: Make unit tests pass

Rather than relying on shared memory for a temporary file for QCOW
testing instead use tempfile crate to get a temporary file. The vector
cache tests also need a trivial update after the refactor.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2019-07-16 10:34:28 +01:00 committed by Samuel Ortiz
parent 9a17871630
commit a50c54671c
3 changed files with 10 additions and 9 deletions

View File

@ -14,3 +14,6 @@ libc = "*"
log = "*"
remain = "*"
vmm-sys-util = { git = "https://github.com/rust-vmm/vmm-sys-util" }
[dev-dependencies]
tempfile = "*"

View File

@ -1623,7 +1623,7 @@ mod tests {
use super::*;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};
use sys_util::SharedMemory;
use tempfile::tempfile;
fn valid_header() -> Vec<u8> {
vec![
@ -1652,8 +1652,7 @@ mod tests {
where
F: FnMut(File),
{
let shm = SharedMemory::new(None).unwrap();
let mut disk_file: File = shm.into();
let mut disk_file: File = tempfile().unwrap();
disk_file.write_all(&header).unwrap();
disk_file.set_len(0x5_0000).unwrap();
disk_file.seek(SeekFrom::Start(0)).unwrap();
@ -1665,8 +1664,8 @@ mod tests {
where
F: FnMut(QcowFile),
{
let shm = SharedMemory::new(None).unwrap();
let qcow_file = QcowFile::new(shm.into(), file_size).unwrap();
let tmp = tempfile().unwrap();
let qcow_file = QcowFile::new(tmp, file_size).unwrap();
testfn(qcow_file); // File closed when the function exits.
}
@ -1674,8 +1673,7 @@ mod tests {
#[test]
fn default_header() {
let header = QcowHeader::create_for_size(0x10_0000);
let shm = SharedMemory::new(None).unwrap();
let mut disk_file: File = shm.into();
let mut disk_file: File = tempfile().unwrap();
header
.write_to(&mut disk_file)
.expect("Failed to write header to shm.");

View File

@ -178,8 +178,8 @@ mod tests {
// Check that three of the four items inserted are still there and that the most recently
// inserted is one of them.
let num_items = (0..=3).filter(|k| cache.contains_key(&k)).count();
let num_items = (0..=3).filter(|k| cache.contains_key(*k)).count();
assert_eq!(num_items, 3);
assert!(cache.contains_key(&3));
assert!(cache.contains_key(3));
}
}