From nobody Sun Feb 8 20:52:17 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1571920517; cv=none; d=zoho.com; s=zohoarc; b=nhUU55ByMaBixUtf90rOMJ5Ood3nIcL1y32WnPj9qhEdneqNDg3+1aBNxbVFjzur3qH8Pe5fnehNYTjjlv5CdgaSxUHIre846OODcE1lRyIXlZ0qUd9WxFLYPGZdKt9WWlhqXx1aVNjTQRvN26UYUb8jKwaV96+LIrr9KXh8zO4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571920517; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=7zqnbAE5N7BdnLdZXmqhSc52WToIyp0Xrdfz+3woMlA=; b=N7C2yfAF1Eg+FTUrZAUCJD3xsGGzD8fZnvbkHr8R4pkg4i5lqdFAqdYEXML/9wvK/PEDLDuj60EO0w0ZxZZhlbK2+Ah4vP2Am4AA1XxSMxr8nFVITGIH3Aw35bxXSf6oBCxeTu3vucJLHkYuOQhbH6JIq5RlSFIrN79udWqlVHk= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1571920517064723.2642827185102; Thu, 24 Oct 2019 05:35:17 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-254-QZLICSKbPNWYLQxD5T_Lhw-1; Thu, 24 Oct 2019 08:35:14 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7CB86107AD31; Thu, 24 Oct 2019 12:35:09 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C146660167; Thu, 24 Oct 2019 12:35:06 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 35AFB18005A0; Thu, 24 Oct 2019 12:35:04 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x9OCZ37R004415 for ; Thu, 24 Oct 2019 08:35:03 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4B61E1C93D; Thu, 24 Oct 2019 12:35:03 +0000 (UTC) Received: from blackfin.pond.sub.org (unknown [10.36.118.123]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8EB6A19D70; Thu, 24 Oct 2019 12:35:00 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 045121132D6D; Thu, 24 Oct 2019 14:34:59 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1571920516; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=7zqnbAE5N7BdnLdZXmqhSc52WToIyp0Xrdfz+3woMlA=; b=BYXruw6dfLwgEG8k7A/jaoobIx8iPqdTDRcRCZv5hkLBf48zJLOEFMAdPzrMk2gmemkoBs XRMnbrLXkDqxqZ/GLS03vzrU/ncGllTtoUOgmg9bwNKemrZc5BTKUMInb6XlZHFtiDxFQu imynBTxi4nM5bImPwzFEkQkkPucvlSY= From: Markus Armbruster To: qemu-devel@nongnu.org Date: Thu, 24 Oct 2019 14:34:42 +0200 Message-Id: <20191024123458.13505-4-armbru@redhat.com> In-Reply-To: <20191024123458.13505-1-armbru@redhat.com> References: <20191024123458.13505-1-armbru@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com Cc: libvir-list@redhat.com Subject: [libvirt] [RFC PATCH 03/19] tests/test-qmp-cmds: Simplify test data setup X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-MC-Unique: QZLICSKbPNWYLQxD5T_Lhw-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Building requests with qdict_put() & friends is tedious to write and hard to read. Parse them from string literals with qdict_from_vjsonf_nofail() instead. Signed-off-by: Markus Armbruster --- tests/test-qmp-cmds.c | 93 ++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 55 deletions(-) diff --git a/tests/test-qmp-cmds.c b/tests/test-qmp-cmds.c index 667e03cb1b..3798ba1b16 100644 --- a/tests/test-qmp-cmds.c +++ b/tests/test-qmp-cmds.c @@ -1,5 +1,6 @@ #include "qemu/osdep.h" #include "qapi/qmp/qdict.h" +#include "qapi/qmp/qjson.h" #include "qapi/qmp/qnum.h" #include "qapi/qmp/qstring.h" #include "qapi/error.h" @@ -144,11 +145,16 @@ __org_qemu_x_Union1 *qmp___org_qemu_x_command(__org_q= emu_x_EnumList *a, } =20 =20 -static QObject *do_qmp_dispatch(QDict *req, bool allow_oob) +static QObject *do_qmp_dispatch(bool allow_oob, const char *template, ...) { - QDict *resp; + va_list ap; + QDict *req, *resp; QObject *ret; =20 + va_start(ap, template); + req =3D qdict_from_vjsonf_nofail(template, ap); + va_end(ap); + resp =3D qmp_dispatch(&qmp_commands, QOBJECT(req), allow_oob); g_assert(resp); ret =3D qdict_get(resp, "return"); @@ -157,14 +163,21 @@ static QObject *do_qmp_dispatch(QDict *req, bool allo= w_oob) =20 qobject_ref(ret); qobject_unref(resp); + qobject_unref(req); return ret; } =20 -static void do_qmp_dispatch_error(QDict *req, bool allow_oob, ErrorClass c= ls) +static void do_qmp_dispatch_error(bool allow_oob, ErrorClass cls, + const char *template, ...) { - QDict *resp; + va_list ap; + QDict *req, *resp; QDict *error; =20 + va_start(ap, template); + req =3D qdict_from_vjsonf_nofail(template, ap); + va_end(ap); + resp =3D qmp_dispatch(&qmp_commands, QOBJECT(req), allow_oob); g_assert(resp); error =3D qdict_get_qdict(resp, "error"); @@ -176,59 +189,43 @@ static void do_qmp_dispatch_error(QDict *req, bool al= low_oob, ErrorClass cls) g_assert(qdict_size(resp) =3D=3D 1); =20 qobject_unref(resp); + qobject_unref(req); } =20 /* test commands with no input and no return value */ static void test_dispatch_cmd(void) { - QDict *req =3D qdict_new(); QDict *ret; =20 - qdict_put_str(req, "execute", "user_def_cmd"); - - ret =3D qobject_to(QDict, do_qmp_dispatch(req, false)); + ret =3D qobject_to(QDict, + do_qmp_dispatch(false, + "{ 'execute': 'user_def_cmd' }")); assert(ret && qdict_size(ret) =3D=3D 0); - qobject_unref(ret); - qobject_unref(req); } =20 static void test_dispatch_cmd_oob(void) { - QDict *req =3D qdict_new(); QDict *ret; =20 - qdict_put_str(req, "exec-oob", "test-flags-command"); - - ret =3D qobject_to(QDict, do_qmp_dispatch(req, true)); + ret =3D qobject_to(QDict, + do_qmp_dispatch(true, + "{ 'exec-oob': 'test-flags-command' }= ")); assert(ret && qdict_size(ret) =3D=3D 0); - qobject_unref(ret); - qobject_unref(req); } =20 /* test commands that return an error due to invalid parameters */ static void test_dispatch_cmd_failure(void) { - QDict *req =3D qdict_new(); - QDict *args =3D qdict_new(); - - qdict_put_str(req, "execute", "user_def_cmd2"); - - do_qmp_dispatch_error(req, false, ERROR_CLASS_GENERIC_ERROR); - - qobject_unref(req); - - /* check that with extra arguments it throws an error */ - req =3D qdict_new(); - qdict_put_int(args, "a", 66); - qdict_put(req, "arguments", args); - - qdict_put_str(req, "execute", "user_def_cmd"); - - do_qmp_dispatch_error(req, false, ERROR_CLASS_GENERIC_ERROR); - - qobject_unref(req); + /* missing arguments */ + do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR, + "{ 'execute': 'user_def_cmd2' }"); + + /* extra arguments */ + do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR, + "{ 'execute': 'user_def_cmd'," + " 'arguments': { 'a': 66 } }"); } =20 static void test_dispatch_cmd_success_response(void) @@ -245,26 +242,15 @@ static void test_dispatch_cmd_success_response(void) /* test commands that involve both input parameters and return values */ static void test_dispatch_cmd_io(void) { - QDict *req =3D qdict_new(); - QDict *args =3D qdict_new(); - QDict *args3 =3D qdict_new(); - QDict *ud1a =3D qdict_new(); - QDict *ud1b =3D qdict_new(); QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef; QDict *ret_dict_dict2, *ret_dict_dict2_userdef; QNum *ret3; int64_t val; =20 - qdict_put_int(ud1a, "integer", 42); - qdict_put_str(ud1a, "string", "hello"); - qdict_put_int(ud1b, "integer", 422); - qdict_put_str(ud1b, "string", "hello2"); - qdict_put(args, "ud1a", ud1a); - qdict_put(args, "ud1b", ud1b); - qdict_put(req, "arguments", args); - qdict_put_str(req, "execute", "user_def_cmd2"); - - ret =3D qobject_to(QDict, do_qmp_dispatch(req, false)); + ret =3D qobject_to(QDict, do_qmp_dispatch(false, + "{ 'execute': 'user_def_cmd2', 'arguments': {" + " 'ud1a': { 'integer': 42, 'string': 'hello' }," + " 'ud1b': { 'integer': 422, 'string': 'hello2' } } }")); =20 assert(!strcmp(qdict_get_str(ret, "string0"), "blah1")); ret_dict =3D qdict_get_qdict(ret, "dict1"); @@ -281,11 +267,8 @@ static void test_dispatch_cmd_io(void) assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4")); qobject_unref(ret); =20 - qdict_put_int(args3, "a", 66); - qdict_put(req, "arguments", args3); - qdict_put_str(req, "execute", "guest-get-time"); - - ret3 =3D qobject_to(QNum, do_qmp_dispatch(req, false)); + ret3 =3D qobject_to(QNum, do_qmp_dispatch(false, + "{ 'execute': 'guest-get-time', 'arguments': { 'a': 66 } }")); g_assert(qnum_get_try_int(ret3, &val)); g_assert_cmpint(val, =3D=3D, 66); qobject_unref(ret3); --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list