]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu*xml2*test: Invoke tests from a function
authorPeter Krempa <pkrempa@redhat.com>
Mon, 4 Dec 2023 16:00:54 +0000 (17:00 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 17 Jan 2024 16:30:30 +0000 (17:30 +0100)
Refactor the code so that the test macros invoke a helper function with
no additional steps. This change prevents regressions in compilation
time when adding extra steps for the tests, which happen when the test
macro gets too complicated.

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

index 3e6c59cd91335012f9c83d6fa9e64bd42bf31b45..ae61d0fd2b7a2d01db26af7fa343d340d81f5538 100644 (file)
@@ -811,18 +811,6 @@ testCompareXMLToArgv(const void *data)
     return ret;
 }
 
-static void
-testInfoSetPaths(testQemuInfo *info,
-                 const char *suffix)
-{
-    info->infile = g_strdup_printf("%s/qemuxml2argvdata/%s.xml",
-                                   abs_srcdir, info->name);
-    info->outfile = g_strdup_printf("%s/qemuxml2argvdata/%s%s.args",
-                                    abs_srcdir, info->name, suffix ? suffix : "");
-    info->errfile = g_strdup_printf("%s/qemuxml2argvdata/%s%s.err",
-                                      abs_srcdir, info->name, suffix ? suffix : "");
-}
-
 
 static int
 testConfXMLCheck(GHashTable *existingTestCases)
@@ -870,6 +858,32 @@ testConfXMLEnumerate(GHashTable *existingTestCases)
 }
 
 
