Eric Blake <ebl...@redhat.com> writes:

> When interpolating a QMP command from the testsuite, we have
> a very common pattern that the command to be executed is a
> string constant, while the arguments may need to be crafted
> from qobject_from_jsonf() or even by hand.  Make it easier to
> craft the arguments without having to save all the interpolation
> to the final qmp() call, by adding new helper functions; the
> function takes QObject instead of QDict in order to reduce the
> number of conversions between the two.
>
> Signed-off-by: Eric Blake <ebl...@redhat.com>
> ---
>  tests/libqtest.h | 31 +++++++++++++++++++++++++++++++
>  tests/libqtest.c | 25 +++++++++++++++++++++++++
>  2 files changed, 56 insertions(+)
>
> diff --git a/tests/libqtest.h b/tests/libqtest.h
> index 82e89b4d4f..26930fed4d 100644
> --- a/tests/libqtest.h
> +++ b/tests/libqtest.h
> @@ -68,6 +68,17 @@ void qtest_qmp_discard_response(QTestState *s, const char 
> *fmt, ...);
>  QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
>
>  /**
> + * qtest_qmp_cmd:
> + * @s: #QTestState instance to operate on.
> + * @cmd: Command name to send
> + * @args: Arguments to transfer to the command, or NULL.

Do we really need "or NULL"?

> + *
> + * Sends a QMP message to QEMU and returns the response. Calling this will
> + * reduce the reference count of @args.

Suggest "decrement".  Same for the wrappers below.

> + */
> +QDict *qtest_qmp_cmd(QTestState *s, const char *cmd, QObject *args);
> +
> +/**
>   * qtest_async_qmp:
>   * @s: #QTestState instance to operate on.
>   * @fmt...: QMP message to send to qemu; formats arguments through
> @@ -550,6 +561,16 @@ static inline void qtest_end(void)
>  QDict *qmp(const char *fmt, ...);
>
>  /**
> + * qmp_cmd:
> + * @cmd: Command name to send
> + * @args: Arguments to transfer to the command, or NULL.
> + *
> + * Sends a QMP message to QEMU and returns the response. Calling this will
> + * reduce the reference count of @args.
> + */
> +QDict *qmp_cmd(const char *cmd, QObject *args);
> +
> +/**
>   * qmp_async:
>   * @fmt...: QMP message to send to qemu; formats arguments through
>   * json-lexer.c (only understands '%(PRI[ud]64|(l|ll)?[du]|[ipsf])').
> @@ -568,6 +589,16 @@ void qmp_async(const char *fmt, ...);
>  void qmp_discard_response(const char *fmt, ...);
>
>  /**
> + * qmp_cmd_discard_response:
> + * @cmd: Command name to send
> + * @args: Arguments to transfer to the command, or NULL.
> + *
> + * Sends a QMP message to QEMU and consumes the response. Calling this will
> + * reduce the reference count of @args.
> + */
> +void qmp_cmd_discard_response(const char *cmd, QObject *args);
> +
> +/**
>   * qmp_receive:
>   *
>   * Reads a QMP message from QEMU and returns the response.
> diff --git a/tests/libqtest.c b/tests/libqtest.c
> index 4a5492a603..1b57cedca1 100644
> --- a/tests/libqtest.c
> +++ b/tests/libqtest.c
> @@ -540,6 +540,17 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
>      return response;
>  }
>
> +QDict *qtest_qmp_cmd(QTestState *s, const char *cmd, QObject *args)
> +{
> +    QDict *dict = qdict_new();
> +
> +    qdict_put_str(dict, "execute", cmd);
> +    if (args) {
> +        qdict_put_obj(dict, "arguments", args);
> +    }
> +    return qtest_qmp(s, "%p", QOBJECT(dict));

This function should be a one-liner:

       return qtest_qmp(s, "{'execute': %s, 'arguments': %p }", cmd, args);

A bit more complicated if we need to support null @args.

The less we clown around with qdict, the happier I am.  There's no
manual qdict building anywhere else in this file.

> +}
> +
>  void qtest_async_qmp(QTestState *s, const char *fmt, ...)
>  {
>      va_list ap;
> @@ -925,6 +936,11 @@ QDict *qmp(const char *fmt, ...)
>      return response;
>  }
>
> +QDict *qmp_cmd(const char *cmd, QObject *args)
> +{
> +    return qtest_qmp_cmd(global_qtest, cmd, args);
> +}
> +
>  void qmp_async(const char *fmt, ...)
>  {
>      va_list ap;
> @@ -942,6 +958,15 @@ void qmp_discard_response(const char *fmt, ...)
>      qtest_qmpv_discard_response(global_qtest, fmt, ap);
>      va_end(ap);
>  }
> +
> +void qmp_cmd_discard_response(const char *cmd, QObject *args)
> +{
> +    QDict *response;
> +
> +    response = qmp_cmd(cmd, args);
> +    QDECREF(response);
> +}
> +
>  char *hmp(const char *fmt, ...)
>  {
>      va_list ap;

Reply via email to