Skip to content

Commit 8d1f58b

Browse files
committed
refactor(tests): moved create_test_socket into test module
Moved `create_test_socket` into test module Signed-off-by: Egor Lazarchuk <[email protected]>
1 parent 5f26d56 commit 8d1f58b

File tree

3 files changed

+49
-139
lines changed

3 files changed

+49
-139
lines changed

src/vmm/src/devices/virtio/vhost_user.rs

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,11 @@ impl<T: VhostUserHandleBackend> VhostUserHandleImpl<T> {
462462
#[cfg(test)]
463463
mod tests {
464464
#![allow(clippy::undocumented_unsafe_blocks)]
465-
#![allow(clippy::cast_possible_truncation)]
466465

467466
use utils::tempfile::TempFile;
468467

469468
use super::*;
469+
use crate::utilities::test_utils::create_tmp_socket;
470470
use crate::vstate::memory::{FileOffset, GuestAddress, GuestMemoryExtension};
471471

472472
#[test]
@@ -494,40 +494,7 @@ mod tests {
494494

495495
let max_queue_num = 69;
496496

497-
let tmp_dir = utils::tempdir::TempDir::new().unwrap();
498-
let tmp_dir_path_str = tmp_dir.as_path().to_str().unwrap();
499-
let tmp_socket_path = format!("{tmp_dir_path_str}/tmp_socket");
500-
501-
unsafe {
502-
let socketfd = libc::socket(libc::AF_UNIX, libc::SOCK_STREAM, 0);
503-
if socketfd < 0 {
504-
panic!("Cannot create socket");
505-
}
506-
let mut socket_addr = libc::sockaddr_un {
507-
sun_family: libc::AF_UNIX as u16,
508-
sun_path: [0; 108],
509-
};
510-
511-
std::ptr::copy::<i8>(
512-
tmp_socket_path.as_ptr().cast(),
513-
socket_addr.sun_path.as_mut_ptr(),
514-
tmp_socket_path.as_bytes().len(),
515-
);
516-
517-
let bind = libc::bind(
518-
socketfd,
519-
(&socket_addr as *const libc::sockaddr_un).cast(),
520-
std::mem::size_of::<libc::sockaddr_un>() as u32,
521-
);
522-
if bind < 0 {
523-
panic!("Cannot bind socket");
524-
}
525-
526-
let listen = libc::listen(socketfd, 1);
527-
if listen < 0 {
528-
panic!("Cannot listen on socket");
529-
}
530-
}
497+
let (_tmp_dir, tmp_socket_path) = create_tmp_socket();
531498

532499
// Creation of the VhostUserHandleImpl correctly connects to the socket, sets the maximum
533500
// number of queues and sets itself as an owner of the session.

src/vmm/src/devices/virtio/vhost_user_block/device.rs

Lines changed: 4 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,14 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
327327
#[cfg(test)]
328328
mod tests {
329329
#![allow(clippy::undocumented_unsafe_blocks)]
330-
#![allow(clippy::cast_possible_truncation)]
331330

332331
use std::os::unix::net::UnixStream;
333332

334333
use utils::tempfile::TempFile;
335334
use vhost::{VhostUserMemoryRegionInfo, VringConfigData};
336335

337336
use super::*;
337+
use crate::utilities::test_utils::create_tmp_socket;
338338
use crate::vstate::memory::{FileOffset, GuestAddress, GuestMemoryExtension};
339339

340340
#[test]
@@ -434,40 +434,7 @@ mod tests {
434434
}
435435
}
436436

437-
let tmp_dir = utils::tempdir::TempDir::new().unwrap();
438-
let tmp_dir_path_str = tmp_dir.as_path().to_str().unwrap();
439-
let tmp_socket_path = format!("{tmp_dir_path_str}/tmp_socket");
440-
441-
unsafe {
442-
let socketfd = libc::socket(libc::AF_UNIX, libc::SOCK_STREAM, 0);
443-
if socketfd < 0 {
444-
panic!("Cannot create socket");
445-
}
446-
let mut socket_addr = libc::sockaddr_un {
447-
sun_family: libc::AF_UNIX as u16,
448-
sun_path: [0; 108],
449-
};
450-
451-
std::ptr::copy::<i8>(
452-
tmp_socket_path.as_ptr().cast(),
453-
socket_addr.sun_path.as_mut_ptr(),
454-
tmp_socket_path.as_bytes().len(),
455-
);
456-
457-
let bind = libc::bind(
458-
socketfd,
459-
(&socket_addr as *const libc::sockaddr_un).cast(),
460-
std::mem::size_of::<libc::sockaddr_un>() as u32,
461-
);
462-
if bind < 0 {
463-
panic!("Cannot bind socket");
464-
}
465-
466-
let listen = libc::listen(socketfd, 1);
467-
if listen < 0 {
468-
panic!("Cannot listen on socket");
469-
}
470-
}
437+
let (_tmp_dir, tmp_socket_path) = create_tmp_socket();
471438

472439
let vhost_block_config = VhostUserBlockConfig {
473440
drive_id: "test_drive".to_string(),
@@ -567,40 +534,7 @@ mod tests {
567534
}
568535
}
569536

570-
let tmp_dir = utils::tempdir::TempDir::new().unwrap();
571-
let tmp_dir_path_str = tmp_dir.as_path().to_str().unwrap();
572-
let tmp_socket_path = format!("{tmp_dir_path_str}/tmp_socket");
573-
574-
unsafe {
575-
let socketfd = libc::socket(libc::AF_UNIX, libc::SOCK_STREAM, 0);
576-
if socketfd < 0 {
577-
panic!("Cannot create socket");
578-
}
579-
let mut socket_addr = libc::sockaddr_un {
580-
sun_family: libc::AF_UNIX as u16,
581-
sun_path: [0; 108],
582-
};
583-
584-
std::ptr::copy::<i8>(
585-
tmp_socket_path.as_ptr().cast(),
586-
socket_addr.sun_path.as_mut_ptr(),
587-
tmp_socket_path.as_bytes().len(),
588-
);
589-
590-
let bind = libc::bind(
591-
socketfd,
592-
(&socket_addr as *const libc::sockaddr_un).cast(),
593-
std::mem::size_of::<libc::sockaddr_un>() as u32,
594-
);
595-
if bind < 0 {
596-
panic!("Cannot bind socket");
597-
}
598-
599-
let listen = libc::listen(socketfd, 1);
600-
if listen < 0 {
601-
panic!("Cannot listen on socket");
602-
}
603-
}
537+
let (_tmp_dir, tmp_socket_path) = create_tmp_socket();
604538

605539
let vhost_block_config = VhostUserBlockConfig {
606540
drive_id: "test_drive".to_string(),
@@ -778,41 +712,7 @@ mod tests {
778712
}
779713

780714
// Block creation
781-
let tmp_dir = utils::tempdir::TempDir::new().unwrap();
782-
let tmp_dir_path_str = tmp_dir.as_path().to_str().unwrap();
783-
let tmp_socket_path = format!("{tmp_dir_path_str}/tmp_socket");
784-
785-
unsafe {
786-
let socketfd = libc::socket(libc::AF_UNIX, libc::SOCK_STREAM, 0);
787-
if socketfd < 0 {
788-
panic!("Cannot create socket");
789-
}
790-
let mut socket_addr = libc::sockaddr_un {
791-
sun_family: libc::AF_UNIX as u16,
792-
sun_path: [0; 108],
793-
};
794-
795-
std::ptr::copy::<i8>(
796-
tmp_socket_path.as_ptr().cast(),
797-
socket_addr.sun_path.as_mut_ptr(),
798-
tmp_socket_path.as_bytes().len(),
799-
);
800-
801-
let bind = libc::bind(
802-
socketfd,
803-
(&socket_addr as *const libc::sockaddr_un).cast(),
804-
std::mem::size_of::<libc::sockaddr_un>() as u32,
805-
);
806-
if bind < 0 {
807-
panic!("Cannot bind socket");
808-
}
809-
810-
let listen = libc::listen(socketfd, 1);
811-
if listen < 0 {
812-
panic!("Cannot listen on socket");
813-
}
814-
}
815-
715+
let (_tmp_dir, tmp_socket_path) = create_tmp_socket();
816716
let vhost_block_config = VhostUserBlockConfig {
817717
drive_id: "test_drive".to_string(),
818718
partuuid: None,

src/vmm/src/utilities/test_utils/mod.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use std::sync::{Arc, Mutex};
66

7+
use utils::tempdir::TempDir;
8+
79
use crate::builder::build_microvm_for_boot;
810
use crate::resources::VmResources;
911
use crate::seccomp_filters::get_empty_filters;
@@ -64,3 +66,44 @@ pub fn default_vmm_no_boot(kernel_image: Option<&str>) -> (Arc<Mutex<Vmm>>, Even
6466
pub fn dirty_tracking_vmm(kernel_image: Option<&str>) -> (Arc<Mutex<Vmm>>, EventManager) {
6567
create_vmm(kernel_image, true, true)
6668
}
69+
70+
#[allow(clippy::undocumented_unsafe_blocks)]
71+
#[allow(clippy::cast_possible_truncation)]
72+
pub fn create_tmp_socket() -> (TempDir, String) {
73+
let tmp_dir = TempDir::new().unwrap();
74+
let tmp_dir_path_str = tmp_dir.as_path().to_str().unwrap();
75+
let tmp_socket_path = format!("{tmp_dir_path_str}/tmp_socket");
76+
77+
unsafe {
78+
let socketfd = libc::socket(libc::AF_UNIX, libc::SOCK_STREAM, 0);
79+
if socketfd < 0 {
80+
panic!("Cannot create socket");
81+
}
82+
let mut socket_addr = libc::sockaddr_un {
83+
sun_family: libc::AF_UNIX as u16,
84+
sun_path: [0; 108],
85+
};
86+
87+
std::ptr::copy(
88+
tmp_socket_path.as_ptr().cast(),
89+
socket_addr.sun_path.as_mut_ptr(),
90+
tmp_socket_path.as_bytes().len(),
91+
);
92+
93+
let bind = libc::bind(
94+
socketfd,
95+
(&socket_addr as *const libc::sockaddr_un).cast(),
96+
std::mem::size_of::<libc::sockaddr_un>() as u32,
97+
);
98+
if bind < 0 {
99+
panic!("Cannot bind socket");
100+
}
101+
102+
let listen = libc::listen(socketfd, 1);
103+
if listen < 0 {
104+
panic!("Cannot listen on socket");
105+
}
106+
}
107+
108+
(tmp_dir, tmp_socket_path)
109+
}

0 commit comments

Comments
 (0)