[PATCH 5/7] tests: Use g_autoptr(qemuMonitorTest)

Michal Privoznik mprivozn at redhat.com
Mon Nov 1 14:16:17 UTC 2021


Instead of calling qemuMonitorTestFree() explicitly, we can use
g_autoptr() and let it be called automagically.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 tests/qemuagenttest.c        | 46 ++++++++++++------------------------
 tests/qemucapabilitiestest.c |  3 +--
 tests/qemuhotplugtest.c      |  3 +--
 tests/qemumigparamstest.c    |  6 ++---
 tests/qemumonitorjsontest.c  | 23 +++++++++---------
 tests/qemumonitortestutils.c | 25 ++++++++------------
 6 files changed, 41 insertions(+), 65 deletions(-)

diff --git a/tests/qemuagenttest.c b/tests/qemuagenttest.c
index cebeb5733e..402c290056 100644
--- a/tests/qemuagenttest.c
+++ b/tests/qemuagenttest.c
@@ -109,7 +109,7 @@ static int
 testQemuAgentFSFreeze(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = qemuMonitorTestNewAgent(xmlopt);
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
     const char *mountpoints[] = {"/fs1", "/fs2", "/fs3", "/fs4", "/fs5"};
     int ret = -1;
 
@@ -152,7 +152,6 @@ testQemuAgentFSFreeze(const void *data)
     ret = 0;
 
  cleanup:
-    qemuMonitorTestFree(test);
     return ret;
 }
 
@@ -161,7 +160,7 @@ static int
 testQemuAgentFSThaw(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = qemuMonitorTestNewAgent(xmlopt);
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
     int ret = -1;
 
     if (!test)
@@ -202,7 +201,6 @@ testQemuAgentFSThaw(const void *data)
     ret = 0;
 
  cleanup:
-    qemuMonitorTestFree(test);
     return ret;
 }
 
@@ -211,7 +209,7 @@ static int
 testQemuAgentFSTrim(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = qemuMonitorTestNewAgent(xmlopt);
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
     int ret = -1;
 
     if (!test)
@@ -232,7 +230,6 @@ testQemuAgentFSTrim(const void *data)
     ret = 0;
 
  cleanup:
-    qemuMonitorTestFree(test);
     return ret;
 }
 
