Blob Blame History Raw
From 4adce466baf293c7b6c8751f3c295faef85b402e Mon Sep 17 00:00:00 2001
Message-Id: <4adce466baf293c7b6c8751f3c295faef85b402e@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Tue, 24 Mar 2020 16:25:58 +0100
Subject: [PATCH] qemublocktest: xml->json: Refactor cleanup in test case
 functions
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Use automatic variable clearing and remove the cleanup sections of
testQemuDiskXMLToProps, testQemuDiskXMLToPropsValidateSchema and
testQemuDiskXMLToPropsValidateFile.

testQemuDiskXMLToPropsValidateFileSrcOnly already uses new helpers.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
(cherry picked from commit 4a9f355535f6af92731cd6e8afaf6cfde2751bf1)
https://bugzilla.redhat.com/show_bug.cgi?id=1804617
Message-Id: <6f11c48265348f2fa72c51f637bb1339dd9d64ee.1585063415.git.pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 tests/qemublocktest.c | 76 +++++++++++++++----------------------------
 1 file changed, 27 insertions(+), 49 deletions(-)

diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c
index 189e95f46a..40aa1e2825 100644
--- a/tests/qemublocktest.c
+++ b/tests/qemublocktest.c
@@ -259,39 +259,38 @@ testQemuDiskXMLToProps(const void *opaque)
     g_autoptr(virDomainDef) vmdef = NULL;
     virDomainDiskDefPtr disk = NULL;
     virStorageSourcePtr n;
-    virJSONValuePtr formatProps = NULL;
-    virJSONValuePtr storageProps = NULL;
+    g_autoptr(virJSONValue) formatProps = NULL;
+    g_autoptr(virJSONValue) storageProps = NULL;
     g_autoptr(virJSONValue) storageSrcOnlyProps = NULL;
-    char *xmlpath = NULL;
-    char *xmlstr = NULL;
-    int ret = -1;
+    g_autofree char *xmlpath = NULL;
+    g_autofree char *xmlstr = NULL;
 
     xmlpath = g_strdup_printf("%s%s.xml", testQemuDiskXMLToJSONPath, data->name);
 
     if (virTestLoadFile(xmlpath, &xmlstr) < 0)
-        goto cleanup;
+        return -1;
 
     /* qemu stores node names in the status XML portion */
     if (!(disk = virDomainDiskDefParse(xmlstr, data->driver->xmlopt,
                                        VIR_DOMAIN_DEF_PARSE_STATUS)))
-        goto cleanup;
+        return -1;
 
     if (!(vmdef = virDomainDefNew()) ||
         virDomainDiskInsert(vmdef, disk) < 0)
-        goto cleanup;
+        return -1;
 
     if (qemuCheckDiskConfig(disk, data->qemuCaps) < 0 ||
         qemuDomainDeviceDefValidateDisk(disk, data->qemuCaps) < 0) {
         VIR_TEST_VERBOSE("invalid configuration for disk");
-        goto cleanup;
+        return -1;
     }
 
     for (n = disk->src; virStorageSourceIsBacking(n); n = n->backingStore) {
         if (testQemuDiskXMLToJSONFakeSecrets(n) < 0)
-            goto cleanup;
+            return -1;
 
         if (qemuDomainValidateStorageSource(n, data->qemuCaps) < 0)
-            goto cleanup;
+            return -1;
 
         qemuDomainPrepareDiskSourceData(disk, n);
 
@@ -300,27 +299,20 @@ testQemuDiskXMLToProps(const void *opaque)
             !(storageProps = qemuBlockStorageSourceGetBackendProps(n, false, false, true))) {
             if (!data->fail) {
                 VIR_TEST_VERBOSE("failed to generate qemu blockdev props");
-                goto cleanup;
+                return -1;
             }
         } else if (data->fail) {
             VIR_TEST_VERBOSE("qemu blockdev props should have failed");
-            goto cleanup;
+            return -1;
         }
 
         if (VIR_APPEND_ELEMENT(data->props, data->nprops, formatProps) < 0 ||
             VIR_APPEND_ELEMENT(data->props, data->nprops, storageProps) < 0 ||
             VIR_APPEND_ELEMENT(data->propssrc, data->npropssrc, storageSrcOnlyProps) < 0)
