testQemuGetRealCaps: Integrate fetching of QMP schema

Move the lookup of the corresponding QMP schema used for validation of
QMP commands from 'testCompareXMLToArgvValidateSchema' to
testQemuGetRealCaps as an optional step.

This will simplify using QMP command validation in other tests which
will use testQemuGetRealCaps.

'testutilsqemuschema' module is now linked into 'test_utils_qemu' as it
contains no monitor-specific code itself and after this patch it's
referenced directly from that module.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2023-03-09 15:15:40 +01:00
parent 6013561a82
commit 83967a9ed2
4 changed files with 28 additions and 32 deletions

View File

@ -147,13 +147,13 @@ endif
if conf.has('WITH_QEMU') if conf.has('WITH_QEMU')
test_utils_qemu_lib = static_library( test_utils_qemu_lib = static_library(
'test_utils_qemu', 'test_utils_qemu',
[ 'testutilsqemu.c' ], [ 'testutilsqemu.c', 'testutilsqemuschema.c' ],
dependencies: [ tests_dep ], dependencies: [ tests_dep ],
) )
test_utils_qemu_monitor_lib = static_library( test_utils_qemu_monitor_lib = static_library(
'test_utils_qemu_monitor', 'test_utils_qemu_monitor',
[ 'qemumonitortestutils.c', 'testutilsqemuschema.c' ], [ 'qemumonitortestutils.c', ],
dependencies: [ tests_dep ], dependencies: [ tests_dep ],
) )

View File

