mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-09-02 09:24:35 +00:00
agent:util: Refactor the unit tests to leverage rstest
Refactor the unit tests in util.rs to leverage rstest for parameterization. Fixes: #9314 Signed-off-by: ChengyuZhu6 <chengyu.zhu@intel.com>
This commit is contained in:
@@ -75,6 +75,7 @@ pub async fn get_vsock_stream(fd: RawFd) -> Result<VsockStream> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use rstest::rstest;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
@@ -172,49 +173,20 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[case("".into())]
|
||||||
|
#[case("a".into())]
|
||||||
|
#[case("foo".into())]
|
||||||
|
#[case("b".repeat(BUF_SIZE - 1))]
|
||||||
|
#[case("c".repeat(BUF_SIZE))]
|
||||||
|
#[case("d".repeat(BUF_SIZE + 1))]
|
||||||
|
#[case("e".repeat((2 * BUF_SIZE) - 1))]
|
||||||
|
#[case("f".repeat(2 * BUF_SIZE))]
|
||||||
|
#[case("g".repeat((2 * BUF_SIZE) + 1))]
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
async fn test_interruptable_io_copier_reader() {
|
async fn test_interruptable_io_copier_reader(#[case] reader_value: String) {
|
||||||
#[derive(Debug)]
|
|
||||||
struct TestData {
|
|
||||||
reader_value: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
let tests = &[
|
|
||||||
TestData {
|
|
||||||
reader_value: "".into(),
|
|
||||||
},
|
|
||||||
TestData {
|
|
||||||
reader_value: "a".into(),
|
|
||||||
},
|
|
||||||
TestData {
|
|
||||||
reader_value: "foo".into(),
|
|
||||||
},
|
|
||||||
TestData {
|
|
||||||
reader_value: "b".repeat(BUF_SIZE - 1),
|
|
||||||
},
|
|
||||||
TestData {
|
|
||||||
reader_value: "c".repeat(BUF_SIZE),
|
|
||||||
},
|
|
||||||
TestData {
|
|
||||||
reader_value: "d".repeat(BUF_SIZE + 1),
|
|
||||||
},
|
|
||||||
TestData {
|
|
||||||
reader_value: "e".repeat((2 * BUF_SIZE) - 1),
|
|
||||||
},
|
|
||||||
TestData {
|
|
||||||
reader_value: "f".repeat(2 * BUF_SIZE),
|
|
||||||
},
|
|
||||||
TestData {
|
|
||||||
reader_value: "g".repeat((2 * BUF_SIZE) + 1),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
for (i, d) in tests.iter().enumerate() {
|
|
||||||
// Create a string containing details of the test
|
|
||||||
let msg = format!("test[{}]: {:?}", i, d);
|
|
||||||
|
|
||||||
let (tx, rx) = channel(true);
|
let (tx, rx) = channel(true);
|
||||||
let reader = Cursor::new(d.reader_value.clone());
|
let reader = Cursor::new(reader_value.clone());
|
||||||
let writer = BufWriter::new();
|
let writer = BufWriter::new();
|
||||||
|
|
||||||
// XXX: Pass a copy of the writer to the copier to allow the
|
// XXX: Pass a copy of the writer to the copier to allow the
|
||||||
@@ -230,12 +202,9 @@ mod tests {
|
|||||||
// Since the readers only specify a small number of bytes, the
|
// Since the readers only specify a small number of bytes, the
|
||||||
// copier will quickly read zero and kill the task, closing the
|
// copier will quickly read zero and kill the task, closing the
|
||||||
// Receiver.
|
// Receiver.
|
||||||
assert!(tx.is_closed(), "{}", msg);
|
assert!(tx.is_closed());
|
||||||
|
|
||||||
let spawn_result: std::result::Result<
|
let spawn_result: std::result::Result<std::result::Result<u64, std::io::Error>, JoinError>;
|
||||||
std::result::Result<u64, std::io::Error>,
|
|
||||||
JoinError,
|
|
||||||
>;
|
|
||||||
|
|
||||||
select! {
|
select! {
|
||||||
res = handle => spawn_result = res,
|
res = handle => spawn_result = res,
|
||||||
@@ -249,11 +218,10 @@ mod tests {
|
|||||||
assert!(result.is_ok());
|
assert!(result.is_ok());
|
||||||
|
|
||||||
let byte_count = result.unwrap() as usize;
|
let byte_count = result.unwrap() as usize;
|
||||||
assert_eq!(byte_count, d.reader_value.len(), "{}", msg);
|
assert_eq!(byte_count, reader_value.len());
|
||||||
|
|
||||||
let value = writer.to_string();
|
let value = writer.to_string();
|
||||||
assert_eq!(value, d.reader_value, "{}", msg);
|
assert_eq!(value, reader_value);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
Reference in New Issue
Block a user