+static void
+testRun(const char *name,
+        const char *suffix,
+        int *ret,
+        struct testQemuConf *testConf,
+        ...)
+{
+    g_autofree char *testname = g_strdup_printf("QEMU XML-2-ARGV %s%s", name, suffix);
+    g_autoptr(testQemuInfo) info = g_new0(testQemuInfo, 1);
+    va_list ap;
+
+    info->name = name;
+    info->conf = testConf;
+
+    va_start(ap, testConf);
+    testQemuInfoSetArgs(info, ap);
+    va_end(ap);
+
+    info->infile = g_strdup_printf("%s/qemuxml2argvdata/%s.xml", abs_srcdir, info->name);
+    info->outfile = g_strdup_printf("%s/qemuxml2argvdata/%s%s.args", abs_srcdir, info->name, suffix);
+    info->errfile = g_strdup_printf("%s/qemuxml2argvdata/%s%s.err", abs_srcdir, info->name, suffix);
+
+    virTestRunLog(ret, testname, testCompareXMLToArgv, info);
+}
+
+
 static int
 mymain(void)
 {
@@ -940,15 +954,7 @@ mymain(void)
  * version.
  */
 # define DO_TEST_FULL(_name, _suffix, ...) \
-    do { \
-        static testQemuInfo info = { \
-            .name = _name, \
-        }; \
-        testQemuInfoSetArgs(&info, &testConf, __VA_ARGS__); \
-        testInfoSetPaths(&info, _suffix); \
-        virTestRunLog(&ret, "QEMU XML-2-ARGV " _name _suffix, testCompareXMLToArgv, &info); \
-        testQemuInfoClear(&info); \
-    } while (0)
+    testRun(_name, _suffix, &ret, &testConf, __VA_ARGS__);
 
 # define DO_TEST_CAPS_INTERNAL(name, arch, ver, ...) \
     DO_TEST_FULL(name, "." arch "-" ver, \
index 4376a975199f517e7c2e41ca9c296fa43e50dac3..20bcac0476f08b3e85d28c88e1c26e248e23d04e 100644 (file)
@@ -107,6 +107,34 @@ testInfoSetPaths(testQemuInfo *info,
 }
 
 
+static void
+testRun(const char *name,
+        const char *suffix,
+        struct testQemuConf *testConf,
+        int *ret,
+        ...)
+{
+    g_autofree char *name_active = g_strdup_printf("QEMU XML-2-XML-active %s", name);
+    g_autofree char *name_inactive = g_strdup_printf("QEMU XML-2-XML-inactive %s", name);
+    g_autoptr(testQemuInfo) info = g_new0(testQemuInfo, 1);
+    va_list ap;
+
+    info->name = name;
+    info->conf = testConf;
+
+    va_start(ap, ret);
+    testQemuInfoSetArgs(info, ap);
+    va_end(ap);
+
+
+    testInfoSetPaths(info, suffix, "inactive");
+    virTestRunLog(ret, name_inactive, testXML2XMLInactive, info);
+
+    testInfoSetPaths(info, suffix, "active");
+    virTestRunLog(ret, name_active, testXML2XMLActive, info);
+}
+
+
 static int
 mymain(void)
 {
@@ -147,22 +175,9 @@ mymain(void)
     virSetConnectStorage(conn);
 
 #define DO_TEST_CAPS_INTERNAL(_name, arch, ver, ...) \
-    do { \
-        static testQemuInfo info = { \
-            .name = _name, \
-        }; \
-        testQemuInfoSetArgs(&info, &testConf, \
-                            ARG_CAPS_ARCH, arch, \
-                            ARG_CAPS_VER, ver, \
-                            __VA_ARGS__, ARG_END); \
- \
-        testInfoSetPaths(&info, "." arch "-" ver, "inactive"); \
-        virTestRunLog(&ret, "QEMU XML-2-XML-inactive " _name, testXML2XMLInactive, &info); \
- \
-        testInfoSetPaths(&info, "." arch "-" ver, "active"); \
-        virTestRunLog(&ret, "QEMU XML-2-XML-active " _name, testXML2XMLActive, &info); \
-        testQemuInfoClear(&info); \
-    } while (0)
+    testRun(_name, "." arch "-" ver, &testConf, &ret, \
+            ARG_CAPS_ARCH, arch, ARG_CAPS_VER, ver, \
+            __VA_ARGS__, ARG_END);
 
 #define DO_TEST_CAPS_ARCH_LATEST_FULL(name, arch, ...) \
     DO_TEST_CAPS_INTERNAL(name, arch, "latest", __VA_ARGS__)
index 9224bd5d05d5b108e806575dc4fb39111f03525f..c86cda09fd2704dbe1a0a5e57360790690140b5a 100644 (file)
@@ -60,11 +60,30 @@ testCompareStatusXMLToXMLFiles(const void *opaque)
 
 static const char *statusPath = abs_srcdir "/qemustatusxml2xmldata/";
 
-static void
-testInfoSetStatusPaths(testQemuInfo *info)
+
+static int
+testRunStatus(const char *name,
+              struct testQemuConf *testConf,
+              ...)
 {
+    g_autofree char *testname = g_strdup_printf("QEMU status XML-2-XML %s", name);
+    g_autoptr(testQemuInfo) info = g_new0(testQemuInfo, 1);
+    va_list ap;
+
+    info->name = name;
+    info->conf = testConf;
+
+    va_start(ap, testConf);
+    testQemuInfoSetArgs(info, ap);
+    va_end(ap);
+
     info->infile = g_strdup_printf("%s%s-in.xml", statusPath, info->name);
     info->outfile = g_strdup_printf("%s%s-out.xml", statusPath, info->name);
+
+    if (virTestRun(testname, testCompareStatusXMLToXMLFiles, info) < 0)
+        return -1;
+
+    return 0;
 }
 
 
@@ -90,20 +109,10 @@ mymain(void)
 
 #define DO_TEST_STATUS(_name) \
     do { \
-        static testQemuInfo info = { \
-            .name = _name, \
-        }; \
-        testQemuInfoSetArgs(&info, &testConf, ARG_END); \
-        testInfoSetStatusPaths(&info); \
-\
-        if (virTestRun("QEMU status XML-2-XML " _name, \
-                       testCompareStatusXMLToXMLFiles, &info) < 0) \
+        if (testRunStatus(_name, &testConf, ARG_END) < 0) \
             ret = -1; \
-\
-        testQemuInfoClear(&info); \
     } while (0)
 
-
     DO_TEST_STATUS("blockjob-mirror");
     DO_TEST_STATUS("vcpus-multi");
     DO_TEST_STATUS("modern");
index cf4ef3a3178b55cef708e0097e2b7ae295d5caec..3799c2eb51954f8c136194c3e080b9796dd359c6 100644 (file)
@@ -624,16 +624,13 @@ testQemuCapsIterate(const char *suffix,
 
 void
 testQemuInfoSetArgs(testQemuInfo *info,
-                    struct testQemuConf *conf, ...)
+                    va_list argptr)
 {
-    va_list argptr;
     testQemuInfoArgName argname;
     int flag;
 
-    info->conf = conf;
     info->args.newargs = true;
 
-    va_start(argptr, conf);
     while ((argname = va_arg(argptr, testQemuInfoArgName)) != ARG_END) {
         switch (argname) {
         case ARG_QEMU_CAPS:
@@ -748,8 +745,6 @@ testQemuInfoSetArgs(testQemuInfo *info,
         if (info->args.invalidarg)
             break;
     }
-
-    va_end(argptr);
 }
 
 
@@ -959,7 +954,7 @@ testQemuInfoInitArgs(testQemuInfo *info)
 
 
 void
-testQemuInfoClear(testQemuInfo *info)
+testQemuInfoFree(testQemuInfo *info)
 {
     VIR_FREE(info->infile);
     VIR_FREE(info->outfile);
@@ -970,6 +965,7 @@ testQemuInfoClear(testQemuInfo *info)
     g_clear_pointer(&info->args.fds, g_hash_table_unref);
     g_clear_object(&info->nbdkitCaps);
     g_clear_pointer(&info->args.fakeNbdkitCaps, virBitmapFree);
+    g_free(info);
 }
 
 
index c9c36d24c60147bd008a979b06fe7304c05b78ce..9343457f596f439c40109a2816b457b13269ceaa 100644 (file)
@@ -115,6 +115,8 @@ struct _testQemuInfo {
 };
 
 typedef struct _testQemuInfo testQemuInfo;
+void testQemuInfoFree(testQemuInfo *info);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(testQemuInfo, testQemuInfoFree);
 
 virDomainXMLOption *testQemuXMLConfInit(void);
 
@@ -153,10 +155,8 @@ int testQemuCapsIterate(const char *suffix,
                         void *opaque);
 
 void testQemuInfoSetArgs(testQemuInfo *info,
-                         struct testQemuConf *conf,
-                         ...);
+                         va_list argptr);
 int testQemuInfoInitArgs(testQemuInfo *info);
-void testQemuInfoClear(testQemuInfo *info);
 
 int testQemuPrepareHostBackendChardevOne(virDomainDeviceDef *dev,
                                          virDomainChrSourceDef *chardev,