[libvirt PATCH 8/9] tests: cputests: introduce and use virTestRunLog

Ján Tomko jtomko at redhat.com
Fri Aug 20 14:48:50 UTC 2021


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 at redhat.com>
---
 tests/cputest.c   | 14 +-------------
 tests/testutils.c | 30 ++++++++++++++++++++++++++++++
 tests/testutils.h |  4 ++++
 3 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/tests/cputest.c b/tests/cputest.c
index c8030be093..3e99b9486b 100644
--- a/tests/cputest.c
+++ b/tests/cputest.c
@@ -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) \
diff --git a/tests/testutils.c b/tests/testutils.c
index 682fa142b5..5e9835ee89 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -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
diff --git a/tests/testutils.h b/tests/testutils.h
index 6848323586..48de864131 100644
--- a/tests/testutils.h
+++ b/tests/testutils.h
@@ -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;
-- 
2.31.1




More information about the libvir-list mailing list