[libvirt] [PATCH 13/15] tests: Use VIR_AUTOFREE for various storage tests

John Ferlan jferlan at redhat.com
Wed Feb 6 13:41:45 UTC 2019


Let's make use of the auto __cleanup capabilities cleaning up any
now unnecessary goto paths.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 tests/storagebackendsheepdogtest.c | 50 ++++++++++++------------------
 tests/storagepoolxml2argvtest.c    | 18 +++--------
 tests/storagepoolxml2xmltest.c     | 33 ++++++--------------
 tests/storagevolxml2argvtest.c     | 42 +++++++++----------------
 tests/storagevolxml2xmltest.c      | 38 +++++++----------------
 tests/virstoragetest.c             | 45 +++++++--------------------
 tests/virstorageutiltest.c         |  7 ++---
 7 files changed, 75 insertions(+), 158 deletions(-)

diff --git a/tests/storagebackendsheepdogtest.c b/tests/storagebackendsheepdogtest.c
index 03ddf76d65..1806f9725f 100644
--- a/tests/storagebackendsheepdogtest.c
+++ b/tests/storagebackendsheepdogtest.c
@@ -57,32 +57,27 @@ test_node_info_parser(const void *opaque)
 {
     const struct testNodeInfoParserData *data = opaque;
     collie_test test = data->data;
-    int ret = -1;
-    char *output = NULL;
+    VIR_AUTOFREE(char *) output = NULL;
     VIR_AUTOPTR(virStoragePoolDef) pool = NULL;
 
     if (!(pool = virStoragePoolDefParseFile(data->poolxml)))
-        goto cleanup;
+        return -1;
 
     if (VIR_STRDUP(output, test.output) < 0)
-        goto cleanup;
+        return -1;
 
     if (virStorageBackendSheepdogParseNodeInfo(pool, output) !=
         test.expected_return)
-        goto cleanup;
+        return -1;
 
-    if (test.expected_return) {
-        ret = 0;
-        goto cleanup;
-    }
+    if (test.expected_return)
+        return 0;
 
     if (pool->capacity == test.expected_capacity &&
         pool->allocation == test.expected_allocation)
-        ret = 0;
+        return 0;
 
- cleanup:
-    VIR_FREE(output);
-    return ret;
+    return -1;
 }
 
 static int
@@ -90,36 +85,31 @@ test_vdi_list_parser(const void *opaque)
 {
     const struct testVDIListParserData *data = opaque;
     collie_test test = data->data;
-    int ret = -1;
-    char *output = NULL;
+    VIR_AUTOFREE(char *) output = NULL;
     VIR_AUTOPTR(virStoragePoolDef) pool = NULL;
     VIR_AUTOPTR(virStorageVolDef) vol = NULL;
 
     if (!(pool = virStoragePoolDefParseFile(data->poolxml)))
-        goto cleanup;
+        return -1;
 
     if (!(vol = virStorageVolDefParseFile(pool, data->volxml, 0)))
-        goto cleanup;
+        return -1;
 
     if (VIR_STRDUP(output, test.output) < 0)
-        goto cleanup;
+        return -1;
 
     if (virStorageBackendSheepdogParseVdiList(vol, output) !=
         test.expected_return)
-        goto cleanup;
+        return -1;
 
-    if (test.expected_return) {
-        ret = 0;
-        goto cleanup;
-    }
+    if (test.expected_return)
+        return 0;
 
     if (vol->target.capacity == test.expected_capacity &&
         vol->target.allocation == test.expected_allocation)
-        ret = 0;
+        return 0;
 
- cleanup:
-    VIR_FREE(output);
-    return ret;
+    return -1;
 }
 
 
