render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
Blob Blame History Raw
From 9b71e87bc50debbc83586d044eecdd0eeb2d66c8 Mon Sep 17 00:00:00 2001
Message-Id: <9b71e87bc50debbc83586d044eecdd0eeb2d66c8@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Wed, 26 Feb 2014 14:55:05 +0100
Subject: [PATCH] domainsnapshotxml2xmltest: Allow for better testing of
 snapshots

https://bugzilla.redhat.com/show_bug.cgi?id=1032370

Until now the test was only testing redefinition of snapshot XMLs stored
in tests/domainsnapshotxml2xmlout. This patch adds new infrastructure to
allow testing of files that may differ and will allow to utilize files
in tests/domainsnapshotxml2xmlin as new tests too.

(cherry picked from commit 11daad9a24077bf233c503c6538b9d55abd37ad7)

Conflicts:
	tests/domainsnapshotxml2xmltest.c - test counter removal not
    backported

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 tests/domainsnapshotxml2xmltest.c | 161 ++++++++++++++++++++++++++++++--------
 1 file changed, 128 insertions(+), 33 deletions(-)

diff --git a/tests/domainsnapshotxml2xmltest.c b/tests/domainsnapshotxml2xmltest.c
index ea33688..3140029 100644
--- a/tests/domainsnapshotxml2xmltest.c
+++ b/tests/domainsnapshotxml2xmltest.c
@@ -8,6 +8,8 @@
 #include <sys/types.h>
 #include <fcntl.h>
 
+#include <regex.h>
+
 #include "testutils.h"
 
 #ifdef WITH_QEMU
@@ -22,21 +24,79 @@
 
 static virQEMUDriver driver;
 
+/* This regex will skip the following XML constructs in test files
+ * that are dynamically generated and thus problematic to test:
+ * <name>1234352345</name> if the snapshot has no name,
+ * <creationTime>23523452345</creationTime>,
+ * <state>nostate</state> as the backend code doesn't fill this
+ */
+static const char *testSnapshotXMLVariableLineRegexStr =
+    "(<(name|creationTime)>[0-9]+</(name|creationTime)>|"
+    "<state>nostate</state>)";
+
+regex_t *testSnapshotXMLVariableLineRegex = NULL;
+
+static char *
+testFilterXML(char *xml)
+{
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    char **xmlLines = NULL;
+    char **xmlLine;
+    char *ret = NULL;
+
+    if (!(xmlLines = virStringSplit(xml, "\n", 0))) {
+        VIR_FREE(xml);
+        goto cleanup;
+    }
+    VIR_FREE(xml);
+
+    for (xmlLine = xmlLines; *xmlLine; xmlLine++) {
+        if (regexec(testSnapshotXMLVariableLineRegex,
+                    *xmlLine, 0, NULL, 0) == 0)
+            continue;
+
+        virBufferStrcat(&buf, *xmlLine, "\n", NULL);
+    }
+
+    if (virBufferError(&buf)) {
+        virReportOOMError();
+        goto cleanup;
+    }
+
+    ret = virBufferContentAndReset(&buf);
+
+cleanup:
+    virBufferFreeAndReset(&buf);
+    virStringFreeList(xmlLines);
+    return ret;
+}
+
 static int
-testCompareXMLToXMLFiles(const char *inxml, const char *uuid, bool internal)
+testCompareXMLToXMLFiles(const char *inxml,
+                         const char *outxml,
+                         const char *uuid,
+                         bool internal,
+                         bool redefine)
 {
     char *inXmlData = NULL;
+    char *outXmlData = NULL;
     char *actual = NULL;
     int ret = -1;
     virDomainSnapshotDefPtr def = NULL;
-    unsigned int flags = (VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE |
-                          VIR_DOMAIN_SNAPSHOT_PARSE_DISKS);
+    unsigned int flags = VIR_DOMAIN_SNAPSHOT_PARSE_DISKS;
+
+    if (internal)
+        flags |= VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL;
+
+    if (redefine)
+        flags |= VIR_DOMAIN_SNAPSHOT_PARSE_REDEFINE;
 
     if (virtTestLoadFile(inxml, &inXmlData) < 0)
         goto cleanup;
 
-    if (internal)
-        flags |= VIR_DOMAIN_SNAPSHOT_PARSE_INTERNAL;
+    if (virtTestLoadFile(outxml, &outXmlData) < 0)
+        goto cleanup;
+
     if (!(def = virDomainSnapshotDefParseString(inXmlData, driver.caps,
                                                 driver.xmlopt,
                                                 QEMU_EXPECTED_VIRT_TYPES,
@@ -48,9 +108,16 @@ testCompareXMLToXMLFiles(const char *inxml, const char *uuid, bool internal)
                                               internal)))
         goto cleanup;
 
+    if (!redefine) {
+        if (!(actual = testFilterXML(actual)))
+            goto cleanup;
+
+        if (!(outXmlData = testFilterXML(outXmlData)))
+            goto cleanup;
+    }
 
-    if (STRNEQ(inXmlData, actual)) {
-        virtTestDifference(stderr, inXmlData, actual);
+    if (STRNEQ(outXmlData, actual)) {
+        virtTestDifference(stderr, outXmlData, actual);
         goto cleanup;
     }
 
@@ -58,15 +125,18 @@ testCompareXMLToXMLFiles(const char *inxml, const char *uuid, bool internal)
 
 cleanup:
     VIR_FREE(inXmlData);
+    VIR_FREE(outXmlData);
     VIR_FREE(actual);
     virDomainSnapshotDefFree(def);
     return ret;
 }
 
 struct testInfo {
-    const char *name;
+    const char *inxml;
+    const char *outxml;
     const char *uuid;
     bool internal;
+    bool redefine;
 };
 
 
@@ -74,18 +144,9 @@ static int
 testCompareXMLToXMLHelper(const void *data)
 {
     const struct testInfo *info = data;
-    char *xml_in = NULL;
-    int ret = -1;
-
-    if (virAsprintf(&xml_in, "%s/domainsnapshotxml2xmlout/%s.xml",
-                    abs_srcdir, info->name) < 0)
-        goto cleanup;
 
-    ret = testCompareXMLToXMLFiles(xml_in, info->uuid, info->internal);
-
-cleanup:
-    VIR_FREE(xml_in);
-    return ret;
+    return testCompareXMLToXMLFiles(info->inxml, info->outxml, info->uuid,
+                                    info->internal, info->redefine);
 }
 
 
@@ -102,28 +163,62 @@ mymain(void)
         return EXIT_FAILURE;
     }
 
