2121
2222import pytest
2323
24+ from framework .microvm import Microvm
2425from framework .utils_vsock import (
2526 ECHO_SERVER_PORT ,
2627 VSOCK_UDS_PATH ,
3940TEST_WORKER_COUNT = 10
4041
4142
42- def test_vsock (uvm_plain_any , bin_vsock_path , test_fc_session_root_path ):
43+ def _boot_vsock_vm (
44+ vm : Microvm ,
45+ * ,
46+ vcpu_count : int | None = None ,
47+ mem_size_mib : int | None = None ,
48+ log_level : str | None = None ,
49+ emit_metrics : bool = False ,
50+ pin_threads : bool = False ,
51+ ):
52+ spawn_kwargs = {}
53+ if log_level is not None :
54+ spawn_kwargs ["log_level" ] = log_level
55+ if emit_metrics :
56+ spawn_kwargs ["emit_metrics" ] = True
57+ vm .spawn (** spawn_kwargs )
58+
59+ config_kwargs = {}
60+ if vcpu_count is not None :
61+ config_kwargs ["vcpu_count" ] = vcpu_count
62+ if mem_size_mib is not None :
63+ config_kwargs ["mem_size_mib" ] = mem_size_mib
64+ vm .basic_config (** config_kwargs )
65+
66+ vm .add_net_iface ()
67+ vm .api .vsock .put (vsock_id = "vsock0" , guest_cid = 3 , uds_path = "/" + VSOCK_UDS_PATH )
68+ vm .start ()
69+ if pin_threads :
70+ vm .pin_threads (0 )
71+
72+ return vm
73+
74+
75+ @pytest .fixture
76+ def vsock_uvm (uvm_plain_acpi , request ):
77+ """Fixture to initialize a microVM with vsock device."""
78+ vcpus = request .param if hasattr (request , "param" ) else 1
79+
80+ return _boot_vsock_vm (
81+ uvm_plain_acpi ,
82+ vcpu_count = vcpus ,
83+ mem_size_mib = 1024 ,
84+ log_level = "Info" ,
85+ emit_metrics = True ,
86+ pin_threads = True ,
87+ )
88+
89+
90+ @pytest .fixture
91+ def vsock_uvm_any (uvm_plain_any ):
92+ """Fixture to initialize a kernel-parametrized microVM with vsock device."""
93+ return _boot_vsock_vm (uvm_plain_any )
94+
95+
96+ def test_vsock (vsock_uvm_any , bin_vsock_path , test_fc_session_root_path ):
4397 """
4498 Test guest and host vsock initiated connections.
4599
46100 Check the module docstring for details on the setup.
47101 """
48102
49- vm = uvm_plain_any
50- vm .spawn ()
51-
52- vm .basic_config ()
53- vm .add_net_iface ()
54- vm .api .vsock .put (vsock_id = "vsock0" , guest_cid = 3 , uds_path = f"/{ VSOCK_UDS_PATH } " )
55- vm .start ()
103+ vm = vsock_uvm_any
56104
57105 check_vsock_device (vm , bin_vsock_path , test_fc_session_root_path , vm .ssh )
58106 metrics = vm .flush_metrics ()
@@ -104,16 +152,11 @@ def negative_test_host_connections(vm, blob_path, blob_hash):
104152 validate_fc_metrics (metrics )
105153
106154
107- def test_vsock_epipe (uvm_plain_any , bin_vsock_path , test_fc_session_root_path ):
155+ def test_vsock_epipe (vsock_uvm_any , bin_vsock_path , test_fc_session_root_path ):
108156 """
109157 Vsock negative test to validate SIGPIPE/EPIPE handling.
110158 """
111- vm = uvm_plain_any
112- vm .spawn ()
113- vm .basic_config ()
114- vm .add_net_iface ()
115- vm .api .vsock .put (vsock_id = "vsock0" , guest_cid = 3 , uds_path = f"/{ VSOCK_UDS_PATH } " )
116- vm .start ()
159+ vm = vsock_uvm_any
117160
118161 # Generate the random data blob file, 20MB
119162 blob_path , blob_hash = make_blob (test_fc_session_root_path , 20 * 2 ** 20 )
@@ -130,8 +173,9 @@ def test_vsock_epipe(uvm_plain_any, bin_vsock_path, test_fc_session_root_path):
130173 validate_fc_metrics (metrics )
131174
132175
176+ @pytest .mark .parametrize ("vsock_uvm" , [1 , 2 ], indirect = True , ids = ["1vcpu" , "2vcpu" ])
133177def test_vsock_transport_reset_h2g (
134- uvm_plain_any , microvm_factory , bin_vsock_path , test_fc_session_root_path
178+ vsock_uvm , microvm_factory , bin_vsock_path , test_fc_session_root_path
135179):
136180 """
137181 Vsock transport reset test.
@@ -148,12 +192,7 @@ def test_vsock_transport_reset_h2g(
148192 6. Close VM -> Load VM from Snapshot -> check that vsock
149193 device is still working.
150194 """
151- test_vm = uvm_plain_any
152- test_vm .spawn ()
153- test_vm .basic_config (vcpu_count = 2 , mem_size_mib = 256 )
154- test_vm .add_net_iface ()
155- test_vm .api .vsock .put (vsock_id = "vsock0" , guest_cid = 3 , uds_path = f"/{ VSOCK_UDS_PATH } " )
156- test_vm .start ()
195+ test_vm = vsock_uvm
157196
158197 # Generate the random data blob file.
159198 blob_path , blob_hash = make_blob (test_fc_session_root_path )
@@ -191,9 +230,9 @@ def test_vsock_transport_reset_h2g(
191230 # it shouldn't receive anything.
192231 worker .sock .settimeout (0.25 )
193232 response = worker .sock .recv (32 )
194- assert (
195- response == b" "
196- ), f"Connection not closed: response received ' { response . decode ( 'utf-8' ) } '"
233+ assert response == b"" , (
234+ f"Connection not closed: response received ' { response . decode ( 'utf-8' ) } ' "
235+ )
197236 except (SocketTimeout , ConnectionResetError , BrokenPipeError ):
198237 pass
199238
@@ -217,16 +256,12 @@ def test_vsock_transport_reset_h2g(
217256 validate_fc_metrics (metrics )
218257
219258
220- def test_vsock_transport_reset_g2h (uvm_plain_any , microvm_factory ):
259+ @pytest .mark .parametrize ("vsock_uvm" , [1 , 2 ], indirect = True , ids = ["1vcpu" , "2vcpu" ])
260+ def test_vsock_transport_reset_g2h (vsock_uvm , microvm_factory ):
221261 """
222262 Vsock transport reset test.
223263 """
224- test_vm = uvm_plain_any
225- test_vm .spawn ()
226- test_vm .basic_config (vcpu_count = 2 , mem_size_mib = 256 )
227- test_vm .add_net_iface ()
228- test_vm .api .vsock .put (vsock_id = "vsock0" , guest_cid = 3 , uds_path = f"/{ VSOCK_UDS_PATH } " )
229- test_vm .start ()
264+ test_vm = vsock_uvm
230265
231266 # Create snapshot and terminate a VM.
232267 snapshot = test_vm .snapshot_full ()
0 commit comments