From: Hanna Reitz <hreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
Peter Maydell <peter.maydell@linaro.org>,
Hanna Reitz <hreitz@redhat.com>,
qemu-devel@nongnu.org
Subject: [PULL 11/25] iotests: add qemu_img_json()
Date: Tue, 22 Mar 2022 12:56:33 +0100 [thread overview]
Message-ID: <20220322115647.726044-12-hreitz@redhat.com> (raw)
In-Reply-To: <20220322115647.726044-1-hreitz@redhat.com>
From: John Snow <jsnow@redhat.com>
qemu_img_json() is a new helper built on top of qemu_img() that tries to
pull a valid JSON document out of the stdout stream.
In the event that the return code is negative (the program crashed), or
the code is greater than zero and did not produce valid JSON output, the
VerboseProcessError raised by qemu_img() is re-raised.
In the event that the return code is zero but we can't parse valid JSON,
allow the JSON deserialization error to be raised.
Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20220321201618.903471-7-jsnow@redhat.com>
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
---
tests/qemu-iotests/iotests.py | 32 ++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
diff --git a/tests/qemu-iotests/iotests.py b/tests/qemu-iotests/iotests.py
index 9351f9c6ac..56aa068277 100644
--- a/tests/qemu-iotests/iotests.py
+++ b/tests/qemu-iotests/iotests.py
@@ -277,6 +277,38 @@ def ordered_qmp(qmsg, conv_keys=True):
def qemu_img_create(*args: str) -> 'subprocess.CompletedProcess[str]':
return qemu_img('create', *args)
+def qemu_img_json(*args: str) -> Any:
+ """
+ Run qemu-img and return its output as deserialized JSON.
+
+ :raise CalledProcessError:
+ When qemu-img crashes, or returns a non-zero exit code without
+ producing a valid JSON document to stdout.
+ :raise JSONDecoderError:
+ When qemu-img returns 0, but failed to produce a valid JSON document.
+
+ :return: A deserialized JSON object; probably a dict[str, Any].
+ """
+ try:
+ res = qemu_img(*args, combine_stdio=False)
+ except subprocess.CalledProcessError as exc:
+ # Terminated due to signal. Don't bother.
+ if exc.returncode < 0:
+ raise
+
+ # Commands like 'check' can return failure (exit codes 2 and 3)
+ # to indicate command completion, but with errors found. For
+ # multi-command flexibility, ignore the exact error codes and
+ # *try* to load JSON.
+ try:
+ return json.loads(exc.stdout)
+ except json.JSONDecodeError:
+ # Nope. This thing is toast. Raise the /process/ error.
+ pass
+ raise
+
+ return json.loads(res.stdout)
+
def qemu_img_measure(*args):
return json.loads(qemu_img_pipe("measure", "--output", "json", *args))
--
2.35.1
next prev parent reply other threads:[~2022-03-22 12:05 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-22 11:56 [PULL 00/25] Block patches for 7.0-rc1 Hanna Reitz
2022-03-22 11:56 ` [PULL 01/25] tests: add (riscv virt) machine mapping to testenv Hanna Reitz
2022-03-22 11:56 ` [PULL 02/25] block/rbd: fix write zeroes with growing images Hanna Reitz
2022-03-22 11:56 ` [PULL 03/25] tests/qemu-iotests: Use GNU sed in two more spots where it is necessary Hanna Reitz
2022-03-22 11:56 ` [PULL 04/25] tests: Do not treat the iotests as separate meson test target anymore Hanna Reitz
2022-03-22 11:56 ` [PULL 05/25] tests/qemu-iotests/testrunner: Supply a test plan in TAP mode Hanna Reitz
2022-03-22 11:56 ` [PULL 06/25] python/utils: add add_visual_margin() text decoration utility Hanna Reitz
2022-03-22 11:56 ` [PULL 07/25] python/utils: add VerboseProcessError Hanna Reitz
2022-03-22 11:56 ` [PULL 08/25] iotests: Remove explicit checks for qemu_img() == 0 Hanna Reitz
2022-03-22 11:56 ` [PULL 09/25] iotests: make qemu_img raise on non-zero rc by default Hanna Reitz
2022-03-22 11:56 ` [PULL 10/25] iotests: fortify compare_images() against crashes Hanna Reitz
2022-03-22 11:56 ` Hanna Reitz [this message]
2022-03-22 11:56 ` [PULL 12/25] iotests: use qemu_img_json() when applicable Hanna Reitz
2022-03-22 11:56 ` [PULL 13/25] iotests: add qemu_img_info() Hanna Reitz
2022-03-22 11:56 ` [PULL 14/25] iotests/remove-bitmap-from-backing: use qemu_img_info() Hanna Reitz
2022-03-22 11:56 ` [PULL 15/25] iotests: add qemu_img_map() function Hanna Reitz
2022-03-22 11:56 ` [PULL 16/25] iotests: change supports_quorum to use qemu_img Hanna Reitz
2022-03-22 11:56 ` [PULL 17/25] iotests: replace unchecked calls to qemu_img_pipe() Hanna Reitz
2022-03-22 11:56 ` [PULL 18/25] iotests/149: Remove qemu_img_pipe() call Hanna Reitz
2022-03-22 11:56 ` [PULL 19/25] iotests: remove remaining calls to qemu_img_pipe() Hanna Reitz
2022-03-22 11:56 ` [PULL 20/25] iotests: use qemu_img() in has_working_luks() Hanna Reitz
2022-03-22 11:56 ` [PULL 21/25] iotests: replace qemu_img_log('create', ...) calls Hanna Reitz
2022-03-22 11:56 ` [PULL 22/25] iotests: remove qemu_img_pipe_and_status() Hanna Reitz
2022-03-22 11:56 ` [PULL 23/25] iotests: make qemu_img_log and img_info_log raise on error Hanna Reitz
2022-03-22 11:56 ` [PULL 24/25] iotests.py: Filters for VM.run_job() Hanna Reitz
2022-03-22 11:56 ` [PULL 25/25] iotests/207: Filter host fingerprint Hanna Reitz
2022-03-22 17:05 ` [PULL 00/25] Block patches for 7.0-rc1 Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220322115647.726044-12-hreitz@redhat.com \
--to=hreitz@redhat.com \
--cc=kwolf@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).