[PATCH 13/31] testQemuGetRealCaps: Integrate fetching of QMP schema

Peter Krempa pkrempa at redhat.com
Thu Mar 30 13:38:07 UTC 2023


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 at redhat.com>
---
 tests/meson.build        |  4 ++--
 tests/qemuxml2argvtest.c | 22 ++--------------------
 tests/testutilsqemu.c    | 29 +++++++++++++++++++++--------
 tests/testutilsqemu.h    |  5 +++--
 4 files changed, 28 insertions(+), 32 deletions(-)

diff --git a/tests/meson.build b/tests/meson.build
index 11010ebc6c..8958e68a69 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -147,13 +147,13 @@ endif
 if conf.has('WITH_QEMU')
   test_utils_qemu_lib = static_library(
     'test_utils_qemu',
-    [ 'testutilsqemu.c' ],
+    [ 'testutilsqemu.c', 'testutilsqemuschema.c' ],
     dependencies: [ tests_dep ],
   )

   test_utils_qemu_monitor_lib = static_library(
     'test_utils_qemu_monitor',
-    [ 'qemumonitortestutils.c', 'testutilsqemuschema.c' ],
+    [ 'qemumonitortestutils.c', ],
     dependencies: [ tests_dep ],
   )

diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index 7f3b2985eb..e0d237bb2d 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -549,32 +549,14 @@ testCompareXMLToArgvValidateSchema(virCommand *cmd,
                                    struct testQemuInfo *info)
 {
     g_auto(GStrv) args = NULL;
-    GHashTable *schema = NULL;
-
-    /* comment out with line comment to enable schema checking for non _CAPS tests
-    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)
+    if (!info->qmpSchema)
         return 0;

     if (virCommandGetArgList(cmd, &args) < 0)
         return -1;

-    if (testCompareXMLToArgvValidateSchemaCommand(args, schema) < 0)
+    if (testCompareXMLToArgvValidateSchemaCommand(args, info->qmpSchema) < 0)
         return -1;

     return 0;
diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c
index a1c55170d9..0d7892353c 100644
--- a/tests/testutilsqemu.c
+++ b/tests/testutilsqemu.c
@@ -2,6 +2,7 @@
 #ifdef WITH_QEMU

 # include "testutilsqemu.h"
+# include "testutilsqemuschema.h"
 # include "testutilshostcpus.h"
 # include "testutils.h"
 # include "viralloc.h"
@@ -910,11 +911,16 @@ testQemuInfoSetArgs(struct testQemuInfo *info,
  * @variant: capabilities variant to fetch caps for
  * @capsLatestFiles: hash table containing latest version of capabilities for the  @arch+ at variant tuple
  * @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+ at version+@variant
  * tuple. The returned pointer is a copy of the cached object and thus can
  * 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 *
 testQemuGetRealCaps(const char *arch,
@@ -922,7 +928,8 @@ testQemuGetRealCaps(const char *arch,
                     const char *variant,
                     GHashTable *capsLatestFiles,
                     GHashTable *capsCache,
-                    char **capsReplies)
+                    GHashTable *schemaCache,
+                    GHashTable **schema)
 {
     g_autofree char *capsfile = NULL;
     bool stripmachinealiases = false;
@@ -960,10 +967,16 @@ testQemuGetRealCaps(const char *arch,
     if (stripmachinealiases)
         virQEMUCapsStripMachineAliases(ret);

-    if (capsReplies) {
-        /* provide path to the replies file for schema testing */
-        capsfile[strlen(capsfile) - 3] = '\0';
-        *capsReplies = g_strdup_printf("%sreplies", capsfile);
+    /* strip 'xml' suffix so that we can format the file to '.replies' */
+    capsfile[strlen(capsfile) - 3] = '\0';
+
+    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;
@@ -1001,7 +1014,8 @@ testQemuInfoInitArgs(struct testQemuInfo *info)
                                              info->args.capsvariant,
                                              info->conf->capslatest,
                                              info->conf->capscache,
-                                             &info->schemafile);
+                                             info->conf->qapiSchemaCache,
+                                             &info->qmpSchema);

         if (!info->qemuCaps)
             return -1;
@@ -1028,7 +1042,6 @@ testQemuInfoClear(struct testQemuInfo *info)
 {
     VIR_FREE(info->infile);
     VIR_FREE(info->outfile);
-    VIR_FREE(info->schemafile);
     VIR_FREE(info->errfile);
     virObjectUnref(info->qemuCaps);
     g_clear_pointer(&info->args.fakeCapsAdd, virBitmapFree);
diff --git a/tests/testutilsqemu.h b/tests/testutilsqemu.h
index d7ee73beed..1e6611daa3 100644
--- a/tests/testutilsqemu.h
+++ b/tests/testutilsqemu.h
@@ -99,7 +99,7 @@ struct testQemuInfo {
     unsigned int flags;
     unsigned int parseFlags;
     virArch arch;
-    char *schemafile;
+    GHashTable *qmpSchema; /* borrowed pointer from the cache */

     struct testQemuArgs args;
     struct testQemuConf *conf;
@@ -158,5 +158,6 @@ testQemuGetRealCaps(const char *arch,
                     const char *variant,
                     GHashTable *capsLatestFiles,
                     GHashTable *capsCache,
-                    char **capsReplies);
+                    GHashTable *schemaCache,
+                    GHashTable **schema);
 #endif
-- 
2.39.2



More information about the libvir-list mailing list