Skip to content

Commit d9884a3

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 6c7758b commit d9884a3

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
@@ -468,11 +468,11 @@ impl<T: VhostUserHandleBackend> VhostUserHandleImpl<T> {
468468
#[cfg(test)]
469469
mod tests {
470470
#![allow(clippy::undocumented_unsafe_blocks)]
471-
#![allow(clippy::cast_possible_truncation)]
472471

473472
use utils::tempfile::TempFile;
474473

475474
use super::*;
475+
use crate::utilities::test_utils::create_tmp_socket;
476476
use crate::vstate::memory::{FileOffset, GuestAddress, GuestMemoryExtension};
477477

478478
#[test]
@@ -500,40 +500,7 @@ mod tests {
500500

501501
let max_queue_num = 69;
502502

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

538505
let vuh = VhostUserHandleImpl::<MockMaster>::new(&tmp_socket_path, max_queue_num).unwrap();
539506
assert_eq!(

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

Lines changed: 4 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,14 @@ impl<T: VhostUserHandleBackend + Send + 'static> VirtioDevice for VhostUserBlock
336336
#[cfg(test)]
337337
mod tests {
338338
#![allow(clippy::undocumented_unsafe_blocks)]
339-
#![allow(clippy::cast_possible_truncation)]
340339

341340
use std::os::unix::net::UnixStream;
342341

343342
use utils::tempfile::TempFile;
344343
use vhost::{VhostUserMemoryRegionInfo, VringConfigData};
345344

346345
use super::*;
346+
use crate::utilities::test_utils::create_tmp_socket;
347347
use crate::vstate::memory::{FileOffset, GuestAddress, GuestMemoryExtension};
348348

349349
#[test]
@@ -395,40 +395,7 @@ mod tests {
395395
}
396396
}
397397

398-
let tmp_dir = utils::tempdir::TempDir::new().unwrap();
399-
let tmp_dir_path_str = tmp_dir.as_path().to_str().unwrap();
400-
let tmp_socket_path = format!("{tmp_dir_path_str}/tmp_socket");
401-
402-
unsafe {
403-
let socketfd = libc::socket(libc::AF_UNIX, libc::SOCK_STREAM, 0);
404-
if socketfd < 0 {
405-
panic!("Cannot create socket");
406-
}
407-
let mut socket_addr = libc::sockaddr_un {
408-
sun_family: libc::AF_UNIX as u16,
409-
sun_path: [0; 108],
410-
};
411-
412-
std::ptr::copy::<i8>(
413-
tmp_socket_path.as_ptr().cast(),
414-
socket_addr.sun_path.as_mut_ptr(),
415-
tmp_socket_path.as_bytes().len(),
416-
);
417-
418-
let bind = libc::bind(
419-
socketfd,
420-
(&socket_addr as *const libc::sockaddr_un).cast(),
421-
std::mem::size_of::<libc::sockaddr_un>() as u32,
422-
);
423-
if bind < 0 {
424-
panic!("Cannot bind socket");
425-
}
426-
427-
let listen = libc::listen(socketfd, 1);
428-
if listen < 0 {
429-
panic!("Cannot listen on socket");
430-
}
431-
}
398+
let (_tmp_dir, tmp_socket_path) = create_tmp_socket();
432399

433400
let vhost_block_config = VhostUserBlockConfig {
434401
drive_id: "test_drive".to_string(),
@@ -526,40 +493,7 @@ mod tests {
526493
}
527494
}
528495

529-
let tmp_dir = utils::tempdir::TempDir::new().unwrap();
530-
let tmp_dir_path_str = tmp_dir.as_path().to_str().unwrap();
531-
let tmp_socket_path = format!("{tmp_dir_path_str}/tmp_socket");
532-
533-
unsafe {
534-
let socketfd = libc::socket(libc::AF_UNIX, libc::SOCK_STREAM, 0);
535-
if socketfd < 0 {
536-
panic!("Cannot create socket");
537-
}
538-
let mut socket_addr = libc::sockaddr_un {
539-
sun_family: libc::AF_UNIX as u16,
540-
sun_path: [0; 108],
541-
};
542-
543-
std::ptr::copy::<i8>(
544-
tmp_socket_path.as_ptr().cast(),
545-
socket_addr.sun_path.as_mut_ptr(),
546-
tmp_socket_path.as_bytes().len(),
547-
);
548-
549-
let bind = libc::bind(
550-
socketfd,
551-
(&socket_addr as *const libc::sockaddr_un).cast(),
552-
std::mem::size_of::<libc::sockaddr_un>() as u32,
553-
);
554-
if bind < 0 {
555-
panic!("Cannot bind socket");
556-
}
557-
558-
let listen = libc::listen(socketfd, 1);
559-
if listen < 0 {
560-
panic!("Cannot listen on socket");
561-
}
562-
}
496+
let (_tmp_dir, tmp_socket_path) = create_tmp_socket();
563497

564498
let vhost_block_config = VhostUserBlockConfig {
565499
drive_id: "test_drive".to_string(),
@@ -711,41 +645,7 @@ mod tests {
711645
}
712646

713647
// Block creation
714-
let tmp_dir = utils::tempdir::TempDir::new().unwrap();
715-
let tmp_dir_path_str = tmp_dir.as_path().to_str().unwrap();
716-
let tmp_socket_path = format!("{tmp_dir_path_str}/tmp_socket");
717-
718-
unsafe {
719-
let socketfd = libc::socket(libc::AF_UNIX, libc::SOCK_STREAM, 0);
720-
if socketfd < 0 {
721-
panic!("Cannot create socket");
722-
}
723-
let mut socket_addr = libc::sockaddr_un {
724-
sun_family: libc::AF_UNIX as u16,
725-
sun_path: [0; 108],
726-
};
727-
728-
std::ptr::copy::<i8>(
729-
tmp_socket_path.as_ptr().cast(),
730-
socket_addr.sun_path.as_mut_ptr(),
731-
tmp_socket_path.as_bytes().len(),
732-
);
733-
734-
let bind = libc::bind(
735-
socketfd,
736-
(&socket_addr as *const libc::sockaddr_un).cast(),
737-
std::mem::size_of::<libc::sockaddr_un>() as u32,
738-
);
739-
if bind < 0 {
740-
panic!("Cannot bind socket");
741-
}
742-
743-
let listen = libc::listen(socketfd, 1);
744-
if listen < 0 {
745-
panic!("Cannot listen on socket");
746-
}
747-
}
748-
648+
let (_tmp_dir, tmp_socket_path) = create_tmp_socket();
749649
let vhost_block_config = VhostUserBlockConfig {
750650
drive_id: "test_drive".to_string(),
751651
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::<i8>(
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)