@@ -127,8 +117,8 @@ static int
 mymain(void)
 {
     int ret = 0;
-    char *poolxml = NULL;
-    char *volxml = NULL;
+    VIR_AUTOFREE(char *) poolxml = NULL;
+    VIR_AUTOFREE(char *) volxml = NULL;
 
     collie_test node_info_tests[] = {
         {"", -1, 0, 0},
@@ -215,8 +205,6 @@ mymain(void)
     }
 
  cleanup:
-    VIR_FREE(poolxml);
-    VIR_FREE(volxml);
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
diff --git a/tests/storagepoolxml2argvtest.c b/tests/storagepoolxml2argvtest.c
index 116a75c3ea..e76e86527f 100644
--- a/tests/storagepoolxml2argvtest.c
+++ b/tests/storagepoolxml2argvtest.c
@@ -88,7 +88,6 @@ testCompareXMLToArgvFiles(bool shouldFail,
     ret = 0;
 
  cleanup:
-    VIR_FREE(actualCmdline);
     virStoragePoolObjEndAPI(&pool);
     if (shouldFail) {
         virResetLastError();
@@ -106,27 +105,20 @@ struct testInfo {
 static int
 testCompareXMLToArgvHelper(const void *data)
 {
-    int result = -1;
     const struct testInfo *info = data;
-    char *poolxml = NULL;
-    char *cmdline = NULL;
+    VIR_AUTOFREE(char *) poolxml = NULL;
+    VIR_AUTOFREE(char *) cmdline = NULL;
 
     if (virAsprintf(&poolxml, "%s/storagepoolxml2xmlin/%s.xml",
                     abs_srcdir, info->pool) < 0)
-        goto cleanup;
+        return -1;
 
     if (virAsprintf(&cmdline, "%s/storagepoolxml2argvdata/%s%s.argv",
                     abs_srcdir, info->pool, info->platformSuffix) < 0 &&
         !info->shouldFail)
-        goto cleanup;
-
-    result = testCompareXMLToArgvFiles(info->shouldFail, poolxml, cmdline);
-
- cleanup:
-    VIR_FREE(poolxml);
-    VIR_FREE(cmdline);
+        return -1;
 
-    return result;
+    return testCompareXMLToArgvFiles(info->shouldFail, poolxml, cmdline);
 }
 
 
diff --git a/tests/storagepoolxml2xmltest.c b/tests/storagepoolxml2xmltest.c
index c8d5c41cd4..bd3408e8b8 100644
--- a/tests/storagepoolxml2xmltest.c
+++ b/tests/storagepoolxml2xmltest.c
@@ -18,47 +18,34 @@
 static int
 testCompareXMLToXMLFiles(const char *inxml, const char *outxml)
 {
-    char *actual = NULL;
-    int ret = -1;
+    VIR_AUTOFREE(char *) actual = NULL;
     VIR_AUTOPTR(virStoragePoolDef) dev = NULL;
 
     if (!(dev = virStoragePoolDefParseFile(inxml)))
-        goto fail;
+        return -1;
 
     if (!(actual = virStoragePoolDefFormat(dev)))
-        goto fail;
+        return -1;
 
     if (virTestCompareToFile(actual, outxml) < 0)
-        goto fail;
+        return -1;
 
-    ret = 0;
-
- fail:
-    VIR_FREE(actual);
-    return ret;
+    return 0;
 }
 
 static int
 testCompareXMLToXMLHelper(const void *data)
 {
-    int result = -1;
-    char *inxml = NULL;
-    char *outxml = NULL;
+    VIR_AUTOFREE(char *) inxml = NULL;
+    VIR_AUTOFREE(char *) outxml = NULL;
 
     if (virAsprintf(&inxml, "%s/storagepoolxml2xmlin/%s.xml",
                     abs_srcdir, (const char*)data) < 0 ||
         virAsprintf(&outxml, "%s/storagepoolxml2xmlout/%s.xml",
-                    abs_srcdir, (const char*)data) < 0) {
-        goto cleanup;
-    }
-
-    result = testCompareXMLToXMLFiles(inxml, outxml);
-
- cleanup:
-    VIR_FREE(inxml);
-    VIR_FREE(outxml);
+                    abs_srcdir, (const char*)data) < 0)
+        return -1;
 
-    return result;
+    return testCompareXMLToXMLFiles(inxml, outxml);
 }
 
 static int
diff --git a/tests/storagevolxml2argvtest.c b/tests/storagevolxml2argvtest.c
index 38bb2ae004..3a4c020f68 100644
--- a/tests/storagevolxml2argvtest.c
+++ b/tests/storagevolxml2argvtest.c
@@ -42,9 +42,9 @@ testCompareXMLToArgvFiles(bool shouldFail,
                           unsigned int flags,
                           unsigned long parse_flags)
 {
-    char *actualCmdline = NULL;
     virStorageVolEncryptConvertStep convertStep = VIR_STORAGE_VOL_ENCRYPT_NONE;
     int ret = -1;
+    VIR_AUTOFREE(char *) actualCmdline = NULL;
     VIR_AUTOPTR(virCommand) cmd = NULL;
     VIR_AUTOPTR(virStorageVolDef) vol = NULL;
     VIR_AUTOPTR(virStorageVolDef) inputvol = NULL;
@@ -109,7 +109,7 @@ testCompareXMLToArgvFiles(bool shouldFail,
                 goto cleanup;
         } else {
             char *createCmdline = actualCmdline;
-            char *cvtCmdline;
+            VIR_AUTOFREE(char *) cvtCmdline = NULL;
             int rc;
 
             if (!(cvtCmdline = virCommandToString(cmd, false)))
@@ -119,7 +119,6 @@ testCompareXMLToArgvFiles(bool shouldFail,
                              createCmdline, cvtCmdline);
 
             VIR_FREE(createCmdline);
-            VIR_FREE(cvtCmdline);
             if (rc < 0)
                 goto cleanup;
         }
@@ -139,7 +138,6 @@ testCompareXMLToArgvFiles(bool shouldFail,
     ret = 0;
 
  cleanup:
-    VIR_FREE(actualCmdline);
     virStoragePoolObjEndAPI(&obj);
     return ret;
 }
@@ -158,45 +156,35 @@ struct testInfo {
 static int
 testCompareXMLToArgvHelper(const void *data)
 {
-    int result = -1;
     const struct testInfo *info = data;
-    char *poolxml = NULL;
-    char *inputpoolxml = NULL;
-    char *volxml = NULL;
-    char *inputvolxml = NULL;
-    char *cmdline = NULL;
+    VIR_AUTOFREE(char *) poolxml = NULL;
+    VIR_AUTOFREE(char *) inputpoolxml = NULL;
+    VIR_AUTOFREE(char *) volxml = NULL;
+    VIR_AUTOFREE(char *) inputvolxml = NULL;
+    VIR_AUTOFREE(char *) cmdline = NULL;
 
     if (info->inputvol &&
         virAsprintf(&inputvolxml, "%s/storagevolxml2xmlin/%s.xml",
                     abs_srcdir, info->inputvol) < 0)
-        goto cleanup;
+        return -1;
     if (info->inputpool &&
         virAsprintf(&inputpoolxml, "%s/storagepoolxml2xmlin/%s.xml",
                     abs_srcdir, info->inputpool) < 0)
-        goto cleanup;
+        return -1;
     if (virAsprintf(&poolxml, "%s/storagepoolxml2xmlin/%s.xml",
                     abs_srcdir, info->pool) < 0 ||
         virAsprintf(&volxml, "%s/storagevolxml2xmlin/%s.xml",
                     abs_srcdir, info->vol) < 0) {
-        goto cleanup;
+        return -1;
     }
     if (virAsprintf(&cmdline, "%s/storagevolxml2argvdata/%s.argv",
                     abs_srcdir, info->cmdline) < 0 && !info->shouldFail)
-        goto cleanup;
-
-    result = testCompareXMLToArgvFiles(info->shouldFail, poolxml, volxml,
-                                       inputpoolxml, inputvolxml,
-                                       cmdline, info->flags,
-                                       info->parseflags);
-
- cleanup:
-    VIR_FREE(poolxml);
-    VIR_FREE(volxml);
-    VIR_FREE(inputvolxml);
-    VIR_FREE(inputpoolxml);
-    VIR_FREE(cmdline);
+        return -1;
 
-    return result;
+    return testCompareXMLToArgvFiles(info->shouldFail, poolxml, volxml,
+                                     inputpoolxml, inputvolxml,
+                                     cmdline, info->flags,
+                                     info->parseflags);
 }
 
 
diff --git a/tests/storagevolxml2xmltest.c b/tests/storagevolxml2xmltest.c
index cb78bd5b28..7c5d8e7e38 100644
--- a/tests/storagevolxml2xmltest.c
+++ b/tests/storagevolxml2xmltest.c
@@ -17,28 +17,23 @@ static int
 testCompareXMLToXMLFiles(const char *poolxml, const char *inxml,
                          const char *outxml, unsigned int flags)
 {
-    char *actual = NULL;
-    int ret = -1;
+    VIR_AUTOFREE(char *) actual = NULL;
     VIR_AUTOPTR(virStoragePoolDef) pool = NULL;
     VIR_AUTOPTR(virStorageVolDef) dev = NULL;
 
     if (!(pool = virStoragePoolDefParseFile(poolxml)))
-        goto fail;
+        return -1;
 
     if (!(dev = virStorageVolDefParseFile(pool, inxml, flags)))
-        goto fail;
+        return -1;
 
     if (!(actual = virStorageVolDefFormat(pool, dev)))
-        goto fail;
+        return -1;
 
     if (virTestCompareToFile(actual, outxml) < 0)
-        goto fail;
+        return -1;
 
-    ret = 0;
-
- fail:
-    VIR_FREE(actual);
-    return ret;
+    return 0;
 }
 
 struct testInfo {
@@ -50,29 +45,20 @@ struct testInfo {
 static int
 testCompareXMLToXMLHelper(const void *data)
 {
-    int result = -1;
     const struct testInfo *info = data;
-    char *poolxml = NULL;
-    char *inxml = NULL;
-    char *outxml = NULL;
+    VIR_AUTOFREE(char *) poolxml = NULL;
+    VIR_AUTOFREE(char *) inxml = NULL;
+    VIR_AUTOFREE(char *) outxml = NULL;
 
     if (virAsprintf(&poolxml, "%s/storagepoolxml2xmlin/%s.xml",
                     abs_srcdir, info->pool) < 0 ||
         virAsprintf(&inxml, "%s/storagevolxml2xmlin/%s.xml",
                     abs_srcdir, info->name) < 0 ||
         virAsprintf(&outxml, "%s/storagevolxml2xmlout/%s.xml",
-                    abs_srcdir, info->name) < 0) {
-        goto cleanup;
-    }
-
-    result = testCompareXMLToXMLFiles(poolxml, inxml, outxml, info->flags);
-
- cleanup:
-    VIR_FREE(poolxml);
-    VIR_FREE(inxml);
-    VIR_FREE(outxml);
+                    abs_srcdir, info->name) < 0)
+        return -1;
 
-    return result;
+    return testCompareXMLToXMLFiles(poolxml, inxml, outxml, info->flags);
 }
 
 
diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c
index c448d8b733..71c371891f 100644
--- a/tests/virstoragetest.c
+++ b/tests/virstoragetest.c
@@ -129,7 +129,7 @@ testPrepImages(void)
 {
     int ret = EXIT_FAILURE;
     VIR_AUTOPTR(virCommand) cmd = NULL;
-    char *buf = NULL;
+    VIR_AUTOFREE(char *) buf = NULL;
     bool compat = false;
 
     qemuimg = virFindFileInPath("qemu-img");
@@ -245,7 +245,6 @@ testPrepImages(void)
 
     ret = 0;
  cleanup:
-    VIR_FREE(buf);
     if (ret)
         testCleanupImages();
     return ret;
@@ -313,7 +312,7 @@ testStorageChain(const void *args)
     virStorageSourcePtr meta;
     virStorageSourcePtr elt;
     size_t i = 0;
-    char *broken = NULL;
+    VIR_AUTOFREE(char *) broken = NULL;
 
     meta = testStorageFileGetMetadata(data->start, data->format, -1, -1);
     if (!meta) {
@@ -349,8 +348,8 @@ testStorageChain(const void *args)
 
     elt = meta;
     while (virStorageSourceIsBacking(elt)) {
-        char *expect = NULL;
-        char *actual = NULL;
+        VIR_AUTOFREE(char *) expect = NULL;
+        VIR_AUTOFREE(char *) actual = NULL;
 
         if (i == data->nfiles) {
             fprintf(stderr, "probed chain was too long\n");
@@ -379,18 +378,12 @@ testStorageChain(const void *args)
                         elt->format,
                         virStorageNetProtocolTypeToString(elt->protocol),
                         NULLSTR(elt->nhosts ? elt->hosts[0].name : NULL)) < 0) {
-            VIR_FREE(expect);
-            VIR_FREE(actual);
             goto cleanup;
         }
         if (STRNEQ(expect, actual)) {
             virTestDifference(stderr, expect, actual);
-            VIR_FREE(expect);
-            VIR_FREE(actual);
             goto cleanup;
         }
-        VIR_FREE(expect);
-        VIR_FREE(actual);
         elt = elt->backingStore;
         i++;
     }
@@ -401,7 +394,6 @@ testStorageChain(const void *args)
 
     ret = 0;
  cleanup:
-    VIR_FREE(broken);
     virStorageSourceFree(meta);
     return ret;
 }
@@ -539,8 +531,7 @@ static int
 testPathCanonicalize(const void *args)
 {
     const struct testPathCanonicalizeData *data = args;
-    char *canon = NULL;
-    int ret = -1;
+    VIR_AUTOFREE(char *) canon = NULL;
 
     canon = virStorageFileCanonicalizePath(data->path,
                                            testPathCanonicalizeReadlink,
@@ -551,15 +542,10 @@ testPathCanonicalize(const void *args)
                 "path canonicalization of '%s' failed: expected '%s' got '%s'\n",
                 data->path, NULLSTR(data->expect), NULLSTR(canon));
 
-        goto cleanup;
+        return -1;
     }
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(canon);
-
-    return ret;
+    return 0;
 }
 
 static virStorageSource backingchain[12];
@@ -629,14 +615,13 @@ static int
 testPathRelative(const void *args)
 {
     const struct testPathRelativeBacking *data = args;
-    char *actual = NULL;
-    int ret = -1;
+    VIR_AUTOFREE(char *) actual = NULL;
 
     if (virStorageFileGetRelativeBackingPath(data->top,
                                              data->base,
                                              &actual) < 0) {
         fprintf(stderr, "relative backing path resolution failed\n");
-        goto cleanup;
+        return -1;
     }
 
     if (STRNEQ_NULLABLE(data->expect, actual)) {
@@ -644,15 +629,10 @@ testPathRelative(const void *args)
                 "expected '%s', got '%s'\n",
                 data->top->path, data->base->path,
                 NULLSTR(data->expect), NULLSTR(actual));
-        goto cleanup;
+        return -1;
     }
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(actual);
-
-    return ret;
+    return 0;
 }
 
 
@@ -667,7 +647,7 @@ testBackingParse(const void *args)
     const struct testBackingParseData *data = args;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     virStorageSourcePtr src = NULL;
-    char *xml = NULL;
+    VIR_AUTOFREE(char *) xml = NULL;
     int ret = -1;
 
     if (!(src = virStorageSourceNewFromBackingAbsolute(data->backing))) {
@@ -702,7 +682,6 @@ testBackingParse(const void *args)
  cleanup:
     virStorageSourceFree(src);
     virBufferFreeAndReset(&buf);
-    VIR_FREE(xml);
 
     return ret;
 }
diff --git a/tests/virstorageutiltest.c b/tests/virstorageutiltest.c
index d91c5d4d6f..766a910975 100644
--- a/tests/virstorageutiltest.c
+++ b/tests/virstorageutiltest.c
@@ -45,8 +45,8 @@ testGlusterExtractPoolSources(const void *opaque)
                                       .sources = NULL
                                     };
     size_t i;
-    char *srcxmldata = NULL;
-    char *actual = NULL;
+    VIR_AUTOFREE(char *) srcxmldata = NULL;
+    VIR_AUTOFREE(char *) actual = NULL;
     int ret = -1;
 
     if (virTestLoadFile(data->srcxml, &srcxmldata) < 0)
@@ -62,9 +62,6 @@ testGlusterExtractPoolSources(const void *opaque)
     ret = virTestCompareToFile(actual, data->dstxml);
 
  cleanup:
-    VIR_FREE(srcxmldata);
-    VIR_FREE(actual);
-
     for (i = 0; i < list.nsources; i++)
         virStoragePoolSourceClear(&list.sources[i]);
     VIR_FREE(list.sources);
-- 
2.20.1




More information about the libvir-list mailing list