-            goto cleanup;
+            return -1;
     }
 
-    ret = 0;
-
- cleanup:
-    virJSONValueFree(formatProps);
-    virJSONValueFree(storageProps);
-    VIR_FREE(xmlpath);
-    VIR_FREE(xmlstr);
-    return ret;
+    return 0;
 }
 
 
@@ -328,9 +320,6 @@ static int
 testQemuDiskXMLToPropsValidateSchema(const void *opaque)
 {
     struct testQemuDiskXMLToJSONData *data = (void *) opaque;
-    virBuffer debug = VIR_BUFFER_INITIALIZER;
-    char *propsstr = NULL;
-    char *debugmsg = NULL;
     int ret = 0;
     size_t i;
 
@@ -338,35 +327,31 @@ testQemuDiskXMLToPropsValidateSchema(const void *opaque)
         return EXIT_AM_SKIP;
 
     for (i = 0; i < data->nprops; i++) {
+        g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
+
         if (testQEMUSchemaValidate(data->props[i], data->schemaroot,
                                    data->schema, &debug) < 0) {
-            debugmsg = virBufferContentAndReset(&debug);
-            propsstr = virJSONValueToString(data->props[i], true);
+            g_autofree char *debugmsg = virBufferContentAndReset(&debug);
+            g_autofree char *propsstr = virJSONValueToString(data->props[i], true);
             VIR_TEST_VERBOSE("json does not conform to QAPI schema");
             VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
                            propsstr, NULLSTR(debugmsg));
-            VIR_FREE(debugmsg);
-            VIR_FREE(propsstr);
             ret = -1;
         }
-
-        virBufferFreeAndReset(&debug);
     }
 
     for (i = 0; i < data->npropssrc; i++) {
+        g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
+
         if (testQEMUSchemaValidate(data->propssrc[i], data->schemaroot,
                                    data->schema, &debug) < 0) {
-            debugmsg = virBufferContentAndReset(&debug);
-            propsstr = virJSONValueToString(data->propssrc[i], true);
+            g_autofree char *debugmsg = virBufferContentAndReset(&debug);
+            g_autofree char *propsstr = virJSONValueToString(data->propssrc[i], true);
             VIR_TEST_VERBOSE("json does not conform to QAPI schema");
             VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
                            propsstr, NULLSTR(debugmsg));
-            VIR_FREE(debugmsg);
-            VIR_FREE(propsstr);
             ret = -1;
         }
-
-        virBufferFreeAndReset(&debug);
     }
 
     return ret;
@@ -378,9 +363,8 @@ testQemuDiskXMLToPropsValidateFile(const void *opaque)
 {
     struct testQemuDiskXMLToJSONData *data = (void *) opaque;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
-    char *jsonpath = NULL;
-    char *actual = NULL;
-    int ret = -1;
+    g_autofree char *jsonpath = NULL;
+    g_autofree char *actual = NULL;
     size_t i;
 
     if (data->fail)
@@ -389,23 +373,17 @@ testQemuDiskXMLToPropsValidateFile(const void *opaque)
     jsonpath = g_strdup_printf("%s%s.json", testQemuDiskXMLToJSONPath, data->name);
 
     for (i = 0; i < data->nprops; i++) {
-        char *jsonstr;
+        g_autofree char *jsonstr = NULL;
 
         if (!(jsonstr = virJSONValueToString(data->props[i], true)))
-            goto cleanup;
+            return -1;
 
         virBufferAdd(&buf, jsonstr, -1);
-        VIR_FREE(jsonstr);
     }
 
     actual = virBufferContentAndReset(&buf);
 
-    ret = virTestCompareToFile(actual, jsonpath);
-
- cleanup:
-    VIR_FREE(jsonpath);
-    VIR_FREE(actual);
-    return ret;
+    return virTestCompareToFile(actual, jsonpath);
 }
 
 
-- 
2.26.0