6ae9ed
From e8f0d99c49eb518883235661fc3d9e14f65fc6fe Mon Sep 17 00:00:00 2001
6ae9ed
Message-Id: <e8f0d99c49eb518883235661fc3d9e14f65fc6fe@dist-git>
6ae9ed
From: Peter Krempa <pkrempa@redhat.com>
6ae9ed
Date: Tue, 2 Aug 2016 13:41:40 +0200
6ae9ed
Subject: [PATCH] tests: Add testing of backing store string parser
6ae9ed
6ae9ed
As we already test that the extraction of the backing store string works
6ae9ed
well additional tests for the backing store string parser can be made
6ae9ed
simpler.
6ae9ed
6ae9ed
Export virStorageSourceNewFromBackingAbsolute and use it to parse the
6ae9ed
backing store strings, format them using virDomainDiskSourceFormat and
6ae9ed
match them against expected XMLs.
6ae9ed
6ae9ed
(cherry picked from commit 4e3dbfa2a5f049987d74bdf35f86226f739fcda6)
6ae9ed
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1134878 [JSON backing]
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1247521 [gluster multi-host]
6ae9ed
---
6ae9ed
 src/libvirt_private.syms  |  1 +
6ae9ed
 src/util/virstoragefile.c |  2 +-
6ae9ed
 src/util/virstoragefile.h |  3 ++
6ae9ed
 tests/virstoragetest.c    | 80 +++++++++++++++++++++++++++++++++++++++++++++++
6ae9ed
 4 files changed, 85 insertions(+), 1 deletion(-)
6ae9ed
6ae9ed
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
6ae9ed
index f73cfa2..3e4b2e7 100644
6ae9ed
--- a/src/libvirt_private.syms
6ae9ed
+++ b/src/libvirt_private.syms
6ae9ed
@@ -2353,6 +2353,7 @@ virStorageSourceIsBlockLocal;
6ae9ed
 virStorageSourceIsEmpty;
6ae9ed
 virStorageSourceIsLocalStorage;
6ae9ed
 virStorageSourceNewFromBacking;
6ae9ed
+virStorageSourceNewFromBackingAbsolute;
6ae9ed
 virStorageSourceParseRBDColonString;
6ae9ed
 virStorageSourcePoolDefFree;
6ae9ed
 virStorageSourcePoolModeTypeFromString;
6ae9ed
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
6ae9ed
index 7dace8a..aa5f302 100644
6ae9ed
--- a/src/util/virstoragefile.c
6ae9ed
+++ b/src/util/virstoragefile.c
6ae9ed
@@ -2523,7 +2523,7 @@ virStorageSourceParseBackingColon(virStorageSourcePtr src,
6ae9ed
 }
6ae9ed
 
6ae9ed
 
6ae9ed
-static virStorageSourcePtr
6ae9ed
+virStorageSourcePtr
6ae9ed
 virStorageSourceNewFromBackingAbsolute(const char *path)
