render / rpms / libvirt

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