44
55import logging
66
7+ from conftest import Defer
78from lib .commands import SSHCommandFailed
89from lib .common import GiB , strtobool , wait_for , wait_for_not
910from lib .host import Host
@@ -181,9 +182,9 @@ def install_randstream(vm: 'VM'):
181182
182183CoalesceOperation = Literal ['snapshot' , 'clone' ]
183184
184- def coalesce_integrity (vm : VM , vdi : VDI , vdi_op : CoalesceOperation , request : pytest . FixtureRequest ):
185+ def coalesce_integrity (vm : VM , vdi : VDI , vdi_op : CoalesceOperation , defer : Defer ):
185186 vbd = vm .connect_vdi (vdi )
186- request . addfinalizer (lambda : vm .disconnect_vdi (vdi ))
187+ defer (lambda : vm .disconnect_vdi (vdi ))
187188
188189 dev = f'/dev/{ vbd .param_get ("device" )} '
189190 vm .ssh (f"randstream generate -v { dev } " )
@@ -192,7 +193,7 @@ def coalesce_integrity(vm: VM, vdi: VDI, vdi_op: CoalesceOperation, request: pyt
192193 match vdi_op :
193194 case 'clone' : new_vdi = vdi .clone ()
194195 case 'snapshot' : new_vdi = vdi .snapshot ()
195- request . addfinalizer (lambda : new_vdi .destroy () if new_vdi is not None else None )
196+ defer (lambda : new_vdi .destroy () if new_vdi is not None else None )
196197
197198 vm .ssh (f"randstream generate -v --seed 1 --size 128Mi { dev } " )
198199 vm .ssh (f"randstream validate -v --expected-checksum ad2ca9af { dev } " )
@@ -201,7 +202,7 @@ def coalesce_integrity(vm: VM, vdi: VDI, vdi_op: CoalesceOperation, request: pyt
201202
202203XVACompression = Literal ['none' , 'gzip' , 'zstd' ]
203204
204- def xva_export_import (vm : VM , compression : XVACompression , request : pytest . FixtureRequest ):
205+ def xva_export_import (vm : VM , compression : XVACompression , defer : Defer ):
205206 # The tests using this function are using specific fixtures to create the VM on the expected SR
206207 # In consequence, we can't use the storage_test_vm, so we have to start the VM explicitly and install randstream
207208 vm .start ()
@@ -214,25 +215,25 @@ def xva_export_import(vm: VM, compression: XVACompression, request: pytest.Fixtu
214215 vm .shutdown (verify = True )
215216
216217 xva_path = f'/tmp/{ vm .uuid } .xva'
217- request . addfinalizer (lambda : vm .host .ssh (f'rm -f { xva_path } ' ))
218+ defer (lambda : vm .host .ssh (f'rm -f { xva_path } ' ))
218219 vm .export (xva_path , compression )
219220 # check that the zero blocks are not part of the result. Most of the data is from the random stream, so
220221 # compression has little effect. We just check the result is between 500 and 700 MiB
221222 size_mb = int (vm .host .ssh (f'du -sm --apparent-size { xva_path } ' ).split ()[0 ])
222223 assert 500 < size_mb < 700 , f"unexpected xva size: { size_mb } "
223224
224225 imported_vm = vm .host .import_vm (xva_path , vm .vdis [0 ].sr .uuid )
225- request . addfinalizer (lambda : imported_vm .destroy ())
226+ defer (lambda : imported_vm .destroy ())
226227 imported_vm .start ()
227228 imported_vm .wait_for_vm_running_and_ssh_up ()
228229 imported_vm .ssh ("randstream validate -v --expected-checksum 24e905d6 /root/data" )
229230
230- def vdi_export_import (vm : VM , sr : SR , image_format : ImageFormat , request : pytest . FixtureRequest ):
231+ def vdi_export_import (vm : VM , sr : SR , image_format : ImageFormat , defer : Defer ):
231232 vdi_src = sr .create_vdi (image_format = image_format )
232- request . addfinalizer (lambda : vdi_src .destroy () if vdi_src is not None else None )
233+ defer (lambda : vdi_src .destroy () if vdi_src is not None else None )
233234
234235 vbd = vm .connect_vdi (vdi_src )
235- request . addfinalizer (lambda : vm .disconnect_vdi (vdi_src ) if vdi_src is not None else None )
236+ defer (lambda : vm .disconnect_vdi (vdi_src ) if vdi_src is not None else None )
236237 dev = f'/dev/{ vbd .param_get ("device" )} '
237238
238239 # generate 2 blocks of data of 200MiB, at position 0 and at position 500MiB
@@ -244,7 +245,7 @@ def vdi_export_import(vm: VM, sr: SR, image_format: ImageFormat, request: pytest
244245 vm .disconnect_vdi (vdi_src )
245246
246247 image_path = f'/tmp/{ vdi_src .uuid } .{ image_format } '
247- request . addfinalizer (lambda : vm .host .ssh (f'rm -f { image_path } ' ))
248+ defer (lambda : vm .host .ssh (f'rm -f { image_path } ' ))
248249
249250 vm .host .xe ('vdi-export' , {'uuid' : vdi_src .uuid , 'filename' : image_path , 'format' : image_format })
250251 vdi_src = vdi_src .destroy ()
@@ -253,11 +254,11 @@ def vdi_export_import(vm: VM, sr: SR, image_format: ImageFormat, request: pytest
253254 size_mb = int (vm .host .ssh (f'du -sm --apparent-size { image_path } ' ).split ()[0 ])
254255 assert 400 < size_mb < 410 , f"unexpected image size: { size_mb } "
255256 vdi_dest = sr .create_vdi (image_format = image_format )
256- request . addfinalizer (lambda : vdi_dest .destroy ())
257+ defer (lambda : vdi_dest .destroy ())
257258
258259 vm .host .xe ('vdi-import' , {'uuid' : vdi_dest .uuid , 'filename' : image_path , 'format' : image_format })
259260 vm .connect_vdi (vdi_dest , 'xvdb' )
260- request . addfinalizer (lambda : vm .disconnect_vdi (vdi_dest ))
261+ defer (lambda : vm .disconnect_vdi (vdi_dest ))
261262
262263 vm .ssh (f"randstream validate -v --size 200MiB --expected-checksum c6310c52 { dev } " )
263264 vm .ssh (f"randstream validate -v --position 500MiB --size 200MiB --expected-checksum 1cb4218e { dev } " )
0 commit comments