]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
testutilsqemu: Extract fetching of real virQEMUCaps into testQemuGetRealCaps
authorPeter Krempa <pkrempa@redhat.com>
Thu, 9 Mar 2023 13:24:45 +0000 (14:24 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 3 Apr 2023 07:19:07 +0000 (09:19 +0200)
'testQemuInfoInitArgs' contains the logic to fetch and use the
capabilities for tests using 'struct testQemuInfo'.

As in certain cases use of 'struct testQemuInfo' is an overkill extract
the code to fetch the capabilities into a standalone helper.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
tests/testutilsqemu.c
tests/testutilsqemu.h

index f6d49bc193d861d6b501d985760625a9daeb634e..a1c55170d9ccbd3364f14c3a9d781298f1ac031c 100644 (file)
@@ -902,10 +902,77 @@ testQemuInfoSetArgs(struct testQemuInfo *info,
 }
 
 
+/**
+ * testQemuGetRealCaps:
+ *
+ * @arch: architecture to fetch caps for
+ * @version: qemu version to fetch caps for ("latest" for fetching the latest version from @capsLatestFiles)
+ * @variant: capabilities variant to fetch caps for
+ * @capsLatestFiles: hash table containing latest version of capabilities for the  @arch+@variant tuple
+ * @capsCache: hash table filled with the cache of capabilities
+ * @capsReplies: Filled with path to the corresponding '.replies' file
+ *
+ * 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
+ * be freely modified. Caller is responsible for freeing it.
+ */
+virQEMUCaps *
+testQemuGetRealCaps(const char *arch,
+                    const char *version,
+                    const char *variant,
+                    GHashTable *capsLatestFiles,
+                    GHashTable *capsCache,
+                    char **capsReplies)
+{
+    g_autofree char *capsfile = NULL;
+    bool stripmachinealiases = false;
+    virQEMUCaps *cachedcaps = NULL;
+    virQEMUCaps *ret = NULL;
+
+    if (STREQ(version, "latest")) {
+        g_autofree char *archvariant = g_strdup_printf("%s%s", arch, variant);
+        struct testQemuCapsFile *f = g_hash_table_lookup(capsLatestFiles, archvariant);
+
+        if (!f) {
+            VIR_TEST_VERBOSE("'latest' caps for '%s' were not found\n", arch);
+            return NULL;
+        }
+
+        capsfile = g_strdup(f->path);
+        stripmachinealiases = true;
+    } else {
+        capsfile = g_strdup_printf("%s/caps_%s_%s%s.xml",
+                                   TEST_QEMU_CAPS_PATH,
+                                   version, arch, variant);
+    }
+
+    if (!g_hash_table_lookup_extended(capsCache, capsfile, NULL, (void **) &cachedcaps)) {
+        if (!(cachedcaps = qemuTestParseCapabilitiesArch(virArchFromString(arch), capsfile))) {
+            VIR_TEST_VERBOSE("Failed to parse qemu capabilities file '%s'", capsfile);
+            return NULL;
+        }
+
+        g_hash_table_insert(capsCache, g_strdup(capsfile), cachedcaps);
+    }
+
+    ret = virQEMUCapsNewCopy(cachedcaps);
+
+    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);
+    }
+
+    return ret;
+}
+
+
 int
 testQemuInfoInitArgs(struct testQemuInfo *info)
 {
-    g_autofree char *capsfile = NULL;
     ssize_t cap;
 
     if (!info->args.newargs)
@@ -927,47 +994,17 @@ testQemuInfoInitArgs(struct testQemuInfo *info)
     }
 
     if (info->args.capsarch && info->args.capsver) {
-        bool stripmachinealiases = false;
-        virQEMUCaps *cachedcaps = NULL;
-
         info->arch = virArchFromString(info->args.capsarch);
-
-        if (STREQ(info->args.capsver, "latest")) {
-            g_autofree char *archvariant = g_strdup_printf("%s%s", info->args.capsarch, info->args.capsvariant);
-            struct testQemuCapsFile *f = g_hash_table_lookup(info->conf->capslatest, archvariant);
-
-            if (!f) {
-                fprintf(stderr, "'latest' caps for '%s' were not found\n", info->args.capsarch);
-                return -1;
-            }
-
-            capsfile = g_strdup(f->path);
-            stripmachinealiases = true;
-        } else {
-            capsfile = g_strdup_printf("%s/caps_%s_%s%s.xml",
-                                       TEST_QEMU_CAPS_PATH,
-                                       info->args.capsver,
-                                       info->args.capsarch,
-                                       info->args.capsvariant);
-        }
-
-        if (!g_hash_table_lookup_extended(info->conf->capscache, capsfile, NULL, (void **) &cachedcaps)) {
-            if (!(cachedcaps = qemuTestParseCapabilitiesArch(info->arch, capsfile)))
-                return -1;
-
-            g_hash_table_insert(info->conf->capscache, g_strdup(capsfile), cachedcaps);
-        }
-
-        info->qemuCaps = virQEMUCapsNewCopy(cachedcaps);
-
-        if (stripmachinealiases)
-            virQEMUCapsStripMachineAliases(info->qemuCaps);
-
         info->flags |= FLAG_REAL_CAPS;
-
-        /* provide path to the replies file for schema testing */
-        capsfile[strlen(capsfile) - 3] = '\0';
-        info->schemafile = g_strdup_printf("%sreplies", capsfile);
+        info->qemuCaps = testQemuGetRealCaps(info->args.capsarch,
+                                             info->args.capsver,
+                                             info->args.capsvariant,
+                                             info->conf->capslatest,
+                                             info->conf->capscache,
+                                             &info->schemafile);
+
+        if (!info->qemuCaps)
+            return -1;
     } else {
         info->qemuCaps = virQEMUCapsNew();
     }
index 69cad61f5a0dabd56ed30e7a3eeb9e0d1ca6b9c2..d7ee73beedbec7d8c5c4d744c9d692106f234d60 100644 (file)
@@ -151,4 +151,12 @@ void testQemuInfoClear(struct testQemuInfo *info);
 int testQemuPrepareHostBackendChardevOne(virDomainDeviceDef *dev,
                                          virDomainChrSourceDef *chardev,
                                          void *opaque);
+
+virQEMUCaps *
+testQemuGetRealCaps(const char *arch,
+                    const char *version,
+                    const char *variant,
+                    GHashTable *capsLatestFiles,
+                    GHashTable *capsCache,
+                    char **capsReplies);
 #endif