66import shutil
77from pathlib import Path
88
9+ import pytest
10+
911import host_tools .drive as drive_tools
1012from framework .utils_drive import partuuid_and_disk_path
1113from host_tools .fcmetrics import FcDeviceMetrics
1214
1315
16+ @pytest .fixture
17+ def uvm_vhost_user_plain_any (microvm_factory , guest_kernel , pci_enabled ):
18+ return microvm_factory .build (
19+ guest_kernel , None , pci = pci_enabled , monitor_memory = False
20+ )
21+
22+
23+ @pytest .fixture
24+ def uvm_vhost_user_booted_ro (uvm_vhost_user_plain_any , rootfs ):
25+ """Returns a VM with a vhost-user rootfs"""
26+ vm = uvm_vhost_user_plain_any
27+
28+ # We need to setup ssh keys manually because we did not specify rootfs
29+ # in microvm_factory.build method
30+ ssh_key = rootfs .with_suffix (".id_rsa" )
31+ vm .ssh_key = ssh_key
32+ vm .spawn ()
33+ vm .basic_config (add_root_device = False )
34+ vm .add_vhost_user_drive ("rootfs" , rootfs , is_root_device = True , is_read_only = True )
35+ vm .add_net_iface ()
36+ vm .start ()
37+
38+ return vm
39+
40+
41+ @pytest .fixture
42+ def uvm_vhost_user_booted_rw (uvm_vhost_user_plain_any , rootfs ):
43+ """Returns a VM with a vhost-user rootfs"""
44+ vm = uvm_vhost_user_plain_any
45+
46+ # We need to setup ssh keys manually because we did not specify rootfs
47+ # in microvm_factory.build method
48+ ssh_key = rootfs .with_suffix (".id_rsa" )
49+ vm .ssh_key = ssh_key
50+ vm .spawn ()
51+ vm .basic_config (add_root_device = False )
52+ # Create a rw rootfs file that is unique to the microVM
53+ rootfs_rw = Path (vm .chroot ()) / "rootfs"
54+ shutil .copy (rootfs , rootfs_rw )
55+ vm .add_vhost_user_drive (
56+ "rootfs" , rootfs_rw , is_root_device = True , is_read_only = False
57+ )
58+ vm .add_net_iface ()
59+ vm .start ()
60+
61+ return vm
62+
63+
1464def _check_block_size (ssh_connection , dev_path , size ):
1565 """
1666 Checks the size of the block device.
@@ -34,26 +84,16 @@ def _check_drives(test_microvm, assert_dict, keys_array):
3484 assert blockdev_out_line_cols [col ] == assert_dict [key ]
3585
3686
37- def test_vhost_user_block (microvm_factory , guest_kernel , rootfs ):
87+ def test_vhost_user_block (uvm_vhost_user_booted_ro ):
3888 """
3989 This test simply tries to boot a VM with
4090 vhost-user-block as a root device.
4191 """
4292
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 ()
93+ vm = uvm_vhost_user_booted_ro
5394 vhost_user_block_metrics = FcDeviceMetrics (
5495 "vhost_user_block" , 1 , aggr_supported = False
5596 )
56- vm .start ()
5797
5898 # Now check that vhost-user-block with rw is last.
5999 # 1-0 means line 1, column 0.
@@ -65,29 +105,14 @@ def test_vhost_user_block(microvm_factory, guest_kernel, rootfs):
65105 vhost_user_block_metrics .validate (vm )
66106
67107
68- def test_vhost_user_block_read_write (microvm_factory , guest_kernel , rootfs ):
108+ def test_vhost_user_block_read_write (uvm_vhost_user_booted_rw ):
69109 """
70110 This test simply tries to boot a VM with
71111 vhost-user-block as a root device.
72112 This test configures vhost-user-block to be read write.
73113 """
74114
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 ()
115+ vm = uvm_vhost_user_booted_rw
91116
92117 # Now check that vhost-user-block with rw is last.
93118 # 1-0 means line 1, column 0.
@@ -98,22 +123,12 @@ def test_vhost_user_block_read_write(microvm_factory, guest_kernel, rootfs):
98123 _check_drives (vm , assert_dict , assert_dict .keys ())
99124
100125
101- def test_vhost_user_block_disconnect (microvm_factory , guest_kernel , rootfs ):
126+ def test_vhost_user_block_disconnect (uvm_vhost_user_booted_ro ):
102127 """
103128 Test that even if backend is killed, Firecracker is still responsive.
104129 """
105130
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 ()
131+ vm = uvm_vhost_user_booted_ro
117132
118133 # Killing the backend
119134 vm .disks_vhost_user ["rootfs" ].kill ()
@@ -123,15 +138,15 @@ def test_vhost_user_block_disconnect(microvm_factory, guest_kernel, rootfs):
123138 _config = vm .api .vm_config .get ().json ()
124139
125140
126- def test_device_ordering (microvm_factory , guest_kernel , rootfs ):
141+ def test_device_ordering (uvm_vhost_user_plain_any , rootfs ):
127142 """
128143 Verify device ordering.
129144
130145 The root device should correspond to /dev/vda in the guest and
131146 the order of the other devices should match their configuration order.
132147 """
133148
134- vm = microvm_factory . build ( guest_kernel , None , monitor_memory = False )
149+ vm = uvm_vhost_user_plain_any
135150
136151 # We need to setup ssh keys manually because we did not specify rootfs
137152 # in microvm_factory.build method
@@ -194,16 +209,12 @@ def test_device_ordering(microvm_factory, guest_kernel, rootfs):
194209 vhost_user_block_metrics .validate (vm )
195210
196211
197- def test_partuuid_boot (
198- microvm_factory ,
199- guest_kernel ,
200- rootfs ,
201- ):
212+ def test_partuuid_boot (uvm_vhost_user_plain_any , rootfs ):
202213 """
203214 Test the output reported by blockdev when booting with PARTUUID.
204215 """
205216
206- vm = microvm_factory . build ( guest_kernel , None , monitor_memory = False )
217+ vm = uvm_vhost_user_plain_any
207218
208219 # We need to setup ssh keys manually because we did not specify rootfs
209220 # in microvm_factory.build method
@@ -230,12 +241,12 @@ def test_partuuid_boot(
230241 _check_drives (vm , assert_dict , assert_dict .keys ())
231242
232243
233- def test_partuuid_update (microvm_factory , guest_kernel , rootfs ):
244+ def test_partuuid_update (uvm_vhost_user_plain_any , rootfs ):
234245 """
235246 Test successful switching from PARTUUID boot to /dev/vda boot.
236247 """
237248
238- vm = microvm_factory . build ( guest_kernel , None , monitor_memory = False )
249+ vm = uvm_vhost_user_plain_any
239250
240251 # We need to setup ssh keys manually because we did not specify rootfs
241252 # in microvm_factory.build method
@@ -272,7 +283,7 @@ def test_partuuid_update(microvm_factory, guest_kernel, rootfs):
272283 vhost_user_block_metrics .validate (vm )
273284
274285
275- def test_config_change (microvm_factory , guest_kernel , rootfs ):
286+ def test_config_change (uvm_plain_any ):
276287 """
277288 Verify handling of block device resize.
278289 We expect that the guest will start reporting the updated size
@@ -283,7 +294,7 @@ def test_config_change(microvm_factory, guest_kernel, rootfs):
283294 new_sizes = [20 , 10 , 30 ] # MB
284295 mkfs_mount_cmd = "mkfs.ext4 /dev/vdb && mkdir -p /tmp/tmp && mount /dev/vdb /tmp/tmp && umount /tmp/tmp"
285296
286- vm = microvm_factory . build ( guest_kernel , rootfs , monitor_memory = False )
297+ vm = uvm_plain_any
287298 vm .spawn (log_level = "Info" )
288299 vm .basic_config ()
289300 vm .add_net_iface ()
0 commit comments