block: qcow: Port internal tests to QcowDisk

Port the internal/mod.rs tests from QcowFile to QcowDisk so
the surviving tests exercise the code paths that are hit when used via
QcowDisk (and thus virtio-block).

Assisted-by: Claude:claude-opus-4-7
Signed-off-by: Rob Bradford <rbradford@meta.com>
This commit is contained in:
Rob Bradford
2026-06-05 18:43:00 +01:00
parent cf55698168
commit 9da113ff1f
3 changed files with 456 additions and 1793 deletions

View File

@@ -351,6 +351,24 @@ impl QcowMetadata {
pub fn decoder(&self) -> Arc<dyn Decoder> {
Arc::clone(&self.decoder)
}
#[cfg(test)]
pub fn header(&self) -> QcowHeader {
self.inner.read().unwrap().header.clone()
}
#[cfg(test)]
pub fn cluster_refcount(&self, address: u64) -> io::Result<u64> {
let mut inner = self.inner.write().unwrap();
let QcowState {
refcounts,
raw_file,
..
} = &mut *inner;
refcounts
.get_cluster_refcount(raw_file, address)
.map_err(|e| io::Error::other(format!("get_cluster_refcount: {e}")))
}
}
impl QcowState {

File diff suppressed because it is too large Load Diff

View File

@@ -123,6 +123,27 @@ impl QcowDisk {
.unwrap();
while async_io.next_completed_request().is_some() {}
}
/// Synchronous read convenience for tests and benchmarks.
#[cfg(test)]
pub fn read_all_at(&self, offset: u64, len: usize) -> Vec<u8> {
let mut async_io = self.create_async_io(1).unwrap();
let mem = Arc::new(GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0), len)]).unwrap());
let range = [(GuestAddress(0), len as u32)];
let target = GuestMemoryTarget::new(Arc::clone(&mem), &range).unwrap();
async_io
.read_to_memory(offset as libc::off_t, target, 0)
.unwrap();
while async_io.next_completed_request().is_some() {}
let mut buf = vec![0u8; len];
mem.read_slice(&mut buf, GuestAddress(0)).unwrap();
buf
}
#[cfg(test)]
pub(crate) fn metadata(&self) -> &QcowMetadata {
&self.metadata
}
}
/// Writes a fresh qcow2 layout into `file`