1111from data import ARP_SERVER , ISO_IMAGES , ISO_IMAGES_BASE , ISO_IMAGES_CACHE , TEST_SSH_PUBKEY , TOOLS
1212from lib import installer , pxe
1313from lib .commands import local_cmd
14- from lib .common import callable_marker , url_download , wait_for
14+ from lib .common import Defer , callable_marker , url_download , wait_for
1515from lib .installer import AnswerFile
1616
1717from typing import TYPE_CHECKING , Any , Generator , Sequence
@@ -271,7 +271,8 @@ def remastered_iso(installer_iso: dict[str, str | bool], answerfile: AnswerFile
271271 yield remastered_iso
272272
273273@pytest .fixture (scope = 'function' )
274- def vm_booted_with_installer (host : Host , create_vms : list [VM ], remastered_iso : str ) -> Generator [VM , None , None ]:
274+ def vm_booted_with_installer (host : Host , create_vms : list [VM ], remastered_iso : str , defer : Defer ) \
275+ -> Generator [VM , None , None ]:
275276 host_vm , = create_vms # one single VM
276277 iso = remastered_iso
277278
@@ -280,52 +281,39 @@ def vm_booted_with_installer(host: Host, create_vms: list[VM], remastered_iso: s
280281 assert mac_address is not None
281282 logging .info ("Host VM has MAC %s" , mac_address )
282283
283- remote_iso = None
284- try :
285- remote_iso = host .pool .push_iso (iso )
286- host_vm .insert_cd (os .path .basename (remote_iso ))
287-
288- try :
289- host_vm .start ()
290- wait_for (host_vm .is_running , "Wait for host VM running" )
291-
292- # catch host-vm IP address
293- wait_for (lambda : pxe .arp_addresses_for (mac_address ),
294- "Wait for DHCP server to see Host VM in ARP tables" ,
295- timeout_secs = 10 * 60 )
296- ips = pxe .arp_addresses_for (mac_address )
297- logging .info ("Host VM has IPs %s" , ips )
298- assert len (ips ) == 1
299- host_vm .ip = ips [0 ]
300- ip = host_vm .ip
301- assert ip is not None
302-
303- # host may not be up if ARP cache was filled
304- wait_for (lambda : local_cmd (["ping" , "-c1" , ip ], check = False ),
305- "Wait for host up" , timeout_secs = 10 * 60 , retry_delay_secs = 10 )
306- wait_for (lambda : local_cmd (["nc" , "-zw5" , ip , "22" ], check = False ),
307- "Wait for ssh up on host" , timeout_secs = 10 * 60 , retry_delay_secs = 5 )
308-
309- yield host_vm
310-
311- logging .info ("Shutting down Host VM" )
312- assert host_vm .ip is not None
313- installer .poweroff (host_vm .ip )
314- wait_for (host_vm .is_halted , "Wait for host VM halted" )
315-
316- except Exception as e :
317- logging .critical ("caught exception %s" , e )
318- host_vm .shutdown (force = True )
319- raise
320- except KeyboardInterrupt :
321- logging .warning ("keyboard interrupt" )
322- host_vm .shutdown (force = True )
323- raise
324-
325- host_vm .eject_cd ()
326- finally :
327- if remote_iso :
328- host .pool .remove_iso (remote_iso )
284+ remote_iso = host .pool .push_iso (iso )
285+ host_vm .insert_cd (os .path .basename (remote_iso ))
286+ defer (lambda : host .pool .remove_iso (remote_iso ))
287+
288+ host_vm .start ()
289+ defer (lambda : host_vm .shutdown (force = True ))
290+ wait_for (host_vm .is_running , "Wait for host VM running" )
291+
292+ # catch host-vm IP address
293+ wait_for (lambda : pxe .arp_addresses_for (mac_address ),
294+ "Wait for DHCP server to see Host VM in ARP tables" ,
295+ timeout_secs = 10 * 60 )
296+ ips = pxe .arp_addresses_for (mac_address )
297+ logging .info ("Host VM has IPs %s" , ips )
298+ assert len (ips ) == 1
299+ host_vm .ip = ips [0 ]
300+ ip = host_vm .ip
301+ assert ip is not None
302+
303+ # host may not be up if ARP cache was filled
304+ wait_for (lambda : local_cmd (["ping" , "-c1" , ip ], check = False ),
305+ "Wait for host up" , timeout_secs = 10 * 60 , retry_delay_secs = 10 )
306+ wait_for (lambda : local_cmd (["nc" , "-zw5" , ip , "22" ], check = False ),
307+ "Wait for ssh up on host" , timeout_secs = 10 * 60 , retry_delay_secs = 5 )
308+
309+ yield host_vm
310+
311+ logging .info ("Shutting down Host VM" )
312+ assert host_vm .ip is not None
313+ installer .poweroff (host_vm .ip )
314+ wait_for (host_vm .is_halted , "Wait for host VM halted" )
315+
316+ host_vm .eject_cd ()
329317
330318@pytest .fixture (scope = 'function' )
331319def xcpng_chained (request : pytest .FixtureRequest ) -> None :
0 commit comments