Blame SOURCES/libvirt-qemublocktest-xml-json-Refactor-cleanup-in-test-case-functions.patch

fbe740
From 4adce466baf293c7b6c8751f3c295faef85b402e Mon Sep 17 00:00:00 2001
fbe740
Message-Id: <4adce466baf293c7b6c8751f3c295faef85b402e@dist-git>
fbe740
From: Peter Krempa <pkrempa@redhat.com>
fbe740
Date: Tue, 24 Mar 2020 16:25:58 +0100
fbe740
Subject: [PATCH] qemublocktest: xml->json: Refactor cleanup in test case
fbe740
 functions
fbe740
MIME-Version: 1.0
fbe740
Content-Type: text/plain; charset=UTF-8
fbe740
Content-Transfer-Encoding: 8bit
fbe740
fbe740
Use automatic variable clearing and remove the cleanup sections of
fbe740
testQemuDiskXMLToProps, testQemuDiskXMLToPropsValidateSchema and
fbe740
testQemuDiskXMLToPropsValidateFile.
fbe740
fbe740
testQemuDiskXMLToPropsValidateFileSrcOnly already uses new helpers.
fbe740
fbe740
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
fbe740
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
fbe740
(cherry picked from commit 4a9f355535f6af92731cd6e8afaf6cfde2751bf1)
fbe740
https://bugzilla.redhat.com/show_bug.cgi?id=1804617
fbe740
Message-Id: <6f11c48265348f2fa72c51f637bb1339dd9d64ee.1585063415.git.pkrempa@redhat.com>
fbe740
Reviewed-by: Ján Tomko <jtomko@redhat.com>
fbe740
---
fbe740
 tests/qemublocktest.c | 76 +++++++++++++++----------------------------
fbe740
 1 file changed, 27 insertions(+), 49 deletions(-)
fbe740
fbe740
diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c
fbe740
index 189e95f46a..40aa1e2825 100644
fbe740
--- a/tests/qemublocktest.c
fbe740
+++ b/tests/qemublocktest.c
fbe740
@@ -259,39 +259,38 @@ testQemuDiskXMLToProps(const void *opaque)
fbe740
     g_autoptr(virDomainDef) vmdef = NULL;
fbe740
     virDomainDiskDefPtr disk = NULL;
fbe740
     virStorageSourcePtr n;
fbe740
-    virJSONValuePtr formatProps = NULL;
fbe740
-    virJSONValuePtr storageProps = NULL;
fbe740
+    g_autoptr(virJSONValue) formatProps = NULL;
fbe740
+    g_autoptr(virJSONValue) storageProps = NULL;
fbe740
     g_autoptr(virJSONValue) storageSrcOnlyProps = NULL;
fbe740
-    char *xmlpath = NULL;
fbe740
-    char *xmlstr = NULL;
fbe740
-    int ret = -1;
fbe740
+    g_autofree char *xmlpath = NULL;
fbe740
+    g_autofree char *xmlstr = NULL;
fbe740
 
fbe740
     xmlpath = g_strdup_printf("%s%s.xml", testQemuDiskXMLToJSONPath, data->name);
fbe740
 
fbe740
     if (virTestLoadFile(xmlpath, &xmlstr) < 0)
fbe740
-        goto cleanup;
fbe740
+        return -1;
fbe740
 
fbe740
     /* qemu stores node names in the status XML portion */
fbe740
     if (!(disk = virDomainDiskDefParse(xmlstr, data->driver->xmlopt,
fbe740
                                        VIR_DOMAIN_DEF_PARSE_STATUS)))
fbe740
-        goto cleanup;
fbe740
+        return -1;
fbe740
 
fbe740
     if (!(vmdef = virDomainDefNew()) ||
fbe740
         virDomainDiskInsert(vmdef, disk) < 0)
fbe740
-        goto cleanup;
fbe740
+        return -1;
fbe740
 
fbe740
     if (qemuCheckDiskConfig(disk, data->qemuCaps) < 0 ||
fbe740
         qemuDomainDeviceDefValidateDisk(disk, data->qemuCaps) < 0) {
fbe740
         VIR_TEST_VERBOSE("invalid configuration for disk");
fbe740
-        goto cleanup;
fbe740
+        return -1;
fbe740
     }
fbe740
 
fbe740
     for (n = disk->src; virStorageSourceIsBacking(n); n = n->backingStore) {
fbe740
         if (testQemuDiskXMLToJSONFakeSecrets(n) < 0)
fbe740
-            goto cleanup;
fbe740
+            return -1;
fbe740
 
fbe740
         if (qemuDomainValidateStorageSource(n, data->qemuCaps) < 0)
fbe740
-            goto cleanup;
fbe740
+            return -1;
fbe740
 
fbe740
         qemuDomainPrepareDiskSourceData(disk, n);
fbe740
 
fbe740
@@ -300,27 +299,20 @@ testQemuDiskXMLToProps(const void *opaque)
fbe740
             !(storageProps = qemuBlockStorageSourceGetBackendProps(n, false, false, true))) {
fbe740
             if (!data->fail) {
fbe740
                 VIR_TEST_VERBOSE("failed to generate qemu blockdev props");
fbe740
-                goto cleanup;
fbe740
+                return -1;
fbe740
             }
fbe740
         } else if (data->fail) {
fbe740
             VIR_TEST_VERBOSE("qemu blockdev props should have failed");
fbe740
-            goto cleanup;
fbe740
+            return -1;
fbe740
         }
fbe740
 
