[libvirt] [PATCH] tests: Centralize VIR_TEST_DEBUG lookup, and document it

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Provide a simple interface for other tests to lookup the testDebug variable.
Also remove a redundant error message in interface tests.

If anyone feels inclined to change this env variable to match the existing
LIBVIRT_* format, it should now be easier to do so.

Signed-off-by: Cole Robinson <crobinso@xxxxxxxxxx>
---
 HACKING                      |   10 ++++++++++
 tests/interfacexml2xmltest.c |    2 --
 tests/statstest.c            |    4 ++--
 tests/testutils.c            |   38 ++++++++++++++++++++++++++------------
 tests/testutils.h            |    4 ++--
 tests/testutilsqemu.c        |    2 +-
 6 files changed, 41 insertions(+), 19 deletions(-)

diff --git a/HACKING b/HACKING
index bcff8c6..fba7778 100644
--- a/HACKING
+++ b/HACKING
@@ -37,6 +37,16 @@ and run the tests:
 
 The latter test checks for memory leaks.
 
+If you encounter any failing tests, the VIR_TEST_DEBUG environment variable
+may help:
+
+  VIR_TEST_DEBUG=1 make check    (or)
+  VIR_TEST_DEBUG=2 make check
+
+Also, individual tests can be run from inside the 'tests/' directory, like:
+
+  ./qemuxml2xmltest
+
 (6) Update tests and/or documentation, particularly if you are adding
 a new feature or changing the output of a program.
 
diff --git a/tests/interfacexml2xmltest.c b/tests/interfacexml2xmltest.c
index 5ffebad..ed3093c 100644
--- a/tests/interfacexml2xmltest.c
+++ b/tests/interfacexml2xmltest.c
@@ -43,8 +43,6 @@ static int testCompareXMLToXMLFiles(const char *xml) {
     ret = 0;
 
  fail:
-    if (ret != 0)
-        fprintf(stderr, "expected: -------\n%s", actual);
     free(actual);
     virInterfaceDefFree(dev);
     return ret;
diff --git a/tests/statstest.c b/tests/statstest.c
index 82c0daa..4c2ea7f 100644
--- a/tests/statstest.c
+++ b/tests/statstest.c
@@ -25,7 +25,7 @@ static int testDevice(const char *path, int expect)
     if (actual == expect) {
         return 0;
     } else {
-        if (getenv("DEBUG_TESTS"))
+        if (virtTestGetDebug())
             fprintf(stderr, "Expect %-6d Actual %-6d\n", expect, actual);
         return -1;
     }
@@ -55,7 +55,7 @@ mymain(int argc ATTRIBUTE_UNUSED,
      * register a handler to stop error messages cluttering
      * up display
      */
-    if (!getenv("VIR_TEST_DEBUG"))
+    if (!virtTestGetDebug())
         virSetErrorFunc(NULL, testQuietError);
 
 #define DO_TEST(dev, num)                                              \
diff --git a/tests/testutils.c b/tests/testutils.c
index e6f5e61..9daec6d 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -45,7 +45,7 @@
     ((((int) ((T)->tv_sec - (U)->tv_sec)) * 1000000.0 +	\
       ((int) ((T)->tv_usec - (U)->tv_usec))) / 1000.0)
 
-unsigned int testDebug = 0;
+unsigned int testDebug = -1;
 
 static unsigned int testOOM = 0;
 static unsigned int testCounter = 0;
@@ -255,10 +255,10 @@ int virtTestDifference(FILE *stream,
     const char *actualStart = actual;
     const char *actualEnd = actual + (strlen(actual)-1);
 
-    if (!testDebug)
+    if (!virtTestGetDebug())
         return 0;
 
-    if (testDebug < 2) {
+    if (virtTestGetDebug() < 2) {
         /* Skip to first character where they differ */
         while (*expectStart && *actualStart &&
                *actualStart == *expectStart) {
@@ -322,12 +322,30 @@ virtTestErrorHook(int n, void *data ATTRIBUTE_UNUSED)
 }
 #endif
 
+unsigned int
+virtTestGetDebug() {
+    char *debugStr;
+    unsigned int debug;
+
+    if (testDebug != -1)
+        return testDebug;
+
+    testDebug = 0;
+
+    if ((debugStr = getenv("VIR_TEST_DEBUG")) == NULL)
+        return 0;
+
+    if (virStrToLong_ui(debugStr, NULL, 10, &debug) < 0)
+        return 0;
+
+    testDebug = debug;
+    return testDebug;
+}
 
 int virtTestMain(int argc,
                  char **argv,
                  int (*func)(int, char **))
 {
-    char *debugStr;
     int ret;
 #if TEST_OOM
     int approxAlloc = 0;
@@ -344,10 +362,6 @@ int virtTestMain(int argc,
         virRandomInitialize(time(NULL) ^ getpid()))
         return 1;
 
-    if ((debugStr = getenv("VIR_TEST_DEBUG")) != NULL) {
-        if (virStrToLong_ui(debugStr, NULL, 10, &testDebug) < 0)
-            testDebug = 0;
-    }
 
 #if TEST_OOM
     if ((oomStr = getenv("VIR_TEST_OOM")) != NULL) {
@@ -375,7 +389,7 @@ int virtTestMain(int argc,
         goto cleanup;
 
 #if TEST_OOM_TRACE
-    if (testDebug)
+    if (virtTestGetDebug())
         virAllocTestHook(virtTestErrorHook, NULL);
 #endif
 
@@ -393,7 +407,7 @@ int virtTestMain(int argc,
 
         approxAlloc = virAllocTestCount();
         testCounter++;
-        if (testDebug)
+        if (virtTestGetDebug())
             fprintf(stderr, "%d) OOM...\n", testCounter);
         else
             fprintf(stderr, "%d) OOM of %d allocs ", testCounter, approxAlloc);
@@ -415,7 +429,7 @@ int virtTestMain(int argc,
             if (mp &&
                 (n % mp) != (worker - 1))
                 continue;
-            if (!testDebug) {
+            if (!virtTestGetDebug()) {
                 if (mp)
                     fprintf(stderr, "%d", worker);
                 else
@@ -444,7 +458,7 @@ int virtTestMain(int argc,
             }
         }
 
-        if (testDebug)
+        if (virtTestGetDebug())
             fprintf(stderr, " ... OOM of %d allocs", approxAlloc);
 
         if (ret == EXIT_SUCCESS)
diff --git a/tests/testutils.h b/tests/testutils.h
index f036e0f..aef1179 100644
--- a/tests/testutils.h
+++ b/tests/testutils.h
@@ -34,6 +34,8 @@ int virtTestDifference(FILE *stream,
                        const char *expect,
                        const char *actual);
 
+unsigned int virtTestGetDebug(void);
+
 int virtTestMain(int argc,
                  char **argv,
                  int (*func)(int, char **));
@@ -43,6 +45,4 @@ int virtTestMain(int argc,
         return virtTestMain(argc,argv, func);   \
     }
 
-extern unsigned int testDebug;
-
 #endif /* __VIT_TEST_UTILS_H__ */
diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c
index 9269f5c..eeeb5ec 100644
--- a/tests/testutilsqemu.c
+++ b/tests/testutilsqemu.c
@@ -132,7 +132,7 @@ virCapsPtr testQemuCapsInit(void) {
                                       NULL) == NULL)
         goto cleanup;
 
-    if (testDebug) {
+    if (virtTestGetDebug()) {
         char *caps_str;
 
         caps_str = virCapabilitiesFormatXML(caps);
-- 
1.6.0.6

--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]