diff mbox series

[1/3] tests/qtest: Add qtest_system_reset() utility function

Message ID 20241115165041.1148095-2-peter.maydell@linaro.org
State Accepted
Headers show
Series qtest: Provide and use function for doing system reset | expand

Commit Message

Peter Maydell Nov. 15, 2024, 4:50 p.m. UTC
We have several qtest tests which want to reset the QEMU under test
during the course of testing something.  They currently generally
have their own functions to do this, which work by sending a
"system_reset" QMP command.  However, "system_reset" only requests a
reset, and many of the tests which send the QMP command forget the
"and then wait for the QMP RESET event" part which is needed to
ensure that the reset has completed.

Provide a qtest_system_reset() function in libqtest so that
we don't need to reimplement this in multiple different tests.

A few tests (for example device hotplug related tests) want to
perform the reset command and then wait for some other event that is
produced during the reset sequence.  For them we provide
qtest_system_reset_nowait() so they can clearly indicate that they
are deliberately not waiting for the RESET event.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 tests/qtest/libqtest.h | 25 +++++++++++++++++++++++++
 tests/qtest/libqtest.c | 16 ++++++++++++++++
 2 files changed, 41 insertions(+)

Comments

Philippe Mathieu-Daudé Nov. 18, 2024, 10:30 a.m. UTC | #1
On 15/11/24 16:50, Peter Maydell wrote:
> We have several qtest tests which want to reset the QEMU under test
> during the course of testing something.  They currently generally
> have their own functions to do this, which work by sending a
> "system_reset" QMP command.  However, "system_reset" only requests a
> reset, and many of the tests which send the QMP command forget the
> "and then wait for the QMP RESET event" part which is needed to
> ensure that the reset has completed.
> 
> Provide a qtest_system_reset() function in libqtest so that
> we don't need to reimplement this in multiple different tests.
> 
> A few tests (for example device hotplug related tests) want to
> perform the reset command and then wait for some other event that is
> produced during the reset sequence.  For them we provide
> qtest_system_reset_nowait() so they can clearly indicate that they
> are deliberately not waiting for the RESET event.
> 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>   tests/qtest/libqtest.h | 25 +++++++++++++++++++++++++
>   tests/qtest/libqtest.c | 16 ++++++++++++++++
>   2 files changed, 41 insertions(+)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
diff mbox series

Patch

diff --git a/tests/qtest/libqtest.h b/tests/qtest/libqtest.h
index beb96b18ebd..f23d80e9e5e 100644
--- a/tests/qtest/libqtest.h
+++ b/tests/qtest/libqtest.h
@@ -88,6 +88,31 @@  QTestState *qtest_init_without_qmp_handshake(const char *extra_args);
  */
 QTestState *qtest_init_with_serial(const char *extra_args, int *sock_fd);
 
+/**
+ * qtest_system_reset:
+ * @s: #QTestState instance to operate on.
+ *
+ * Send a "system_reset" command to the QEMU under test, and wait for
+ * the reset to complete before returning.
+ */
+void qtest_system_reset(QTestState *s);
+
+/**
+ * qtest_system_reset_nowait:
+ * @s: #QTestState instance to operate on.
+ *
+ * Send a "system_reset" command to the QEMU under test, but do not
+ * wait for the reset to complete before returning. The caller is
+ * responsible for waiting for either the RESET event or some other
+ * event of interest to them before proceeding.
+ *
+ * This function should only be used if you're specifically testing
+ * for some other event; in that case you can't use qtest_system_reset()
+ * because it will read and discard any other QMP events that arrive
+ * before the RESET event.
+ */
+void qtest_system_reset_nowait(QTestState *s);
+
 /**
  * qtest_wait_qemu:
  * @s: #QTestState instance to operate on.
diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index 817fd7aac52..8de5f1fde30 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -215,6 +215,22 @@  static void qtest_check_status(QTestState *s)
 #endif
 }
 
+void qtest_system_reset_nowait(QTestState *s)
+{
+    /* Request the system reset, but do not wait for it to complete */
+    qtest_qmp_assert_success(s, "{'execute': 'system_reset' }");
+}
+
+void qtest_system_reset(QTestState *s)
+{
+    qtest_system_reset_nowait(s);
+    /*
+     * Wait for the RESET event, which is sent once the system reset
+     * has actually completed.
+     */
+    qtest_qmp_eventwait(s, "RESET");
+}
+
 void qtest_wait_qemu(QTestState *s)
 {
     if (s->qemu_pid != -1) {