]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
tests: cputests: introduce and use virTestRunLog
authorJán Tomko <jtomko@redhat.com>
Fri, 20 Aug 2021 14:29:45 +0000 (16:29 +0200)
committerJán Tomko <jtomko@redhat.com>
Mon, 23 Aug 2021 12:43:57 +0000 (14:43 +0200)
A helper that resets the log before each test and prints
it on failure.

It also takes the return variable as an argument,
so it can be used to eliminate number of branches
the compiler has to consider in the main function.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
tests/cputest.c
tests/testutils.c
tests/testutils.h

index c8030be093cf683986fb7e1991f7e7e0617fef2e..3e99b9486bd0e8506428636cd26bfa8618957add 100644 (file)
@@ -912,23 +912,11 @@ mymain(void)
             flags, result \
         }; \
         g_autofree char *testLabel = NULL; \
- \
-        g_free(virTestLogContentAndReset());\
  \
         testLabel = g_strdup_printf("%s(%s): %s", #api, \
                                     virArchToString(arch), name); \
  \
-        if (virTestRun(testLabel, api, &data) < 0) { \
-            if (virTestGetDebug()) { \
-                char *log; \
-                if ((log = virTestLogContentAndReset()) && \
-                     strlen(log) > 0) \
-                    VIR_TEST_DEBUG("\n%s", log); \
-                VIR_FREE(log); \
-            } \
-            ret = -1; \
-        } \
- \
+        virTestRunLog(&ret, testLabel, api, &data); \
     } while (0)
 
 #define DO_TEST_COMPARE(arch, host, cpu, result) \
index 682fa142b5ae67002e903187fb760c1bce527643..5e9835ee89d0e2b63e7e28a1a9aa46c6a08dcd8c 100644 (file)
@@ -180,6 +180,36 @@ virTestRun(const char *title,
 }
 
 
+/*
+ * A wrapper for virTestRun that resets the log content before each run
+ * and sets ret to -1 on failure. On success, ret is untouched.
+ */
+void
+virTestRunLog(int *ret,
+              const char *title,
+              int (*body)(const void *data),
+              const void *data)
+{
+    int rc;
+
+    g_free(virTestLogContentAndReset());
+
+    rc = virTestRun(title, body, data);
+
+    if (rc >= 0)
+        return;
+
+    *ret = -1;
+
+    if (virTestGetDebug()) {
+        g_autofree char *log = virTestLogContentAndReset();
+
+        if (strlen(log) > 0)
+            VIR_TEST_DEBUG("\n%s", log);
+    }
+}
+
+
 /**
  * virTestLoadFile:
  * @file: name of the file to load
index 6848323586b1b9898c601d3c30e47ccedd92261f..48de864131698e8ede1713512664826ea80e1d81 100644 (file)
@@ -40,6 +40,10 @@ extern virArch virTestHostArch;
 int virTestRun(const char *title,
                int (*body)(const void *data),
                const void *data);
+void virTestRunLog(int *ret,
+                   const char *title,
+                   int (*body)(const void *data),
+                   const void *data);
 int virTestLoadFile(const char *file, char **buf);
 char *virTestLoadFilePath(const char *p, ...)
     G_GNUC_NULL_TERMINATED;