vsock: use tempfile in tests

Tests were run in a series before the previous commit because of a
testing failure (#232), and masked a bug. This is not necessary anymore
since the bug was fixed in the previous commit.

Instead of reverting to running the tests without the #[serial]
attribute, make every test self-contained by using unique temp
directories in each test run.

Test files that refer to sockets need to be unique because they risk
sharing filenames with other tests after refactoring. Since these tests
create/use/free resources, they should take care not to litter /tmp/ and
not share any file with other tests.

This commit uses a unique temp dir as location of test run files instead
of `/tmp/`. It adds a new dev-dependency, `tempfile`.

Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
This commit is contained in:
Manos Pitsidianakis 2023-07-10 15:47:49 +03:00 committed by Viresh Kumar
parent 7f809eeab8
commit 8a1deef49a
7 changed files with 185 additions and 172 deletions

85
Cargo.lock generated
View File

@ -266,19 +266,6 @@ dependencies = [
"typenum",
]
[[package]]
name = "dashmap"
version = "5.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "907076dfda823b0b36d2a1bb5f90c96660a5bbcd7729e10727f07858f22c4edc"
dependencies = [
"cfg-if",
"hashbrown 0.12.3",
"lock_api",
"once_cell",
"parking_lot_core",
]
[[package]]
name = "digest"
version = "0.10.7"
@ -663,16 +650,6 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09fc20d2ca12cb9f044c93e3bd6d32d523e6e2ec3db4f7b2939cd99026ecd3f0"
[[package]]
name = "lock_api"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1cc9717a20b1bb222f333e6a92fd32f7d8a18ddc5a3191a11af45dcbf4dcd16"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.19"
@ -748,29 +725,6 @@ dependencies = [
"hashbrown 0.12.3",
]
[[package]]
name = "parking_lot"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93f00c865fe7cabf650081affecd3871070f26767e7b2070a3ffae14c654b447"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-targets",
]
[[package]]
name = "pathdiff"
version = "0.2.1"
@ -1007,12 +961,6 @@ version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe232bdf6be8c8de797b22184ee71118d63780ea42ac85b61d1baa6d3b782ae9"
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "serde"
version = "1.0.168"
@ -1057,31 +1005,6 @@ dependencies = [
"unsafe-libyaml",
]
[[package]]
name = "serial_test"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e56dd856803e253c8f298af3f4d7eb0ae5e23a737252cd90bb4f3b435033b2d"
dependencies = [
"dashmap",
"futures",
"lazy_static",
"log",
"parking_lot",
"serial_test_derive",
]
[[package]]
name = "serial_test_derive"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91d129178576168c589c9ec973feedf7d3126c01ac2bf08795109aa35b69fb8f"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.23",
]
[[package]]
name = "sha2"
version = "0.10.7"
@ -1108,12 +1031,6 @@ dependencies = [
"autocfg",
]
[[package]]
name = "smallvec"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9"
[[package]]
name = "strsim"
version = "0.10.0"
@ -1406,7 +1323,7 @@ dependencies = [
"log",
"serde",
"serde_yaml",
"serial_test",
"tempfile",
"thiserror",
"vhost",
"vhost-user-backend",

View File

@ -33,4 +33,4 @@ serde_yaml = "0.9"
[dev-dependencies]
virtio-queue = { version = "0.9", features = ["test-utils"] }
serial_test = "2.0"
tempfile = "3.6.0"

View File

@ -260,9 +260,9 @@ fn main() {
#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
use std::fs::File;
use std::io::Write;
use tempfile::tempdir;
impl VsockArgs {
fn from_args(guest_cid: u64, socket: &str, uds_path: &str, tx_buffer_size: u32) -> Self {
@ -287,9 +287,12 @@ mod tests {
}
#[test]
#[serial]
fn test_vsock_config_setup() {
let args = VsockArgs::from_args(3, "/tmp/vhost4.socket", "/tmp/vm4.vsock", 64 * 1024);
let test_dir = tempdir().expect("Could not create a temp test directory.");
let socket_path = test_dir.path().join("vhost4.socket").display().to_string();
let uds_path = test_dir.path().join("vm4.vsock").display().to_string();
let args = VsockArgs::from_args(3, &socket_path, &uds_path, 64 * 1024);
let configs = Vec::<VsockConfig>::try_from(args);
assert!(configs.is_ok());
@ -299,17 +302,38 @@ mod tests {
let config = &configs[0];
assert_eq!(config.get_guest_cid(), 3);
assert_eq!(config.get_socket_path(), "/tmp/vhost4.socket");
assert_eq!(config.get_uds_path(), "/tmp/vm4.vsock");
assert_eq!(config.get_socket_path(), socket_path);
assert_eq!(config.get_uds_path(), uds_path);
assert_eq!(config.get_tx_buffer_size(), 64 * 1024);
test_dir.close().unwrap();
}
#[test]
#[serial]
fn test_vsock_config_setup_from_vm_args() {
let params = "--vm socket=/tmp/vhost3.socket,uds_path=/tmp/vm3.vsock \
--vm socket=/tmp/vhost4.socket,uds-path=/tmp/vm4.vsock,guest-cid=4,tx_buffer_size=65536 \
--vm guest-cid=5,socket=/tmp/vhost5.socket,uds_path=/tmp/vm5.vsock,tx-buffer-size=32768";
let test_dir = tempdir().expect("Could not create a temp test directory.");
let socket_paths = [
test_dir.path().join("vhost3.socket"),
test_dir.path().join("vhost4.socket"),
test_dir.path().join("vhost5.socket"),
];
let uds_paths = [
test_dir.path().join("vm3.vsock"),
test_dir.path().join("vm4.vsock"),
test_dir.path().join("vm5.vsock"),
];
let params = format!(
"--vm socket={vhost3_socket},uds_path={vm3_vsock} \
--vm socket={vhost4_socket},uds-path={vm4_vsock},guest-cid=4,tx_buffer_size=65536 \
--vm guest-cid=5,socket={vhost5_socket},uds_path={vm5_vsock},tx-buffer-size=32768",
vhost3_socket = socket_paths[0].display(),
vhost4_socket = socket_paths[1].display(),
vhost5_socket = socket_paths[2].display(),
vm3_vsock = uds_paths[0].display(),
vm4_vsock = uds_paths[1].display(),
vm5_vsock = uds_paths[2].display(),
);
let mut params = params.split_whitespace().collect::<Vec<&str>>();
params.insert(0, ""); // to make the test binary name agnostic
@ -324,61 +348,89 @@ mod tests {
let config = configs.get(0).unwrap();
assert_eq!(config.get_guest_cid(), 3);
assert_eq!(config.get_socket_path(), "/tmp/vhost3.socket");
assert_eq!(config.get_uds_path(), "/tmp/vm3.vsock");
assert_eq!(
config.get_socket_path(),
socket_paths[0].display().to_string()
);
assert_eq!(config.get_uds_path(), uds_paths[0].display().to_string());
assert_eq!(config.get_tx_buffer_size(), 65536);
let config = configs.get(1).unwrap();
assert_eq!(config.get_guest_cid(), 4);
assert_eq!(config.get_socket_path(), "/tmp/vhost4.socket");
assert_eq!(config.get_uds_path(), "/tmp/vm4.vsock");
assert_eq!(
config.get_socket_path(),
socket_paths[1].display().to_string()
);
assert_eq!(config.get_uds_path(), uds_paths[1].display().to_string());
assert_eq!(config.get_tx_buffer_size(), 65536);
let config = configs.get(2).unwrap();
assert_eq!(config.get_guest_cid(), 5);
assert_eq!(config.get_socket_path(), "/tmp/vhost5.socket");
assert_eq!(config.get_uds_path(), "/tmp/vm5.vsock");
assert_eq!(
config.get_socket_path(),
socket_paths[2].display().to_string()
);
assert_eq!(config.get_uds_path(), uds_paths[2].display().to_string());
assert_eq!(config.get_tx_buffer_size(), 32768);
test_dir.close().unwrap();
}
#[test]
#[serial]
fn test_vsock_config_setup_from_file() {
let mut yaml = File::create("./config.yaml").unwrap();
let test_dir = tempdir().expect("Could not create a temp test directory.");
let config_path = test_dir.path().join("config.yaml");
let socket_path = test_dir.path().join("vhost4.socket");
let uds_path = test_dir.path().join("vm4.vsock");
let mut yaml = File::create(&config_path).unwrap();
yaml.write_all(
b"vms:
format!(
"vms:
- guest_cid: 4
socket: /tmp/vhost4.socket
uds_path: /tmp/vm4.vsock
socket: {}
uds_path: {}
tx_buffer_size: 65536",
socket_path.display(),
uds_path.display(),
)
.as_bytes(),
)
.unwrap();
let args = VsockArgs::from_file("./config.yaml");
let args = VsockArgs::from_file(&config_path.display().to_string());
let configs = Vec::<VsockConfig>::try_from(args).unwrap();
assert_eq!(configs.len(), 1);
let config = &configs[0];
assert_eq!(config.get_guest_cid(), 4);
assert_eq!(config.get_socket_path(), "/tmp/vhost4.socket");
assert_eq!(config.get_uds_path(), "/tmp/vm4.vsock");
std::fs::remove_file("./config.yaml").unwrap();
assert_eq!(config.get_socket_path(), socket_path.display().to_string());
assert_eq!(config.get_uds_path(), uds_path.display().to_string());
std::fs::remove_file(&config_path).unwrap();
test_dir.close().unwrap();
}
#[test]
#[serial]
fn test_vsock_server() {
const CID: u64 = 3;
const VHOST_SOCKET_PATH: &str = "test_vsock_server.socket";
const VSOCK_SOCKET_PATH: &str = "test_vsock_server.vsock";
const CONN_TX_BUF_SIZE: u32 = 64 * 1024;
let config = VsockConfig::new(
CID,
VHOST_SOCKET_PATH.to_string(),
VSOCK_SOCKET_PATH.to_string(),
CONN_TX_BUF_SIZE,
);
let test_dir = tempdir().expect("Could not create a temp test directory.");
let vhost_socket_path = test_dir
.path()
.join("test_vsock_server.socket")
.display()
.to_string();
let vsock_socket_path = test_dir
.path()
.join("test_vsock_server.vsock")
.display()
.to_string();
let config = VsockConfig::new(CID, vhost_socket_path, vsock_socket_path, CONN_TX_BUF_SIZE);
let cid_map: Arc<RwLock<CidMap>> = Arc::new(RwLock::new(HashMap::new()));
@ -398,5 +450,7 @@ mod tests {
assert_eq!(backend.threads.len(), 1);
assert_eq!(vring_workers.len(), backend.threads.len());
test_dir.close().unwrap();
}
}

View File

@ -329,30 +329,31 @@ impl VsockThreadBackend {
mod tests {
use super::*;
use crate::vhu_vsock::{VhostUserVsockBackend, VsockConfig, VSOCK_OP_RW};
use serial_test::serial;
use std::os::unix::net::UnixListener;
use tempfile::tempdir;
use virtio_vsock::packet::{VsockPacket, PKT_HEADER_SIZE};
const DATA_LEN: usize = 16;
const CONN_TX_BUF_SIZE: u32 = 64 * 1024;
#[test]
#[serial]
fn test_vsock_thread_backend() {
const CID: u64 = 3;
const VSOCK_SOCKET_PATH: &str = "test_vsock_thread_backend.vsock";
const VSOCK_PEER_PORT: u32 = 1234;
const VSOCK_PEER_PATH: &str = "test_vsock_thread_backend.vsock_1234";
let _ = std::fs::remove_file(VSOCK_PEER_PATH);
let _listener = UnixListener::bind(VSOCK_PEER_PATH).unwrap();
let test_dir = tempdir().expect("Could not create a temp test directory.");
let vsock_socket_path = test_dir.path().join("test_vsock_thread_backend.vsock");
let vsock_peer_path = test_dir.path().join("test_vsock_thread_backend.vsock_1234");
let _listener = UnixListener::bind(&vsock_peer_path).unwrap();
let epoll_fd = epoll::create(false).unwrap();
let cid_map: Arc<RwLock<CidMap>> = Arc::new(RwLock::new(HashMap::new()));
let mut vtp = VsockThreadBackend::new(
VSOCK_SOCKET_PATH.to_string(),
vsock_socket_path.display().to_string(),
epoll_fd,
CID,
CONN_TX_BUF_SIZE,
@ -394,26 +395,42 @@ mod tests {
assert!(vtp.recv_pkt(&mut packet).is_ok());
// cleanup
let _ = std::fs::remove_file(VSOCK_PEER_PATH);
let _ = std::fs::remove_file(&vsock_peer_path);
let _ = std::fs::remove_file(&vsock_socket_path);
test_dir.close().unwrap();
}
#[test]
#[serial]
fn test_vsock_thread_backend_sibling_vms() {
const CID: u64 = 3;
const VSOCK_SOCKET_PATH: &str = "test_vsock_thread_backend.vsock";
const SIBLING_CID: u64 = 4;
const SIBLING_VHOST_SOCKET_PATH: &str = "test_vsock_thread_backend_sibling.socket";
const SIBLING_VSOCK_SOCKET_PATH: &str = "test_vsock_thread_backend_sibling.vsock";
const SIBLING_LISTENING_PORT: u32 = 1234;
let test_dir = tempdir().expect("Could not create a temp test directory.");
let vsock_socket_path = test_dir
.path()
.join("test_vsock_thread_backend.vsock")
.display()
.to_string();
let sibling_vhost_socket_path = test_dir
.path()
.join("test_vsock_thread_backend_sibling.socket")
.display()
.to_string();
let sibling_vsock_socket_path = test_dir
.path()
.join("test_vsock_thread_backend_sibling.vsock")
.display()
.to_string();
let cid_map: Arc<RwLock<CidMap>> = Arc::new(RwLock::new(HashMap::new()));
let sibling_config = VsockConfig::new(
SIBLING_CID,
SIBLING_VHOST_SOCKET_PATH.to_string(),
SIBLING_VSOCK_SOCKET_PATH.to_string(),
sibling_vhost_socket_path,
sibling_vsock_socket_path,
CONN_TX_BUF_SIZE,
);
@ -425,13 +442,8 @@ mod tests {
.insert(SIBLING_CID, sibling_backend.clone());
let epoll_fd = epoll::create(false).unwrap();
let mut vtp = VsockThreadBackend::new(
VSOCK_SOCKET_PATH.to_string(),
epoll_fd,
CID,
CONN_TX_BUF_SIZE,
cid_map,
);
let mut vtp =
VsockThreadBackend::new(vsock_socket_path, epoll_fd, CID, CONN_TX_BUF_SIZE, cid_map);
assert!(!vtp.pending_raw_pkts());
@ -489,5 +501,7 @@ mod tests {
assert_eq!(recvd_data_raw[1], 0xFEu8);
assert_eq!(recvd_data_raw[2], 0xBAu8);
assert_eq!(recvd_data_raw[3], 0xBEu8);
test_dir.close().unwrap();
}
}

View File

@ -348,24 +348,34 @@ impl VhostUserBackend<VringRwLock, ()> for VhostUserVsockBackend {
#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
use std::convert::TryInto;
use tempfile::tempdir;
use vhost_user_backend::VringT;
use vm_memory::GuestAddress;
const CONN_TX_BUF_SIZE: u32 = 64 * 1024;
#[test]
#[serial]
fn test_vsock_backend() {
const CID: u64 = 3;
const VHOST_SOCKET_PATH: &str = "test_vsock_backend.socket";
const VSOCK_SOCKET_PATH: &str = "test_vsock_backend.vsock";
let test_dir = tempdir().expect("Could not create a temp test directory.");
let vhost_socket_path = test_dir
.path()
.join("test_vsock_backend.socket")
.display()
.to_string();
let vsock_socket_path = test_dir
.path()
.join("test_vsock_backend.vsock")
.display()
.to_string();
let config = VsockConfig::new(
CID,
VHOST_SOCKET_PATH.to_string(),
VSOCK_SOCKET_PATH.to_string(),
vhost_socket_path.to_string(),
vsock_socket_path.to_string(),
CONN_TX_BUF_SIZE,
);
@ -426,16 +436,28 @@ mod tests {
assert!(!ret.unwrap());
// cleanup
let _ = std::fs::remove_file(VHOST_SOCKET_PATH);
let _ = std::fs::remove_file(VSOCK_SOCKET_PATH);
let _ = std::fs::remove_file(vhost_socket_path);
let _ = std::fs::remove_file(vsock_socket_path);
test_dir.close().unwrap();
}
#[test]
#[serial]
fn test_vsock_backend_failures() {
const CID: u64 = 3;
const VHOST_SOCKET_PATH: &str = "test_vsock_backend_failures.socket";
const VSOCK_SOCKET_PATH: &str = "test_vsock_backend_failures.vsock";
let test_dir = tempdir().expect("Could not create a temp test directory.");
let vhost_socket_path = test_dir
.path()
.join("test_vsock_backend_failures.socket")
.display()
.to_string();
let vsock_socket_path = test_dir
.path()
.join("test_vsock_backend_failures.vsock")
.display()
.to_string();
let config = VsockConfig::new(
CID,
@ -451,8 +473,8 @@ mod tests {
let config = VsockConfig::new(
CID,
VHOST_SOCKET_PATH.to_string(),
VSOCK_SOCKET_PATH.to_string(),
vhost_socket_path.to_string(),
vsock_socket_path.to_string(),
CONN_TX_BUF_SIZE,
);
@ -487,7 +509,9 @@ mod tests {
);
// cleanup
let _ = std::fs::remove_file(VHOST_SOCKET_PATH);
let _ = std::fs::remove_file(VSOCK_SOCKET_PATH);
let _ = std::fs::remove_file(vhost_socket_path);
let _ = std::fs::remove_file(vsock_socket_path);
test_dir.close().unwrap();
}
}

View File

@ -665,8 +665,8 @@ impl Drop for VhostUserVsockThread {
#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
use std::collections::HashMap;
use tempfile::tempdir;
use vm_memory::GuestAddress;
use vmm_sys_util::eventfd::EventFd;
@ -679,12 +679,17 @@ mod tests {
}
#[test]
#[serial]
fn test_vsock_thread() {
let cid_map: Arc<RwLock<CidMap>> = Arc::new(RwLock::new(HashMap::new()));
let test_dir = tempdir().expect("Could not create a temp test directory.");
let t = VhostUserVsockThread::new(
"test_vsock_thread.vsock".to_string(),
test_dir
.path()
.join("test_vsock_thread.vsock")
.display()
.to_string(),
3,
CONN_TX_BUF_SIZE,
cid_map,
@ -738,13 +743,16 @@ mod tests {
dummy_fd.write(1).unwrap();
t.process_backend_evt(EventSet::empty());
test_dir.close().unwrap();
}
#[test]
#[serial]
fn test_vsock_thread_failures() {
let cid_map: Arc<RwLock<CidMap>> = Arc::new(RwLock::new(HashMap::new()));
let test_dir = tempdir().expect("Could not create a temp test directory.");
let t = VhostUserVsockThread::new(
"/sys/not_allowed.vsock".to_string(),
3,
@ -753,13 +761,13 @@ mod tests {
);
assert!(t.is_err());
let mut t = VhostUserVsockThread::new(
"test_vsock_thread_failures.vsock".to_string(),
3,
CONN_TX_BUF_SIZE,
cid_map,
)
.unwrap();
let vsock_socket_path = test_dir
.path()
.join("test_vsock_thread_failures.vsock")
.display()
.to_string();
let mut t =
VhostUserVsockThread::new(vsock_socket_path, 3, CONN_TX_BUF_SIZE, cid_map).unwrap();
assert!(VhostUserVsockThread::epoll_register(-1, -1, epoll::Events::EPOLLIN).is_err());
assert!(VhostUserVsockThread::epoll_modify(-1, -1, epoll::Events::EPOLLIN).is_err());
assert!(VhostUserVsockThread::epoll_unregister(-1, -1).is_err());
@ -780,5 +788,7 @@ mod tests {
.push_back(ConnMapKey::new(0, 0));
assert!(t.process_rx(&vring, false).is_err());
assert!(t.process_rx(&vring, true).is_err());
test_dir.close().unwrap();
}
}

View File

@ -369,7 +369,6 @@ mod tests {
use super::*;
use crate::vhu_vsock::{VSOCK_HOST_CID, VSOCK_OP_RW, VSOCK_TYPE_STREAM};
use serial_test::serial;
use std::io::Result as IoResult;
use std::ops::Deref;
use virtio_bindings::bindings::virtio_ring::{VRING_DESC_F_NEXT, VRING_DESC_F_WRITE};
@ -505,7 +504,6 @@ mod tests {
}
#[test]
#[serial]
fn test_vsock_conn_init() {
// new locally inititated connection
let dummy_file = VsockDummySocket::new();
@ -554,7 +552,6 @@ mod tests {
}
#[test]
#[serial]
fn test_vsock_conn_credit() {
// new locally inititated connection
let dummy_file = VsockDummySocket::new();
@ -585,7 +582,6 @@ mod tests {
}
#[test]
#[serial]
fn test_vsock_conn_init_pkt() {
// parameters for packet head construction
let head_params = HeadParams::new(PKT_HEADER_SIZE, 10);
@ -622,7 +618,6 @@ mod tests {
}
#[test]
#[serial]
fn test_vsock_conn_recv_pkt() {
// parameters for packet head construction
let head_params = HeadParams::new(PKT_HEADER_SIZE, 5);
@ -719,7 +714,6 @@ mod tests {
}
#[test]
#[serial]
fn test_vsock_conn_send_pkt() {
// parameters for packet head construction
let head_params = HeadParams::new(PKT_HEADER_SIZE, 5);