@@ -244,7 +241,7 @@ testQemuAgentGetFSInfoCommon(virDomainXMLOption *xmlopt,
 {
     int ret = -1;
     g_autofree char *domain_filename = NULL;
-    qemuMonitorTest *ret_test = NULL;
+    g_autoptr(qemuMonitorTest) ret_test = NULL;
     g_autoptr(virDomainDef) ret_def = NULL;
 
     if (!test || !def)
@@ -303,8 +300,6 @@ testQemuAgentGetFSInfoCommon(virDomainXMLOption *xmlopt,
     ret = 0;
 
  cleanup:
-    if (ret_test)
-        qemuMonitorTestFree(ret_test);
     return ret;
 }
 
@@ -312,7 +307,7 @@ static int
 testQemuAgentGetFSInfo(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = NULL;
+    g_autoptr(qemuMonitorTest) test = NULL;
     g_autoptr(virDomainDef) def = NULL;
     qemuAgentFSInfo **info = NULL;
     int ret = -1, ninfo = 0, i;
@@ -405,7 +400,6 @@ testQemuAgentGetFSInfo(const void *data)
     for (i = 0; i < ninfo; i++)
         qemuAgentFSInfoFree(info[i]);
     VIR_FREE(info);
-    qemuMonitorTestFree(test);
     return ret;
 }
 
@@ -413,7 +407,7 @@ static int
 testQemuAgentSuspend(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = qemuMonitorTestNewAgent(xmlopt);
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
     int ret = -1;
     size_t i;
 
@@ -450,7 +444,6 @@ testQemuAgentSuspend(const void *data)
     ret = 0;
 
  cleanup:
-    qemuMonitorTestFree(test);
     return ret;
 }
 
@@ -511,7 +504,7 @@ static int
 testQemuAgentShutdown(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = qemuMonitorTestNewAgent(xmlopt);
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
     struct qemuAgentShutdownTestData priv;
     int ret = -1;
 
@@ -590,7 +583,6 @@ testQemuAgentShutdown(const void *data)
     ret = 0;
 
  cleanup:
-    qemuMonitorTestFree(test);
     return ret;
 }
 
@@ -631,7 +623,7 @@ static int
 testQemuAgentCPU(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = qemuMonitorTestNewAgent(xmlopt);
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
     qemuAgentCPUInfo *cpuinfo = NULL;
     int nvcpus;
     int ret = -1;
@@ -703,7 +695,6 @@ testQemuAgentCPU(const void *data)
 
  cleanup:
     VIR_FREE(cpuinfo);
-    qemuMonitorTestFree(test);
     return ret;
 }
 
@@ -715,7 +706,7 @@ static int
 testQemuAgentArbitraryCommand(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = qemuMonitorTestNewAgent(xmlopt);
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
     int ret = -1;
     g_autofree char *reply = NULL;
 
@@ -746,7 +737,6 @@ testQemuAgentArbitraryCommand(const void *data)
     ret = 0;
 
  cleanup:
-    qemuMonitorTestFree(test);
     return ret;
 }
 
@@ -764,7 +754,7 @@ static int
 testQemuAgentTimeout(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = qemuMonitorTestNewAgent(xmlopt);
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
     g_autofree char *reply = NULL;
     int ret = -1;
 
@@ -809,7 +799,6 @@ testQemuAgentTimeout(const void *data)
     ret = 0;
 
  cleanup:
-    qemuMonitorTestFree(test);
     return ret;
 }
 
@@ -890,7 +879,7 @@ static int
 testQemuAgentGetInterfaces(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = qemuMonitorTestNewAgent(xmlopt);
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
     size_t i;
     int ret = -1;
     int ifaces_count = 0;
@@ -991,7 +980,6 @@ testQemuAgentGetInterfaces(const void *data)
     ret = 0;
 
  cleanup:
-    qemuMonitorTestFree(test);
     if (ifaces) {
         for (i = 0; i < ifaces_count; i++)
             virDomainInterfaceFree(ifaces[i]);
@@ -1039,7 +1027,7 @@ static int
 testQemuAgentGetDisks(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = qemuMonitorTestNewAgent(xmlopt);
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
     size_t i;
     int ret = -1;
     int disks_count = 0;
@@ -1100,7 +1088,6 @@ testQemuAgentGetDisks(const void *data)
     ret = 0;
 
  cleanup:
-    qemuMonitorTestFree(test);
     if (disks) {
         for (i = 0; i < disks_count; i++)
             qemuAgentDiskInfoFree(disks[i]);
@@ -1189,7 +1176,7 @@ static int
 testQemuAgentUsers(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = qemuMonitorTestNewAgent(xmlopt);
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
     virTypedParameterPtr params = NULL;
     int nparams = 0;
     int maxparams = 0;
@@ -1255,7 +1242,6 @@ testQemuAgentUsers(const void *data)
 
  cleanup:
     virTypedParamsFree(params, nparams);
-    qemuMonitorTestFree(test);
     return ret;
 }
 
@@ -1289,7 +1275,7 @@ static int
 testQemuAgentOSInfo(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = qemuMonitorTestNewAgent(xmlopt);
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
     virTypedParameterPtr params = NULL;
     int nparams = 0;
     int maxparams = 0;
@@ -1375,7 +1361,6 @@ testQemuAgentOSInfo(const void *data)
     ret = 0;
 
  cleanup:
-    qemuMonitorTestFree(test);
     virTypedParamsFree(params, nparams);
     return ret;
 }
@@ -1393,7 +1378,7 @@ static int
 testQemuAgentTimezone(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = qemuMonitorTestNewAgent(xmlopt);
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
     virTypedParameterPtr params = NULL;
     int nparams = 0;
     int ret = -1;
@@ -1455,7 +1440,6 @@ testQemuAgentTimezone(const void *data)
     ret = 0;
 
  cleanup:
-    qemuMonitorTestFree(test);
     virTypedParamsFree(params, nparams);
     return ret;
 }
diff --git a/tests/qemucapabilitiestest.c b/tests/qemucapabilitiestest.c
index b495bfc07d..b866818e0a 100644
--- a/tests/qemucapabilitiestest.c
+++ b/tests/qemucapabilitiestest.c
@@ -72,7 +72,7 @@ testQemuCaps(const void *opaque)
     testQemuData *data = (void *) opaque;
     g_autofree char *repliesFile = NULL;
     g_autofree char *capsFile = NULL;
-    qemuMonitorTest *mon = NULL;
+    g_autoptr(qemuMonitorTest) mon = NULL;
     g_autoptr(virQEMUCaps) capsActual = NULL;
     g_autofree char *binary = NULL;
     g_autofree char *actual = NULL;
@@ -132,7 +132,6 @@ testQemuCaps(const void *opaque)
 
     ret = 0;
  cleanup:
-    qemuMonitorTestFree(mon);
     return ret;
 }
 
diff --git a/tests/qemuhotplugtest.c b/tests/qemuhotplugtest.c
index ec448da09e..ac2cf9bb65 100644
--- a/tests/qemuhotplugtest.c
+++ b/tests/qemuhotplugtest.c
@@ -255,7 +255,7 @@ testQemuHotplug(const void *data)
     virDomainObj *vm = NULL;
     virDomainDeviceDef *dev = NULL;
     g_autoptr(virCaps) caps = NULL;
-    qemuMonitorTest *test_mon = NULL;
+    g_autoptr(qemuMonitorTest) test_mon = NULL;
     qemuDomainObjPrivate *priv = NULL;
 
     domain_filename = g_strdup_printf("%s/qemuhotplugtestdomains/qemuhotplug-%s.xml",
@@ -359,7 +359,6 @@ testQemuHotplug(const void *data)
         test->vm = NULL;
     }
     virDomainDeviceDefFree(dev);
-    qemuMonitorTestFree(test_mon);
     return ((ret < 0 && fail) || (!ret && !fail)) ? 0 : -1;
 }
 
diff --git a/tests/qemumigparamstest.c b/tests/qemumigparamstest.c
index 4ab40d9d2e..7d677e2b3a 100644
--- a/tests/qemumigparamstest.c
+++ b/tests/qemumigparamstest.c
@@ -96,7 +96,7 @@ qemuMigParamsTestXML(const void *opaque)
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
     g_autofree char *replyFile = NULL;
     g_autofree char *xmlFile = NULL;
-    qemuMonitorTest *mon = NULL;
+    g_autoptr(qemuMonitorTest) mon = NULL;
     g_autoptr(virJSONValue) params = NULL;
     g_autoptr(qemuMigrationParams) migParams = NULL;
     g_autofree char *actualXML = NULL;
@@ -128,7 +128,6 @@ qemuMigParamsTestXML(const void *opaque)
     ret = 0;
 
  cleanup:
-    qemuMonitorTestFree(mon);
     return ret;
 }
 
@@ -139,7 +138,7 @@ qemuMigParamsTestJSON(const void *opaque)
     const qemuMigParamsData *data = opaque;
     g_autofree char *replyFile = NULL;
     g_autofree char *jsonFile = NULL;
-    qemuMonitorTest *mon = NULL;
+    g_autoptr(qemuMonitorTest) mon = NULL;
     g_autoptr(virJSONValue) paramsIn = NULL;
     g_autoptr(virJSONValue) paramsOut = NULL;
     g_autoptr(qemuMigrationParams) migParams = NULL;
@@ -184,7 +183,6 @@ qemuMigParamsTestJSON(const void *opaque)
     ret = 0;
 
  cleanup:
-    qemuMonitorTestFree(mon);
     return ret;
 }
 
diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c
index bdf557656b..e51d6768d5 100644
--- a/tests/qemumonitorjsontest.c
+++ b/tests/qemumonitorjsontest.c
@@ -650,10 +650,14 @@ qemuMonitorJSONTestAttachOneChardev(virDomainXMLOption *xmlopt,
 
 {
     struct qemuMonitorJSONTestAttachChardevData data = {0};
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewSchema(xmlopt, schema);
     g_autofree char *jsonreply = NULL;
     g_autofree char *fulllabel = NULL;
     int ret = -1;
 
+    if (!test)
+        goto cleanup;
+
     if (!reply)
         reply = "";
 
@@ -661,17 +665,16 @@ qemuMonitorJSONTestAttachOneChardev(virDomainXMLOption *xmlopt,
 
     fulllabel = g_strdup_printf("qemuMonitorJSONTestAttachChardev(%s)", label);
 
+    qemuMonitorTestAllowUnusedCommands(test);
+
+    if (qemuMonitorTestAddItemExpect(test, "chardev-add",
+                                     expectargs, true, jsonreply) < 0)
+        goto cleanup;
+
     data.chr = chr;
     data.fail = fail;
     data.expectPty = expectPty;
-    if (!(data.test = qemuMonitorTestNewSchema(xmlopt, schema)))
-        goto cleanup;
-
-    qemuMonitorTestAllowUnusedCommands(data.test);
-
-    if (qemuMonitorTestAddItemExpect(data.test, "chardev-add",
-                                     expectargs, true, jsonreply) < 0)
-        goto cleanup;
+    data.test = test;
 
     if (virTestRun(fulllabel, &testQemuMonitorJSONAttachChardev, &data) < 0)
         goto cleanup;
@@ -679,7 +682,6 @@ qemuMonitorJSONTestAttachOneChardev(virDomainXMLOption *xmlopt,
     ret = 0;
 
  cleanup:
-    qemuMonitorTestFree(data.test);
     return ret;
 }
 
@@ -2656,7 +2658,7 @@ static int
 testQueryJobs(const void *opaque)
 {
     const struct testQueryJobsData *data = opaque;
-    qemuMonitorTest *test = qemuMonitorTestNewSimple(data->xmlopt);
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewSimple(data->xmlopt);
     g_autofree char *filenameJSON = NULL;
     g_autofree char *fileJSON = NULL;
     g_autofree char *filenameResult = NULL;
@@ -2701,7 +2703,6 @@ testQueryJobs(const void *opaque)
     for (i = 0; i < njobs; i++)
         qemuMonitorJobInfoFree(jobs[i]);
     VIR_FREE(jobs);
-    qemuMonitorTestFree(test);
     return ret;
 }
 
diff --git a/tests/qemumonitortestutils.c b/tests/qemumonitortestutils.c
index 75a6a76b92..2c63e95bda 100644
--- a/tests/qemumonitortestutils.c
+++ b/tests/qemumonitortestutils.c
@@ -996,7 +996,7 @@ qemuMonitorCommonTestNew(virDomainXMLOption *xmlopt,
                          virDomainObj *vm,
                          virDomainChrSourceDef *src)
 {
-    qemuMonitorTest *test = NULL;
+    g_autoptr(qemuMonitorTest) test = NULL;
     char *path = NULL;
     char *tmpdir_template = NULL;
 
@@ -1044,12 +1044,11 @@ qemuMonitorCommonTestNew(virDomainXMLOption *xmlopt,
     if (virNetSocketListen(test->server, 1) < 0)
         goto error;
 
-    return test;
+    return g_steal_pointer(&test);
 
  error:
     VIR_FREE(path);
     VIR_FREE(tmpdir_template);
-    qemuMonitorTestFree(test);
     return NULL;
 
 }
@@ -1116,7 +1115,7 @@ qemuMonitorTestNew(virDomainXMLOption *xmlopt,
                    const char *greeting,
                    GHashTable *schema)
 {
-    qemuMonitorTest *test = NULL;
+    g_autoptr(qemuMonitorTest) test = NULL;
     virDomainChrSourceDef src;
 
     memset(&src, 0, sizeof(src));
@@ -1150,11 +1149,10 @@ qemuMonitorTestNew(virDomainXMLOption *xmlopt,
 
     virDomainChrSourceDefClear(&src);
 
-    return test;
+    return g_steal_pointer(&test);
 
  error:
     virDomainChrSourceDefClear(&src);
-    qemuMonitorTestFree(test);
     return NULL;
 }
 
@@ -1177,7 +1175,7 @@ qemuMonitorTestNewFromFile(const char *fileName,
                            virDomainXMLOption *xmlopt,
                            bool simple)
 {
-    qemuMonitorTest *test = NULL;
+    g_autoptr(qemuMonitorTest) test = NULL;
     g_autofree char *json = NULL;
     char *tmp;
     char *singleReply;
@@ -1226,10 +1224,9 @@ qemuMonitorTestNewFromFile(const char *fileName,
     if (test && qemuMonitorTestAddItem(test, NULL, singleReply) < 0)
         goto error;
 
-    return test;
+    return g_steal_pointer(&test);
 
  error:
-    qemuMonitorTestFree(test);
     return NULL;
 }
 
@@ -1311,7 +1308,7 @@ qemuMonitorTestNewFromFileFull(const char *fileName,
                                virDomainObj *vm,
                                GHashTable *qmpschema)
 {
-    qemuMonitorTest *ret = NULL;
+    g_autoptr(qemuMonitorTest) ret = NULL;
     g_autofree char *jsonstr = NULL;
     char *tmp;
     size_t line = 0;
@@ -1377,10 +1374,9 @@ qemuMonitorTestNewFromFileFull(const char *fileName,
             goto error;
     }
 
-    return ret;
+    return g_steal_pointer(&ret);
 
  error:
-    qemuMonitorTestFree(ret);
     return NULL;
 }
 
@@ -1388,7 +1384,7 @@ qemuMonitorTestNewFromFileFull(const char *fileName,
 qemuMonitorTest *
 qemuMonitorTestNewAgent(virDomainXMLOption *xmlopt)
 {
-    qemuMonitorTest *test = NULL;
+    g_autoptr(qemuMonitorTest) test = NULL;
     virDomainChrSourceDef src;
 
     memset(&src, 0, sizeof(src));
@@ -1413,11 +1409,10 @@ qemuMonitorTestNewAgent(virDomainXMLOption *xmlopt)
 
     virDomainChrSourceDefClear(&src);
 
-    return test;
+    return g_steal_pointer(&test);
 
  error:
     virDomainChrSourceDefClear(&src);
-    qemuMonitorTestFree(test);
     return NULL;
 }
 
-- 
2.32.0




More information about the libvir-list mailing list