mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-28 16:27:50 +00:00
Merge pull request #9313 from ChengyuZhu6/rtest
agent: Refactor unit tests to leverage rstest for parameterization
This commit is contained in:
commit
4029d154ba
@ -182,6 +182,7 @@ mod tests {
|
||||
use super::{Namespace, NamespaceType};
|
||||
use crate::mount::remove_mounts;
|
||||
use nix::sched::CloneFlags;
|
||||
use rstest::rstest;
|
||||
use tempfile::Builder;
|
||||
use test_utils::skip_if_not_root;
|
||||
|
||||
@ -226,26 +227,23 @@ mod tests {
|
||||
assert!(ns_pid.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_namespace_type() {
|
||||
let ipc = NamespaceType::Ipc;
|
||||
assert_eq!("ipc", ipc.get());
|
||||
assert_eq!(CloneFlags::CLONE_NEWIPC, ipc.get_flags());
|
||||
|
||||
let uts = NamespaceType::Uts;
|
||||
assert_eq!("uts", uts.get());
|
||||
assert_eq!(CloneFlags::CLONE_NEWUTS, uts.get_flags());
|
||||
|
||||
let pid = NamespaceType::Pid;
|
||||
assert_eq!("pid", pid.get());
|
||||
assert_eq!(CloneFlags::CLONE_NEWPID, pid.get_flags());
|
||||
#[rstest]
|
||||
#[case::ipc(NamespaceType::Ipc, "ipc", CloneFlags::CLONE_NEWIPC)]
|
||||
#[case::uts(NamespaceType::Uts, "uts", CloneFlags::CLONE_NEWUTS)]
|
||||
#[case::pid(NamespaceType::Pid, "pid", CloneFlags::CLONE_NEWPID)]
|
||||
fn test_namespace_type(
|
||||
#[case] ns_type: NamespaceType,
|
||||
#[case] ns_name: &str,
|
||||
#[case] ns_flag: CloneFlags,
|
||||
) {
|
||||
assert_eq!(ns_name, ns_type.get());
|
||||
assert_eq!(ns_flag, ns_type.get_flags());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new() {
|
||||
// Create dummy logger and temp folder.
|
||||
let logger = slog::Logger::root(slog::Discard, o!());
|
||||
|
||||
let ns_ipc = Namespace::new(&logger);
|
||||
assert_eq!(NamespaceType::Ipc, ns_ipc.ns_type);
|
||||
}
|
||||
@ -301,65 +299,20 @@ mod tests {
|
||||
assert_eq!(ns_root.persistent_ns_dir, tmpdir.path().to_str().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_namespace_type_get() {
|
||||
#[derive(Debug)]
|
||||
struct TestData<'a> {
|
||||
ns_type: NamespaceType,
|
||||
str: &'a str,
|
||||
#[rstest]
|
||||
#[case::namespace_type_get_ipc(NamespaceType::Ipc, "ipc")]
|
||||
#[case::namespace_type_get_uts(NamespaceType::Uts, "uts")]
|
||||
#[case::namespace_type_get_pid(NamespaceType::Pid, "pid")]
|
||||
fn test_namespace_type_get(#[case] ns_type: NamespaceType, #[case] ns_name: &str) {
|
||||
assert_eq!(ns_name, ns_type.get())
|
||||
}
|
||||
|
||||
let tests = &[
|
||||
TestData {
|
||||
ns_type: NamespaceType::Ipc,
|
||||
str: "ipc",
|
||||
},
|
||||
TestData {
|
||||
ns_type: NamespaceType::Uts,
|
||||
str: "uts",
|
||||
},
|
||||
TestData {
|
||||
ns_type: NamespaceType::Pid,
|
||||
str: "pid",
|
||||
},
|
||||
];
|
||||
|
||||
#[rstest]
|
||||
#[case::namespace_type_get_flags_ipc(NamespaceType::Ipc, CloneFlags::CLONE_NEWIPC)]
|
||||
#[case::namespace_type_get_flags_uts(NamespaceType::Uts, CloneFlags::CLONE_NEWUTS)]
|
||||
#[case::namespace_type_get_flags_pid(NamespaceType::Pid, CloneFlags::CLONE_NEWPID)]
|
||||
fn test_namespace_type_get_flags(#[case] ns_type: NamespaceType, #[case] ns_flag: CloneFlags) {
|
||||
// Run the tests
|
||||
for (i, d) in tests.iter().enumerate() {
|
||||
// Create a string containing details of the test
|
||||
let msg = format!("test[{}]: {:?}", i, d);
|
||||
assert_eq!(d.str, d.ns_type.get(), "{}", msg)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_namespace_type_get_flags() {
|
||||
#[derive(Debug)]
|
||||
struct TestData {
|
||||
ns_type: NamespaceType,
|
||||
ns_flag: CloneFlags,
|
||||
}
|
||||
|
||||
let tests = &[
|
||||
TestData {
|
||||
ns_type: NamespaceType::Ipc,
|
||||
ns_flag: CloneFlags::CLONE_NEWIPC,
|
||||
},
|
||||
TestData {
|
||||
ns_type: NamespaceType::Uts,
|
||||
ns_flag: CloneFlags::CLONE_NEWUTS,
|
||||
},
|
||||
TestData {
|
||||
ns_type: NamespaceType::Pid,
|
||||
ns_flag: CloneFlags::CLONE_NEWPID,
|
||||
},
|
||||
];
|
||||
|
||||
// Run the tests
|
||||
for (i, d) in tests.iter().enumerate() {
|
||||
// Create a string containing details of the test
|
||||
let msg = format!("test[{}]: {:?}", i, d);
|
||||
assert_eq!(d.ns_flag, d.ns_type.get_flags(), "{}", msg)
|
||||
}
|
||||
assert_eq!(ns_flag, ns_type.get_flags())
|
||||
}
|
||||
}
|
||||
|
@ -75,6 +75,7 @@ pub async fn get_vsock_stream(fd: RawFd) -> Result<VsockStream> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use rstest::rstest;
|
||||
use std::io;
|
||||
use std::io::Cursor;
|
||||
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)]
|
||||
async fn test_interruptable_io_copier_reader() {
|
||||
#[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);
|
||||
|
||||
async fn test_interruptable_io_copier_reader(#[case] reader_value: String) {
|
||||
let (tx, rx) = channel(true);
|
||||
let reader = Cursor::new(d.reader_value.clone());
|
||||
let reader = Cursor::new(reader_value.clone());
|
||||
let writer = BufWriter::new();
|
||||
|
||||
// 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
|
||||
// copier will quickly read zero and kill the task, closing the
|
||||
// Receiver.
|
||||
assert!(tx.is_closed(), "{}", msg);
|
||||
assert!(tx.is_closed());
|
||||
|
||||
let spawn_result: std::result::Result<
|
||||
std::result::Result<u64, std::io::Error>,
|
||||
JoinError,
|
||||
>;
|
||||
let spawn_result: std::result::Result<std::result::Result<u64, std::io::Error>, JoinError>;
|
||||
|
||||
select! {
|
||||
res = handle => spawn_result = res,
|
||||
@ -249,11 +218,10 @@ mod tests {
|
||||
assert!(result.is_ok());
|
||||
|
||||
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();
|
||||
assert_eq!(value, d.reader_value, "{}", msg);
|
||||
}
|
||||
assert_eq!(value, reader_value);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
|
Loading…
Reference in New Issue
Block a user