@ -549,32 +549,14 @@ testCompareXMLToArgvValidateSchema(virCommand *cmd,
struct testQemuInfo *info) struct testQemuInfo *info)
{ {
g_auto(GStrv) args = NULL; g_auto(GStrv) args = NULL;
GHashTable *schema = NULL;
/* comment out with line comment to enable schema checking for non _CAPS tests if (!info->qmpSchema)
if (!info->schemafile)
info->schemafile = testQemuGetLatestCapsForArch(virArchToString(info->arch), "replies");
// */
if (info->schemafile) {
/* lookup and insert into cache if not found */
if (!g_hash_table_lookup_extended(info->conf->qapiSchemaCache,
info->schemafile,
NULL, (void **) &schema)) {
schema = testQEMUSchemaLoad(info->schemafile);
g_hash_table_insert(info->conf->qapiSchemaCache,
g_strdup(info->schemafile),
schema);
}
}
if (!schema)
return 0; return 0;
if (virCommandGetArgList(cmd, &args) < 0) if (virCommandGetArgList(cmd, &args) < 0)
return -1; return -1;
if (testCompareXMLToArgvValidateSchemaCommand(args, schema) < 0) if (testCompareXMLToArgvValidateSchemaCommand(args, info->qmpSchema) < 0)
return -1; return -1;
return 0; return 0;

View File

@ -2,6 +2,7 @@
#ifdef WITH_QEMU #ifdef WITH_QEMU
# include "testutilsqemu.h" # include "testutilsqemu.h"
# include "testutilsqemuschema.h"
# include "testutilshostcpus.h" # include "testutilshostcpus.h"
# include "testutils.h" # include "testutils.h"
# include "viralloc.h" # include "viralloc.h"
@ -910,11 +911,16 @@ testQemuInfoSetArgs(struct testQemuInfo *info,
* @variant: capabilities variant to fetch caps for * @variant: capabilities variant to fetch caps for
* @capsLatestFiles: hash table containing latest version of capabilities for the @arch+@variant tuple * @capsLatestFiles: hash table containing latest version of capabilities for the @arch+@variant tuple
* @capsCache: hash table filled with the cache of capabilities * @capsCache: hash table filled with the cache of capabilities
* @capsReplies: Filled with path to the corresponding '.replies' file * @schemaCache: hash table for caching QMP schemas (may be NULL, see below)
* @schema: Filled with the QMP schema (hash table) (may be NULL, see below)
* *
* Fetches and returns the appropriate virQEMUCaps for the @arch+@version+@variant * Fetches and returns the appropriate virQEMUCaps for the @arch+@version+@variant
* tuple. The returned pointer is a copy of the cached object and thus can * tuple. The returned pointer is a copy of the cached object and thus can
* be freely modified. Caller is responsible for freeing it. * be freely modified. Caller is responsible for freeing it.
*
* If @schemaCache and @schema are non-NULL, @schema is filled with with a
* pointer (borrowed from the cache) to the hash table representing the QEMU QMP
* schema used for validation of the monitor traffic.
*/ */
virQEMUCaps * virQEMUCaps *
testQemuGetRealCaps(const char *arch, testQemuGetRealCaps(const char *arch,
@ -922,7 +928,8 @@ testQemuGetRealCaps(const char *arch,
const char *variant, const char *variant,
GHashTable *capsLatestFiles, GHashTable *capsLatestFiles,
GHashTable *capsCache, GHashTable *capsCache,
char **capsReplies) GHashTable *schemaCache,
GHashTable **schema)
{ {
g_autofree char *capsfile = NULL; g_autofree char *capsfile = NULL;
bool stripmachinealiases = false; bool stripmachinealiases = false;
@ -960,10 +967,16 @@ testQemuGetRealCaps(const char *arch,
if (stripmachinealiases) if (stripmachinealiases)
virQEMUCapsStripMachineAliases(ret); virQEMUCapsStripMachineAliases(ret);
if (capsReplies) { /* strip 'xml' suffix so that we can format the file to '.replies' */
/* provide path to the replies file for schema testing */ capsfile[strlen(capsfile) - 3] = '\0';
capsfile[strlen(capsfile) - 3] = '\0';
*capsReplies = g_strdup_printf("%sreplies", capsfile); if (schemaCache && schema) {
g_autofree char *schemafile = g_strdup_printf("%sreplies", capsfile);
if (!g_hash_table_lookup_extended(schemaCache, schemafile, NULL, (void **) schema)) {
*schema = testQEMUSchemaLoad(schemafile);
g_hash_table_insert(schemaCache, g_strdup(schemafile), *schema);
}
} }
return ret; return ret;
@ -1001,7 +1014,8 @@ testQemuInfoInitArgs(struct testQemuInfo *info)
info->args.capsvariant, info->args.capsvariant,
info->conf->capslatest, info->conf->capslatest,
info->conf->capscache, info->conf->capscache,
&info->schemafile); info->conf->qapiSchemaCache,
&info->qmpSchema);
if (!info->qemuCaps) if (!info->qemuCaps)
return -1; return -1;
@ -1028,7 +1042,6 @@ testQemuInfoClear(struct testQemuInfo *info)
{ {
VIR_FREE(info->infile); VIR_FREE(info->infile);
VIR_FREE(info->outfile); VIR_FREE(info->outfile);
VIR_FREE(info->schemafile);
VIR_FREE(info->errfile); VIR_FREE(info->errfile);
virObjectUnref(info->qemuCaps); virObjectUnref(info->qemuCaps);
g_clear_pointer(&info->args.fakeCapsAdd, virBitmapFree); g_clear_pointer(&info->args.fakeCapsAdd, virBitmapFree);

View File

@ -99,7 +99,7 @@ struct testQemuInfo {
unsigned int flags; unsigned int flags;
unsigned int parseFlags; unsigned int parseFlags;
virArch arch; virArch arch;
char *schemafile; GHashTable *qmpSchema; /* borrowed pointer from the cache */
struct testQemuArgs args; struct testQemuArgs args;
struct testQemuConf *conf; struct testQemuConf *conf;
@ -158,5 +158,6 @@ testQemuGetRealCaps(const char *arch,
const char *variant, const char *variant,
GHashTable *capsLatestFiles, GHashTable *capsLatestFiles,
GHashTable *capsCache, GHashTable *capsCache,
char **capsReplies); GHashTable *schemaCache,
GHashTable **schema);
#endif #endif