6ae9ed
 {
6ae9ed
     virStorageSourcePtr ret;
6ae9ed
diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
6ae9ed
index 78beaf4..1a76fad 100644
6ae9ed
--- a/src/util/virstoragefile.h
6ae9ed
+++ b/src/util/virstoragefile.h
6ae9ed
@@ -380,4 +380,7 @@ int virStorageFileGetRelativeBackingPath(virStorageSourcePtr from,
6ae9ed
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
6ae9ed
 
6ae9ed
 int virStorageFileCheckCompat(const char *compat);
6ae9ed
+
6ae9ed
+virStorageSourcePtr virStorageSourceNewFromBackingAbsolute(const char *path);
6ae9ed
+
6ae9ed
 #endif /* __VIR_STORAGE_FILE_H__ */
6ae9ed
diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c
6ae9ed
index 580065e..6016a3b 100644
6ae9ed
--- a/tests/virstoragetest.c
6ae9ed
+++ b/tests/virstoragetest.c
6ae9ed
@@ -665,6 +665,58 @@ testPathRelative(const void *args)
6ae9ed
 }
6ae9ed
 
6ae9ed
 
6ae9ed
+struct testBackingParseData {
6ae9ed
+    const char *backing;
6ae9ed
+    const char *expect;
6ae9ed
+};
6ae9ed
+
6ae9ed
+static int
6ae9ed
+testBackingParse(const void *args)
6ae9ed
+{
6ae9ed
+    const struct testBackingParseData *data = args;
6ae9ed
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
6ae9ed
+    virStorageSourcePtr src = NULL;
6ae9ed
+    char *xml = NULL;
6ae9ed
+    int ret = -1;
6ae9ed
+
6ae9ed
+    if (!(src = virStorageSourceNewFromBackingAbsolute(data->backing))) {
6ae9ed
+        if (!data->expect)
6ae9ed
+            ret = 0;
6ae9ed
+
6ae9ed
+        goto cleanup;
6ae9ed
+    }
6ae9ed
+
6ae9ed
+    if (src && !data->expect) {
6ae9ed
+        fprintf(stderr, "parsing of backing store string '%s' should "
6ae9ed
+                        "have failed\n", data->backing);
6ae9ed
+        goto cleanup;
6ae9ed
+    }
6ae9ed
+
6ae9ed
+    if (virDomainDiskSourceFormat(&buf, src, 0, 0) < 0 ||
6ae9ed
+        !(xml = virBufferContentAndReset(&buf))) {
6ae9ed
+        fprintf(stderr, "failed to format disk source xml\n");
6ae9ed
+        goto cleanup;
6ae9ed
+    }
6ae9ed
+
6ae9ed
+    if (!STREQ(xml, data->expect)) {
6ae9ed
+        fprintf(stderr, "\n backing store string '%s'\n"
6ae9ed
+                        "expected storage source xml:\n%s\n"
6ae9ed
+                        "actual storage source xml:\n%s\n",
6ae9ed
+                        data->backing, data->expect, xml);
6ae9ed
+        goto cleanup;
6ae9ed
+    }
6ae9ed
+
6ae9ed
+    ret = 0;
6ae9ed
+
6ae9ed
+ cleanup:
6ae9ed
+    virStorageSourceFree(src);
6ae9ed
+    virBufferFreeAndReset(&buf;;
6ae9ed
+    VIR_FREE(xml);
6ae9ed
+
6ae9ed
+    return ret;
6ae9ed
+}
6ae9ed
+
6ae9ed
+
6ae9ed
 static int
6ae9ed
 mymain(void)
6ae9ed
 {
6ae9ed
@@ -674,6 +726,7 @@ mymain(void)
6ae9ed
     struct testLookupData data2;
6ae9ed
     struct testPathCanonicalizeData data3;
6ae9ed
     struct testPathRelativeBacking data4;
6ae9ed
+    struct testBackingParseData data5;
6ae9ed
     virStorageSourcePtr chain = NULL;
6ae9ed
     virStorageSourcePtr chain2; /* short for chain->backingStore */
6ae9ed
     virStorageSourcePtr chain3; /* short for chain2->backingStore */
6ae9ed
@@ -1276,6 +1329,33 @@ mymain(void)
6ae9ed
     TEST_RELATIVE_BACKING(21, backingchain[10], backingchain[11], "../../../../blah/image4");
6ae9ed
     TEST_RELATIVE_BACKING(22, backingchain[11], backingchain[11], "../blah/image4");
6ae9ed
 
6ae9ed
+
6ae9ed
+    virTestCounterReset("Backing store parse ");
6ae9ed
+
6ae9ed
+#define TEST_BACKING_PARSE(bck, xml)                                           \
6ae9ed
+    do {                                                                       \
6ae9ed
+        data5.backing = bck;                                                   \
6ae9ed
+        data5.expect = xml;                                                    \
6ae9ed
+        if (virTestRun(virTestCounterNext(),                                   \
6ae9ed
+                       testBackingParse, &data5) < 0)                          \
6ae9ed
+            ret = -1;                                                          \
6ae9ed
+    } while (0)
6ae9ed
+
6ae9ed
+    TEST_BACKING_PARSE("path", "<source file='path'/>\n");
6ae9ed
+    TEST_BACKING_PARSE("://", NULL);
6ae9ed
+    TEST_BACKING_PARSE("http://example.com/file",
6ae9ed
+                       "<source protocol='http' name='file'>\n"
6ae9ed
+                       "  <host name='example.com'/>\n"
6ae9ed
+                       "</source>\n");
6ae9ed
+    TEST_BACKING_PARSE("rbd:testshare:id=asdf:mon_host=example.com",
6ae9ed
+                       "<source protocol='rbd' name='testshare'>\n"
6ae9ed
+                       "  <host name='example.com'/>\n"
6ae9ed
+                       "</source>\n");
6ae9ed
+    TEST_BACKING_PARSE("nbd:example.org:6000:exportname=blah",
6ae9ed
+                       "<source protocol='nbd' name='blah'>\n"
6ae9ed
+                       "  <host name='example.org' port='6000'/>\n"
6ae9ed
+                       "</source>\n");
6ae9ed
+
6ae9ed
  cleanup:
6ae9ed
     /* Final cleanup */
6ae9ed
     virStorageSourceFree(chain);
6ae9ed
-- 
6ae9ed
2.9.2
6ae9ed