Skip to content

Commit b26da3e

Browse files
committed
testscript: raise exceptions in wait_until_*() helper
Better to get more context when things fail and prevent assert clutter. On-behalf-of: SAP philipp.schuster@sap.com Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
1 parent 6e37799 commit b26da3e

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

tests/testscript.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -826,22 +826,22 @@ def is_shutoff():
826826
controllerVM.execute('virsh domstate testvm | grep "shut off"')[0] == 0
827827
)
828828

829-
assert wait_until_succeed(is_shutoff)
829+
wait_until_succeed(is_shutoff)
830830

831831
controllerVM.fail("find /run/libvirt/ch -name *.xml | grep .")
832832

833833
controllerVM.succeed("virsh start testvm")
834834
wait_for_ssh(controllerVM)
835835

836836
controllerVM.succeed("virsh shutdown testvm")
837-
assert wait_until_succeed(is_shutoff)
837+
wait_until_succeed(is_shutoff)
838838
controllerVM.fail("find /run/libvirt/ch -name *.xml | grep .")
839839

840840
controllerVM.succeed("virsh start testvm")
841841
wait_for_ssh(controllerVM)
842842

843843
controllerVM.succeed("virsh destroy testvm")
844-
assert wait_until_succeed(is_shutoff)
844+
wait_until_succeed(is_shutoff)
845845
controllerVM.fail("find /run/libvirt/ch -name *.xml | grep .")
846846

847847
def test_libvirt_event_stop_failed(self):
@@ -950,7 +950,7 @@ def prompt():
950950
)
951951
return status == 0
952952

953-
assert wait_until_succeed(prompt)
953+
wait_until_succeed(prompt)
954954

955955
controllerVM.succeed(
956956
textwrap.dedent("""
@@ -1063,7 +1063,7 @@ def migration_finished():
10631063
status, _ = controllerVM.execute("screen -ls | grep migrate")
10641064
return status != 0
10651065

1066-
self.assertTrue(wait_until_succeed(migration_finished))
1066+
wait_until_succeed(migration_finished)
10671067

10681068
computeVM.succeed("virsh list | grep testvm | grep running")
10691069

@@ -1361,11 +1361,8 @@ def check_virsh_list(vm):
13611361
status, _ = vm.execute("virsh list | grep testvm > /dev/null")
13621362
return status == 0
13631363

1364-
status = wait_until_fail(lambda: check_virsh_list(controllerVM))
1365-
assert status
1366-
1367-
status = wait_until_fail(lambda: check_virsh_list(computeVM))
1368-
assert status
1364+
wait_until_fail(lambda: check_virsh_list(controllerVM))
1365+
wait_until_fail(lambda: check_virsh_list(computeVM))
13691366

13701367
def test_live_migration_kill_chv_on_receiver_side(self):
13711368
"""
@@ -1399,11 +1396,9 @@ def check_virsh_list(vm):
13991396
status, _ = vm.execute("virsh list | grep testvm > /dev/null")
14001397
return status == 0
14011398

1402-
status = wait_until_fail(lambda: check_virsh_list(computeVM))
1403-
assert status
1399+
wait_until_fail(lambda: check_virsh_list(computeVM))
14041400

1405-
status = wait_until_succeed(lambda: check_virsh_list(controllerVM))
1406-
assert status
1401+
wait_until_succeed(lambda: check_virsh_list(controllerVM))
14071402

14081403
controllerVM.succeed("virsh list | grep 'running'")
14091404

@@ -1952,29 +1947,34 @@ def measure_ms(func):
19521947
return (time.time() - start) * 1000
19531948

19541949

1955-
def wait_until_succeed(func, retries=600) -> bool:
1950+
def wait_until_succeed(func, retries=600):
19561951
"""
19571952
Waits for the command to succeed.
19581953
After each failure, it waits 100ms.
19591954
19601955
:param func: Function that is expected to succeed.
19611956
:param retries: Amount of retries.
1962-
:return: whether the function succeeded eventually
19631957
"""
19641958
for _i in range(retries):
19651959
if func():
1966-
return True
1960+
return
19671961
time.sleep(0.1)
1968-
return False
1962+
raise RuntimeError("function didn't succeed")
1963+
19691964

1965+
def wait_until_fail(func, retries=600):
1966+
"""
1967+
Waits for the command to succeed.
1968+
After each failure, it waits 100ms.
19701969
1971-
def wait_until_fail(func):
1972-
retries = 600
1970+
:param func: Function that is expected to succeed.
1971+
:param retries: Amount of retries.
1972+
"""
19731973
for i in range(retries):
19741974
if not func():
1975-
return True
1975+
return
19761976
time.sleep(0.1)
1977-
return False
1977+
raise RuntimeError("function didn't fail")
19781978

19791979

19801980
def wait_for_ssh(machine: Machine, user="root", password="root", ip="192.168.1.2"):
@@ -2164,10 +2164,7 @@ def wait_for_guest_pci_device_enumeration(machine: Machine, new_count: int):
21642164
:return:
21652165
"""
21662166
# retries=20 => max 2s => we expect hotplug events to be relatively quick
2167-
if not wait_until_succeed(lambda: number_of_devices(machine) == new_count, 20):
2168-
raise RuntimeError(
2169-
f"guest did acknowledge PCI hotplug event: should be {new_count} but is {number_of_devices(machine)}"
2170-
)
2167+
wait_until_succeed(lambda: number_of_devices(machine) == new_count, 20)
21712168

21722169

21732170
runner = unittest.TextTestRunner()

0 commit comments

Comments
 (0)