55import logging
66
77from lib .commands import SSHCommandFailed
8- from lib .common import GiB , strtobool , wait_for , wait_for_not
8+ from lib .common import Defer , GiB , strtobool , wait_for , wait_for_not
99from lib .host import Host
1010from lib .sr import SR
1111from lib .vdi import VDI , ImageFormat
@@ -181,9 +181,9 @@ def install_randstream(vm: 'VM'):
181181
182182CoalesceOperation = Literal ['snapshot' , 'clone' ]
183183
184- def coalesce_integrity (vm : VM , vdi : VDI , vdi_op : CoalesceOperation , request : pytest . FixtureRequest ):
184+ def coalesce_integrity (vm : VM , vdi : VDI , vdi_op : CoalesceOperation , defer : Defer ):
185185 vbd = vm .connect_vdi (vdi )
186- request . addfinalizer (lambda : vm .disconnect_vdi (vdi ))
186+ defer (lambda : vm .disconnect_vdi (vdi ))
187187
188188 dev = f'/dev/{ vbd .param_get ("device" )} '
189189 vm .ssh (f"randstream generate -v { dev } " )
@@ -192,7 +192,7 @@ def coalesce_integrity(vm: VM, vdi: VDI, vdi_op: CoalesceOperation, request: pyt
192192 match vdi_op :
193193 case 'clone' : new_vdi = vdi .clone ()
194194 case 'snapshot' : new_vdi = vdi .snapshot ()
195- request . addfinalizer (lambda : new_vdi .destroy () if new_vdi is not None else None )
195+ defer (lambda : new_vdi .destroy () if new_vdi is not None else None )
196196
197197 vm .ssh (f"randstream generate -v --seed 1 --size 128Mi { dev } " )
198198 vm .ssh (f"randstream validate -v --expected-checksum ad2ca9af { dev } " )
@@ -201,7 +201,7 @@ def coalesce_integrity(vm: VM, vdi: VDI, vdi_op: CoalesceOperation, request: pyt
201201
202202XVACompression = Literal ['none' , 'gzip' , 'zstd' ]
203203
204- def xva_export_import (vm : VM , compression : XVACompression , request : pytest . FixtureRequest ):
204+ def xva_export_import (vm : VM , compression : XVACompression , defer : Defer ):
205205 # The tests using this function are using specific fixtures to create the VM on the expected SR
206206 # In consequence, we can't use the storage_test_vm, so we have to start the VM explicitly and install randstream
207207 vm .start ()
@@ -214,25 +214,25 @@ def xva_export_import(vm: VM, compression: XVACompression, request: pytest.Fixtu
214214 vm .shutdown (verify = True )
215215
216216 xva_path = f'/tmp/{ vm .uuid } .xva'
217- request . addfinalizer (lambda : vm .host .ssh (f'rm -f { xva_path } ' ))
217+ defer (lambda : vm .host .ssh (f'rm -f { xva_path } ' ))
218218 vm .export (xva_path , compression )
219219 # check that the zero blocks are not part of the result. Most of the data is from the random stream, so
220220 # compression has little effect. We just check the result is between 500 and 700 MiB
221221 size_mb = int (vm .host .ssh (f'du -sm --apparent-size { xva_path } ' ).split ()[0 ])
222222 assert 500 < size_mb < 700 , f"unexpected xva size: { size_mb } "
223223
224224 imported_vm = vm .host .import_vm (xva_path , vm .vdis [0 ].sr .uuid )
225- request . addfinalizer (lambda : imported_vm .destroy ())
225+ defer (lambda : imported_vm .destroy ())
226226 imported_vm .start ()
227227 imported_vm .wait_for_vm_running_and_ssh_up ()
228228 imported_vm .ssh ("randstream validate -v --expected-checksum 24e905d6 /root/data" )
229229
230- def vdi_export_import (vm : VM , sr : SR , image_format : ImageFormat , request : pytest . FixtureRequest ):
230+ def vdi_export_import (vm : VM , sr : SR , image_format : ImageFormat , defer : Defer ):
231231 vdi_src = sr .create_vdi (image_format = image_format )
232- request . addfinalizer (lambda : vdi_src .destroy () if vdi_src is not None else None )
232+ defer (lambda : vdi_src .destroy () if vdi_src is not None else None )
233233
234234 vbd = vm .connect_vdi (vdi_src )
235- request . addfinalizer (lambda : vm .disconnect_vdi (vdi_src ) if vdi_src is not None else None )
235+ defer (lambda : vm .disconnect_vdi (vdi_src ) if vdi_src is not None else None )
236236 dev = f'/dev/{ vbd .param_get ("device" )} '
237237
238238 # generate 2 blocks of data of 200MiB, at position 0 and at position 500MiB
@@ -244,7 +244,7 @@ def vdi_export_import(vm: VM, sr: SR, image_format: ImageFormat, request: pytest
244244 vm .disconnect_vdi (vdi_src )
245245
246246 image_path = f'/tmp/{ vdi_src .uuid } .{ image_format } '
247- request . addfinalizer (lambda : vm .host .ssh (f'rm -f { image_path } ' ))
247+ defer (lambda : vm .host .ssh (f'rm -f { image_path } ' ))
248248
249249 vm .host .xe ('vdi-export' , {'uuid' : vdi_src .uuid , 'filename' : image_path , 'format' : image_format })
250250 vdi_src = vdi_src .destroy ()
@@ -253,11 +253,11 @@ def vdi_export_import(vm: VM, sr: SR, image_format: ImageFormat, request: pytest
253253 size_mb = int (vm .host .ssh (f'du -sm --apparent-size { image_path } ' ).split ()[0 ])
254254 assert 400 < size_mb < 410 , f"unexpected image size: { size_mb } "
255255 vdi_dest = sr .create_vdi (image_format = image_format )
256- request . addfinalizer (lambda : vdi_dest .destroy ())
256+ defer (lambda : vdi_dest .destroy ())
257257
258258 vm .host .xe ('vdi-import' , {'uuid' : vdi_dest .uuid , 'filename' : image_path , 'format' : image_format })
259259 vm .connect_vdi (vdi_dest , 'xvdb' )
260- request . addfinalizer (lambda : vm .disconnect_vdi (vdi_dest ))
260+ defer (lambda : vm .disconnect_vdi (vdi_dest ))
261261
262262 vm .ssh (f"randstream validate -v --size 200MiB --expected-checksum c6310c52 { dev } " )
263263 vm .ssh (f"randstream validate -v --position 500MiB --size 200MiB --expected-checksum 1cb4218e { dev } " )
0 commit comments