From nobody Wed May 14 12:40:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 152174358643847.70100590376603; Thu, 22 Mar 2018 11:33:06 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 853EFC0587C3; Thu, 22 Mar 2018 18:33:04 +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 213B36B8EE; Thu, 22 Mar 2018 18:33:04 +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 97D36180BADB; Thu, 22 Mar 2018 18:33:03 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w2MIW0lM015675 for ; Thu, 22 Mar 2018 14:32:01 -0400 Received: by smtp.corp.redhat.com (Postfix) id D222D8442D; Thu, 22 Mar 2018 18:32:00 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.136]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5B4F984436; Thu, 22 Mar 2018 18:32:00 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Thu, 22 Mar 2018 19:31:47 +0100 Message-Id: <172ea5cd0acd19f71a16b44d2fca5296b30d24f7.1521743167.git.pkrempa@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 10/11] tests: qemumonitor: Allow testing schema for fake monitor interactions 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 22 Mar 2018 18:33:05 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add infrastructure that will allow testing schema of the commands we pass to the fake monitor object, so that we can make sure that it actually does something. Signed-off-by: Peter Krempa --- tests/Makefile.am | 2 +- tests/qemuhotplugtest.c | 3 +- tests/qemumonitortestutils.c | 85 ++++++++++++++++++++++++++++++++++++++++= ++-- tests/qemumonitortestutils.h | 7 ++-- 4 files changed, 90 insertions(+), 7 deletions(-) diff --git a/tests/Makefile.am b/tests/Makefile.am index cf254f65c3..289ef35bdd 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -555,6 +555,7 @@ endif ! WITH_LIBXL QEMUMONITORTESTUTILS_SOURCES =3D \ qemumonitortestutils.c \ qemumonitortestutils.h \ + testutilsqemuschema.h testutilsqemuschema.c \ $(NULL) if WITH_QEMU @@ -616,7 +617,6 @@ qemumonitorjsontest_SOURCES =3D \ qemumonitorjsontest.c \ testutils.c testutils.h \ testutilsqemu.c testutilsqemu.h \ - testutilsqemuschema.c testutilsqemuschema.h \ $(NULL) qemumonitorjsontest_LDADD =3D libqemumonitortestutils.la \ $(qemu_LDADDS) $(LDADDS) diff --git a/tests/qemuhotplugtest.c b/tests/qemuhotplugtest.c index d42f8e12cb..85e53653e1 100644 --- a/tests/qemuhotplugtest.c +++ b/tests/qemuhotplugtest.c @@ -283,7 +283,8 @@ testQemuHotplug(const void *data) /* Now is the best time to feed the spoofed monitor with predefined * replies. */ - if (!(test_mon =3D qemuMonitorTestNew(true, driver.xmlopt, vm, &driver= , NULL))) + if (!(test_mon =3D qemuMonitorTestNew(true, driver.xmlopt, vm, &driver, + NULL, NULL))) goto cleanup; tmp =3D test->mon; diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c index 5e30fb067c..1232b8ebe3 100644 --- a/tests/qemumonitortestutils.c +++ b/tests/qemumonitortestutils.c @@ -25,12 +25,14 @@ #include #include "testutils.h" +#include "testutilsqemuschema.h" #include "qemumonitortestutils.h" #include "virthread.h" #include "qemu/qemu_processpriv.h" #include "qemu/qemu_monitor.h" #include "qemu/qemu_agent.h" +#include "qemu/qemu_qapi.h" #include "rpc/virnetsocket.h" #include "viralloc.h" #include "virlog.h" @@ -76,6 +78,7 @@ struct _qemuMonitorTest { qemuMonitorTestItemPtr *items; virDomainObjPtr vm; + virHashTablePtr qapischema; }; @@ -537,6 +540,67 @@ qemuMonitorTestHandlerDataFree(void *opaque) VIR_FREE(data); } + +/* Returns -1 on error, 0 if validation was successful/not necessary, 1 if + * the validation has failed, and the reply was properly constructed */ +static int +qemuMonitorTestProcessCommandDefaultValidate(qemuMonitorTestPtr test, + const char *cmdname, + virJSONValuePtr args) +{ + virBuffer debug =3D VIR_BUFFER_INITIALIZER; + virJSONValuePtr schemaroot; + virJSONValuePtr emptyargs =3D NULL; + char *schemapath =3D NULL; + int ret =3D -1; + + if (!test->qapischema || !test->json || test->agent) + return 0; + + /* 'device_add' needs to be skipped as it does not have fully defined = schema */ + if (STREQ(cmdname, "device_add")) + return 0; + + if (virAsprintf(&schemapath, "%s/arg-type", cmdname) < 0) + goto cleanup; + + if (virQEMUQapiSchemaPathGet(schemapath, test->qapischema, &schemaroot= ) < 0) { + if (qemuMonitorReportError(test, + "command '%s' not found in QAPI schema", + cmdname) =3D=3D 0) + ret =3D 1; + goto cleanup; + } + + if (!args) { + if (!(emptyargs =3D virJSONValueNewObject())) + goto cleanup; + + args =3D emptyargs; + } + + if (testQEMUSchemaValidate(args, schemaroot, test->qapischema, &debug)= < 0) { + char *debugmsg =3D virBufferContentAndReset(&debug); + if (qemuMonitorReportError(test, + "failed to validate arguments of '%s' " + "against QAPI schema: %s", + cmdname, debugmsg) =3D=3D 0) + ret =3D 1; + + VIR_FREE(debugmsg); + goto cleanup; + } + + ret =3D 0; + + cleanup: + virBufferFreeAndReset(&debug); + virJSONValueFree(emptyargs); + VIR_FREE(schemapath); + return ret; +} + + static int qemuMonitorTestProcessCommandDefault(qemuMonitorTestPtr test, qemuMonitorTestItemPtr item, @@ -544,10 +608,12 @@ qemuMonitorTestProcessCommandDefault(qemuMonitorTestP= tr test, { struct qemuMonitorTestHandlerData *data =3D item->opaque; virJSONValuePtr val =3D NULL; + virJSONValuePtr cmdargs =3D NULL; char *cmdcopy =3D NULL; const char *cmdname; char *tmp; int ret =3D -1; + int rc; if (test->agent || test->json) { if (!(val =3D virJSONValueFromString(cmdstr))) @@ -557,6 +623,8 @@ qemuMonitorTestProcessCommandDefault(qemuMonitorTestPtr= test, ret =3D qemuMonitorReportError(test, "Missing command name in = %s", cmdstr); goto cleanup; } + + cmdargs =3D virJSONValueObjectGet(val, "arguments"); } else { if (VIR_STRDUP(cmdcopy, cmdstr) < 0) return -1; @@ -572,6 +640,14 @@ qemuMonitorTestProcessCommandDefault(qemuMonitorTestPt= r test, *tmp =3D '\0'; } + if ((rc =3D qemuMonitorTestProcessCommandDefaultValidate(test, cmdname= , cmdargs)) < 0) + goto cleanup; + + if (rc =3D=3D 1) { + ret =3D 0; + goto cleanup; + } + if (data->command_name && STRNEQ(data->command_name, cmdname)) ret =3D qemuMonitorTestAddInvalidCommandResponse(test, data->comma= nd_name, cmdname); @@ -1160,7 +1236,8 @@ qemuMonitorTestNew(bool json, virDomainXMLOptionPtr xmlopt, virDomainObjPtr vm, virQEMUDriverPtr driver, - const char *greeting) + const char *greeting, + virHashTablePtr schema) { qemuMonitorTestPtr test =3D NULL; virDomainChrSourceDef src; @@ -1171,6 +1248,7 @@ qemuMonitorTestNew(bool json, goto error; test->json =3D json; + test->qapischema =3D schema; if (!(test->mon =3D qemuMonitorOpen(test->vm, &src, json, @@ -1249,7 +1327,8 @@ qemuMonitorTestNewFromFile(const char *fileName, goto error; } else { /* Create new mocked monitor with our greeting */ - if (!(test =3D qemuMonitorTestNew(true, xmlopt, NULL, NULL= , singleReply))) + if (!(test =3D qemuMonitorTestNew(true, xmlopt, NULL, NULL, + singleReply, NULL))) goto error; } @@ -1331,7 +1410,7 @@ qemuMonitorTestNewFromFileFull(const char *fileName, if (virTestLoadFile(fileName, &jsonstr) < 0) return NULL; - if (!(ret =3D qemuMonitorTestNew(true, driver->xmlopt, vm, driver, NUL= L))) + if (!(ret =3D qemuMonitorTestNew(true, driver->xmlopt, vm, driver, NUL= L, NULL))) goto cleanup; tmp =3D jsonstr; diff --git a/tests/qemumonitortestutils.h b/tests/qemumonitortestutils.h index 8b19b37e71..d3dc02933b 100644 --- a/tests/qemumonitortestutils.h +++ b/tests/qemumonitortestutils.h @@ -74,13 +74,16 @@ int qemuMonitorTestAddItemExpect(qemuMonitorTestPtr tes= t, const char *response); # define qemuMonitorTestNewSimple(json, xmlopt) \ - qemuMonitorTestNew(json, xmlopt, NULL, NULL, NULL) + qemuMonitorTestNew(json, xmlopt, NULL, NULL, NULL, NULL) +# define qemuMonitorTestNewSchema(xmlopt, schema) \ + qemuMonitorTestNew(true, xmlopt, NULL, NULL, NULL, schema) qemuMonitorTestPtr qemuMonitorTestNew(bool json, virDomainXMLOptionPtr xmlopt, virDomainObjPtr vm, virQEMUDriverPtr driver, - const char *greeting); + const char *greeting, + virHashTablePtr schema); qemuMonitorTestPtr qemuMonitorTestNewFromFile(const char *fileName, virDomainXMLOptionPtr xmlopt, --=20 2.16.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list