mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-29 08:47:56 +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 super::{Namespace, NamespaceType};
|
||||||
use crate::mount::remove_mounts;
|
use crate::mount::remove_mounts;
|
||||||
use nix::sched::CloneFlags;
|
use nix::sched::CloneFlags;
|
||||||
|
use rstest::rstest;
|
||||||
use tempfile::Builder;
|
use tempfile::Builder;
|
||||||
use test_utils::skip_if_not_root;
|
use test_utils::skip_if_not_root;
|
||||||
|
|
||||||
@ -226,26 +227,23 @@ mod tests {
|
|||||||
assert!(ns_pid.is_err());
|
assert!(ns_pid.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[rstest]
|
||||||
fn test_namespace_type() {
|
#[case::ipc(NamespaceType::Ipc, "ipc", CloneFlags::CLONE_NEWIPC)]
|
||||||
let ipc = NamespaceType::Ipc;
|
#[case::uts(NamespaceType::Uts, "uts", CloneFlags::CLONE_NEWUTS)]
|
||||||
assert_eq!("ipc", ipc.get());
|
#[case::pid(NamespaceType::Pid, "pid", CloneFlags::CLONE_NEWPID)]
|
||||||
assert_eq!(CloneFlags::CLONE_NEWIPC, ipc.get_flags());
|
fn test_namespace_type(
|
||||||
|
#[case] ns_type: NamespaceType,
|
||||||
let uts = NamespaceType::Uts;
|
#[case] ns_name: &str,
|
||||||
assert_eq!("uts", uts.get());
|
#[case] ns_flag: CloneFlags,
|
||||||
assert_eq!(CloneFlags::CLONE_NEWUTS, uts.get_flags());
|
) {
|
||||||
|
assert_eq!(ns_name, ns_type.get());
|
||||||
let pid = NamespaceType::Pid;
|
assert_eq!(ns_flag, ns_type.get_flags());
|
||||||
assert_eq!("pid", pid.get());
|
|
||||||
assert_eq!(CloneFlags::CLONE_NEWPID, pid.get_flags());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_new() {
|
fn test_new() {
|
||||||
// Create dummy logger and temp folder.
|
// Create dummy logger and temp folder.
|
||||||
let logger = slog::Logger::root(slog::Discard, o!());
|
let logger = slog::Logger::root(slog::Discard, o!());
|
||||||
|
|
||||||
let ns_ipc = Namespace::new(&logger);
|
let ns_ipc = Namespace::new(&logger);
|
||||||
assert_eq!(NamespaceType::Ipc, ns_ipc.ns_type);
|
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());
|
assert_eq!(ns_root.persistent_ns_dir, tmpdir.path().to_str().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[rstest]
|
||||||
fn test_namespace_type_get() {
|
#[case::namespace_type_get_ipc(NamespaceType::Ipc, "ipc")]
|
||||||
#[derive(Debug)]
|
#[case::namespace_type_get_uts(NamespaceType::Uts, "uts")]
|
||||||
struct TestData<'a> {
|
#[case::namespace_type_get_pid(NamespaceType::Pid, "pid")]
|
||||||
ns_type: NamespaceType,
|
fn test_namespace_type_get(#[case] ns_type: NamespaceType, #[case] ns_name: &str) {
|
||||||
str: &'a str,
|
assert_eq!(ns_name, ns_type.get())
|
||||||
}
|
}
|
||||||
|
|
||||||
let tests = &[
|
#[rstest]
|
||||||
TestData {
|
#[case::namespace_type_get_flags_ipc(NamespaceType::Ipc, CloneFlags::CLONE_NEWIPC)]
|
||||||
ns_type: NamespaceType::Ipc,
|
#[case::namespace_type_get_flags_uts(NamespaceType::Uts, CloneFlags::CLONE_NEWUTS)]
|
||||||
str: "ipc",
|
#[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) {
|
||||||
TestData {
|
|
||||||
ns_type: NamespaceType::Uts,
|
|
||||||
str: "uts",
|
|
||||||
},
|
|
||||||
TestData {
|
|
||||||
ns_type: NamespaceType::Pid,
|
|
||||||
str: "pid",
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
// Run the tests
|
// Run the tests
|
||||||
for (i, d) in tests.iter().enumerate() {
|
assert_eq!(ns_flag, ns_type.get_flags())
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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)]
|
||||||
|
Loading…
Reference in New Issue
Block a user