render / rpms / libvirt

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