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