-# define DO_TEST(name, uuid, internal)                                  \
-    do {                                                                \
-        const struct testInfo info = {name, uuid, internal};            \
-        if (virtTestRun("SNAPSHOT XML-2-XML " name,                     \
-                        1, testCompareXMLToXMLHelper, &info) < 0)       \
-            ret = -1;                                                   \
+    if (VIR_ALLOC(testSnapshotXMLVariableLineRegex) < 0)
+        goto cleanup;
+
+    if (regcomp(testSnapshotXMLVariableLineRegex,
+                testSnapshotXMLVariableLineRegexStr,
+                REG_EXTENDED | REG_NOSUB) != 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       "failed to compile test regex");
+        goto cleanup;
+    }
+
+
+# define DO_TEST(prefix, name, inpath, outpath, uuid, internal, redefine)     \
+    do {                                                                      \
+        const struct testInfo info = {abs_srcdir "/" inpath "/" name ".xml",  \
+                                      abs_srcdir "/" outpath "/" name ".xml", \
+                                      uuid, internal, redefine};              \
+        if (virtTestRun("SNAPSHOT XML-2-XML " prefix " " name, 1,             \
+                        testCompareXMLToXMLHelper, &info) < 0)                \
+            ret = -1;                                                         \
     } while (0)
 
+# define DO_TEST_IN(name, uuid) DO_TEST("in->in", name,\
+                                        "domainsnapshotxml2xmlin",\
+                                        "domainsnapshotxml2xmlin",\
+                                        uuid, false, false)
+
+# define DO_TEST_OUT(name, uuid, internal) DO_TEST("out->out", name,\
+                                                   "domainsnapshotxml2xmlout",\
+                                                   "domainsnapshotxml2xmlout",\
+                                                   uuid, internal, true)
+
+# define DO_TEST_INOUT(name, uuid, internal, redefine) \
+    DO_TEST("in->out", name,\
+            "domainsnapshotxml2xmlin",\
+            "domainsnapshotxml2xmlout",\
+            uuid, internal, redefine)
+
     /* Unset or set all envvars here that are copied in qemudBuildCommandLine
      * using ADD_ENV_COPY, otherwise these tests may fail due to unexpected
      * values for these envvars */
     setenv("PATH", "/bin", 1);
 
-    DO_TEST("all_parameters", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", true);
-    DO_TEST("disk_snapshot", "c7a5fdbd-edaf-9455-926a-d65c16db1809", true);
-    DO_TEST("full_domain", "c7a5fdbd-edaf-9455-926a-d65c16db1809", true);
-    DO_TEST("noparent_nodescription_noactive", NULL, false);
-    DO_TEST("noparent_nodescription", NULL, true);
-    DO_TEST("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false);
-    DO_TEST("metadata", "c7a5fdbd-edaf-9455-926a-d65c16db1809", false);
-    DO_TEST("external_vm", "c7a5fdbd-edaf-9455-926a-d65c16db1809", false);
+    DO_TEST_OUT("all_parameters", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", true);
+    DO_TEST_OUT("disk_snapshot", "c7a5fdbd-edaf-9455-926a-d65c16db1809", true);
+    DO_TEST_OUT("full_domain", "c7a5fdbd-edaf-9455-926a-d65c16db1809", true);
+    DO_TEST_OUT("noparent_nodescription_noactive", NULL, false);
+    DO_TEST_OUT("noparent_nodescription", NULL, true);
+    DO_TEST_OUT("noparent", "9d37b878-a7cc-9f9a-b78f-49b3abad25a8", false);
+    DO_TEST_OUT("metadata", "c7a5fdbd-edaf-9455-926a-d65c16db1809", false);
+    DO_TEST_OUT("external_vm", "c7a5fdbd-edaf-9455-926a-d65c16db1809", false);
 
+cleanup:
+    if (testSnapshotXMLVariableLineRegex)
+        regfree(testSnapshotXMLVariableLineRegex);
+    VIR_FREE(testSnapshotXMLVariableLineRegex);
     virObjectUnref(driver.caps);
     virObjectUnref(driver.xmlopt);
 
-- 
1.9.0