55import os
66import shutil
77from pathlib import Path
8+ import pytest
89
910import host_tools .drive as drive_tools
1011from framework .utils_drive import partuuid_and_disk_path
1112from host_tools .fcmetrics import FcDeviceMetrics
1213
1314
15+ @pytest .fixture
16+ def uvm_vhost_user_plain_any (microvm_factory , guest_kernel , pci_enabled ):
17+ return microvm_factory .build (guest_kernel , None , pci = pci_enabled , monitor_memory = False )
18+
19+
20+ @pytest .fixture
21+ def uvm_vhost_user_booted_ro (uvm_vhost_user_plain_any , rootfs ):
22+ """Returns a VM with a vhost-user rootfs"""
23+ vm = uvm_vhost_user_plain_any
24+
25+ # We need to setup ssh keys manually because we did not specify rootfs
26+ # in microvm_factory.build method
27+ ssh_key = rootfs .with_suffix (".id_rsa" )
28+ vm .ssh_key = ssh_key
29+ vm .spawn ()
30+ vm .basic_config (add_root_device = False )
31+ vm .add_vhost_user_drive ("rootfs" , rootfs , is_root_device = True , is_read_only = True )
32+ vm .add_net_iface ()
33+ vm .start ()
34+
35+ return vm
36+
37+
38+ @pytest .fixture
39+ def uvm_vhost_user_booted_rw (uvm_vhost_user_plain_any , rootfs ):
40+ """Returns a VM with a vhost-user rootfs"""
41+ vm = uvm_vhost_user_plain_any
42+
43+ # We need to setup ssh keys manually because we did not specify rootfs
44+ # in microvm_factory.build method
45+ ssh_key = rootfs .with_suffix (".id_rsa" )
46+ vm .ssh_key = ssh_key
47+ vm .spawn ()
48+ vm .basic_config (add_root_device = False )
49+ # Create a rw rootfs file that is unique to the microVM
50+ rootfs_rw = Path (vm .chroot ()) / "rootfs"
51+ shutil .copy (rootfs , rootfs_rw )
52+ vm .add_vhost_user_drive ("rootfs" , rootfs_rw , is_root_device = True , is_read_only = False )
53+ vm .add_net_iface ()
54+ vm .start ()
55+
56+ return vm
57+
58+
1459def _check_block_size (ssh_connection , dev_path , size ):
1560 """
1661 Checks the size of the block device.
@@ -34,26 +79,16 @@ def _check_drives(test_microvm, assert_dict, keys_array):
3479 assert blockdev_out_line_cols [col ] == assert_dict [key ]
3580
3681
37- def test_vhost_user_block (microvm_factory , guest_kernel , rootfs ):
82+ def test_vhost_user_block (uvm_vhost_user_booted_ro ):
3883 """
3984 This test simply tries to boot a VM with
4085 vhost-user-block as a root device.
4186 """
4287
43- vm = microvm_factory .build (guest_kernel , None , monitor_memory = False )
44-
45- # We need to setup ssh keys manually because we did not specify rootfs
46- # in microvm_factory.build method
47- ssh_key = rootfs .with_suffix (".id_rsa" )
48- vm .ssh_key = ssh_key
49- vm .spawn ()
50- vm .basic_config (add_root_device = False )
51- vm .add_vhost_user_drive ("rootfs" , rootfs , is_root_device = True , is_read_only = True )
52- vm .add_net_iface ()
88+ vm = uvm_vhost_user_booted_ro
5389 vhost_user_block_metrics = FcDeviceMetrics (
5490 "vhost_user_block" , 1 , aggr_supported = False
5591 )
56- vm .start ()
5792
5893 # Now check that vhost-user-block with rw is last.
5994 # 1-0 means line 1, column 0.
@@ -65,29 +100,14 @@ def test_vhost_user_block(microvm_factory, guest_kernel, rootfs):
65100 vhost_user_block_metrics .validate (vm )
66101
67102
68- def test_vhost_user_block_read_write (microvm_factory , guest_kernel , rootfs ):
103+ def test_vhost_user_block_read_write (uvm_vhost_user_booted_rw ):
69104 """
70105 This test simply tries to boot a VM with
71106 vhost-user-block as a root device.
72107 This test configures vhost-user-block to be read write.
73108 """
74109
75- vm = microvm_factory .build (guest_kernel , None , monitor_memory = False )
76-
77- # We need to setup ssh keys manually because we did not specify rootfs
78- # in microvm_factory.build method
79- ssh_key = rootfs .with_suffix (".id_rsa" )
80- vm .ssh_key = ssh_key
81- vm .spawn ()
82- vm .basic_config (add_root_device = False )
83-
84- # Create a rw rootfs file that is unique to the microVM
85- rootfs_rw = Path (vm .chroot ()) / "rootfs"
86- shutil .copy (rootfs , rootfs_rw )
87-
88- vm .add_vhost_user_drive ("rootfs" , rootfs_rw , is_root_device = True )
89- vm .add_net_iface ()
90- vm .start ()
110+ vm = uvm_vhost_user_booted_rw
91111
92112 # Now check that vhost-user-block with rw is last.
93113 # 1-0 means line 1, column 0.
@@ -98,22 +118,12 @@ def test_vhost_user_block_read_write(microvm_factory, guest_kernel, rootfs):
98118 _check_drives (vm , assert_dict , assert_dict .keys ())
99119
100120
101- def test_vhost_user_block_disconnect (microvm_factory , guest_kernel , rootfs ):
121+ def test_vhost_user_block_disconnect (uvm_vhost_user_booted_ro ):
102122 """
103123 Test that even if backend is killed, Firecracker is still responsive.
104124 """
105125
106- vm = microvm_factory .build (guest_kernel , None , monitor_memory = False )
107-
108- # We need to set up ssh keys manually because we did not specify rootfs
109- # in microvm_factory.build method
110- ssh_key = rootfs .with_suffix (".id_rsa" )
111- vm .ssh_key = ssh_key
112- vm .spawn ()
113- vm .basic_config (add_root_device = False )
114- vm .add_vhost_user_drive ("rootfs" , rootfs , is_root_device = True , is_read_only = True )
115- vm .add_net_iface ()
116- vm .start ()
126+ vm = uvm_vhost_user_booted_ro
117127
118128 # Killing the backend
119129 vm .disks_vhost_user ["rootfs" ].kill ()
@@ -123,15 +133,15 @@ def test_vhost_user_block_disconnect(microvm_factory, guest_kernel, rootfs):
123133 _config = vm .api .vm_config .get ().json ()
124134
125135
126- def test_device_ordering (microvm_factory , guest_kernel , rootfs ):
136+ def test_device_ordering (uvm_vhost_user_plain_any , rootfs ):
127137 """
128138 Verify device ordering.
129139
130140 The root device should correspond to /dev/vda in the guest and
131141 the order of the other devices should match their configuration order.
132142 """
133143
134- vm = microvm_factory . build ( guest_kernel , None , monitor_memory = False )
144+ vm = uvm_vhost_user_plain_any
135145
136146 # We need to setup ssh keys manually because we did not specify rootfs
137147 # in microvm_factory.build method
@@ -194,16 +204,12 @@ def test_device_ordering(microvm_factory, guest_kernel, rootfs):
194204 vhost_user_block_metrics .validate (vm )
195205
196206
197- def test_partuuid_boot (
198- microvm_factory ,
199- guest_kernel ,
200- rootfs ,
201- ):
207+ def test_partuuid_boot (uvm_vhost_user_plain_any , rootfs ):
202208 """
203209 Test the output reported by blockdev when booting with PARTUUID.
204210 """
205211
206- vm = microvm_factory . build ( guest_kernel , None , monitor_memory = False )
212+ vm = uvm_vhost_user_plain_any
207213
208214 # We need to setup ssh keys manually because we did not specify rootfs
209215 # in microvm_factory.build method
@@ -230,12 +236,12 @@ def test_partuuid_boot(
230236 _check_drives (vm , assert_dict , assert_dict .keys ())
231237
232238
233- def test_partuuid_update (microvm_factory , guest_kernel , rootfs ):
239+ def test_partuuid_update (uvm_vhost_user_plain_any , rootfs ):
234240 """
235241 Test successful switching from PARTUUID boot to /dev/vda boot.
236242 """
237243
238- vm = microvm_factory . build ( guest_kernel , None , monitor_memory = False )
244+ vm = uvm_vhost_user_plain_any
239245
240246 # We need to setup ssh keys manually because we did not specify rootfs
241247 # in microvm_factory.build method
@@ -272,7 +278,7 @@ def test_partuuid_update(microvm_factory, guest_kernel, rootfs):
272278 vhost_user_block_metrics .validate (vm )
273279
274280
275- def test_config_change (microvm_factory , guest_kernel , rootfs ):
281+ def test_config_change (uvm_plain_any ):
276282 """
277283 Verify handling of block device resize.
278284 We expect that the guest will start reporting the updated size
@@ -283,7 +289,7 @@ def test_config_change(microvm_factory, guest_kernel, rootfs):
283289 new_sizes = [20 , 10 , 30 ] # MB
284290 mkfs_mount_cmd = "mkfs.ext4 /dev/vdb && mkdir -p /tmp/tmp && mount /dev/vdb /tmp/tmp && umount /tmp/tmp"
285291
286- vm = microvm_factory . build ( guest_kernel , rootfs , monitor_memory = False )
292+ vm = uvm_plain_any
287293 vm .spawn (log_level = "Info" )
288294 vm .basic_config ()
289295 vm .add_net_iface ()
0 commit comments