Skip to content

Commit a14b488

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 aec501f commit a14b488

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
@@ -818,22 +818,22 @@ def is_shutoff():
818818
controllerVM.execute('virsh domstate testvm | grep "shut off"')[0] == 0
819819
)
820820

821-
assert wait_until_succeed(is_shutoff)
821+
wait_until_succeed(is_shutoff)
822822

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

825825
controllerVM.succeed("virsh start testvm")
826826
wait_for_ssh(controllerVM)
827827

828828
controllerVM.succeed("virsh shutdown testvm")
829-
assert wait_until_succeed(is_shutoff)
829+
wait_until_succeed(is_shutoff)
830830
controllerVM.fail("find /run/libvirt/ch -name *.xml | grep .")
831831

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

835835
controllerVM.succeed("virsh destroy testvm")
836-
assert wait_until_succeed(is_shutoff)
836+
wait_until_succeed(is_shutoff)
837837
controllerVM.fail("find /run/libvirt/ch -name *.xml | grep .")
838838

839839
def test_libvirt_event_stop_failed(self):
@@ -942,7 +942,7 @@ def prompt():
942942
)
943943
return status == 0
944944

945-
assert wait_until_succeed(prompt)
945+
wait_until_succeed(prompt)
946946

947947
controllerVM.succeed(
948948
textwrap.dedent("""
@@ -1055,7 +1055,7 @@ def migration_finished():
10551055
status, _ = controllerVM.execute("screen -ls | grep migrate")
10561056
return status != 0
10571057

1058-
self.assertTrue(wait_until_succeed(migration_finished))
1058+
wait_until_succeed(migration_finished)
10591059

10601060
computeVM.succeed("virsh list | grep testvm | grep running")
10611061

@@ -1353,11 +1353,8 @@ def check_virsh_list(vm):
13531353
status, _ = vm.execute("virsh list | grep testvm > /dev/null")
13541354
return status == 0
13551355

1356-
status = wait_until_fail(lambda: check_virsh_list(controllerVM))
1357-
assert status
1358-
1359-
status = wait_until_fail(lambda: check_virsh_list(computeVM))
1360-
assert status
1356+
wait_until_fail(lambda: check_virsh_list(controllerVM))
1357+
wait_until_fail(lambda: check_virsh_list(computeVM))
13611358

13621359
def test_live_migration_kill_chv_on_receiver_side(self):
13631360
"""
@@ -1391,11 +1388,9 @@ def check_virsh_list(vm):
13911388
status, _ = vm.execute("virsh list | grep testvm > /dev/null")
13921389
return status == 0
13931390

1394-
status = wait_until_fail(lambda: check_virsh_list(computeVM))
1395-
assert status
1391+
wait_until_fail(lambda: check_virsh_list(computeVM))
13961392

1397-
status = wait_until_succeed(lambda: check_virsh_list(controllerVM))
1398-
assert status
1393+
wait_until_succeed(lambda: check_virsh_list(controllerVM))
13991394

14001395
controllerVM.succeed("virsh list | grep 'running'")
14011396

@@ -1944,29 +1939,34 @@ def measure_ms(func):
19441939
return (time.time() - start) * 1000
19451940

19461941

1947-
def wait_until_succeed(func, retries=600) -> bool:
1942+
def wait_until_succeed(func, retries=600):
19481943
"""
19491944
Waits for the command to succeed.
19501945
After each failure, it waits 100ms.
19511946
19521947
:param func: Function that is expected to succeed.
19531948
:param retries: Amount of retries.
1954-
:return: whether the function succeeded eventually
19551949
"""
19561950
for _i in range(retries):
19571951
if func():
1958-
return True
1952+
return
19591953
time.sleep(0.1)
1960-
return False
1954+
raise RuntimeError("function didn't succeed")
1955+
19611956

1957+
def wait_until_fail(func, retries=600):
1958+
"""
1959+
Waits for the command to succeed.
1960+
After each failure, it waits 100ms.
19621961
1963-
def wait_until_fail(func):
1964-
retries = 600
1962+
:param func: Function that is expected to succeed.
1963+
:param retries: Amount of retries.
1964+
"""
19651965
for i in range(retries):
19661966
if not func():
1967-
return True
1967+
return
19681968
time.sleep(0.1)
1969-
return False
1969+
raise RuntimeError("function didn't fail")
19701970

19711971

19721972
def wait_for_ssh(machine: Machine, user="root", password="root", ip="192.168.1.2"):
@@ -2160,10 +2160,7 @@ def wait_for_guest_pci_device_enumeration(machine: Machine, new_count: int):
21602160
:return:
21612161
"""
21622162
# retries=20 => max 2s => we expect hotplug events to be relatively quick
2163-
if not wait_until_succeed(lambda: number_of_devices(machine) == new_count, 20):
2164-
raise RuntimeError(
2165-
f"guest did acknowledge PCI hotplug event: should be {new_count} but is {number_of_devices(machine)}"
2166-
)
2163+
wait_until_succeed(lambda: number_of_devices(machine) == new_count, 20)
21672164

21682165

21692166
runner = unittest.TextTestRunner()

0 commit comments

Comments
 (0)