All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Eric Blake <eblake@redhat.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH v4 13/22] libqtest: Add qmp_raw()
Date: Wed, 09 Aug 2017 16:54:58 +0200	[thread overview]
Message-ID: <87inhwokot.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <20170804012551.2714-14-eblake@redhat.com> (Eric Blake's message of "Thu, 3 Aug 2017 20:25:42 -0500")

Eric Blake <eblake@redhat.com> writes:

> The majority of calls into libqtest's qmp() and friends are passing
> a JSON object that includes a command name; we can prove this by
> adding an assertion.  The only outlier is qmp-test, which is testing
> appropriate error responses to protocol violations and id support,
> by sending raw strings, where those raw strings don't need
> interpolation anyways.
>
> Adding a new entry-point makes a clean separation of which input
> needs interpolation, so that upcoming patches can refactor qmp()
> to be more like the recent additions to test-qga in taking the
> command name separately from the arguments for an overall
> reduction in testsuite boilerplate.
>
> This change also lets us move the workaround for the QMP parser
> limitation of not ending a parse until } or newline: all calls
> through qmp() are passing an object ending in }, so only the
> few callers of qmp_raw() have to worry about a trailing newline.
>
> Signed-off-by: Eric Blake <eblake@redhat.com>
> ---
>  tests/libqtest.h |  8 ++++++++
>  tests/libqtest.c | 13 +++++++------
>  tests/qmp-test.c | 19 ++++++++++++-------
>  3 files changed, 27 insertions(+), 13 deletions(-)
>
> diff --git a/tests/libqtest.h b/tests/libqtest.h
> index 33af3ae8ff..917ec5cf92 100644
> --- a/tests/libqtest.h
> +++ b/tests/libqtest.h
> @@ -550,6 +550,14 @@ static inline void qtest_end(void)
>  QDict *qmp(const char *fmt, ...);
>
>  /**
> + * qmp_raw:
> + * @msg: Raw QMP message to send to qemu.
> + *
> + * Sends a QMP message to QEMU and returns the response.
> + */
> +QDict *qmp_raw(const char *msg);
> +
> +/**
>   * qmp_async:
>   * @fmt...: QMP message to send to qemu; formats arguments through
>   * json-lexer.c (only understands '%(PRI[ud]64|(l|ll)?[du]|[ipsf%])').
> diff --git a/tests/libqtest.c b/tests/libqtest.c
> index 0fa32928c8..3071be2efb 100644
> --- a/tests/libqtest.c
> +++ b/tests/libqtest.c
> @@ -451,7 +451,7 @@ static void qmp_fd_sendv(int fd, const char *fmt, va_list ap)
>      QString *qstr;
>      const char *str;
>
> -    assert(*fmt);
> +    assert(strstr(fmt, "execute"));

I doubt this assertion is worthwhile.

One , qmp_fd_sendv() works just fine whether you include an 'execute' or
not.  Two, there are zillions of other ways to send nonsense with
qmp_fd_sendv().  If you do, your test doesn't work, so you fix it.

Rejecting nonsensical QMP input is QEMU's job, not libqtest's.

>
>      /*
>       * A round trip through QObject is only needed if % interpolation
> @@ -496,11 +496,6 @@ void qmp_fd_send(int fd, const char *msg)
>      }
>      /* Send QMP request */
>      socket_send(fd, msg, strlen(msg));
> -    /*
> -     * BUG: QMP doesn't react to input until it sees a newline, an
> -     * object, or an array.  Work-around: give it a newline.
> -     */
> -    socket_send(fd, "\n", 1);
>  }
>
>  QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
> @@ -899,6 +894,12 @@ QDict *qmp(const char *fmt, ...)
>      return response;
>  }
>
> +QDict *qmp_raw(const char *msg)
> +{
> +    qmp_fd_send(global_qtest->qmp_fd, msg);
> +    return qtest_qmp_receive(global_qtest);
> +}
> +
>  void qmp_async(const char *fmt, ...)
>  {
>      va_list ap;
> diff --git a/tests/qmp-test.c b/tests/qmp-test.c
> index 5d0260b2be..905fb4b3e5 100644
> --- a/tests/qmp-test.c
> +++ b/tests/qmp-test.c
> @@ -44,28 +44,33 @@ static void test_malformed(void)
>  {
>      QDict *resp;
>
> +    /*
> +     * BUG: QMP doesn't react to input until it sees a newline, an
> +     * object, or an array.  Work-around: give it a newline.
> +     */
> +
>      /* Not even a dictionary */
> -    resp = qmp("null");
> +    resp = qmp_raw("null\n");
>      g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
>      QDECREF(resp);
>
>      /* No "execute" key */
> -    resp = qmp("{}");
> +    resp = qmp_raw("{}");
>      g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
>      QDECREF(resp);
>
>      /* "execute" isn't a string */
> -    resp = qmp("{ 'execute': true }");
> +    resp = qmp_raw("{ 'execute': true }");
>      g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
>      QDECREF(resp);
>
>      /* "arguments" isn't a dictionary */
> -    resp = qmp("{ 'execute': 'no-such-cmd', 'arguments': [] }");
> +    resp = qmp_raw("{ 'execute': 'no-such-cmd', 'arguments': [] }");
>      g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
>      QDECREF(resp);
>
>      /* extra key */
> -    resp = qmp("{ 'execute': 'no-such-cmd', 'extra': true }");
> +    resp = qmp_raw("{ 'execute': 'no-such-cmd', 'extra': true }");
>      g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
>      QDECREF(resp);
>  }
> @@ -114,14 +119,14 @@ static void test_qmp_protocol(void)
>      test_malformed();
>
>      /* Test 'id' */
> -    resp = qmp("{ 'execute': 'query-name', 'id': 'cookie#1' }");
> +    resp = qmp_raw("{ 'execute': 'query-name', 'id': 'cookie#1' }");
>      ret = qdict_get_qdict(resp, "return");
>      g_assert(ret);
>      g_assert_cmpstr(qdict_get_try_str(resp, "id"), ==, "cookie#1");
>      QDECREF(resp);
>
>      /* Test command failure with 'id' */
> -    resp = qmp("{ 'execute': 'human-monitor-command', 'id': 2 }");
> +    resp = qmp_raw("{ 'execute': 'human-monitor-command', 'id': 2 }");
>      g_assert_cmpstr(get_error_class(resp), ==, "GenericError");
>      g_assert_cmpint(qdict_get_int(resp, "id"), ==, 2);
>      QDECREF(resp);

I'm afraid I don't like this patch.  All the extra function buys us is
an assertion that isn't even tight, and the lifting of a newline out of
a place where it shouldn't be.

  reply	other threads:[~2017-08-09 14:55 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-04  1:25 [Qemu-devel] [PATCH v4 00/22] Clean up around qmp() and hmp() Eric Blake
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 01/22] qobject: Accept "%"PRId64 in qobject_from_jsonf() Eric Blake
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 02/22] tests: Clean up wait for event Eric Blake
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 03/22] tests/libqtest: Clean up how we read the QMP greeting Eric Blake
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 04/22] tests: Add assertion for no qmp("") Eric Blake
2017-08-09  7:13   ` Markus Armbruster
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 05/22] qobject: Simplify qobject_from_jsonv() Eric Blake
2017-08-09  7:59   ` Markus Armbruster
2017-08-09 13:14     ` Eric Blake
2017-10-02  5:46       ` Markus Armbruster
2017-10-02 14:30         ` Eric Blake
2018-01-08 16:46           ` Eric Blake
2017-09-11 21:52   ` Eric Blake
2017-10-02  7:15     ` Markus Armbruster
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 06/22] qobject: Perform %% interpolation in qobject_from_jsonf() Eric Blake
2017-08-09  9:06   ` Markus Armbruster
2017-08-09 13:21     ` Eric Blake
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 07/22] numa-test: Use hmp() Eric Blake
2017-08-09  9:07   ` Markus Armbruster
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 08/22] qtest: Avoid passing raw strings through hmp() Eric Blake
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 09/22] qtest: Document calling conventions Eric Blake
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 10/22] libqtest: Skip round-trip through QObject Eric Blake
2017-08-09 10:10   ` Markus Armbruster
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 11/22] test-qga: Simplify command construction Eric Blake
2017-08-09 11:40   ` Markus Armbruster
2017-08-09 13:29     ` Eric Blake
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 12/22] libqtest: Change qmp_fd_send() to drop varargs Eric Blake
2017-08-09 13:18   ` Markus Armbruster
2017-08-09 13:44     ` Eric Blake
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 13/22] libqtest: Add qmp_raw() Eric Blake
2017-08-09 14:54   ` Markus Armbruster [this message]
2017-08-09 15:18     ` Eric Blake
2017-08-10  7:29       ` Markus Armbruster
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 14/22] libqtest: Separate qmp_discard_response() from command Eric Blake
2017-08-09 15:15   ` Markus Armbruster
2017-08-09 15:32     ` Eric Blake
2017-08-10  7:40       ` Markus Armbruster
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 15/22] libqtest: Delete qtest_qmp() wrappers Eric Blake
2017-08-09 15:34   ` Markus Armbruster
2017-08-09 16:35     ` Eric Blake
2017-08-10  7:47       ` Markus Armbruster
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 16/22] libqtest: Add qmp_cmd() helper Eric Blake
2017-08-09 15:40   ` Markus Armbruster
2017-08-09 16:39     ` Eric Blake
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 17/22] libqtest: Add qmp_args() helper Eric Blake
2017-08-09 15:57   ` Markus Armbruster
2017-08-09 21:57     ` Eric Blake
2017-08-10  8:17       ` Markus Armbruster
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 18/22] tests/libqos/usb: Clean up string interpolation into QMP input Eric Blake
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 19/22] libqtest: Add qmp_args_dict() helper Eric Blake
2017-08-09 15:59   ` Markus Armbruster
2017-08-09 16:41     ` Eric Blake
2017-08-10  8:19       ` Markus Armbruster
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 20/22] tests/libqos/pci: Clean up string interpolation into QMP input Eric Blake
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 21/22] libqtest: Drop now-unused qmp() Eric Blake
2017-08-09 16:01   ` Markus Armbruster
2017-08-09 16:43     ` Eric Blake
2017-08-04  1:25 ` [Qemu-devel] [PATCH v4 22/22] libqtest: Rename qmp_cmd() to qmp() Eric Blake
2017-08-04  1:54 ` [Qemu-devel] [PATCH v4 00/22] Clean up around qmp() and hmp() no-reply
2017-08-04 11:50   ` Eric Blake
2017-08-04 12:10     ` Fam Zheng
2017-08-07  6:43       ` Fam Zheng
2017-08-07  7:33         ` Fam Zheng
2017-08-07 14:08           ` Philippe Mathieu-Daudé
2017-08-07  8:09   ` Fam Zheng
2017-08-04  2:02 ` no-reply

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=87inhwokot.fsf@dusky.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=eblake@redhat.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.