mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-13 15:14:08 +00:00
agent: fix tests for async functions
Use tokio::test to test async functions. Signed-off-by: Tim Zhang <tim@hyper.sh>
This commit is contained in:
parent
9f79ddb9df
commit
f3bd439465
@ -1825,27 +1825,34 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_linux_container<U, F: FnOnce(LinuxContainer) -> Result<U, anyhow::Error>>(
|
fn new_linux_container() -> (Result<LinuxContainer>, tempfile::TempDir) {
|
||||||
|
// Create a temporal directory
|
||||||
|
let dir = tempdir()
|
||||||
|
.map_err(|e| anyhow!(e).context("tempdir failed"))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Create a new container
|
||||||
|
(
|
||||||
|
LinuxContainer::new(
|
||||||
|
"some_id",
|
||||||
|
&dir.path().join("rootfs").to_str().unwrap(),
|
||||||
|
create_dummy_opts(),
|
||||||
|
&slog_scope::logger(),
|
||||||
|
),
|
||||||
|
dir,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_linux_container_and_then<U, F: FnOnce(LinuxContainer) -> Result<U, anyhow::Error>>(
|
||||||
op: F,
|
op: F,
|
||||||
) -> Result<U, anyhow::Error> {
|
) -> Result<U, anyhow::Error> {
|
||||||
// Create a temporal directory
|
let (container, _dir) = new_linux_container();
|
||||||
tempdir()
|
container.and_then(op)
|
||||||
.map_err(|e| anyhow!(e).context("tempdir failed"))
|
|
||||||
.and_then(|p: tempfile::TempDir| {
|
|
||||||
// Create a new container
|
|
||||||
LinuxContainer::new(
|
|
||||||
"some_id",
|
|
||||||
&p.path().join("rootfs").to_str().unwrap(),
|
|
||||||
create_dummy_opts(),
|
|
||||||
&slog_scope::logger(),
|
|
||||||
)
|
|
||||||
.and_then(op)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_pause_bad_status() {
|
fn test_linuxcontainer_pause_bad_status() {
|
||||||
let ret = new_linux_container(|mut c: LinuxContainer| {
|
let ret = new_linux_container_and_then(|mut c: LinuxContainer| {
|
||||||
// Change state to pause, c.pause() should fail
|
// Change state to pause, c.pause() should fail
|
||||||
c.status.transition(Status::PAUSED);
|
c.status.transition(Status::PAUSED);
|
||||||
c.pause().map_err(|e| anyhow!(e))
|
c.pause().map_err(|e| anyhow!(e))
|
||||||
@ -1857,7 +1864,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_pause_cgroupmgr_is_none() {
|
fn test_linuxcontainer_pause_cgroupmgr_is_none() {
|
||||||
let ret = new_linux_container(|mut c: LinuxContainer| {
|
let ret = new_linux_container_and_then(|mut c: LinuxContainer| {
|
||||||
c.cgroup_manager = None;
|
c.cgroup_manager = None;
|
||||||
c.pause().map_err(|e| anyhow!(e))
|
c.pause().map_err(|e| anyhow!(e))
|
||||||
});
|
});
|
||||||
@ -1867,7 +1874,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_pause() {
|
fn test_linuxcontainer_pause() {
|
||||||
let ret = new_linux_container(|mut c: LinuxContainer| {
|
let ret = new_linux_container_and_then(|mut c: LinuxContainer| {
|
||||||
c.cgroup_manager = FsManager::new("").ok();
|
c.cgroup_manager = FsManager::new("").ok();
|
||||||
c.pause().map_err(|e| anyhow!(e))
|
c.pause().map_err(|e| anyhow!(e))
|
||||||
});
|
});
|
||||||
@ -1877,7 +1884,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_resume_bad_status() {
|
fn test_linuxcontainer_resume_bad_status() {
|
||||||
let ret = new_linux_container(|mut c: LinuxContainer| {
|
let ret = new_linux_container_and_then(|mut c: LinuxContainer| {
|
||||||
// Change state to created, c.resume() should fail
|
// Change state to created, c.resume() should fail
|
||||||
c.status.transition(Status::CREATED);
|
c.status.transition(Status::CREATED);
|
||||||
c.resume().map_err(|e| anyhow!(e))
|
c.resume().map_err(|e| anyhow!(e))
|
||||||
@ -1889,7 +1896,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_resume_cgroupmgr_is_none() {
|
fn test_linuxcontainer_resume_cgroupmgr_is_none() {
|
||||||
let ret = new_linux_container(|mut c: LinuxContainer| {
|
let ret = new_linux_container_and_then(|mut c: LinuxContainer| {
|
||||||
c.status.transition(Status::PAUSED);
|
c.status.transition(Status::PAUSED);
|
||||||
c.cgroup_manager = None;
|
c.cgroup_manager = None;
|
||||||
c.resume().map_err(|e| anyhow!(e))
|
c.resume().map_err(|e| anyhow!(e))
|
||||||
@ -1900,7 +1907,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_resume() {
|
fn test_linuxcontainer_resume() {
|
||||||
let ret = new_linux_container(|mut c: LinuxContainer| {
|
let ret = new_linux_container_and_then(|mut c: LinuxContainer| {
|
||||||
c.cgroup_manager = FsManager::new("").ok();
|
c.cgroup_manager = FsManager::new("").ok();
|
||||||
// Change status to paused, this way we can resume it
|
// Change status to paused, this way we can resume it
|
||||||
c.status.transition(Status::PAUSED);
|
c.status.transition(Status::PAUSED);
|
||||||
@ -1912,7 +1919,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_state() {
|
fn test_linuxcontainer_state() {
|
||||||
let ret = new_linux_container(|c: LinuxContainer| c.state());
|
let ret = new_linux_container_and_then(|c: LinuxContainer| c.state());
|
||||||
assert!(ret.is_err(), "Expecting Err, Got {:?}", ret);
|
assert!(ret.is_err(), "Expecting Err, Got {:?}", ret);
|
||||||
assert!(
|
assert!(
|
||||||
format!("{:?}", ret).contains("not supported"),
|
format!("{:?}", ret).contains("not supported"),
|
||||||
@ -1923,7 +1930,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_oci_state_no_root_parent() {
|
fn test_linuxcontainer_oci_state_no_root_parent() {
|
||||||
let ret = new_linux_container(|mut c: LinuxContainer| {
|
let ret = new_linux_container_and_then(|mut c: LinuxContainer| {
|
||||||
c.config.spec.as_mut().unwrap().root.as_mut().unwrap().path = "/".to_string();
|
c.config.spec.as_mut().unwrap().root.as_mut().unwrap().path = "/".to_string();
|
||||||
c.oci_state()
|
c.oci_state()
|
||||||
});
|
});
|
||||||
@ -1937,13 +1944,13 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_oci_state() {
|
fn test_linuxcontainer_oci_state() {
|
||||||
let ret = new_linux_container(|c: LinuxContainer| c.oci_state());
|
let ret = new_linux_container_and_then(|c: LinuxContainer| c.oci_state());
|
||||||
assert!(ret.is_ok(), "Expecting Ok, Got {:?}", ret);
|
assert!(ret.is_ok(), "Expecting Ok, Got {:?}", ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_config() {
|
fn test_linuxcontainer_config() {
|
||||||
let ret = new_linux_container(|c: LinuxContainer| Ok(c));
|
let ret = new_linux_container_and_then(|c: LinuxContainer| Ok(c));
|
||||||
assert!(ret.is_ok(), "Expecting ok, Got {:?}", ret);
|
assert!(ret.is_ok(), "Expecting ok, Got {:?}", ret);
|
||||||
assert!(
|
assert!(
|
||||||
ret.as_ref().unwrap().config().is_ok(),
|
ret.as_ref().unwrap().config().is_ok(),
|
||||||
@ -1954,13 +1961,13 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_processes() {
|
fn test_linuxcontainer_processes() {
|
||||||
let ret = new_linux_container(|c: LinuxContainer| c.processes());
|
let ret = new_linux_container_and_then(|c: LinuxContainer| c.processes());
|
||||||
assert!(ret.is_ok(), "Expecting Ok, Got {:?}", ret);
|
assert!(ret.is_ok(), "Expecting Ok, Got {:?}", ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_get_process_not_found() {
|
fn test_linuxcontainer_get_process_not_found() {
|
||||||
let _ = new_linux_container(|mut c: LinuxContainer| {
|
let _ = new_linux_container_and_then(|mut c: LinuxContainer| {
|
||||||
let p = c.get_process("123");
|
let p = c.get_process("123");
|
||||||
assert!(p.is_err(), "Expecting Err, Got {:?}", p);
|
assert!(p.is_err(), "Expecting Err, Got {:?}", p);
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -1969,7 +1976,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_get_process() {
|
fn test_linuxcontainer_get_process() {
|
||||||
let _ = new_linux_container(|mut c: LinuxContainer| {
|
let _ = new_linux_container_and_then(|mut c: LinuxContainer| {
|
||||||
c.processes.insert(
|
c.processes.insert(
|
||||||
1,
|
1,
|
||||||
Process::new(&sl!(), &oci::Process::default(), "123", true, 1).unwrap(),
|
Process::new(&sl!(), &oci::Process::default(), "123", true, 1).unwrap(),
|
||||||
@ -1982,49 +1989,57 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_stats() {
|
fn test_linuxcontainer_stats() {
|
||||||
let ret = new_linux_container(|c: LinuxContainer| c.stats());
|
let ret = new_linux_container_and_then(|c: LinuxContainer| c.stats());
|
||||||
assert!(ret.is_ok(), "Expecting Ok, Got {:?}", ret);
|
assert!(ret.is_ok(), "Expecting Ok, Got {:?}", ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_set() {
|
fn test_linuxcontainer_set() {
|
||||||
let ret =
|
let ret = new_linux_container_and_then(|mut c: LinuxContainer| {
|
||||||
new_linux_container(|mut c: LinuxContainer| c.set(oci::LinuxResources::default()));
|
c.set(oci::LinuxResources::default())
|
||||||
|
});
|
||||||
assert!(ret.is_ok(), "Expecting Ok, Got {:?}", ret);
|
assert!(ret.is_ok(), "Expecting Ok, Got {:?}", ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_linuxcontainer_start() {
|
async fn test_linuxcontainer_start() {
|
||||||
let ret = new_linux_container(|mut c: LinuxContainer| {
|
let (c, _dir) = new_linux_container();
|
||||||
c.start(Process::new(&sl!(), &oci::Process::default(), "123", true, 1).unwrap())
|
let ret = c
|
||||||
});
|
.unwrap()
|
||||||
|
.start(Process::new(&sl!(), &oci::Process::default(), "123", true, 1).unwrap())
|
||||||
|
.await;
|
||||||
assert!(ret.is_err(), "Expecting Err, Got {:?}", ret);
|
assert!(ret.is_err(), "Expecting Err, Got {:?}", ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_linuxcontainer_run() {
|
async fn test_linuxcontainer_run() {
|
||||||
let ret = new_linux_container(|mut c: LinuxContainer| {
|
let (c, _dir) = new_linux_container();
|
||||||
c.run(Process::new(&sl!(), &oci::Process::default(), "123", true, 1).unwrap())
|
let ret = c
|
||||||
});
|
.unwrap()
|
||||||
|
.run(Process::new(&sl!(), &oci::Process::default(), "123", true, 1).unwrap())
|
||||||
|
.await;
|
||||||
assert!(ret.is_err(), "Expecting Err, Got {:?}", ret);
|
assert!(ret.is_err(), "Expecting Err, Got {:?}", ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_linuxcontainer_destroy() {
|
async fn test_linuxcontainer_destroy() {
|
||||||
let ret = new_linux_container(|mut c: LinuxContainer| c.destroy());
|
let (c, _dir) = new_linux_container();
|
||||||
|
|
||||||
|
let ret = c.unwrap().destroy().await;
|
||||||
assert!(ret.is_ok(), "Expecting Ok, Got {:?}", ret);
|
assert!(ret.is_ok(), "Expecting Ok, Got {:?}", ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_signal() {
|
fn test_linuxcontainer_signal() {
|
||||||
let ret =
|
let ret = new_linux_container_and_then(|c: LinuxContainer| {
|
||||||
new_linux_container(|c: LinuxContainer| c.signal(nix::sys::signal::SIGCONT, true));
|
c.signal(nix::sys::signal::SIGCONT, true)
|
||||||
|
});
|
||||||
assert!(ret.is_ok(), "Expecting Ok, Got {:?}", ret);
|
assert!(ret.is_ok(), "Expecting Ok, Got {:?}", ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_linuxcontainer_exec() {
|
fn test_linuxcontainer_exec() {
|
||||||
let ret = new_linux_container(|mut c: LinuxContainer| c.exec());
|
let ret = new_linux_container_and_then(|mut c: LinuxContainer| c.exec());
|
||||||
assert!(ret.is_err(), "Expecting Err, Got {:?}", ret);
|
assert!(ret.is_err(), "Expecting Err, Got {:?}", ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,8 +190,8 @@ mod tests {
|
|||||||
use nix::sched::CloneFlags;
|
use nix::sched::CloneFlags;
|
||||||
use tempfile::Builder;
|
use tempfile::Builder;
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_setup_persistent_ns() {
|
async fn test_setup_persistent_ns() {
|
||||||
skip_if_not_root!();
|
skip_if_not_root!();
|
||||||
// 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!());
|
||||||
@ -200,7 +200,8 @@ mod tests {
|
|||||||
let ns_ipc = Namespace::new(&logger)
|
let ns_ipc = Namespace::new(&logger)
|
||||||
.get_ipc()
|
.get_ipc()
|
||||||
.set_root_dir(tmpdir.path().to_str().unwrap())
|
.set_root_dir(tmpdir.path().to_str().unwrap())
|
||||||
.setup();
|
.setup()
|
||||||
|
.await;
|
||||||
|
|
||||||
assert!(ns_ipc.is_ok());
|
assert!(ns_ipc.is_ok());
|
||||||
assert!(remove_mounts(&[ns_ipc.unwrap().path]).is_ok());
|
assert!(remove_mounts(&[ns_ipc.unwrap().path]).is_ok());
|
||||||
@ -211,7 +212,8 @@ mod tests {
|
|||||||
let ns_uts = Namespace::new(&logger)
|
let ns_uts = Namespace::new(&logger)
|
||||||
.get_uts("test_hostname")
|
.get_uts("test_hostname")
|
||||||
.set_root_dir(tmpdir.path().to_str().unwrap())
|
.set_root_dir(tmpdir.path().to_str().unwrap())
|
||||||
.setup();
|
.setup()
|
||||||
|
.await;
|
||||||
|
|
||||||
assert!(ns_uts.is_ok());
|
assert!(ns_uts.is_ok());
|
||||||
assert!(remove_mounts(&[ns_uts.unwrap().path]).is_ok());
|
assert!(remove_mounts(&[ns_uts.unwrap().path]).is_ok());
|
||||||
@ -223,7 +225,8 @@ mod tests {
|
|||||||
let ns_pid = Namespace::new(&logger)
|
let ns_pid = Namespace::new(&logger)
|
||||||
.get_pid()
|
.get_pid()
|
||||||
.set_root_dir(tmpdir.path().to_str().unwrap())
|
.set_root_dir(tmpdir.path().to_str().unwrap())
|
||||||
.setup();
|
.setup()
|
||||||
|
.await;
|
||||||
|
|
||||||
assert!(ns_pid.is_err());
|
assert!(ns_pid.is_err());
|
||||||
}
|
}
|
||||||
|
@ -749,11 +749,11 @@ mod tests {
|
|||||||
assert_eq!(s.hostname, hostname);
|
assert_eq!(s.hostname, hostname);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn test_sandbox_set_destroy() {
|
async fn test_sandbox_set_destroy() {
|
||||||
let logger = slog::Logger::root(slog::Discard, o!());
|
let logger = slog::Logger::root(slog::Discard, o!());
|
||||||
let mut s = Sandbox::new(&logger).unwrap();
|
let mut s = Sandbox::new(&logger).unwrap();
|
||||||
let ret = s.destroy();
|
let ret = s.destroy().await;
|
||||||
assert!(ret.is_ok());
|
assert!(ret.is_ok());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user