fbe740
         if (VIR_APPEND_ELEMENT(data->props, data->nprops, formatProps) < 0 ||
fbe740
             VIR_APPEND_ELEMENT(data->props, data->nprops, storageProps) < 0 ||
fbe740
             VIR_APPEND_ELEMENT(data->propssrc, data->npropssrc, storageSrcOnlyProps) < 0)
fbe740
-            goto cleanup;
fbe740
+            return -1;
fbe740
     }
fbe740
 
fbe740
-    ret = 0;
fbe740
-
fbe740
- cleanup:
fbe740
-    virJSONValueFree(formatProps);
fbe740
-    virJSONValueFree(storageProps);
fbe740
-    VIR_FREE(xmlpath);
fbe740
-    VIR_FREE(xmlstr);
fbe740
-    return ret;
fbe740
+    return 0;
fbe740
 }
fbe740
 
fbe740
 
fbe740
@@ -328,9 +320,6 @@ static int
fbe740
 testQemuDiskXMLToPropsValidateSchema(const void *opaque)
fbe740
 {
fbe740
     struct testQemuDiskXMLToJSONData *data = (void *) opaque;
fbe740
-    virBuffer debug = VIR_BUFFER_INITIALIZER;
fbe740
-    char *propsstr = NULL;
fbe740
-    char *debugmsg = NULL;
fbe740
     int ret = 0;
fbe740
     size_t i;
fbe740
 
fbe740
@@ -338,35 +327,31 @@ testQemuDiskXMLToPropsValidateSchema(const void *opaque)
fbe740
         return EXIT_AM_SKIP;
fbe740
 
fbe740
     for (i = 0; i < data->nprops; i++) {
fbe740
+        g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
fbe740
+
fbe740
         if (testQEMUSchemaValidate(data->props[i], data->schemaroot,
fbe740
                                    data->schema, &debug) < 0) {
fbe740
-            debugmsg = virBufferContentAndReset(&debug);
fbe740
-            propsstr = virJSONValueToString(data->props[i], true);
fbe740
+            g_autofree char *debugmsg = virBufferContentAndReset(&debug);
fbe740
+            g_autofree char *propsstr = virJSONValueToString(data->props[i], true);
fbe740
             VIR_TEST_VERBOSE("json does not conform to QAPI schema");
fbe740
             VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
fbe740
                            propsstr, NULLSTR(debugmsg));
fbe740
-            VIR_FREE(debugmsg);
fbe740
-            VIR_FREE(propsstr);
fbe740
             ret = -1;
fbe740
         }
fbe740
-
fbe740
-        virBufferFreeAndReset(&debug);
fbe740
     }
fbe740
 
fbe740
     for (i = 0; i < data->npropssrc; i++) {
fbe740
+        g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
fbe740
+
fbe740
         if (testQEMUSchemaValidate(data->propssrc[i], data->schemaroot,
fbe740
                                    data->schema, &debug) < 0) {
fbe740
-            debugmsg = virBufferContentAndReset(&debug);
fbe740
-            propsstr = virJSONValueToString(data->propssrc[i], true);
fbe740
+            g_autofree char *debugmsg = virBufferContentAndReset(&debug);
fbe740
+            g_autofree char *propsstr = virJSONValueToString(data->propssrc[i], true);
fbe740
             VIR_TEST_VERBOSE("json does not conform to QAPI schema");
fbe740
             VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
fbe740
                            propsstr, NULLSTR(debugmsg));
fbe740
-            VIR_FREE(debugmsg);
fbe740
-            VIR_FREE(propsstr);
fbe740
             ret = -1;
fbe740
         }
fbe740
-
fbe740
-        virBufferFreeAndReset(&debug);
fbe740
     }
fbe740
 
fbe740
     return ret;
fbe740
@@ -378,9 +363,8 @@ testQemuDiskXMLToPropsValidateFile(const void *opaque)
fbe740
 {
fbe740
     struct testQemuDiskXMLToJSONData *data = (void *) opaque;
fbe740
     virBuffer buf = VIR_BUFFER_INITIALIZER;
fbe740
-    char *jsonpath = NULL;
fbe740
-    char *actual = NULL;
fbe740
-    int ret = -1;
fbe740
+    g_autofree char *jsonpath = NULL;
fbe740
+    g_autofree char *actual = NULL;
fbe740
     size_t i;
fbe740
 
fbe740
     if (data->fail)
fbe740
@@ -389,23 +373,17 @@ testQemuDiskXMLToPropsValidateFile(const void *opaque)
fbe740
     jsonpath = g_strdup_printf("%s%s.json", testQemuDiskXMLToJSONPath, data->name);
fbe740
 
fbe740
     for (i = 0; i < data->nprops; i++) {
fbe740
-        char *jsonstr;
fbe740
+        g_autofree char *jsonstr = NULL;
fbe740
 
fbe740
         if (!(jsonstr = virJSONValueToString(data->props[i], true)))
fbe740
-            goto cleanup;
fbe740
+            return -1;
fbe740
 
fbe740
         virBufferAdd(&buf, jsonstr, -1);
fbe740
-        VIR_FREE(jsonstr);
fbe740
     }
fbe740
 
fbe740
     actual = virBufferContentAndReset(&buf;;
fbe740
 
fbe740
-    ret = virTestCompareToFile(actual, jsonpath);
fbe740
-
fbe740
- cleanup:
fbe740
-    VIR_FREE(jsonpath);
fbe740
-    VIR_FREE(actual);
fbe740
-    return ret;
fbe740
+    return virTestCompareToFile(actual, jsonpath);
fbe740
 }
fbe740
 
fbe740
 
fbe740
-- 
fbe740
2.26.0
fbe740