88from host_tools .network import SSHConnection
99
1010
11- @pytest .fixture (params = [None ])
12- def uvm_with_rng (uvm_plain , request ):
13- """Fixture of a microvm with virtio-rng configured"""
14- rate_limiter = request .param
15- uvm = uvm_plain
11+ def uvm_with_rng_booted (microvm_factory , guest_kernel , rootfs , rate_limiter ):
12+ """Return a booted microvm with virtio-rng configured"""
13+ uvm = microvm_factory .build (guest_kernel , rootfs )
1614 uvm .spawn (log_level = "INFO" )
1715 uvm .basic_config (vcpu_count = 2 , mem_size_mib = 256 )
1816 uvm .add_net_iface ()
@@ -23,6 +21,34 @@ def uvm_with_rng(uvm_plain, request):
2321 return uvm
2422
2523
24+ def uvm_with_rng_restored (microvm_factory , guest_kernel , rootfs , rate_limiter ):
25+ """Return a restored uvm with virtio-rng configured"""
26+ uvm = uvm_with_rng_booted (microvm_factory , guest_kernel , rootfs , rate_limiter )
27+ snapshot = uvm .snapshot_full ()
28+ uvm .kill ()
29+ uvm2 = microvm_factory .build_from_snapshot (snapshot )
30+ uvm2 .rng_rate_limiter = uvm .rng_rate_limiter
31+ return uvm2
32+
33+
34+ @pytest .fixture (params = [uvm_with_rng_booted , uvm_with_rng_restored ])
35+ def uvm_ctor (request ):
36+ """Fixture to return uvms with different constructors"""
37+ return request .param
38+
39+
40+ @pytest .fixture (params = [None ])
41+ def rate_limiter (request ):
42+ """Fixture to return different rate limiters"""
43+ return request .param
44+
45+
46+ @pytest .fixture
47+ def uvm_any (microvm_factory , uvm_ctor , guest_kernel , rootfs , rate_limiter ):
48+ """Return booted and restored uvms"""
49+ return uvm_ctor (microvm_factory , guest_kernel , rootfs , rate_limiter )
50+
51+
2652def list_rng_available (ssh_connection : SSHConnection ) -> list [str ]:
2753 """Returns a list of rng devices available in the VM"""
2854 return (
@@ -62,35 +88,17 @@ def test_rng_not_present(uvm_nano):
6288 ), "virtio_rng device should not be available in the uvm"
6389
6490
65- def test_rng_present (uvm_with_rng ):
91+ def test_rng_present (uvm_any ):
6692 """
6793 Test a guest microVM with an entropy defined configured and ensure
6894 that we can access `/dev/hwrng`
6995 """
7096
71- vm = uvm_with_rng
97+ vm = uvm_any
7298 assert_virtio_rng_is_current_hwrng_device (vm .ssh )
7399 check_entropy (vm .ssh )
74100
75101
76- def test_rng_snapshot (uvm_with_rng , microvm_factory ):
77- """
78- Test that a virtio-rng device is functional after resuming from
79- a snapshot
80- """
81-
82- vm = uvm_with_rng
83- assert_virtio_rng_is_current_hwrng_device (vm .ssh )
84- check_entropy (vm .ssh )
85- snapshot = vm .snapshot_full ()
86-
87- new_vm = microvm_factory .build ()
88- new_vm .spawn ()
89- new_vm .restore_from_snapshot (snapshot , resume = True )
90- assert_virtio_rng_is_current_hwrng_device (new_vm .ssh )
91- check_entropy (new_vm .ssh )
92-
93-
94102def _get_percentage_difference (measured , base ):
95103 """Return the percentage delta between the arguments."""
96104 if measured == base :
@@ -199,7 +207,7 @@ def _rate_limiter_id(rate_limiter):
199207
200208# parametrize the RNG rate limiter
201209@pytest .mark .parametrize (
202- "uvm_with_rng " ,
210+ "rate_limiter " ,
203211 [
204212 {"bandwidth" : {"size" : 1000 , "refill_time" : 100 }},
205213 {"bandwidth" : {"size" : 10000 , "refill_time" : 100 }},
@@ -208,16 +216,14 @@ def _rate_limiter_id(rate_limiter):
208216 indirect = True ,
209217 ids = _rate_limiter_id ,
210218)
211- def test_rng_bw_rate_limiter (uvm_with_rng ):
219+ @pytest .mark .parametrize ("uvm_ctor" , [uvm_with_rng_booted ], indirect = True )
220+ def test_rng_bw_rate_limiter (uvm_any ):
212221 """
213222 Test that rate limiter without initial burst budget works
214223 """
215- vm = uvm_with_rng
216- # _start_vm_with_rng(vm, rate_limiter)
217-
224+ vm = uvm_any
218225 size = vm .rng_rate_limiter ["bandwidth" ]["size" ]
219226 refill_time = vm .rng_rate_limiter ["bandwidth" ]["refill_time" ]
220-
221227 expected_kbps = size / refill_time
222228
223229 assert_virtio_rng_is_current_hwrng_device (vm .ssh )
0 commit comments