mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-08-25 11:13:15 +00:00
shim-interface: add UT for get_uds_with_sid
Fixes: #9189 Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
This commit is contained in:
parent
7145243bd3
commit
2972a3a675
3
src/libs/Cargo.lock
generated
3
src/libs/Cargo.lock
generated
@ -1475,6 +1475,9 @@ dependencies = [
|
|||||||
"hyperlocal",
|
"hyperlocal",
|
||||||
"kata-sys-util",
|
"kata-sys-util",
|
||||||
"kata-types",
|
"kata-types",
|
||||||
|
"nix 0.24.2",
|
||||||
|
"tempfile",
|
||||||
|
"test-utils",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -13,8 +13,13 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "^1.0"
|
anyhow = "^1.0"
|
||||||
|
nix = "0.24.0"
|
||||||
tokio = { version = "1.8.0", features = ["rt-multi-thread"] }
|
tokio = { version = "1.8.0", features = ["rt-multi-thread"] }
|
||||||
hyper = { version = "0.14.20", features = ["stream", "server", "http1"] }
|
hyper = { version = "0.14.20", features = ["stream", "server", "http1"] }
|
||||||
hyperlocal = "0.8"
|
hyperlocal = "0.8"
|
||||||
kata-types = { path = "../kata-types" }
|
kata-types = { path = "../kata-types" }
|
||||||
kata-sys-util = {path = "../kata-sys-util" }
|
kata-sys-util = {path = "../kata-sys-util" }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
tempfile = "3.2.0"
|
||||||
|
test-utils = {path = "../test-utils"}
|
||||||
|
@ -88,13 +88,119 @@ pub fn mgmt_socket_addr(sid: &str) -> Result<String> {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
use std::path;
|
||||||
|
use tempfile::tempdir;
|
||||||
|
|
||||||
|
use test_utils::skip_if_not_root;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mgmt_socket_addr() {
|
fn test_mgmt_socket_addr() {
|
||||||
let sid = "414123";
|
// this test has to run as root, so has to manually cleanup afterwards
|
||||||
let addr = mgmt_socket_addr(sid).unwrap();
|
skip_if_not_root!();
|
||||||
assert_eq!(addr, "unix:///run/kata/414123/shim-monitor.sock");
|
|
||||||
|
|
||||||
|
let sid = "katatest";
|
||||||
|
let sandbox_test = path::Path::new(KATA_PATH).join("katatest98654sandboxpath1");
|
||||||
|
fs::create_dir_all(sandbox_test.as_path()).unwrap();
|
||||||
|
let addr = mgmt_socket_addr(sid).unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
addr,
|
||||||
|
"unix:///run/kata/katatest98654sandboxpath1/shim-monitor.sock"
|
||||||
|
);
|
||||||
|
fs::remove_dir_all(sandbox_test).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_mgmt_socket_addr_with_sid_empty() {
|
||||||
let sid = "";
|
let sid = "";
|
||||||
assert!(mgmt_socket_addr(sid).is_err());
|
let result = mgmt_socket_addr(sid);
|
||||||
|
assert!(result.is_err());
|
||||||
|
if let Err(err) = result {
|
||||||
|
let left = format!("{:?}", err.to_string());
|
||||||
|
let left_unquoted = &left[1..left.len() - 1];
|
||||||
|
let left_unescaped = left_unquoted.replace("\\\"", "\"");
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
left_unescaped,
|
||||||
|
format!("Empty sandbox id for acquiring socket address for shim_mgmt")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_uds_with_sid_ok() {
|
||||||
|
let run_path = tempdir().unwrap();
|
||||||
|
let dir1 = run_path.path().join("kata98654sandboxpath1");
|
||||||
|
let dir2 = run_path.path().join("aata98654dangboxpath1");
|
||||||
|
fs::create_dir_all(dir1.as_path()).unwrap();
|
||||||
|
fs::create_dir_all(dir2.as_path()).unwrap();
|
||||||
|
|
||||||
|
let result = get_uds_with_sid("kata", &run_path.path().display().to_string());
|
||||||
|
assert!(result.is_ok());
|
||||||
|
assert_eq!(
|
||||||
|
result.unwrap(),
|
||||||
|
format!(
|
||||||
|
"unix://{}",
|
||||||
|
run_path
|
||||||
|
.path()
|
||||||
|
.join("kata98654sandboxpath1")
|
||||||
|
.join(SHIM_MGMT_SOCK_NAME)
|
||||||
|
.display()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_uds_with_sid_with_zero() {
|
||||||
|
let result = get_uds_with_sid("acdsdfe", KATA_PATH);
|
||||||
|
assert!(result.is_err());
|
||||||
|
if let Err(err) = result {
|
||||||
|
let left = format!("{:?}", err.to_string());
|
||||||
|
let left_unquoted = &left[1..left.len() - 1];
|
||||||
|
let left_unescaped = left_unquoted.replace("\\\"", "\"");
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
left_unescaped,
|
||||||
|
format!(
|
||||||
|
"sandbox with the provided prefix {:?} is not found",
|
||||||
|
"acdsdfe"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_uds_with_sid_with_invalid() {
|
||||||
|
let result = get_uds_with_sid("^abcdse", KATA_PATH);
|
||||||
|
assert!(result.is_err());
|
||||||
|
if let Err(err) = result {
|
||||||
|
let left = format!("{:?}", err.to_string());
|
||||||
|
let left_unquoted = &left[1..left.len() - 1];
|
||||||
|
let left_unescaped = left_unquoted.replace("\\\"", "\"");
|
||||||
|
assert_eq!(
|
||||||
|
left_unescaped,
|
||||||
|
"The short id contains invalid characters.".to_owned()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_uds_with_sid_more_than_one() {
|
||||||
|
let run_path = tempdir().unwrap();
|
||||||
|
|
||||||
|
let dir1 = run_path.path().join("kata98654sandboxpath1");
|
||||||
|
let dir2 = run_path.path().join("kata98654dangboxpath1");
|
||||||
|
let dir3 = run_path.path().join("aata98654dangboxpath1");
|
||||||
|
fs::create_dir_all(dir1.as_path()).unwrap();
|
||||||
|
fs::create_dir_all(dir2.as_path()).unwrap();
|
||||||
|
fs::create_dir_all(dir3.as_path()).unwrap();
|
||||||
|
|
||||||
|
let result = get_uds_with_sid("kata", &run_path.path().display().to_string());
|
||||||
|
assert!(result.is_err());
|
||||||
|
if let Err(err) = result {
|
||||||
|
let left = format!("{:?}", err.to_string());
|
||||||
|
let left_unquoted = &left[1..left.len() - 1];
|
||||||
|
let left_unescaped = left_unquoted.replace("\\\"", "\"");
|
||||||
|
assert_eq!(left_unescaped, format!("more than one sandbox exists with the provided prefix {:?}, please provide a unique prefix", "kata"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user