Skip to content

Commit 01f9dcb

Browse files
Merge pull request #4821 from pmtk/qemu-ga-tweaks
USHIFT-5621: qemu-ga tweaks: add timeouts, increase read file buffer, use subprocess lib
2 parents 7195e25 + f494ae1 commit 01f9dcb

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

test/bin/scenario.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ sos_report_for_vm() {
224224
}
225225

226226
invoke_qemu_script() {
227-
"${ROOTDIR}/_output/robotenv/bin/python" "${ROOTDIR}/test/resources/qemu-guest-agent.py" "$@"
227+
timeout --verbose --foreground 2m \
228+
"${ROOTDIR}/_output/robotenv/bin/python" "${ROOTDIR}/test/resources/qemu-guest-agent.py" "$@"
228229
}
229230

230231
sos_report_for_vm_offline() {

test/resources/qemu-guest-agent.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,26 @@
2727
import argparse
2828
from base64 import b64decode, b64encode
2929
from os.path import basename
30+
import subprocess
3031
from time import sleep
32+
from typing import Iterator, Tuple
3133

3234
from robot.libraries.BuiltIn import BuiltIn, DotDict
33-
from robot.libraries.Process import Process, ExecutionResult
3435
from robot.utils.robottime import timestr_to_secs
3536

3637

37-
def _execute(vm_name: str, agent_message: dict) -> dict | int:
38+
def _execute(vm_name: str, agent_message: dict) -> dict:
3839
virsh_args = f'virsh --connect=qemu:///system qemu-agent-command --domain={vm_name} --cmd='
3940
msg = json.dumps(agent_message)
4041
virsh_args += f'\'{msg}\''
4142

42-
result: ExecutionResult = Process().run_process(virsh_args, shell=True)
43+
result = subprocess.run(virsh_args, shell=True, capture_output=True, text=True)
4344
# Only raise an error if the virsh command itself fails. The guest-agent may return a non-zero exit code which
4445
# should be handled by the keyword caller.
45-
if result.rc != 0:
46+
if result.returncode != 0:
4647
raise RuntimeError(f'virsh command failed:\nstdout={result.stdout}'
4748
f'\nstderr={result.stderr}'
48-
f'\nrc={result.rc}')
49+
f'\nrc={result.returncode}')
4950
# qemu-agent-command returns data to stdout as:
5051
# {
5152
# return: {
@@ -77,7 +78,7 @@ def _do_guest_exec(vm_name: str, cmd: str, *args, env: dict, stdin: str) -> int:
7778
return content['pid']
7879

7980

80-
def _do_guest_exec_status(vm_name: str, pid: int) -> (dict, bool):
81+
def _do_guest_exec_status(vm_name: str, pid: int) -> Tuple[dict, bool]:
8182
# _do_guest_exec_status wraps a given Process ID (pid) in a qemu-guest-agent guest-exec-status API call. For more
8283
# info this API, see https://qemu-project.gitlab.io/qemu/interop/qemu-ga-ref.html#qapidoc-198. For information on
8384
# the guest-exec-status return message, see https://qemu-project.gitlab.io/qemu/interop/qemu-ga-ref.html#qapidoc-194
@@ -117,7 +118,7 @@ def _do_kill(vm_name: str, pid: str, signal: str) -> int:
117118
return _do_guest_exec(vm_name, '/usr/bin/kill', signal, pid)
118119

119120

120-
def terminate_guest_process(vm_name: str, pid: str | int, kill: bool = False) -> (int, bool):
121+
def terminate_guest_process(vm_name: str, pid: str | int, kill: bool = False) -> Tuple[int, bool]:
121122
"""
122123
:param vm_name: The name of the VM to execute the command on.
123124
:type vm_name: str
@@ -152,7 +153,7 @@ def terminate_guest_process(vm_name: str, pid: str | int, kill: bool = False) ->
152153
return _do_kill(vm_name, pid, "-15" if not kill else "-9")
153154

154155

155-
def get_guest_process_result(vm_name: str, pid: int) -> (DotDict, bool):
156+
def get_guest_process_result(vm_name: str, pid: int) -> Tuple[DotDict, bool]:
156157
"""
157158
:param vm_name: The name of the VM to execute the command on
158159
:type vm_name: str
@@ -174,8 +175,7 @@ def get_guest_process_result(vm_name: str, pid: int) -> (DotDict, bool):
174175
return DotDict(_do_guest_exec_status(vm_name, pid))
175176

176177

177-
def wait_for_guest_process(vm_name: str, pid: int, timeout: int = None, on_timeout: str = "continue") -> (
178-
DotDict, bool):
178+
def wait_for_guest_process(vm_name: str, pid: int, timeout: int = None, on_timeout: str = "continue") -> Tuple[DotDict, bool]:
179179
"""
180180
:param vm_name: The name of the VM to execute the command on
181181
:type vm_name: str
@@ -247,7 +247,7 @@ def wait_for_guest_process(vm_name: str, pid: int, timeout: int = None, on_timeo
247247

248248

249249
def run_guest_process(vm_name: str, cmd: str, *args, env: dict = None, stdin: str = None, timeout: int = None,
250-
on_timeout: str = "continue") -> (DotDict, bool):
250+
on_timeout: str = "continue") -> Tuple[DotDict, bool]:
251251
"""
252252
:param vm_name: The name of the VM to execute the command on
253253
:type vm_name: str
@@ -351,15 +351,15 @@ def _close_file(vm_name, handle):
351351
_execute(vm_name, agent_cmd_wrapper)
352352

353353

354-
def _stream_file(vm_name: str, path: str) -> str:
354+
def _stream_file(vm_name: str, path: str) -> Iterator[str]:
355355
handle = _open_file(vm_name, path, 'r')
356356
try:
357357
while True:
358358
content = _execute(vm_name, {
359359
'execute': 'guest-file-read',
360360
'arguments': {
361361
'handle': handle,
362-
'count': 10240
362+
'count': 100 * 1024
363363
}
364364
})
365365

0 commit comments

Comments
 (0)