Skip to content

Commit 08d2c89

Browse files
committed
testscript: add helpers for hotplug()
For correct test execution and to prevent (CI) flakiness, we have to gracefully wait for every(!) device hotplug/unplug (attach/detach). This adds a new helper (hotplug_device()) for that. On-behalf-of: SAP philipp.schuster@sap.com Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
1 parent 3bc1532 commit 08d2c89

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/testscript.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,56 @@ def number_of_storage_devices(machine: Machine) -> int:
20472047
return number_of_devices(machine, PCI_CLASS_GENERIC_STORAGE_CONTROLLER)
20482048

20492049

2050+
def hotplug(machine: Machine, cmd: str, expect_success: bool = True):
2051+
"""
2052+
Hotplugs (attaches or detaches) a device and waits for the guest to
2053+
acknowledge that.
2054+
2055+
:param machine: The VM host
2056+
:param cmd: virsh command to perform the detach or attach
2057+
:param expect_success: whether the command is expected to succeed
2058+
:return:
2059+
"""
2060+
if cmd.startswith("virsh attach-"):
2061+
is_attach = True
2062+
elif cmd.startswith("virsh detach-"):
2063+
is_attach = False
2064+
else:
2065+
raise RuntimeError(
2066+
f"command is neither `virsh attach-*` nor `virsh detach-*`: `{cmd}`"
2067+
)
2068+
2069+
num_old = number_of_devices(machine)
2070+
num_new_expected = -1
2071+
2072+
match (is_attach, expect_success):
2073+
case (True, True):
2074+
machine.succeed(cmd)
2075+
num_new_expected = num_old + 1
2076+
case (True, False):
2077+
machine.fail(cmd)
2078+
num_new_expected = num_old
2079+
case (False, True):
2080+
machine.succeed(cmd)
2081+
num_new_expected = num_old - 1
2082+
case (False, False):
2083+
machine.fail(cmd)
2084+
num_new_expected = num_old
2085+
2086+
wait_for_guest_pci_device_enumeration(machine, num_new_expected)
2087+
2088+
2089+
def hotplug_fail(machine: Machine, cmd: str):
2090+
"""
2091+
Hotplugs (attaches or detaches) a device and expect that to fail.
2092+
2093+
:param machine: The VM host
2094+
:param cmd: virsh command to perform the detach or attach
2095+
:return:
2096+
"""
2097+
hotplug(machine, cmd, False)
2098+
2099+
20502100
def reset_system_image(machine: Machine):
20512101
"""
20522102
Replaces the (possibly modified) system image with its original

0 commit comments

Comments
 (0)