]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
testQemuAgentSSHKeys: Refactor cleanup
authorPeter Krempa <pkrempa@redhat.com>
Mon, 14 Jun 2021 11:51:17 +0000 (13:51 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Mon, 9 Aug 2021 08:09:00 +0000 (10:09 +0200)
Use automatic memory freeing for the 'qemuMonitorTest' object and the
list of keys so that the cleanup section can be removed.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
tests/qemuagenttest.c

index e0d2575c458781e5c8a7b3fdfb072fb6cbf59a52..a447c934940722b0a9c8ccd69da7b8c5c99c0959 100644 (file)
@@ -39,16 +39,15 @@ static int
 testQemuAgentSSHKeys(const void *data)
 {
     virDomainXMLOption *xmlopt = (virDomainXMLOption *)data;
-    qemuMonitorTest *test = qemuMonitorTestNewAgent(xmlopt);
-    char **keys = NULL;
+    g_autoptr(qemuMonitorTest) test = qemuMonitorTestNewAgent(xmlopt);
+    g_auto(GStrv) keys = NULL;
     int nkeys = 0;
-    int ret = -1;
 
     if (!test)
         return -1;
 
     if (qemuMonitorTestAddAgentSyncResponse(test) < 0)
-        goto cleanup;
+        return -1;
 
     if (qemuMonitorTestAddItem(test, "guest-ssh-get-authorized-keys",
                                "{\"return\": {"
@@ -57,59 +56,52 @@ testQemuAgentSSHKeys(const void *data)
                                "    \"algo2 key2 comments2\""
                                "  ]"
                                "}}") < 0)
-        goto cleanup;
+        return -1;
 
     if (qemuMonitorTestAddAgentSyncResponse(test) < 0)
-        goto cleanup;
+        return -1;
 
     if (qemuMonitorTestAddItem(test, "guest-ssh-add-authorized-keys",
                                "{ \"return\" : {} }") < 0)
-        goto cleanup;
+        return -1;
 
     if (qemuMonitorTestAddAgentSyncResponse(test) < 0)
-        goto cleanup;
+        return -1;
 
     if (qemuMonitorTestAddItem(test, "guest-ssh-remove-authorized-keys",
                                "{ \"return\" : {} }") < 0)
-        goto cleanup;
+        return -1;
 
     if ((nkeys = qemuAgentSSHGetAuthorizedKeys(qemuMonitorTestGetAgent(test),
                                                "user",
                                                &keys)) < 0)
-        goto cleanup;
+        return -1;
 
     if (nkeys != 2) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        "expected 2 keys, got %d", nkeys);
-        ret = -1;
-        goto cleanup;
+        return -1;
     }
 
     if (STRNEQ(keys[1], "algo2 key2 comments2")) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "Unexpected key returned: %s", keys[1]);
-        ret = -1;
-        goto cleanup;
+        return -1;
     }
 
-    if ((ret = qemuAgentSSHAddAuthorizedKeys(qemuMonitorTestGetAgent(test),
-                                             "user",
-                                             (const char **) keys,
-                                             nkeys,
-                                             true)) < 0)
-        goto cleanup;
-
-    if ((ret = qemuAgentSSHRemoveAuthorizedKeys(qemuMonitorTestGetAgent(test),
-                                                "user",
-                                                (const char **) keys,
-                                                nkeys)) < 0)
-        goto cleanup;
+    if (qemuAgentSSHAddAuthorizedKeys(qemuMonitorTestGetAgent(test),
+                                      "user",
+                                      (const char **) keys,
+                                      nkeys,
+                                      true) < 0)
+        return -1;
 
-    ret = 0;
+    if (qemuAgentSSHRemoveAuthorizedKeys(qemuMonitorTestGetAgent(test),
+                                         "user",
+                                         (const char **) keys,
+                                         nkeys) < 0)
+        return -1;
 
- cleanup:
-    virStringListFreeCount(keys, nkeys);
-    qemuMonitorTestFree(test);
-    return ret;
+    return 0;
 }